Add textarea fallback for CodeMirror initialization failures and fix module dependency conflicts with import map

This commit is contained in:
2026-03-21 12:43:30 -04:00
parent 25c7533409
commit eac1980e62
3 changed files with 70 additions and 39 deletions
+40 -31
View File
@@ -16,6 +16,7 @@
let editorView = null;
let editorVault = null;
let editorPath = null;
let fallbackEditorEl = null;
// ---------------------------------------------------------------------------
// File extension → Lucide icon mapping
@@ -846,41 +847,48 @@
editorView.destroy();
editorView = null;
}
fallbackEditorEl = null;
// Wait for CodeMirror to be available
await waitForCodeMirror();
try {
await waitForCodeMirror();
const { EditorView, EditorState, basicSetup, markdown, oneDark, keymap } = window.CodeMirror;
const { EditorView, EditorState, basicSetup, markdown, oneDark, keymap } = window.CodeMirror;
// Determine theme
const currentTheme = document.documentElement.getAttribute("data-theme");
const extensions = [
basicSetup,
markdown(),
keymap.of([{
key: "Mod-s",
run: () => {
saveFile();
return true;
}
}]),
EditorView.lineWrapping,
];
const currentTheme = document.documentElement.getAttribute("data-theme");
const extensions = [
basicSetup,
markdown(),
keymap.of([{
key: "Mod-s",
run: () => {
saveFile();
return true;
}
}]),
EditorView.lineWrapping,
];
if (currentTheme === "dark") {
extensions.push(oneDark);
if (currentTheme === "dark") {
extensions.push(oneDark);
}
const state = EditorState.create({
doc: rawData.raw,
extensions: extensions,
});
editorView = new EditorView({
state: state,
parent: bodyEl,
});
} catch (err) {
console.error("CodeMirror init failed, falling back to textarea:", err);
fallbackEditorEl = document.createElement("textarea");
fallbackEditorEl.className = "fallback-editor";
fallbackEditorEl.value = rawData.raw;
bodyEl.appendChild(fallbackEditorEl);
}
const state = EditorState.create({
doc: rawData.raw,
extensions: extensions,
});
editorView = new EditorView({
state: state,
parent: bodyEl,
});
modal.classList.add("active");
safeCreateIcons();
}
@@ -903,14 +911,15 @@
editorView.destroy();
editorView = null;
}
fallbackEditorEl = null;
editorVault = null;
editorPath = null;
}
async function saveFile() {
if (!editorView || !editorVault || !editorPath) return;
if ((!editorView && !fallbackEditorEl) || !editorVault || !editorPath) return;
const content = editorView.state.doc.toString();
const content = editorView ? editorView.state.doc.toString() : fallbackEditorEl.value;
const saveBtn = document.getElementById("editor-save");
const originalText = saveBtn.textContent;