tldraw / tldraw

SDK for creating whiteboards and canvas experiences on the web.
https://tldraw.dev
Other
35.34k stars 2.15k forks source link

[Feature]: Ability to get a snapshot of undo/redo history #1714

Open LacombeJ opened 1 year ago

LacombeJ commented 1 year ago

What's the feature?

Functionality in the tldraw API that allows one to get a snapshot of the undo/redo history to persist it. This could be used in the case of switching between tabs and different tldraw contexts.

A proposed usage could be like the following (similar to store snapshots):

const historySnapshot = editor.history.getSnapshot();
editor.history.loadSnapshot(historySnapshot);

Currently I am using the below method to manually set the _redo and _undo properties of the history, but this is a hack because the stack function isn't public (I am copying the code from tldraw source to call it).

React.useEffect(() => {
  if (editor) {
    const history = props.restore();
    if (history) {
      editor.history.clear();
      editor.history._redos.set(stack(history.redos) as any);
      editor.history._undos.set(stack(history.undos) as any);
    }

    return () => {
      const undos = editor.history._undos.value.toArray();
      const redos = editor.history._redos.value.toArray();
      props.store({ undos, redos });
    };
  }
}, [props.loaded, editor]);

Contact Details

No response

Code of Conduct

github-actions[bot] commented 3 hours ago

This issue is stale because it has been open for 150 days with no activity.