generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
59 lines (50 loc) · 1.62 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { ViewPlugin, ViewUpdate, EditorView } from "@codemirror/view"
import { App, Editor, EditorRange, MarkdownView, Notice, Plugin } from 'obsidian';
function getCursor(editor: Editor): EditorRange {
return { from: editor.getCursor(), to: editor.getCursor() };
}
export default class ObsidianEmacsPlugin extends Plugin {
async onload() {
this.addCommand({
id: 'recenter-top-bottom',
hotkeys: [{ modifiers: ["Ctrl"], key: "l" }],
name: 'Scroll the window so that current line is in the middle of the window.',
editorCallback: (editor: Editor) => {
this.recenterTopBottom(editor);
}
});
this.addCommand({
id: 'kill-line',
hotkeys: [{ modifiers: ["Ctrl"], key: "k" }],
name: 'Kill the rest of the current line; if no nonblanks there, kill thru newline.',
editorCallback: (editor: Editor) => {
this.killLine(editor);
}
});
}
onunload() {
}
async recenterTopBottom(editor: Editor) {
editor.scrollIntoView(getCursor(editor), true);
}
async killLine(editor: Editor) {
const { line } = editor.getCursor();
const from_ = editor.getCursor("from");
const to = editor.getCursor("to");
if (from_.line === to.line && from_.ch === to.ch){
// nothing selected and kill line
const length = editor.getLine(line).length;
if (length === to.ch || length === 0) {
editor.setSelection({line:line, ch:0}, {line:line+1, ch:0});
} else {
editor.setSelection(from_, {line:line, ch:length});
}
}
// kill the selection directly
const sel = editor.getSelection();
if (sel !== "" && sel !== "\n") {
await navigator.clipboard.writeText(sel);
}
editor.replaceSelection("");
}
}