prosemirror-utils
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -283,4 +283,11 @@ 'use strict'; | ||
// (position: number, node: ProseMirrorNode) → (tr: Transaction) → ?Transaction | ||
// (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, node: ProseMirrorNode) → (tr: Transaction) → Transaction | ||
// Returns a `delete` transaction that removes a node at a given position with the given `node`. | ||
// It will return the original transaction if replacing is not possible. | ||
var replaceNodeAtPos = function replaceNodeAtPos(position, node) { | ||
@@ -292,8 +299,9 @@ return function (tr) { | ||
if (tr.doc.canReplaceWith($pos.index($pos.depth), $pos.indexAfter($pos.depth), node.type)) { | ||
return tr.replaceWith(from, to, node); | ||
return cloneTr(tr.replaceWith(from, to, node)); | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// (position: number, node: ProseMirrorNode) → (tr: Transaction) → ?Transaction | ||
// (position: number, node: ProseMirrorNode) → (tr: Transaction) → Transaction | ||
// Returns a `delete` transaction that removes a node at a given position with the given `node`. | ||
@@ -305,8 +313,9 @@ var removeNodeAtPos = function removeNodeAtPos(position, node) { | ||
var to = $pos.after($pos.depth); | ||
return tr.delete(from, to); | ||
return cloneTr(tr.delete(from, to)); | ||
}; | ||
}; | ||
// :: (nodeType: NodeType) → (tr: Transaction) → ?Transaction | ||
// :: (nodeType: NodeType) → (tr: Transaction) → Transaction | ||
// Returns a `replace` transaction that replaces a node of a given `nodeType` with the given `node`. | ||
// It will return the original transaction if parent node hasn't been found. | ||
var removeParentNodeOfType = function removeParentNodeOfType(nodeType) { | ||
@@ -318,7 +327,9 @@ return function (tr) { | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: (nodeType: NodeType, node: ProseMirrorNode) → (tr: Transaction) → ?Transaction | ||
// :: (nodeType: NodeType, node: ProseMirrorNode) → (tr: Transaction) → Transaction | ||
// Returns a `replace` transaction that replaces parent node of a given `nodeType` with the given `node`. | ||
// It will return the original transaction if parent node hasn't been found. | ||
var replaceParentNodeOfType = function replaceParentNodeOfType(nodeType, node) { | ||
@@ -330,7 +341,9 @@ return function (tr) { | ||
} | ||
return tr; | ||
}; | ||
}; | ||
// :: (tr: Transaction) → ?Transaction | ||
// :: (tr: Transaction) → Transaction | ||
// Returns a `delete` transaction that removes selected node. | ||
// It will return the original transaction if current selection is not a NodeSelection | ||
var removeSelectedNode = function removeSelectedNode(tr) { | ||
@@ -341,8 +354,10 @@ // NodeSelection | ||
var to = tr.curSelection.$to.pos; | ||
return tr.delete(from, to); | ||
return cloneTr(tr.delete(from, to)); | ||
} | ||
return tr; | ||
}; | ||
// :: (node: ProseMirrorNode) → (tr: Transaction) → ?Transaction | ||
// :: (node: ProseMirrorNode) → (tr: Transaction) → Transaction | ||
// Returns an `insert` transaction that inserts a given `node` at the current cursor position if it is allowed by schema. If schema restricts such nesting, it will try to find the appropriate place for the given `node` in the document, looping through parent nodes up until the root document node. | ||
// It will return the original transaction if the place for insertion hasn't been found. | ||
var safeInsert = function safeInsert(node) { | ||
@@ -356,3 +371,3 @@ return function (tr) { | ||
if ($from.parent.canReplaceWith(index, index, node.type)) { | ||
return tr.insert($from.pos, node); | ||
return cloneTr(tr.insert($from.pos, node)); | ||
} | ||
@@ -366,5 +381,6 @@ | ||
if ($pos.parent.canReplaceWith(_index, _index, node.type)) { | ||
return tr.insert(pos, node); | ||
return cloneTr(tr.insert(pos, node)); | ||
} | ||
} | ||
return tr; | ||
}; | ||
@@ -371,0 +387,0 @@ }; |
{ | ||
"name": "prosemirror-utils", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Utils library for ProseMirror", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -91,13 +91,17 @@ # Utils library for ProseMirror | ||
* **`removeParentNodeOfType`**`(nodeType: NodeType) → fn(tr: Transaction) → ?Transaction`\ | ||
* **`removeParentNodeOfType`**`(nodeType: NodeType) → fn(tr: Transaction) → Transaction`\ | ||
Returns a `replace` transaction that replaces a node of a given `nodeType` with the given `node`. | ||
It will return the original transaction if parent node hasn't been found. | ||
- **`replaceParentNodeOfType`**`(nodeType: NodeType, node: ProseMirrorNode) → fn(tr: Transaction) → ?Transaction`\ | ||
- **`replaceParentNodeOfType`**`(nodeType: NodeType, node: ProseMirrorNode) → fn(tr: Transaction) → Transaction`\ | ||
Returns a `replace` transaction that replaces parent node of a given `nodeType` with the given `node`. | ||
It will return the original transaction if parent node hasn't been found. | ||
* **`removeSelectedNode`**`(tr: Transaction) → ?Transaction`\ | ||
* **`removeSelectedNode`**`(tr: Transaction) → Transaction`\ | ||
Returns a `delete` transaction that removes selected node. | ||
It will return the original transaction if current selection is not a NodeSelection | ||
- **`safeInsert`**`(node: ProseMirrorNode) → fn(tr: Transaction) → ?Transaction`\ | ||
- **`safeInsert`**`(node: ProseMirrorNode) → fn(tr: Transaction) → Transaction`\ | ||
Returns an `insert` transaction that inserts a given `node` at the current cursor position if it is allowed by schema. If schema restricts such nesting, it will try to find the appropriate place for the given `node` in the document, looping through parent nodes up until the root document node. | ||
It will return the original transaction if the place for insertion hasn't been found. | ||
@@ -104,0 +108,0 @@ ## License |
@@ -8,16 +8,14 @@ import { Node as ProsemirrorNode, NodeType, MarkType } from 'prosemirror-model'; | ||
export type NodeWithPos = {node: Node, pos: number}; | ||
// ancestors | ||
export function findParentNode(predicate: Predicate): (selection: Selection) => {pos: number, node: ProsemirrorNode} | void; | ||
export function findParentNode(predicate: Predicate): (selection: Selection) => {pos: number, node: ProsemirrorNode} | undefined; | ||
export function findParentDomRef(predicate: Predicate, domAtPos: DomAtPos): (selection: Selection) => Node | void; | ||
export function findParentDomRef(predicate: Predicate, domAtPos: DomAtPos): (selection: Selection) => Node | undefined; | ||
export function hasParentNode(predicate: Predicate): (selection: Selection) => boolean; | ||
export function findParentNodeOfType(nodeType: NodeType): (selection: Selection) => {pos: number, node: ProsemirrorNode} | void; | ||
export function findParentNodeOfType(nodeType: NodeType): (selection: Selection) => {pos: number, node: ProsemirrorNode} | undefined; | ||
export function hasParentNodeOfType(nodeType: NodeType): (selection: Selection) => boolean; | ||
export function findParentDomRefOfType(nodeType: NodeType, domAtPos: DomAtPos): (selection: Selection) => Node | void; | ||
export function findParentDomRefOfType(nodeType: NodeType, domAtPos: DomAtPos): (selection: Selection) => Node | undefined; | ||
@@ -44,3 +42,3 @@ // descendants | ||
// table | ||
export function findTable(selection: Selection): {pos: number, node: ProsemirrorNode} | void; | ||
export function findTable(selection: Selection): {pos: number, node: ProsemirrorNode} | undefined; | ||
@@ -55,15 +53,15 @@ export function isCellSelection(selection: Selection): boolean; | ||
export function getCellsInColumn(columnIndex: number): (selection: Selection) => {pos: number, node: ProsemirrorNode}[] | void; | ||
export function getCellsInColumn(columnIndex: number): (selection: Selection) => {pos: number, node: ProsemirrorNode}[] | undefined; | ||
export function getCellsInRow(rowIndex: number): (selection: Selection) => {pos: number, node: ProsemirrorNode}[] | void; | ||
export function getCellsInRow(rowIndex: number): (selection: Selection) => {pos: number, node: ProsemirrorNode}[] | undefined; | ||
export function getCellsInTable(selection: Selection): {pos: number, node: ProsemirrorNode}[] | void; | ||
export function getCellsInTable(selection: Selection): {pos: number, node: ProsemirrorNode}[] | undefined; | ||
// Transforms | ||
export function removeParentNodeOfType(nodeType: NodeType): (tr: Transaction) => Transaction | void; | ||
export function removeParentNodeOfType(nodeType: NodeType): (tr: Transaction) => Transaction; | ||
export function replaceParentNodeOfType(nodeType: NodeType, node: ProsemirrorNode): (tr: Transaction) => Transaction | void; | ||
export function replaceParentNodeOfType(nodeType: NodeType, node: ProsemirrorNode): (tr: Transaction) => Transaction; | ||
export function removeSelectedNode(tr: Transaction): Transaction | void; | ||
export function removeSelectedNode(tr: Transaction): Transaction; | ||
export function safeInsert(node: ProsemirrorNode): (tr: Transaction) => Transaction | void; | ||
export function safeInsert(node: ProsemirrorNode): (tr: Transaction) => Transaction; |
Sorry, the diff of this file is not supported yet
50562
398
110