fix(editor): use $state for prop tracking to fix Svelte state_referenced_locally warnings

This commit is contained in:
Hibryda 2026-03-10 03:13:36 +01:00
parent 61cd33f393
commit 86fa55bc3e

View file

@ -234,29 +234,31 @@
}); });
// When content prop changes externally (different file loaded), replace editor content // When content prop changes externally (different file loaded), replace editor content
let lastContent = content; let lastContent = $state(content);
$effect(() => { $effect(() => {
if (view && content !== lastContent) { const c = content;
if (view && c !== lastContent) {
const currentDoc = view.state.doc.toString(); const currentDoc = view.state.doc.toString();
if (content !== currentDoc) { if (c !== currentDoc) {
view.dispatch({ view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: content }, changes: { from: 0, to: view.state.doc.length, insert: c },
}); });
} }
lastContent = content; lastContent = c;
} }
}); });
// When lang changes, recreate editor // When lang changes, recreate editor
let lastLang = lang; let lastLang = $state(lang);
$effect(() => { $effect(() => {
if (lang !== lastLang && view) { const l = lang;
lastLang = lang; if (l !== lastLang && view) {
lastLang = l;
const currentContent = view.state.doc.toString(); const currentContent = view.state.doc.toString();
view.destroy(); view.destroy();
// Small delay to let DOM settle // Small delay to let DOM settle
queueMicrotask(async () => { queueMicrotask(async () => {
const langExt = await getLangExtension(lang); const langExt = await getLangExtension(l);
const extensions = [ const extensions = [
lineNumbers(), lineNumbers(),
highlightActiveLineGutter(), highlightActiveLineGutter(),