-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicComponentLoader.jsx
More file actions
387 lines (340 loc) · 15.2 KB
/
DynamicComponentLoader.jsx
File metadata and controls
387 lines (340 loc) · 15.2 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
const { useState, useEffect, useRef } = dc;
const { Component: PreactComponent } = dc.preact;
function ErrorDisplay({ errorMessage }) {
const errorStyles = {
wrapper: { padding: '20px' },
details: {
fontFamily: 'sans-serif',
border: '1px solid #c53030',
borderRadius: '8px',
backgroundColor: '#2d1c1c',
color: '#fed7d7',
padding: '16px',
},
summary: {
cursor: 'pointer',
fontWeight: 'bold',
fontSize: '16px',
color: '#f56565',
listStyle: 'none',
display: 'flex',
alignItems: 'center',
},
summaryText: { marginLeft: '8px' },
content: {
marginTop: '12px',
borderTop: '1px solid #742a2a',
paddingTop: '12px',
color: '#e0e0e0',
fontSize: '14px',
},
pre: {
whiteSpace: 'pre-wrap',
wordWrap: 'break-word',
color: '#ccc',
fontSize: '13px',
marginTop: '12px',
padding: '10px',
backgroundColor: 'rgba(0,0,0,0.3)',
borderRadius: '4px',
fontFamily: 'monospace',
}
};
return (
<div style={errorStyles.wrapper}>
<details style={errorStyles.details} open>
<summary style={errorStyles.summary}>
<span>⚠️</span>
<span style={errorStyles.summaryText}>Component Error</span>
</summary>
<div style={errorStyles.content}>
<p>Failed to render component.</p>
<pre style={errorStyles.pre}>{errorMessage}</pre>
</div>
</details>
</div>
);
}
class ErrorBoundary extends PreactComponent {
constructor(props) {
super(props);
this.state = { hasError: false, error: null };
}
static getDerivedStateFromError(error) {
return { hasError: true, error };
}
componentDidCatch(error, info) {
console.error("ErrorBoundary caught an error:", error, info);
}
componentDidUpdate(prevProps) {
if (prevProps.renderKey !== this.props.renderKey) {
this.setState({ hasError: false, error: null });
}
}
render() {
if (this.state.hasError) {
return <ErrorDisplay errorMessage={this.state.error?.toString()} />;
}
return this.props.children;
}
}
function DynamicComponentLoader(props) {
const componentName = props.componentName;
const renderKey = props.renderKey;
const componentProps = props.componentProps || {};
const [LoadedComponent, setLoadedComponent] = useState(null);
const [loadError, setLoadError] = useState(null);
const [isLoading, setIsLoading] = useState(false);
const sandboxRef = useRef(null);
// DOM Mutation Observer to track and PREVENT full-tab escapes
useEffect(() => {
if (!LoadedComponent) return;
const sandboxBoundary = document.querySelector('.component-sandbox-isolator');
if (!sandboxBoundary) return;
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
mutation.removedNodes.forEach((node) => {
if (node.nodeType === 1 && node.classList && node.classList.contains('component-render-root')) {
const escapedElement = document.querySelector('.component-render-root');
if (escapedElement && !sandboxBoundary.contains(escapedElement)) {
const viewContent = sandboxBoundary.querySelector('.view-content');
if (viewContent) {
viewContent.appendChild(escapedElement);
if (escapedElement.style.position === 'absolute') {
escapedElement.style.position = 'relative';
}
}
}
}
});
}
});
});
observer.observe(sandboxBoundary, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['style']
});
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['style']
});
return () => observer.disconnect();
}, [LoadedComponent]);
useEffect(() => {
let isCancelled = false;
const loadComponent = async () => {
if (!componentName) {
setLoadedComponent(null);
setLoadError(null);
setIsLoading(false);
return;
}
setLoadedComponent(null);
setLoadError(null);
setIsLoading(true);
let originalGetActiveFile = null;
let originalRequire = null;
try {
const allFiles = app.vault.getMarkdownFiles();
const matchingFiles = allFiles.filter(file => {
const name = file.name.toLowerCase();
const search = componentName.toLowerCase();
const isMatch = name.includes(search);
const isType = name.includes('.component') || name.includes('.viewer');
return isMatch && isType && file.name.endsWith('.md');
});
if (matchingFiles.length === 0) {
const looseMatches = allFiles.filter(file =>
file.name.toLowerCase().includes(componentName.toLowerCase()) &&
file.name.endsWith('.md')
);
if (looseMatches.length > 0) {
matchingFiles.push(...looseMatches);
}
}
if (matchingFiles.length === 0) {
throw new Error(`No component found matching "${componentName}" in vault`);
}
matchingFiles.sort((a, b) => {
let scoreA = (a.name.includes('.viewer') ? 2 : 0) + (a.name.includes('.component') ? 1 : 0);
let scoreB = (b.name.includes('.viewer') ? 2 : 0) + (b.name.includes('.component') ? 1 : 0);
const folderPath = props.folderPath;
if (folderPath) {
const targetExample = folderPath + "/example";
if (a.path.includes(targetExample)) scoreA += 1000;
if (b.path.includes(targetExample)) scoreB += 1000;
if (a.path.includes(folderPath)) scoreA += 500;
if (b.path.includes(folderPath)) scoreB += 500;
}
if (a.path.includes('VIEWS INCEPTIONS/example')) scoreA += 200;
if (b.path.includes('VIEWS INCEPTIONS/example')) scoreB += 200;
return scoreB - scoreA;
});
const file = matchingFiles[0];
const filePath = file.path;
const calculatedFolderPath = filePath.substring(0, filePath.lastIndexOf('/'));
const mockFile = {
path: `${calculatedFolderPath}/mock-viewer.md`,
basename: 'mock-viewer',
extension: 'md',
parent: { path: calculatedFolderPath }
};
originalGetActiveFile = app.workspace.getActiveFile;
const stubbedGetActiveFile = () => mockFile;
app.workspace.getActiveFile = stubbedGetActiveFile;
if (typeof dc !== 'undefined' && dc.app && dc.app !== app) {
dc.app.workspace.getActiveFile = stubbedGetActiveFile;
}
originalRequire = dc.require;
dc.require = async (requirePath) => {
try {
return await originalRequire.call(dc, requirePath);
} catch (err) {
if (typeof requirePath === 'string' && err.message.includes('Could not find a script')) {
if (requirePath.includes('/src/src/')) {
const fixedPath = requirePath.replace('/src/src/', '/src/');
return await originalRequire.call(dc, fixedPath);
}
if (!requirePath.endsWith('.jsx') && !requirePath.endsWith('.js') && !requirePath.endsWith('.ts') && !requirePath.endsWith('.tsx') && !requirePath.endsWith('.md')) {
const fixedPath = requirePath + '.md';
return await originalRequire.call(dc, fixedPath);
}
}
throw err;
}
};
const fileContent = await app.vault.read(file);
const resolvedPath = dc.resolvePath(filePath);
const headerMatch = fileContent.match(/^#+\s+([^\r\n]+)/m);
let dynamicModule = null;
let loadedViaManual = false;
try {
let headerToUse = null;
if (headerMatch) {
headerToUse = headerMatch[1].trim();
} else if (fileContent.includes('# ViewComponent') || fileContent.includes('## ViewComponent')) {
headerToUse = "ViewComponent";
}
if (headerToUse) {
dynamicModule = await dc.require(dc.headerLink(resolvedPath, headerToUse));
} else {
try {
dynamicModule = await dc.require(dc.headerLink(resolvedPath, "ViewComponent"));
} catch (defaultErr) {
dynamicModule = await dc.require(resolvedPath);
}
}
} catch (requireErr) {
const codeBlockMatch = fileContent.match(/```(?:datacorejsx|jsx|js|ts|tsx)\r?\n([\s\S]*?)\r?\n```/);
if (codeBlockMatch) {
let code = codeBlockMatch[1];
if (code.includes('<')) {
code = code.replace(/<([A-Z]\w*)\s*\/>/g, 'dc.preact.h($1, null)');
code = code.replace(/<([A-Z]\w*)\s*>\s*<\/\1>/g, 'dc.preact.h($1, null)');
code = code.replace(/<([A-Z]\w*)\s+folderPath=\{([^}]+)\}\s*\/>/g, 'dc.preact.h($1, {folderPath: $2})');
}
try {
const AsyncFunction = Object.getPrototypeOf(async function () { }).constructor;
const manualFn = new AsyncFunction('dc', 'app', code);
dynamicModule = await manualFn(dc, app);
loadedViaManual = true;
} catch (manualErr) {
throw new Error(`Failed to load component. \nRequire error: ${requireErr.message}\nManual error: ${manualErr.message}`);
}
} else {
throw new Error("No header and no valid code block found in file.");
}
}
if (isCancelled) return;
let FactoryOrComp = null;
if (loadedViaManual) {
FactoryOrComp = dynamicModule;
} else {
if (typeof dynamicModule === 'function') {
FactoryOrComp = dynamicModule;
} else if (dynamicModule && typeof dynamicModule === 'object') {
const keys = Object.keys(dynamicModule);
if (keys.length > 0) FactoryOrComp = dynamicModule[keys[0]];
}
}
if (!FactoryOrComp) {
throw new Error("Module did not export a renderable component.");
}
let FinalComp = FactoryOrComp;
if (typeof FinalComp === 'object' && FinalComp !== null) {
const vnode = FinalComp;
FinalComp = () => vnode;
}
if (typeof FactoryOrComp === 'function' && !loadedViaManual && (FactoryOrComp.length > 0 || FactoryOrComp.constructor.name === 'AsyncFunction')) {
try {
const result = await FactoryOrComp({ folderPath: calculatedFolderPath });
if (typeof result === 'function') {
FinalComp = result;
} else if (result && typeof result === 'object') {
FinalComp = () => result;
}
} catch (err) {
console.error("[DynamicLoader] Failed to execute component factory:", err);
}
}
if (!isCancelled) {
setLoadedComponent(() => FinalComp);
setIsLoading(false);
}
} catch (err) {
console.error("[❌ Component Error]", err);
if (!isCancelled) {
setLoadError(err.toString());
setIsLoading(false);
}
} finally {
if (originalGetActiveFile) {
app.workspace.getActiveFile = originalGetActiveFile;
}
if (typeof dc !== 'undefined' && dc.app) {
dc.app.workspace.getActiveFile = originalGetActiveFile;
}
if (originalRequire) {
dc.require = originalRequire;
}
}
};
loadComponent();
return () => { isCancelled = true; };
}, [componentName, renderKey]);
if (isLoading) {
return (
<div style={{ padding: '20px', textAlign: 'center', color: '#888' }}>
<div style={{ fontSize: '32px', marginBottom: '12px' }}>⏳</div>
<div>Loading component...</div>
</div>
);
}
if (loadError) {
return <ErrorDisplay errorMessage={loadError} />;
}
if (LoadedComponent) {
return (
<div ref={sandboxRef} className="component-render-root">
<ErrorBoundary renderKey={renderKey}>
<LoadedComponent
key={`component-${renderKey}`}
{...componentProps}
/>
</ErrorBoundary>
</div>
);
}
return (
<div style={{ padding: '20px', textAlign: 'center', color: '#888' }}>
<div style={{ fontSize: '32px', marginBottom: '12px' }}>📦</div>
<div>Select a component to load</div>
</div>
);
}
return { ErrorDisplay, ErrorBoundary, DynamicComponentLoader };