prosemirror-utils
Advanced tools
Comparing version 0.2.20 to 0.3.0
@@ -173,3 +173,3 @@ 'use strict'; | ||
// dispatch( | ||
// safeInsert(node)(tr) | ||
// setParentNodeMarkup(schema.nodes.panel, null, { panelType })(tr); | ||
// ); | ||
@@ -329,2 +329,46 @@ // ``` | ||
// ($pos: ResolvedPos) → ?{pos: number, node: ProseMirrorNode} | ||
// Iterates over parent nodes, returning a table node closest to a given `$pos`. | ||
// | ||
// ```javascript | ||
// const table = findTableClosestToPos(state.doc.resolve(10)); | ||
// ``` | ||
var findTableClosestToPos = function findTableClosestToPos($pos) { | ||
var predicate = function predicate(node) { | ||
return node.type.spec.tableRole && /table/i.test(node.type.spec.tableRole); | ||
}; | ||
return findParentNodeClosestToPos($pos, predicate); | ||
}; | ||
// ($pos: ResolvedPos) → ?{pos: number, node: ProseMirrorNode} | ||
// Iterates over parent nodes, returning a table cell or a table header node closest to a given `$pos`. | ||
// | ||
// ```javascript | ||
// const cell = findCellClosestToPos(state.doc.resolve(10)); | ||
// ``` | ||
var findCellClosestToPos = function findCellClosestToPos($pos) { | ||
var predicate = function predicate(node) { | ||
return node.type.spec.tableRole && /cell/i.test(node.type.spec.tableRole); | ||
}; | ||
return findParentNodeClosestToPos($pos, predicate); | ||
}; | ||
// ($pos: ResolvedPos) → ?{left: number, top: number, right: number, bottom: number} | ||
// Returns the rectangle spanning a cell closest to a given `$pos`. | ||
// | ||
// ```javascript | ||
// dispatch( | ||
// findCellRectClosestToPos(state.doc.resolve(10)) | ||
// ); | ||
// ``` | ||
var findCellRectClosestToPos = function findCellRectClosestToPos($pos) { | ||
var cell = findCellClosestToPos($pos); | ||
if (cell) { | ||
var table = findTableClosestToPos($pos); | ||
var map = prosemirrorTables.TableMap.get(table.node); | ||
var cellPos = cell.pos - table.pos - 1; | ||
return map.rectBetween(cellPos, cellPos); | ||
} | ||
}; | ||
// :: (predicate: (node: ProseMirrorNode) → boolean) → (selection: Selection) → ?{pos: number, node: ProseMirrorNode} | ||
@@ -353,2 +397,21 @@ // Iterates over parent nodes, returning the closest node and its start position `predicate` returns truthy for. | ||
// :: ($pos: ResolvedPos, predicate: (node: ProseMirrorNode) → boolean) → ?{pos: number, node: ProseMirrorNode} | ||
// Iterates over parent nodes starting from the given `$pos`, returning the closest node and its start position `predicate` returns truthy for. | ||
// | ||
// ```javascript | ||
// const predicate = node => node.type === schema.nodes.blockquote; | ||
// const parent = findParentNodeAtPos(state.doc.resolve(5), predicate); | ||
// ``` | ||
var findParentNodeClosestToPos = function findParentNodeClosestToPos($pos, predicate) { | ||
for (var i = $pos.depth; i > 0; i--) { | ||
var node = $pos.node(i); | ||
if (predicate(node)) { | ||
return { | ||
pos: $pos.start(i), | ||
node: node | ||
}; | ||
} | ||
} | ||
}; | ||
// :: (predicate: (node: ProseMirrorNode) → boolean, domAtPos: (pos: number) → {node: dom.Node, offset: number}) → (selection: Selection) → ?dom.Node | ||
@@ -399,2 +462,14 @@ // Iterates over parent nodes, returning DOM reference of the closest node `predicate` returns truthy for. | ||
// :: ($pos: ResolvedPos, nodeType: union<NodeType, [NodeType]>) → (state: EditorState) → ?{node: ProseMirrorNode, pos: number} | ||
// Iterates over parent nodes starting from the given `$pos`, returning closest node of a given `nodeType`. | ||
// | ||
// ```javascript | ||
// const parent = findParentNodeOfTypeAtPos(state.doc.resolve(10), schema.nodes.paragraph); | ||
// ``` | ||
var findParentNodeOfTypeClosestToPos = function findParentNodeOfTypeClosestToPos($pos, nodeType) { | ||
return findParentNodeClosestToPos($pos, function (node) { | ||
return equalNodeType(nodeType, node); | ||
}); | ||
}; | ||
// :: (nodeType: union<NodeType, [NodeType]>) → (selection: Selection) → boolean | ||
@@ -833,2 +908,24 @@ // Checks if there's a parent node of a given `nodeType`. | ||
// :: ($pos: ResolvedPos, schema: Schema) → (tr: Transaction) → Transaction | ||
// Returns a new transaction that clears the content of a cell closest to a given `$pos`. | ||
// | ||
// ```javascript | ||
// dispatch( | ||
// emptyCellClosestToPos(state.doc.resolve(10), state.schema)(state.tr) | ||
// ); | ||
// ``` | ||
var emptyCellClosestToPos = function emptyCellClosestToPos($pos, schema) { | ||
return function (tr) { | ||
var cell = findCellClosestToPos($pos); | ||
if (cell) { | ||
var emptyCell = tableNodeTypes(schema).cell.createAndFill().content; | ||
if (!cell.node.content.eq(emptyCell)) { | ||
tr.replaceWith($pos.pos, $pos.pos + cell.node.nodeSize - 1, new prosemirrorModel.Slice(emptyCell, 0, 0)); | ||
return cloneTr(tr); | ||
} | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: (schema: Schema) → (tr: Transaction) → Transaction | ||
@@ -848,3 +945,4 @@ // Returns a new transaction that clears the content of selected cells. | ||
if (!cell.content.eq(emptyCell)) { | ||
tr.replaceWith(tr.mapping.map(pos + 1), tr.mapping.map(pos + cell.nodeSize - 1), new prosemirrorModel.Slice(emptyCell, 0, 0)); | ||
var $pos = tr.doc.resolve(tr.mapping.map(pos + 1)); | ||
tr = emptyCellClosestToPos($pos, schema)(tr); | ||
} | ||
@@ -997,3 +1095,3 @@ }); | ||
} | ||
if (isCellSelection(selection) && selection.isColSelection()) { | ||
if (isCellSelection(selection)) { | ||
var table = findTable(selection); | ||
@@ -1027,3 +1125,3 @@ if (table) { | ||
} | ||
if (isCellSelection(selection) && selection.isRowSelection()) { | ||
if (isCellSelection(selection)) { | ||
var table = findTable(selection); | ||
@@ -1042,8 +1140,46 @@ if (table) { | ||
// :: ($pos: ResolvedPos) → (tr: Transaction) → Transaction | ||
// Returns a new transaction that removes a column closest to a given `$pos`. | ||
// | ||
// ```javascript | ||
// dispatch( | ||
// removeColumnClosestToPos(state.doc.resolve(3))(state.tr) | ||
// ); | ||
// ``` | ||
var removeColumnClosestToPos = function removeColumnClosestToPos($pos) { | ||
return function (tr) { | ||
var rect = findCellRectClosestToPos($pos); | ||
if (rect) { | ||
return removeColumnAt(rect.left)(tr); | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: ($pos: ResolvedPos) → (tr: Transaction) → Transaction | ||
// Returns a new transaction that removes a row closest to a given `$pos`. | ||
// | ||
// ```javascript | ||
// dispatch( | ||
// removeRowClosestToPos(state.doc.resolve(3))(state.tr) | ||
// ); | ||
// ``` | ||
var removeRowClosestToPos = function removeRowClosestToPos($pos) { | ||
return function (tr) { | ||
var rect = findCellRectClosestToPos($pos); | ||
if (rect) { | ||
return removeRowAt(rect.top)(tr); | ||
} | ||
return tr; | ||
}; | ||
}; | ||
exports.isNodeSelection = isNodeSelection; | ||
exports.canInsert = canInsert; | ||
exports.findParentNode = findParentNode; | ||
exports.findParentNodeClosestToPos = findParentNodeClosestToPos; | ||
exports.findParentDomRef = findParentDomRef; | ||
exports.hasParentNode = hasParentNode; | ||
exports.findParentNodeOfType = findParentNodeOfType; | ||
exports.findParentNodeOfTypeClosestToPos = findParentNodeOfTypeClosestToPos; | ||
exports.hasParentNodeOfType = hasParentNodeOfType; | ||
@@ -1074,2 +1210,3 @@ exports.findParentDomRefOfType = findParentDomRefOfType; | ||
exports.selectTable = selectTable; | ||
exports.emptyCellClosestToPos = emptyCellClosestToPos; | ||
exports.emptySelectedCells = emptySelectedCells; | ||
@@ -1083,2 +1220,4 @@ exports.addColumnAt = addColumnAt; | ||
exports.removeSelectedRows = removeSelectedRows; | ||
exports.removeColumnClosestToPos = removeColumnClosestToPos; | ||
exports.removeRowClosestToPos = removeRowClosestToPos; | ||
exports.removeParentNodeOfType = removeParentNodeOfType; | ||
@@ -1085,0 +1224,0 @@ exports.replaceParentNodeOfType = replaceParentNodeOfType; |
{ | ||
"name": "prosemirror-utils", | ||
"version": "0.2.20", | ||
"version": "0.3.0", | ||
"description": "Utils library for ProseMirror", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -32,2 +32,11 @@ # Utils library for ProseMirror | ||
* **`findParentNodeClosestToPos`**`($pos: ResolvedPos, predicate: fn(node: ProseMirrorNode) → boolean) → ?{pos: number, node: ProseMirrorNode}`\ | ||
Iterates over parent nodes starting from the given `$pos`, returning the closest node and its start position `predicate` returns truthy for. | ||
```javascript | ||
const predicate = node => node.type === schema.nodes.blockquote; | ||
const parent = findParentNodeAtPos(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`\ | ||
@@ -61,2 +70,10 @@ Iterates over parent nodes, returning DOM reference of the closest node `predicate` returns truthy for. | ||
* **`findParentNodeOfTypeClosestToPos`**`($pos: ResolvedPos, nodeType: NodeType | [NodeType]) → fn(state: EditorState) → ?{node: ProseMirrorNode, pos: number}`\ | ||
Iterates over parent nodes starting from the given `$pos`, returning closest node of a given `nodeType`. | ||
```javascript | ||
const parent = findParentNodeOfTypeAtPos(state.doc.resolve(10), schema.nodes.paragraph); | ||
``` | ||
* **`hasParentNodeOfType`**`(nodeType: NodeType | [NodeType]) → fn(selection: Selection) → boolean`\ | ||
@@ -306,2 +323,12 @@ Checks if there's a parent node of a given `nodeType`. | ||
* **`emptyCellClosestToPos`**`($pos: ResolvedPos, schema: Schema) → fn(tr: Transaction) → Transaction`\ | ||
Returns a new transaction that clears the content of a cell closest to a given `$pos`. | ||
```javascript | ||
dispatch( | ||
emptyCellClosestToPos(state.doc.resolve(10), state.schema)(state.tr) | ||
); | ||
``` | ||
* **`addColumnAt`**`(columnIndex: number) → fn(tr: Transaction) → Transaction`\ | ||
@@ -377,2 +404,22 @@ Returns a new transaction that adds a new column at index `columnIndex`. | ||
* **`removeColumnClosestToPos`**`($pos: ResolvedPos) → fn(tr: Transaction) → Transaction`\ | ||
Returns a new transaction that removes a column closest to a given `$pos`. | ||
```javascript | ||
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`. | ||
```javascript | ||
dispatch( | ||
removeRowClosestToPos(state.doc.resolve(3))(state.tr) | ||
); | ||
``` | ||
### Utils for document transformation | ||
@@ -455,3 +502,3 @@ | ||
dispatch( | ||
safeInsert(node)(tr) | ||
setParentNodeMarkup(schema.nodes.panel, null, { panelType })(tr); | ||
); | ||
@@ -458,0 +505,0 @@ ``` |
@@ -11,2 +11,4 @@ import { Node as ProsemirrorNode, Schema, NodeType, Mark, MarkType, ResolvedPos, Fragment } from 'prosemirror-model'; | ||
export function findParentNodeClosestToPos($pos: ResolvedPos, predicate: Predicate): {pos: number, node: ProsemirrorNode} | undefined; | ||
export function findParentDomRef(predicate: Predicate, domAtPos: DomAtPos): (selection: Selection) => Node | undefined; | ||
@@ -18,2 +20,4 @@ | ||
export function findParentNodeOfTypeClosestToPos($pos: ResolvedPos, nodeType: NodeType | NodeType[]): {pos: number, node: ProsemirrorNode} | undefined; | ||
export function hasParentNodeOfType(nodeType: NodeType | NodeType[]): (selection: Selection) => boolean; | ||
@@ -75,2 +79,4 @@ | ||
export function emptyCellClosestToPos($pos: ResolvedPos, schema: Schema): (tr: Transaction) => Transaction; | ||
export function addColumnAt(columnIndex: number): (tr: Transaction) => Transaction; | ||
@@ -90,2 +96,6 @@ | ||
export function removeColumnClosestToPos($pos: ResolvedPos): (tr: Transaction) => Transaction; | ||
export function removeRowClosestToPos($pos: ResolvedPos): (tr: Transaction) => Transaction; | ||
// Transforms | ||
@@ -92,0 +102,0 @@ export function removeParentNodeOfType(nodeType: NodeType | NodeType[]): (tr: Transaction) => Transaction; |
Sorry, the diff of this file is not supported yet
127980
1196
539