Ensured wysiwyg details contents are wrapped in block elements

Fixes issue where inline-only content would disappear when unwrapping a
details block element.
This commit is contained in:
Dan Brown 2022-07-23 11:18:03 +01:00
parent a9ee2e6889
commit f86bb27a83
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
3 changed files with 37 additions and 5 deletions

View File

@ -235,7 +235,7 @@ export function build(options) {
"-doc-root[doc-root|#text]",
"-li[details]",
"+code-block[pre]",
"+doc-root[code-block]"
"+doc-root[p|h1|h2|h3|h4|h5|h6|blockquote|code-block|div]"
].join(','),
plugins: gatherPlugins(options),
contextmenu: false,

View File

@ -2,6 +2,7 @@
* @param {Editor} editor
* @param {String} url
*/
import {blockElementTypes} from "./util";
function register(editor, url) {
@ -217,14 +218,26 @@ function ensureDetailsWrappedInEditable(detailsEl) {
unwrapDetailsEditable(detailsEl);
detailsEl.attr('contenteditable', 'false');
const wrap = tinymce.html.Node.create('doc-root', {contenteditable: 'true'});
const rootWrap = tinymce.html.Node.create('doc-root', {contenteditable: 'true'});
let previousBlockWrap = null;
for (const child of detailsEl.children()) {
if (child.name !== 'summary') {
wrap.append(child);
if (child.name === 'summary') continue;
const isBlock = blockElementTypes.includes(child.name);
if (!isBlock) {
if (!previousBlockWrap) {
previousBlockWrap = tinymce.html.Node.create('p');
rootWrap.append(previousBlockWrap);
}
previousBlockWrap.append(child);
} else {
rootWrap.append(child);
previousBlockWrap = null;
}
}
detailsEl.append(wrap);
detailsEl.append(rootWrap);
}
/**

View File

@ -0,0 +1,19 @@
export const blockElementTypes = [
'p',
'h1',
'h2',
'h3',
'h4',
'h5',
'h6',
'div',
'blockquote',
'pre',
'code-block',
'details',
'ul',
'ol',
'table'
];