/**
* @param {Editor} editor
* @param {String} url
*/
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',
tooltip: 'Insert collapsible block',
onAction() {
editor.execCommand('InsertDetailsBlock');
}
});
editor.ui.registry.addButton('removedetails', {
icon: 'table-delete-table',
tooltip: 'Unwrap',
onAction() {
unwrapDetailsInSelection(editor)
}
});
editor.ui.registry.addButton('editdetials', {
icon: 'togglelabel',
tooltip: 'Edit label',
onAction() {
const details = getSelectedDetailsBlock(editor);
const dialog = editor.windowManager.open(detailsDialog(editor));
dialog.setData({summary: getSummaryTextFromDetails(details)});
}
});
editor.ui.registry.addButton('toggledetails', {
icon: 'togglefold',
tooltip: 'Toggle open/closed',
onAction() {
const details = getSelectedDetailsBlock(editor);
details.toggleAttribute('open');
editor.focus();
}
});
editor.addCommand('InsertDetailsBlock', function () {
const content = editor.selection.getContent({format: 'html'});
const details = document.createElement('details');
const summary = document.createElement('summary');
details.appendChild(summary);
details.innerHTML += content;
editor.insertContent(details.outerHTML);
});
editor.ui.registry.addContextToolbar('details', {
predicate: function (node) {
return node.nodeName.toLowerCase() === 'details';
},
items: 'editdetials toggledetails removedetails',
position: 'node',
scope: 'node'
});
editor.on('PreInit', () => {
setupElementFilters(editor);
});
}
/**
* @param {Editor} editor
*/
function getSelectedDetailsBlock(editor) {
return editor.selection.getNode().closest('details');
}
/**
* @param {Element} element
*/
function getSummaryTextFromDetails(element) {
const summary = element.querySelector('summary');
if (!summary) {
return '';
}
return summary.textContent;
}
/**
* @param {Editor} editor
*/
function detailsDialog(editor) {
return {
title: 'Edit collapsible block',
body: {
type: 'panel',
items: [
{
type: 'input',
name: 'summary',
label: 'Toggle label text',
},
],
},
buttons: [
{
type: 'cancel',
text: 'Cancel'
},
{
type: 'submit',
text: 'Save',
primary: true,
}
],
onSubmit(api) {
const {summary} = api.getData();
setSummary(editor, summary);
api.close();
}
}
}
function setSummary(editor, summaryContent) {
const details = getSelectedDetailsBlock(editor);
if (!details) return;
editor.undoManager.transact(() => {
let summary = details.querySelector('summary');
if (!summary) {
summary = document.createElement('summary');
details.prepend(summary);
}
summary.textContent = summaryContent;
});
}
/**
* @param {Editor} editor
*/
function unwrapDetailsInSelection(editor) {
const details = editor.selection.getNode().closest('details');
if (details) {
const summary = details.querySelector('summary');
editor.undoManager.transact(() => {
if (summary) {
summary.remove();
}
while (details.firstChild) {
details.parentNode.insertBefore(details.firstChild, details);
}
details.remove();
});
}
editor.focus();
}
/**
* @param {Editor} editor
*/
function setupElementFilters(editor) {
editor.parser.addNodeFilter('details', function(elms) {
for (const el of elms) {
ensureDetailsWrappedInEditable(el);
}
});
editor.serializer.addNodeFilter('details', function(elms) {
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
* @return {register}
*/
export function getPlugin(options) {
return register;
}