]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/plugins-table-additions.js
fcb774af123ab9b882c9d88ed165c622c791024f
[bookstack] / resources / js / wysiwyg / plugins-table-additions.js
1 /**
2  * @param {Editor} editor
3  */
4 function register(editor) {
5     editor.ui.registry.addIcon('tableclearformatting', '<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" viewBox="0 0 24 24"><path d="M15.53088 4.64727v-.82364c0-.453-.37063-.82363-.82363-.82363H4.82363C4.37063 3 4 3.37064 4 3.82363v3.29454c0 .453.37064.82364.82363.82364h9.88362c.453 0 .82363-.37064.82363-.82364v-.82363h.82364v3.29454H8.11817v7.4127c0 .453.37064.82364.82364.82364h1.64727c.453 0 .82363-.37064.82363-.82364v-5.76544h6.58907V4.64727Z"/><path d="m18.42672 19.51563-1.54687-1.54688-1.54688 1.54688c-.26751.2675-.70124.2675-.96875 0-.26751-.26752-.26751-.70124 0-.96876L15.9111 17l-1.54688-1.54688c-.26751-.2675-.26751-.70123 0-.96875.26751-.2675.70124-.2675.96875 0l1.54688 1.54688 1.54687-1.54688c.26751-.2675.70124-.2675.96875 0 .26751.26752.26751.70124 0 .96875L17.8486 17l1.54687 1.54688c.26751.2675.26751.70123 0 .96874-.26751.26752-.70124.26752-.96875 0z"/></svg>');
6     editor.addCommand('tableclearformatting', () => {
7         const table = editor.dom.getParent(editor.selection.getStart(), 'table');
8         if (!table) {
9             return;
10         }
11
12         const attrsToRemove = ['class', 'style', 'width', 'height'];
13         const styled = [table, ...table.querySelectorAll(attrsToRemove.map(a => `[${a}]`).join(','))];
14         for (const elem of styled) {
15             for (const attr of attrsToRemove) {
16                 elem.removeAttribute(attr);
17             }
18         }
19     });
20
21     editor.addCommand('tableclearsizes', () => {
22         const table = editor.dom.getParent(editor.selection.getStart(), 'table');
23         if (!table) {
24             return;
25         }
26
27         const targets = [table, ...table.querySelectorAll('tr,td,th,tbody,thead,tfoot,th>*,td>*')];
28         for (const elem of targets) {
29             elem.removeAttribute('width');
30             elem.removeAttribute('height');
31             elem.style.height = null;
32             elem.style.width = null;
33         }
34     });
35
36     const onPreInit = () => {
37         const exitingButtons = editor.ui.registry.getAll().buttons;
38
39         editor.ui.registry.addMenuButton('customtable', {
40             ...exitingButtons.table,
41             fetch: callback => callback('inserttable | cell row column | advtablesort | tableprops tableclearformatting tableclearsizes deletetable'),
42         });
43
44         editor.ui.registry.addMenuItem('tableclearformatting', {
45             icon: 'tableclearformatting',
46             text: 'Clear table formatting',
47             onSetup: exitingButtons.tableprops.onSetup,
48             onAction() {
49                 editor.execCommand('tableclearformatting');
50             },
51         });
52
53         editor.ui.registry.addMenuItem('tableclearsizes', {
54             icon: 'resize',
55             text: 'Resize to contents',
56             onSetup: exitingButtons.tableprops.onSetup,
57             onAction() {
58                 editor.execCommand('tableclearsizes');
59             },
60         });
61
62         editor.off('PreInit', onPreInit);
63     };
64
65     editor.on('PreInit', onPreInit);
66 }
67
68 /**
69  * @return {register}
70  */
71 export function getPlugin() {
72     return register;
73 }