-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
190 lines (162 loc) · 5.04 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
import {
App,
Editor,
EditorSelection,
EditorSelectionOrCaret,
MarkdownView,
Plugin,
PluginSettingTab,
Setting,
} from "obsidian";
import {
LineProcessor,
SelectionLineNumbers,
SelectionProcessor,
} from "./types";
interface BlockquoteLevelsSettings {
spaceBetweenPrefixes: boolean;
}
const DEFAULT_SETTINGS: BlockquoteLevelsSettings = {
spaceBetweenPrefixes: false,
};
export default class BlockquoteLevels extends Plugin {
settings: BlockquoteLevelsSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new BlockquoteLevelsSettingTab(this.app, this));
this.addCommand({
id: "blockquote-levels-increase",
name: "Increase",
editorCallback: (editor: Editor, view: MarkdownView) => {
if (editor.somethingSelected()) {
this.increaseLevelForSelections(editor);
} else {
this.increaseLevelForLine(editor);
}
},
});
this.addCommand({
id: "blockquote-levels-decrease",
name: "Decrease",
editorCallback: (editor: Editor, view: MarkdownView) => {
if (editor.somethingSelected()) {
this.decreaseLevelForSelections(editor);
} else {
this.decreaseLevelForLine(editor);
}
},
});
}
onunload() {}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
private increaseLevelForSelections(editor: Editor) {
const prefix = this.settings.spaceBetweenPrefixes ? "> " : ">";
this.processSelections(
editor,
(line: string) => /^>/.test(line) ? `${prefix}${line}` : `> ${line}`,
);
}
private decreaseLevelForSelections(editor: Editor) {
this.processSelections(
editor,
(line: string) => line.replace(/^>\s*/, ""),
);
}
private processSelections(editor: Editor, lineProcessor: LineProcessor) {
this.expandAndSortSelections(editor);
// Replace text
for (const selection of editor.listSelections()) {
const text = editor.getRange(selection.anchor, selection.head)
.split(/\n/)
.map(lineProcessor)
.join("\n");
editor.replaceRange(text, selection.anchor, selection.head);
}
}
private expandAndSortSelections(editor: Editor) {
const preparedSelections = editor.listSelections()
// Sort selections by line numbers, descending, so we're working from the
// bottom up, seems safer
.sort((s1, s2) =>
this.getLineNumbersOfSelection(s2).lastLine -
this.getLineNumbersOfSelection(s1).lastLine
)
// Expand selections to full lines
.map((selection): EditorSelectionOrCaret => {
const { firstLine, lastLine } = this.getLineNumbersOfSelection(
selection,
);
return {
anchor: { line: firstLine, ch: 0 },
head: { line: lastLine, ch: editor.getLine(lastLine).length },
};
});
editor.setSelections(preparedSelections);
}
getLineNumbersOfSelection(selection: EditorSelection): SelectionLineNumbers {
const nos = [selection.anchor.line, selection.head.line]
.sort((a, b) => a - b);
return {
firstLine: nos[0],
lastLine: nos[1],
};
}
private increaseLevelForLine(editor: Editor) {
this.processLine(
editor,
() => this.increaseLevelForSelections(editor),
);
}
private decreaseLevelForLine(editor: Editor) {
this.processLine(
editor,
() => this.decreaseLevelForSelections(editor),
);
}
private processLine(editor: Editor, selectionProcessor: SelectionProcessor) {
const { line, ch } = editor.getCursor("head");
const origLineLength = editor.getLine(line).length;
// Create selection containing the line the cursor is in
editor.setSelection(
{ line, ch: 0 },
{ line, ch: origLineLength },
);
selectionProcessor(editor);
// Set correct new cursor position, which also clears the selection
const cursorOffset = editor.getLine(line).length - origLineLength;
editor.setCursor({ line, ch: ch + cursorOffset });
}
}
class BlockquoteLevelsSettingTab extends PluginSettingTab {
plugin: BlockquoteLevels;
constructor(app: App, plugin: BlockquoteLevels) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", { text: "Blockquote Levels Settings" });
new Setting(containerEl)
.setName("Use a space between subsequent blockquote prefixes")
.setDesc('Disabled: ">>> quote", Enabled: "> > > quote"')
.addToggle((toggle) =>
toggle
.setValue(this.plugin.settings.spaceBetweenPrefixes)
.onChange(async (value) => {
console.log(
"[Blockquote Levels] " +
"Use spaces between subsequent blockquote prefixes: " +
value,
);
this.plugin.settings.spaceBetweenPrefixes = value;
await this.plugin.saveSettings();
})
);
}
}