@wordpress/sync
Version:
74 lines (73 loc) • 1.97 kB
JavaScript
// packages/sync/src/undo-manager.ts
import { LOCAL_EDITOR_ORIGIN } from "./config";
import { YMultiDocUndoManager } from "./y-utilities/y-multidoc-undomanager";
function createUndoManager() {
const yUndoManager = new YMultiDocUndoManager([], {
// Throttle undo/redo captures. (default: 500ms)
captureTimeout: 200,
// Ensure that we only scope the undo/redo to the current editor.
// The yjs document's clientID is added once it's available.
trackedOrigins: /* @__PURE__ */ new Set([LOCAL_EDITOR_ORIGIN])
});
return {
/**
* Record changes into the history.
* Since Yjs automatically tracks changes, this method translates the WordPress
* HistoryRecord format into Yjs operations.
*
* @param _record A record of changes to record.
* @param _isStaged Whether to immediately create an undo point or not.
*/
addRecord(_record, _isStaged = false) {
},
/**
* Add a Yjs map to the scope of the undo manager.
*
* @param {Y.Map< any >} ymap The Yjs map to add to the scope.
*/
addToScope(ymap) {
yUndoManager.addToScope(ymap);
},
/**
* Undo the last recorded changes.
*
*/
undo() {
if (!yUndoManager.canUndo()) {
return;
}
yUndoManager.undo();
return [];
},
/**
* Redo the last undone changes.
*/
redo() {
if (!yUndoManager.canRedo()) {
return;
}
yUndoManager.redo();
return [];
},
/**
* Check if there are changes that can be undone.
*
* @return {boolean} Whether there are changes to undo.
*/
hasUndo() {
return yUndoManager.canUndo();
},
/**
* Check if there are changes that can be redone.
*
* @return {boolean} Whether there are changes to redo.
*/
hasRedo() {
return yUndoManager.canRedo();
}
};
}
export {
createUndoManager
};
//# sourceMappingURL=undo-manager.js.map