From 2eedc65a31373e85a52a425930ef630fad01cbb9 Mon Sep 17 00:00:00 2001 From: Pomroka <3210260+Pomroka@users.noreply.github.com> Date: Sat, 6 Aug 2022 12:21:29 +0200 Subject: [PATCH] fix: not working in new pane fix: make sure register once per CM editor --- main.ts | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/main.ts b/main.ts index 56cb4f7..98f885a 100644 --- a/main.ts +++ b/main.ts @@ -85,8 +85,15 @@ export default class VimrcPlugin extends Plugin { this.app.workspace.on("window-open", (workspaceWindow, w) => { this.registerYankEvents(w); }) + + // Two events cos + // this don't trigger on loading/reloading obsidian with note opened + this.app.workspace.on("active-leaf-change", async (cm: any) => { + this.updateSelectionEvent(); + }) + // and this don't trigger on opening same file in new pane this.app.workspace.on("file-open", async (cm: any) => { - this.registerFileOpenEvent(); + this.updateSelectionEvent(); }) this.initialized = true; @@ -104,13 +111,23 @@ export default class VimrcPlugin extends Plugin { }) } - registerFileOpenEvent(){ - // Record the position of selections in each file opened - let cmEditor = this.getCodeMirror(this.getActiveView()); - CodeMirror.on(cmEditor, "cursorActivity", async (cm: any) => { - this.currentSelection = cm.listSelections(); - }); - } + updateSelectionEvent() { + const view = this.getActiveView() + if (!view) { return } + const cm = this.getCodeMirror(view); + // if (cm._handlers.cursorActivity.some((e: { name: string; }) => e.name === 'updateSelection')) { + if (this.getCursorActivityHandlers(cm).some((e: { name: string; }) => e.name === 'updateSelection')) { + return; + } + cm.on("cursorActivity", function updateSelection(cm: CodeMirror.Editor) { + console.log('cursorActivity'); + this.currentSelection = cm.listSelections(); + }); + } + + private getCursorActivityHandlers(cm: CodeMirror.Editor) { + return (cm as any)._handlers.cursorActivity; + } async onload() { await this.loadSettings();