This commit is contained in:
2026-01-28 23:58:28 +01:00
parent 0179639ef6
commit 96ffb00527
2 changed files with 65 additions and 30 deletions

View File

@@ -368,6 +368,33 @@
.replace(/\scontenteditable="[^"]*"/gi, '')
.trim();
};
const getModelHtml = (model) => {
if (!model) return '';
let html = '';
try {
html = String(model.get ? model.get('content') || '' : '').trim();
} catch {}
if (!html && model.components) {
try {
const comps = model.components();
if (comps && typeof comps.toHTML === 'function') {
html = String(comps.toHTML() || '').trim();
}
} catch {}
}
if (!html && typeof model.toHTML === 'function') {
try {
const full = String(model.toHTML() || '').trim();
if (full) {
const wrapper = document.createElement('div');
wrapper.innerHTML = full;
const first = wrapper.firstElementChild;
html = first ? String(first.innerHTML || '').trim() : String(wrapper.innerHTML || '').trim();
}
} catch {}
}
return html;
};
const storeSnapshot = (model, snap) => {
if (!model || !snap) return;
const key = model.cid || model;
@@ -413,7 +440,7 @@
if (!isTextLike(target) || !target.view || !target.view.el) return;
const el = target.view.el;
if (el.isContentEditable || el.getAttribute('contenteditable') === 'true') return;
const modelHtml = String(target.get ? target.get('content') || '' : '').trim();
const modelHtml = String(getModelHtml(target) || '').trim();
if (!modelHtml) return;
const viewHtml = String(el.innerHTML || '').trim();
if (viewHtml) return;
@@ -433,17 +460,12 @@
if (window.__bridgeRteOpen) return;
const viewHtml = normalizeViewHtmlForModel(target.view.el.innerHTML || '');
if (!viewHtml) return;
const modelHtml = normalizeViewHtmlForModel(target.get ? target.get('content') || '' : '');
const modelHtml = normalizeViewHtmlForModel(getModelHtml(target));
if (viewHtml === modelHtml) return;
try {
syncing.add(target);
if (target.components) {
try {
const comps = target.components();
if (!comps || !comps.length) {
target.components(viewHtml);
}
} catch {}
try { target.components(viewHtml); } catch {}
}
if (target.set) target.set('content', viewHtml);
target.trigger && target.trigger('change:content');
@@ -471,18 +493,14 @@
if (window.__bridgeRteOpen) return;
const viewHtml = normalizeViewHtmlForModel(target.view.el.innerHTML || '');
if (!viewHtml) return;
const modelHtml = normalizeViewHtmlForModel(target.get ? target.get('content') || '' : '');
const modelHtml = normalizeViewHtmlForModel(getModelHtml(target));
const comps = target.components ? target.components() : null;
const hasComps = !!(comps && comps.length);
if (viewHtml === modelHtml && hasComps) return;
try {
syncing.add(target);
if (target.components) {
try {
if (!hasComps) {
target.components(viewHtml);
}
} catch {}
try { target.components(viewHtml); } catch {}
}
if (target.set) target.set('content', viewHtml);
target.trigger && target.trigger('change:content');
@@ -516,12 +534,39 @@
const setupBlurLogger = (editor) => {
if (!editor || !editor.Canvas || !editor.Canvas.getBody) return;
const getModelHtmlForLog = (model) => {
if (!model) return '';
let html = '';
try {
html = String(model.get ? model.get('content') || '' : '').trim();
} catch {}
if (!html && model.components) {
try {
const comps = model.components();
if (comps && typeof comps.toHTML === 'function') {
html = String(comps.toHTML() || '').trim();
}
} catch {}
}
if (!html && typeof model.toHTML === 'function') {
try {
const full = String(model.toHTML() || '').trim();
if (full) {
const wrapper = document.createElement('div');
wrapper.innerHTML = full;
const first = wrapper.firstElementChild;
html = first ? String(first.innerHTML || '').trim() : String(wrapper.innerHTML || '').trim();
}
} catch {}
}
return html;
};
const logComponentInput = (model, label) => {
try {
const modelType = model && model.get ? model.get('type') : undefined;
const selectedEl = model && model.view && model.view.el;
const viewOuter = selectedEl ? String(selectedEl.outerHTML || '') : '';
const modelContent = model && model.get ? String(model.get('content') || '') : '';
const modelContent = String(getModelHtmlForLog(model) || '');
const editorHtml = editor && typeof editor.getHtml === 'function' ? String(editor.getHtml() || '') : '';
console.warn(`[UI EDIT ${label}]`, {
modelType,
@@ -575,7 +620,7 @@
const buildSnapshot = (target, selected, selectedEl, editorHtml) => {
let modelContent = '';
try {
modelContent = selected && selected.get ? String(selected.get('content') || '') : '';
modelContent = String(getModelHtmlForLog(selected) || '');
} catch {}
let viewOuter = '';
try {
@@ -616,17 +661,12 @@
if (!viewEl) return;
const viewHtml = normalizeViewHtml(viewEl.innerHTML || '');
if (!viewHtml) return;
const modelHtml = normalizeViewHtml(component.get ? component.get('content') || '' : '');
const modelHtml = normalizeViewHtml(getModelHtml(component));
if (viewHtml === modelHtml) return;
try {
syncing.add(component);
if (component.components) {
try {
const comps = component.components();
if (!comps || !comps.length) {
component.components(viewHtml);
}
} catch {}
try { component.components(viewHtml); } catch {}
}
if (component.set) component.set('content', viewHtml);
component.trigger && component.trigger('change:content');
@@ -693,12 +733,7 @@
if (!viewHtml) return;
syncing.add(selected);
if (selected.components) {
try {
const comps = selected.components();
if (!comps || !comps.length) {
selected.components(viewHtml);
}
} catch {}
try { selected.components(viewHtml); } catch {}
}
if (selected.set) selected.set('content', viewHtml);
selected.trigger && selected.trigger('change:content');