From 5ae9ed1e226638096a85d79836962397b6bbb263 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sun, 20 Mar 2022 13:30:48 +0000 Subject: [PATCH] Added functioning wysiwyg tasklist toolbar button - Includes new icon. - Includes menu button overrides of existing list styles to prevent incompatible mixing. --- resources/js/wysiwyg/plugins-tasklist.js | 56 +++++++++++++++++++++--- resources/js/wysiwyg/toolbars.js | 2 +- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/resources/js/wysiwyg/plugins-tasklist.js b/resources/js/wysiwyg/plugins-tasklist.js index 3fbc2c1e8..cb1bb4a7a 100644 --- a/resources/js/wysiwyg/plugins-tasklist.js +++ b/resources/js/wysiwyg/plugins-tasklist.js @@ -2,11 +2,59 @@ * @param {Editor} editor * @param {String} url */ - function register(editor, url) { - editor.on('PreInit', () => { + // Tasklist UI buttons + editor.ui.registry.addIcon('tasklist', ''); + editor.ui.registry.addToggleButton('tasklist', { + tooltip: 'Task list', + icon: 'tasklist', + active: false, + onAction(api) { + if (api.isActive()) { + editor.execCommand('RemoveList'); + } else { + editor.execCommand('InsertUnorderedList', null, { + 'list-item-attributes': { + class: 'task-list-item', + }, + 'list-style-type': 'tasklist', + }); + } + }, + onSetup(api) { + editor.on('NodeChange', event => { + const inList = event.parents.find(el => el.nodeName === 'LI' && el.classList.contains('task-list-item')) !== undefined; + api.setActive(inList); + }); + } + }); + // Tweak existing bullet list button active state to not be active + // when we're in a task list. + const existingBullListButton = editor.ui.registry.getAll().buttons.bullist; + existingBullListButton.onSetup = function(api) { + editor.on('NodeChange', event => { + const notInTaskList = event.parents.find(el => el.nodeName === 'LI' && el.classList.contains('task-list-item')) === undefined; + const inList = event.parents.find(el => el.nodeName === 'UL') !== undefined; + api.setActive(inList && notInTaskList); + }); + }; + existingBullListButton.onAction = function() { + editor.execCommand('InsertUnorderedList', null, { + 'list-item-attributes': {class: null} + }); + }; + // Tweak existing number list to not allow classes on child items + const existingNumListButton = editor.ui.registry.getAll().buttons.numlist; + existingNumListButton.onAction = function() { + editor.execCommand('InsertOrderedList', null, { + 'list-item-attributes': {class: null} + }); + }; + + // Setup filters on pre-init + editor.on('PreInit', () => { editor.parser.addNodeFilter('li', function(nodes) { for (const node of nodes) { if (node.attributes.map.class === 'task-list-item') { @@ -14,7 +62,6 @@ function register(editor, url) { } } }); - editor.serializer.addNodeFilter('li', function(nodes) { for (const node of nodes) { if (node.attributes.map.class === 'task-list-item') { @@ -22,16 +69,15 @@ function register(editor, url) { } } }); - }); + // Handle checkbox click in editor editor.on('click', function(event) { const clickedEl = event.originalTarget; if (clickedEl.nodeName === 'LI' && clickedEl.classList.contains('task-list-item')) { handleTaskListItemClick(event, clickedEl, editor); } }); - } /** diff --git a/resources/js/wysiwyg/toolbars.js b/resources/js/wysiwyg/toolbars.js index 40cf09dc3..4f8897f84 100644 --- a/resources/js/wysiwyg/toolbars.js +++ b/resources/js/wysiwyg/toolbars.js @@ -10,7 +10,7 @@ export function getPrimaryToolbar(options) { 'styleselect', 'bold italic underline forecolor backcolor formatoverflow', 'alignleft aligncenter alignright alignjustify', - 'bullist numlist listoverflow', + 'bullist numlist tasklist listoverflow', textDirPlugins, 'link table imagemanager-insert insertoverflow', 'code about fullscreen'