generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ts
204 lines (172 loc) · 5.15 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
import {
App,
Plugin,
PluginSettingTab,
Setting,
MarkdownView,
addIcon,
} from "obsidian";
import * as CodeMirror from "codemirror";
interface languageMap {
[name: string]: string;
}
interface PluginSettings {
languages: languageMap;
}
const DEFAULT_SETTINGS: PluginSettings = {
languages: { "e3dea0f5-37f2-4d79-ae58-490af3228069": "js" },
};
interface SelectionRange {
start: { line: number; ch: number };
end: { line: number; ch: number };
}
export default class CodeBlockFromSelection extends Plugin {
settings: PluginSettings;
async onload() {
await this.loadSettings();
this.initCommands();
this.addSettingTab(new SettingTab(this.app, this));
}
initCommands(): void {
for (const [uuid, language] of Object.entries(this.settings.languages)) {
this.addCommand({
id: uuid,
name: language,
callback: () => this.insertCodeBlock(language),
});
}
}
_addCommand(id: string, language: string) {
this.addCommand({
id: id,
name: language,
callback: () => this.insertCodeBlock(language),
});
}
uuid(): string {
var d = Date.now();
if (
typeof performance !== "undefined" &&
typeof performance.now === "function"
) {
d += performance.now(); //use high-precision timer if available
}
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
/[xy]/g,
function (c) {
var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16);
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
}
);
}
insertCodeBlock(language: string): void {
let editor = this.getEditor();
if (editor) {
let selectedText = this.getSelectedText(editor);
let line = this.getLineUnderCursor(editor).start.line;
editor.replaceSelection(`\`\`\`${language}\n${selectedText}\n\`\`\`\n`);
if (!selectedText) {
editor.setSelection({ line: line + 1, ch: 0 });
}
}
}
getEditor(): CodeMirror.Editor {
return this.app.workspace.getActiveViewOfType(MarkdownView)?.sourceMode
.cmEditor;
}
getSelectedText(editor: CodeMirror.Editor): string {
if (!editor.somethingSelected()) this.selectLineUnderCursor(editor);
return editor.getSelection();
}
selectLineUnderCursor(editor: CodeMirror.Editor) {
let selection = this.getLineUnderCursor(editor);
editor.getDoc().setSelection(selection.start, selection.end);
}
getLineUnderCursor(editor: CodeMirror.Editor): SelectionRange {
let fromCh, toCh: number;
let cursor = editor.getCursor();
fromCh = 0;
toCh = editor.getLine(cursor.line).length;
return {
start: { line: cursor.line, ch: fromCh },
end: { line: cursor.line, ch: toCh },
};
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class SettingTab extends PluginSettingTab {
plugin: CodeBlockFromSelection;
constructor(app: App, plugin: CodeBlockFromSelection) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let { containerEl } = this;
containerEl.empty();
containerEl.createEl("h2", {
text: "Code block from selection - Settings",
});
new Setting(containerEl).setName("Add new language").addButton(cb => {
cb.setButtonText("Add");
cb.setCta();
cb.onClick(() => {
this.addNewLanguage();
});
});
// init settingtab
for (const [uuid, language] of Object.entries(
this.plugin.settings.languages
)) {
const setting = new Setting(containerEl);
setting
.setName("Language")
.setDesc("")
.addText(text =>
text
.setPlaceholder("add new language")
.setValue(language)
.onChange(async value => {
this.plugin.settings.languages[uuid] = value;
await this.plugin.saveSettings();
this.plugin._addCommand(uuid, value);
})
);
this.addExtraButton(setting, uuid);
}
}
addNewLanguage() {
let setting = new Setting(this.containerEl);
let uuid = this.plugin.uuid();
setting
.setName("language")
.setDesc("")
.addText(text =>
text.setPlaceholder("add new language").onChange(async value => {
this.plugin.settings.languages[uuid] = value;
await this.plugin.saveSettings();
this.plugin._addCommand(uuid, value);
})
);
this.addExtraButton(setting, uuid);
}
addExtraButton(setting: Setting, uuid: string) {
setting.addExtraButton(cb => {
cb.setIcon("cross");
cb.setTooltip("Remove");
cb.onClick(() => {
delete this.plugin.settings.languages[uuid];
this.plugin.saveSettings();
(this.plugin.app as any).commands.removeCommand(
`${this.plugin.manifest.id}:${uuid}`
);
setting.settingEl.hide();
});
});
}
}