Facilitate referencing UndoManager StackItem inside Type observers

This commit is contained in:
Myles J 2024-02-28 23:58:37 +00:00
parent a9dc72fcc0
commit 917261a1ce
No known key found for this signature in database
GPG Key ID: 6D89F44387384D91
2 changed files with 73 additions and 0 deletions

View File

@ -191,6 +191,12 @@ export class UndoManager extends Observable {
*/ */
this.undoing = false this.undoing = false
this.redoing = false this.redoing = false
/**
* The currently popped stack item if UndoManager.undoing or UndoManager.redoing
*
* @type {StackItem|null}
*/
this.doingStackItem = null
this.lastChange = 0 this.lastChange = 0
this.ignoreRemoteMapChanges = ignoreRemoteMapChanges this.ignoreRemoteMapChanges = ignoreRemoteMapChanges
this.captureTimeout = captureTimeout this.captureTimeout = captureTimeout
@ -331,10 +337,12 @@ export class UndoManager extends Observable {
*/ */
undo () { undo () {
this.undoing = true this.undoing = true
this.doingStackItem = array.last(this.undoStack) ?? null
let res let res
try { try {
res = popStackItem(this, this.undoStack, 'undo') res = popStackItem(this, this.undoStack, 'undo')
} finally { } finally {
this.doingStackItem = null
this.undoing = false this.undoing = false
} }
return res return res
@ -347,10 +355,12 @@ export class UndoManager extends Observable {
*/ */
redo () { redo () {
this.redoing = true this.redoing = true
this.doingStackItem = array.last(this.redoStack) ?? null
let res let res
try { try {
res = popStackItem(this, this.redoStack, 'redo') res = popStackItem(this, this.redoStack, 'redo')
} finally { } finally {
this.doingStackItem = null
this.redoing = false this.redoing = false
} }
return res return res

View File

@ -715,3 +715,66 @@ export const testUndoDeleteInMap = (tc) => {
undoManager.undo() undoManager.undo()
t.compare(map0.toJSON(), { a: 'a' }) t.compare(map0.toJSON(), { a: 'a' })
} }
/**
* It should expose the StackItem being processed if undoing
*
* @param {t.TestCase} tc
*/
export const testUndoDoingStackItem = async (tc) => {
const doc = new Y.Doc()
const text = doc.getText('text')
const undoManager = new Y.UndoManager([text])
undoManager.on('stack-item-added', /** @param {any} event */ event => {
event.stackItem.meta.set('str', '42')
})
const meta = new Promise((resolve) => {
setTimeout(() => resolve('ABORTED'), 50)
text.observe((event) => {
const /** @type {Y.UndoManager} */ origin = event.transaction.origin
if (origin === undoManager && origin.undoing) {
resolve(origin.doingStackItem?.meta.get('str'))
}
})
})
text.insert(0, 'abc')
undoManager.undo()
t.compare(await meta, '42')
t.compare(undoManager.doingStackItem, null)
}
/**
* It should expose the StackItem being processed if redoing
*
* @param {t.TestCase} tc
*/
export const testRedoDoingStackItem = async (tc) => {
const doc = new Y.Doc()
const text = doc.getText('text')
const undoManager = new Y.UndoManager([text])
undoManager.on('stack-item-added', /** @param {any} event */ event => {
event.stackItem.meta.set('str', '42')
})
const meta = new Promise(resolve => {
setTimeout(() => resolve('ABORTED'), 50)
text.observe((event) => {
const /** @type {Y.UndoManager} */ origin = event.transaction.origin
if (origin === undoManager && origin.redoing) {
resolve(origin.doingStackItem?.meta.get('str'))
}
})
})
text.insert(0, 'abc')
undoManager.undo()
undoManager.redo()
t.compare(await meta, '42')
t.compare(undoManager.doingStackItem, null)
}