From 9466d1c2b1da1b77577324ad63d423be047b2c8d Mon Sep 17 00:00:00 2001 From: ko-shin-ryo Date: Sun, 17 Nov 2024 02:29:45 +0900 Subject: [PATCH] fix: prevent yank events registration issues in multi-window setup When opening a vault with multiple windows, non-active windows could miss the timing to register yank events. Added a mechanism to manage windows that have registered yank events to ensure proper event handling across all windows. --- main.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/main.ts b/main.ts index 812ba66..9aa7685 100644 --- a/main.ts +++ b/main.ts @@ -71,6 +71,7 @@ export default class VimrcPlugin extends Plugin { private lastYankBuffer: string[] = [""]; private lastSystemClipboard = ""; private yankToSystemClipboard: boolean = false; + private registeredYankEventsWindows: Set = new Set(); private currentKeyChord: any = []; private vimChordStatusBar: HTMLElement = null; private vimStatusBar: HTMLElement = null; @@ -115,6 +116,9 @@ export default class VimrcPlugin extends Plugin { this.app.workspace.on("window-open", (workspaceWindow, w) => { this.registerYankEvents(w); }) + this.app.workspace.on("window-close", (workspaceWindow, w) => { + this.registeredYankEventsWindows.delete(w); + }) this.prepareChordDisplay(); this.prepareVimModeDisplay(); @@ -123,13 +127,12 @@ export default class VimrcPlugin extends Plugin { // this don't trigger on loading/reloading obsidian with note opened this.app.workspace.on("active-leaf-change", async () => { this.updateSelectionEvent(); - this.updateVimEvents(); + this.registerYankEvents(activeWindow); }); // and this don't trigger on opening same file in new pane this.app.workspace.on("file-open", async () => { this.updateSelectionEvent(); - this.updateVimEvents(); }); @@ -137,6 +140,8 @@ export default class VimrcPlugin extends Plugin { } registerYankEvents(win: Window) { + if (this.registeredYankEventsWindows.has(win)) + return; this.registerDomEvent(win.document, 'click', () => { this.captureYankBuffer(win); }); @@ -146,6 +151,7 @@ export default class VimrcPlugin extends Plugin { this.registerDomEvent(win.document, 'focusin', () => { this.captureYankBuffer(win); }) + this.registeredYankEventsWindows.add(win) } async updateSelectionEvent() {