mirror of
https://github.com/esm7/obsidian-vimrc-support.git
synced 2026-07-22 05:00:25 +00:00
Merge pull request #128 from Pomroka/master
fix: empty selection in jsfile command
This commit is contained in:
commit
a3e7973fde
2 changed files with 33 additions and 8 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -12,5 +12,6 @@ package-lock.json
|
|||
# build
|
||||
main.js
|
||||
*.js.map
|
||||
data.json
|
||||
|
||||
obsidian.d.ts
|
||||
|
|
|
|||
40
main.ts
40
main.ts
|
|
@ -61,7 +61,7 @@ export default class VimrcPlugin extends Plugin {
|
|||
// https://developer.mozilla.org/en-US/docs/Web/API/KeyboardLayoutMap
|
||||
let keyMap: Record<string, string> = {};
|
||||
let layout = await (navigator as any).keyboard.getLayoutMap();
|
||||
let doneIterating = new Promise((resolve, reject) => {
|
||||
let doneIterating = new Promise<void>((resolve, reject) => {
|
||||
let counted = 0;
|
||||
layout.forEach((value: any, index: any) => {
|
||||
keyMap[index] = value;
|
||||
|
|
@ -86,6 +86,16 @@ export default class VimrcPlugin extends Plugin {
|
|||
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 () => {
|
||||
this.updateSelectionEvent();
|
||||
});
|
||||
// and this don't trigger on opening same file in new pane
|
||||
this.app.workspace.on("file-open", async () => {
|
||||
this.updateSelectionEvent();
|
||||
});
|
||||
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
|
|
@ -101,6 +111,25 @@ export default class VimrcPlugin extends Plugin {
|
|||
})
|
||||
}
|
||||
|
||||
async updateSelectionEvent() {
|
||||
const view = this.getActiveView();
|
||||
if (!view) return
|
||||
|
||||
let cm = this.getCodeMirror(view);
|
||||
if (
|
||||
this.getCursorActivityHandlers(cm).some(
|
||||
(e: { name: string }) => e.name === "updateSelection")
|
||||
) return
|
||||
cm.on("cursorActivity", async (cm: CodeMirror.Editor) => this.updateSelection(cm));
|
||||
}
|
||||
async updateSelection(cm: any) {
|
||||
this.currentSelection = cm.listSelections();
|
||||
}
|
||||
|
||||
private getCursorActivityHandlers(cm: CodeMirror.Editor) {
|
||||
return (cm as any)._handlers.cursorActivity;
|
||||
}
|
||||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
this.addSettingTab(new SettingsTab(this.app, this))
|
||||
|
|
@ -182,11 +211,6 @@ export default class VimrcPlugin extends Plugin {
|
|||
this.defineJsCommand(this.codeMirrorVimObject);
|
||||
this.defineJsFile(this.codeMirrorVimObject);
|
||||
|
||||
// Record the position of selections
|
||||
CodeMirror.on(cmEditor, "cursorActivity", async (cm: any) => {
|
||||
this.currentSelection = cm.listSelections()
|
||||
});
|
||||
|
||||
vimCommands.split("\n").forEach(
|
||||
function (line: string, index: number, arr: [string]) {
|
||||
if (line.trim().length > 0 && line.trim()[0] != '"') {
|
||||
|
|
@ -546,14 +570,14 @@ export default class VimrcPlugin extends Plugin {
|
|||
if (extraCode[0] != '{' || extraCode[extraCode.length - 1] != '}')
|
||||
throw new Error("Expected an extra code argument which is JS code surrounded by curly brackets: {...}");
|
||||
}
|
||||
let currentSelections = this.currentSelection;
|
||||
var chosenSelection = currentSelections && currentSelections.length > 0 ? currentSelections[0] : null;
|
||||
let content = '';
|
||||
try {
|
||||
content = await this.app.vault.adapter.read(fileName);
|
||||
} catch (e) {
|
||||
throw new Error(`Cannot read file ${params.args[0]} from vault root: ${e.message}`);
|
||||
}
|
||||
let currentSelections = this.currentSelection;
|
||||
var chosenSelection = currentSelections && currentSelections.length > 0 ? currentSelections[0] : null;
|
||||
const command = Function('editor', 'view', 'selection', content + extraCode);
|
||||
const view = this.getActiveView();
|
||||
command(view.editor, view, chosenSelection);
|
||||
|
|
|
|||
Loading…
Reference in a new issue