2 * @param {Editor} editor
6 function register(editor, url) {
8 editor.on('PreInit', () => {
10 editor.parser.addNodeFilter('li', function(nodes) {
11 for (const node of nodes) {
12 if (node.attributes.map.class === 'task-list-item') {
13 parseTaskListNode(node);
18 editor.serializer.addNodeFilter('li', function(nodes) {
19 for (const node of nodes) {
20 if (node.attributes.map.class === 'task-list-item') {
21 serializeTaskListNode(node);
28 editor.on('click', function(event) {
29 const clickedEl = event.originalTarget;
30 if (clickedEl.nodeName === 'LI' && clickedEl.classList.contains('task-list-item')) {
31 handleTaskListItemClick(event, clickedEl, editor);
38 * @param {MouseEvent} event
39 * @param {Element} clickedEl
40 * @param {Editor} editor
42 function handleTaskListItemClick(event, clickedEl, editor) {
43 const bounds = clickedEl.getBoundingClientRect();
44 const withinBounds = event.clientX <= bounds.right
45 && event.clientX >= bounds.left
46 && event.clientY >= bounds.top
47 && event.clientY <= bounds.bottom;
49 // Outside of the task list item bounds mean we're probably clicking the pseudo-element.
51 editor.undoManager.transact(() => {
52 if (clickedEl.hasAttribute('checked')) {
53 clickedEl.removeAttribute('checked');
55 clickedEl.setAttribute('checked', 'checked');
62 * @param {AstNode} node
64 function parseTaskListNode(node) {
65 // Force task list item class
66 node.attr('class', 'task-list-item');
68 // Copy checkbox status and remove checkbox within editor
69 for (const child of node.children()) {
70 if (child.name === 'input') {
71 if (child.attr('checked') === 'checked') {
72 node.attr('checked', 'checked');
80 * @param {AstNode} node
82 function serializeTaskListNode(node) {
83 const isChecked = node.attr('checked') === 'checked';
84 node.attr('checked', null);
86 const inputAttrs = {type: 'checkbox', disabled: 'disabled'};
88 inputAttrs.checked = 'checked';
91 const checkbox = new tinymce.html.Node.create('input', inputAttrs);
92 checkbox.shortEnded = true;
93 node.firstChild ? node.insert(checkbox, node.firstChild, true) : node.append(checkbox);
97 * @param {WysiwygConfigOptions} options
100 export function getPlugin(options) {