Context-Based Undo History

When you have tools that can have instances, like for example a text editor window, you would need a history for each of those windows, not a global undo history because you want to undo the specific steps when that window is in focus only. This is valid for when windows are not visible, text editors etc. unless you want your action in the global undo history of the editor. Example usage:

History Action Example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
-- this function name is as an example, the main idea is that you need to call this when your window gets focus
local function myWindowFocus(myWindow)
        -- lets set our history for this window
        -- 'history' member should be initialized at the window creation like: myWindow.history = require("editor/api/history")
        -- set the current editor history object as the one of our window
        editor.setUndoHistory(myWindow.history)
        -- from now on, the undo/redo will operate on that window's history object, not the editor's global one
end

-- you should call this when your window lost focus
local function myWindowLostFocus(myWindow)
        -- will revert to default editor global history object
        editor.setUndoHistory(nil)
end