generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
37 lines (35 loc) · 1 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
import { Editor, Plugin } from "obsidian";
export default class JoinLinesPlugin extends Plugin {
async onload() {
this.addCommand({
id: "join-lines",
name: "Join lines",
editorCallback(editor) {
joinLines(editor);
},
});
}
}
function joinLines(editor: Editor) {
const selectedText = editor.getSelection();
if (selectedText) {
const joinedText = selectedText.replace(/\n/g, " ");
editor.replaceSelection(joinedText);
return;
}
// No text selected:
// join current line with next line,
// and preserve the cursor position
const cursor = editor.getCursor();
const currLine = cursor.line;
const nextLine = currLine + 1;
const currLineText = editor.getLine(currLine);
const nextLineText = editor.getLine(nextLine);
const joinedText = currLineText + " " + nextLineText;
editor.replaceRange(
joinedText,
{ line: currLine, ch: 0 },
{ line: nextLine, ch: nextLineText.length },
);
editor.setCursor({ line: currLine, ch: cursor.ch });
}