Install prosemirror-utils
package from npm:
npm install prosemirror-utils
-
findParentNode
(predicate: fn(node: ProseMirrorNode) → boolean) → fn(selection: Selection) → ?{pos: number, start: number, node: ProseMirrorNode}
Iterates over parent nodes, returning the closest node and its start positionpredicate
returns truthy for.start
points to the start position of the node,pos
points directly before the node.const predicate = node => node.type === schema.nodes.blockquote; const parent = findParentNode(predicate)(selection);
-
findParentNodeClosestToPos
($pos: ResolvedPos, predicate: fn(node: ProseMirrorNode) → boolean) → ?{pos: number, start: number, node: ProseMirrorNode}
Iterates over parent nodes starting from the given$pos
, returning the closest node and its start positionpredicate
returns truthy for.start
points to the start position of the node,pos
points directly before the node.const predicate = node => node.type === schema.nodes.blockquote; const parent = findParentNodeClosestToPos(state.doc.resolve(5), predicate);
-
findParentDomRef
(predicate: fn(node: ProseMirrorNode) → boolean, domAtPos: fn(pos: number) → {node: dom.Node, offset: number}) → fn(selection: Selection) → ?dom.Node
Iterates over parent nodes, returning DOM reference of the closest nodepredicate
returns truthy for.const domAtPos = view.domAtPos.bind(view); const predicate = node => node.type === schema.nodes.table; const parent = findParentDomRef(predicate, domAtPos)(selection); // <table>
-
hasParentNode
(predicate: fn(node: ProseMirrorNode) → boolean) → fn(selection: Selection) → boolean
Checks if there's a parent nodepredicate
returns truthy for.if (hasParentNode(node => node.type === schema.nodes.table)(selection)) { // .... }
-
findParentNodeOfType
(nodeType: NodeType | [NodeType]) → fn(selection: Selection) → ?{pos: number, start: number, node: ProseMirrorNode}
Iterates over parent nodes, returning closest node of a givennodeType
.start
points to the start position of the node,pos
points directly before the node.const parent = findParentNodeOfType(schema.nodes.paragraph)(selection);
-
findParentNodeOfTypeClosestToPos
($pos: ResolvedPos, nodeType: NodeType | [NodeType]) → ?{pos: number, start: number, node: ProseMirrorNode}
Iterates over parent nodes starting from the given$pos
, returning closest node of a givennodeType
.start
points to the start position of the node,pos
points directly before the node.const parent = findParentNodeOfTypeClosestToPos(state.doc.resolve(10), schema.nodes.paragraph);
-
hasParentNodeOfType
(nodeType: NodeType | [NodeType]) → fn(selection: Selection) → boolean
Checks if there's a parent node of a givennodeType
.if (hasParentNodeOfType(schema.nodes.table)(selection)) { // .... }
-
findParentDomRefOfType
(nodeType: NodeType | [NodeType], domAtPos: fn(pos: number) → {node: dom.Node, offset: number}) → fn(selection: Selection) → ?dom.Node
Iterates over parent nodes, returning DOM reference of the closest node of a givennodeType
.const domAtPos = view.domAtPos.bind(view); const parent = findParentDomRefOfType(schema.nodes.codeBlock, domAtPos)(selection); // <pre>
-
findSelectedNodeOfType
(nodeType: NodeType | [NodeType]) → fn(selection: Selection) → ?{pos: number, start: number, node: ProseMirrorNode}
Returns a node of a givennodeType
if it is selected.start
points to the start position of the node,pos
points directly before the node.const { extension, inlineExtension, bodiedExtension } = schema.nodes; const selectedNode = findSelectedNodeOfType([ extension, inlineExtension, bodiedExtension, ])(selection);
-
isNodeSelection
(selection: Selection) → boolean
Checks if current selection is aNodeSelection
.if (isNodeSelection(tr.selection)) { // ... }
-
findPositionOfNodeBefore
(selection: Selection) → ?number
Returns position of the previous node.const pos = findPositionOfNodeBefore(tr.selection);
-
findDomRefAtPos
(position: number, domAtPos: fn(pos: number) → {node: dom.Node, offset: number}) → dom.Node
Returns DOM reference of a node at a givenposition
. If the node type is of typeTEXT_NODE
it will return the reference of the parent node.const domAtPos = view.domAtPos.bind(view); const ref = findDomRefAtPos($from.pos, domAtPos);
-
flatten
(node: ProseMirrorNode, descend: ?boolean = true) → [{node: ProseMirrorNode, pos: number}]
Flattens descendants of a givennode
. It doesn't descend into a node when descend argument isfalse
(defaults totrue
).const children = flatten(node);
-
findChildren
(node: ProseMirrorNode, predicate: fn(node: ProseMirrorNode) → boolean, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
Iterates over descendants of a givennode
, returning child nodes predicate returns truthy for. It doesn't descend into a node when descend argument isfalse
(defaults totrue
).const textNodes = findChildren(node, child => child.isText, false);
-
findTextNodes
(node: ProseMirrorNode, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
Returns text nodes of a givennode
. It doesn't descend into a node when descend argument isfalse
(defaults totrue
).const textNodes = findTextNodes(node);
-
findInlineNodes
(node: ProseMirrorNode, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
Returns inline nodes of a givennode
. It doesn't descend into a node when descend argument isfalse
(defaults totrue
).const inlineNodes = findInlineNodes(node);
-
findBlockNodes
(node: ProseMirrorNode, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
Returns block descendants of a givennode
. It doesn't descend into a node when descend argument isfalse
(defaults totrue
).const blockNodes = findBlockNodes(node);
-
findChildrenByAttr
(node: ProseMirrorNode, predicate: fn(attrs: ?Object) → boolean, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
Iterates over descendants of a givennode
, returning child nodes predicate returns truthy for. It doesn't descend into a node when descend argument isfalse
(defaults totrue
).const mergedCells = findChildrenByAttr(table, attrs => attrs.colspan === 2);
-
findChildrenByType
(node: ProseMirrorNode, nodeType: NodeType, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
Iterates over descendants of a givennode
, returning child nodes of a given nodeType. It doesn't descend into a node when descend argument isfalse
(defaults totrue
).const cells = findChildrenByType(table, schema.nodes.tableCell);
-
findChildrenByMark
(node: ProseMirrorNode, markType: markType, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]
Iterates over descendants of a givennode
, returning child nodes that have a mark of a given markType. It doesn't descend into anode
when descend argument isfalse
(defaults totrue
).const nodes = findChildrenByMark(state.doc, schema.marks.strong);
-
contains
(node: ProseMirrorNode, nodeType: NodeType) → boolean
Returnstrue
if a given node contains nodes of a givennodeType
if (contains(panel, schema.nodes.listItem)) { // ... }
-
findTable
(selection: Selection) → ?{pos: number, start: number, node: ProseMirrorNode}
Iterates over parent nodes, returning the closest table node.const table = findTable(selection);
-
isCellSelection
(selection: Selection) → boolean
Checks if current selection is aCellSelection
.if (isCellSelection(selection)) { // ... }
-
isColumnSelected
(columnIndex: number) → fn(selection: Selection) → boolean
Checks if entire column at indexcolumnIndex
is selected.const className = isColumnSelected(i)(selection) ? 'selected' : '';
-
isRowSelected
(rowIndex: number) → fn(selection: Selection) → boolean
Checks if entire row at indexrowIndex
is selected.const className = isRowSelected(i)(selection) ? 'selected' : '';
-
isTableSelected
(selection: Selection) → boolean
Checks if entire table is selectedconst className = isTableSelected(selection) ? 'selected' : '';
-
getCellsInColumn
(columnIndex: number) → fn(selection: Selection) → ?[{pos: number, start: number, node: ProseMirrorNode}]
Returns an array of cells in a column at indexcolumnIndex
.const cells = getCellsInColumn(i)(selection); // [{node, pos}, {node, pos}]
-
getCellsInRow
(rowIndex: number) → fn(selection: Selection) → ?[{pos: number, start: number, node: ProseMirrorNode}]
Returns an array of cells in a row at indexrowIndex
.const cells = getCellsInRow(i)(selection); // [{node, pos}, {node, pos}]
-
getCellsInTable
(selection: Selection) → ?[{pos: number, start: number, node: ProseMirrorNode}]
Returns an array of all cells in a table.const cells = getCellsInTable(selection); // [{node, pos}, {node, pos}]
-
selectColumn
(columnIndex: number) → fn(tr: Transaction) → Transaction
Returns a new transaction that creates aCellSelection
on a column at indexcolumnIndex
.dispatch( selectColumn(i)(state.tr) );
-
selectRow
(rowIndex: number) → fn(tr: Transaction) → Transaction
Returns a new transaction that creates aCellSelection
on a column at indexrowIndex
.dispatch( selectRow(i)(state.tr) );
-
selectTable
(selection: Selection) → fn(tr: Transaction) → Transaction
Returns a new transaction that creates aCellSelection
on the entire table.dispatch( selectTable(i)(state.tr) );
-
emptyCell
(cell: {pos: number, node: ProseMirrorNode}, schema: Schema) → fn(tr: Transaction) → Transaction
Returns a new transaction that clears the content of a givencell
.const $pos = state.doc.resolve(13); dispatch( emptyCell(findCellClosestToPos($pos), state.schema)(state.tr) );
-
addColumnAt
(columnIndex: number) → fn(tr: Transaction) → Transaction
Returns a new transaction that adds a new column at indexcolumnIndex
.dispatch( addColumnAt(i)(state.tr) );
-
addRowAt
(rowIndex: number, clonePreviousRow: ?boolean) → fn(tr: Transaction) → Transaction
Returns a new transaction that adds a new row at indexrowIndex
. Optionally clone the previous row.dispatch( addRowAt(i)(state.tr) );
dispatch( addRowAt(i, true)(state.tr) );
-
cloneRowAt
(cloneRowIndex: number) → fn(tr: Transaction) → Transaction
Returns a new transaction that adds a new row aftercloneRowIndex
, cloning the row attributes atcloneRowIndex
.dispatch( cloneRowAt(i)(state.tr) );
-
removeColumnAt
(columnIndex: number) → fn(tr: Transaction) → Transaction
Returns a new transaction that removes a column at indexcolumnIndex
. If there is only one column left, it will remove the entire table.dispatch( removeColumnAt(i)(state.tr) );
-
removeRowAt
(rowIndex: number) → fn(tr: Transaction) → Transaction
Returns a new transaction that removes a row at indexrowIndex
. If there is only one row left, it will remove the entire table.dispatch( removeRowAt(i)(state.tr) );
-
removeTable
(tr: Transaction) → Transaction
Returns a new transaction that removes a table node if the cursor is inside of it.dispatch( removeTable(state.tr) );
-
removeSelectedColumns
(tr: Transaction) → Transaction
Returns a new transaction that removes selected columns.dispatch( removeSelectedColumns(state.tr) );
-
removeSelectedRows
(tr: Transaction) → Transaction
Returns a new transaction that removes selected rows.dispatch( removeSelectedRows(state.tr) );
-
removeColumnClosestToPos
($pos: ResolvedPos) → fn(tr: Transaction) → Transaction
Returns a new transaction that removes a column closest to a given$pos
.dispatch( removeColumnClosestToPos(state.doc.resolve(3))(state.tr) );
-
removeRowClosestToPos
($pos: ResolvedPos) → fn(tr: Transaction) → Transaction
Returns a new transaction that removes a row closest to a given$pos
.dispatch( removeRowClosestToPos(state.doc.resolve(3))(state.tr) );
-
findCellClosestToPos
($pos: ResolvedPos) → ?{pos: number, start: number, node: ProseMirrorNode}
Iterates over parent nodes, returning a table cell or a table header node closest to a given$pos
.const cell = findCellClosestToPos(state.selection.$from);
-
findCellRectClosestToPos
($pos: ResolvedPos) → ?{left: number, top: number, right: number, bottom: number}
Returns the rectangle spanning a cell closest to a given$pos
.dispatch( findCellRectClosestToPos(state.selection.$from) );
-
forEachCellInColumn
(columnIndex: number, cellTransform: fn(cell: {pos: number, start: number, node: ProseMirrorNode}, tr: Transaction) → Transaction, setCursorToLastCell: ?boolean) → fn(tr: Transaction) → Transaction
Returns a new transaction that maps a givencellTransform
function to each cell in a column at a givencolumnIndex
. It will set the selection into the last cell of the column ifsetCursorToLastCell
param is set totrue
.dispatch( forEachCellInColumn(0, (cell, tr) => emptyCell(cell, state.schema)(tr))(state.tr) );
-
forEachCellInRow
(rowIndex: number, cellTransform: fn(cell: {pos: number, start: number, node: ProseMirrorNode}, tr: Transaction) → Transaction, setCursorToLastCell: ?boolean) → fn(tr: Transaction) → Transaction
Returns a new transaction that maps a givencellTransform
function to each cell in a row at a givenrowIndex
. It will set the selection into the last cell of the row ifsetCursorToLastCell
param is set totrue
.dispatch( forEachCellInRow(0, (cell, tr) => setCellAttrs(cell, { background: 'red' })(tr))(state.tr) );
-
setCellAttrs
(cell: {pos: number, start: number, node: ProseMirrorNode}, attrs: Object) → fn(tr: Transaction) → Transaction
Returns a new transaction that sets givenattrs
to a givencell
.dispatch( setCellAttrs(findCellClosestToPos($pos), { background: 'blue' })(tr); );
-
createTable
(schema: Schema, rowsCount: ?number = 3, colsCount: ?number = 3, withHeaderRow: ?boolean = true) → Node
Returns a table node of a given size.withHeaderRow
defines whether the first row of the table will be a header row.const table = createTable(state.schema); // 3x3 table node dispatch( tr.replaceSelectionWith(table).scrollIntoView() );
-
removeParentNodeOfType
(nodeType: NodeType | [NodeType]) → fn(tr: Transaction) → Transaction
Returns a new transaction that removes a node of a givennodeType
. It will return an original transaction if parent node hasn't been found.dispatch( removeParentNodeOfType(schema.nodes.table)(tr) );
-
replaceParentNodeOfType
(nodeType: NodeType | [NodeType], content: ProseMirrorNode | Fragment) → fn(tr: Transaction) → Transaction
Returns a new transaction that replaces parent node of a givennodeType
with the givencontent
. It will return an original transaction if either parent node hasn't been found or replacing is not possible.const node = schema.nodes.paragraph.createChecked({}, schema.text('new')); dispatch( replaceParentNodeOfType(schema.nodes.table, node)(tr) );
-
removeSelectedNode
(tr: Transaction) → Transaction
Returns a new transaction that removes selected node. It will return an original transaction if current selection is not aNodeSelection
.dispatch( removeSelectedNode(tr) );
-
replaceSelectedNode
(node: ProseMirrorNode) → fn(tr: Transaction) → Transaction
Returns a new transaction that replaces selected node with a givennode
. It will return the original transaction if either current selection is not a NodeSelection or replacing is not possible.const node = schema.nodes.paragraph.createChecked({}, schema.text('new')); dispatch( replaceSelectedNode(node)(tr) );
-
canInsert
($pos: ResolvedPos, content: ProseMirrorNode | Fragment) → boolean
Checks if a givencontent
can be inserted at the given$pos
const { selection: { $from } } = state; const node = state.schema.nodes.atom.createChecked(); if (canInsert($from, node)) { // ... }
-
safeInsert
(content: ProseMirrorNode | Fragment, position: ?number) → fn(tr: Transaction) → Transaction
Returns a new transaction that inserts a givencontent
at the current cursor position, or at a givenposition
, if it is allowed by schema. If schema restricts such nesting, it will try to find an appropriate place for a given node in the document, looping through parent nodes up until the root document node. If cursor is inside of an empty paragraph, it will try to replace that paragraph with the given content. If insertion is successful and inserted node has content, it will set cursor inside of that content. It will return an original transaction if the place for insertion hasn't been found.const node = schema.nodes.extension.createChecked({}); dispatch( safeInsert(node)(tr) );
-
setParentNodeMarkup
(nodeType: NodeType | [NodeType], type: ?NodeType | null, attrs: ?Object | null, marks: ?[Mark]) → fn(tr: Transaction) → Transaction
Returns a transaction that changes the type, attributes, and/or marks of the parent node of a givennodeType
.const node = schema.nodes.extension.createChecked({}); dispatch( setParentNodeMarkup(schema.nodes.panel, null, { panelType })(tr); );
-
selectParentNodeOfType
(nodeType: NodeType | [NodeType]) → fn(tr: Transaction) → Transaction
Returns a new transaction that sets aNodeSelection
on a parent node of agiven nodeType
.dispatch( selectParentNodeOfType([tableCell, tableHeader])(state.tr) );
-
removeNodeBefore
(tr: Transaction) → Transaction
Returns a new transaction that deletes previous node.dispatch( removeNodeBefore(state.tr) );
-
setTextSelection
(position: number, dir: ?number = 1) → fn(tr: Transaction) → Transaction
Returns a new transaction that tries to find a valid cursor selection starting at the givenposition
and searching back ifdir
is negative, and forward if positive. If a valid cursor position hasn't been found, it will return the original transaction.dispatch( setTextSelection(5)(tr) );
- Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0