Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multi windows (yank <--> clipboard sync) #110

Merged
merged 3 commits into from
Jul 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 43 additions & 27 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class VimrcPlugin extends Plugin {
private editorMode: 'cm5' | 'cm6' = null;
private initialized = false;

private lastYankBuffer = new Array<string>(0);
private lastYankBuffer: string[] = [""];
private lastSystemClipboard = "";
private yankToSystemClipboard: boolean = false;
private currentKeyChord: any = [];
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down