RTE
This commit is contained in:
@@ -289,15 +289,33 @@
|
||||
saveBtn.style.background = '#0ea5e9';
|
||||
saveBtn.style.color = '#ffffff';
|
||||
saveBtn.style.cursor = 'pointer';
|
||||
saveBtn.addEventListener('click', () => {
|
||||
let html = String(content.innerHTML || '').trim();
|
||||
const isTextLike = component && component.is && (component.is('text') || component.is('button') || component.is('link'));
|
||||
const normalizeRteHtml = (rawHtml) => {
|
||||
const wrapper = doc.createElement('div');
|
||||
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) {
|
||||
const fallbackText = String(content.textContent || '').trim();
|
||||
const fallbackText = wrapper.textContent.trim();
|
||||
if (fallbackText) {
|
||||
html = fallbackText.replace(/</g, '<').replace(/>/g, '>');
|
||||
}
|
||||
}
|
||||
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 {
|
||||
if (!isTextLike && component.components) {
|
||||
component.components(html);
|
||||
|
||||
Reference in New Issue
Block a user