prosemirror-utils
Advanced tools
Comparing version 1.0.0-0 to 1.1.1
@@ -1,19 +0,23 @@ | ||
'use strict'; | ||
"use strict"; | ||
(() => { | ||
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { | ||
get: (a, b) => (typeof require !== "undefined" ? require : a)[b] | ||
}) : x)(function(x) { | ||
if (typeof require !== "undefined") | ||
return require.apply(this, arguments); | ||
throw Error('Dynamic require of "' + x + '" is not supported'); | ||
}); | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
// src/selection.ts | ||
var import_prosemirror_state3 = __require("prosemirror-state"); | ||
var prosemirrorState = require('prosemirror-state'); | ||
var prosemirrorModel = require('prosemirror-model'); | ||
// src/helpers.ts | ||
var import_prosemirror_state2 = __require("prosemirror-state"); | ||
var import_prosemirror_model2 = __require("prosemirror-model"); | ||
// :: (nodeType: union<NodeType, [NodeType]>) → (tr: Transaction) → Transaction | ||
// 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. | ||
// | ||
// ```javascript | ||
// dispatch( | ||
// removeParentNodeOfType(schema.nodes.table)(tr) | ||
// ); | ||
// ``` | ||
var removeParentNodeOfType = function removeParentNodeOfType(nodeType) { | ||
return function (tr) { | ||
var parent = findParentNodeOfType(nodeType)(tr.selection); | ||
// src/transforms.ts | ||
var import_prosemirror_state = __require("prosemirror-state"); | ||
var import_prosemirror_model = __require("prosemirror-model"); | ||
var removeParentNodeOfType = (nodeType) => (tr) => { | ||
const parent = findParentNodeOfType(nodeType)(tr.selection); | ||
if (parent) { | ||
@@ -24,23 +28,10 @@ return removeNodeAtPos(parent.pos)(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 an original transaction if either parent node hasn't been found or replacing is not possible. | ||
// | ||
// ```javascript | ||
// const node = schema.nodes.paragraph.createChecked({}, schema.text('new')); | ||
// | ||
// dispatch( | ||
// replaceParentNodeOfType(schema.nodes.table, node)(tr) | ||
// ); | ||
// ``` | ||
var replaceParentNodeOfType = function replaceParentNodeOfType(nodeType, content) { | ||
return function (tr) { | ||
var replaceParentNodeOfType = (nodeType, content) => (tr) => { | ||
if (!Array.isArray(nodeType)) { | ||
nodeType = [nodeType]; | ||
} | ||
for (var i = 0, count = nodeType.length; i < count; i++) { | ||
var parent = findParentNodeOfType(nodeType[i])(tr.selection); | ||
for (let i = 0, count = nodeType.length; i < count; i++) { | ||
const parent = findParentNodeOfType(nodeType[i])(tr.selection); | ||
if (parent) { | ||
var newTr = replaceNodeAtPos(parent.pos, content)(tr); | ||
const newTr = replaceNodeAtPos(parent.pos, content)(tr); | ||
if (newTr !== tr) { | ||
@@ -53,42 +44,25 @@ return newTr; | ||
}; | ||
}; | ||
// :: (tr: Transaction) → Transaction | ||
// Returns a new transaction that removes selected node. It will return an original transaction if current selection is not a `NodeSelection`. | ||
// | ||
// ```javascript | ||
// dispatch( | ||
// removeSelectedNode(tr) | ||
// ); | ||
// ``` | ||
var removeSelectedNode = function removeSelectedNode(tr) { | ||
if (isNodeSelection(tr.selection)) { | ||
var from = tr.selection.$from.pos; | ||
var to = tr.selection.$to.pos; | ||
return cloneTr(tr.delete(from, to)); | ||
} | ||
return tr; | ||
}; | ||
// :: (content: union<ProseMirrorNode, ProseMirrorFragment>) → (tr: Transaction) → Transaction | ||
// Returns a new transaction that replaces selected node with a given `node`, keeping NodeSelection on the new `node`. | ||
// It will return the original transaction if either current selection is not a NodeSelection or replacing is not possible. | ||
// | ||
// ```javascript | ||
// const node = schema.nodes.paragraph.createChecked({}, schema.text('new')); | ||
// dispatch( | ||
// replaceSelectedNode(node)(tr) | ||
// ); | ||
// ``` | ||
var replaceSelectedNode = function replaceSelectedNode(content) { | ||
return function (tr) { | ||
var removeSelectedNode = (tr) => { | ||
if (isNodeSelection(tr.selection)) { | ||
var _tr$selection = tr.selection, | ||
$from = _tr$selection.$from, | ||
$to = _tr$selection.$to; | ||
if (content instanceof prosemirrorModel.Fragment && $from.parent.canReplace($from.index(), $from.indexAfter(), content) || $from.parent.canReplaceWith($from.index(), $from.indexAfter(), content.type)) { | ||
return cloneTr(tr.replaceWith($from.pos, $to.pos, content) | ||
// restore node selection | ||
.setSelection(new prosemirrorState.NodeSelection(tr.doc.resolve($from.pos)))); | ||
const from = tr.selection.$from.pos; | ||
const to = tr.selection.$to.pos; | ||
return cloneTr(tr.delete(from, to)); | ||
} | ||
return tr; | ||
}; | ||
var replaceSelectedNode = (content) => (tr) => { | ||
if (isNodeSelection(tr.selection)) { | ||
const { $from, $to } = tr.selection; | ||
if (content instanceof import_prosemirror_model.Fragment && $from.parent.canReplace( | ||
$from.index(), | ||
$from.indexAfter(), | ||
content | ||
) || content instanceof import_prosemirror_model.Node && $from.parent.canReplaceWith( | ||
$from.index(), | ||
$from.indexAfter(), | ||
content.type | ||
)) { | ||
return cloneTr( | ||
tr.replaceWith($from.pos, $to.pos, content).setSelection(new import_prosemirror_state.NodeSelection(tr.doc.resolve($from.pos))) | ||
); | ||
} | ||
@@ -98,18 +72,8 @@ } | ||
}; | ||
}; | ||
// :: (position: number, dir: ?number) → (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. | ||
// | ||
// ```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), dir, true); | ||
var setTextSelection = (position, dir = 1) => (tr) => { | ||
const nextSelection = import_prosemirror_state.Selection.findFrom( | ||
tr.doc.resolve(position), | ||
dir, | ||
true | ||
); | ||
if (nextSelection) { | ||
@@ -120,42 +84,17 @@ return tr.setSelection(nextSelection); | ||
}; | ||
}; | ||
var isSelectableNode = function isSelectableNode(node) { | ||
return node.type && node.type.spec.selectable; | ||
}; | ||
var shouldSelectNode = function shouldSelectNode(node) { | ||
return isSelectableNode(node) && node.type.isLeaf; | ||
}; | ||
var setSelection = function setSelection(node, pos, tr) { | ||
if (shouldSelectNode(node)) { | ||
return tr.setSelection(new prosemirrorState.NodeSelection(tr.doc.resolve(pos))); | ||
} | ||
return setTextSelection(pos)(tr); | ||
}; | ||
// :: (content: union<ProseMirrorNode, Fragment>, position: ?number, tryToReplace?: boolean) → (tr: Transaction) → Transaction | ||
// 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 `tryToReplace` is true and current selection is a NodeSelection, it will replace selected node with inserted content if its allowed by schema. | ||
// 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. | ||
// | ||
// ```javascript | ||
// const node = schema.nodes.extension.createChecked({}); | ||
// dispatch( | ||
// safeInsert(node)(tr) | ||
// ); | ||
// ``` | ||
var safeInsert = function safeInsert(content, position, tryToReplace) { | ||
return function (tr) { | ||
var hasPosition = typeof position === 'number'; | ||
var $from = tr.selection.$from; | ||
var $insertPos = hasPosition ? tr.doc.resolve(position) : isNodeSelection(tr.selection) ? tr.doc.resolve($from.pos + 1) : $from; | ||
var parent = $insertPos.parent; | ||
// try to replace selected node | ||
var isSelectableNode = (node) => Boolean(node instanceof import_prosemirror_model.Node && node.type && node.type.spec.selectable); | ||
var shouldSelectNode = (node) => isSelectableNode(node) && node.type.isLeaf; | ||
var setSelection = (node, pos, tr) => { | ||
if (shouldSelectNode(node)) { | ||
return tr.setSelection(new import_prosemirror_state.NodeSelection(tr.doc.resolve(pos))); | ||
} | ||
return setTextSelection(pos)(tr); | ||
}; | ||
var safeInsert = (content, position, tryToReplace) => (tr) => { | ||
const hasPosition = typeof position === "number"; | ||
const { $from } = tr.selection; | ||
const $insertPos = hasPosition ? tr.doc.resolve(position) : isNodeSelection(tr.selection) ? tr.doc.resolve($from.pos + 1) : $from; | ||
const { parent } = $insertPos; | ||
if (isNodeSelection(tr.selection) && tryToReplace) { | ||
var oldTr = tr; | ||
const oldTr = tr; | ||
tr = replaceSelectedNode(content)(tr); | ||
@@ -166,29 +105,27 @@ if (oldTr !== tr) { | ||
} | ||
// try to replace an empty paragraph | ||
if (isEmptyParagraph(parent)) { | ||
var _oldTr = tr; | ||
const oldTr = tr; | ||
tr = replaceParentNodeOfType(parent.type, content)(tr); | ||
if (_oldTr !== tr) { | ||
var pos = isSelectableNode(content) ? // for selectable node, selection position would be the position of the replaced parent | ||
$insertPos.before($insertPos.depth) : $insertPos.pos; | ||
if (oldTr !== tr) { | ||
const pos = isSelectableNode(content) ? ( | ||
// for selectable node, selection position would be the position of the replaced parent | ||
$insertPos.before($insertPos.depth) | ||
) : $insertPos.pos; | ||
return setSelection(content, pos, tr); | ||
} | ||
} | ||
// given node is allowed at the current cursor position | ||
if (canInsert($insertPos, content)) { | ||
tr.insert($insertPos.pos, content); | ||
var _pos = hasPosition ? $insertPos.pos : isSelectableNode(content) ? // for atom nodes selection position after insertion is the previous pos | ||
tr.selection.$anchor.pos - 1 : tr.selection.$anchor.pos; | ||
return cloneTr(setSelection(content, _pos, tr)); | ||
const pos = hasPosition ? $insertPos.pos : isSelectableNode(content) ? ( | ||
// for atom nodes selection position after insertion is the previous pos | ||
tr.selection.$anchor.pos - 1 | ||
) : tr.selection.$anchor.pos; | ||
return cloneTr(setSelection(content, pos, tr)); | ||
} | ||
// looking for a place in the doc where the node is allowed | ||
for (var i = $insertPos.depth; i > 0; i--) { | ||
var _pos2 = $insertPos.after(i); | ||
var $pos = tr.doc.resolve(_pos2); | ||
for (let i = $insertPos.depth; i > 0; i--) { | ||
const pos = $insertPos.after(i); | ||
const $pos = tr.doc.resolve(pos); | ||
if (canInsert($pos, content)) { | ||
tr.insert(_pos2, content); | ||
return cloneTr(setSelection(content, _pos2, tr)); | ||
tr.insert(pos, content); | ||
return cloneTr(setSelection(content, pos, tr)); | ||
} | ||
@@ -198,37 +135,23 @@ } | ||
}; | ||
}; | ||
// :: (nodeType: union<NodeType, [NodeType]>, type: ?union<NodeType, null>, attrs: ?union<Object, null>, marks?: [Mark]) → (tr: Transaction) → Transaction | ||
// Returns a transaction that changes the type, attributes, and/or marks of the parent node of a given `nodeType`. | ||
// | ||
// ```javascript | ||
// const node = schema.nodes.extension.createChecked({}); | ||
// dispatch( | ||
// setParentNodeMarkup(schema.nodes.panel, null, { panelType })(tr); | ||
// ); | ||
// ``` | ||
var setParentNodeMarkup = function setParentNodeMarkup(nodeType, type, attrs, marks) { | ||
return function (tr) { | ||
var parent = findParentNodeOfType(nodeType)(tr.selection); | ||
var setParentNodeMarkup = (nodeType, type, attrs, marks) => (tr) => { | ||
const parent = findParentNodeOfType(nodeType)(tr.selection); | ||
if (parent) { | ||
return cloneTr(tr.setNodeMarkup(parent.pos, type, Object.assign({}, parent.node.attrs, attrs), marks)); | ||
return cloneTr( | ||
tr.setNodeMarkup( | ||
parent.pos, | ||
type, | ||
Object.assign({}, parent.node.attrs, attrs), | ||
marks | ||
) | ||
); | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: (nodeType: union<NodeType, [NodeType]>) → (tr: Transaction) → Transaction | ||
// Returns a new transaction that sets a `NodeSelection` on a parent node of a `given nodeType`. | ||
// | ||
// ```javascript | ||
// dispatch( | ||
// selectParentNodeOfType([tableCell, tableHeader])(state.tr) | ||
// ); | ||
// ``` | ||
var selectParentNodeOfType = function selectParentNodeOfType(nodeType) { | ||
return function (tr) { | ||
var selectParentNodeOfType = (nodeType) => (tr) => { | ||
if (!isNodeSelection(tr.selection)) { | ||
var parent = findParentNodeOfType(nodeType)(tr.selection); | ||
const parent = findParentNodeOfType(nodeType)(tr.selection); | ||
if (parent) { | ||
return cloneTr(tr.setSelection(prosemirrorState.NodeSelection.create(tr.doc, parent.pos))); | ||
return cloneTr( | ||
tr.setSelection(import_prosemirror_state.NodeSelection.create(tr.doc, parent.pos)) | ||
); | ||
} | ||
@@ -238,58 +161,30 @@ } | ||
}; | ||
}; | ||
var removeNodeBefore = (tr) => { | ||
const position = findPositionOfNodeBefore(tr.selection); | ||
if (typeof position === "number") { | ||
return removeNodeAtPos(position)(tr); | ||
} | ||
return tr; | ||
}; | ||
// :: (tr: Transaction) → Transaction | ||
// Returns a new transaction that deletes previous node. | ||
// | ||
// ```javascript | ||
// dispatch( | ||
// removeNodeBefore(state.tr) | ||
// ); | ||
// ``` | ||
var removeNodeBefore = function removeNodeBefore(tr) { | ||
var position = findPositionOfNodeBefore(tr.selection); | ||
if (typeof position === 'number') { | ||
return removeNodeAtPos(position)(tr); | ||
} | ||
return tr; | ||
}; | ||
// :: (selection: Selection) → boolean | ||
// Checks if current selection is a `NodeSelection`. | ||
// | ||
// ```javascript | ||
// if (isNodeSelection(tr.selection)) { | ||
// // ... | ||
// } | ||
// ``` | ||
var isNodeSelection = function isNodeSelection(selection) { | ||
return selection instanceof prosemirrorState.NodeSelection; | ||
}; | ||
// (nodeType: union<NodeType, [NodeType]>) → boolean | ||
// Checks if the type a given `node` equals to a given `nodeType`. | ||
var equalNodeType = function equalNodeType(nodeType, node) { | ||
return Array.isArray(nodeType) && nodeType.indexOf(node.type) > -1 || node.type === nodeType; | ||
}; | ||
// (tr: Transaction) → Transaction | ||
// Creates a new transaction object from a given transaction | ||
var cloneTr = function cloneTr(tr) { | ||
return Object.assign(Object.create(tr), tr).setTime(Date.now()); | ||
}; | ||
// (position: number, content: union<ProseMirrorNode, Fragment>) → (tr: Transaction) → Transaction | ||
// Returns a `replace` transaction that replaces a node at a given position with the given `content`. | ||
// It will return the original transaction if replacing is not possible. | ||
// `position` should point at the position immediately before the node. | ||
var replaceNodeAtPos = function replaceNodeAtPos(position, content) { | ||
return function (tr) { | ||
var node = tr.doc.nodeAt(position); | ||
var $pos = tr.doc.resolve(position); | ||
// src/helpers.ts | ||
var isNodeSelection = (selection) => { | ||
return selection instanceof import_prosemirror_state2.NodeSelection; | ||
}; | ||
var equalNodeType = (nodeType, node) => { | ||
return Array.isArray(nodeType) && nodeType.indexOf(node.type) > -1 || node.type === nodeType; | ||
}; | ||
var cloneTr = (tr) => { | ||
return Object.assign(Object.create(tr), tr).setTime(Date.now()); | ||
}; | ||
var replaceNodeAtPos = (position, content) => (tr) => { | ||
const node = tr.doc.nodeAt(position); | ||
const $pos = tr.doc.resolve(position); | ||
if (!node) { | ||
return tr; | ||
} | ||
if (canReplace($pos, content)) { | ||
tr = tr.replaceWith(position, position + node.nodeSize, content); | ||
var start = tr.selection.$from.pos - 1; | ||
// put cursor inside of the inserted node | ||
const start = tr.selection.$from.pos - 1; | ||
tr = setTextSelection(Math.max(start, 0), -1)(tr); | ||
// move cursor to the start of the node | ||
tr = setTextSelection(tr.selection.$from.start())(tr); | ||
@@ -300,94 +195,45 @@ return cloneTr(tr); | ||
}; | ||
}; | ||
// ($pos: ResolvedPos, doc: ProseMirrorNode, content: union<ProseMirrorNode, Fragment>, ) → boolean | ||
// Checks if replacing a node at a given `$pos` inside of the `doc` node with the given `content` is possible. | ||
var canReplace = function canReplace($pos, content) { | ||
var node = $pos.node($pos.depth); | ||
return node && node.type.validContent(content instanceof prosemirrorModel.Fragment ? content : prosemirrorModel.Fragment.from(content)); | ||
}; | ||
// (position: number) → (tr: Transaction) → Transaction | ||
// Returns a `delete` transaction that removes a node at a given position with the given `node`. | ||
// `position` should point at the position immediately before the node. | ||
var removeNodeAtPos = function removeNodeAtPos(position) { | ||
return function (tr) { | ||
var node = tr.doc.nodeAt(position); | ||
var canReplace = ($pos, content) => { | ||
const node = $pos.node($pos.depth); | ||
return node && node.type.validContent( | ||
content instanceof import_prosemirror_model2.Fragment ? content : import_prosemirror_model2.Fragment.from(content) | ||
); | ||
}; | ||
var removeNodeAtPos = (position) => (tr) => { | ||
const node = tr.doc.nodeAt(position); | ||
if (!node) { | ||
return tr; | ||
} | ||
return cloneTr(tr.delete(position, position + node.nodeSize)); | ||
}; | ||
}; | ||
// :: ($pos: ResolvedPos, content: union<ProseMirrorNode, Fragment>) → boolean | ||
// Checks if a given `content` can be inserted at the given `$pos` | ||
// | ||
// ```javascript | ||
// const { selection: { $from } } = state; | ||
// const node = state.schema.nodes.atom.createChecked(); | ||
// if (canInsert($from, node)) { | ||
// // ... | ||
// } | ||
// ``` | ||
var canInsert = function canInsert($pos, content) { | ||
var index = $pos.index(); | ||
if (content instanceof prosemirrorModel.Fragment) { | ||
return $pos.parent.canReplace(index, index, content); | ||
} else if (content instanceof prosemirrorModel.Node) { | ||
return $pos.parent.canReplaceWith(index, index, content.type); | ||
} | ||
return false; | ||
}; | ||
// (node: ProseMirrorNode) → boolean | ||
// Checks if a given `node` is an empty paragraph | ||
var isEmptyParagraph = function isEmptyParagraph(node) { | ||
return !node || node.type.name === 'paragraph' && node.nodeSize === 2; | ||
}; | ||
// :: (predicate: (node: ProseMirrorNode) → boolean) → (selection: Selection) → ?{pos: number, start: number, depth: number, node: ProseMirrorNode} | ||
// Iterates over parent nodes, returning the closest node and its start position `predicate` returns truthy for. `start` points to the start position of the node, `pos` points directly before the node. | ||
// | ||
// ```javascript | ||
// const predicate = node => node.type === schema.nodes.blockquote; | ||
// const parent = findParentNode(predicate)(selection); | ||
// ``` | ||
var findParentNode = function findParentNode(predicate) { | ||
return function (_ref) { | ||
var $from = _ref.$from; | ||
return findParentNodeClosestToPos($from, predicate); | ||
var canInsert = ($pos, content) => { | ||
const index = $pos.index(); | ||
if (content instanceof import_prosemirror_model2.Fragment) { | ||
return $pos.parent.canReplace(index, index, content); | ||
} else if (content instanceof import_prosemirror_model2.Node) { | ||
return $pos.parent.canReplaceWith(index, index, content.type); | ||
} | ||
return false; | ||
}; | ||
}; | ||
var isEmptyParagraph = (node) => { | ||
return !node || node.type.name === "paragraph" && node.nodeSize === 2; | ||
}; | ||
// :: ($pos: ResolvedPos, predicate: (node: ProseMirrorNode) → boolean) → ?{pos: number, start: number, depth: number, node: ProseMirrorNode} | ||
// Iterates over parent nodes starting from the given `$pos`, returning the closest node and its start position `predicate` returns truthy for. `start` points to the start position of the node, `pos` points directly before the node. | ||
// | ||
// ```javascript | ||
// const predicate = node => node.type === schema.nodes.blockquote; | ||
// const parent = findParentNodeClosestToPos(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: i > 0 ? $pos.before(i) : 0, | ||
start: $pos.start(i), | ||
depth: i, | ||
node: node | ||
}; | ||
// src/selection.ts | ||
var findParentNode = (predicate) => ({ $from }) => findParentNodeClosestToPos($from, predicate); | ||
var findParentNodeClosestToPos = ($pos, predicate) => { | ||
for (let i = $pos.depth; i > 0; i--) { | ||
const node = $pos.node(i); | ||
if (predicate(node)) { | ||
return { | ||
pos: i > 0 ? $pos.before(i) : 0, | ||
start: $pos.start(i), | ||
depth: i, | ||
node | ||
}; | ||
} | ||
} | ||
} | ||
}; | ||
// :: (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 closest node `predicate` returns truthy for. | ||
// | ||
// ```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) { | ||
return function (selection) { | ||
var parent = findParentNode(predicate)(selection); | ||
}; | ||
var findParentDomRef = (predicate, domAtPos) => (selection) => { | ||
const parent = findParentNode(predicate)(selection); | ||
if (parent) { | ||
@@ -397,295 +243,107 @@ return findDomRefAtPos(parent.pos, domAtPos); | ||
}; | ||
}; | ||
// :: (predicate: (node: ProseMirrorNode) → boolean) → (selection: Selection) → boolean | ||
// Checks if there's a parent node `predicate` returns truthy for. | ||
// | ||
// ```javascript | ||
// if (hasParentNode(node => node.type === schema.nodes.table)(selection)) { | ||
// // .... | ||
// } | ||
// ``` | ||
var hasParentNode = function hasParentNode(predicate) { | ||
return function (selection) { | ||
var hasParentNode = (predicate) => (selection) => { | ||
return !!findParentNode(predicate)(selection); | ||
}; | ||
}; | ||
// :: (nodeType: union<NodeType, [NodeType]>) → (selection: Selection) → ?{pos: number, start: number, depth: number, node: ProseMirrorNode} | ||
// Iterates over parent nodes, returning closest node of a given `nodeType`. `start` points to the start position of the node, `pos` points directly before the node. | ||
// | ||
// ```javascript | ||
// const parent = findParentNodeOfType(schema.nodes.paragraph)(selection); | ||
// ``` | ||
var findParentNodeOfType = function findParentNodeOfType(nodeType) { | ||
return function (selection) { | ||
return findParentNode(function (node) { | ||
return equalNodeType(nodeType, node); | ||
})(selection); | ||
var findParentNodeOfType = (nodeType) => (selection) => { | ||
return findParentNode((node) => equalNodeType(nodeType, node))(selection); | ||
}; | ||
}; | ||
// :: ($pos: ResolvedPos, nodeType: union<NodeType, [NodeType]>) → ?{pos: number, start: number, depth: number, node: ProseMirrorNode} | ||
// Iterates over parent nodes starting from the given `$pos`, returning closest node of a given `nodeType`. `start` points to the start position of the node, `pos` points directly before the node. | ||
// | ||
// ```javascript | ||
// const parent = findParentNodeOfTypeClosestToPos(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 | ||
// Checks if there's a parent node of a given `nodeType`. | ||
// | ||
// ```javascript | ||
// if (hasParentNodeOfType(schema.nodes.table)(selection)) { | ||
// // .... | ||
// } | ||
// ``` | ||
var hasParentNodeOfType = function hasParentNodeOfType(nodeType) { | ||
return function (selection) { | ||
return hasParentNode(function (node) { | ||
return equalNodeType(nodeType, node); | ||
})(selection); | ||
var findParentNodeOfTypeClosestToPos = ($pos, nodeType) => { | ||
return findParentNodeClosestToPos( | ||
$pos, | ||
(node) => equalNodeType(nodeType, node) | ||
); | ||
}; | ||
}; | ||
// :: (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 closest node of a given `nodeType`. | ||
// | ||
// ```javascript | ||
// const domAtPos = view.domAtPos.bind(view); | ||
// const parent = findParentDomRefOfType(schema.nodes.codeBlock, domAtPos)(selection); // <pre> | ||
// ``` | ||
var findParentDomRefOfType = function findParentDomRefOfType(nodeType, domAtPos) { | ||
return function (selection) { | ||
return findParentDomRef(function (node) { | ||
return equalNodeType(nodeType, node); | ||
}, domAtPos)(selection); | ||
var hasParentNodeOfType = (nodeType) => (selection) => { | ||
return hasParentNode((node) => equalNodeType(nodeType, node))(selection); | ||
}; | ||
}; | ||
// :: (nodeType: union<NodeType, [NodeType]>) → (selection: Selection) → ?{pos: number, start: number, depth: number, node: ProseMirrorNode} | ||
// Returns a node of a given `nodeType` if it is selected. `start` points to the start position of the node, `pos` points directly before the node. | ||
// | ||
// ```javascript | ||
// const { extension, inlineExtension, bodiedExtension } = schema.nodes; | ||
// const selectedNode = findSelectedNodeOfType([ | ||
// extension, | ||
// inlineExtension, | ||
// bodiedExtension, | ||
// ])(selection); | ||
// ``` | ||
var findSelectedNodeOfType = function findSelectedNodeOfType(nodeType) { | ||
return function (selection) { | ||
var findParentDomRefOfType = (nodeType, domAtPos) => (selection) => { | ||
return findParentDomRef( | ||
(node) => equalNodeType(nodeType, node), | ||
domAtPos | ||
)(selection); | ||
}; | ||
var findSelectedNodeOfType = (nodeType) => (selection) => { | ||
if (isNodeSelection(selection)) { | ||
var node = selection.node, | ||
$from = selection.$from; | ||
const { node, $from } = selection; | ||
if (equalNodeType(nodeType, node)) { | ||
return { node: node, pos: $from.pos, depth: $from.depth }; | ||
return { | ||
node, | ||
start: $from.start(), | ||
pos: $from.pos, | ||
depth: $from.depth | ||
}; | ||
} | ||
} | ||
}; | ||
}; | ||
// :: (selection: Selection) → ?number | ||
// Returns position of the previous node. | ||
// | ||
// ```javascript | ||
// const pos = findPositionOfNodeBefore(tr.selection); | ||
// ``` | ||
var findPositionOfNodeBefore = function findPositionOfNodeBefore(selection) { | ||
var nodeBefore = selection.$from.nodeBefore; | ||
var maybeSelection = prosemirrorState.Selection.findFrom(selection.$from, -1); | ||
if (maybeSelection && nodeBefore) { | ||
// leaf node | ||
var parent = findParentNodeOfType(nodeBefore.type)(maybeSelection); | ||
if (parent) { | ||
return parent.pos; | ||
var findPositionOfNodeBefore = (selection) => { | ||
const { nodeBefore } = selection.$from; | ||
const maybeSelection = import_prosemirror_state3.Selection.findFrom(selection.$from, -1); | ||
if (maybeSelection && nodeBefore) { | ||
const parent = findParentNodeOfType(nodeBefore.type)(maybeSelection); | ||
if (parent) { | ||
return parent.pos; | ||
} | ||
return maybeSelection.$from.pos; | ||
} | ||
return maybeSelection.$from.pos; | ||
} | ||
}; | ||
}; | ||
var findDomRefAtPos = (position, domAtPos) => { | ||
const dom = domAtPos(position); | ||
const node = dom.node.childNodes[dom.offset]; | ||
if (dom.node.nodeType === Node.TEXT_NODE && dom.node.parentNode) { | ||
return dom.node.parentNode; | ||
} | ||
if (!node || node.nodeType === Node.TEXT_NODE) { | ||
return dom.node; | ||
} | ||
return node; | ||
}; | ||
// :: (position: number, domAtPos: (pos: number) → {node: dom.Node, offset: number}) → dom.Node | ||
// Returns DOM reference of a node at a given `position`. If the node type is of type `TEXT_NODE` it will return the reference of the parent node. | ||
// | ||
// ```javascript | ||
// const domAtPos = view.domAtPos.bind(view); | ||
// const ref = findDomRefAtPos($from.pos, domAtPos); | ||
// ``` | ||
var findDomRefAtPos = function findDomRefAtPos(position, domAtPos) { | ||
var dom = domAtPos(position); | ||
var node = dom.node.childNodes[dom.offset]; | ||
if (dom.node.nodeType === Node.TEXT_NODE) { | ||
return dom.node.parentNode; | ||
} | ||
if (!node || node.nodeType === Node.TEXT_NODE) { | ||
return dom.node; | ||
} | ||
return node; | ||
}; | ||
// :: (node: ProseMirrorNode, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }] | ||
// Flattens descendants of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`). | ||
// | ||
// ```javascript | ||
// const children = flatten(node); | ||
// ``` | ||
var flatten = function flatten(node) { | ||
var descend = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true; | ||
if (!node) { | ||
throw new Error('Invalid "node" parameter'); | ||
} | ||
var result = []; | ||
node.descendants(function (child, pos) { | ||
result.push({ node: child, pos: pos }); | ||
if (!descend) { | ||
return false; | ||
// src/node.ts | ||
var flatten = (node, descend = true) => { | ||
if (!node) { | ||
throw new Error('Invalid "node" parameter'); | ||
} | ||
}); | ||
return result; | ||
}; | ||
// :: (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. It doesn't descend into a node when descend argument is `false` (defaults to `true`). | ||
// | ||
// ```javascript | ||
// const textNodes = findChildren(node, child => child.isText, false); | ||
// ``` | ||
var findChildren = function findChildren(node, predicate, descend) { | ||
if (!node) { | ||
throw new Error('Invalid "node" parameter'); | ||
} else if (!predicate) { | ||
throw new Error('Invalid "predicate" parameter'); | ||
} | ||
return flatten(node, descend).filter(function (child) { | ||
return predicate(child.node); | ||
}); | ||
}; | ||
// :: (node: ProseMirrorNode, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }] | ||
// Returns text nodes of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`). | ||
// | ||
// ```javascript | ||
// const textNodes = findTextNodes(node); | ||
// ``` | ||
var findTextNodes = function findTextNodes(node, descend) { | ||
return findChildren(node, function (child) { | ||
return child.isText; | ||
}, descend); | ||
}; | ||
// :: (node: ProseMirrorNode, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }] | ||
// Returns inline nodes of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`). | ||
// | ||
// ```javascript | ||
// const inlineNodes = findInlineNodes(node); | ||
// ``` | ||
var findInlineNodes = function findInlineNodes(node, descend) { | ||
return findChildren(node, function (child) { | ||
return child.isInline; | ||
}, descend); | ||
}; | ||
// :: (node: ProseMirrorNode, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }] | ||
// Returns block descendants of a given `node`. It doesn't descend into a node when descend argument is `false` (defaults to `true`). | ||
// | ||
// ```javascript | ||
// const blockNodes = findBlockNodes(node); | ||
// ``` | ||
var findBlockNodes = function findBlockNodes(node, descend) { | ||
return findChildren(node, function (child) { | ||
return child.isBlock; | ||
}, descend); | ||
}; | ||
// :: (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. It doesn't descend into a node when descend argument is `false` (defaults to `true`). | ||
// | ||
// ```javascript | ||
// const mergedCells = findChildrenByAttr(table, attrs => attrs.colspan === 2); | ||
// ``` | ||
var findChildrenByAttr = function findChildrenByAttr(node, predicate, descend) { | ||
return findChildren(node, function (child) { | ||
return !!predicate(child.attrs); | ||
}, descend); | ||
}; | ||
// :: (node: ProseMirrorNode, nodeType: NodeType, descend: ?boolean) → [{ node: ProseMirrorNode, pos: number }] | ||
// 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`). | ||
// | ||
// ```javascript | ||
// const cells = findChildrenByType(table, schema.nodes.tableCell); | ||
// ``` | ||
var findChildrenByType = function findChildrenByType(node, nodeType, descend) { | ||
return findChildren(node, function (child) { | ||
return child.type === nodeType; | ||
}, descend); | ||
}; | ||
// :: (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. It doesn't descend into a `node` when descend argument is `false` (defaults to `true`). | ||
// | ||
// ```javascript | ||
// const nodes = findChildrenByMark(state.doc, schema.marks.strong); | ||
// ``` | ||
var findChildrenByMark = function findChildrenByMark(node, markType, descend) { | ||
return findChildren(node, function (child) { | ||
return markType.isInSet(child.marks); | ||
}, descend); | ||
}; | ||
// :: (node: ProseMirrorNode, nodeType: NodeType) → boolean | ||
// Returns `true` if a given node contains nodes of a given `nodeType` | ||
// | ||
// ```javascript | ||
// if (contains(panel, schema.nodes.listItem)) { | ||
// // ... | ||
// } | ||
// ``` | ||
var contains = function contains(node, nodeType) { | ||
return !!findChildrenByType(node, nodeType).length; | ||
}; | ||
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; | ||
exports.findParentDomRefOfType = findParentDomRefOfType; | ||
exports.findSelectedNodeOfType = findSelectedNodeOfType; | ||
exports.findPositionOfNodeBefore = findPositionOfNodeBefore; | ||
exports.findDomRefAtPos = findDomRefAtPos; | ||
exports.flatten = flatten; | ||
exports.findChildren = findChildren; | ||
exports.findTextNodes = findTextNodes; | ||
exports.findInlineNodes = findInlineNodes; | ||
exports.findBlockNodes = findBlockNodes; | ||
exports.findChildrenByAttr = findChildrenByAttr; | ||
exports.findChildrenByType = findChildrenByType; | ||
exports.findChildrenByMark = findChildrenByMark; | ||
exports.contains = contains; | ||
exports.removeParentNodeOfType = removeParentNodeOfType; | ||
exports.replaceParentNodeOfType = replaceParentNodeOfType; | ||
exports.removeSelectedNode = removeSelectedNode; | ||
exports.replaceSelectedNode = replaceSelectedNode; | ||
exports.setTextSelection = setTextSelection; | ||
exports.safeInsert = safeInsert; | ||
exports.setParentNodeMarkup = setParentNodeMarkup; | ||
exports.selectParentNodeOfType = selectParentNodeOfType; | ||
exports.removeNodeBefore = removeNodeBefore; | ||
const result = []; | ||
node.descendants((child, pos) => { | ||
result.push({ node: child, pos }); | ||
if (!descend) { | ||
return false; | ||
} | ||
}); | ||
return result; | ||
}; | ||
var findChildren = (node, predicate, descend = true) => { | ||
if (!node) { | ||
throw new Error('Invalid "node" parameter'); | ||
} else if (!predicate) { | ||
throw new Error('Invalid "predicate" parameter'); | ||
} | ||
return flatten(node, descend).filter((child) => predicate(child.node)); | ||
}; | ||
var findTextNodes = (node, descend = true) => { | ||
return findChildren(node, (child) => child.isText, descend); | ||
}; | ||
var findInlineNodes = (node, descend = true) => { | ||
return findChildren(node, (child) => child.isInline, descend); | ||
}; | ||
var findBlockNodes = (node, descend = true) => { | ||
return findChildren(node, (child) => child.isBlock, descend); | ||
}; | ||
var findChildrenByAttr = (node, predicate, descend = true) => { | ||
return findChildren(node, (child) => !!predicate(child.attrs), descend); | ||
}; | ||
var findChildrenByType = (node, nodeType, descend = true) => { | ||
return findChildren(node, (child) => child.type === nodeType, descend); | ||
}; | ||
var findChildrenByMark = (node, markType, descend = true) => { | ||
return findChildren( | ||
node, | ||
(child) => Boolean(markType.isInSet(child.marks)), | ||
descend | ||
); | ||
}; | ||
var contains = (node, nodeType) => { | ||
return !!findChildrenByType(node, nodeType).length; | ||
}; | ||
})(); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "prosemirror-utils", | ||
"version": "1.0.0-0", | ||
"version": "1.1.1", | ||
"description": "Utils library for ProseMirror", | ||
"main": "dist/index.js", | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"type": "module", | ||
"author": { | ||
@@ -11,2 +14,8 @@ "name": "Eduard Shvedai", | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
} | ||
}, | ||
"maintainers": [ | ||
@@ -16,2 +25,6 @@ { | ||
"email": "eshvedai@atlassian.com" | ||
}, | ||
{ | ||
"name": "Rodrigo Vieira", | ||
"email": "rvieira@atlassian.com" | ||
} | ||
@@ -24,24 +37,14 @@ ], | ||
}, | ||
"keywords": [ | ||
"ProseMirror", | ||
"utils", | ||
"helpers" | ||
], | ||
"keywords": ["ProseMirror", "utils", "helpers"], | ||
"jest": { | ||
"transform": { | ||
"^.+\\.js$": "babel-jest" | ||
}, | ||
"setupTestFrameworkScriptFile": "./jestFrameworkSetup.js", | ||
"testURL": "http://localhost/" | ||
"preset": "ts-jest", | ||
"setupFilesAfterEnv": ["./jestFrameworkSetup.ts"], | ||
"testEnvironment": "jsdom" | ||
}, | ||
"typings": "typings.d.ts", | ||
"files": [ | ||
"dist", | ||
"typings.d.ts" | ||
], | ||
"files": ["dist"], | ||
"scripts": { | ||
"build": "NODE_ENV=production rollup -c", | ||
"build_readme": "builddocs --name utils --format markdown --main src/README.md src/*.js > README.md", | ||
"build_all": "npm run build && npm run build_readme", | ||
"test": "NODE_ENV=testing jest", | ||
"lint": "eslint ./src/ --ext .ts --fix", | ||
"build": "./build.js", | ||
"build_all": "npm run build", | ||
"test": "jest ./__tests__/*.ts", | ||
"test-ci": "NODE_ENV=testing jest --coverage && codecov", | ||
@@ -52,29 +55,29 @@ "prepare": "npm run build_all", | ||
"peerDependencies": { | ||
"prosemirror-model": "^1.0.0", | ||
"prosemirror-state": "^1.0.1" | ||
"prosemirror-model": "^1.19.2", | ||
"prosemirror-state": "^1.4.3" | ||
}, | ||
"devDependencies": { | ||
"babel-core": "^6.26.3", | ||
"babel-jest": "^23.6.0", | ||
"babel-preset-env": "^1.7.0", | ||
"builddocs": "^0.3.2", | ||
"@types/jest": "^29.5.3", | ||
"@typescript-eslint/eslint-plugin": "^6.0.0", | ||
"@typescript-eslint/parser": "^6.0.0", | ||
"codecov": "^3.1.0", | ||
"esbuild": "^0.18.12", | ||
"eslint": "^8.44.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"husky": "^1.3.0", | ||
"jest": "^23.6.0", | ||
"jest-diff": "^23.6.0", | ||
"lint-staged": "^8.1.0", | ||
"prettier": "^1.15.3", | ||
"prosemirror-model": "^1.0.0", | ||
"prosemirror-schema-basic": "^1.0.0", | ||
"prosemirror-state": "^1.0.1", | ||
"prosemirror-test-builder": "^1.0.1", | ||
"jest": "^29.6.1", | ||
"jest-environment-jsdom": "^29.6.1", | ||
"lint-staged": "^13.2.3", | ||
"prettier": "^2.8.8", | ||
"prosemirror-model": "1.19.2", | ||
"prosemirror-schema-basic": "^1.2.2", | ||
"prosemirror-state": "^1.4.3", | ||
"prosemirror-test-builder": "^1.1.1", | ||
"prosemirror-transform": "^1.7.3", | ||
"prosemirror-view": "^1.1.1", | ||
"rollup": "^0.56.3", | ||
"rollup-plugin-babel": "^3.0.3" | ||
"ts-jest": "^29.1.1", | ||
"typescript": "^5.1.6" | ||
}, | ||
"lint-staged": { | ||
"*.{js, md}": [ | ||
"prettier --write", | ||
"git add" | ||
] | ||
"*.{js, md}": ["prettier --write", "git add"] | ||
}, | ||
@@ -81,0 +84,0 @@ "prettier": { |
@@ -11,2 +11,17 @@ # Utils library for ProseMirror | ||
## How to | ||
### Test | ||
```sh | ||
npm run test | ||
``` | ||
### Build | ||
```sh | ||
npm run build_all | ||
``` | ||
## Quick Start | ||
@@ -13,0 +28,0 @@ |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
711
1
346
Yes
74355
20
6
3