This commit is contained in:
2026-01-15 03:37:24 +01:00
parent 0143adbc96
commit ae31b61cf1

View File

@@ -329,24 +329,37 @@
const closeModal = () => { const closeModal = () => {
editor.__bridgeRteAllowClose = true; editor.__bridgeRteAllowClose = true;
editor.__bridgeRteModalOpen = false; editor.__bridgeRteModalOpen = false;
if (typeof modal.close === 'function') {
modal.close();
} else {
const mdl = modal.getModel && modal.getModel(); const mdl = modal.getModel && modal.getModel();
if (modal.__bridgeOriginalClose) { if (mdl && typeof mdl.set === 'function') {
modal.__bridgeOriginalClose();
} else if (mdl && typeof mdl.set === 'function') {
mdl.set('open', false); mdl.set('open', false);
} else if (modal.el) { } else if (modal.el) {
modal.el.style.display = 'none'; modal.el.style.display = 'none';
} }
}
}; };
if (!modal.__bridgeCloseLocked) { if (!modal.__bridgeCloseLocked) {
modal.__bridgeCloseLocked = true; modal.__bridgeCloseLocked = true;
modal.__bridgeOriginalClose = modal.close ? modal.close.bind(modal) : null; modal.__bridgeOriginalClose = modal.close ? modal.close.bind(modal) : null;
modal.close = function (...args) { modal.close = function (...args) {
if (!editor.__bridgeRteModalOpen && typeof modal.__bridgeOriginalClose === 'function') {
return modal.__bridgeOriginalClose(...args);
}
if (editor.__bridgeRteAllowClose && typeof modal.__bridgeOriginalClose === 'function') { if (editor.__bridgeRteAllowClose && typeof modal.__bridgeOriginalClose === 'function') {
editor.__bridgeRteAllowClose = false; editor.__bridgeRteAllowClose = false;
return modal.__bridgeOriginalClose(...args); return modal.__bridgeOriginalClose(...args);
} }
if (!editor.__bridgeRteModalOpen && !modal.__bridgeOriginalClose) {
const mdl = modal.getModel && modal.getModel();
if (mdl && typeof mdl.set === 'function') {
mdl.set('open', false);
} else if (modal.el) {
modal.el.style.display = 'none';
}
}
if (editor.__bridgeRteAllowClose && !modal.__bridgeOriginalClose) { if (editor.__bridgeRteAllowClose && !modal.__bridgeOriginalClose) {
editor.__bridgeRteAllowClose = false; editor.__bridgeRteAllowClose = false;
const mdl = modal.getModel && modal.getModel(); const mdl = modal.getModel && modal.getModel();
@@ -535,12 +548,11 @@
const isTextLike = component && component.is && (component.is('text') || component.is('button') || component.is('link')); const isTextLike = component && component.is && (component.is('text') || component.is('button') || component.is('link'));
const hasTags = /<[^>]+>/.test(html); const hasTags = /<[^>]+>/.test(html);
try { try {
if (!isTextLike && component.components) { if (component.components) {
component.components(html); component.components(html);
} }
if (component.set) { if (component.set) {
const nextContent = (!hasTags && isTextLike) ? html : html; component.set('content', html);
component.set('content', nextContent);
component.trigger && component.trigger('change:content'); component.trigger && component.trigger('change:content');
component.trigger && component.trigger('change:components'); component.trigger && component.trigger('change:components');
} }
@@ -590,6 +602,15 @@
} catch { } catch {
modal.open(); modal.open();
} }
const modalEl = modal.getEl ? modal.getEl() : modal.el;
const closeBtn = modalEl && modalEl.querySelector ? modalEl.querySelector('.gjs-mdl-btn-close') : null;
if (closeBtn && !closeBtn.__bridgeRteBound) {
closeBtn.__bridgeRteBound = true;
closeBtn.addEventListener('click', () => {
editor.__bridgeRteAllowClose = true;
}, true);
}
}; };
const setupRichTextEditor = (editor) => { const setupRichTextEditor = (editor) => {
@@ -724,6 +745,12 @@
const setupPlainTextPreserver = (editor) => { const setupPlainTextPreserver = (editor) => {
const isTextLike = (model) => !!(model && model.is && (model.is('text') || model.is('button') || model.is('link'))); const isTextLike = (model) => !!(model && model.is && (model.is('text') || model.is('button') || model.is('link')));
const lastContent = new Map(); const lastContent = new Map();
const normalizeHtml = (value) => {
return String(value || '')
.replace(/<br\s*\/?>/gi, '')
.replace(/&nbsp;/g, '')
.trim();
};
const snapshotContent = (model) => { const snapshotContent = (model) => {
const el = model.view && model.view.el; const el = model.view && model.view.el;
const html = String(model.get ? (model.get('content') || '') : '').trim(); const html = String(model.get ? (model.get('content') || '') : '').trim();
@@ -732,26 +759,28 @@
const composite = html || inner || text; const composite = html || inner || text;
return { return {
html: composite, html: composite,
text, normalized: normalizeHtml(composite),
}; };
}; };
const rememberIfPresent = (model) => { const rememberIfPresent = (model) => {
if (!isTextLike(model)) return; if (!isTextLike(model)) return;
const snap = snapshotContent(model); const snap = snapshotContent(model);
if (!snap.html) return; if (!snap.normalized) return;
lastContent.set(model.cid || model, snap); lastContent.set(model.cid || model, snap);
}; };
const restoreIfEmpty = (model) => { const restoreIfEmpty = (model) => {
if (!isTextLike(model) || !model.get) return; if (!isTextLike(model) || !model.get) return;
const current = snapshotContent(model); const current = snapshotContent(model);
if (current.html) { if (current.normalized) {
rememberIfPresent(model); rememberIfPresent(model);
return; return;
} }
const stored = lastContent.get(model.cid || model); const stored = lastContent.get(model.cid || model);
if (!stored || !stored.html) return; if (!stored || !stored.normalized) return;
if (model.components && model.components().length) return;
try { try {
if (model.components) {
model.components(stored.html);
}
model.set('content', stored.html); model.set('content', stored.html);
model.trigger && model.trigger('change:content'); model.trigger && model.trigger('change:content');
} catch {} } catch {}