From 899f4effac43951a35df8ada19bd2c04a25e96a5 Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Mon, 15 Apr 2024 18:27:14 +0800 Subject: [PATCH] fix: can not track the first transaction --- src/state.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/state.ts b/src/state.ts index 0eb68b6..38fe047 100644 --- a/src/state.ts +++ b/src/state.ts @@ -9,7 +9,7 @@ function createUndoManager(doc: Y.Doc) { doc, trackedOrigins: new Set([TRACK_ALL_ORIGINS]), }); - doc.on("update", () => { + const updateScope = () => { // The UndoManager can only track shared types that are created // See https://discuss.yjs.dev/t/global-document-undo-manager/2555 const keys = Array.from(doc.share.keys()); @@ -17,11 +17,13 @@ function createUndoManager(doc: Y.Doc) { const scope = keys.map((key) => doc.get(key)); undoManager.addToScope(scope); // undoManager.addTrackedOrigin(origin); - }); + }; doc.on("beforeTransaction", (transaction) => { // Try to track all origins // Workaround for https://github.com/yjs/yjs/issues/624 transaction.origin = TRACK_ALL_ORIGINS; + // Track all shared types before running UndoManager.afterTransactionHandler + updateScope(); }); return undoManager; }