diff --git a/frontend/src/lib/components/Editor.svelte b/frontend/src/lib/components/Editor.svelte index 8e573c766d..57bebf80a3 100644 --- a/frontend/src/lib/components/Editor.svelte +++ b/frontend/src/lib/components/Editor.svelte @@ -344,26 +344,36 @@ } export function setCode(ncode: string, noHistory: boolean = false): void { - code = ncode - if (noHistory) { - editor?.setValue(ncode) - } else { - if (editor?.getModel()) { - // editor.setValue(ncode) - editor.pushUndoStop() + if (code != ncode) { + if (noHistory) { + editor?.setValue(ncode) + } else { + if (editor?.getModel()) { + // editor.setValue(ncode) + editor.pushUndoStop() - editor.executeEdits('set', [ - { - range: editor.getModel()!.getFullModelRange(), // full range - text: ncode - } - ]) + editor.executeEdits('set', [ + { + range: editor.getModel()!.getFullModelRange(), // full range + text: ncode + } + ]) - editor.pushUndoStop() + editor.pushUndoStop() + } } } } + function updateCode(): boolean { + const ncode = getCode() + if (code == ncode) { + return false + } + code = ncode + return true + } + export function append(code: string): void { if (editor) { const lineCount = editor.getModel()?.getLineCount() || 0 @@ -387,7 +397,7 @@ export async function format() { if (editor) { - code = getCode() + updateCode() if (lang != 'shell' && lang != 'nu') { if ($formatOnSave != false) { if (scriptLang == 'deno' && languageClients.length > 0) { @@ -424,7 +434,7 @@ await editor?.getAction('editor.action.formatDocument')?.run() } } - code = getCode() + updateCode() } if (formatAction) { formatAction() @@ -1275,9 +1285,9 @@ editor?.onDidChangeModelContent((event) => { timeoutModel && clearTimeout(timeoutModel) timeoutModel = setTimeout(() => { - let ncode = getCode() - code = ncode - dispatch('change', ncode) + if (updateCode()) { + dispatch('change', code) + } }, changeTimeout) ataModel && clearTimeout(ataModel) @@ -1296,12 +1306,12 @@ dispatch('focus') editor?.addCommand(KeyMod.CtrlCmd | KeyCode.KeyS, function () { - code = getCode() + updateCode() shouldBindKey && format && format() }) editor?.addCommand(KeyMod.CtrlCmd | KeyCode.Enter, function () { - code = getCode() + updateCode() shouldBindKey && cmdEnterAction && cmdEnterAction() }) diff --git a/frontend/src/lib/components/SimpleEditor.svelte b/frontend/src/lib/components/SimpleEditor.svelte index 7cca3416ae..91bc2be3c0 100644 --- a/frontend/src/lib/components/SimpleEditor.svelte +++ b/frontend/src/lib/components/SimpleEditor.svelte @@ -157,8 +157,19 @@ } export function setCode(ncode: string): void { + if (ncode != code) { + code = ncode + editor?.setValue(ncode) + } + } + + function updateCode(): boolean { + const ncode = getCode() + if (code == ncode) { + return false + } code = ncode - editor?.setValue(ncode) + return true } function updatePlaceholderVisibility(value: string) { @@ -171,11 +182,11 @@ export function format() { if (editor) { - code = getCode() + updateCode() editor.getAction('editor.action.formatDocument')?.run() if (formatAction) { formatAction() - code = getCode() + updateCode() } } } @@ -368,8 +379,9 @@ suggestion = '' timeoutModel && clearTimeout(timeoutModel) timeoutModel = setTimeout(() => { - code = getCode() - dispatch('change', { code }) + if (updateCode()) { + dispatch('change', { code }) + } }, 200) }) @@ -379,7 +391,7 @@ loadExtraLib() editor.addCommand(KeyMod.CtrlCmd | KeyCode.KeyS, function () { - code = getCode() + updateCode() shouldBindKey && format && format() }) @@ -411,12 +423,12 @@ editor.onDidFocusEditorText(() => { if (!editor) return editor.addCommand(KeyMod.CtrlCmd | KeyCode.KeyS, function () { - code = getCode() + updateCode() shouldBindKey && format && format() }) editor.addCommand(KeyMod.CtrlCmd | KeyCode.Enter, function () { - code = getCode() + updateCode() shouldBindKey && cmdEnterAction && cmdEnterAction() }) dispatch('focus') @@ -424,8 +436,7 @@ editor.onDidBlurEditorText(() => { dispatch('blur') - - code = getCode() + updateCode() }) if (lang === 'css' && !cssClassesLoaded) { diff --git a/frontend/src/lib/components/TemplateEditor.svelte b/frontend/src/lib/components/TemplateEditor.svelte index c76e016a2b..0dfdfedfab 100644 --- a/frontend/src/lib/components/TemplateEditor.svelte +++ b/frontend/src/lib/components/TemplateEditor.svelte @@ -380,7 +380,6 @@ $componentControl[$selectedComponent[0]] = { ...$componentControl[$selectedComponent[0]], setCode: (value: string) => { - code = value setCode(value) } } @@ -411,9 +410,9 @@ } export function setCode(ncode: string): void { - code = ncode - if (editor) { - editor.setValue(ncode) + if (code != ncode) { + code = ncode + editor?.setValue(ncode) } } @@ -473,15 +472,22 @@ editor.addCommand(KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.Digit7, function () {}) }) + function updateCode(): boolean { + const ncode = getCode() + if (code == ncode) { + return false + } + code = ncode + return true + } + let timeoutModel: NodeJS.Timeout | undefined = undefined editor.onDidChangeModelContent((event) => { timeoutModel && clearTimeout(timeoutModel) timeoutModel = setTimeout(() => { - const newCode = getCode() - if (code != newCode) { - code = newCode + if (updateCode()) { + dispatch('change', { code }) } - dispatch('change', { code: newCode }) }, 200) }) @@ -507,10 +513,7 @@ editor.onDidBlurEditorText(() => { dispatch('blur') - const newCode = getCode() - if (code != newCode) { - code = newCode - } + updateCode() }) jsLoader = setTimeout(async () => {