diff --git a/main.ts b/main.ts index bd5bd9b..1cfe8fb 100644 --- a/main.ts +++ b/main.ts @@ -46,7 +46,7 @@ export default class VimrcPlugin extends Plugin { private editorMode: 'cm5' | 'cm6' = null; private initialized = false; - private lastYankBuffer = new Array(0); + private lastYankBuffer: string[] = [""]; private lastSystemClipboard = ""; private yankToSystemClipboard: boolean = false; private currentKeyChord: any = []; @@ -92,19 +92,26 @@ export default class VimrcPlugin extends Plugin { console.log('Vimrc plugin: using CodeMirror 5 mode'); } - this.registerDomEvent(document, 'click', () => { - this.captureYankBuffer(); - }); - this.registerDomEvent(document, 'keyup', () => { - this.captureYankBuffer(); - }); - this.registerDomEvent(document, 'focusin', () => { - this.captureYankBuffer(); + await this.registerYankEvents(activeWindow) + this.app.workspace.on("window-open", (workspaceWindow, w) => { + this.registerYankEvents(w) }) this.initialized = true; } + registerYankEvents(win: Window) { + this.registerDomEvent(win.document, 'click', () => { + this.captureYankBuffer(win); + }); + this.registerDomEvent(win.document, 'keyup', () => { + this.captureYankBuffer(win); + }); + this.registerDomEvent(win.document, 'focusin', () => { + this.captureYankBuffer(win); + }) + } + async onload() { await this.loadSettings(); this.addSettingTab(new SettingsTab(this.app, this)) @@ -445,25 +452,34 @@ export default class VimrcPlugin extends Plugin { } - captureYankBuffer() { - if (this.yankToSystemClipboard) { - let currentBuffer = this.codeMirrorVimObject.getRegisterController().getRegister('yank').keyBuffer; - if (currentBuffer != this.lastYankBuffer) { - if (this.lastYankBuffer.length > 0 && currentBuffer.length > 0 && currentBuffer[0]) { - navigator.clipboard.writeText(currentBuffer[0]); - navigator.clipboard.readText().then((value) => { this.lastSystemClipboard = value; }); - } - this.lastYankBuffer = currentBuffer; - return; + async captureYankBuffer(win: Window) { + if (!this.yankToSystemClipboard) { + return + } + + const yankRegister = this.codeMirrorVimObject.getRegisterController().getRegister('yank') + const currentYankBuffer = yankRegister.keyBuffer; + + // yank -> clipboard + const buf = currentYankBuffer[0] + if (buf !== this.lastYankBuffer[0]) { + await win.navigator.clipboard.writeText(buf); + this.lastYankBuffer = currentYankBuffer + this.lastSystemClipboard = await win.navigator.clipboard.readText() + return + } + + // clipboard -> yank + try { + const currentClipboardText = await win.navigator.clipboard.readText() + if (currentClipboardText !== this.lastSystemClipboard) { + yankRegister.setText(currentClipboardText); + this.lastYankBuffer = yankRegister.keyBuffer; + this.lastSystemClipboard = currentClipboardText; } - let currentClipboard = navigator.clipboard.readText().then((value) => { - if (value != this.lastSystemClipboard) { - let yankRegister = this.codeMirrorVimObject.getRegisterController().getRegister('yank') - yankRegister.setText(value); - this.lastYankBuffer = yankRegister.keyBuffer; - this.lastSystemClipboard = value; - } - }) + } catch (e) { + // XXX: Avoid "Uncaught (in promise) DOMException: Document is not focused." + // XXX: It is not good but easy workaround } } diff --git a/package.json b/package.json index bd87b7c..d426a67 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "@types/node": "^14.14.6", "codemirror": "^5.62.2", "keyboardevent-from-electron-accelerator": "*", - "obsidian": "^0.12.17", + "obsidian": "^0.15.3", "rollup": "^2.33.0", "tslib": "^2.0.3", "typescript": "^4.0.5"