2022-03-19 12:04:33 -04:00
|
|
|
/**
|
|
|
|
* @param {Editor} editor
|
|
|
|
* @param {String} url
|
|
|
|
*/
|
|
|
|
|
2022-03-19 13:12:56 -04:00
|
|
|
function register(editor, url) {
|
2022-03-19 12:04:33 -04:00
|
|
|
|
|
|
|
editor.on('PreInit', () => {
|
|
|
|
|
2022-03-19 13:12:56 -04:00
|
|
|
editor.parser.addNodeFilter('li', function(nodes) {
|
|
|
|
for (const node of nodes) {
|
|
|
|
if (node.attributes.map.class === 'task-list-item') {
|
|
|
|
parseTaskListNode(node);
|
2022-03-19 12:04:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-19 13:12:56 -04:00
|
|
|
editor.serializer.addNodeFilter('li', function(nodes) {
|
|
|
|
for (const node of nodes) {
|
|
|
|
if (node.attributes.map.class === 'task-list-item') {
|
|
|
|
serializeTaskListNode(node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-03-19 12:04:33 -04:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {AstNode} node
|
|
|
|
*/
|
2022-03-19 13:12:56 -04:00
|
|
|
function parseTaskListNode(node) {
|
|
|
|
// Force task list item class
|
|
|
|
node.attr('class', 'task-list-item');
|
2022-03-19 12:04:33 -04:00
|
|
|
|
2022-03-19 13:12:56 -04:00
|
|
|
// Copy checkbox status and remove checkbox within editor
|
2022-03-19 12:04:33 -04:00
|
|
|
for (const child of node.children()) {
|
2022-03-19 13:12:56 -04:00
|
|
|
if (child.name === 'input') {
|
|
|
|
if (child.attr('checked') === 'checked') {
|
|
|
|
node.attr('checked', 'checked');
|
|
|
|
}
|
|
|
|
child.remove();
|
2022-03-19 12:04:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-19 13:12:56 -04:00
|
|
|
* @param {AstNode} node
|
2022-03-19 12:04:33 -04:00
|
|
|
*/
|
2022-03-19 13:12:56 -04:00
|
|
|
function serializeTaskListNode(node) {
|
|
|
|
const isChecked = node.attr('checked') === 'checked';
|
|
|
|
node.attr('checked', null);
|
2022-03-19 12:04:33 -04:00
|
|
|
|
2022-03-19 13:12:56 -04:00
|
|
|
const inputAttrs = {type: 'checkbox', disabled: 'disabled'};
|
|
|
|
if (isChecked) {
|
|
|
|
inputAttrs.checked = 'checked';
|
2022-03-19 12:04:33 -04:00
|
|
|
}
|
|
|
|
|
2022-03-19 13:12:56 -04:00
|
|
|
const checkbox = new tinymce.html.Node.create('input', inputAttrs);
|
|
|
|
checkbox.shortEnded = true;
|
|
|
|
node.firstChild ? node.insert(checkbox, node.firstChild, true) : node.append(checkbox);
|
|
|
|
}
|
2022-03-19 12:04:33 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {WysiwygConfigOptions} options
|
|
|
|
* @return {register}
|
|
|
|
*/
|
|
|
|
export function getPlugin(options) {
|
|
|
|
return register;
|
|
|
|
}
|