Skip to content

Refactor undo/redo and integrate with playground #10

@amyworrall

Description

@amyworrall

Lexical has an implementation of undo/redo, in History.swift. (Arguably this should have been in a Plugin, but there's some historical cruft here.)

Currently, to integrate history into a surface, you need to:

  • register handlers for the commands undo, redo, and clearEditor, and in the handlers, proxy those commands to lexicalView.editorHistory
  • create toolbar buttons to dispatch the commands undo and redo
  • register handlers for canUndo and canRedo, and use these handlers to trigger updating the toolbar to enable/disable the undo/redo buttons
  • register an update listener, that calls applyChange() on the EditorHistory instance, passing the active and previous editor states

I would like to see this improved. We should have a new EditorHistoryPlugin class, in its own package, which will:

  • own and expose the EditorHistory object
  • creates and keeps a reference to externalHistoryState, which is passed in to EditorHistory on init
  • register the handlers for undo, redo, and clearEditor
    • In fact, you could move the declarations of the constants for the .undo and .redo commands into EditorHistoryPlugin as well
  • registers the update listener that calls applyChange() on the EditorHistory object
  • expose getters for canUndo and canRedo, which just check if externalHistoryState.undoStack > 0 (and same for redoStack). Note that this doesn't obviate the need for the canUndo and canRedo commands -- those are still useful to know when these values change.
  • move History.swift into this package

Then, to integrate it into the playground:

  • It should be enough to make the toolbar buttons just dispatch the undo and redo commands, now that the EditorHistoryPlugin is listening for them.
  • Still register canUndo and canRedo, and use them to trigger a toolbar update.

Note, I've tagged this task good first issue, although there are quite a lot of moving parts. If you need any more context or help, let me know!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions