Socket
Socket
Sign inDemoInstall

prosemirror-state

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prosemirror-state - npm Package Compare versions

Comparing version 0.12.0 to 0.13.0

9

dist/index.js
;var assign;
((assign = require("./selection"), exports.Selection = assign.Selection, exports.TextSelection = assign.TextSelection, exports.NodeSelection = assign.NodeSelection, exports.isSelectable = assign.isSelectable))
((assign = require("./selection"), exports.Selection = assign.Selection, exports.TextSelection = assign.TextSelection, exports.NodeSelection = assign.NodeSelection))
exports.EditorTransform = require("./transform").EditorTransform
;var assign$1;
((assign$1 = require("./transform"), exports.EditorTransform = assign$1.EditorTransform, exports.extendTransformAction = assign$1.extendTransformAction))
exports.EditorState = require("./state").EditorState
;var assign$1;
((assign$1 = require("./plugin"), exports.Plugin = assign$1.Plugin, exports.PluginKey = assign$1.PluginKey))
;var assign$2;
((assign$2 = require("./plugin"), exports.Plugin = assign$2.Plugin, exports.PluginKey = assign$2.PluginKey))

@@ -38,6 +38,7 @@ // ::- Plugins wrap extra functionality that can be added to an

//
// applyAction:: (state: EditorState, action: Action) → T
// applyAction:: (action: Action, value: T, oldState: EditorState, newState: EditorState) → T
// Apply the given action to this state field, producing a new field
// value. Note that the `state` argument is the _old_ state, before
// the action was applied.
// value. Note that the `newState` argument is a partially
// constructed state does not yet contain the state from plugins
// coming after this plugin.
//

@@ -44,0 +45,0 @@ // toJSON:: ?(value: T) → *

@@ -133,3 +133,3 @@ // ::- Superclass for editor selections.

var $pos = doc.resolve(json.node), after = $pos.nodeAfter
if (after && json.after == json.pos + after.nodeSize && isSelectable(after)) { return new NodeSelection($pos) }
if (after && json.after == json.pos + after.nodeSize && NodeSelection.isSelectable(after)) { return new NodeSelection($pos) }
else { return Selection.near($pos) }

@@ -187,2 +187,11 @@ }

// :: (Node, number, ?number) → TextSelection
// Create a text selection from non-resolved positions.
TextSelection.create = function create (doc, anchor, head) {
if ( head === void 0 ) head = anchor;
var $anchor = doc.resolve(anchor)
return new this($anchor, head == anchor ? $anchor : doc.resolve(head))
};
Object.defineProperties( TextSelection.prototype, prototypeAccessors$1 );

@@ -217,3 +226,3 @@

var $from = doc.resolve(from.pos), node = $from.nodeAfter
if (!from.deleted && !to.deleted && node && to.pos == from.pos + node.nodeSize && isSelectable(node))
if (!from.deleted && !to.deleted && node && to.pos == from.pos + node.nodeSize && NodeSelection.isSelectable(node))
{ return new NodeSelection($from) }

@@ -227,2 +236,15 @@ return Selection.near($from)

// :: (Node, number, ?number) → TextSelection
// Create a node selection from non-resolved positions.
NodeSelection.create = function create (doc, from) {
return new this(doc.resolve(from))
};
// :: (Node) → bool
// Determines whether the given node may be selected as a node
// selection.
NodeSelection.isSelectable = function isSelectable (node) {
return !node.isText && node.type.spec.selectable !== false
};
return NodeSelection;

@@ -238,3 +260,3 @@ }(Selection));

function findSelectionIn(doc, node, pos, index, dir, text) {
if (node.isTextblock) { return new TextSelection(doc.resolve(pos)) }
if (node.isTextblock) { return TextSelection.create(doc, pos) }
for (var i = index - (dir > 0 ? 0 : 1); dir > 0 ? i < node.childCount : i >= 0; i += dir) {

@@ -245,4 +267,4 @@ var child = node.child(i)

if (inner) { return inner }
} else if (!text && isSelectable(child)) {
return new NodeSelection(doc.resolve(pos - (dir < 0 ? child.nodeSize : 0)))
} else if (!text && NodeSelection.isSelectable(child)) {
return NodeSelection.create(doc, pos - (dir < 0 ? child.nodeSize : 0))
}

@@ -252,6 +274,1 @@ pos += child.nodeSize * dir

}
function isSelectable(node) {
return !node.isText && node.type.spec.selectable !== false
}
exports.isSelectable = isSelectable

@@ -146,3 +146,3 @@ var ref = require("prosemirror-model");

var field = fields[i]
newInstance[field.name] = field.applyAction(action, this$1[field.name], this$1)
newInstance[field.name] = field.applyAction(action, this$1[field.name], this$1, newInstance)
}

@@ -266,2 +266,9 @@ return newInstance

//
// sealed: ?bool
// Should be set to true when this action's transform should not be
// changed after the fact with
// [`extendTransformAction`](#state.extendTransformAction) (for
// example when the action carries metadata that makes assumptions
// about the transform). Defaults to false.
//
// scrollIntoView:: ?bool

@@ -268,0 +275,0 @@ // When true, the next display update will scroll the cursor into

@@ -1,9 +0,10 @@

var ref = require("prosemirror-model");
var Fragment = ref.Fragment;
var ref$1 = require("prosemirror-transform");
var Transform = ref$1.Transform;
var insertPoint = ref$1.insertPoint;
var ref = require("prosemirror-transform");
var Transform = ref.Transform;
var ref$1 = require("prosemirror-model");
var Node = ref$1.Node;
var ref$2 = require("./selection");
var Selection = ref$2.Selection;
var warnedAboutReplaceSelection = false
// ::- A selection-aware extension of `Transform`. Use

@@ -49,42 +50,40 @@ // [`EditorState.tr`](#state.EditorState.tr) to create an instance.

// :: (?Node, ?bool) → EditorTransform
// Replace the selection with the given node, or delete it if `node`
// is null. When `inheritMarks` is true and the node is an inline
// node, it inherits the marks from the place where it is inserted.
EditorTransform.prototype.replaceSelection = function replaceSelection (node, inheritMarks) {
// :: (Slice) → EditorTransform
EditorTransform.prototype.replaceSelection = function replaceSelection (slice) {
if (slice instanceof Node) { // FIXME backwards compat hack, drop in next release
if (!warnedAboutReplaceSelection) {
if (typeof console != "undefined" && console.warn)
{ console.warn("Calling EditorTransform.replaceSelection with a node is deprecated. Use replaceSelectionWith") }
warnedAboutReplaceSelection = true
}
return this.replaceSelectionWith(slice)
}
var ref = this.selection;
var $from = ref.$from;
var $to = ref.$to;
var from = ref.from;
var to = ref.to;
var selNode = ref.node;
var startLen = this.steps.length
this.replaceRange(from, to, slice)
// Move the selection to the position after the inserted content.
// When that ended in an inline node, search backwards, to get the
// position after that node. If not, search forward.
var lastNode = slice.content.lastChild
for (var i = 0; i < slice.openRight; i++) { lastNode = lastNode.lastChild }
selectionToInsertionEnd(this, startLen, lastNode && lastNode.isInline ? -1 : 1)
return this
};
if (node && node.isInline && inheritMarks !== false)
// :: (Node, ?bool) → EditorTransform
// Replace the selection with the given node or slice, or delete it
// if `content` is null. When `inheritMarks` is true and the content
// is inline, it inherits the marks from the place where it is
// inserted.
EditorTransform.prototype.replaceSelectionWith = function replaceSelectionWith (node, inheritMarks) {
var ref = this.selection;
var from = ref.from;
var to = ref.to;
var startLen = this.steps.length
if (inheritMarks !== false)
{ node = node.mark(this.state.storedMarks || this.doc.marksAt(from, to > from)) }
var fragment = Fragment.from(node)
if (selNode && selNode.isTextblock && node && node.isInline) {
// Putting inline stuff onto a selected textblock puts it
// inside, so cut off the sides
from++
to--
} else if (selNode) {
var depth = $from.depth
// This node can not simply be removed/replaced. Remove its parent as well
while (depth && $from.node(depth).childCount == 1 &&
!$from.node(depth).canReplace($from.index(depth), $to.indexAfter(depth), fragment)) {
depth--
}
if (depth < $from.depth) {
from = $from.before(depth + 1)
to = $from.after(depth + 1)
}
} else if (node && from == to) {
var point = insertPoint(this.doc, from, node.type, node.attrs)
if (point != null) { from = to = point }
}
this.replaceWith(from, to, fragment)
var map = this.mapping.maps[this.mapping.maps.length - 1]
this.setSelection(Selection.near(this.doc.resolve(map.map(to)), node && node.isInline ? -1 : 1))
this.replaceRangeWith(from, to, node)
selectionToInsertionEnd(this, startLen, node.isInline ? -1 : 1)
return this

@@ -96,3 +95,6 @@ };

EditorTransform.prototype.deleteSelection = function deleteSelection () {
return this.replaceSelection()
var ref = this.selection;
var from = ref.from;
var to = ref.to;
return this.deleteRange(from, to)
};

@@ -106,19 +108,10 @@

var useSel = from == null
if (useSel) {
;var assign;
((assign = this.selection, from = assign.from, to = assign.to))
if (from == null) {
if (!text) { return this.deleteSelection() }
return this.replaceSelectionWith(this.state.schema.text(text), true)
} else {
if (!text) { return this.deleteRange(from, to) }
var node = this.state.schema.text(text, this.state.storedMarks || this.doc.marksAt(from, to > from))
return this.replaceRangeWith(from, to, node)
}
var node = text ? this.state.schema.text(text, this.state.storedMarks || this.doc.marksAt(from, to > from)) : null
if (useSel)
{ this.replaceSelection(node, false) }
else
{ this.replaceWith(from, to, node) }
if (text && useSel) {
var map = this.mapping.maps[this.mapping.maps.length - 1]
this.setSelection(Selection.findFrom(this.doc.resolve(map.map(to)), -1))
}
return this
};

@@ -150,1 +143,27 @@

exports.EditorTransform = EditorTransform
function selectionToInsertionEnd(tr, startLen, bias) {
if (tr.steps.length == startLen) { return }
var map = tr.mapping.maps[tr.mapping.maps.length - 1], end
map.forEach(function (_from, _to, _newFrom, newTo) { return end = newTo; })
if (end != null) { tr.setSelection(Selection.near(tr.doc.resolve(end), bias)) }
}
// :: (Action, (transform: Transform)) → Action
// If, when dispatching actions, you need to extend a transform action
// with additional steps, you can use this helper. It takes an action
// and a function that extends a transform, and will update the action
// to reflect any additional steps. It won't call the function if the
// action is not a transform action or a
// [sealed](#state.TransformAction.sealed) transform action.
function extendTransformAction(action, f) {
if (action.type != "transform" || action.sealed) { return action }
var tr = action.transform, steps = tr.steps.length, set = tr.selectionSet
f(tr)
if (!set && tr.selectionSet)
{ action.selection = tr.selection }
else if (action.selection && tr.steps.length > steps)
{ action.selection = action.selection.map(tr.doc, tr.mapping.slice(steps)) }
return action
}
exports.extendTransformAction = extendTransformAction
{
"name": "prosemirror-state",
"version": "0.12.0",
"version": "0.13.0",
"description": "ProseMirror editor state",

@@ -19,4 +19,4 @@ "main": "dist/index.js",

"dependencies": {
"prosemirror-model": "^0.12.0",
"prosemirror-transform": "^0.12.0"
"prosemirror-model": "^0.13.0",
"prosemirror-transform": "^0.13.0"
},

@@ -23,0 +23,0 @@ "devDependencies": {

# prosemirror-state
[ [**WEBSITE**](http://prosemirror.net) | [**ISSUES**](https://github.com/prosemirror/prosemirror/issues) | [**FORUM**](https://discuss.prosemirror.net) | [**GITTER**](https://gitter.im/ProseMirror/prosemirror) ]
[ [**WEBSITE**](http://prosemirror.net) | [**ISSUES**](https://github.com/prosemirror/prosemirror/issues) | [**FORUM**](https://discuss.prosemirror.net) | [**GITTER**](https://gitter.im/ProseMirror/prosemirror) | [**CHANGELOG**](https://github.com/ProseMirror/prosemirror/blob/master/CHANGELOG.md) ]

@@ -5,0 +5,0 @@ ProseMirror is a well-behaved rich semantic content editor based on

;({Selection: exports.Selection,
TextSelection: exports.TextSelection,
NodeSelection: exports.NodeSelection,
isSelectable: exports.isSelectable} = require("./selection"))
NodeSelection: exports.NodeSelection} = require("./selection"))
exports.EditorTransform = require("./transform").EditorTransform
;({EditorTransform: exports.EditorTransform,
extendTransformAction: exports.extendTransformAction} = require("./transform"))

@@ -8,0 +8,0 @@ exports.EditorState = require("./state").EditorState

@@ -13,3 +13,3 @@ // ::- Plugins wrap extra functionality that can be added to an

// Note that the [`onAction`](#view.EditorProps.onAction) and
// [`state`](#view.EditorProps.state] props can't be defined by
// [`state`](#view.EditorProps.state) props can't be defined by
// plugins, only by the main props object. Props that are

@@ -61,6 +61,7 @@ // functions will be bound to have the plugin instance as their

//
// applyAction:: (state: EditorState, action: Action) → T
// applyAction:: (action: Action, value: T, oldState: EditorState, newState: EditorState) → T
// Apply the given action to this state field, producing a new field
// value. Note that the `state` argument is the _old_ state, before
// the action was applied.
// value. Note that the `newState` argument is a partially
// constructed state does not yet contain the state from plugins
// coming after this plugin.
//

@@ -67,0 +68,0 @@ // toJSON:: ?(value: T) → *

@@ -19,2 +19,8 @@ This module implements the state object of a ProseMirror editor, along

It is possible, when dispatching an action, to change it before
applying it. The following helper can be used to safely update
transform actions:
@extendTransformAction
### Selection

@@ -30,3 +36,3 @@

@NodeSelection
c
### Plugin System

@@ -33,0 +39,0 @@

@@ -132,3 +132,3 @@ // ::- Superclass for editor selections.

let $pos = doc.resolve(json.node), after = $pos.nodeAfter
if (after && json.after == json.pos + after.nodeSize && isSelectable(after)) return new NodeSelection($pos)
if (after && json.after == json.pos + after.nodeSize && NodeSelection.isSelectable(after)) return new NodeSelection($pos)
else return Selection.near($pos)

@@ -181,2 +181,9 @@ }

}
// :: (Node, number, ?number) → TextSelection
// Create a text selection from non-resolved positions.
static create(doc, anchor, head = anchor) {
let $anchor = doc.resolve(anchor)
return new this($anchor, head == anchor ? $anchor : doc.resolve(head))
}
}

@@ -207,3 +214,3 @@ exports.TextSelection = TextSelection

let $from = doc.resolve(from.pos), node = $from.nodeAfter
if (!from.deleted && !to.deleted && node && to.pos == from.pos + node.nodeSize && isSelectable(node))
if (!from.deleted && !to.deleted && node && to.pos == from.pos + node.nodeSize && NodeSelection.isSelectable(node))
return new NodeSelection($from)

@@ -216,2 +223,15 @@ return Selection.near($from)

}
// :: (Node, number, ?number) → TextSelection
// Create a node selection from non-resolved positions.
static create(doc, from) {
return new this(doc.resolve(from))
}
// :: (Node) → bool
// Determines whether the given node may be selected as a node
// selection.
static isSelectable(node) {
return !node.isText && node.type.spec.selectable !== false
}
}

@@ -226,3 +246,3 @@ exports.NodeSelection = NodeSelection

function findSelectionIn(doc, node, pos, index, dir, text) {
if (node.isTextblock) return new TextSelection(doc.resolve(pos))
if (node.isTextblock) return TextSelection.create(doc, pos)
for (let i = index - (dir > 0 ? 0 : 1); dir > 0 ? i < node.childCount : i >= 0; i += dir) {

@@ -233,4 +253,4 @@ let child = node.child(i)

if (inner) return inner
} else if (!text && isSelectable(child)) {
return new NodeSelection(doc.resolve(pos - (dir < 0 ? child.nodeSize : 0)))
} else if (!text && NodeSelection.isSelectable(child)) {
return NodeSelection.create(doc, pos - (dir < 0 ? child.nodeSize : 0))
}

@@ -240,6 +260,1 @@ pos += child.nodeSize * dir

}
function isSelectable(node) {
return !node.isText && node.type.spec.selectable !== false
}
exports.isSelectable = isSelectable

@@ -142,3 +142,3 @@ const {Mark, Node} = require("prosemirror-model")

let field = fields[i]
newInstance[field.name] = field.applyAction(action, this[field.name], this)
newInstance[field.name] = field.applyAction(action, this[field.name], this, newInstance)
}

@@ -257,2 +257,9 @@ return newInstance

//
// sealed: ?bool
// Should be set to true when this action's transform should not be
// changed after the fact with
// [`extendTransformAction`](#state.extendTransformAction) (for
// example when the action carries metadata that makes assumptions
// about the transform). Defaults to false.
//
// scrollIntoView:: ?bool

@@ -259,0 +266,0 @@ // When true, the next display update will scroll the cursor into

@@ -1,5 +0,7 @@

const {Fragment} = require("prosemirror-model")
const {Transform, insertPoint} = require("prosemirror-transform")
const {Transform} = require("prosemirror-transform")
const {Node} = require("prosemirror-model")
const {Selection} = require("./selection")
let warnedAboutReplaceSelection = false
// ::- A selection-aware extension of `Transform`. Use

@@ -39,37 +41,34 @@ // [`EditorState.tr`](#state.EditorState.tr) to create an instance.

// :: (?Node, ?bool) → EditorTransform
// Replace the selection with the given node, or delete it if `node`
// is null. When `inheritMarks` is true and the node is an inline
// node, it inherits the marks from the place where it is inserted.
replaceSelection(node, inheritMarks) {
let {$from, $to, from, to, node: selNode} = this.selection
if (node && node.isInline && inheritMarks !== false)
node = node.mark(this.state.storedMarks || this.doc.marksAt(from, to > from))
let fragment = Fragment.from(node)
if (selNode && selNode.isTextblock && node && node.isInline) {
// Putting inline stuff onto a selected textblock puts it
// inside, so cut off the sides
from++
to--
} else if (selNode) {
let depth = $from.depth
// This node can not simply be removed/replaced. Remove its parent as well
while (depth && $from.node(depth).childCount == 1 &&
!$from.node(depth).canReplace($from.index(depth), $to.indexAfter(depth), fragment)) {
depth--
// :: (Slice) → EditorTransform
replaceSelection(slice) {
if (slice instanceof Node) { // FIXME backwards compat hack, drop in next release
if (!warnedAboutReplaceSelection) {
if (typeof console != "undefined" && console.warn)
console.warn("Calling EditorTransform.replaceSelection with a node is deprecated. Use replaceSelectionWith")
warnedAboutReplaceSelection = true
}
if (depth < $from.depth) {
from = $from.before(depth + 1)
to = $from.after(depth + 1)
}
} else if (node && from == to) {
let point = insertPoint(this.doc, from, node.type, node.attrs)
if (point != null) from = to = point
return this.replaceSelectionWith(slice)
}
let {from, to} = this.selection, startLen = this.steps.length
this.replaceRange(from, to, slice)
// Move the selection to the position after the inserted content.
// When that ended in an inline node, search backwards, to get the
// position after that node. If not, search forward.
let lastNode = slice.content.lastChild
for (let i = 0; i < slice.openRight; i++) lastNode = lastNode.lastChild
selectionToInsertionEnd(this, startLen, lastNode && lastNode.isInline ? -1 : 1)
return this
}
this.replaceWith(from, to, fragment)
let map = this.mapping.maps[this.mapping.maps.length - 1]
this.setSelection(Selection.near(this.doc.resolve(map.map(to)), node && node.isInline ? -1 : 1))
// :: (Node, ?bool) → EditorTransform
// Replace the selection with the given node or slice, or delete it
// if `content` is null. When `inheritMarks` is true and the content
// is inline, it inherits the marks from the place where it is
// inserted.
replaceSelectionWith(node, inheritMarks) {
let {from, to} = this.selection, startLen = this.steps.length
if (inheritMarks !== false)
node = node.mark(this.state.storedMarks || this.doc.marksAt(from, to > from))
this.replaceRangeWith(from, to, node)
selectionToInsertionEnd(this, startLen, node.isInline ? -1 : 1)
return this

@@ -81,3 +80,4 @@ }

deleteSelection() {
return this.replaceSelection()
let {from, to} = this.selection
return this.deleteRange(from, to)
}

@@ -89,18 +89,10 @@

insertText(text, from, to = from) {
let useSel = from == null
if (useSel) {
;({from, to} = this.selection)
if (from == null) {
if (!text) return this.deleteSelection()
return this.replaceSelectionWith(this.state.schema.text(text), true)
} else {
if (!text) return this.deleteRange(from, to)
let node = this.state.schema.text(text, this.state.storedMarks || this.doc.marksAt(from, to > from))
return this.replaceRangeWith(from, to, node)
}
let node = text ? this.state.schema.text(text, this.state.storedMarks || this.doc.marksAt(from, to > from)) : null
if (useSel)
this.replaceSelection(node, false)
else
this.replaceWith(from, to, node)
if (text && useSel) {
let map = this.mapping.maps[this.mapping.maps.length - 1]
this.setSelection(Selection.findFrom(this.doc.resolve(map.map(to)), -1))
}
return this
}

@@ -128,1 +120,27 @@

exports.EditorTransform = EditorTransform
function selectionToInsertionEnd(tr, startLen, bias) {
if (tr.steps.length == startLen) return
let map = tr.mapping.maps[tr.mapping.maps.length - 1], end
map.forEach((_from, _to, _newFrom, newTo) => end = newTo)
if (end != null) tr.setSelection(Selection.near(tr.doc.resolve(end), bias))
}
// :: (Action, (transform: Transform)) → Action
// If, when dispatching actions, you need to extend a transform action
// with additional steps, you can use this helper. It takes an action
// and a function that extends a transform, and will update the action
// to reflect any additional steps. It won't call the function if the
// action is not a transform action or a
// [sealed](#state.TransformAction.sealed) transform action.
function extendTransformAction(action, f) {
if (action.type != "transform" || action.sealed) return action
let tr = action.transform, steps = tr.steps.length, set = tr.selectionSet
f(tr)
if (!set && tr.selectionSet)
action.selection = tr.selection
else if (action.selection && tr.steps.length > steps)
action.selection = action.selection.map(tr.doc, tr.mapping.slice(steps))
return action
}
exports.extendTransformAction = extendTransformAction
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