From a4e98f7888103531b192a4e7bd30e9e5bbb3e8e9 Mon Sep 17 00:00:00 2001 From: Wolf Mermelstein Date: Tue, 9 Jun 2026 17:51:17 +0000 Subject: [PATCH 1/2] fix the unwrap none bug --- .../component/LexicalMarkdown/collaboration/undo.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/js/app/packages/core/component/LexicalMarkdown/collaboration/undo.ts b/js/app/packages/core/component/LexicalMarkdown/collaboration/undo.ts index 246a85ccbf..2efc645dfa 100644 --- a/js/app/packages/core/component/LexicalMarkdown/collaboration/undo.ts +++ b/js/app/packages/core/component/LexicalMarkdown/collaboration/undo.ts @@ -223,6 +223,10 @@ export function registerLoroHistory( 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, @@ -309,6 +313,10 @@ export function registerLoroHistory( dirtyLeaves: Set; tags: Set; }): void => { + if (!isReadyForHistory) { + return; + } + const mergeAction = getMergeAction( prevEditorState, editorState, @@ -393,6 +401,7 @@ export function registerLoroHistory( isGroupActive = false; } undoManager.clear(); + isReadyForHistory = true; editor.dispatchCommand(CAN_UNDO_COMMAND, false); editor.dispatchCommand(CAN_REDO_COMMAND, false); return false; @@ -410,6 +419,7 @@ export function registerLoroHistory( isGroupActive = false; } undoManager.clear(); + isReadyForHistory = true; editor.dispatchCommand(CAN_UNDO_COMMAND, false); editor.dispatchCommand(CAN_REDO_COMMAND, false); return true; From 7ef444eda96872ec7e033f2b00168b78be54cf14 Mon Sep 17 00:00:00 2001 From: Wolf Mermelstein Date: Mon, 15 Jun 2026 20:00:01 +0000 Subject: [PATCH 2/2] try finally the group start call --- .../LexicalMarkdown/collaboration/undo.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/js/app/packages/core/component/LexicalMarkdown/collaboration/undo.ts b/js/app/packages/core/component/LexicalMarkdown/collaboration/undo.ts index 2efc645dfa..e782eec306 100644 --- a/js/app/packages/core/component/LexicalMarkdown/collaboration/undo.ts +++ b/js/app/packages/core/component/LexicalMarkdown/collaboration/undo.ts @@ -219,6 +219,17 @@ 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(); @@ -336,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; } }