Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,25 @@ export function registerLoroHistory(
excludeOriginPrefixes: ['history-'],
});

const tryGroupStart = () => {
try {
undoManager.groupStart();
} catch (e) {
console.error(
'Something went wrong starting a undo group (probably group start called multiple times). Ignoring..',
e
);
}
};

// Track if we're currently in a group
let isGroupActive = false;
let prevChangeTime = Date.now();
let prevChangeType = OTHER;
// Don't record history until the editor has been initialized and CLEAR_HISTORY_COMMAND
// has been dispatched. Without this guard, setEditorState during initialization triggers
// groupStart() on a Loro doc that may have no committed ops, causing a WASM panic.
let isReadyForHistory = false;

const getMergeAction = (
prevState: null | EditorState,
Expand Down Expand Up @@ -309,6 +324,10 @@ export function registerLoroHistory(
dirtyLeaves: Set<string>;
tags: Set<string>;
}): void => {
if (!isReadyForHistory) {
return;
}

const mergeAction = getMergeAction(
prevEditorState,
editorState,
Expand All @@ -328,12 +347,12 @@ export function registerLoroHistory(
isGroupActive = false;
}
// Start a new group for the next changes
undoManager.groupStart();
tryGroupStart();
isGroupActive = true;
} else if (mergeAction === HISTORY_MERGE) {
// If we're merging and no group is active, start one
if (!isGroupActive) {
undoManager.groupStart();
tryGroupStart();
isGroupActive = true;
}
}
Expand Down Expand Up @@ -393,6 +412,7 @@ export function registerLoroHistory(
isGroupActive = false;
}
undoManager.clear();
isReadyForHistory = true;
editor.dispatchCommand(CAN_UNDO_COMMAND, false);
editor.dispatchCommand(CAN_REDO_COMMAND, false);
return false;
Expand All @@ -410,6 +430,7 @@ export function registerLoroHistory(
isGroupActive = false;
}
undoManager.clear();
isReadyForHistory = true;
editor.dispatchCommand(CAN_UNDO_COMMAND, false);
editor.dispatchCommand(CAN_REDO_COMMAND, false);
return true;
Expand Down
Loading