-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.ts
127 lines (114 loc) · 4.01 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { Editor, MarkdownView, Plugin, EditorPosition } from "obsidian";
export default class Underline extends Plugin {
async onload() {
this.addCommand({
id: "toggle-underline-tag",
name: "Toggle underline tag",
editorCallback: (editor: Editor, view: MarkdownView) =>
this.wrapper(editor, view),
hotkeys: [
{
modifiers: ["Mod"],
key: "u",
},
],
});
this.addCommand({
id: "toggle-center-tag",
name: "Toggle center tag",
editorCallback: (editor: Editor, view: MarkdownView) =>
this.wrapper(editor, view, "<center>", "</center>"),
// hotkeys: [
// {
// modifiers: ["Mod", "Shift"],
// key: "c",
// },
// ],
});
this.addCommand({
id: "toggle-link-heading",
name: "Toggle a link to heading in the same file",
editorCallback: (editor: Editor, view: MarkdownView) =>
this.wrapper(editor, view, "[[#", "]]"),
// hotkeys: [
// {
// modifiers: ["Mod"],
// key: "3",
// },
// ],
});
this.addCommand({
id: "toggle-link-block",
name: "Toggle a link to block in the same file",
editorCallback: (editor: Editor, view: MarkdownView) =>
this.wrapper(editor, view, "[[#^", "]]"),
// hotkeys: [
// {
// modifiers: ["Mod"],
// key: "6",
// },
// ],
});
}
wrapper(editor: Editor, view: MarkdownView, prefix: string = "<u>", suffix: string = "</u>"): void {
const PL = prefix.length; // Prefix Length
const SL = suffix.length; // Suffix Length
// let markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
// if (!markdownView) {
// return;
// }
// let editor = markdownView.editor;
let selectedText = editor.somethingSelected() ? editor.getSelection() : "";
let last_cursor = editor.getCursor(); // the cursor that at the last position of doc
last_cursor.line = editor.lastLine();
last_cursor.ch = editor.getLine(last_cursor.line).length;
const last_offset = editor.posToOffset(last_cursor);
function Cursor(offset: number): EditorPosition {
if (offset > last_offset) {
return last_cursor;
}
offset = offset < 0 ? 0 : offset;
return editor.offsetToPos(offset);
}
/* Detect whether the selected text is packed by <u></u>.
If true, unpack it, else pack with <u></u>. */
const fos = editor.posToOffset(editor.getCursor("from")); // from offset
const tos = editor.posToOffset(editor.getCursor("to")); // to offset
const len = selectedText.length;
var beforeText = editor.getRange(Cursor(fos - PL), Cursor(tos - len));
var afterText = editor.getRange(Cursor(fos + len), Cursor(tos + SL));
var startText = editor.getRange(Cursor(fos), Cursor(fos + PL));
var endText = editor.getRange(Cursor(tos - SL), Cursor(tos));
if (beforeText === prefix && afterText === suffix) {
//=> undo underline (inside selection)
editor.setSelection(Cursor(fos - PL), Cursor(tos + SL));
editor.replaceSelection(`${selectedText}`);
// re-select
editor.setSelection(Cursor(fos - PL), Cursor(tos - PL));
} else if (startText === prefix && endText === suffix) {
//=> undo underline (outside selection)
editor.replaceSelection(
editor.getRange(Cursor(fos + PL), Cursor(tos - SL))
);
// re-select
editor.setSelection(Cursor(fos), Cursor(tos - PL - SL));
} else {
//=> do underline
if (selectedText) {
// console.log("selected");
editor.replaceSelection(`${prefix}${selectedText}${suffix}`);
// re-select
editor.setSelection(
editor.offsetToPos(fos + PL),
editor.offsetToPos(tos + PL)
);
} else {
// console.log("not selected");
editor.replaceSelection(`${prefix}${suffix}`);
let cursor = editor.getCursor();
cursor.ch -= SL;
editor.setCursor(cursor);
}
}
}
}