-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.ts
330 lines (289 loc) · 9.32 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import { Editor, MarkdownView, Plugin } from 'obsidian';
interface VaultConfig {
readableLineLength: boolean;
}
declare module 'obsidian' {
interface Vault {
getConfig<T extends keyof VaultConfig>(config: T): VaultConfig[T];
setConfig<T extends keyof VaultConfig>(config: T, value: VaultConfig[T]): void;
}
}
export default class HotkeysPlus extends Plugin {
onload() {
console.log('Loading Hotkeys++ plugin');
this.addCommand({
id: 'better-toggle-todo',
name: 'Toggle to-do lists',
callback: () => this.toggleTodos(),
hotkeys: [
{
modifiers: ['Mod'],
key: 'm',
},
],
});
this.addCommand({
id: 'toggle-bullet-number',
name: 'Toggle line to bulleted or numbered lists',
callback: () => this.toggleLists(),
hotkeys: [
{
modifiers: ['Mod', 'Shift'],
key: 'm',
},
],
});
this.addCommand({
id: 'toggle-block-quote',
name: 'Toggle line to block quote',
callback: () => this.toggleBlockQuote(),
hotkeys: [
{
modifiers: ['Mod'],
key: '<',
},
],
});
this.addCommand({
id: 'toggle-embed',
name: 'Toggle line to embed internal links',
callback: () => this.toggleEmbed(),
hotkeys: [
{
modifiers: ['Mod', 'Shift'],
key: '1',
},
],
});
this.addCommand({
id: 'duplicate-lines-down',
name: 'Copy line(s) down',
callback: () => this.duplicateLines('down'),
});
this.addCommand({
id: 'duplicate-lines-up',
name: 'Copy line(s) up',
callback: () => this.duplicateLines('up'),
});
this.addCommand({
id: 'clean-selected',
name: 'Trims selected text and removes new line characters.',
callback: () => this.cleanSelected(),
});
this.addCommand({
id: 'insert-line-above',
name: 'Insert line above current line',
callback: () => this.insertLine('above'),
});
this.addCommand({
id: 'insert-line-below',
name: 'Insert line below current line',
callback: () => this.insertLine('below'),
});
this.addCommand({
id: 'clear-current-line',
name: 'Clear current line',
callback: () => this.clearCurrentLine(),
});
this.addCommand({
id: 'toggle-readable-length',
name: 'Toggle Readable Line Length',
callback: () =>
this.app.vault.setConfig(
'readableLineLength',
!this.app.vault.getConfig('readableLineLength'),
),
});
}
clearCurrentLine(): void {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const editor = view.editor;
const lineNumber = editor.getCursor().line;
editor.setLine(lineNumber, '');
}
insertLine(mode: 'above' | 'below'): void {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const editor = view.editor;
const lineNumber = editor.getCursor().line;
const currentLineText = editor.getLine(lineNumber);
let newLineText = '';
if (currentLineText.trim().startsWith('- ')) {
newLineText = currentLineText.substring(0, currentLineText.indexOf('- ') + 2);
}
for (let i = 1; i < 30; i++) {
if (currentLineText.trim().startsWith(i.toString() + '. ')) {
let correction: number;
if (mode == 'above') correction = -1;
else correction = 1;
newLineText =
currentLineText.substring(0, currentLineText.indexOf(i.toString() + '. ')) +
(i + correction).toString() +
'. ';
}
}
if (mode == 'above') {
editor.replaceRange(newLineText + '\n', { line: lineNumber, ch: 0 });
editor.setSelection({ line: lineNumber, ch: newLineText.length });
} else {
editor.replaceRange('\n' + newLineText, {
line: lineNumber,
ch: currentLineText.length,
});
editor.setSelection({ line: lineNumber + 1, ch: newLineText.length });
}
}
duplicateLines(mode: 'up' | 'down'): void {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const editor = view.editor;
const selectedText = this.getSelectedText(editor);
let newString = selectedText.content + '\n';
if (mode === 'down') {
editor.replaceRange(newString, selectedText.start, selectedText.start);
} else {
if (selectedText.end.line === editor.lastLine()) {
// create a new line so that lastLine + 1 exists
const newLastLineContent = editor.getLine(editor.lastLine()) + '\n';
const cursorAnchor = editor.getCursor('anchor');
const cursorHead = editor.getCursor('head');
editor.setLine(editor.lastLine(), newLastLineContent);
editor.setSelection(cursorAnchor, cursorHead); // preserve original cursor / selection state (adding a new line may have pushed the cursor down)
newString = selectedText.content; // because there is no other content on the newly created line, we don't need a trailing newline char
}
const nextLineStart = {
line: selectedText.end.line + 1,
ch: 0,
};
editor.replaceRange(newString, nextLineStart, nextLineStart);
}
}
cleanSelected(): void {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const editor = view.editor;
const selectedText = this.getSelectedText(editor);
let newString = selectedText.content.trim().replace(/(\r\n|\n|\r)/gm, ' ');
newString = newString.replace(/ +/gm, ' ');
editor.replaceRange(newString, selectedText.start, selectedText.end);
}
onunload() {
console.log('Unloading Hotkeys++ plugin');
}
getSelectedText(editor: Editor) {
if (editor.somethingSelected()) {
// Toggle to-dos under the selection
const cursorStart = editor.getCursor('from');
const cursorEnd = editor.getCursor('to');
const content = editor.getRange(
{ line: cursorStart.line, ch: 0 },
{ line: cursorEnd.line, ch: editor.getLine(cursorEnd.line).length },
);
return {
start: { line: cursorStart.line, ch: 0 },
end: {
line: cursorEnd.line,
ch: editor.getLine(cursorEnd.line).length,
},
content: content,
};
} else {
// Toggle the todo in the line
const lineNr = editor.getCursor().line;
const contents = editor.getDoc().getLine(lineNr);
const cursorStart = {
line: lineNr,
ch: 0,
};
const cursorEnd = {
line: lineNr,
ch: contents.length,
};
const content = editor.getRange(cursorStart, cursorEnd);
return { start: cursorStart, end: cursorEnd, content: content };
}
}
toggleElement(re: RegExp, subst: any) {
const view = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!view) return;
const editor = view.editor;
const selection = editor.somethingSelected();
const selectedText = this.getSelectedText(editor);
const newString = selectedText.content.replace(re, subst);
editor.replaceRange(newString, selectedText.start, selectedText.end);
// Keep cursor in the same place
if (selection) {
editor.setSelection(selectedText.start, {
line: selectedText.end.line,
ch: editor.getLine(selectedText.end.line).length,
});
}
}
toggleTodos() {
const re =
/(^\s*|^\t*)(-\s\[ \]\s|-\s\[x\]\s|\*\s|-\s|\d*\.\s|\*\s|\b|^)([^\n\r]*)/gim;
return this.toggleElement(re, this.replaceTodoElement);
}
toggleLists() {
const re =
/(^\s*|^\t*)(-\s\[ \]\s|-\s\[x\]\s|\*\s|-\s|\d*\.\s|\*\s|\b|^)([^\n\r]*)/gim;
return this.toggleElement(re, this.replaceListElement);
}
toggleBlockQuote() {
const re = />\s|^/gim;
return this.toggleElement(re, this.replaceBlockQuote);
}
toggleEmbed() {
const re = /\S*\[\[/gim;
return this.toggleElement(re, this.replaceEmbed);
}
replaceListElement(
match: string,
spaces: string,
startText: string,
sentence: string,
) {
if (startText === '- ') {
return spaces + '1. ' + sentence;
} else if (startText === '') {
return spaces + '- ' + sentence;
} else if (startText === '1. ') {
return spaces + '' + sentence;
} else {
return spaces + '- ' + sentence;
}
}
replaceBlockQuote(startText: string) {
if (startText === '> ') {
return '';
} else if (startText === '') {
return '> ';
} else {
return '> ';
}
}
replaceEmbed(startText: string) {
if (startText === '![[') {
return '[[';
} else if (startText === '[[') {
return '![[';
} else {
return '';
}
}
replaceTodoElement(
match: string,
spaces: string,
startText: string,
sentence: string,
) {
if (startText === '- [ ] ') {
return spaces + '- [x] ' + sentence;
} else if (startText === '- [x] ') {
return spaces + '- ' + sentence;
} else {
return spaces + '- [ ] ' + sentence;
}
}
}