Skip to content

Commit

Permalink
Add undo/redo stack size readable Svelte store
Browse files Browse the repository at this point in the history
  • Loading branch information
canadaduane committed Oct 17, 2020
1 parent 4eb2979 commit b807133
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
6 changes: 5 additions & 1 deletion example/src/ImageButton.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export let icon;
export let alt;
export let disabled = false;
const dispatch = createEventDispatcher();
</script>
Expand All @@ -21,6 +22,9 @@
margin: 8px;
}
.disabled {
opacity: 0.5;
}
button:active {
background-color: var(--medium);
border: 1px solid var(--dark);
Expand All @@ -33,7 +37,7 @@
}
</style>

<button on:click={() => dispatch('click')}>
<button class:disabled on:click={() => dispatch('click')}>
<img src={icon} {alt} />
<slot />
</button>
16 changes: 14 additions & 2 deletions example/src/UndoPanel.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<script>
export let undoManager;
import { undo } from 'sveltyjs';
import ImageButton from './ImageButton.svelte';
import undoIcon from './images/undo.png';
import redoIcon from './images/redo.png';
const undoStore = undo.readable(undoManager);
</script>

<style>
Expand All @@ -30,10 +34,18 @@
</style>

<panel>
<ImageButton icon={undoIcon} on:click={() => undoManager.undo()}>
<ImageButton
disabled={$undoStore.undoSize === 0}
icon={undoIcon}
on:click={() => undoManager.undo()}>
Undo
{#if $undoStore.undoSize > 0}({$undoStore.undoSize}){/if}
</ImageButton>
<ImageButton icon={redoIcon} on:click={() => undoManager.redo()}>
<ImageButton
disabled={$undoStore.redoSize === 0}
icon={redoIcon}
on:click={() => undoManager.redo()}>
Redo
{#if $undoStore.redoSize > 0}({$undoStore.redoSize}){/if}
</ImageButton>
</panel>
5 changes: 3 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * as array from './types/array'
export * as map from './types/map'
export * as array from './types/array';
export * as map from './types/map';
export * as undo from './undo';
36 changes: 36 additions & 0 deletions src/undo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as Y from 'yjs';
import { readable as svelteReadable } from 'svelte/store';

function readable(undoManager: Y.UndoManager) {
const stackCount = svelteReadable({ undoSize: 0, redoSize: 0 }, (set) => {
let undoSize = 0;
let redoSize = 0;

const updateStackSizes = () => {
undoSize = undoManager.undoStack.length;
redoSize = undoManager.redoStack.length;
set({ undoSize, redoSize });
};

const added = (/* { stackItem, type } */) => {
updateStackSizes();
};

const popped = (/* { stackItem, type } */) => {
updateStackSizes();
};

undoManager.on('stack-item-added', added);
undoManager.on('stack-item-popped', popped);

return () => {
// clean up when readable store is unsubscribed
undoManager.off('stack-item-added', added);
undoManager.off('stack-item-popped', popped);
};
});

return stackCount;
}

export { readable };

0 comments on commit b807133

Please sign in to comment.