]> BookStack Code Mirror - bookstack/blobdiff - resources/js/editor/commands.js
Started work on details/summary blocks
[bookstack] / resources / js / editor / commands.js
index 1817bd2a9d7ca4f4f731863ac073c67d07ad1c58..bbb815b1d6f290beef6356db040f2626bd249a5b 100644 (file)
@@ -1,3 +1,8 @@
+/**
+ * @param {String} attrName
+ * @param {String} attrValue
+ * @return {PmCommandHandler}
+ */
 export function setBlockAttr(attrName, attrValue) {
     return function (state, dispatch) {
         const ref = state.selection;
@@ -37,6 +42,10 @@ export function setBlockAttr(attrName, attrValue) {
     }
 }
 
+/**
+ * @param {PmNodeType} blockType
+ * @return {PmCommandHandler}
+ */
 export function insertBlockBefore(blockType) {
     return function (state, dispatch) {
         const startPosition = state.selection.$from.before(1);
@@ -49,6 +58,40 @@ export function insertBlockBefore(blockType) {
     }
 }
 
+/**
+ * @param {Number} rows
+ * @param {Number} columns
+ * @param {Object} tableAttrs
+ * @return {PmCommandHandler}
+ */
+export function insertTable(rows, columns, tableAttrs) {
+    return function (state, dispatch) {
+        if (!dispatch) return true;
+
+        const tr = state.tr;
+        const nodes = state.schema.nodes;
+
+        const rowNodes = [];
+        for (let y = 0; y < rows; y++) {
+            const rowCells = [];
+            for (let x = 0; x < columns; x++) {
+                const cellText = nodes.paragraph.create(null);
+                rowCells.push(nodes.table_cell.create(null, cellText));
+            }
+            rowNodes.push(nodes.table_row.create(null, rowCells));
+        }
+
+        const table = nodes.table.create(tableAttrs, rowNodes);
+        tr.replaceSelectionWith(table);
+        dispatch(tr);
+
+        return true;
+    }
+}
+
+/**
+ * @return {PmCommandHandler}
+ */
 export function removeMarks() {
     return function (state, dispatch) {
         if (dispatch) {