prosemirror-state
Advanced tools
Comparing version 0.22.0 to 0.23.0
@@ -16,3 +16,3 @@ # How to contribute | ||
[GitHub issue tracker](http://github.com/prosemirror/prosemirror/issues). | ||
Before reporting a bug, read these pointers. | ||
Before reporting a bug, please read these pointers. | ||
@@ -26,6 +26,7 @@ - The issue tracker is for *bugs*, not requests for help. Questions | ||
- Mention very precisely what went wrong. "X is broken" is not a good bug | ||
report. What did you expect to happen? What happened instead? Describe the | ||
exact steps a maintainer has to take to make the problem occur. We can not | ||
fix something that we can not observe. | ||
- Mention very precisely what went wrong. "X is broken" is not a good | ||
bug report. What did you expect to happen? What happened instead? | ||
Describe the exact steps a maintainer has to take to make the | ||
problem occur. A screencast can be useful, but is no substitute for | ||
a textual description. | ||
@@ -50,4 +51,4 @@ - A great way to make it easy to reproduce your problem, if it can not | ||
- Follow the code style of the rest of the project (see below). Run | ||
`npm run lint` (in the main repository checkout) that the linter is | ||
happy. | ||
`npm run lint` (in the main repository checkout) to make sure that | ||
the linter is happy. | ||
@@ -54,0 +55,0 @@ - If your changes are easy to test or likely to regress, add tests in |
1131
dist/index.js
@@ -1,9 +0,1128 @@ | ||
;var assign; | ||
((assign = require("./selection"), exports.Selection = assign.Selection, exports.SelectionRange = assign.SelectionRange, exports.TextSelection = assign.TextSelection, exports.NodeSelection = assign.NodeSelection, exports.AllSelection = assign.AllSelection)) | ||
'use strict'; | ||
exports.Transaction = require("./transaction").Transaction | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
exports.EditorState = require("./state").EditorState | ||
var prosemirrorModel = require('prosemirror-model'); | ||
var prosemirrorTransform = require('prosemirror-transform'); | ||
;var assign$1; | ||
((assign$1 = require("./plugin"), exports.Plugin = assign$1.Plugin, exports.PluginKey = assign$1.PluginKey)) | ||
var classesById = Object.create(null); | ||
// ::- Superclass for editor selections. Every selection type should | ||
// extend this. Should not be instantiated directly. | ||
var Selection = function Selection($anchor, $head, ranges) { | ||
// :: [SelectionRange] | ||
// The ranges covered by the selection. | ||
this.ranges = ranges || [new SelectionRange($anchor.min($head), $anchor.max($head))]; | ||
// :: ResolvedPos | ||
// The resolved anchor of the selection (the side that stays in | ||
// place when the selection is modified). | ||
this.$anchor = $anchor; | ||
// :: ResolvedPos | ||
// The resolved head of the selection (the side that moves when | ||
// the selection is modified). | ||
this.$head = $head; | ||
}; | ||
var prototypeAccessors = { anchor: {},head: {},from: {},to: {},$from: {},$to: {},empty: {} }; | ||
// :: number | ||
// The selection's anchor, as an unresolved position. | ||
prototypeAccessors.anchor.get = function () { return this.$anchor.pos }; | ||
// :: number | ||
// The selection's head. | ||
prototypeAccessors.head.get = function () { return this.$head.pos }; | ||
// :: number | ||
// The lower bound of the selection's main range. | ||
prototypeAccessors.from.get = function () { return this.$from.pos }; | ||
// :: number | ||
// The upper bound of the selection's main range. | ||
prototypeAccessors.to.get = function () { return this.$to.pos }; | ||
// :: ResolvedPos | ||
// The resolved lowerbound of the selection's main range. | ||
prototypeAccessors.$from.get = function () { | ||
return this.ranges[0].$from | ||
}; | ||
// :: ResolvedPos | ||
// The resolved upper bound of the selection's main range. | ||
prototypeAccessors.$to.get = function () { | ||
return this.ranges[0].$to | ||
}; | ||
// :: bool | ||
// Indicates whether the selection contains any content. | ||
prototypeAccessors.empty.get = function () { | ||
var ranges = this.ranges; | ||
for (var i = 0; i < ranges.length; i++) | ||
{ if (ranges[i].$from.pos != ranges[i].$to.pos) { return false } } | ||
return true | ||
}; | ||
// eq:: (Selection) → bool | ||
// Test whether the selection is the same as another selection. | ||
// map:: (doc: Node, mapping: Mappable) → Selection | ||
// Map this selection through a [mappable](#transform.Mappable) thing. `doc` | ||
// should be the new document to which we are mapping. | ||
// :: Slice | ||
// Get the content of this selection as a slice. | ||
Selection.prototype.content = function content () { | ||
return this.$from.node(0).slice(this.from, this.to, true) | ||
}; | ||
// :: (Transaction, ?Slice) | ||
// Replace the selection with a slice or, if no slice is given, | ||
// delete the selection. Will append to the given transaction. | ||
Selection.prototype.replace = function replace (tr, content) { | ||
if ( content === void 0 ) content = prosemirrorModel.Slice.empty; | ||
// Put the new selection at 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 = content.content.lastChild, lastParent = null; | ||
for (var i = 0; i < content.openEnd; i++) { | ||
lastParent = lastNode; | ||
lastNode = lastNode.lastChild; | ||
} | ||
var mapFrom = tr.steps.length, ranges = this.ranges; | ||
for (var i$1 = 0; i$1 < ranges.length; i$1++) { | ||
var ref = ranges[i$1]; | ||
var $from = ref.$from; | ||
var $to = ref.$to; | ||
var mapping = tr.mapping.slice(mapFrom); | ||
tr.replaceRange(mapping.map($from.pos), mapping.map($to.pos), i$1 ? prosemirrorModel.Slice.empty : content); | ||
if (i$1 == 0) | ||
{ selectionToInsertionEnd(tr, mapFrom, (lastNode ? lastNode.isInline : lastParent && lastParent.isTextblock) ? -1 : 1); } | ||
} | ||
}; | ||
// :: (Transaction, Node) | ||
// Replace the selection with the given node, appending the changes | ||
// to the given transaction. | ||
Selection.prototype.replaceWith = function replaceWith (tr, node) { | ||
var mapFrom = tr.steps.length, ranges = this.ranges; | ||
for (var i = 0; i < ranges.length; i++) { | ||
var ref = ranges[i]; | ||
var $from = ref.$from; | ||
var $to = ref.$to; | ||
var mapping = tr.mapping.slice(mapFrom); | ||
var from = mapping.map($from.pos), to = mapping.map($to.pos); | ||
if (i) { | ||
tr.deleteRange(from, to); | ||
} else { | ||
tr.replaceRangeWith(from, to, node); | ||
selectionToInsertionEnd(tr, mapFrom, node.isInline ? -1 : 1); | ||
} | ||
} | ||
}; | ||
// toJSON:: () → Object | ||
// Convert the selection to a JSON representation. When implementing | ||
// this for a custom selection class, make sure to give the object a | ||
// `type` property whose value matches the ID under which you | ||
// [registered](#state.Selection^jsonID) your class. | ||
// :: (ResolvedPos, number, ?bool) → ?Selection | ||
// Find a valid cursor or leaf node selection starting at the given | ||
// position and searching back if `dir` is negative, and forward if | ||
// positive. When `textOnly` is true, only consider cursor | ||
// selections. Will return null when no valid selection position is | ||
// found. | ||
Selection.findFrom = function findFrom ($pos, dir, textOnly) { | ||
var inner = $pos.parent.inlineContent ? new TextSelection($pos) | ||
: findSelectionIn($pos.node(0), $pos.parent, $pos.pos, $pos.index(), dir, textOnly); | ||
if (inner) { return inner } | ||
for (var depth = $pos.depth - 1; depth >= 0; depth--) { | ||
var found = dir < 0 | ||
? findSelectionIn($pos.node(0), $pos.node(depth), $pos.before(depth + 1), $pos.index(depth), dir, textOnly) | ||
: findSelectionIn($pos.node(0), $pos.node(depth), $pos.after(depth + 1), $pos.index(depth) + 1, dir, textOnly); | ||
if (found) { return found } | ||
} | ||
}; | ||
// :: (ResolvedPos, ?number) → Selection | ||
// Find a valid cursor or leaf node selection near the given | ||
// position. Searches forward first by default, but if `bias` is | ||
// negative, it will search backwards first. | ||
Selection.near = function near ($pos, bias) { | ||
if ( bias === void 0 ) bias = 1; | ||
return this.findFrom($pos, bias) || this.findFrom($pos, -bias) || new AllSelection($pos.node(0)) | ||
}; | ||
// :: (Node) → Selection | ||
// Find the cursor or leaf node selection closest to the start of | ||
// the given document. Will return an | ||
// [`AllSelection`](#state.AllSelection) if no valid position | ||
// exists. | ||
Selection.atStart = function atStart (doc) { | ||
return findSelectionIn(doc, doc, 0, 0, 1) || new AllSelection(doc) | ||
}; | ||
// :: (Node) → Selection | ||
// Find the cursor or leaf node selection closest to the end of the | ||
// given document. | ||
Selection.atEnd = function atEnd (doc) { | ||
return findSelectionIn(doc, doc, doc.content.size, doc.childCount, -1) || new AllSelection(doc) | ||
}; | ||
// :: (Node, Object) → Selection | ||
// Deserialize the JSON representation of a selection. Must be | ||
// implemented for custom classes (as a static class method). | ||
Selection.fromJSON = function fromJSON (doc, json) { | ||
var cls = classesById[json.type]; | ||
if (!cls) { return this.backwardsCompatFromJSON(doc, json) } | ||
return cls.fromJSON(doc, json) | ||
}; | ||
Selection.backwardsCompatFromJSON = function backwardsCompatFromJSON (doc, json) { | ||
if (json.anchor != null) { return TextSelection.fromJSON(doc, json) } | ||
if (json.node != null) { return NodeSelection.fromJSON(doc, {anchor: json.node, head: json.after}) } | ||
throw new RangeError("Unrecognized JSON data " + JSON.stringify(json)) | ||
}; | ||
// :: (string, constructor<Selection>) | ||
// To be able to deserialize selections from JSON, custom selection | ||
// classes must register themselves with an ID string, so that they | ||
// can be disambiguated. Try to pick something that's unlikely to | ||
// clash with classes from other modules. | ||
Selection.jsonID = function jsonID (id, selectionClass) { | ||
if (id in classesById) { throw new RangeError("Duplicate use of selection JSON ID " + id) } | ||
classesById[id] = selectionClass; | ||
selectionClass.prototype.jsonID = id; | ||
return selectionClass | ||
}; | ||
// :: () → SelectionBookmark | ||
// Get a [bookmark](#state.SelectionBookmark) for this selection, | ||
// which is a value that can be mapped without having access to a | ||
// current document, and later resolved to a real selection for a | ||
// given document again. (This is used mostly by the history to | ||
// track and restore old selections.) The default implementation of | ||
// this method just converts the selection to a text selection and | ||
// returns the bookmark for that. | ||
Selection.prototype.getBookmark = function getBookmark () { | ||
return TextSelection.between(this.anchor, this.head).getBookmark() | ||
}; | ||
Object.defineProperties( Selection.prototype, prototypeAccessors ); | ||
// :: bool | ||
// Controls whether, when a selection of this type is active in the | ||
// browser, the selected range should be visible to the user. Defaults | ||
// to `true`. | ||
Selection.prototype.visible = true; | ||
// SelectionBookmark:: interface | ||
// A lightweight, document-independent representation of a selection. | ||
// You can define a custom bookmark type for a custom selection class | ||
// to make the history handle it well. | ||
// | ||
// map:: (mapping: Mapping) → SelectionBookmark | ||
// Map the bookmark through a set of changes. | ||
// | ||
// resolve:: (doc: Node) → Selection | ||
// Resolve the bookmark to a real selection again. This may need to | ||
// do some error checking and may fall back to a default (usually | ||
// [`TextSelection.between`](#state.TextSelection^between)) if | ||
// mapping made the bookmark invalid. | ||
// ::- Represents a selected range in a document. | ||
var SelectionRange = function SelectionRange($from, $to) { | ||
// :: ResolvedPos | ||
// The lower bound of the range. | ||
this.$from = $from; | ||
// :: ResolvedPos | ||
// The upper bound of the range. | ||
this.$to = $to; | ||
}; | ||
// ::- A text selection represents a classical editor selection, with | ||
// a head (the moving side) and anchor (immobile side), both of which | ||
// point into textblock nodes. It can be empty (a regular cursor | ||
// position). | ||
var TextSelection = (function (Selection) { | ||
function TextSelection($anchor, $head) { | ||
if ( $head === void 0 ) $head = $anchor; | ||
Selection.call(this, $anchor, $head); | ||
} | ||
if ( Selection ) TextSelection.__proto__ = Selection; | ||
TextSelection.prototype = Object.create( Selection && Selection.prototype ); | ||
TextSelection.prototype.constructor = TextSelection; | ||
var prototypeAccessors$1 = { $cursor: {} }; | ||
// :: ?ResolvedPos | ||
// Returns a resolved position if this is a cursor selection (an | ||
// empty text selection), and null otherwise. | ||
prototypeAccessors$1.$cursor.get = function () { return this.$anchor.pos == this.$head.pos ? this.$head : null }; | ||
TextSelection.prototype.map = function map (doc, mapping) { | ||
var $head = doc.resolve(mapping.map(this.head)); | ||
if (!$head.parent.inlineContent) { return Selection.near($head) } | ||
var $anchor = doc.resolve(mapping.map(this.anchor)); | ||
return new TextSelection($anchor.parent.inlineContent ? $anchor : $head, $head) | ||
}; | ||
TextSelection.prototype.replace = function replace (tr, content) { | ||
if ( content === void 0 ) content = prosemirrorModel.Slice.empty; | ||
Selection.prototype.replace.call(this, tr, content); | ||
if (content == prosemirrorModel.Slice.empty) { | ||
var marks = this.$from.marksAcross(this.$to); | ||
if (marks) { tr.ensureMarks(marks); } | ||
} | ||
}; | ||
TextSelection.prototype.eq = function eq (other) { | ||
return other instanceof TextSelection && other.anchor == this.anchor && other.head == this.head | ||
}; | ||
TextSelection.prototype.getBookmark = function getBookmark () { | ||
return new TextBookmark(this.anchor, this.head) | ||
}; | ||
TextSelection.prototype.toJSON = function toJSON () { | ||
return {type: "text", anchor: this.anchor, head: this.head} | ||
}; | ||
TextSelection.fromJSON = function fromJSON (doc, json) { | ||
return new TextSelection(doc.resolve(json.anchor), doc.resolve(json.head)) | ||
}; | ||
// :: (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)) | ||
}; | ||
// :: (ResolvedPos, ResolvedPos, ?number) → Selection | ||
// Return a text selection that spans the given positions or, if | ||
// they aren't text positions, find a text selection near them. | ||
// `bias` determines whether the method searches forward (default) | ||
// or backwards (negative number) first. Will fall back to calling | ||
// [`Selection.near`](#state.Selection^near) when the document | ||
// doesn't contain a valid text position. | ||
TextSelection.between = function between ($anchor, $head, bias) { | ||
var dPos = $anchor.pos - $head.pos; | ||
if (!bias || dPos) { bias = dPos >= 0 ? 1 : -1; } | ||
if (!$head.parent.inlineContent) { | ||
var found = Selection.findFrom($head, bias, true) || Selection.findFrom($head, -bias, true); | ||
if (found) { $head = found.$head; } | ||
else { return Selection.near($head, bias) } | ||
} | ||
if (!$anchor.parent.inlineContent) { | ||
if (dPos == 0) { | ||
$anchor = $head; | ||
} else { | ||
$anchor = (Selection.findFrom($anchor, -bias, true) || Selection.findFrom($anchor, bias, true)).$anchor; | ||
if (($anchor.pos < $head.pos) != (dPos < 0)) { $anchor = $head; } | ||
} | ||
} | ||
return new TextSelection($anchor, $head) | ||
}; | ||
Object.defineProperties( TextSelection.prototype, prototypeAccessors$1 ); | ||
return TextSelection; | ||
}(Selection)); | ||
Selection.jsonID("text", TextSelection); | ||
var TextBookmark = function TextBookmark(anchor, head) { | ||
this.anchor = anchor; | ||
this.head = head; | ||
}; | ||
TextBookmark.prototype.map = function map (mapping) { | ||
return new TextBookmark(mapping.map(this.anchor), mapping.map(this.head)) | ||
}; | ||
TextBookmark.prototype.resolve = function resolve (doc) { | ||
return TextSelection.between(doc.resolve(this.anchor), doc.resolve(this.head)) | ||
}; | ||
// ::- A node selection is a selection that points at a single node. | ||
// All nodes marked [selectable](#model.NodeSpec.selectable) can be | ||
// the target of a node selection. In such a selection, `from` and | ||
// `to` point directly before and after the selected node, `anchor` | ||
// equals `from`, and `head` equals `to`.. | ||
var NodeSelection = (function (Selection) { | ||
function NodeSelection($pos) { | ||
var node = $pos.nodeAfter; | ||
var $end = $pos.node(0).resolve($pos.pos + node.nodeSize); | ||
Selection.call(this, $pos, $end); | ||
// :: Node The selected node. | ||
this.node = node; | ||
} | ||
if ( Selection ) NodeSelection.__proto__ = Selection; | ||
NodeSelection.prototype = Object.create( Selection && Selection.prototype ); | ||
NodeSelection.prototype.constructor = NodeSelection; | ||
NodeSelection.prototype.map = function map (doc, mapping) { | ||
var ref = mapping.mapResult(this.anchor); | ||
var deleted = ref.deleted; | ||
var pos = ref.pos; | ||
var $pos = doc.resolve(pos); | ||
if (deleted) { return Selection.near($pos) } | ||
return new NodeSelection($pos) | ||
}; | ||
NodeSelection.prototype.content = function content () { | ||
return new prosemirrorModel.Slice(prosemirrorModel.Fragment.from(this.node), 0, 0) | ||
}; | ||
NodeSelection.prototype.eq = function eq (other) { | ||
return other instanceof NodeSelection && other.anchor == this.anchor | ||
}; | ||
NodeSelection.prototype.toJSON = function toJSON () { | ||
return {type: "node", anchor: this.anchor} | ||
}; | ||
NodeSelection.prototype.getBookmark = function getBookmark () { return new NodeBookmark(this.anchor) }; | ||
NodeSelection.fromJSON = function fromJSON (doc, json) { | ||
return new NodeSelection(doc.resolve(json.anchor)) | ||
}; | ||
// :: (Node, number) → NodeSelection | ||
// 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; | ||
}(Selection)); | ||
NodeSelection.prototype.visible = false; | ||
Selection.jsonID("node", NodeSelection); | ||
var NodeBookmark = function NodeBookmark(anchor) { | ||
this.anchor = anchor; | ||
}; | ||
NodeBookmark.prototype.map = function map (mapping) { | ||
var ref = mapping.mapResult(this.anchor); | ||
var deleted = ref.deleted; | ||
var pos = ref.pos; | ||
return deleted ? new TextBookmark(pos, pos) : new NodeBookmark(pos) | ||
}; | ||
NodeBookmark.prototype.resolve = function resolve (doc) { | ||
var $pos = doc.resolve(this.anchor), node = $pos.nodeAfter; | ||
if (node && NodeSelection.isSelectable(node)) { return new NodeSelection($pos) } | ||
return Selection.near($pos) | ||
}; | ||
// ::- A selection type that represents selecting the whole document | ||
// (which can not necessarily be expressed with a text selection, when | ||
// there are for example leaf block nodes at the start or end of the | ||
// document). | ||
var AllSelection = (function (Selection) { | ||
function AllSelection(doc) { | ||
Selection.call(this, doc.resolve(0), doc.resolve(doc.content.size)); | ||
} | ||
if ( Selection ) AllSelection.__proto__ = Selection; | ||
AllSelection.prototype = Object.create( Selection && Selection.prototype ); | ||
AllSelection.prototype.constructor = AllSelection; | ||
AllSelection.prototype.toJSON = function toJSON () { return {type: "all"} }; | ||
AllSelection.fromJSON = function fromJSON (doc) { return new AllSelection(doc) }; | ||
AllSelection.prototype.map = function map (doc) { return new AllSelection(doc) }; | ||
AllSelection.prototype.eq = function eq (other) { return other instanceof AllSelection }; | ||
AllSelection.prototype.getBookmark = function getBookmark () { return AllBookmark }; | ||
return AllSelection; | ||
}(Selection)); | ||
Selection.jsonID("all", AllSelection); | ||
var AllBookmark = { | ||
map: function map() { return this }, | ||
resolve: function resolve(doc) { return new AllSelection(doc) } | ||
}; | ||
// FIXME we'll need some awareness of text direction when scanning for selections | ||
// Try to find a selection inside the given node. `pos` points at the | ||
// position where the search starts. When `text` is true, only return | ||
// text selections. | ||
function findSelectionIn(doc, node, pos, index, dir, text) { | ||
if (node.inlineContent) { return TextSelection.create(doc, pos) } | ||
for (var i = index - (dir > 0 ? 0 : 1); dir > 0 ? i < node.childCount : i >= 0; i += dir) { | ||
var child = node.child(i); | ||
if (!child.isAtom) { | ||
var inner = findSelectionIn(doc, child, pos + dir, dir < 0 ? child.childCount : 0, dir, text); | ||
if (inner) { return inner } | ||
} else if (!text && NodeSelection.isSelectable(child)) { | ||
return NodeSelection.create(doc, pos - (dir < 0 ? child.nodeSize : 0)) | ||
} | ||
pos += child.nodeSize * dir; | ||
} | ||
} | ||
function selectionToInsertionEnd(tr, startLen, bias) { | ||
var last = tr.steps.length - 1; | ||
if (last < startLen || !(tr.steps[last] instanceof prosemirrorTransform.ReplaceStep)) { return } | ||
var map = tr.mapping.maps[last], end; | ||
map.forEach(function (_from, _to, _newFrom, newTo) { return end = newTo; }); | ||
if (end != null) { tr.setSelection(Selection.near(tr.doc.resolve(end), bias)); } | ||
} | ||
var UPDATED_SEL = 1; | ||
var UPDATED_MARKS = 2; | ||
var UPDATED_SCROLL = 4; | ||
// ::- An editor state transaction, which can be applied to a state to | ||
// create an updated state. Use | ||
// [`EditorState.tr`](#state.EditorState.tr) to create an instance. | ||
// | ||
// Transactions track changes to the document (they are a subclass of | ||
// [`Transform`](#transform.Transform)), but also other state changes, | ||
// like selection updates and adjustments of the set of [stored | ||
// marks](#state.EditorState.storedMarks). In addition, you can store | ||
// metadata properties in a transaction, which are extra pieces of | ||
// information that client code or plugins can use to describe what a | ||
// transacion represents, so that they can update their [own | ||
// state](#state.StateField) accordingly. | ||
// | ||
// The [editor view](#view.EditorView) uses a few metadata properties: | ||
// it will attach a property `"pointer"` with the value `true` to | ||
// selection transactions directly caused by mouse or touch input, and | ||
// a `"paste"` property of true to transactions caused by a paste.. | ||
var Transaction = (function (Transform$$1) { | ||
function Transaction(state) { | ||
Transform$$1.call(this, state.doc); | ||
// :: number | ||
// The timestamp associated with this transaction, in the same | ||
// format as `Date.now()`. | ||
this.time = Date.now(); | ||
this.curSelection = state.selection; | ||
// The step count for which the current selection is valid. | ||
this.curSelectionFor = 0; | ||
// :: ?[Mark] | ||
// The stored marks set by this transaction, if any. | ||
this.storedMarks = state.storedMarks; | ||
// Bitfield to track which aspects of the state were updated by | ||
// this transaction. | ||
this.updated = 0; | ||
// Object used to store metadata properties for the transaction. | ||
this.meta = Object.create(null); | ||
} | ||
if ( Transform$$1 ) Transaction.__proto__ = Transform$$1; | ||
Transaction.prototype = Object.create( Transform$$1 && Transform$$1.prototype ); | ||
Transaction.prototype.constructor = Transaction; | ||
var prototypeAccessors = { selection: {},selectionSet: {},storedMarksSet: {},isGeneric: {},scrolledIntoView: {} }; | ||
// :: Selection | ||
// The transaction's current selection. This defaults to the editor | ||
// selection [mapped](#state.Selection.map) through the steps in the | ||
// transaction, but can be overwritten with | ||
// [`setSelection`](#state.Transaction.setSelection). | ||
prototypeAccessors.selection.get = function () { | ||
if (this.curSelectionFor < this.steps.length) { | ||
this.curSelection = this.curSelection.map(this.doc, this.mapping.slice(this.curSelectionFor)); | ||
this.curSelectionFor = this.steps.length; | ||
} | ||
return this.curSelection | ||
}; | ||
// :: (Selection) → Transaction | ||
// Update the transaction's current selection. Will determine the | ||
// selection that the editor gets when the transaction is applied. | ||
Transaction.prototype.setSelection = function setSelection (selection) { | ||
this.curSelection = selection; | ||
this.curSelectionFor = this.steps.length; | ||
this.updated = (this.updated | UPDATED_SEL) & ~UPDATED_MARKS; | ||
this.storedMarks = null; | ||
return this | ||
}; | ||
// :: bool | ||
// Whether the selection was explicitly updated by this transaction. | ||
prototypeAccessors.selectionSet.get = function () { | ||
return (this.updated & UPDATED_SEL) > 0 | ||
}; | ||
// :: (?[Mark]) → Transaction | ||
// Set the current stored marks. | ||
Transaction.prototype.setStoredMarks = function setStoredMarks (marks) { | ||
this.storedMarks = marks; | ||
this.updated |= UPDATED_MARKS; | ||
return this | ||
}; | ||
// :: ([Mark]) → Transaction | ||
// Make sure the current stored marks or, if that is null, the marks | ||
// at the selection, match the given set of marks. Does nothing if | ||
// this is already the case. | ||
Transaction.prototype.ensureMarks = function ensureMarks (marks) { | ||
if (!prosemirrorModel.Mark.sameSet(this.storedMarks || this.selection.$from.marks(), marks)) | ||
{ this.setStoredMarks(marks); } | ||
return this | ||
}; | ||
// :: (Mark) → Transaction | ||
// Add a mark to the set of stored marks. | ||
Transaction.prototype.addStoredMark = function addStoredMark (mark) { | ||
return this.ensureMarks(mark.addToSet(this.storedMarks || this.selection.$head.marks())) | ||
}; | ||
// :: (union<Mark, MarkType>) → Transaction | ||
// Remove a mark or mark type from the set of stored marks. | ||
Transaction.prototype.removeStoredMark = function removeStoredMark (mark) { | ||
return this.ensureMarks(mark.removeFromSet(this.storedMarks || this.selection.$head.marks())) | ||
}; | ||
// :: bool | ||
// Whether the stored marks were explicitly set for this transaction. | ||
prototypeAccessors.storedMarksSet.get = function () { | ||
return (this.updated & UPDATED_MARKS) > 0 | ||
}; | ||
Transaction.prototype.addStep = function addStep (step, doc) { | ||
Transform$$1.prototype.addStep.call(this, step, doc); | ||
this.updated = this.updated & ~UPDATED_MARKS; | ||
this.storedMarks = null; | ||
}; | ||
// :: (number) → Transaction | ||
// Update the timestamp for the transaction. | ||
Transaction.prototype.setTime = function setTime (time) { | ||
this.time = time; | ||
return this | ||
}; | ||
// :: (Slice) → Transaction | ||
// Replace the current selection with the given slice. | ||
Transaction.prototype.replaceSelection = function replaceSelection (slice) { | ||
this.selection.replace(this, slice); | ||
return this | ||
}; | ||
// :: (Node, ?bool) → Transaction | ||
// Replace the selection with the given node. When `inheritMarks` is | ||
// true and the content is inline, it inherits the marks from the | ||
// place where it is inserted. | ||
Transaction.prototype.replaceSelectionWith = function replaceSelectionWith (node, inheritMarks) { | ||
var selection = this.selection; | ||
if (inheritMarks !== false) | ||
{ node = node.mark(this.storedMarks || selection.$from.marks(selection.to > selection.from)); } | ||
selection.replaceWith(this, node); | ||
return this | ||
}; | ||
// :: () → Transaction | ||
// Delete the selection. | ||
Transaction.prototype.deleteSelection = function deleteSelection () { | ||
this.selection.replace(this); | ||
return this | ||
}; | ||
// :: (string, from: ?number, to: ?number) → Transaction | ||
// Replace the given range, or the selection if no range is given, | ||
// with a text node containing the given string. | ||
Transaction.prototype.insertText = function insertText (text, from, to) { | ||
if ( to === void 0 ) to = from; | ||
var schema = this.doc.type.schema; | ||
if (from == null) { | ||
if (!text) { return this.deleteSelection() } | ||
return this.replaceSelectionWith(schema.text(text), true) | ||
} else { | ||
if (!text) { return this.deleteRange(from, to) } | ||
var node = schema.text(text, this.storedMarks || this.doc.resolve(from).marks(to > from)); | ||
return this.replaceRangeWith(from, to, node) | ||
} | ||
}; | ||
// :: (union<string, Plugin, PluginKey>, any) → Transaction | ||
// Store a metadata property in this transaction, keyed either by | ||
// name or by plugin. | ||
Transaction.prototype.setMeta = function setMeta (key, value) { | ||
this.meta[typeof key == "string" ? key : key.key] = value; | ||
return this | ||
}; | ||
// :: (union<string, Plugin, PluginKey>) → any | ||
// Retrieve a metadata property for a given name or plugin. | ||
Transaction.prototype.getMeta = function getMeta (key) { | ||
return this.meta[typeof key == "string" ? key : key.key] | ||
}; | ||
// :: bool | ||
// Returns true if this transaction doesn't contain any metadata, | ||
// and can thus safely be extended. | ||
prototypeAccessors.isGeneric.get = function () { | ||
var this$1 = this; | ||
for (var _ in this$1.meta) { return false } | ||
return true | ||
}; | ||
// :: () → Transaction | ||
// Indicate that the editor should scroll the selection into view | ||
// when updated to the state produced by this transaction. | ||
Transaction.prototype.scrollIntoView = function scrollIntoView () { | ||
this.updated |= UPDATED_SCROLL; | ||
return this | ||
}; | ||
prototypeAccessors.scrolledIntoView.get = function () { | ||
return (this.updated & UPDATED_SCROLL) > 0 | ||
}; | ||
Object.defineProperties( Transaction.prototype, prototypeAccessors ); | ||
return Transaction; | ||
}(prosemirrorTransform.Transform)); | ||
function bind(f, self) { | ||
return !self || !f ? f : f.bind(self) | ||
} | ||
var FieldDesc = function FieldDesc(name, desc, self) { | ||
this.name = name; | ||
this.init = bind(desc.init, self); | ||
this.apply = bind(desc.apply, self); | ||
}; | ||
var baseFields = [ | ||
new FieldDesc("doc", { | ||
init: function init(config) { return config.doc || config.schema.topNodeType.createAndFill() }, | ||
apply: function apply(tr) { return tr.doc } | ||
}), | ||
new FieldDesc("selection", { | ||
init: function init(config, instance) { return config.selection || Selection.atStart(instance.doc) }, | ||
apply: function apply(tr) { return tr.selection } | ||
}), | ||
new FieldDesc("storedMarks", { | ||
init: function init() { return null }, | ||
apply: function apply(tr, _marks, _old, state) { return state.selection.$cursor ? tr.storedMarks : null } | ||
}), | ||
new FieldDesc("scrollToSelection", { | ||
init: function init() { return 0 }, | ||
apply: function apply(tr, prev) { return tr.scrolledIntoView ? prev + 1 : prev } | ||
}) | ||
]; | ||
// Object wrapping the part of a state object that stays the same | ||
// across transactions. Stored in the state's `config` property. | ||
var Configuration = function Configuration(schema, plugins) { | ||
var this$1 = this; | ||
this.schema = schema; | ||
this.fields = baseFields.concat(); | ||
this.plugins = []; | ||
this.pluginsByKey = Object.create(null); | ||
if (plugins) { plugins.forEach(function (plugin) { | ||
if (this$1.pluginsByKey[plugin.key]) | ||
{ throw new RangeError("Adding different instances of a keyed plugin (" + plugin.key + ")") } | ||
this$1.plugins.push(plugin); | ||
this$1.pluginsByKey[plugin.key] = plugin; | ||
if (plugin.spec.state) | ||
{ this$1.fields.push(new FieldDesc(plugin.key, plugin.spec.state, plugin)); } | ||
}); } | ||
}; | ||
// ::- The state of a ProseMirror editor is represented by an object | ||
// of this type. A state is a persistent data structure—it isn't | ||
// updated, but rather a new state value is computed from an old one | ||
// using the [`apply`](#state.EditorState.apply) method. | ||
// | ||
// A state holds a number of built-in fields, and plugins can | ||
// [define](#state.PluginSpec.state) additional fields. | ||
var EditorState = function EditorState(config) { | ||
this.config = config; | ||
}; | ||
var prototypeAccessors$1 = { schema: {},plugins: {},tr: {} }; | ||
// doc:: Node | ||
// The current document. | ||
// selection:: Selection | ||
// The selection. | ||
// storedMarks:: ?[Mark] | ||
// A set of marks to apply to the next input. Will be null when | ||
// no explicit marks have been set. | ||
// :: Schema | ||
// The schema of the state's document. | ||
prototypeAccessors$1.schema.get = function () { | ||
return this.config.schema | ||
}; | ||
// :: [Plugin] | ||
// The plugins that are active in this state. | ||
prototypeAccessors$1.plugins.get = function () { | ||
return this.config.plugins | ||
}; | ||
// :: (Transaction) → EditorState | ||
// Apply the given transaction to produce a new state. | ||
EditorState.prototype.apply = function apply (tr) { | ||
return this.applyTransaction(tr).state | ||
}; | ||
// : (Transaction) → ?Transaction | ||
EditorState.prototype.filterTransaction = function filterTransaction (tr, ignore) { | ||
var this$1 = this; | ||
if ( ignore === void 0 ) ignore = -1; | ||
for (var i = 0; i < this.config.plugins.length; i++) { if (i != ignore) { | ||
var plugin = this$1.config.plugins[i]; | ||
if (plugin.spec.filterTransaction && !plugin.spec.filterTransaction.call(plugin, tr, this$1)) | ||
{ return false } | ||
} } | ||
return true | ||
}; | ||
// :: (Transaction) → {state: EditorState, transactions: [Transaction]} | ||
// Verbose variant of [`apply`](#state.EditorState.apply) that | ||
// returns the precise transactions that were applied (which might | ||
// be influenced by the [transaction | ||
// hooks](#state.PluginSpec.filterTransaction) of | ||
// plugins) along with the new state. | ||
EditorState.prototype.applyTransaction = function applyTransaction (tr) { | ||
var this$1 = this; | ||
if (!this.filterTransaction(tr)) { return {state: this, transactions: []} } | ||
var trs = [tr], newState = this.applyInner(tr), seen = null; | ||
// This loop repeatedly gives plugins a chance to respond to | ||
// transactions as new transactions are added, making sure to only | ||
// pass the transactions the plugin did not see before. | ||
outer: for (;;) { | ||
var haveNew = false; | ||
for (var i = 0; i < this.config.plugins.length; i++) { | ||
var plugin = this$1.config.plugins[i]; | ||
if (plugin.spec.appendTransaction) { | ||
var n = seen ? seen[i].n : 0, oldState = seen ? seen[i].state : this$1; | ||
var tr$1 = n < trs.length && | ||
plugin.spec.appendTransaction.call(plugin, n ? trs.slice(n) : trs, oldState, newState); | ||
if (tr$1 && newState.filterTransaction(tr$1, i)) { | ||
tr$1.setMeta("appendedTransaction", tr$1); | ||
if (!seen) { | ||
seen = []; | ||
for (var j = 0; j < this.config.plugins.length; j++) | ||
{ seen.push(j < i ? {state: newState, n: trs.length} : {state: this$1, n: 0}); } | ||
} | ||
trs.push(tr$1); | ||
newState = newState.applyInner(tr$1); | ||
haveNew = true; | ||
} | ||
if (seen) { seen[i] = {state: newState, n: trs.length}; } | ||
} | ||
} | ||
if (!haveNew) { return {state: newState, transactions: trs} } | ||
} | ||
}; | ||
// : (Transaction) → EditorState | ||
EditorState.prototype.applyInner = function applyInner (tr) { | ||
var this$1 = this; | ||
if (!tr.before.eq(this.doc)) { throw new RangeError("Applying a mismatched transaction") } | ||
var newInstance = new EditorState(this.config), fields = this.config.fields; | ||
for (var i = 0; i < fields.length; i++) { | ||
var field = fields[i]; | ||
newInstance[field.name] = field.apply(tr, this$1[field.name], this$1, newInstance); | ||
} | ||
for (var i$1 = 0; i$1 < applyListeners.length; i$1++) { applyListeners[i$1](this$1, tr, newInstance); } | ||
return newInstance | ||
}; | ||
// :: Transaction | ||
// Start a [transaction](#state.Transaction) from this state. | ||
prototypeAccessors$1.tr.get = function () { return new Transaction(this) }; | ||
// :: (Object) → EditorState | ||
// Create a new state. | ||
// | ||
// config::- Configuration options. Must contain `schema` or `doc` (or both). | ||
// | ||
// schema:: ?Schema | ||
// The schema to use. | ||
// | ||
// doc:: ?Node | ||
// The starting document. | ||
// | ||
// selection:: ?Selection | ||
// A valid selection in the document. | ||
// | ||
// plugins:: ?[Plugin] | ||
// The plugins that should be active in this state. | ||
EditorState.create = function create (config) { | ||
var $config = new Configuration(config.schema || config.doc.type.schema, config.plugins); | ||
var instance = new EditorState($config); | ||
for (var i = 0; i < $config.fields.length; i++) | ||
{ instance[$config.fields[i].name] = $config.fields[i].init(config, instance); } | ||
return instance | ||
}; | ||
// :: (Object) → EditorState | ||
// Create a new state based on this one, but with an adjusted set of | ||
// active plugins. State fields that exist in both sets of plugins | ||
// are kept unchanged. Those that no longer exist are dropped, and | ||
// those that are new are initialized using their | ||
// [`init`](#state.StateField.init) method, passing in the new | ||
// configuration object.. | ||
// | ||
// config::- configuration options | ||
// | ||
// schema:: ?Schema | ||
// New schema to use. | ||
// | ||
// plugins:: ?[Plugin] | ||
// New set of active plugins. | ||
EditorState.prototype.reconfigure = function reconfigure (config) { | ||
var this$1 = this; | ||
var $config = new Configuration(config.schema || this.schema, config.plugins); | ||
var fields = $config.fields, instance = new EditorState($config); | ||
for (var i = 0; i < fields.length; i++) { | ||
var name = fields[i].name; | ||
instance[name] = this$1.hasOwnProperty(name) ? this$1[name] : fields[i].init(config, instance); | ||
} | ||
return instance | ||
}; | ||
// :: (?Object<Plugin>) → Object | ||
// Serialize this state to JSON. If you want to serialize the state | ||
// of plugins, pass an object mapping property names to use in the | ||
// resulting JSON object to plugin objects. | ||
EditorState.prototype.toJSON = function toJSON (pluginFields) { | ||
var this$1 = this; | ||
var result = {doc: this.doc.toJSON(), selection: this.selection.toJSON()}; | ||
if (pluginFields) { for (var prop in pluginFields) { | ||
if (prop == "doc" || prop == "selection") | ||
{ throw new RangeError("The JSON fields `doc` and `selection` are reserved") } | ||
var plugin = pluginFields[prop], state = plugin.spec.state; | ||
if (state && state.toJSON) { result[prop] = state.toJSON.call(plugin, this$1[plugin.key]); } | ||
} } | ||
return result | ||
}; | ||
// :: (Object, Object, ?Object<Plugin>) → EditorState | ||
// Deserialize a JSON representation of a state. `config` should | ||
// have at least a `schema` field, and should contain array of | ||
// plugins to initialize the state with. `pluginFields` can be used | ||
// to deserialize the state of plugins, by associating plugin | ||
// instances with the property names they use in the JSON object. | ||
// | ||
// config::- configuration options | ||
// | ||
// schema:: Schema | ||
// The schema to use. | ||
// | ||
// plugins:: ?[Plugin] | ||
// The set of active plugins. | ||
EditorState.fromJSON = function fromJSON (config, json, pluginFields) { | ||
if (!config.schema) { throw new RangeError("Required config field 'schema' missing") } | ||
var $config = new Configuration(config.schema, config.plugins); | ||
var instance = new EditorState($config); | ||
$config.fields.forEach(function (field) { | ||
if (field.name == "doc") { | ||
instance.doc = prosemirrorModel.Node.fromJSON(config.schema, json.doc); | ||
} else if (field.name == "selection") { | ||
instance.selection = Selection.fromJSON(instance.doc, json.selection); | ||
} else { | ||
if (pluginFields) { for (var prop in pluginFields) { | ||
var plugin = pluginFields[prop], state = plugin.spec.state; | ||
if (plugin.key == field.name && state && state.fromJSON && | ||
Object.prototype.hasOwnProperty.call(json, prop)) { | ||
// This field belongs to a plugin mapped to a JSON field, read it from there. | ||
instance[field.name] = state.fromJSON.call(plugin, config, json[prop], instance); | ||
return | ||
} | ||
} } | ||
instance[field.name] = field.init(config, instance); | ||
} | ||
}); | ||
return instance | ||
}; | ||
// Kludge to allow the view to track mappings between different | ||
// instances of a state. | ||
EditorState.addApplyListener = function addApplyListener (f) { | ||
applyListeners.push(f); | ||
}; | ||
EditorState.removeApplyListener = function removeApplyListener (f) { | ||
var found = applyListeners.indexOf(f); | ||
if (found > -1) { applyListeners.splice(found, 1); } | ||
}; | ||
Object.defineProperties( EditorState.prototype, prototypeAccessors$1 ); | ||
var applyListeners = []; | ||
// PluginSpec:: interface | ||
// | ||
// This is the type passed to the [`Plugin`](#state.Plugin) | ||
// constructor. It provides a definition for a plugin. | ||
// | ||
// props:: ?EditorProps | ||
// The [view props](#view.EditorProps) added by this plugin. Props | ||
// that are functions will be bound to have the plugin instance as | ||
// their `this` binding. | ||
// | ||
// state:: ?StateField<any> | ||
// Allows a plugin to define a [state field](#state.StateField), an | ||
// extra slot in the state object in which it can keep its own data. | ||
// | ||
// key:: ?PluginKey | ||
// Can be used to make this a keyed plugin. You can have only one | ||
// plugin with a given key in a given state, but it is possible to | ||
// access the plugin's configuration and state through the key, | ||
// without having access to the plugin instance object. | ||
// | ||
// view:: ?(EditorView) → Object | ||
// When the plugin needs to interact with the editor view, or | ||
// set something up in the DOM, use this field. The function | ||
// will be called when the plugin's state is associated with an | ||
// editor view. | ||
// | ||
// return::- | ||
// Should return an object with the following optional | ||
// properties: | ||
// | ||
// update:: ?(view: EditorView, prevState: EditorState) | ||
// Called whenever the view's state is updated. | ||
// | ||
// destroy:: ?() | ||
// Called when the view is destroyed or receives a state | ||
// with different plugins. | ||
// | ||
// filterTransaction:: ?(Transaction, EditorState) → bool | ||
// When present, this will be called before a transaction is | ||
// applied by the state, allowing the plugin to cancel it (by | ||
// returning false). | ||
// | ||
// appendTransaction:: ?(transactions: [Transaction], oldState: EditorState, newState: EditorState) → ?Transaction | ||
// Allows the plugin to append another transaction to be applied | ||
// after the given array of transactions. When another plugin | ||
// appends a transaction after this was called, it is called again | ||
// with the new state and new transactions—but only the new | ||
// transactions, i.e. it won't be passed transactions that it | ||
// already saw. | ||
function bindProps(obj, self, target) { | ||
for (var prop in obj) { | ||
var val = obj[prop]; | ||
if (val instanceof Function) { val = val.bind(self); } | ||
else if (prop == "handleDOMEvents") { val = bindProps(val, self, {}); } | ||
target[prop] = val; | ||
} | ||
return target | ||
} | ||
// ::- Plugins bundle functionality that can be added to an editor. | ||
// They are part of the [editor state](#state.EditorState) and | ||
// may influence that state and the view that contains it. | ||
var Plugin = function Plugin(spec) { | ||
// :: EditorProps | ||
// The [props](#view.EditorProps) exported by this plugin. | ||
this.props = {}; | ||
if (spec.props) { bindProps(spec.props, this, this.props); } | ||
// :: Object | ||
// The plugin's [spec object](#state.PluginSpec). | ||
this.spec = spec; | ||
this.key = spec.key ? spec.key.key : createKey("plugin"); | ||
}; | ||
// :: (EditorState) → any | ||
// Extract the plugin's state field from an editor state. | ||
Plugin.prototype.getState = function getState (state) { return state[this.key] }; | ||
// StateField:: interface<T> | ||
// A plugin spec may provide a state field (under its | ||
// [`state`](#state.PluginSpec.state) property) of this type, which | ||
// describes the state it wants to keep. Functions provided here are | ||
// always called with the plugin instance as their `this` binding. | ||
// | ||
// init:: (config: Object, instance: EditorState) → T | ||
// Initialize the value of the field. `config` will be the object | ||
// passed to [`EditorState.create`](#state.EditorState^create). Note | ||
// that `instance` is a half-initialized state instance, and will | ||
// not have values for plugin fields initialized after this one. | ||
// | ||
// apply:: (tr: Transaction, value: T, oldState: EditorState, newState: EditorState) → T | ||
// Apply the given transaction to this state field, producing a new | ||
// field value. Note that the `newState` argument is again a partially | ||
// constructed state does not yet contain the state from plugins | ||
// coming after this one. | ||
// | ||
// toJSON:: ?(value: T) → * | ||
// Convert this field to JSON. Optional, can be left off to disable | ||
// JSON serialization for the field. | ||
// | ||
// fromJSON:: ?(config: Object, value: *, state: EditorState) → T | ||
// Deserialize the JSON representation of this field. Note that the | ||
// `state` argument is again a half-initialized state. | ||
var keys = Object.create(null); | ||
function createKey(name) { | ||
if (name in keys) { return name + "$" + ++keys[name] } | ||
keys[name] = 0; | ||
return name + "$" | ||
} | ||
// ::- A key is used to [tag](#state.PluginSpec.key) | ||
// plugins in a way that makes it possible to find them, given an | ||
// editor state. Assigning a key does mean only one plugin of that | ||
// type can be active in a state. | ||
var PluginKey = function PluginKey(name) { | ||
if ( name === void 0 ) name = "key"; | ||
this.key = createKey(name); }; | ||
// :: (EditorState) → ?Plugin | ||
// Get the active plugin with this key, if any, from an editor | ||
// state. | ||
PluginKey.prototype.get = function get (state) { return state.config.pluginsByKey[this.key] }; | ||
// :: (EditorState) → ?any | ||
// Get the plugin's state from an editor state. | ||
PluginKey.prototype.getState = function getState (state) { return state[this.key] }; | ||
exports.Selection = Selection; | ||
exports.SelectionRange = SelectionRange; | ||
exports.TextSelection = TextSelection; | ||
exports.NodeSelection = NodeSelection; | ||
exports.AllSelection = AllSelection; | ||
exports.Transaction = Transaction; | ||
exports.EditorState = EditorState; | ||
exports.Plugin = Plugin; | ||
exports.PluginKey = PluginKey; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "prosemirror-state", | ||
"version": "0.22.0", | ||
"version": "0.23.0", | ||
"description": "ProseMirror editor state", | ||
@@ -19,17 +19,18 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"prosemirror-model": "^0.22.0", | ||
"prosemirror-transform": "^0.22.0" | ||
"prosemirror-model": "^0.23.0", | ||
"prosemirror-transform": "^0.23.0" | ||
}, | ||
"devDependencies": { | ||
"buble": "^0.15.1", | ||
"ist": "^1.0.0", | ||
"mocha": "^3.0.2", | ||
"ist": "^1.0.0", | ||
"rimraf": "^2.5.4", | ||
"prosemirror-test-builder": "^0.22.0" | ||
"prosemirror-test-builder": "^0.23.0", | ||
"rollup": "^0.49.0", | ||
"rollup-plugin-buble": "^0.15.0" | ||
}, | ||
"scripts": { | ||
"test": "mocha test/test-*.js", | ||
"build": "rimraf dist && buble -i src -o dist", | ||
"prepublish": "npm run build" | ||
"build": "rollup -c", | ||
"watch": "rollup -c -w", | ||
"prepare": "npm run build" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # prosemirror-state | ||
This [module](http://prosemirror.net/ref.html#state) implements the | ||
This [module](http://prosemirror.net/docs/ref/#state) implements the | ||
editor state, which tracks the current document and selection, and | ||
@@ -15,9 +15,5 @@ managed plugins. | ||
The [project page](http://prosemirror.net) has more information, a | ||
number of [demos](http://prosemirror.net/#demos) and the | ||
[documentation](http://prosemirror.net/docs.html). | ||
number of [examples](http://prosemirror.net/examples/) and the | ||
[documentation](http://prosemirror.net/docs/). | ||
**NOTE:** This project is in *BETA* stage. It isn't thoroughly tested, | ||
and the API might still change across `0.x` releases. You are welcome | ||
to use it, but don't expect it to be very stable yet. | ||
This code is released under an | ||
@@ -24,0 +20,0 @@ [MIT license](https://github.com/prosemirror/prosemirror/tree/master/LICENSE). |
@@ -1,11 +0,7 @@ | ||
;({Selection: exports.Selection, | ||
SelectionRange: exports.SelectionRange, | ||
TextSelection: exports.TextSelection, | ||
NodeSelection: exports.NodeSelection, | ||
AllSelection: exports.AllSelection} = require("./selection")) | ||
export {Selection, SelectionRange, TextSelection, NodeSelection, AllSelection} from "./selection" | ||
exports.Transaction = require("./transaction").Transaction | ||
export {Transaction} from "./transaction" | ||
exports.EditorState = require("./state").EditorState | ||
export {EditorState} from "./state" | ||
;({Plugin: exports.Plugin, PluginKey: exports.PluginKey} = require("./plugin")) | ||
export {Plugin, PluginKey} from "./plugin" |
// PluginSpec:: interface | ||
// | ||
// A plugin spec provides a definition for a plugin. | ||
// This is the type passed to the [`Plugin`](#state.Plugin) | ||
// constructor. It provides a definition for a plugin. | ||
// | ||
@@ -11,10 +12,10 @@ // props:: ?EditorProps | ||
// state:: ?StateField<any> | ||
// A [state field](#state.StateField) defined by this plugin. | ||
// Allows a plugin to define a [state field](#state.StateField), an | ||
// extra slot in the state object in which it can keep its own data. | ||
// | ||
// key:: ?PluginKey | ||
// Can optionally be used to make this a keyed plugin. You can | ||
// have only one plugin with a given key in a given state, but | ||
// it is possible to access the plugin's configuration and state | ||
// through the key, without having access to the plugin instance | ||
// itself. | ||
// Can be used to make this a keyed plugin. You can have only one | ||
// plugin with a given key in a given state, but it is possible to | ||
// access the plugin's configuration and state through the key, | ||
// without having access to the plugin instance object. | ||
// | ||
@@ -46,4 +47,6 @@ // view:: ?(EditorView) → Object | ||
// after the given array of transactions. When another plugin | ||
// appends a transaction after this was called, it is called | ||
// again with the new state and extended array of transactions. | ||
// appends a transaction after this was called, it is called again | ||
// with the new state and new transactions—but only the new | ||
// transactions, i.e. it won't be passed transactions that it | ||
// already saw. | ||
@@ -60,6 +63,6 @@ function bindProps(obj, self, target) { | ||
// ::- Plugins wrap extra functionality that can be added to an | ||
// editor. They can define new [state fields](#state.StateField), and | ||
// add [view props](#view.EditorProps). | ||
class Plugin { | ||
// ::- Plugins bundle functionality that can be added to an editor. | ||
// They are part of the [editor state](#state.EditorState) and | ||
// may influence that state and the view that contains it. | ||
export class Plugin { | ||
// :: (PluginSpec) | ||
@@ -69,7 +72,7 @@ // Create a plugin. | ||
// :: EditorProps | ||
// The props exported by this plugin. | ||
// The [props](#view.EditorProps) exported by this plugin. | ||
this.props = {} | ||
if (spec.props) bindProps(spec.props, this, this.props) | ||
// :: Object | ||
// The plugin's configuration object. | ||
// The plugin's [spec object](#state.PluginSpec). | ||
this.spec = spec | ||
@@ -80,24 +83,23 @@ this.key = spec.key ? spec.key.key : createKey("plugin") | ||
// :: (EditorState) → any | ||
// Get the state field for this plugin. | ||
// Extract the plugin's state field from an editor state. | ||
getState(state) { return state[this.key] } | ||
} | ||
exports.Plugin = Plugin | ||
// StateField:: interface<T> | ||
// A plugin may provide a state field (under its `state` property) of | ||
// this type, which describes the state it wants to keep. Functions | ||
// provided here are always called with the plugin instance as their | ||
// `this` binding. | ||
// A plugin spec may provide a state field (under its | ||
// [`state`](#state.PluginSpec.state) property) of this type, which | ||
// describes the state it wants to keep. Functions provided here are | ||
// always called with the plugin instance as their `this` binding. | ||
// | ||
// init:: (config: Object, instance: EditorState) → T | ||
// Initialize the value of this field. `config` will be the object | ||
// Initialize the value of the field. `config` will be the object | ||
// passed to [`EditorState.create`](#state.EditorState^create). Note | ||
// that `instance` is a half-initialized state instance, and will | ||
// not have values for any fields initialized after this one. | ||
// not have values for plugin fields initialized after this one. | ||
// | ||
// apply:: (tr: Transaction, value: T, oldState: EditorState, newState: EditorState) → T | ||
// Apply the given transaction to this state field, producing a new | ||
// field value. Note that the `newState` argument is a partially | ||
// field value. Note that the `newState` argument is again a partially | ||
// constructed state does not yet contain the state from plugins | ||
// coming after this plugin. | ||
// coming after this one. | ||
// | ||
@@ -124,3 +126,3 @@ // toJSON:: ?(value: T) → * | ||
// type can be active in a state. | ||
class PluginKey { | ||
export class PluginKey { | ||
// :: (?string) | ||
@@ -139,2 +141,1 @@ // Create a plugin key. | ||
} | ||
exports.PluginKey = PluginKey |
@@ -16,6 +16,8 @@ This module implements the state object of a ProseMirror editor, along | ||
A ProseMirror selection can be either a classical | ||
[text selection](#state.TextSelection) (of which cursors are a special | ||
case), or a [_node_ selection](#state.NodeSelection), where a specific | ||
document node is selected. | ||
A ProseMirror selection can be one of several types. This module | ||
defines types for classical [text selections](#state.TextSelection) | ||
(of which cursors are a special case) and [_node_ | ||
selections](#state.NodeSelection), where a specific document node is | ||
selected. It is possible to extend the editor with custom selection | ||
types. | ||
@@ -32,8 +34,8 @@ @Selection | ||
To make distributing and using extra editor functionality easier, | ||
To make it easy to package and enable extra editor functionality, | ||
ProseMirror has a plugin system. | ||
@PluginSpec | ||
@StateField | ||
@Plugin | ||
@StateField | ||
@PluginKey |
@@ -1,8 +0,9 @@ | ||
const {Slice, Fragment} = require("prosemirror-model") | ||
import {Slice, Fragment} from "prosemirror-model" | ||
import {ReplaceStep} from "prosemirror-transform" | ||
const classesById = Object.create(null) | ||
// ::- Superclass for editor selections. Should not be instantiated | ||
// directly, only extended. | ||
class Selection { | ||
// ::- Superclass for editor selections. Every selection type should | ||
// extend this. Should not be instantiated directly. | ||
export class Selection { | ||
// :: (ResolvedPos, ResolvedPos, ?[SelectionRange]) | ||
@@ -27,17 +28,15 @@ // Initialize a selection with the head and anchor and ranges. If no | ||
// :: number | ||
// The selection's immobile side (does not move when | ||
// shift-selecting). | ||
// The selection's anchor, as an unresolved position. | ||
get anchor() { return this.$anchor.pos } | ||
// :: number | ||
// The selection's mobile side (the side that moves when | ||
// shift-selecting). | ||
// The selection's head. | ||
get head() { return this.$head.pos } | ||
// :: number | ||
// The lower bound of the selection's first range. | ||
// The lower bound of the selection's main range. | ||
get from() { return this.$from.pos } | ||
// :: number | ||
// The upper bound of the selection's first range. | ||
// The upper bound of the selection's main range. | ||
get to() { return this.$to.pos } | ||
@@ -71,3 +70,3 @@ | ||
// Map this selection through a [mappable](#transform.Mappable) thing. `doc` | ||
// should be the new document, to which we are mapping. | ||
// should be the new document to which we are mapping. | ||
@@ -123,4 +122,3 @@ // :: Slice | ||
// `type` property whose value matches the ID under which you | ||
// [registered](#state.Selection^jsonID) your class. The default | ||
// implementation adds `type`, `head`, and `anchor` properties. | ||
// [registered](#state.Selection^jsonID) your class. | ||
@@ -130,4 +128,5 @@ // :: (ResolvedPos, number, ?bool) → ?Selection | ||
// position and searching back if `dir` is negative, and forward if | ||
// negative. When `textOnly` is true, only consider cursor | ||
// selections. | ||
// positive. When `textOnly` is true, only consider cursor | ||
// selections. Will return null when no valid selection position is | ||
// found. | ||
static findFrom($pos, dir, textOnly) { | ||
@@ -156,4 +155,5 @@ let inner = $pos.parent.inlineContent ? new TextSelection($pos) | ||
// Find the cursor or leaf node selection closest to the start of | ||
// the given document. Will return an `AllSelection` if no valid | ||
// position exists. | ||
// the given document. Will return an | ||
// [`AllSelection`](#state.AllSelection) if no valid position | ||
// exists. | ||
static atStart(doc) { | ||
@@ -165,4 +165,3 @@ return findSelectionIn(doc, doc, 0, 0, 1) || new AllSelection(doc) | ||
// Find the cursor or leaf node selection closest to the end of the | ||
// given document. Will return an `AllSelection` if no valid | ||
// position exists. | ||
// given document. | ||
static atEnd(doc) { | ||
@@ -173,3 +172,3 @@ return findSelectionIn(doc, doc, doc.content.size, doc.childCount, -1) || new AllSelection(doc) | ||
// :: (Node, Object) → Selection | ||
// Deserialize a JSON representation of a selection. Must be | ||
// Deserialize the JSON representation of a selection. Must be | ||
// implemented for custom classes (as a static class method). | ||
@@ -212,3 +211,2 @@ static fromJSON(doc, json) { | ||
} | ||
exports.Selection = Selection | ||
@@ -232,7 +230,7 @@ // :: bool | ||
// do some error checking and may fall back to a default (usually | ||
// [`TextSelection.between`](#state.TextSelection.between) if | ||
// [`TextSelection.between`](#state.TextSelection^between)) if | ||
// mapping made the bookmark invalid. | ||
// ::- Represents a selected range in a document. | ||
class SelectionRange { | ||
export class SelectionRange { | ||
// :: (ResolvedPos, ResolvedPos) | ||
@@ -248,3 +246,2 @@ constructor($from, $to) { | ||
} | ||
exports.SelectionRange = SelectionRange | ||
@@ -255,3 +252,3 @@ // ::- A text selection represents a classical editor selection, with | ||
// position). | ||
class TextSelection extends Selection { | ||
export class TextSelection extends Selection { | ||
// :: (ResolvedPos, ?ResolvedPos) | ||
@@ -278,4 +275,4 @@ // Construct a text selection between the given points. | ||
if (content == Slice.empty) { | ||
if (this.$from.parentOffset < this.$from.parent.content.size) | ||
tr.ensureMarks(this.$from.marks(true)) | ||
let marks = this.$from.marksAcross(this.$to) | ||
if (marks) tr.ensureMarks(marks) | ||
} | ||
@@ -333,3 +330,2 @@ } | ||
} | ||
exports.TextSelection = TextSelection | ||
@@ -351,7 +347,8 @@ Selection.jsonID("text", TextSelection) | ||
// ::- A node selection is a selection that points at a | ||
// single node. All nodes marked [selectable](#model.NodeSpec.selectable) | ||
// can be the target of a node selection. In such an object, `from` | ||
// and `to` point directly before and after the selected node. | ||
class NodeSelection extends Selection { | ||
// ::- A node selection is a selection that points at a single node. | ||
// All nodes marked [selectable](#model.NodeSpec.selectable) can be | ||
// the target of a node selection. In such a selection, `from` and | ||
// `to` point directly before and after the selected node, `anchor` | ||
// equals `from`, and `head` equals `to`.. | ||
export class NodeSelection extends Selection { | ||
// :: (ResolvedPos) | ||
@@ -393,3 +390,3 @@ // Create a node selection. Does not verify the validity of its | ||
// :: (Node, number, ?number) → NodeSelection | ||
// :: (Node, number) → NodeSelection | ||
// Create a node selection from non-resolved positions. | ||
@@ -407,3 +404,2 @@ static create(doc, from) { | ||
} | ||
exports.NodeSelection = NodeSelection | ||
@@ -433,3 +429,3 @@ NodeSelection.prototype.visible = false | ||
// document). | ||
class AllSelection extends Selection { | ||
export class AllSelection extends Selection { | ||
// :: (Node) | ||
@@ -451,3 +447,2 @@ // Create an all-selection over the given document. | ||
} | ||
exports.AllSelection = AllSelection | ||
@@ -481,6 +476,7 @@ Selection.jsonID("all", AllSelection) | ||
function selectionToInsertionEnd(tr, startLen, bias) { | ||
if (tr.steps.length == startLen) return | ||
let map = tr.mapping.maps[tr.mapping.maps.length - 1], end | ||
let last = tr.steps.length - 1 | ||
if (last < startLen || !(tr.steps[last] instanceof ReplaceStep)) return | ||
let map = tr.mapping.maps[last], end | ||
map.forEach((_from, _to, _newFrom, newTo) => end = newTo) | ||
if (end != null) tr.setSelection(Selection.near(tr.doc.resolve(end), bias)) | ||
} |
@@ -1,5 +0,5 @@ | ||
const {Node} = require("prosemirror-model") | ||
import {Node} from "prosemirror-model" | ||
const {Selection} = require("./selection") | ||
const {Transaction} = require("./transaction") | ||
import {Selection} from "./selection" | ||
import {Transaction} from "./transaction" | ||
@@ -60,9 +60,9 @@ function bind(f, self) { | ||
// ::- The state of a ProseMirror editor is represented by an object | ||
// of this type. This is a persistent data structure—it isn't updated, | ||
// but rather a new state value is computed from an old one with the | ||
// [`apply`](#state.EditorState.apply) method. | ||
// of this type. A state is a persistent data structure—it isn't | ||
// updated, but rather a new state value is computed from an old one | ||
// using the [`apply`](#state.EditorState.apply) method. | ||
// | ||
// In addition to the built-in state fields, plugins can define | ||
// additional pieces of state. | ||
class EditorState { | ||
// A state holds a number of built-in fields, and plugins can | ||
// [define](#state.PluginSpec.state) additional fields. | ||
export class EditorState { | ||
constructor(config) { | ||
@@ -79,4 +79,4 @@ this.config = config | ||
// storedMarks:: ?[Mark] | ||
// A set of marks to apply to the next character that's typed. Will | ||
// be null whenever no explicit marks have been set. | ||
// A set of marks to apply to the next input. Will be null when | ||
// no explicit marks have been set. | ||
@@ -167,3 +167,3 @@ // :: Schema | ||
// :: (Object) → EditorState | ||
// Create a state. | ||
// Create a new state. | ||
// | ||
@@ -280,4 +280,3 @@ // config::- Configuration options. Must contain `schema` or `doc` (or both). | ||
} | ||
exports.EditorState = EditorState | ||
const applyListeners = [] |
@@ -1,3 +0,3 @@ | ||
const {Transform} = require("prosemirror-transform") | ||
const {Mark} = require("prosemirror-model") | ||
import {Transform} from "prosemirror-transform" | ||
import {Mark} from "prosemirror-model" | ||
@@ -19,11 +19,12 @@ const UPDATED_SEL = 1, UPDATED_MARKS = 2, UPDATED_SCROLL = 4 | ||
// | ||
// The [editor view](#view.EditorView) uses a single metadata | ||
// property: it will attach a property `"pointer"` with the value | ||
// `true` to selection transactions directly caused by mouse or touch | ||
// input. | ||
class Transaction extends Transform { | ||
// The [editor view](#view.EditorView) uses a few metadata properties: | ||
// it will attach a property `"pointer"` with the value `true` to | ||
// selection transactions directly caused by mouse or touch input, and | ||
// a `"paste"` property of true to transactions caused by a paste.. | ||
export class Transaction extends Transform { | ||
constructor(state) { | ||
super(state.doc) | ||
// :: number | ||
// The timestamp associated with this transaction. | ||
// The timestamp associated with this transaction, in the same | ||
// format as `Date.now()`. | ||
this.time = Date.now() | ||
@@ -34,3 +35,3 @@ this.curSelection = state.selection | ||
// :: ?[Mark] | ||
// The stored marks in this transaction. | ||
// The stored marks set by this transaction, if any. | ||
this.storedMarks = state.storedMarks | ||
@@ -45,5 +46,5 @@ // Bitfield to track which aspects of the state were updated by | ||
// :: Selection | ||
// The transform's current selection. This defaults to the | ||
// editor selection [mapped](#state.Selection.map) through the steps in | ||
// this transform, but can be overwritten with | ||
// The transaction's current selection. This defaults to the editor | ||
// selection [mapped](#state.Selection.map) through the steps in the | ||
// transaction, but can be overwritten with | ||
// [`setSelection`](#state.Transaction.setSelection). | ||
@@ -59,5 +60,4 @@ get selection() { | ||
// :: (Selection) → Transaction | ||
// Update the transaction's current selection. This will determine | ||
// the selection that the editor gets when the transaction is | ||
// applied. | ||
// Update the transaction's current selection. Will determine the | ||
// selection that the editor gets when the transaction is applied. | ||
setSelection(selection) { | ||
@@ -95,2 +95,14 @@ this.curSelection = selection | ||
// :: (Mark) → Transaction | ||
// Add a mark to the set of stored marks. | ||
addStoredMark(mark) { | ||
return this.ensureMarks(mark.addToSet(this.storedMarks || this.selection.$head.marks())) | ||
} | ||
// :: (union<Mark, MarkType>) → Transaction | ||
// Remove a mark or mark type from the set of stored marks. | ||
removeStoredMark(mark) { | ||
return this.ensureMarks(mark.removeFromSet(this.storedMarks || this.selection.$head.marks())) | ||
} | ||
// :: bool | ||
@@ -116,2 +128,3 @@ // Whether the stored marks were explicitly set for this transaction. | ||
// :: (Slice) → Transaction | ||
// Replace the current selection with the given slice. | ||
replaceSelection(slice) { | ||
@@ -123,6 +136,5 @@ this.selection.replace(this, slice) | ||
// :: (Node, ?bool) → Transaction | ||
// 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. | ||
// Replace the selection with the given node. When `inheritMarks` is | ||
// true and the content is inline, it inherits the marks from the | ||
// place where it is inserted. | ||
replaceSelectionWith(node, inheritMarks) { | ||
@@ -174,3 +186,3 @@ let selection = this.selection | ||
// Returns true if this transaction doesn't contain any metadata, | ||
// and can thus be safely extended. | ||
// and can thus safely be extended. | ||
get isGeneric() { | ||
@@ -192,15 +204,2 @@ for (let _ in this.meta) return false | ||
} | ||
// :: (Mark) → Transaction | ||
// Add a mark to the set of stored marks. | ||
addStoredMark(mark) { | ||
return this.ensureMarks(mark.addToSet(this.storedMarks || this.selection.$head.marks())) | ||
} | ||
// :: (union<Mark, MarkType>) → Transaction | ||
// Remove a mark or mark type from the set of stored marks. | ||
removeStoredMark(mark) { | ||
return this.ensureMarks(mark.removeFromSet(this.storedMarks || this.selection.$head.marks())) | ||
} | ||
} | ||
exports.Transaction = Transaction |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
197464
18
2920
23
+ Addedprosemirror-model@0.23.1(transitive)
+ Addedprosemirror-transform@0.23.0(transitive)
- Removedprosemirror-model@0.22.0(transitive)
- Removedprosemirror-transform@0.22.2(transitive)
Updatedprosemirror-model@^0.23.0