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
|
import type { EditorView } from 'codemirror';
import { createEditor, setEditorTheme } from './editor';
// Global API
const adelieEditor = {
/**
* Initialize an editor in the given element
* @param element - The container element or selector
* @param options - Editor configuration
*/
init(
element: HTMLElement | string,
options?: {
language?: 'javascript' | 'css' | 'html' | 'tabloid';
initialCode?: string;
theme?: 'light' | 'dark';
}
) {
const container =
typeof element === 'string' ? document.querySelector<HTMLElement>(element) : element;
if (!container) {
throw new Error('Editor container not found');
}
// Auto-detect theme if not specified
const theme =
options?.theme ||
(document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'light');
const view = createEditor({
parent: container,
language: options?.language || 'javascript',
initialCode: options?.initialCode || '',
theme,
});
// Listen for theme changes
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.attributeName === 'data-theme') {
const newTheme =
document.documentElement.getAttribute('data-theme') === 'dark'
? 'dark'
: 'light';
setEditorTheme(view, newTheme);
}
}
});
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['data-theme'],
});
return view;
},
/**
* Create an editor with full control
*/
create: createEditor,
/**
* Change editor theme
*/
setTheme: setEditorTheme,
};
// Export to window and as default
if (typeof window !== 'undefined') {
(window as unknown as Record<string, unknown>).adelieEditor = adelieEditor;
}
// Also export for module systems
export default adelieEditor;
|