-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
307 lines (268 loc) · 7.75 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
import { App, Plugin, PluginSettingTab, Setting } from 'obsidian';
import * as flowchart from 'flowchart.js';
interface FlowchartPluginSettings {
config: Record<string, any>;
}
const DEFAULT_SETTINGS: FlowchartPluginSettings = {
config: {
x: 0,
y: 0,
'line-width': 2,
'line-length': 50,
'text-margin': 10,
'font-size': 14,
'font-color': 'black',
'line-color': 'black',
'element-color': 'black',
fill: 'white',
'yes-text': 'yes',
'no-text': 'no',
'arrow-end': 'block',
scale: 1,
symbols: {
start: {
'font-color': 'black',
'element-color': 'black',
fill: 'white',
},
end: {
'font-color': 'black',
'element-color': 'black',
fill: 'white',
},
},
},
};
export default class FlowchartPlugin extends Plugin {
settings: FlowchartPluginSettings;
async onload() {
await this.loadSettings();
console.log('Flowchart plugin loaded');
// Register the markdown post processor for 'flowchart' code blocks
this.registerMarkdownCodeBlockProcessor('flowchart', (source, el, ctx) => {
this.renderFlowchart(source, el);
});
this.addSettingTab(new FlowchartSettingTab(this.app, this));
}
renderFlowchart(source: string, el: HTMLElement) {
// Wrap the rendering in a requestAnimationFrame for timing
requestAnimationFrame(() => {
try {
const config = this.mergeSymbolSettings(this.settings.config);
const diagram = flowchart.parse(source);
const container = el.createEl('div', {
cls: 'obsidian-flowchart-container',
});
diagram.drawSVG(container, config);
container
.querySelector('svg')
?.setAttribute('aria-label', 'Flowchart diagram');
// Apply a fix for deprecated xlink attributes
this.fixXlinkAttributes(container);
} catch (error) {
console.error('Error rendering flowchart: ', error);
el.createEl('div', {
cls: 'error-message',
text: 'Failed to render flowchart. Check your input.',
});
}
});
}
private mergeSymbolSettings(
config: Record<string, any>
): Record<string, any> {
console.log(config);
// Ensure symbols inherit main settings if not explicitly defined
const {
'font-color': fontColor,
'element-color': elementColor,
fill,
} = config;
const mergedSymbols = {
start: {
'font-color': fontColor,
'element-color': elementColor,
fill: fill,
},
end: {
'font-color': fontColor,
'element-color': elementColor,
fill: fill,
},
};
return {
...config,
symbols: {
...config.symbols,
...mergedSymbols,
},
};
}
fixXlinkAttributes(el: HTMLElement) {
// Find elements with deprecated xlink attributes and replace them
const elements = el.querySelectorAll('[*|href]');
elements.forEach((element) => {
const xlinkHref = element.getAttribute('xlink:href');
if (xlinkHref) {
element.setAttribute('href', xlinkHref);
element.removeAttribute('xlink:href');
}
});
}
onunload() {
console.log('Flowchart plugin unloaded');
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
}
class FlowchartSettingTab extends PluginSettingTab {
plugin: FlowchartPlugin;
constructor(app: App, plugin: FlowchartPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Flowchart Plugin Settings' });
this.addThemeNotice(containerEl);
this.addFlowchartSettings(containerEl);
this.addResetButton(containerEl);
}
private addFlowchartSettings(containerEl: HTMLElement): void {
new Setting(containerEl)
.setName('Line Width')
.setDesc('Set the line width for the flowchart.')
.addText((text) =>
text
.setValue(this.plugin.settings.config['line-width'].toString())
.onChange(async (value) => {
this.plugin.settings.config['line-width'] = parseInt(value) || 2;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('Line Length')
.setDesc('Set the line length for the flowchart.')
.addText((text) =>
text
.setValue(this.plugin.settings.config['line-length'].toString())
.onChange(async (value) => {
this.plugin.settings.config['line-length'] = parseInt(value) || 2;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('Yes Text')
.setDesc('Text for Yes responses in the flowchart.')
.addText((text) =>
text
.setValue(this.plugin.settings.config['yes-text'])
.onChange(async (value) => {
this.plugin.settings.config['yes-text'] = value || 'yes';
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('No Text')
.setDesc('Text for No responses in the flowchart.')
.addText((text) =>
text
.setValue(this.plugin.settings.config['no-text'])
.onChange(async (value) => {
this.plugin.settings.config['no-text'] = value || 'no';
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('Font Size')
.setDesc('Set the font size for the flowchart.')
.addText((text) =>
text
.setValue(this.plugin.settings.config['font-size'].toString())
.onChange(async (value) => {
this.plugin.settings.config['font-size'] = parseInt(value) || 14;
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('Font Color')
.setDesc('Set the font color for the flowchart.')
.addColorPicker((picker) =>
picker
.setValue(this.plugin.settings.config['font-color'])
.onChange(async (value) => {
this.plugin.settings.config['font-color'] = value || 'black';
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('Line Color')
.setDesc('Set the line color for the flowchart.')
.addColorPicker((picker) =>
picker
.setValue(this.plugin.settings.config['line-color'])
.onChange(async (value) => {
this.plugin.settings.config['line-color'] = value || 'black';
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('Element Color')
.setDesc('Set the element color for the flowchart.')
.addColorPicker((picker) =>
picker
.setValue(this.plugin.settings.config['element-color'])
.onChange(async (value) => {
this.plugin.settings.config['element-color'] = value || 'black';
await this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('Fill Color')
.setDesc('Set the background fill color for the flowchart.')
.addColorPicker((picker) =>
picker
.setValue(this.plugin.settings.config['fill'])
.onChange(async (value) => {
this.plugin.settings.config['fill'] = value || 'white';
await this.plugin.saveSettings();
})
);
}
private addResetButton(containerEl: HTMLElement): void {
new Setting(containerEl)
.setName('Reset Settings')
.setDesc('Reset all settings to their default values.')
.addButton((button) =>
button.setButtonText('Reset').onClick(async () => {
console.log('Before reset:', this.plugin.settings);
this.plugin.settings = JSON.parse(JSON.stringify(DEFAULT_SETTINGS));
console.log('After reset:', this.plugin.settings);
await this.plugin.saveSettings();
this.display();
})
);
}
private addThemeNotice(containerEl: HTMLElement): void {
const isDarkMode = document.body.classList.contains('theme-dark');
const isLightMode = document.body.classList.contains('theme-light');
let mode = '';
if (isDarkMode) {
mode = 'Dark';
} else if (isLightMode) {
mode = 'Light';
}
if (!mode) {
return;
}
containerEl.createEl('div', {
text: `You are in ${mode} Mode. Adjust colors for better visibility!`,
cls: 'theme-notice',
});
}
}