Implement undo/redo for custom widget editor

So I have a sort of tree widget that serves as editor for a certain file type. I noticed for text editors the Edit > Undo / Redo menu items are enable but not for mine. How do I implement it for my own widget editor? Is there a undo history service or something like that?

[original thread by Hanksha]

[Hanksha]

Ah could it but I have to register a handler for CommonCommands.UNDO and CommonCommands.REDO which isEnabled returns true when my widget is active?

@hanksha good idea, i would try to implement it like that as well

[Hanksha]

@anton-kosyakov How do I get the currently active/focused widget?

[Hanksha]

Ah is it via ApplicationShell.currentWidget?

[Hanksha]

Yeah that worked.

active = a shell widget to which document.activeElement belongs

current = last active shell widget

i.e. if a context menu is opened then active is undefined, but current will point to previously focused shell widget

current is useful for commands since they used from menus and quick palette

active is useful to evaluate contexts in when closures for example

[Hanksha]

Ok thanks for the details

[Hanksha]

Just found an issue regarding undo/redo:
If I have my widget editor active and focused, while there is another tab with a text editor which has changes. When I use undo/redo it applies it on the text editor and not my currently active widget editor.

your widget editor is Theia code editor?

maybe your handler does not win over code editor handler?

[Hanksha]

My widget extends TreeWidget and serves as my editor for a certain type of file. The thing is the code editor is not even focused or visible so it should not be enabled for undo/redo commands.

I think it is done to undo if several editors were affected by some operation, even if they are not visible and focused

Could you file an issue? It seems that with at least one opened code editor, code editor will always win. Not sure yet how we can handle it.

@jankoehnlein Did you try to contribute undo/redo handlers for diagrams?

[Deleted]

@anton-kosyakov Unfortunately not, my diagrams were all based on a text editor and re-used it’s undo/redo stack