]> BookStack Code Mirror - bookstack/blob - resources/js/editor/commands.js
Started work on details/summary blocks
[bookstack] / resources / js / editor / commands.js
1 /**
2  * @param {String} attrName
3  * @param {String} attrValue
4  * @return {PmCommandHandler}
5  */
6 export function setBlockAttr(attrName, attrValue) {
7     return function (state, dispatch) {
8         const ref = state.selection;
9         const from = ref.from;
10         const to = ref.to;
11         let applicable = false;
12
13         state.doc.nodesBetween(from, to, function (node, pos) {
14             if (applicable) {
15                 return false
16             }
17             if (!node.isTextblock || node.attrs[attrName] === attrValue) {
18                 return
19             }
20
21             applicable = node.attrs[attrName] !== undefined;
22         });
23
24         if (!applicable) {
25             return false
26         }
27
28         if (dispatch) {
29             const tr = state.tr;
30             tr.doc.nodesBetween(from, to, function (node, pos) {
31                 const nodeAttrs = Object.assign({}, node.attrs);
32                 if (node.attrs[attrName] !== undefined) {
33                     nodeAttrs[attrName] = attrValue;
34                     tr.setBlockType(pos, pos + 1, node.type, nodeAttrs)
35                 }
36             });
37
38             dispatch(tr);
39         }
40
41         return true
42     }
43 }
44
45 /**
46  * @param {PmNodeType} blockType
47  * @return {PmCommandHandler}
48  */
49 export function insertBlockBefore(blockType) {
50     return function (state, dispatch) {
51         const startPosition = state.selection.$from.before(1);
52
53         if (dispatch) {
54             dispatch(state.tr.insert(startPosition, blockType.create()));
55         }
56
57         return true
58     }
59 }
60
61 /**
62  * @param {Number} rows
63  * @param {Number} columns
64  * @param {Object} tableAttrs
65  * @return {PmCommandHandler}
66  */
67 export function insertTable(rows, columns, tableAttrs) {
68     return function (state, dispatch) {
69         if (!dispatch) return true;
70
71         const tr = state.tr;
72         const nodes = state.schema.nodes;
73
74         const rowNodes = [];
75         for (let y = 0; y < rows; y++) {
76             const rowCells = [];
77             for (let x = 0; x < columns; x++) {
78                 const cellText = nodes.paragraph.create(null);
79                 rowCells.push(nodes.table_cell.create(null, cellText));
80             }
81             rowNodes.push(nodes.table_row.create(null, rowCells));
82         }
83
84         const table = nodes.table.create(tableAttrs, rowNodes);
85         tr.replaceSelectionWith(table);
86         dispatch(tr);
87
88         return true;
89     }
90 }
91
92 /**
93  * @return {PmCommandHandler}
94  */
95 export function removeMarks() {
96     return function (state, dispatch) {
97         if (dispatch) {
98             dispatch(state.tr.removeMark(state.selection.from, state.selection.to, null));
99         }
100         return true;
101     }
102 }