-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
191 lines (165 loc) · 6.87 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
import { Plugin, MarkdownPostProcessorContext, PluginSettingTab, App, Setting } from 'obsidian';
import { FlowChart } from '@solothought/text2chart';
import text2chartCSS from '@solothought/text2chart/style.css';
// Settings interface
interface StFlowSettings {
defaultWidth: string;
defaultHeight: string;
}
const DEFAULT_SETTINGS: StFlowSettings = {
defaultWidth: '800px',
defaultHeight: '600px'
};
export default class StFlowPlugin extends Plugin {
settings: StFlowSettings;
private fullscreenChart: FlowChart | null = null; // Track fullscreen instance
async onload() {
this.devLog('Loading StFlow Plugin');
await this.loadSettings();
this.addStyle();
this.registerMarkdownCodeBlockProcessor('stflow', this.processStFlow.bind(this));
this.addSettingTab(new StFlowSettingTab(this.app, this));
}
private addStyle() {
const style = document.createElement('style');
style.setAttribute('data-plugin', 'stflow'); // Mark for cleanup
style.appendChild(document.createTextNode(text2chartCSS));
document.head.appendChild(style);
}
async processStFlow(source: string, el: HTMLElement, ctx: MarkdownPostProcessorContext) {
try {
// Parse metadata for custom size
let width = this.settings.defaultWidth;
let height = this.settings.defaultHeight;
let flowchartText = source;
const lines = source.split('\n');
let metadataEnd = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i].trim();
if (!line.startsWith('#')) {
metadataEnd = i;
break;
}
const parts = line.substring(1).split(';').map(part => part.trim());
for (const part of parts) {
if (part.startsWith('width:')) {
width = part.split(':')[1].trim();
} else if (part.startsWith('height:')) {
height = part.split(':')[1].trim();
} else if (part.match(/^\s*width\s*:/)) {
width = part.split(':')[1].trim();
} else if (part.match(/^\s*height\s*:/)) {
height = part.split(':')[1].trim();
}
}
if (i === lines.length - 1) metadataEnd = lines.length;
}
flowchartText = lines.slice(metadataEnd).join('\n');
// Create container
const container = el.createEl('div', { cls: 'stflow-container' });
const chartContainer = container.createEl('div', { cls: 'stflow-chart' });
// Initialize FlowChart
const chart = new FlowChart({
target: chartContainer,
props: {
text: flowchartText,
width: width,
height: height,
minimap: false,
toolbar: false,
}
});
// Add fullscreen icon
const fullscreenIcon = el.createEl('span', {
cls: 'stflow-fullscreen-icon',
text: '⛶'
});
// Toggle fullscreen
fullscreenIcon.addEventListener('click', () => {
if (!this.fullscreenChart) {
// Create fullscreen overlay
const overlay = document.body.appendChild(document.createElement('div'));
overlay.className = 'stflow-fullscreen-overlay';
const content = overlay.appendChild(document.createElement('div'));
content.className = 'stflow-fullscreen-content';
// Render fullscreen chart
this.fullscreenChart = new FlowChart({
target: content,
props: {
text: flowchartText,
width: '90vw',
height: '90vh'
}
});
// Add close button
const closeButton = content.appendChild(document.createElement('span'));
closeButton.className = 'stflow-fullscreen-close';
closeButton.textContent = '✖'; // Close symbol
closeButton.addEventListener('click', () => {
overlay.remove();
this.fullscreenChart?.$destroy();
this.fullscreenChart = null;
fullscreenIcon.textContent = '⛶';
});
fullscreenIcon.textContent = '🗕'; // Minimize symbol while open
}
});
} catch (error) {
const errorEl = el.createEl('div', { cls: 'stflow-error' });
errorEl.textContent = `Error rendering flowchart: ${error.message}`;
}
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
onunload() {
this.devLog('Unloading StFlow Plugin');
if (this.fullscreenChart) {
this.fullscreenChart.$destroy();
this.fullscreenChart = null;
}
// Remove dynamically added styles
const styleElements = document.querySelectorAll('style[data-plugin="stflow"]');
styleElements.forEach(el => el.remove());
}
private devLog(...args: any[]) {
if (process.env.NODE_ENV === 'development') {
console.log(...args);
}
}
}
// Settings tab
class StFlowSettingTab extends PluginSettingTab {
plugin: StFlowPlugin;
constructor(app: App, plugin: StFlowPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName('Default flowchart width')
.setDesc('Set the default width (e.g., 800px, 100%)')
.addText(text => text
.setPlaceholder('800px')
.setValue(this.plugin.settings.defaultWidth)
.onChange(async (value) => {
this.plugin.settings.defaultWidth = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Default flowchart height')
.setDesc('Set the default height (e.g., 600px, 100%)')
.addText(text => text
.setPlaceholder('600px')
.setValue(this.plugin.settings.defaultHeight)
.onChange(async (value) => {
this.plugin.settings.defaultHeight = value;
await this.plugin.saveSettings();
}));
}
}