This commit is contained in:
2026-01-16 22:54:17 +01:00
parent 55dbd82ca7
commit d8f039dc5e

View File

@@ -289,15 +289,33 @@
saveBtn.style.background = '#0ea5e9'; saveBtn.style.background = '#0ea5e9';
saveBtn.style.color = '#ffffff'; saveBtn.style.color = '#ffffff';
saveBtn.style.cursor = 'pointer'; saveBtn.style.cursor = 'pointer';
saveBtn.addEventListener('click', () => { const normalizeRteHtml = (rawHtml) => {
let html = String(content.innerHTML || '').trim(); const wrapper = doc.createElement('div');
const isTextLike = component && component.is && (component.is('text') || component.is('button') || component.is('link')); wrapper.innerHTML = String(rawHtml || '').trim();
// Unwrap single empty div wrapper inserted by contenteditable
if (wrapper.children.length === 1 && wrapper.firstElementChild.tagName === 'DIV' && wrapper.firstElementChild.attributes.length === 0) {
wrapper.innerHTML = wrapper.firstElementChild.innerHTML;
}
// Drop empty div/br artifacts
wrapper.querySelectorAll('div').forEach((el) => {
if (!el.attributes.length && !el.textContent.trim() && !el.querySelector('img,br,span,b,strong,i,em,u,a,ul,ol,li')) {
el.remove();
}
});
let html = wrapper.innerHTML.replace(/<br\s*\/?>/gi, '').trim();
if (!html) { if (!html) {
const fallbackText = String(content.textContent || '').trim(); const fallbackText = wrapper.textContent.trim();
if (fallbackText) { if (fallbackText) {
html = fallbackText.replace(/</g, '&lt;').replace(/>/g, '&gt;'); html = fallbackText.replace(/</g, '&lt;').replace(/>/g, '&gt;');
} }
} }
return html;
};
saveBtn.addEventListener('click', () => {
const rawHtml = content.innerHTML || '';
let html = normalizeRteHtml(rawHtml);
const isTextLike = component && component.is && (component.is('text') || component.is('button') || component.is('link'));
try { try {
if (!isTextLike && component.components) { if (!isTextLike && component.components) {
component.components(html); component.components(html);