prosemirror-utils
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -6,4 +6,4 @@ 'use strict'; | ||
var prosemirrorState = require('prosemirror-state'); | ||
var prosemirrorModel = require('prosemirror-model'); | ||
var prosemirrorTables = require('prosemirror-tables'); | ||
var prosemirrorModel = require('prosemirror-model'); | ||
@@ -71,2 +71,16 @@ // :: (selection: Selection) → boolean | ||
// :: ($pos: ResolvedPos, content: union<ProseMirrorNode, Fragment>) → boolean | ||
// Checks if a given `content` can be inserted at the given `$pos` | ||
var canInsert = function canInsert($pos, content) { | ||
var index = $pos.index(); | ||
// Fragment | ||
if (content instanceof prosemirrorModel.Fragment) { | ||
return $pos.parent.canReplace(index, index, content); | ||
} else if (content instanceof prosemirrorModel.Node) { | ||
// Node | ||
return $pos.parent.canReplaceWith(index, index, content.type); | ||
} | ||
return false; | ||
}; | ||
// :: (predicate: (node: ProseMirrorNode) → boolean) → (selection: Selection) → ?{pos: number, node: ProseMirrorNode} | ||
@@ -620,14 +634,12 @@ // Iterates over parent nodes, returning the first node and its position `predicate` returns truthy for. | ||
// :: (node: ProseMirrorNode) → (tr: Transaction) → Transaction | ||
// :: (content: union<ProseMirrorNode, Fragment>) → (tr: Transaction) → Transaction | ||
// Returns a new 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 an appropriate place for a 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) { | ||
var safeInsert = function safeInsert(content) { | ||
return function (tr) { | ||
var $from = tr.selection.$from; | ||
// given node is allowed at the current cursor position | ||
var index = $from.index(); | ||
// given node is allowed at the current cursor position | ||
if ($from.parent.canReplaceWith(index, index, node.type)) { | ||
return cloneTr(tr.insert($from.pos, node)); | ||
if (canInsert($from, content)) { | ||
return cloneTr(tr.insert($from.pos, content)); | ||
} | ||
@@ -639,5 +651,4 @@ | ||
var $pos = tr.doc.resolve(pos); | ||
var _index = $pos.index(); | ||
if ($pos.parent.canReplaceWith(_index, _index, node.type)) { | ||
return cloneTr(tr.insert(pos, node)); | ||
if (canInsert($pos, content)) { | ||
return cloneTr(tr.insert(pos, content)); | ||
} | ||
@@ -676,2 +687,3 @@ } | ||
exports.isNodeSelection = isNodeSelection; | ||
exports.canInsert = canInsert; | ||
exports.findParentNode = findParentNode; | ||
@@ -678,0 +690,0 @@ exports.findParentDomRef = findParentDomRef; |
{ | ||
"name": "prosemirror-utils", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Utils library for ProseMirror", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -194,3 +194,7 @@ # Utils library for ProseMirror | ||
* **`safeInsert`**`(node: ProseMirrorNode) → fn(tr: Transaction) → Transaction`\ | ||
* **`canInsert`**`($pos: ResolvedPos, content: ProseMirrorNode | Fragment) → boolean`\ | ||
Checks if a given `content` can be inserted at the given `$pos` | ||
* **`safeInsert`**`(content: ProseMirrorNode | Fragment) → fn(tr: Transaction) → Transaction`\ | ||
Returns a new 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 an appropriate place for a given `node` in the document, looping through parent nodes up until the root document node. | ||
@@ -197,0 +201,0 @@ It will return the original transaction if the place for insertion hasn't been found. |
@@ -1,2 +0,2 @@ | ||
import { Node as ProsemirrorNode, Schema, NodeType, Mark, MarkType } from 'prosemirror-model'; | ||
import { Node as ProsemirrorNode, Schema, NodeType, Mark, MarkType, ResolvedPos, Fragment } from 'prosemirror-model'; | ||
import { Selection, Transaction } from 'prosemirror-state'; | ||
@@ -90,6 +90,8 @@ | ||
export function safeInsert(node: ProsemirrorNode): (tr: Transaction) => Transaction; | ||
export function canInsert($pos: ResolvedPos, node: ProsemirrorNode | Fragment): boolean; | ||
export function safeInsert(node: ProsemirrorNode | Fragment): (tr: Transaction) => Transaction; | ||
export function setParentNodeMarkup(nodeType: NodeType | NodeType[], type?: NodeType | null, attrs?: { [key: string]: any } | null, marks?: Mark[]): (tr: Transaction) => Transaction; | ||
export function selectParentNodeOfType(nodeType: NodeType | NodeType[]): (tr: Transaction) => Transaction; |
Sorry, the diff of this file is not supported yet
85327
708
215