generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
229 lines (201 loc) · 8.17 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
import { App, FileSystemAdapter, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
import path from 'path';
import Renderer from 'common/lib/render';
import i18n from "./common/lib/lang";
interface TyporadifySettings {
customCss: string;
theme: string;
displayLineNumbers: boolean;
applyLineBreaks: boolean;
autoNumbering: boolean;
landscape: boolean;
marginsType: number;
pageSize: string;
printBackground: boolean;
}
const DEFAULT_SETTINGS: TyporadifySettings = {
customCss: "",
theme: "",
displayLineNumbers: false,
applyLineBreaks: false,
autoNumbering: true,
landscape: false,
marginsType: 0,
pageSize: "A4",
printBackground: false
}
const t = i18n();
export default class Typoradify extends Plugin {
settings: TyporadifySettings;
async onload() {
await this.loadSettings();
await this.registerCommands();
await this.registerSettingTabs();
}
async registerCommands() {
this.addCommand({
id: 'render-html',
name: t("command.html"),
callback: async () => {
new Notice(t("notice.html", { filename: path.basename(await this.getCurrentFile() as string) }) as string);
await Renderer.renderHTML(global, await this.getCurrentFile() as string);
}
});
this.addCommand({
id: 'render-raw-html',
name: t("command.rawhtml"),
callback: async () => {
new Notice(t("notice.rawhtml", { filename: path.basename(await this.getCurrentFile() as string) }) as string);
await Renderer.renderRawHTML(global, await this.getCurrentFile() as string);
}
});
this.addCommand({
id: 'render-pdf',
name: t("command.pdf"),
callback: async () => {
new Notice(t("notice.pdf", { filename: path.basename(await this.getCurrentFile() as string) }) as string);
await Renderer.renderPDF(global, await this.getCurrentFile() as string);
}
});
}
async registerSettingTabs() {
this.addSettingTab(new TyporadifySettingTab(this.app, this));
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async getCurrentFile(): Promise<string | null> {
const fileData = this.app.workspace.getActiveFile();
if (!fileData) return null;
const adapter = this.app.vault.adapter;
if (adapter instanceof FileSystemAdapter)
return adapter.getFullPath(fileData.path);
return null;
}
}
class TyporadifySettingTab extends PluginSettingTab {
plugin: Typoradify;
constructor(app: App, plugin: Typoradify) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('div', {
text: t("title") as string, attr: {
class: "setting-item-name"
}
});
containerEl.createEl('div', {
text: t("description") as string, attr: {
class: "setting-item-description"
}
});
containerEl.createEl('h2', { text: t("setting.parser.title") as string });
new Setting(containerEl)
.setName(t("setting.parser.autoNumbering.name") as string)
.setDesc(t("setting.parser.autoNumbering.description") as string)
.addToggle(toggle => toggle
.setValue(this.plugin.settings.autoNumbering)
.onChange(async (value) => {
console.log(`[CONFIG] Change autoNumbering to ${value}`);
this.plugin.settings.autoNumbering = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(t("setting.parser.applyLineBreaks.name") as string)
.setDesc(t("setting.parser.applyLineBreaks.description") as string)
.addToggle(toggle => toggle
.setValue(this.plugin.settings.applyLineBreaks)
.onChange(async (value) => {
console.log(`[CONFIG] Change applyLineBreaks to ${value}`);
this.plugin.settings.applyLineBreaks = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(t("setting.parser.displayLineNumbers.name") as string)
.setDesc(t("setting.parser.displayLineNumbers.description") as string)
.addToggle(toggle => toggle
.setValue(this.plugin.settings.displayLineNumbers)
.onChange(async (value: boolean) => {
console.log(`[CONFIG] Change displayLineNumbers to ${value}`);
this.plugin.settings.displayLineNumbers = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(t("setting.parser.theme.name") as string)
.setDesc(t("setting.parser.theme.description") as string)
.addText(text => text
.setPlaceholder(t("setting.parser.theme.placeholder") as string)
.setValue(this.plugin.settings.theme)
.onChange(async (value: string) => {
console.log(`[CONFIG] Change theme file to ${value}`);
this.plugin.settings.theme = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(t("setting.parser.customCss.name") as string)
.setDesc(t("setting.parser.customCss.description") as string)
.addTextArea(textarea => textarea
.setPlaceholder(t("setting.parser.customCss.placeholder") as string)
.setValue(this.plugin.settings.customCss)
.onChange(async (value: string) => {
console.log(`[CONFIG] Change customCss to ${value}`);
this.plugin.settings.customCss = value;
await this.plugin.saveSettings();
}));
containerEl.createEl('h2', { text: t("setting.printer.title") as string });
new Setting(containerEl)
.setName(t("setting.printer.landscape.name") as string)
.setDesc(t("setting.printer.landscape.description") as string)
.addDropdown(dropdown => dropdown
.addOptions({ "false": t("setting.printer.landscape.option.portrait") as string, "true": t("setting.printer.landscape.option.landscape") as string })
.setValue((this.plugin.settings.landscape ? "true" : "false"))
.onChange(async (value) => {
console.log(`[CONFIG] Change landscape to ${value}`);
this.plugin.settings.landscape = (value === "true");
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(t("setting.printer.marginsType.name") as string)
.setDesc(t("setting.printer.marginsType.description") as string)
.addDropdown(dropdown => dropdown
.addOptions({ 0: t("setting.printer.marginsType.option.default") as string, 1: t("setting.printer.marginsType.option.min") as string, 2: t("setting.printer.marginsType.option.max") as string })
.setValue(this.plugin.settings.marginsType.toString())
.onChange(async (value) => {
console.log(`[CONFIG] Change marginsType to ${value}`);
this.plugin.settings.marginsType = parseInt(value);
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(t("setting.printer.pageSize.name") as string)
.setDesc(t("setting.printer.pageSize.description") as string)
.addDropdown(dropdown => dropdown
.addOptions({ A3: "A3", A4: "A4", A5: "A5", Legal: "Legal", Letter: "Letter", Tabloid: "Tabloid" })
.setValue(this.plugin.settings.pageSize)
.onChange(async (value) => {
console.log(`[CONFIG] Change pageSize to ${value}`);
this.plugin.settings.pageSize = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName(t("setting.printer.printBackground.name") as string)
.setDesc(t("setting.printer.printBackground.description") as string)
.addToggle(toggle => toggle
.setValue(this.plugin.settings.printBackground)
.onChange(async (value) => {
console.log(`[CONFIG] Change printBackground to ${value}`);
this.plugin.settings.printBackground = value;
await this.plugin.saveSettings();
}));
containerEl.createEl('div', {
text: "この世界は優しくも正しくもないので、優しくて正しい人が生き残ることは常に困難です——比企谷八幡", attr: {
class: "setting-item-description"
}
});
}
}