]> BookStack Code Mirror - bookstack/blob - resources/js/editor/commands.js
d77e75026fa3d8afa5cad1018a43a01714a51589
[bookstack] / resources / js / editor / commands.js
1 export function setBlockAttr(attrName, attrValue) {
2     return function (state, dispatch) {
3         const ref = state.selection;
4         const from = ref.from;
5         const to = ref.to;
6         let applicable = false;
7
8         state.doc.nodesBetween(from, to, function (node, pos) {
9             if (applicable) {
10                 return false
11             }
12             if (!node.isTextblock || node.attrs[attrName] === attrValue) {
13                 return
14             }
15
16             applicable = node.attrs[attrName] !== undefined;
17         });
18
19         if (!applicable) {
20             return false
21         }
22
23         if (dispatch) {
24             const tr = state.tr;
25             tr.doc.nodesBetween(from, to, function (node, pos) {
26                 const nodeAttrs = Object.assign({}, node.attrs);
27                 if (node.attrs[attrName] !== undefined) {
28                     nodeAttrs[attrName] = attrValue;
29                     tr.setBlockType(pos, pos+1, node.type, nodeAttrs)
30                 }
31             });
32
33             dispatch(tr);
34         }
35
36         return true
37     }
38 }