-
Notifications
You must be signed in to change notification settings - Fork 28
/
main.ts
186 lines (133 loc) · 4.23 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
import { Plugin, WorkspaceWindow } from 'obsidian';
import { TikzjaxPluginSettings, DEFAULT_SETTINGS, TikzjaxSettingTab } from "./settings";
import { optimize } from "./svgo.browser";
// @ts-ignore
import tikzjaxJs from 'inline:./tikzjax.js';
export default class TikzjaxPlugin extends Plugin {
settings: TikzjaxPluginSettings;
async onload() {
await this.loadSettings();
this.addSettingTab(new TikzjaxSettingTab(this.app, this));
// Support pop-out windows
this.app.workspace.onLayoutReady(() => {
this.loadTikZJaxAllWindows();
this.registerEvent(this.app.workspace.on("window-open", (win, window) => {
this.loadTikZJax(window.document);
}));
});
this.addSyntaxHighlighting();
this.registerTikzCodeBlock();
}
onunload() {
this.unloadTikZJaxAllWindows();
this.removeSyntaxHighlighting();
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
loadTikZJax(doc: Document) {
const s = document.createElement("script");
s.id = "tikzjax";
s.type = "text/javascript";
s.innerText = tikzjaxJs;
doc.body.appendChild(s);
doc.addEventListener('tikzjax-load-finished', this.postProcessSvg);
}
unloadTikZJax(doc: Document) {
const s = doc.getElementById("tikzjax");
s.remove();
doc.removeEventListener("tikzjax-load-finished", this.postProcessSvg);
}
loadTikZJaxAllWindows() {
for (const window of this.getAllWindows()) {
this.loadTikZJax(window.document);
}
}
unloadTikZJaxAllWindows() {
for (const window of this.getAllWindows()) {
this.unloadTikZJax(window.document);
}
}
getAllWindows() {
// Via https://discord.com/channels/686053708261228577/840286264964022302/991591350107635753
const windows = [];
// push the main window's root split to the list
windows.push(this.app.workspace.rootSplit.win);
// @ts-ignore floatingSplit is undocumented
const floatingSplit = this.app.workspace.floatingSplit;
floatingSplit.children.forEach((child: any) => {
// if this is a window, push it to the list
if (child instanceof WorkspaceWindow) {
windows.push(child.win);
}
});
return windows;
}
registerTikzCodeBlock() {
this.registerMarkdownCodeBlockProcessor("tikz", (source, el, ctx) => {
const script = el.createEl("script");
script.setAttribute("type", "text/tikz");
script.setAttribute("data-show-console", "true");
script.setText(this.tidyTikzSource(source));
});
}
addSyntaxHighlighting() {
// @ts-ignore
window.CodeMirror.modeInfo.push({name: "Tikz", mime: "text/x-latex", mode: "stex"});
}
removeSyntaxHighlighting() {
// @ts-ignore
window.CodeMirror.modeInfo = window.CodeMirror.modeInfo.filter(el => el.name != "Tikz");
}
tidyTikzSource(tikzSource: string) {
// Remove non-breaking space characters, otherwise we get errors
const remove = " ";
tikzSource = tikzSource.replaceAll(remove, "");
let lines = tikzSource.split("\n");
// Trim whitespace that is inserted when pasting in code, otherwise TikZJax complains
lines = lines.map(line => line.trim());
// Remove empty lines
lines = lines.filter(line => line);
return lines.join("\n");
}
colorSVGinDarkMode(svg: string) {
// Replace the color "black" with currentColor (the current text color)
// so that diagram axes, etc are visible in dark mode
// And replace "white" with the background color
svg = svg.replaceAll(/("#000"|"black")/g, `"currentColor"`)
.replaceAll(/("#fff"|"white")/g, `"var(--background-primary)"`);
return svg;
}
optimizeSVG(svg: string) {
// Optimize the SVG using SVGO
// Fixes misaligned text nodes on mobile
return optimize(svg, {plugins:
[
{
name: 'preset-default',
params: {
overrides: {
// Don't use the "cleanupIDs" plugin
// To avoid problems with duplicate IDs ("a", "b", ...)
// when inlining multiple svgs with IDs
cleanupIDs: false
}
}
}
]
// @ts-ignore
}).data;
}
postProcessSvg = (e: Event) => {
const svgEl = e.target as HTMLElement;
let svg = svgEl.outerHTML;
if (this.settings.invertColorsInDarkMode) {
svg = this.colorSVGinDarkMode(svg);
}
svg = this.optimizeSVG(svg);
svgEl.outerHTML = svg;
}
}