2 * @param {String} attrName
3 * @param {String} attrValue
4 * @return {PmCommandHandler}
6 export function setBlockAttr(attrName, attrValue) {
7 return function (state, dispatch) {
8 const ref = state.selection;
11 let applicable = false;
13 state.doc.nodesBetween(from, to, function (node, pos) {
17 if (!node.isTextblock || node.attrs[attrName] === attrValue) {
21 applicable = node.attrs[attrName] !== undefined;
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)
46 * @param {PmNodeType} blockType
47 * @return {PmCommandHandler}
49 export function insertBlockBefore(blockType) {
50 return function (state, dispatch) {
51 const startPosition = state.selection.$from.before(1);
54 dispatch(state.tr.insert(startPosition, blockType.create()));
62 * @param {Number} rows
63 * @param {Number} columns
64 * @return {PmCommandHandler}
66 export function insertTable(rows, columns) {
67 return function (state, dispatch) {
68 if (!dispatch) return true;
71 const nodes = state.schema.nodes;
74 for (let y = 0; y < rows; y++) {
76 for (let x = 0; x < columns; x++) {
77 rowCells.push(nodes.table_cell.create(null));
79 rowNodes.push(nodes.table_row.create(null, rowCells));
82 const table = nodes.table.create(null, rowNodes);
83 tr.replaceSelectionWith(table);
91 * @return {PmCommandHandler}
93 export function removeMarks() {
94 return function (state, dispatch) {
96 dispatch(state.tr.removeMark(state.selection.from, state.selection.to, null));