From eac1980e62d0766e63874603aef8e3d4cd23ae7f Mon Sep 17 00:00:00 2001 From: Bruno Charest Date: Sat, 21 Mar 2026 12:43:30 -0400 Subject: [PATCH] Add textarea fallback for CodeMirror initialization failures and fix module dependency conflicts with import map --- frontend/app.js | 71 +++++++++++++++++++++++++-------------------- frontend/index.html | 23 ++++++++++----- frontend/style.css | 15 ++++++++++ 3 files changed, 70 insertions(+), 39 deletions(-) diff --git a/frontend/app.js b/frontend/app.js index 8ff8729..7f9525b 100644 --- a/frontend/app.js +++ b/frontend/app.js @@ -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; diff --git a/frontend/index.html b/frontend/index.html index cc678bc..3a18485 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -9,17 +9,24 @@ +