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.16 to 0.2.17

417

dist/index.js

@@ -9,2 +9,213 @@ 'use strict';

// :: (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);
if (parent) {
return removeNodeAtPos(parent.pos)(tr);
}
return 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) {
if (!Array.isArray(nodeType)) {
nodeType = [nodeType];
}
for (var i = 0, count = nodeType.length; i < count; i++) {
var parent = findParentNodeOfType(nodeType[i])(tr.selection);
if (parent) {
var newTr = replaceNodeAtPos(parent.pos, content)(tr);
if (newTr !== tr) {
return newTr;
}
}
}
return tr;
};
};
// :: (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;
};
// :: (node: ProseMirrorNode) → (tr: Transaction) → Transaction
// Returns a new transaction that replaces selected node with a given `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(node) {
return function (tr) {
if (isNodeSelection(tr.selection)) {
var _tr$selection = tr.selection,
$from = _tr$selection.$from,
$to = _tr$selection.$to;
if ($from.parent.canReplaceWith($from.index(), $from.indexAfter(), node.type)) {
return cloneTr(tr.replaceWith($from.pos, $to.pos, node));
}
}
return tr;
};
};
// :: (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);
if (nextSelection) {
return tr.setSelection(nextSelection);
}
return tr;
};
};
// :: (content: union<ProseMirrorNode, Fragment>, position: ?number) → (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 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) {
return function (tr) {
var hasPosition = typeof position === 'number';
var $from = hasPosition ? tr.doc.resolve(position) : tr.selection.$from;
var parent = $from.parent,
depth = $from.depth;
// try to replace an empty paragraph
if (isEmptyParagraph(parent)) {
var oldTr = tr;
tr = replaceParentNodeOfType(parent.type, content)(tr);
if (oldTr !== tr) {
return setTextSelection($from.pos)(tr);
}
}
// given node is allowed at the current cursor position
if (canInsert($from, content)) {
tr.insert($from.pos, content);
return cloneTr(setTextSelection(hasPosition ? $from.pos : tr.selection.$anchor.pos)(tr));
}
// looking for a place in the doc where the node is allowed
for (var i = $from.depth; i > 0; i--) {
var pos = $from.after(i);
var $pos = tr.doc.resolve(pos);
if (canInsert($pos, content)) {
tr.insert(pos, content);
return cloneTr(setTextSelection(tr.mapping.map(pos), -1)(tr));
}
}
return tr;
};
};
// :: (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(
// safeInsert(node)(tr)
// );
// ```
var setParentNodeMarkup = function setParentNodeMarkup(nodeType, type, attrs, marks) {
return function (tr) {
var parent = findParentNodeOfType(nodeType)(tr.selection);
if (parent) {
return cloneTr(tr.setNodeMarkup(parent.pos - 1, 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) {
if (!isNodeSelection(tr.selection)) {
var parent = findParentNodeOfType(nodeType)(tr.selection);
if (parent) {
return cloneTr(tr.setSelection(prosemirrorState.NodeSelection.create(tr.doc, parent.pos - 1)));
}
}
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

@@ -44,3 +255,4 @@ // Checks if current selection is a `NodeSelection`.

if (canReplace($pos, content)) {
return cloneTr(tr.replaceWith(before, before + node.nodeSize, content));
tr = tr.replaceWith(before, before + node.nodeSize, content);
return cloneTr(setTextSelection(tr.selection.$from.pos - 1, -1)(tr));
}

@@ -821,205 +1033,2 @@ return tr;

// :: (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);
if (parent) {
return removeNodeAtPos(parent.pos)(tr);
}
return 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 parent = findParentNodeOfType(nodeType)(tr.selection);
if (parent) {
return replaceNodeAtPos(parent.pos, content)(tr);
}
return tr;
};
};
// :: (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;
};
// :: (node: ProseMirrorNode) → (tr: Transaction) → Transaction
// Returns a new transaction that replaces selected node with a given `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(node) {
return function (tr) {
if (isNodeSelection(tr.selection)) {
var _tr$selection = tr.selection,
$from = _tr$selection.$from,
$to = _tr$selection.$to;
if ($from.parent.canReplaceWith($from.index(), $from.indexAfter(), node.type)) {
return cloneTr(tr.replaceWith($from.pos, $to.pos, node));
}
}
return tr;
};
};
// :: (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);
if (nextSelection) {
return tr.setSelection(nextSelection);
}
return tr;
};
};
// :: (content: union<ProseMirrorNode, Fragment>, position: ?number) → (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 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) {
return function (tr) {
var hasPosition = typeof position === 'number';
var $from = hasPosition ? tr.doc.resolve(position) : tr.selection.$from;
var parent = $from.parent,
depth = $from.depth;
// try to replace an empty paragraph
if (isEmptyParagraph(parent)) {
var oldTr = tr;
tr = replaceParentNodeOfType(parent.type, content)(tr);
if (oldTr !== tr) {
return setTextSelection($from.pos)(tr);
}
}
// given node is allowed at the current cursor position
if (canInsert($from, content)) {
tr.insert($from.pos, content);
return cloneTr(setTextSelection(hasPosition ? $from.pos : tr.selection.$anchor.pos)(tr));
}
// looking for a place in the doc where the node is allowed
for (var i = $from.depth; i > 0; i--) {
var pos = $from.after(i);
var $pos = tr.doc.resolve(pos);
if (canInsert($pos, content)) {
tr.insert(pos, content);
return cloneTr(setTextSelection(tr.mapping.map(pos), -1)(tr));
}
}
return tr;
};
};
// :: (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(
// safeInsert(node)(tr)
// );
// ```
var setParentNodeMarkup = function setParentNodeMarkup(nodeType, type, attrs, marks) {
return function (tr) {
var parent = findParentNodeOfType(nodeType)(tr.selection);
if (parent) {
return cloneTr(tr.setNodeMarkup(parent.pos - 1, 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) {
if (!isNodeSelection(tr.selection)) {
var parent = findParentNodeOfType(nodeType)(tr.selection);
if (parent) {
return cloneTr(tr.setSelection(prosemirrorState.NodeSelection.create(tr.doc, parent.pos - 1)));
}
}
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;
};
exports.isNodeSelection = isNodeSelection;

@@ -1026,0 +1035,0 @@ exports.canInsert = canInsert;

{
"name": "prosemirror-utils",
"version": "0.2.16",
"version": "0.2.17",
"description": "Utils library for ProseMirror",

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

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