A small utility for managing an undo stack

Related tags

Vue.js svelte-undo
Overview

svelte-undo

A small utility for managing an undo stack, that you can subscribe to in your Svelte applications (or indeed anywhere else).

Demo here.

Usage

import { createStack } from 'svelte-undo';

let value = { answer: 42 };

const stack = createStack(value);

// undo/redo have no effect if we're at the
// beginning/end of the stack
console.log((value = stack.undo())); // { answer: 42 }
console.log((value = stack.redo())); // { answer: 42 }

// stack.push returns the new value
value = stack.push({ answer: 43 });
value = stack.push({ answer: 44 });
value = stack.push({ answer: 45 });

console.log(value); // { answer: 45 }

console.log((value = stack.undo())); // { answer: 44 }
console.log((value = stack.undo())); // { answer: 43 }
console.log((value = stack.undo())); // { answer: 42 }
console.log((value = stack.undo())); // { answer: 42 }

console.log((value = stack.redo())); // { answer: 43 }

// pushing clears anything 'forward' in the stack
value = stack.push({ answer: 99 });

// you can also pass a function to `push`
value = stack.push((value) => ({ answer: value.answer + 1 }));

console.log((value = stack.undo())); // { answer: 99 }
console.log((value = stack.undo())); // { answer: 43 }
console.log((value = stack.redo())); // { answer: 99 }
console.log((value = stack.redo())); // { answer: 100 }
console.log((value = stack.redo())); // { answer: 100 }

// you can subscribe to the state of the undo stack
const unsubscribe = stack.subscribe(($stack) => {
	console.log($stack.first); // false
	console.log($stack.last); // true — we're currently at the end of the stack
	console.log($stack.current); // { answer: 99 }
});

unsubscribe();

In a Svelte component, you can reference first and last as store properties without manually subscribing:

<button disabled={$stack.first} on:click={() => value = stack.undo()}>Undo</button>
<button disabled={$stack.last} on:click={() => value = stack.redo()}>Redo</button>

You can also access the current value of the stack, if that's preferable to tracking the value manually:

<h1>The answer is {$stack.current.answer}</h1>

Don't mutate the objects you push to the undo stack; chaos will result. Instead, create a fresh copy each time, either manually or using something like Immer.

License

MIT

You might also like...

A small javascript DOM manipulation library based on Jquery's syntax. Acts as a small utility library with the most common functions.

Quantdom JS Quantdom is a very small (about 600 bytes when ran through terser & gzipped) dom danipulation library that uuses a Jquery like syntax and

Aug 16, 2022

This is a full-stack exercise tracker web application built using the MERN (MongoDB, ExpressJS, ReactJS, NodeJS) stack. You can easily track your exercises with this Full-Stack Web Application.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Dec 25, 2021

A utility for creating toggleable items with JavaScript. Inspired by bootstrap's toggle utility. Implemented in vanillaJS in a functional style.

LUX TOGGLE Demo: https://jesschampion.github.io/lux-toggle/ A utility for creating toggleable dom elements with JavaScript. Inspired by bootstrap's to

Oct 3, 2020

Small utility for react-redux's `useSelector` that allows passing args.

use-selector-with Monorepo for the following two packages: use-selector-with: Small utility for react-redux's useSelector that allows passing args. es

Jan 28, 2022

A small CLI utility to configure Japa inside an existing Node.js project

A small CLI utility to configure Japa inside an existing Node.js project

Mar 11, 2022

An utility library for Encoding and Compressing whatever you give him into a small string.

🟢 ORBIT ENCODER ⚙️ 🗜️ ORBIT-ENCODER is an utility library for Data Compression and Encoding. It can take whatever object you give him as argument an

Nov 16, 2022

A small utility server to exchange data and messages between clients. Comes complete with E2E public key encryption

Zenotta Intercom A small utility server to exchange arbitrary data between clients. Comes complete with E2E public key encryption Official documentati

Oct 2, 2022

✏️ A small jQuery extension to turn a static HTML table into an editable one. For quickly populating a small table with JSON data, letting the user modify it with validation, and then getting JSON data back out.

jquery-editable-table A small jQuery extension to turn an HTML table editable for fast data entry and validation Demo 👉 https://jsfiddle.net/torrobin

Jul 31, 2022

Full Stack MERN website for local small business "Daisy's Cakes"

Daisys-Cakes Full Stack MERN website for local small business "Daisy's Cakes" Contribution Info This section will be deleted later when the project is

Nov 13, 2022

Long stack traces for node.js inspired by https://github.com/tlrobinson/long-stack-traces

longjohn Long stack traces for node.js with configurable call trace length Inspiration I wrote this while trying to add long-stack-traces to my server

Dec 23, 2022

This is MERN Stack Ecommerce Project Made to Teach MERN Stack on YouTube

MERN E-COMMERCE TUTORIAL Hi! My name is Abhishek Singh, I have created this tutorial to teach MERN Stack for free on YouTube. Prerequisite Must have b

Jan 5, 2023

Online Inventory Control System for an apparel manufacturing company "CASANOVA" (Pvt) Ltd. Technology stack: Node.js, Express.js, MongoDB Atlas, React.js (MERN Stack).

Project Name - Online Inventory Control System for an apparel manufacturing company "CASANOVA". The project was given a "A" grade. Group Leader - IT20

Dec 26, 2021

Full-Stack Instgram Clone using MERN Stack and Socket.io

Full-Stack Instgram Clone using MERN Stack and Socket.io

Instagram MERN Full-Stack Instgram Clone using MERN Stack and Socket.io Visit Now 🚀 🖥️ Tech Stack Frontend: Backend: Realtime Communication: Cloud S

Dec 27, 2022

It is a solo Project and In this repo I try to build a E-Commerce full-stack website with MERN stack technologies. For Practice purpose.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Aug 3, 2022

Catamyst Frontend Stack - Discussion and demo for Frontend Web Development with Modern Tech Stack

Catamyst Frontend Stack Discussion and demo for Frontend Web Development with Modern Tech Stack. Made by M Haidar Hanif from Catamyst. Haidar is the F

Dec 24, 2022

A technology stack solution using the AWS Serverless architecture.Atlas stack for building applications focused on generating value.

Atlas A technology stack solution using the AWS Serverless architecture.Atlas stack for building applications focused on generating value. Description

Dec 15, 2022

🔥 Dreamy-db - A Powerful database for storing, accessing, and managing multiple database.

🔥 Dreamy-db - A Powerful database for storing, accessing, and managing multiple database.

Dreamy-db About Dreamy-db - A Powerful database for storing, accessing, and managing multiple databases. A powerful node.js module that allows you to

Dec 22, 2022

🚀 The ultimate library for managing multi-channel notifications with a single API.

🚀 The ultimate library for managing multi-channel notifications with a single API.

🚀 The ultimate library for managing multi-channel notifications with a single API.

Jan 4, 2023

A dashboard for managing orders and inventory for a wordpress e-commerce site which has woo commerce plugin installed

A dashboard for managing orders and inventory for a wordpress e-commerce site which has woo commerce plugin installed

WordPressWooCommerceDashboard - A dashboard for managing orders and inventory for a wordpress e-commerce site which has woo commerce plugin installed. This program provides shipping tracking for Delhivery.

Jan 3, 2022
Comments
  • Wrong answer value in README

    Wrong answer value in README

    // you can subscribe to the state of the undo stack
    const unsubscribe = stack.subscribe(($stack) => {
    	console.log($stack.first); // false
    	console.log($stack.last); // true — we're currently at the end of the stack
    	console.log($stack.current); // { answer: 99 }
    });
    

    answer should be 100 in console.log($stack.current)

    opened by JVariance 0
Owner
Rich Harris
Rich Harris
Small library to accomplish common tasks in Svelte/SvelteKit

svelte-utilities svelte-utilities is a small utilities library for Svelte/SvelteKit that provides features I use in almost every project, such as: Laz

null 40 Nov 25, 2022
:bento: Full-Stack solution to quickly build PWA applications with Vue.js and Firebase

Welcome to bento-starter ?? ?? bento-starter is an Open-Source Full-Stack solution that helps you to build fast and maintainable web applications usin

Franck Abgrall 1.5k Dec 24, 2022
demo for full-stack ci/cd

vue + nestjs (TypeScript) Features CI/CD full-stack mutiple-env (test: http://81.70.221.165:8026 / production: http://81.70.221.165) continuing~ welco

Evan Zuo 3 Oct 24, 2022
Modern Vue Stack 2022. Joyful development experience 🎉

Modern Vue Modern Vue stack 2022 with Micro front end & Monorepo ?? . Joyful development experience ?? . The main branch will keep clean for quickly c

han 263 Dec 18, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
An enchanced useState hook which keeps track of the states history, allowing you to undo and redo states.

useTimeline An enchanced useState hook which keeps track of the states history, allowing you to undo and redo states. useTimeline is a simple hook bas

null 13 Apr 22, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
A powerful and simple JavaScript library provides a history for undo/redo functionality. Just like a time machine! 🕐

UndoRedo.js A powerful and simple Javascript library provides a history for undo/redo functionality. Just like a time machine! ?? Installation: Node.j

${Mr.DJA} 26 Dec 14, 2022