New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

prosemirror-utils

Package Overview
Dependencies
Maintainers
1
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prosemirror-utils - npm Package Compare versions

Comparing version 0.2.13 to 0.2.14

405

dist/index.js

@@ -10,3 +10,10 @@ 'use strict';

// :: (selection: Selection) → boolean
// Checks if current selection is a NodeSelection
// Checks if current selection is a `NodeSelection`.
//
// Example
// ```javascript
// if (isNodeSelection(tr.selection)) {
// // ...
// }
// ```
var isNodeSelection = function isNodeSelection(selection) {

@@ -83,2 +90,11 @@ return selection instanceof prosemirrorState.NodeSelection;

// Checks if a given `content` can be inserted at the given `$pos`
//
// Example
// ```javascript
// const { selection: { $from } } = state;
// const node = state.schema.nodes.atom.createChecked();
// if (canInsert($from, node)) {
// // ...
// }
// ```
var canInsert = function canInsert($pos, content) {

@@ -102,3 +118,9 @@ var index = $pos.index();

// :: (predicate: (node: ProseMirrorNode) → boolean) → (selection: Selection) → ?{pos: number, node: ProseMirrorNode}
// Iterates over parent nodes, returning the first node and its position `predicate` returns truthy for.
// Iterates over parent nodes, returning the closest node and its start position `predicate` returns truthy for.
//
// Example
// ```javascript
// const predicate = node => node.type === schema.nodes.blockquote;
// const parent = findParentNode(predicate)(selection);
// ```
var findParentNode = function findParentNode(predicate) {

@@ -121,3 +143,10 @@ return function (selection) {

// :: (predicate: (node: ProseMirrorNode) → boolean, domAtPos: (pos: number) → {node: dom.Node, offset: number}) → (selection: Selection) → ?dom.Node
// Iterates over parent nodes, returning DOM reference of the first node `predicate` returns truthy for.
// Iterates over parent nodes, returning DOM reference of the closest node `predicate` returns truthy for.
//
// Example
// ```javascript
// const domAtPos = view.domAtPos.bind(view);
// const predicate = node => node.type === schema.nodes.table;
// const parent = findParentDomRef(predicate, domAtPos)(selection); // <table>
// ```
var findParentDomRef = function findParentDomRef(predicate, domAtPos) {

@@ -134,2 +163,9 @@ return function (selection) {

// Checks if there's a parent node `predicate` returns truthy for.
//
// Example
// ```javascript
// if (hasParentNode(node => node.type === schema.nodes.table)(selection)) {
// // ....
// }
// ```
var hasParentNode = function hasParentNode(predicate) {

@@ -142,3 +178,8 @@ return function (selection) {

// :: (nodeType: union<NodeType, [NodeType]>) → (selection: Selection) → ?{node: ProseMirrorNode, pos: number}
// Iterates over parent nodes, returning first node of the given `nodeType`.
// Iterates over parent nodes, returning closest node of a given `nodeType`.
//
// Example
// ```javascript
// const parent = findParentNodeOfType(schema.nodes.paragraph)(selection);
// ```
var findParentNodeOfType = function findParentNodeOfType(nodeType) {

@@ -153,3 +194,10 @@ return function (selection) {

// :: (nodeType: union<NodeType, [NodeType]>) → (selection: Selection) → boolean
// Checks if there's a parent node of the given `nodeType`.
// Checks if there's a parent node of a given `nodeType`.
//
// Example
// ```javascript
// if (hasParentNodeOfType(schema.nodes.table)(selection)) {
// // ....
// }
// ```
var hasParentNodeOfType = function hasParentNodeOfType(nodeType) {

@@ -164,3 +212,9 @@ return function (selection) {

// :: (nodeType: union<NodeType, [NodeType]>, domAtPos: (pos: number) → {node: dom.Node, offset: number}) → (selection: Selection) → ?dom.Node
// Iterates over parent nodes, returning DOM reference of the first node of the given `nodeType`.
// Iterates over parent nodes, returning DOM reference of the closest node of a given `nodeType`.
//
// Example
// ```javascript
// const domAtPos = view.domAtPos.bind(view);
// const parent = findParentDomRefOfType(schema.nodes.codeBlock, domAtPos)(selection); // <pre>
// ```
var findParentDomRefOfType = function findParentDomRefOfType(nodeType, domAtPos) {

@@ -175,3 +229,13 @@ return function (selection) {

// :: (nodeType: union<NodeType, [NodeType]>) → (selection: Selection) → ?{node: ProseMirrorNode, pos: number}
// Returns a node of a given `nodeType` if its selected.
// Returns a node of a given `nodeType` if it is selected.
//
// Example
// ```javascript
// const { extension, inlineExtension, bodiedExtension } = schema.nodes;
// const selectedNode = findSelectedNodeOfType([
// extension,
// inlineExtension,
// bodiedExtension,
// ])(selection);
// ```
var findSelectedNodeOfType = function findSelectedNodeOfType(nodeType) {

@@ -191,3 +255,8 @@ return function (selection) {

// :: (selection: Selection) → ?number
// Returns position of the previous node
// Returns position of the previous node.
//
// Example
// ```javascript
// const pos = findPositionOfNodeBefore(tr.selection);
// ```
var findPositionOfNodeBefore = function findPositionOfNodeBefore(selection) {

@@ -228,3 +297,8 @@ var nodeBefore = selection.$from.nodeBefore;

// :: (node: ProseMirrorNode, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }]
// Flattens descendants of a given `node`. Doesn't descend into a `node` when `descend` argument is `false`. Defaults to `true`.
// Flattens descendants of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
//
// Example
// ```javascript
// const children = flatten(node);
// ```
var flatten = function flatten(node) {

@@ -247,3 +321,8 @@ var descend = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

// :: (node: ProseMirrorNode, predicate: (node: ProseMirrorNode) → boolean, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }]
// Iterates over descendants of a given `node`, returning child nodes `predicate` returns truthy for. Doesn't descend into a `node` when `descend` argument is `false`.
// Iterates over descendants of a given `node`, returning child nodes predicate returns truthy for. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
//
// Example
// ```javascript
// const textNodes = findChildren(node, child => child.isText, false);
// ```
var findChildren = function findChildren(node, predicate, descend) {

@@ -261,3 +340,8 @@ if (!node) {

// :: (node: ProseMirrorNode, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }]
// Returns text nodes of a given `node`. Doesn't descend into a `node` when `descend` argument is `false`.
// Returns text nodes of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
//
// Example
// ```javascript
// const textNodes = findTextNodes(node);
// ```
var findTextNodes = function findTextNodes(node, descend) {

@@ -270,3 +354,8 @@ return findChildren(node, function (child) {

// :: (node: ProseMirrorNode, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }]
// Returns inline nodes of a given `node`. Doesn't descend into a `node` when `descend` argument is `false`.
// Returns inline nodes of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
//
// Example
// ```javascript
// const inlineNodes = findInlineNodes(node);
// ```
var findInlineNodes = function findInlineNodes(node, descend) {

@@ -279,3 +368,8 @@ return findChildren(node, function (child) {

// :: (node: ProseMirrorNode, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }]
// Returns block descendants of a given `node`. Doesn't descend into a `node` when `descend` argument is `false`.
// Returns block descendants of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
//
// Example
// ```javascript
// const blockNodes = findBlockNodes(node);
// ```
var findBlockNodes = function findBlockNodes(node, descend) {

@@ -288,3 +382,8 @@ return findChildren(node, function (child) {

// :: (node: ProseMirrorNode, predicate: (attrs: ?Object) → boolean, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }]
// Iterates over descendants of a given `node`, returning child nodes `predicate` returns truthy for. Doesn't descend into a `node` when `descend` argument is `false`.
// Iterates over descendants of a given `node`, returning child nodes predicate returns truthy for. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
//
// Example
// ```javascript
// const mergedCells = findChildrenByAttr(table, attrs => attrs.colspan === 2);
// ```
var findChildrenByAttr = function findChildrenByAttr(node, predicate, descend) {

@@ -297,3 +396,8 @@ return findChildren(node, function (child) {

// :: (node: ProseMirrorNode, nodeType: NodeType, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }]
// Iterates over descendants of a given `node`, returning child nodes of a given `nodeType`. Doesn't descend into a `node` when `descend` argument is `false`.
// Iterates over descendants of a given `node`, returning child nodes of a given nodeType. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
//
// Example
// ```javascript
// const cells = findChildrenByType(table, schema.nodes.tableCell);
// ```
var findChildrenByType = function findChildrenByType(node, nodeType, descend) {

@@ -306,4 +410,8 @@ return findChildren(node, function (child) {

// :: (node: ProseMirrorNode, markType: markType, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }]
// Iterates over descendants of a given `node`, returning child nodes that have a mark of a given `markType`. Doesn't descend into a `node` when `descend` argument is `false`.
// exapmle: `findChildrenByMark(paragraph, schema.marks.strong)`
// Iterates over descendants of a given `node`, returning child nodes that have a mark of a given markType. It doesn't descend into a `node` when descend argument is `false` (defaults to `true`).
//
// Example
// ```javascript
// const nodes = findChildrenByMark(state.doc, schema.marks.strong);
// ```
var findChildrenByMark = function findChildrenByMark(node, markType, descend) {

@@ -316,3 +424,10 @@ return findChildren(node, function (child) {

// :: (node: ProseMirrorNode, nodeType: NodeType) → boolean
// Returns `true` if a given `node` contains nodes of a given `nodeType`
// Returns `true` if a given node contains nodes of a given `nodeType`
//
// Example
// ```javascript
// if (contains(panel, schema.nodes.listItem)) {
// // ...
// }
// ```
var contains = function contains(node, nodeType) {

@@ -323,3 +438,8 @@ return !!findChildrenByType(node, nodeType).length;

// :: (selection: Selection) → ?{pos: number, node: ProseMirrorNode}
// Iterates over parent nodes, returning the first found table node.
// Iterates over parent nodes, returning the closest table node.
//
// Example
// ```javascript
// const table = findTable(selection);
// ```
var findTable = function findTable(selection) {

@@ -332,3 +452,10 @@ return findParentNode(function (node) {

// :: (selection: Selection) → boolean
// Checks if current selection is a CellSelection
// Checks if current selection is a `CellSelection`.
//
// Example
// ```javascript
// if (isCellSelection(selection)) {
// // ...
// }
// ```
var isCellSelection = function isCellSelection(selection) {

@@ -339,3 +466,8 @@ return selection instanceof prosemirrorTables.CellSelection;

// :: (columnIndex: number) → (selection: Selection) → boolean
// Checks if entire column at index `columnIndex` is selected
// Checks if entire column at index `columnIndex` is selected.
//
// Example
// ```javascript
// const className = isColumnSelected(i)(selection) ? 'selected' : '';
// ```
var isColumnSelected = function isColumnSelected(columnIndex) {

@@ -360,3 +492,8 @@ return function (selection) {

// :: (rowIndex: number) → (selection: Selection) → boolean
// Checks if entire row at index `rowIndex` is selected
// Checks if entire row at index `rowIndex` is selected.
//
// Example
// ```javascript
// const className = isRowSelected(i)(selection) ? 'selected' : '';
// ```
var isRowSelected = function isRowSelected(rowIndex) {

@@ -380,2 +517,7 @@ return function (selection) {

// Checks if entire table is selected
//
// Example
// ```javascript
// const className = isTableSelected(selection) ? 'selected' : '';
// ```
var isTableSelected = function isTableSelected(selection) {

@@ -391,2 +533,7 @@ if (isCellSelection(selection)) {

// Returns an array of cells in a column at index `columnIndex`.
//
// Example
// ```javascript
// const cells = getCellsInColumn(i)(selection); // [{node, pos}, {node, pos}]
// ```
var getCellsInColumn = function getCellsInColumn(columnIndex) {

@@ -415,2 +562,7 @@ return function (selection) {

// Returns an array of cells in a row at index `rowIndex`.
//
// Example
// ```javascript
// const cells = getCellsInRow(i)(selection); // [{node, pos}, {node, pos}]
// ```
var getCellsInRow = function getCellsInRow(rowIndex) {

@@ -439,2 +591,7 @@ return function (selection) {

// Returns an array of all cells in a table.
//
// Example
// ```javascript
// const cells = getCellsInTable(selection); // [{node, pos}, {node, pos}]
// ```
var getCellsInTable = function getCellsInTable(selection) {

@@ -458,3 +615,10 @@ var table = findTable(selection);

// :: (columnIndex: number) → (tr: Transaction) → Transaction
// Creates a CellSelection on a column at `columnIndex`.
// Returns a new transaction that creates a `CellSelection` on a column at index `columnIndex`.
//
// Example
// ```javascript
// dispatch(
// selectColumn(i)(state.tr)
// );
// ```
var selectColumn = function selectColumn(columnIndex) {

@@ -473,3 +637,10 @@ return function (tr) {

// :: (rowIndex: number) → (tr: Transaction) → Transaction
// Creates a CellSelection on a row at `rowIndex`.
// Returns a new transaction that creates a `CellSelection` on a column at index `rowIndex`.
//
// Example
// ```javascript
// dispatch(
// selectRow(i)(state.tr)
// );
// ```
var selectRow = function selectRow(rowIndex) {

@@ -488,3 +659,10 @@ return function (tr) {

// :: (selection: Selection) → (tr: Transaction) → Transaction
// Creates a CellSelection on the entire table.
// Returns a new transaction that creates a `CellSelection` on the entire table.
//
// Example
// ```javascript
// dispatch(
// selectTable(i)(state.tr)
// );
// ```
var selectTable = function selectTable(tr) {

@@ -501,3 +679,10 @@ var cells = getCellsInTable(tr.selection);

// :: (schema: Schema) → (tr: Transaction) → Transaction
// Clears the content of selected cells.
// Returns a new transaction that clears the content of selected cells.
//
// Example
// ```javascript
// dispatch(
// emptySelectedCells(state.schema)(state.tr)
// );
// ```
var emptySelectedCells = function emptySelectedCells(schema) {

@@ -521,3 +706,10 @@ return function (tr) {

// :: (columnIndex: number) → (tr: Transaction) → Transaction
// Returns a new transaction that adds a new column at `columnIndex`.
// Returns a new transaction that adds a new column at index `columnIndex`.
//
// Example
// ```javascript
// dispatch(
// addColumnAt(i)(state.tr)
// );
// ```
var addColumnAt = function addColumnAt(columnIndex) {

@@ -541,3 +733,10 @@ return function (tr) {

// :: (rowIndex: number) → (tr: Transaction) → Transaction
// Returns a new transaction that adds a new row at `rowIndex`.
// Returns a new transaction that adds a new row at index `rowIndex`.
//
// Example
// ```javascript
// dispatch(
// addRowAt(i)(state.tr)
// );
// ```
var addRowAt = function addRowAt(rowIndex) {

@@ -561,3 +760,10 @@ return function (tr) {

// :: (columnIndex: number) → (tr: Transaction) → Transaction
// Returns a new transaction that removes a column at `columnIndex`.
// Returns a new transaction that removes a column at index `columnIndex`.
//
// Example
// ```javascript
// dispatch(
// removeColumnAt(i)(state.tr)
// );
// ```
var removeColumnAt = function removeColumnAt(columnIndex) {

@@ -582,3 +788,10 @@ return function (tr) {

// :: (rowIndex: number) → (tr: Transaction) → Transaction
// Returns a new transaction that removes a row at `rowIndex`.
// Returns a new transaction that removes a row at index `rowIndex`.
//
// Example
// ```javascript
// dispatch(
// removeRowAt(i)(state.tr)
// );
// ```
var removeRowAt = function removeRowAt(rowIndex) {

@@ -603,3 +816,10 @@ return function (tr) {

// :: (tr: Transaction) → Transaction
// Returns a new transaction that removes a table if the cursor is inside
// Returns a new transaction that removes a table node if the cursor is inside of it.
//
// Example
// ```javascript
// dispatch(
// removeTable(state.tr)
// );
// ```
var removeTable = function removeTable(tr) {

@@ -618,3 +838,10 @@ var $from = tr.selection.$from;

// :: (tr: Transaction) → Transaction
// Returns a new transaction that removes selected columns
// Returns a new transaction that removes selected columns.
//
// Example
// ```javascript
// dispatch(
// removeSelectedColumns(state.tr)
// );
// ```
var removeSelectedColumns = function removeSelectedColumns(tr) {

@@ -642,3 +869,10 @@ var _tr = tr,

// :: (tr: Transaction) → Transaction
// Returns a new transaction that removes selected rows
// Returns a new transaction that removes selected rows.
//
// Example
// ```javascript
// dispatch(
// removeSelectedRows(state.tr)
// );
// ```
var removeSelectedRows = function removeSelectedRows(tr) {

@@ -666,4 +900,10 @@ var _tr2 = tr,

// :: (nodeType: union<NodeType, [NodeType]>) → (tr: Transaction) → Transaction
// Returns a new transaction that removes a node of a given `nodeType`.
// It will return the original transaction if parent node hasn't been found.
// Returns a new transaction that removes a node of a given `nodeType`. It will return an original transaction if parent node hasn't been found.
//
// Example
// ```javascript
// dispatch(
// removeParentNodeOfType(schema.nodes.table)(tr)
// );
// ```
var removeParentNodeOfType = function removeParentNodeOfType(nodeType) {

@@ -680,4 +920,12 @@ return function (tr) {

// :: (nodeType: union<NodeType, [NodeType]>, content: union<ProseMirrorNode, Fragment>) → (tr: Transaction) → Transaction
// Returns a new transaction that replaces parent node of a given `nodeType` with the given `content`.
// It will return the original transaction if parent node hasn't been found, or replacing is not possible.
// Returns a new transaction that replaces parent node of a given `nodeType` with the given `content`. It will return an original transaction if either parent node hasn't been found or replacing is not possible.
//
// Example
// ```javascript
// const node = schema.nodes.paragraph.createChecked({}, schema.text('new'));
//
// dispatch(
// replaceParentNodeOfType(schema.nodes.table, node)(tr)
// );
// ```
var replaceParentNodeOfType = function replaceParentNodeOfType(nodeType, content) {

@@ -694,4 +942,10 @@ return function (tr) {

// :: (tr: Transaction) → Transaction
// Returns a new transaction that removes selected node.
// It will return the original transaction if current selection is not a NodeSelection
// Returns a new transaction that removes selected node. It will return an original transaction if current selection is not a `NodeSelection`.
//
// Example
// ```javascript
// dispatch(
// removeSelectedNode(tr)
// );
// ```
var removeSelectedNode = function removeSelectedNode(tr) {

@@ -708,3 +962,11 @@ if (isNodeSelection(tr.selection)) {

// Returns a new transaction that replaces selected node with a given `node`.
// It will return the original transaction if current selection is not a NodeSelection, or replacing is not possible.
// It will return the original transaction if either current selection is not a NodeSelection or replacing is not possible.
//
// Example
// ```javascript
// const node = schema.nodes.paragraph.createChecked({}, schema.text('new'));
// dispatch(
// replaceSelectedNode(node)(tr)
// );
// ```
var replaceSelectedNode = function replaceSelectedNode(node) {

@@ -725,8 +987,17 @@ return function (tr) {

// :: (position: number) → (tr: Transaction) → Transaction
// Tries to find a valid cursor selection **starting** at the given `position` and returns a new transaction.
// :: (position: number, dir: ?number = 1) → (tr: Transaction) → Transaction
// Returns a new transaction that tries to find a valid cursor selection starting at the given `position`
// and searching back if `dir` is negative, and forward if positive.
// If a valid cursor position hasn't been found, it will return the original transaction.
//
// Example
// ```javascript
// dispatch(
// setTextSelection(5)(tr)
// );
// ```
var setTextSelection = function setTextSelection(position) {
var dir = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
return function (tr) {
var nextSelection = prosemirrorState.Selection.findFrom(tr.doc.resolve(position), 1, true);
var nextSelection = prosemirrorState.Selection.findFrom(tr.doc.resolve(position), dir, true);
if (nextSelection) {

@@ -740,9 +1011,17 @@ return tr.setSelection(nextSelection);

// :: (content: union<ProseMirrorNode, Fragment>, position: ?number) → (tr: Transaction) → Transaction
// Returns a new transaction that inserts a given `node` at the current cursor position, or at a given `position`, 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 at the top level (depth=0), 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 the original transaction if the place for insertion hasn't been found.
// Returns a new transaction that inserts a given `content` at the current cursor position, or at a given `position`, 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.
//
// Example
// ```javascript
// const node = schema.nodes.extension.createChecked({});
// dispatch(
// safeInsert(node)(tr)
// );
// ```
var safeInsert = function safeInsert(content, position) {
return function (tr) {
var $from = typeof position === 'number' ? tr.doc.resolve(position) : tr.selection.$from;
var hasPosition = typeof position === 'number';
var $from = hasPosition ? tr.doc.resolve(position) : tr.selection.$from;
var parent = $from.parent,

@@ -764,3 +1043,3 @@ depth = $from.depth;

tr.insert($from.pos, content);
return cloneTr(setTextSelection($from.pos)(tr));
return cloneTr(setTextSelection(hasPosition ? $from.pos : tr.selection.$anchor.pos)(tr));
}

@@ -774,3 +1053,3 @@

tr.insert(pos, content);
return cloneTr(setTextSelection(pos)(tr));
return cloneTr(setTextSelection(tr.mapping.map(pos), -1)(tr));
}

@@ -784,2 +1063,10 @@ }

// Returns a transaction that changes the type, attributes, and/or marks of the parent node of a given `nodeType`.
//
// Example
// ```javascript
// const node = schema.nodes.extension.createChecked({});
// dispatch(
// safeInsert(node)(tr)
// );
// ```
var setParentNodeMarkup = function setParentNodeMarkup(nodeType, type, attrs, marks) {

@@ -796,3 +1083,10 @@ return function (tr) {

// :: (nodeType: union<NodeType, [NodeType]>) → (tr: Transaction) → Transaction
// Returns a transaction that sets a NodeSelection on a parent node of a given `nodeType`.
// Returns a new transaction that sets a `NodeSelection` on a parent node of a `given nodeType`.
//
// Example
// ```javascript
// dispatch(
// selectParentNodeOfType([tableCell, tableHeader])(state.tr)
// );
// ```
var selectParentNodeOfType = function selectParentNodeOfType(nodeType) {

@@ -811,3 +1105,10 @@ return function (tr) {

// :: (tr: Transaction) → Transaction
// Returns a transaction that deletes previous node from the current selection
// Returns a new transaction that deletes previous node.
//
// Example
// ```javascript
// dispatch(
// removeNodeBefore(state.tr)
// );
// ```
var removeNodeBefore = function removeNodeBefore(tr) {

@@ -814,0 +1115,0 @@ var position = findPositionOfNodeBefore(tr.selection);

2

package.json
{
"name": "prosemirror-utils",
"version": "0.2.13",
"version": "0.2.14",
"description": "Utils library for ProseMirror",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -19,42 +19,102 @@ # Utils library for ProseMirror

## Documentation
## Public API documentation
### Selection
### Utils for working with `selection`
* **`findParentNode`**`(predicate: fn(node: ProseMirrorNode) → boolean) → fn(selection: Selection) → ?{pos: number, node: ProseMirrorNode}`\
Iterates over parent nodes, returning the first node and its position `predicate` returns truthy for.
Iterates over parent nodes, returning the closest node and its start position `predicate` returns truthy for.
Example
```javascript
const predicate = node => node.type === schema.nodes.blockquote;
const parent = findParentNode(predicate)(selection);
```
* **`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 first node `predicate` returns truthy for.
Iterates over parent nodes, returning DOM reference of the closest node `predicate` returns truthy for.
Example
```javascript
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 node `predicate` returns truthy for.
Example
```javascript
if (hasParentNode(node => node.type === schema.nodes.table)(selection)) {
// ....
}
```
* **`findParentNodeOfType`**`(nodeType: NodeType | [NodeType]) → fn(selection: Selection) → ?{node: ProseMirrorNode, pos: number}`\
Iterates over parent nodes, returning first node of the given `nodeType`.
Iterates over parent nodes, returning closest node of a given `nodeType`.
Example
```javascript
const parent = findParentNodeOfType(schema.nodes.paragraph)(selection);
```
* **`hasParentNodeOfType`**`(nodeType: NodeType | [NodeType]) → fn(selection: Selection) → boolean`\
Checks if there's a parent node of the given `nodeType`.
Checks if there's a parent node of a given `nodeType`.
Example
```javascript
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 first node of the given `nodeType`.
Iterates over parent nodes, returning DOM reference of the closest node of a given `nodeType`.
Example
```javascript
const domAtPos = view.domAtPos.bind(view);
const parent = findParentDomRefOfType(schema.nodes.codeBlock, domAtPos)(selection); // <pre>
```
* **`findSelectedNodeOfType`**`(nodeType: NodeType | [NodeType]) → fn(selection: Selection) → ?{node: ProseMirrorNode, pos: number}`\
Returns a node of a given `nodeType` if its selected.
Returns a node of a given `nodeType` if it is selected.
Example
```javascript
const { extension, inlineExtension, bodiedExtension } = schema.nodes;
const selectedNode = findSelectedNodeOfType([
extension,
inlineExtension,
bodiedExtension,
])(selection);
```
* **`isNodeSelection`**`(selection: Selection) → boolean`\
Checks if current selection is a NodeSelection
Checks if current selection is a `NodeSelection`.
Example
```javascript
if (isNodeSelection(tr.selection)) {
// ...
}
```
* **`findPositionOfNodeBefore`**`(selection: Selection) → ?number`\
Returns position of the previous node
Returns position of the previous node.
Example
```javascript
const pos = findPositionOfNodeBefore(tr.selection);
```
* **`findDomRefAtPos`**`(position: number, domAtPos: fn(pos: number) → {node: dom.Node, offset: number}) → dom.Node`\

@@ -71,169 +131,407 @@ Returns DOM reference of a node at a given `position`.

### Node
### Utils for working with ProseMirror `node`
* **`flatten`**`(node: ProseMirrorNode, descend: ?boolean = true) → [{node: ProseMirrorNode, pos: number}]`\
Flattens descendants of a given `node`. Doesn't descend into a `node` when `descend` argument is `false`. Defaults to `true`.
Flattens descendants of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
Example
```javascript
const children = flatten(node);
```
* **`findChildren`**`(node: ProseMirrorNode, predicate: fn(node: ProseMirrorNode) → boolean, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]`\
Iterates over descendants of a given `node`, returning child nodes `predicate` returns truthy for. Doesn't descend into a `node` when `descend` argument is `false`.
Iterates over descendants of a given `node`, returning child nodes predicate returns truthy for. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
Example
```javascript
const textNodes = findChildren(node, child => child.isText, false);
```
* **`findTextNodes`**`(node: ProseMirrorNode, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]`\
Returns text nodes of a given `node`. Doesn't descend into a `node` when `descend` argument is `false`.
Returns text nodes of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
Example
```javascript
const textNodes = findTextNodes(node);
```
* **`findInlineNodes`**`(node: ProseMirrorNode, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]`\
Returns inline nodes of a given `node`. Doesn't descend into a `node` when `descend` argument is `false`.
Returns inline nodes of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
Example
```javascript
const inlineNodes = findInlineNodes(node);
```
* **`findBlockNodes`**`(node: ProseMirrorNode, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]`\
Returns block descendants of a given `node`. Doesn't descend into a `node` when `descend` argument is `false`.
Returns block descendants of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
Example
```javascript
const blockNodes = findBlockNodes(node);
```
* **`findChildrenByAttr`**`(node: ProseMirrorNode, predicate: fn(attrs: ?Object) → boolean, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]`\
Iterates over descendants of a given `node`, returning child nodes `predicate` returns truthy for. Doesn't descend into a `node` when `descend` argument is `false`.
Iterates over descendants of a given `node`, returning child nodes predicate returns truthy for. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
Example
```javascript
const mergedCells = findChildrenByAttr(table, attrs => attrs.colspan === 2);
```
* **`findChildrenByType`**`(node: ProseMirrorNode, nodeType: NodeType, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]`\
Iterates over descendants of a given `node`, returning child nodes of a given `nodeType`. Doesn't descend into a `node` when `descend` argument is `false`.
Iterates over descendants of a given `node`, returning child nodes of a given nodeType. It doesn't descend into a node when descend argument is `false` (defaults to `true`).
Example
```javascript
const cells = findChildrenByType(table, schema.nodes.tableCell);
```
* **`findChildrenByMark`**`(node: ProseMirrorNode, markType: markType, descend: ?boolean) → [{node: ProseMirrorNode, pos: number}]`\
Iterates over descendants of a given `node`, returning child nodes that have a mark of a given `markType`. Doesn't descend into a `node` when `descend` argument is `false`.
exapmle: `findChildrenByMark(paragraph, schema.marks.strong)`
Iterates over descendants of a given `node`, returning child nodes that have a mark of a given markType. It doesn't descend into a `node` when descend argument is `false` (defaults to `true`).
Example
```javascript
const nodes = findChildrenByMark(state.doc, schema.marks.strong);
```
* **`contains`**`(node: ProseMirrorNode, nodeType: NodeType) → boolean`\
Returns `true` if a given `node` contains nodes of a given `nodeType`
Returns `true` if a given node contains nodes of a given `nodeType`
Example
```javascript
if (contains(panel, schema.nodes.listItem)) {
// ...
}
```
### Tables
### Utils for working with `table`
* **`findTable`**`(selection: Selection) → ?{pos: number, node: ProseMirrorNode}`\
Iterates over parent nodes, returning the first found table node.
Iterates over parent nodes, returning the closest table node.
Example
```javascript
const table = findTable(selection);
```
* **`isCellSelection`**`(selection: Selection) → boolean`\
Checks if current selection is a CellSelection
Checks if current selection is a `CellSelection`.
Example
```javascript
if (isCellSelection(selection)) {
// ...
}
```
* **`isColumnSelected`**`(columnIndex: number) → fn(selection: Selection) → boolean`\
Checks if entire column at index `columnIndex` is selected
Checks if entire column at index `columnIndex` is selected.
Example
```javascript
const className = isColumnSelected(i)(selection) ? 'selected' : '';
```
* **`isRowSelected`**`(rowIndex: number) → fn(selection: Selection) → boolean`\
Checks if entire row at index `rowIndex` is selected
Checks if entire row at index `rowIndex` is selected.
Example
```javascript
const className = isRowSelected(i)(selection) ? 'selected' : '';
```
* **`isTableSelected`**`(selection: Selection) → boolean`\
Checks if entire table is selected
Example
```javascript
const className = isTableSelected(selection) ? 'selected' : '';
```
* **`getCellsInColumn`**`(columnIndex: number) → fn(selection: Selection) → ?[{pos: number, node: ProseMirrorNode}]`\
Returns an array of cells in a column at index `columnIndex`.
Example
```javascript
const cells = getCellsInColumn(i)(selection); // [{node, pos}, {node, pos}]
```
* **`getCellsInRow`**`(rowIndex: number) → fn(selection: Selection) → ?[{pos: number, node: ProseMirrorNode}]`\
Returns an array of cells in a row at index `rowIndex`.
Example
```javascript
const cells = getCellsInRow(i)(selection); // [{node, pos}, {node, pos}]
```
* **`getCellsInTable`**`(selection: Selection) → ?[{pos: number, node: ProseMirrorNode}]`\
Returns an array of all cells in a table.
Example
```javascript
const cells = getCellsInTable(selection); // [{node, pos}, {node, pos}]
```
* **`selectColumn`**`(columnIndex: number) → fn(tr: Transaction) → Transaction`\
Creates a CellSelection on a column at `columnIndex`.
Returns a new transaction that creates a `CellSelection` on a column at index `columnIndex`.
Example
```javascript
dispatch(
selectColumn(i)(state.tr)
);
```
* **`selectRow`**`(rowIndex: number) → fn(tr: Transaction) → Transaction`\
Creates a CellSelection on a row at `rowIndex`.
Returns a new transaction that creates a `CellSelection` on a column at index `rowIndex`.
Example
```javascript
dispatch(
selectRow(i)(state.tr)
);
```
* **`selectTable`**`(selection: Selection) → fn(tr: Transaction) → Transaction`\
Creates a CellSelection on the entire table.
Returns a new transaction that creates a `CellSelection` on the entire table.
Example
```javascript
dispatch(
selectTable(i)(state.tr)
);
```
* **`emptySelectedCells`**`(schema: Schema) → fn(tr: Transaction) → Transaction`\
Clears the content of selected cells.
Returns a new transaction that clears the content of selected cells.
Example
```javascript
dispatch(
emptySelectedCells(state.schema)(state.tr)
);
```
* **`addColumnAt`**`(columnIndex: number) → fn(tr: Transaction) → Transaction`\
Returns a new transaction that adds a new column at `columnIndex`.
Returns a new transaction that adds a new column at index `columnIndex`.
Example
```javascript
dispatch(
addColumnAt(i)(state.tr)
);
```
* **`addRowAt`**`(rowIndex: number) → fn(tr: Transaction) → Transaction`\
Returns a new transaction that adds a new row at `rowIndex`.
Returns a new transaction that adds a new row at index `rowIndex`.
Example
```javascript
dispatch(
addRowAt(i)(state.tr)
);
```
* **`removeColumnAt`**`(columnIndex: number) → fn(tr: Transaction) → Transaction`\
Returns a new transaction that removes a column at `columnIndex`.
Returns a new transaction that removes a column at index `columnIndex`.
Example
```javascript
dispatch(
removeColumnAt(i)(state.tr)
);
```
* **`removeRowAt`**`(rowIndex: number) → fn(tr: Transaction) → Transaction`\
Returns a new transaction that removes a row at `rowIndex`.
Returns a new transaction that removes a row at index `rowIndex`.
Example
```javascript
dispatch(
removeRowAt(i)(state.tr)
);
```
* **`removeTable`**`(tr: Transaction) → Transaction`\
Returns a new transaction that removes a table if the cursor is inside
Returns a new transaction that removes a table node if the cursor is inside of it.
Example
```javascript
dispatch(
removeTable(state.tr)
);
```
* **`removeSelectedColumns`**`(tr: Transaction) → Transaction`\
Returns a new transaction that removes selected columns
Returns a new transaction that removes selected columns.
Example
```javascript
dispatch(
removeSelectedColumns(state.tr)
);
```
* **`removeSelectedRows`**`(tr: Transaction) → Transaction`\
Returns a new transaction that removes selected rows
Returns a new transaction that removes selected rows.
Example
```javascript
dispatch(
removeSelectedRows(state.tr)
);
```
### Transforms
### Utils for document transformation
* **`removeParentNodeOfType`**`(nodeType: NodeType | [NodeType]) → fn(tr: Transaction) → Transaction`\
Returns a new transaction that removes a node of a given `nodeType`.
It will return the original transaction if parent node hasn't been found.
Returns a new transaction that removes a node of a given `nodeType`. It will return an original transaction if parent node hasn't been found.
Example
```javascript
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 given `nodeType` with the given `content`.
It will return the original transaction if parent node hasn't been found, or replacing is not possible.
Returns a new transaction that replaces parent node of a given `nodeType` with the given `content`. It will return an original transaction if either parent node hasn't been found or replacing is not possible.
Example
```javascript
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 the original transaction if current selection is not a NodeSelection
Returns a new transaction that removes selected node. It will return an original transaction if current selection is not a `NodeSelection`.
Example
```javascript
dispatch(
removeSelectedNode(tr)
);
```
* **`replaceSelectedNode`**`(node: ProseMirrorNode) → fn(tr: Transaction) → Transaction`\
Returns a new transaction that replaces selected node with a given `node`.
It will return the original transaction if current selection is not a NodeSelection, or replacing is not possible.
It will return the original transaction if either current selection is not a NodeSelection or replacing is not possible.
Example
```javascript
const node = schema.nodes.paragraph.createChecked({}, schema.text('new'));
dispatch(
replaceSelectedNode(node)(tr)
);
```
* **`canInsert`**`($pos: ResolvedPos, content: ProseMirrorNode | Fragment) → boolean`\
Checks if a given `content` can be inserted at the given `$pos`
Example
```javascript
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 given `node` at the current cursor position, or at a given `position`, 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 at the top level (depth=0), 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 the original transaction if the place for insertion hasn't been found.
Returns a new transaction that inserts a given `content` at the current cursor position, or at a given `position`, 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.
Example
```javascript
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 given `nodeType`.
Example
```javascript
const node = schema.nodes.extension.createChecked({});
dispatch(
safeInsert(node)(tr)
);
```
* **`selectParentNodeOfType`**`(nodeType: NodeType | [NodeType]) → fn(tr: Transaction) → Transaction`\
Returns a transaction that sets a NodeSelection on a parent node of a given `nodeType`.
Returns a new transaction that sets a `NodeSelection` on a parent node of a `given nodeType`.
Example
```javascript
dispatch(
selectParentNodeOfType([tableCell, tableHeader])(state.tr)
);
```
* **`removeNodeBefore`**`(tr: Transaction) → Transaction`\
Returns a transaction that deletes previous node from the current selection
Returns a new transaction that deletes previous node.
Example
```javascript
dispatch(
removeNodeBefore(state.tr)
);
```
* **`setTextSelection`**`(position: number) → fn(tr: Transaction) → Transaction`\
Tries to find a valid cursor selection **starting** at the given `position` and returns a new transaction.
Returns a new transaction that tries to find a valid cursor selection starting at the given `position`.
If a valid cursor position hasn't been found, it will return the original transaction.
Example
```javascript
dispatch(
setTextSelection(5)(tr)
);
```
## License

@@ -240,0 +538,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc