X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/9e0b8a9fb6b0c96f9fed911544e22d9d56cb74a1..refs/pull/3598/head:/resources/js/wysiwyg/plugins-details.js
diff --git a/resources/js/wysiwyg/plugins-details.js b/resources/js/wysiwyg/plugins-details.js
index 90fdf84ec..44a0a35ab 100644
--- a/resources/js/wysiwyg/plugins-details.js
+++ b/resources/js/wysiwyg/plugins-details.js
@@ -2,10 +2,13 @@
* @param {Editor} editor
* @param {String} url
*/
+import {blockElementTypes} from "./util";
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,57 +20,66 @@ 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);
- const dialog = editor.windowManager.open(detailsDialog(editor));
- dialog.setData({summary: getSummaryTextFromDetails(details)});
+ showDetailLabelEditWindow(editor);
}
});
- editor.ui.registry.addButton('collapsedetails', {
- icon: 'action-prev',
- tooltip: 'Collapse',
- onAction() {
- const details = getSelectedDetailsBlock(editor);
- details.removeAttribute('open');
- editor.focus();
- }
+ editor.on('dblclick', event => {
+ if (!getSelectedDetailsBlock(editor) || event.target.closest('doc-root')) return;
+ showDetailLabelEditWindow(editor);
});
- editor.ui.registry.addButton('expanddetails', {
- icon: 'action-next',
- tooltip: 'Expand',
+ editor.ui.registry.addButton('toggledetails', {
+ icon: 'togglefold',
+ tooltip: 'Toggle open/closed',
onAction() {
const details = getSelectedDetailsBlock(editor);
- details.setAttribute('open', 'open');
+ details.toggleAttribute('open');
editor.focus();
}
});
editor.addCommand('InsertDetailsBlock', function () {
- const content = editor.selection.getContent({format: 'html'});
+ let content = editor.selection.getContent({format: 'html'});
const details = document.createElement('details');
const summary = document.createElement('summary');
+ const id = 'details-' + Date.now();
+ details.setAttribute('data-id', id)
details.appendChild(summary);
- details.innerHTML += content;
+ if (!content) {
+ content = '
';
+ }
+
+ details.innerHTML += content;
editor.insertContent(details.outerHTML);
+ editor.focus();
+
+ const domDetails = editor.dom.select(`[data-id="${id}"]`)[0] || null;
+ if (domDetails) {
+ const firstChild = domDetails.querySelector('doc-root > *');
+ if (firstChild) {
+ firstChild.focus();
+ }
+ domDetails.removeAttribute('data-id');
+ }
});
editor.ui.registry.addContextToolbar('details', {
predicate: function (node) {
return node.nodeName.toLowerCase() === 'details';
},
- items: 'removedetails editdetials collapsedetails expanddetails',
+ items: 'editdetials toggledetails removedetails',
position: 'node',
scope: 'node'
});
@@ -77,6 +89,15 @@ function register(editor, url) {
});
}
+/**
+ * @param {Editor} editor
+ */
+function showDetailLabelEditWindow(editor) {
+ const details = getSelectedDetailsBlock(editor);
+ const dialog = editor.windowManager.open(detailsDialog(editor));
+ dialog.setData({summary: getSummaryTextFromDetails(details)});
+}
+
/**
* @param {Editor} editor
*/
@@ -107,7 +128,7 @@ function detailsDialog(editor) {
{
type: 'input',
name: 'summary',
- label: 'Toggle label text',
+ label: 'Toggle label',
},
],
},
@@ -138,7 +159,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;
});
@@ -149,19 +170,21 @@ function setSummary(editor, summaryContent) {
*/
function unwrapDetailsInSelection(editor) {
const details = editor.selection.getNode().closest('details');
+ const selectionBm = editor.selection.getBookmark();
+
if (details) {
- const summary = details.querySelector('summary');
+ const elements = details.querySelectorAll('details > *:not(summary, doc-root), doc-root > *');
+
editor.undoManager.transact(() => {
- if (summary) {
- summary.remove();
- }
- while (details.firstChild) {
- details.parentNode.insertBefore(details.firstChild, details);
+ for (const element of elements) {
+ details.parentNode.insertBefore(element, details);
}
details.remove();
});
}
+
editor.focus();
+ editor.selection.moveToBookmark(selectionBm);
}
/**
@@ -170,31 +193,69 @@ 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);
}
});
+
+ editor.serializer.addNodeFilter('doc-root', function(elms) {
+ for (const el of elms) {
+ el.unwrap();
+ }
+ });
+}
+
+/**
+ * @param {tinymce.html.Node} detailsEl
+ */
+function ensureDetailsWrappedInEditable(detailsEl) {
+ unwrapDetailsEditable(detailsEl);
+
+ detailsEl.attr('contenteditable', 'false');
+ const rootWrap = tinymce.html.Node.create('doc-root', {contenteditable: 'true'});
+ let previousBlockWrap = null;
+
+ for (const child of detailsEl.children()) {
+ 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(rootWrap);
+}
+
+/**
+ * @param {tinymce.html.Node} detailsEl
+ */
+function unwrapDetailsEditable(detailsEl) {
+ detailsEl.attr('contenteditable', null);
+ let madeUnwrap = false;
+ for (const child of detailsEl.children()) {
+ if (child.name === 'doc-root') {
+ child.unwrap();
+ madeUnwrap = true;
+ }
+ }
+
+ if (madeUnwrap) {
+ unwrapDetailsEditable(detailsEl);
+ }
}