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

View File

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