diff --git a/resources/js/wysiwyg/plugins-details.js b/resources/js/wysiwyg/plugins-details.js index 90fdf84ec..83a29a29d 100644 --- a/resources/js/wysiwyg/plugins-details.js +++ b/resources/js/wysiwyg/plugins-details.js @@ -6,6 +6,8 @@ function register(editor, url) { editor.ui.registry.addIcon('details', ''); + editor.ui.registry.addIcon('togglefold', ''); + editor.ui.registry.addIcon('togglelabel', ''); editor.ui.registry.addButton('details', { icon: 'details', @@ -17,14 +19,14 @@ function register(editor, url) { editor.ui.registry.addButton('removedetails', { icon: 'table-delete-table', - tooltip: 'Unwrap collapsible block', + tooltip: 'Unwrap', onAction() { unwrapDetailsInSelection(editor) } }); editor.ui.registry.addButton('editdetials', { - icon: 'tag', + icon: 'togglelabel', tooltip: 'Edit label', onAction() { const details = getSelectedDetailsBlock(editor); @@ -33,22 +35,12 @@ function register(editor, url) { } }); - editor.ui.registry.addButton('collapsedetails', { - icon: 'action-prev', - tooltip: 'Collapse', + editor.ui.registry.addButton('toggledetails', { + icon: 'togglefold', + tooltip: 'Toggle open/closed', onAction() { const details = getSelectedDetailsBlock(editor); - details.removeAttribute('open'); - editor.focus(); - } - }); - - editor.ui.registry.addButton('expanddetails', { - icon: 'action-next', - tooltip: 'Expand', - onAction() { - const details = getSelectedDetailsBlock(editor); - details.setAttribute('open', 'open'); + details.toggleAttribute('open'); editor.focus(); } }); @@ -67,7 +59,7 @@ function register(editor, url) { predicate: function (node) { return node.nodeName.toLowerCase() === 'details'; }, - items: 'removedetails editdetials collapsedetails expanddetails', + items: 'editdetials toggledetails removedetails', position: 'node', scope: 'node' }); @@ -138,7 +130,7 @@ function setSummary(editor, summaryContent) { let summary = details.querySelector('summary'); if (!summary) { summary = document.createElement('summary'); - details.appendChild(summary); + details.prepend(summary); } summary.textContent = summaryContent; }); @@ -170,33 +162,44 @@ function unwrapDetailsInSelection(editor) { function setupElementFilters(editor) { editor.parser.addNodeFilter('details', function(elms) { for (const el of elms) { - // el.attr('contenteditable', 'false'); - // console.log(el); - // let wrap = el.find('div[detailswrap]'); - // if (!wrap) { - // wrap = document.createElement('div'); - // wrap.setAttribute('detailswrap', 'true'); - // } - // - // for (const child of el.children) { - // if (child.nodeName.toLowerCase() === 'summary' || child.hasAttribute('detailswrap')) { - // continue; - // } - // wrap.appendChild(child); - // } - // - // el.appendChild(wrap); - // wrap.setAttribute('contenteditable', 'true'); + ensureDetailsWrappedInEditable(el); } }); editor.serializer.addNodeFilter('details', function(elms) { - for (const summaryEl of elms) { - summaryEl.attr('contenteditable', null); + for (const el of elms) { + unwrapDetailsEditable(el); + el.attr('open', null); } }); } +/** + * @param {tinymce.html.Node} detailsEl + */ +function ensureDetailsWrappedInEditable(detailsEl) { + detailsEl.attr('contenteditable', 'false'); + const wrap = tinymce.html.Node.create('div', {detailswrap: 'true', contenteditable: 'true'}); + for (const child of detailsEl.children()) { + if (child.name !== 'summary') { + wrap.append(child); + } + } + detailsEl.append(wrap); +} + +/** + * @param {tinymce.html.Node} detailsEl + */ +function unwrapDetailsEditable(detailsEl) { + detailsEl.attr('contenteditable', null); + for (const child of detailsEl.children()) { + if (child.attr('detailswrap')) { + child.unwrap(); + } + } +} + /** * @param {WysiwygConfigOptions} options diff --git a/resources/lang/en/editor.php b/resources/lang/en/editor.php index e84d60a7d..2b1d1a519 100644 --- a/resources/lang/en/editor.php +++ b/resources/lang/en/editor.php @@ -120,7 +120,7 @@ return [ 'show_caption' => 'Show caption', 'constrain' => 'Constrain proportions', - // Images, links & embed + // Images, links, details/summary & embed 'source' => 'Source', 'alt_desc' => 'Alternative description', 'embed' => 'Embed', @@ -131,6 +131,11 @@ return [ 'open_link' => 'Open link in...', 'open_link_current' => 'Current window', 'open_link_new' => 'New window', + 'insert_collapsible' => 'Insert collapsible block', + 'collapsible_unwrap' => 'Unwrap', + 'edit_label' => 'Edit label', + 'toggle_open_closed' => 'Toggle open/closed', + 'collapsible_edit' => 'Edit collapsible block', // About view 'about_title' => 'About the WYSIWYG Editor', diff --git a/resources/sass/_tinymce.scss b/resources/sass/_tinymce.scss index c5cc179d0..57bb69754 100644 --- a/resources/sass/_tinymce.scss +++ b/resources/sass/_tinymce.scss @@ -37,10 +37,16 @@ body.page-content.mce-content-body { pointer-events: none; } -// Prevent details summary clicks { +// Details/summary editor usability .page-content.mce-content-body details summary { pointer-events: none; } +.page-content.mce-content-body details [detailswrap] { + padding: $-s; + margin-left: (2px - $-s); + margin-right: (2px - $-s); + margin-bottom: (2px - $-s); +} /** * Dark Mode Overrides