This commit is contained in:
2026-01-21 00:23:27 +01:00
parent 5e76717a93
commit 8e2e9938a1

View File

@@ -46,6 +46,8 @@ export function initEditor() {
let isDirty = false;
let dirtyCleanup = null;
let dialogCancelBound = false;
let suppressDirty = false;
let suppressTimer = null;
  const ok  = (m) => toast(m, true);
  const err = (m) => toast(m, false);
@@ -120,6 +122,7 @@ export function initEditor() {
}
function markDirty() {
if (suppressDirty) return;
isDirty = true;
}
@@ -127,6 +130,15 @@ export function initEditor() {
isDirty = false;
}
function beginSuppressDirty(ms = 800) {
suppressDirty = true;
if (suppressTimer) clearTimeout(suppressTimer);
suppressTimer = setTimeout(() => {
suppressDirty = false;
suppressTimer = null;
}, ms);
}
function attachGjsDirtyTracker(editor) {
if (!editor || typeof editor.on !== 'function') return () => {};
const onUpdate = () => markDirty();
@@ -182,6 +194,7 @@ export function initEditor() {
}
function setSavedSnapshotFromData(payload) {
beginSuppressDirty();
const fields = extractContentFields(payload);
savedSnapshot = buildSnapshot(fields);
clearDirty();
@@ -275,6 +288,7 @@ export function initEditor() {
}
async function applyVersionPayload(payload) {
beginSuppressDirty();
const data = extractContentFields(payload);
const targetType = data.editorType === 'craftjs' ? 'craftjs' : 'grapesjs';
setEditorType(targetType);
@@ -305,6 +319,7 @@ export function initEditor() {
}
async function loadLatestContentFromServer() {
beginSuppressDirty();
const res = await apiAction('content.get', { method: 'GET', data: { id: current.id, section_id: current.section.id } });
await applyVersionPayload(res || {});
}
@@ -594,6 +609,7 @@ export function initEditor() {
setEditorType(editorType);
if (editorType === 'craftjs') {
const craftHtml = extractCraftHtml(craftJson, fresh);
beginSuppressDirty();
craftEditor?.setContent(craftHtml, craftJson);
if (dirtyCleanup) dirtyCleanup();
dirtyCleanup = attachCraftDirtyTracker();
@@ -691,6 +707,7 @@ export function initEditor() {
waitForEditor(6000)
.then((ed) => {
if (dirtyCleanup) dirtyCleanup();
beginSuppressDirty();
dirtyCleanup = attachGjsDirtyTracker(ed);
})
.catch(() => {});