Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

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.16.0 to 0.17.0

dist/transaction.js

7

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

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

//
// 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 `newState` argument is a partially
// 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
// constructed state does not yet contain the state from plugins

@@ -43,0 +43,0 @@ // coming after this plugin.

@@ -26,18 +26,2 @@ // ::- Superclass for editor selections.

// :: (?Object) → SelectionAction
// Create an [action](#state.Action) that updates the selection to
// this one.
Selection.prototype.action = function action (options) {
var action = {type: "selection", selection: this, time: Date.now()}
if (options) { for (var prop in options) { action[prop] = options[prop] } }
return action
};
// :: () → SelectionAction
// Create an action that updates the selection to this one and
// scrolls it into view.
Selection.prototype.scrollAction = function scrollAction () {
return this.action({scrollIntoView: true})
};
// eq:: (other: Selection) → bool

@@ -44,0 +28,0 @@ // Test whether the selection is the same as another selection.

var ref = require("prosemirror-model");
var Mark = ref.Mark;
var Node = ref.Node;

@@ -7,4 +6,4 @@

var Selection = ref$1.Selection;
var ref$2 = require("./transform");
var EditorTransform = ref$2.EditorTransform;
var ref$2 = require("./transaction");
var Transaction = ref$2.Transaction;

@@ -18,3 +17,3 @@ function bind(f, self) {

this.init = bind(desc.init, self)
this.applyAction = bind(desc.applyAction, self)
this.apply = bind(desc.apply, self)
};

@@ -25,5 +24,3 @@

init: function init(config) { return config.doc || config.schema.nodes.doc.createAndFill() },
applyAction: function applyAction(action, doc) {
return action.type == "transform" ? action.transform.doc : doc
}
apply: function apply(tr) { return tr.doc }
}),

@@ -33,9 +30,3 @@

init: function init(config, instance) { return config.selection || Selection.atStart(instance.doc) },
applyAction: function applyAction(action, selection) {
if (action.type == "transform")
{ return action.selection || selection.map(action.transform.doc, action.transform.mapping) }
if (action.type == "selection")
{ return action.selection }
return selection
}
apply: function apply(tr) { return tr.selection }
}),

@@ -45,11 +36,3 @@

init: function init() { return null },
applyAction: function applyAction(action, storedMarks, state) {
if (action.type == "transform") { return action.selection ? null : storedMarks }
if (action.type == "selection") { return null }
if (action.type == "addStoredMark" && state.selection.empty)
{ return action.mark.addToSet(storedMarks || currentMarks(state.doc, state.selection)) }
if (action.type == "removeStoredMark" && state.selection.empty)
{ return action.markType.removeFromSet(storedMarks || currentMarks(state.doc, state.selection)) }
return storedMarks
}
apply: function apply(tr, _marks, _old, state) { return state.selection.empty ? tr.storedMarks : null }
}),

@@ -59,15 +42,8 @@

init: function init() { return 0 },
applyAction: function applyAction(action, prev) {
return (action.type == "transform" || action.type == "selection") && action.scrollIntoView
? prev + 1 : prev
}
apply: function apply(tr, prev) { return tr.scrolledIntoView ? prev + 1 : prev }
})
]
function currentMarks(doc, selection) {
return selection.head == null ? Mark.none : doc.marksAt(selection.head)
}
// Object wrapping the part of a state object that stays the same
// across actions. Stored in the state's `config` property.
// across transactions. Stored in the state's `config` property.
var Configuration = function Configuration(schema, plugins) {

@@ -77,3 +53,3 @@ var this$1 = this;

this.schema = schema
this.fields = baseFields.slice()
this.fields = baseFields.concat()
this.plugins = []

@@ -94,3 +70,3 @@ this.pluginsByKey = Object.create(null)

// but rather a new state value is computed from an old one with the
// [`applyAction`](#state.EditorState.applyAction) method.
// [`apply`](#state.EditorState.apply) method.
//

@@ -127,20 +103,78 @@ // In addition to the built-in state fields, plugins can define

// :: (Action) → EditorState
// Apply the given action to produce a new state.
EditorState.prototype.applyAction = function applyAction (action) {
// :: (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;
if (action.type == null) { throw new RangeError("Not a valid action") }
for (var i = 0; i < this.config.plugins.length; i++) { if (i != ignore) {
var plugin = this$1.config.plugins[i]
if (plugin.options.filterTransaction && !plugin.options.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.Plugin.constructor^options.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.options.appendTransaction) {
var n = seen ? seen[i].n : 0, oldState = seen ? seen[i].state : this$1
var tr$1 = n < trs.length &&
plugin.options.appendTransaction.call(plugin, n ? trs.slice(n) : trs, oldState, newState)
if (tr$1 && newState.filterTransaction(tr$1, i)) {
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.applyAction(action, this$1[field.name], this$1, newInstance)
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, action, newInstance) }
for (var i$1 = 0; i$1 < applyListeners.length; i$1++) { applyListeners[i$1](this$1, tr, newInstance) }
return newInstance
};
// :: EditorTransform
// Create a selection-aware [`Transform` object](#state.EditorTransform).
prototypeAccessors.tr.get = function () { return new EditorTransform(this) };
// :: Transaction
// Start a [transaction](#state.Transaction) from this state.
prototypeAccessors.tr.get = function () { return new Transaction(this) };

@@ -244,70 +278,1 @@ // :: (Object) → EditorState

var applyListeners = []
// Action:: interface
// State updates are performed through actions, which are objects that
// describe the update.
//
// type:: string
// The type of this action. This determines the way the action is
// interpreted, and which other fields it should have.
// TransformAction:: interface
// An action type that transforms the state's document. Applying this
// will create a state in which the document is the result of this
// transformation.
//
// type:: "transform"
//
// transform:: Transform
//
// selection:: ?Selection
// If given, this selection will be used as the new selection. If
// not, the old selection is mapped through the transform.
//
// time:: number
// The timestamp at which the change was made.
//
// 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
// When true, the next display update will scroll the cursor into
// view.
// SelectionAction:: interface
// An action that updates the selection.
//
// type:: "selection"
//
// selection:: Selection
// The new selection.
//
// origin:: ?string
// An optional string giving more information about the source of
// the action. The [view](##view) will set this to `"mouse"` for
// selection actions originating from mouse events.
//
// scrollIntoView:: ?bool
// When true, the next display update will scroll the cursor into
// view.
//
// time:: number
// The timestamp at which the action was created.
// AddStoredMarkAction:: interface
// An action type that adds a stored mark to the state.
//
// type:: "addStoredMark"
//
// mark:: Mark
// RemoveStoredMarkAction:: interface
// An action type that removes a stored mark from the state.
//
// type:: "removeStoredMark"
//
// markType:: MarkType
{
"name": "prosemirror-state",
"version": "0.16.0",
"version": "0.17.0",
"description": "ProseMirror editor state",

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

"dependencies": {
"prosemirror-model": "^0.16.0",
"prosemirror-transform": "^0.16.0"
"prosemirror-model": "^0.17.0",
"prosemirror-transform": "^0.17.0"
},

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

@@ -5,4 +5,3 @@ ;({Selection: exports.Selection,

;({EditorTransform: exports.EditorTransform,
extendTransformAction: exports.extendTransformAction} = require("./transform"))
exports.Transaction = require("./transaction").Transaction

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

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

// The [view props](#view.EditorProps) added by this plugin.
// Note that the [`onAction`](#view.EditorProps.onAction) and
// [`state`](#view.EditorProps.state) props can't be defined by
// plugins, only by the main props object. Props that are
// Note that the
// [`dispatchTransaction`](#view.EditorProps.dispatchTransaction)
// and [`state`](#view.EditorProps.state) props can't be defined
// by plugins, only by the main props object. Props that are
// functions will be bound to have the plugin instance as their

@@ -35,3 +36,3 @@ // `this` binding.

//
// return:: Object
// return::-
// Should return an object with the following optional

@@ -46,2 +47,13 @@ // properties:

// 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 extended array of transactions.
constructor(options) {

@@ -80,5 +92,5 @@ // :: EditorProps

//
// 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 `newState` argument is a partially
// 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
// constructed state does not yet contain the state from plugins

@@ -85,0 +97,0 @@ // coming after this plugin.

@@ -9,18 +9,7 @@ This module implements the state object of a ProseMirror editor, along

[object](#state.EditorState). That object is updated (creating a new
state) by applying [actions](#state.Action) to it.
state) by applying [transactions](#state.Transaction) to it.
@EditorState
@Action
@TransformAction
@SelectionAction
@AddStoredMarkAction
@RemoveStoredMarkAction
@EditorTransform
@Transaction
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

@@ -27,0 +16,0 @@

@@ -27,18 +27,2 @@ // ::- Superclass for editor selections.

// :: (?Object) → SelectionAction
// Create an [action](#state.Action) that updates the selection to
// this one.
action(options) {
let action = {type: "selection", selection: this, time: Date.now()}
if (options) for (let prop in options) action[prop] = options[prop]
return action
}
// :: () → SelectionAction
// Create an action that updates the selection to this one and
// scrolls it into view.
scrollAction() {
return this.action({scrollIntoView: true})
}
// eq:: (other: Selection) → bool

@@ -45,0 +29,0 @@ // Test whether the selection is the same as another selection.

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

const {Mark, Node} = require("prosemirror-model")
const {Node} = require("prosemirror-model")
const {Selection} = require("./selection")
const {EditorTransform} = require("./transform")
const {Transaction} = require("./transaction")

@@ -14,3 +14,3 @@ function bind(f, self) {

this.init = bind(desc.init, self)
this.applyAction = bind(desc.applyAction, self)
this.apply = bind(desc.apply, self)
}

@@ -22,5 +22,3 @@ }

init(config) { return config.doc || config.schema.nodes.doc.createAndFill() },
applyAction(action, doc) {
return action.type == "transform" ? action.transform.doc : doc
}
apply(tr) { return tr.doc }
}),

@@ -30,9 +28,3 @@

init(config, instance) { return config.selection || Selection.atStart(instance.doc) },
applyAction(action, selection) {
if (action.type == "transform")
return action.selection || selection.map(action.transform.doc, action.transform.mapping)
if (action.type == "selection")
return action.selection
return selection
}
apply(tr) { return tr.selection }
}),

@@ -42,11 +34,3 @@

init() { return null },
applyAction(action, storedMarks, state) {
if (action.type == "transform") return action.selection ? null : storedMarks
if (action.type == "selection") return null
if (action.type == "addStoredMark" && state.selection.empty)
return action.mark.addToSet(storedMarks || currentMarks(state.doc, state.selection))
if (action.type == "removeStoredMark" && state.selection.empty)
return action.markType.removeFromSet(storedMarks || currentMarks(state.doc, state.selection))
return storedMarks
}
apply(tr, _marks, _old, state) { return state.selection.empty ? tr.storedMarks : null }
}),

@@ -56,19 +40,12 @@

init() { return 0 },
applyAction(action, prev) {
return (action.type == "transform" || action.type == "selection") && action.scrollIntoView
? prev + 1 : prev
}
apply(tr, prev) { return tr.scrolledIntoView ? prev + 1 : prev }
})
]
function currentMarks(doc, selection) {
return selection.head == null ? Mark.none : doc.marksAt(selection.head)
}
// Object wrapping the part of a state object that stays the same
// across actions. Stored in the state's `config` property.
// across transactions. Stored in the state's `config` property.
class Configuration {
constructor(schema, plugins) {
this.schema = schema
this.fields = baseFields.slice()
this.fields = baseFields.concat()
this.plugins = []

@@ -90,3 +67,3 @@ this.pluginsByKey = Object.create(null)

// but rather a new state value is computed from an old one with the
// [`applyAction`](#state.EditorState.applyAction) method.
// [`apply`](#state.EditorState.apply) method.
//

@@ -122,18 +99,71 @@ // In addition to the built-in state fields, plugins can define

// :: (Action) → EditorState
// Apply the given action to produce a new state.
applyAction(action) {
if (action.type == null) throw new RangeError("Not a valid action")
// :: (Transaction) → EditorState
// Apply the given transaction to produce a new state.
apply(tr) {
return this.applyTransaction(tr).state
}
// : (Transaction) → ?Transaction
filterTransaction(tr, ignore = -1) {
for (let i = 0; i < this.config.plugins.length; i++) if (i != ignore) {
let plugin = this.config.plugins[i]
if (plugin.options.filterTransaction && !plugin.options.filterTransaction.call(plugin, tr, this))
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.Plugin.constructor^options.filterTransaction) of
// plugins) along with the new state.
applyTransaction(tr) {
if (!this.filterTransaction(tr)) return {state: this, transactions: []}
let 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 (;;) {
let haveNew = false
for (let i = 0; i < this.config.plugins.length; i++) {
let plugin = this.config.plugins[i]
if (plugin.options.appendTransaction) {
let n = seen ? seen[i].n : 0, oldState = seen ? seen[i].state : this
let tr = n < trs.length &&
plugin.options.appendTransaction.call(plugin, n ? trs.slice(n) : trs, oldState, newState)
if (tr && newState.filterTransaction(tr, i)) {
if (!seen) {
seen = []
for (let j = 0; j < this.config.plugins.length; j++)
seen.push(j < i ? {state: newState, n: trs.length} : {state: this, n: 0})
}
trs.push(tr)
newState = newState.applyInner(tr)
haveNew = true
}
if (seen) seen[i] = {state: newState, n: trs.length}
}
}
if (!haveNew) return {state: newState, transactions: trs}
}
}
// : (Transaction) → EditorState
applyInner(tr) {
if (!tr.before.eq(this.doc)) throw new RangeError("Applying a mismatched transaction")
let newInstance = new EditorState(this.config), fields = this.config.fields
for (let i = 0; i < fields.length; i++) {
let field = fields[i]
newInstance[field.name] = field.applyAction(action, this[field.name], this, newInstance)
newInstance[field.name] = field.apply(tr, this[field.name], this, newInstance)
}
for (let i = 0; i < applyListeners.length; i++) applyListeners[i](this, action, newInstance)
for (let i = 0; i < applyListeners.length; i++) applyListeners[i](this, tr, newInstance)
return newInstance
}
// :: EditorTransform
// Create a selection-aware [`Transform` object](#state.EditorTransform).
get tr() { return new EditorTransform(this) }
// :: Transaction
// Start a [transaction](#state.Transaction) from this state.
get tr() { return new Transaction(this) }

@@ -232,70 +262,1 @@ // :: (Object) → EditorState

const applyListeners = []
// Action:: interface
// State updates are performed through actions, which are objects that
// describe the update.
//
// type:: string
// The type of this action. This determines the way the action is
// interpreted, and which other fields it should have.
// TransformAction:: interface
// An action type that transforms the state's document. Applying this
// will create a state in which the document is the result of this
// transformation.
//
// type:: "transform"
//
// transform:: Transform
//
// selection:: ?Selection
// If given, this selection will be used as the new selection. If
// not, the old selection is mapped through the transform.
//
// time:: number
// The timestamp at which the change was made.
//
// 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
// When true, the next display update will scroll the cursor into
// view.
// SelectionAction:: interface
// An action that updates the selection.
//
// type:: "selection"
//
// selection:: Selection
// The new selection.
//
// origin:: ?string
// An optional string giving more information about the source of
// the action. The [view](##view) will set this to `"mouse"` for
// selection actions originating from mouse events.
//
// scrollIntoView:: ?bool
// When true, the next display update will scroll the cursor into
// view.
//
// time:: number
// The timestamp at which the action was created.
// AddStoredMarkAction:: interface
// An action type that adds a stored mark to the state.
//
// type:: "addStoredMark"
//
// mark:: Mark
// RemoveStoredMarkAction:: interface
// An action type that removes a stored mark from the state.
//
// type:: "removeStoredMark"
//
// markType:: MarkType
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