prosekit/pm/state
Re-exports from prosemirror-state.
Classes
Section titled “Classes”EditorState
Section titled “EditorState”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
method.
A state holds a number of built-in fields, and plugins can define additional fields.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”-
new EditorState():
EditorState
Properties
Section titled “Properties”-
The current document.
-
storedMarks:
null
| readonlyMark
[] -
A set of marks to apply to the next input. Will be null when no explicit marks have been set.
Accessors
Section titled “Accessors”-
get plugins(): readonly
ProseMirrorPlugin
<any
>[] -
The plugins that are active in this state.
-
get tr():
Transaction
-
Start a transaction from this state.
Methods
Section titled “Methods”-
static create(
config
:EditorStateConfig
):EditorState
-
Create a new state.
-
static fromJSON(
config
:object
,json
:any
,pluginFields?
:object
):EditorState
-
Deserialize a JSON representation of a state.
config
should have at least aschema
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.
-
apply(
tr
:Transaction
):EditorState
-
Apply the given transaction to produce a new state.
-
applyTransaction(
rootTr
:Transaction
):object
-
Verbose variant of
apply
that returns the precise transactions that were applied (which might be influenced by the transaction hooks of plugins) along with the new state.
-
reconfigure(
config
: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
method, passing in the new configuration object..
-
toJSON(
pluginFields?
:object
):any
-
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. The argument may also be a string or number, in which case it is ignored, to support the way
JSON.stringify
callstoString
methods.
ProseMirrorPlugin<PluginState>
Section titled “ProseMirrorPlugin<PluginState>”Plugins bundle functionality that can be added to an editor. They are part of the editor state and may influence that state and the view that contains it.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”-
new ProseMirrorPlugin<PluginState>(
spec
:PluginSpec
<PluginState
>):ProseMirrorPlugin
<PluginState
> -
Create a plugin.
Properties
Section titled “Properties”-
readonly spec:
PluginSpec
<PluginState
> -
The plugin's spec object.
-
readonly props:
EditorProps
<ProseMirrorPlugin
<PluginState
>> -
The props exported by this plugin.
Methods
Section titled “Methods”-
getState(
state
:EditorState
):undefined
|PluginState
-
Extract the plugin's state field from an editor state.
PluginKey<PluginState>
Section titled “PluginKey<PluginState>”A key is used to tag 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.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”Methods
Section titled “Methods”-
get(
state
:EditorState
):undefined
|ProseMirrorPlugin
<PluginState
> -
Get the active plugin with this key, if any, from an editor state.
-
getState(
state
:EditorState
):undefined
|PluginState
-
Get the plugin's state from an editor state.
Transaction
Section titled “Transaction”An editor state transaction, which can be applied to a state to create an updated state. Use EditorState.tr
to create an instance.
Transactions track changes to the document (they are a subclass of Transform
), but also other state changes, like selection updates and adjustments of the set of stored marks. 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 transaction represents, so that they can update their own state accordingly.
The editor view 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, a "composition"
property holding an ID identifying the composition that caused it to transactions caused by composed DOM input, and a "uiEvent"
property of that may be "paste"
, "cut"
, or "drop"
.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”-
new Transaction(
doc
:ProseMirrorNode
):Transaction
-
Create a transform that starts with the given document.
Properties
Section titled “Properties”-
time:
number
-
The timestamp associated with this transaction, in the same format as
Date.now()
.
-
storedMarks:
null
| readonlyMark
[] -
The stored marks set by this transaction, if any.
-
The current document (the result of applying the steps in the transform).
-
readonly docs:
ProseMirrorNode
[] -
The documents before each of the steps.
Accessors
Section titled “Accessors”-
The transaction's current selection. This defaults to the editor selection mapped through the steps in the transaction, but can be overwritten with
setSelection
.
-
get selectionSet():
boolean
-
Whether the selection was explicitly updated by this transaction.
-
get storedMarksSet():
boolean
-
Whether the stored marks were explicitly set for this transaction.
-
get isGeneric():
boolean
-
Returns true if this transaction doesn't contain any metadata, and can thus safely be extended.
-
get scrolledIntoView():
boolean
-
True when this transaction has had
scrollIntoView
called on it.
-
get before():
ProseMirrorNode
-
The starting document.
-
get docChanged():
boolean
-
True when the document has been changed (when there are any steps).
Methods
Section titled “Methods”-
setSelection(
selection
:Selection
):this
-
Update the transaction's current selection. Will determine the selection that the editor gets when the transaction is applied.
-
setStoredMarks(
marks
:null
| readonlyMark
[]):this
-
Set the current stored marks.
-
ensureMarks(
marks
: readonlyMark
[]):this
-
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.
-
addStoredMark(
mark
:Mark
):this
-
Add a mark to the set of stored marks.
-
removeStoredMark(
mark
:MarkType
|Mark
):this
-
Remove a mark or mark type from the set of stored marks.
-
setTime(
time
:number
):this
-
Update the timestamp for the transaction.
-
replaceSelection(
slice
:Slice
):this
-
Replace the current selection with the given slice.
-
replaceSelectionWith(
node
:ProseMirrorNode
,inheritMarks?
:boolean
):this
-
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.
-
deleteSelection():
this
-
Delete the selection.
-
insertText(
text
:string
,from?
:number
,to?
:number
):this
-
Replace the given range, or the selection if no range is given, with a text node containing the given string.
-
setMeta(
key
:string
|ProseMirrorPlugin
<any
> |PluginKey
<any
>,value
:any
):this
-
Store a metadata property in this transaction, keyed either by name or by plugin.
-
getMeta(
key
:string
|ProseMirrorPlugin
<any
> |PluginKey
<any
>):any
-
Retrieve a metadata property for a given name or plugin.
-
scrollIntoView():
this
-
Indicate that the editor should scroll the selection into view when updated to the state produced by this transaction.
-
Apply a new step in this transform, saving the result. Throws an error when the step fails.
-
maybeStep(
step
:Step
):StepResult
-
Try to apply a step in this transformation, ignoring it if it fails. Returns the step result.
-
Replace the part of the document between
from
andto
with the givenslice
.
-
replaceWith(
from
:number
,to
:number
,content
:ProseMirrorNode
|ProseMirrorFragment
| readonlyProseMirrorNode
[]):this
-
Replace the given range with the given content, which may be a fragment, node, or array of nodes.
-
delete(
from
:number
,to
:number
):this
-
Delete the content between the given positions.
-
insert(
pos
:number
,content
:ProseMirrorNode
|ProseMirrorFragment
| readonlyProseMirrorNode
[]):this
-
Insert the given content at the given position.
-
replaceRange(
from
:number
,to
:number
,slice
:Slice
):this
-
Replace a range of the document with a given slice, using
from
,to
, and the slice'sopenStart
property as hints, rather than fixed start and end points. This method may grow the replaced area or close open nodes in the slice in order to get a fit that is more in line with WYSIWYG expectations, by dropping fully covered parent nodes of the replaced region when they are marked non-defining as context, or including an open parent node from the slice that is marked as defining its content.This is the method, for example, to handle paste. The similar
replace
method is a more primitive tool which will not move the start and end of its given range, and is useful in situations where you need more precise control over what happens.
-
replaceRangeWith(
from
:number
,to
:number
,node
:ProseMirrorNode
):this
-
Replace the given range with a node, but use
from
andto
as hints, rather than precise positions. When from and to are the same and are at the start or end of a parent node in which the given node doesn't fit, this method may move them out towards a parent that does allow the given node to be placed. When the given range completely covers a parent node, this method may completely replace that parent node.
-
deleteRange(
from
:number
,to
:number
):this
-
Delete the given range, expanding it to cover fully covered parent nodes until a valid replace is found.
-
Split the content in the given range off from its parent, if there is sibling content before or after it, and move it up the tree to the depth specified by
target
. You'll probably want to useliftTarget
to computetarget
, to make sure the lift is valid.
-
join(
pos
:number
,depth?
:number
):this
-
Join the blocks around the given position. If depth is 2, their last and first siblings are also joined, and so on.
-
Wrap the given range in the given set of wrappers. The wrappers are assumed to be valid in this position, and should probably be computed with
findWrapping
.
-
setBlockType(
from
:number
,to
:undefined
|number
,type
:NodeType
,attrs?
:null
|Attrs
| (oldNode
:ProseMirrorNode
) =>Attrs
):this
-
Set the type of all textblocks (partly) between
from
andto
to the given node type with the given attributes.
-
setNodeMarkup(
pos
:number
,type?
:null
|NodeType
,attrs?
:null
|Attrs
,marks?
: readonlyMark
[]):this
-
Change the type, attributes, and/or marks of the node at
pos
. Whentype
isn't given, the existing node type is preserved,
-
setNodeAttribute(
pos
:number
,attr
:string
,value
:any
):this
-
Set a single attribute on a given node to a new value. The
pos
addresses the document content. UsesetDocAttribute
to set attributes on the document itself.
-
setDocAttribute(
attr
:string
,value
:any
):this
-
Set a single attribute on the document to a new value.
-
addNodeMark(
pos
:number
,mark
:Mark
):this
-
Add a mark to the node at position
pos
.
-
removeNodeMark(
pos
:number
,mark
:MarkType
|Mark
):this
-
Remove a mark (or all marks of the given type) from the node at position
pos
.
-
split(
pos
:number
,depth?
:number
,typesAfter?
: (null
| {type
:NodeType
;attrs?
:null
|Attrs
; })[]):this
-
Split the node at the given position, and optionally, if
depth
is greater than one, any number of nodes above that. By default, the parts split off will inherit the node type of the original node. This can be changed by passing an array of types and attributes to use after the split (with the outermost nodes coming first).
-
Add the given mark to the inline content between
from
andto
.
-
removeMark(
from
:number
,to
:number
,mark?
:null
|MarkType
|Mark
):this
-
Remove marks from inline nodes between
from
andto
. Whenmark
is a single mark, remove precisely that mark. When it is a mark type, remove all marks of that type. When it is null, remove all marks of any type.
-
clearIncompatible(
pos
:number
,parentType
:NodeType
,match?
:ContentMatch
):this
-
Removes all marks and nodes from the content of the node at
pos
that don't match the given new parent node type. Accepts an optional starting content match as third argument.
abstract Selection
Section titled “abstract Selection”Superclass for editor selections. Every selection type should extend this. Should not be instantiated directly.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”-
new Selection(
$anchor
:ResolvedPos
,$head
:ResolvedPos
,ranges?
: readonlySelectionRange
[]):Selection
-
Initialize a selection with the head and anchor and ranges. If no ranges are given, constructs a single range across
$anchor
and$head
.
Properties
Section titled “Properties”-
readonly $anchor:
ResolvedPos
-
The resolved anchor of the selection (the side that stays in place when the selection is modified).
-
readonly $head:
ResolvedPos
-
The resolved head of the selection (the side that moves when the selection is modified).
-
ranges: readonly
SelectionRange
[] -
The ranges covered by the selection.
-
visible:
boolean
-
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
.
Accessors
Section titled “Accessors”-
get anchor():
number
-
The selection's anchor, as an unresolved position.
-
get head():
number
-
The selection's head.
-
get from():
number
-
The lower bound of the selection's main range.
-
get to():
number
-
The upper bound of the selection's main range.
-
get $from():
ResolvedPos
-
The resolved lower bound of the selection's main range.
-
get $to():
ResolvedPos
-
The resolved upper bound of the selection's main range.
-
get empty():
boolean
-
Indicates whether the selection contains any content.
Methods
Section titled “Methods”-
Test whether the selection is the same as another selection.
-
abstract map(
doc
:ProseMirrorNode
,mapping
:Mappable
):Selection
-
Map this selection through a mappable thing.
doc
should be the new document to which we are mapping.
-
replace(
tr
:Transaction
,content?
:Slice
):void
-
Replace the selection with a slice or, if no slice is given, delete the selection. Will append to the given transaction.
-
replaceWith(
tr
:Transaction
,node
:ProseMirrorNode
):void
-
Replace the selection with the given node, appending the changes to the given transaction.
-
abstract toJSON():
any
-
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 your class.
-
static findFrom(
$pos
:ResolvedPos
,dir
:number
,textOnly?
:boolean
):null
|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. WhentextOnly
is true, only consider cursor selections. Will return null when no valid selection position is found.
-
static near(
$pos
:ResolvedPos
,bias?
: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.
-
static atStart(
doc
:ProseMirrorNode
):Selection
-
Find the cursor or leaf node selection closest to the start of the given document. Will return an
AllSelection
if no valid position exists.
-
static atEnd(
doc
:ProseMirrorNode
):Selection
-
Find the cursor or leaf node selection closest to the end of the given document.
-
static fromJSON(
doc
:ProseMirrorNode
,json
:any
):Selection
-
Deserialize the JSON representation of a selection. Must be implemented for custom classes (as a static class method).
-
static jsonID(
id
:string
,selectionClass
:object
):object
-
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.
-
Get a bookmark 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.
SelectionRange
Section titled “SelectionRange”Represents a selected range in a document.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”-
new SelectionRange(
$from
:ResolvedPos
,$to
:ResolvedPos
):SelectionRange
-
Create a range.
Properties
Section titled “Properties”-
readonly $from:
ResolvedPos
-
The lower bound of the range.
-
readonly $to:
ResolvedPos
-
The upper bound of the range.
TextSelection
Section titled “TextSelection”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).
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”-
new TextSelection(
$anchor
:ResolvedPos
,$head?
:ResolvedPos
):TextSelection
-
Construct a text selection between the given points.
Properties
Section titled “Properties”-
readonly $anchor:
ResolvedPos
-
The resolved anchor of the selection (the side that stays in place when the selection is modified).
-
readonly $head:
ResolvedPos
-
The resolved head of the selection (the side that moves when the selection is modified).
-
ranges: readonly
SelectionRange
[] -
The ranges covered by the selection.
-
visible:
boolean
-
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
.
Accessors
Section titled “Accessors”-
get anchor():
number
-
The selection's anchor, as an unresolved position.
-
get head():
number
-
The selection's head.
-
get from():
number
-
The lower bound of the selection's main range.
-
get to():
number
-
The upper bound of the selection's main range.
-
get $from():
ResolvedPos
-
The resolved lower bound of the selection's main range.
-
get $to():
ResolvedPos
-
The resolved upper bound of the selection's main range.
-
get empty():
boolean
-
Indicates whether the selection contains any content.
-
get $cursor():
null
|ResolvedPos
-
Returns a resolved position if this is a cursor selection (an empty text selection), and null otherwise.
Methods
Section titled “Methods”-
replaceWith(
tr
:Transaction
,node
:ProseMirrorNode
):void
-
Replace the selection with the given node, appending the changes to the given transaction.
-
static findFrom(
$pos
:ResolvedPos
,dir
:number
,textOnly?
:boolean
):null
|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. WhentextOnly
is true, only consider cursor selections. Will return null when no valid selection position is found.
-
static near(
$pos
:ResolvedPos
,bias?
: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.
-
static atStart(
doc
:ProseMirrorNode
):Selection
-
Find the cursor or leaf node selection closest to the start of the given document. Will return an
AllSelection
if no valid position exists.
-
static atEnd(
doc
:ProseMirrorNode
):Selection
-
Find the cursor or leaf node selection closest to the end of the given document.
-
static fromJSON(
doc
:ProseMirrorNode
,json
:any
):Selection
-
Deserialize the JSON representation of a selection. Must be implemented for custom classes (as a static class method).
-
static jsonID(
id
:string
,selectionClass
:object
):object
-
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.
-
map(
doc
:ProseMirrorNode
,mapping
:Mappable
):Selection
-
Map this selection through a mappable thing.
doc
should be the new document to which we are mapping.
-
replace(
tr
:Transaction
,content?
:Slice
):void
-
Replace the selection with a slice or, if no slice is given, delete the selection. Will append to the given transaction.
-
Get a bookmark 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.
-
toJSON():
any
-
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 your class.
-
static create(
doc
:ProseMirrorNode
,anchor
:number
,head?
:number
):TextSelection
-
Create a text selection from non-resolved positions.
-
static between(
$anchor
:ResolvedPos
,$head
:ResolvedPos
,bias?
: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 callingSelection.near
when the document doesn't contain a valid text position.
NodeSelection
Section titled “NodeSelection”A node selection is a selection that points at a single node. All nodes marked 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
..
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”-
new NodeSelection(
$pos
:ResolvedPos
):NodeSelection
-
Create a node selection. Does not verify the validity of its argument.
Properties
Section titled “Properties”-
readonly $anchor:
ResolvedPos
-
The resolved anchor of the selection (the side that stays in place when the selection is modified).
-
readonly $head:
ResolvedPos
-
The resolved head of the selection (the side that moves when the selection is modified).
-
ranges: readonly
SelectionRange
[] -
The ranges covered by the selection.
-
visible:
boolean
-
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
.
-
The selected node.
Accessors
Section titled “Accessors”-
get anchor():
number
-
The selection's anchor, as an unresolved position.
-
get head():
number
-
The selection's head.
-
get from():
number
-
The lower bound of the selection's main range.
-
get to():
number
-
The upper bound of the selection's main range.
-
get $from():
ResolvedPos
-
The resolved lower bound of the selection's main range.
-
get $to():
ResolvedPos
-
The resolved upper bound of the selection's main range.
-
get empty():
boolean
-
Indicates whether the selection contains any content.
Methods
Section titled “Methods”-
replace(
tr
:Transaction
,content?
:Slice
):void
-
Replace the selection with a slice or, if no slice is given, delete the selection. Will append to the given transaction.
-
replaceWith(
tr
:Transaction
,node
:ProseMirrorNode
):void
-
Replace the selection with the given node, appending the changes to the given transaction.
-
static findFrom(
$pos
:ResolvedPos
,dir
:number
,textOnly?
:boolean
):null
|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. WhentextOnly
is true, only consider cursor selections. Will return null when no valid selection position is found.
-
static near(
$pos
:ResolvedPos
,bias?
: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.
-
static atStart(
doc
:ProseMirrorNode
):Selection
-
Find the cursor or leaf node selection closest to the start of the given document. Will return an
AllSelection
if no valid position exists.
-
static atEnd(
doc
:ProseMirrorNode
):Selection
-
Find the cursor or leaf node selection closest to the end of the given document.
-
static fromJSON(
doc
:ProseMirrorNode
,json
:any
):Selection
-
Deserialize the JSON representation of a selection. Must be implemented for custom classes (as a static class method).
-
static jsonID(
id
:string
,selectionClass
:object
):object
-
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.
-
map(
doc
:ProseMirrorNode
,mapping
:Mappable
):Selection
-
Map this selection through a mappable thing.
doc
should be the new document to which we are mapping.
-
toJSON():
any
-
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 your class.
-
Get a bookmark 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.
-
static create(
doc
:ProseMirrorNode
,from
:number
):NodeSelection
-
Create a node selection from non-resolved positions.
-
static isSelectable(
node
:ProseMirrorNode
):boolean
-
Determines whether the given node may be selected as a node selection.
AllSelection
Section titled “AllSelection”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).
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”-
new AllSelection(
doc
:ProseMirrorNode
):AllSelection
-
Create an all-selection over the given document.
Properties
Section titled “Properties”-
readonly $anchor:
ResolvedPos
-
The resolved anchor of the selection (the side that stays in place when the selection is modified).
-
readonly $head:
ResolvedPos
-
The resolved head of the selection (the side that moves when the selection is modified).
-
ranges: readonly
SelectionRange
[] -
The ranges covered by the selection.
-
visible:
boolean
-
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
.
Accessors
Section titled “Accessors”-
get anchor():
number
-
The selection's anchor, as an unresolved position.
-
get head():
number
-
The selection's head.
-
get from():
number
-
The lower bound of the selection's main range.
-
get to():
number
-
The upper bound of the selection's main range.
-
get $from():
ResolvedPos
-
The resolved lower bound of the selection's main range.
-
get $to():
ResolvedPos
-
The resolved upper bound of the selection's main range.
-
get empty():
boolean
-
Indicates whether the selection contains any content.
Methods
Section titled “Methods”-
replaceWith(
tr
:Transaction
,node
:ProseMirrorNode
):void
-
Replace the selection with the given node, appending the changes to the given transaction.
-
static findFrom(
$pos
:ResolvedPos
,dir
:number
,textOnly?
:boolean
):null
|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. WhentextOnly
is true, only consider cursor selections. Will return null when no valid selection position is found.
-
static near(
$pos
:ResolvedPos
,bias?
: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.
-
static atStart(
doc
:ProseMirrorNode
):Selection
-
Find the cursor or leaf node selection closest to the start of the given document. Will return an
AllSelection
if no valid position exists.
-
static atEnd(
doc
:ProseMirrorNode
):Selection
-
Find the cursor or leaf node selection closest to the end of the given document.
-
static fromJSON(
doc
:ProseMirrorNode
,json
:any
):Selection
-
Deserialize the JSON representation of a selection. Must be implemented for custom classes (as a static class method).
-
static jsonID(
id
:string
,selectionClass
:object
):object
-
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.
-
replace(
tr
:Transaction
,content?
:Slice
):void
-
Replace the selection with a slice or, if no slice is given, delete the selection. Will append to the given transaction.
-
toJSON():
any
-
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 your class.
-
map(
doc
:ProseMirrorNode
):AllSelection
-
Map this selection through a mappable thing.
doc
should be the new document to which we are mapping.
-
getBookmark():
object
-
Get a bookmark 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.
Interfaces
Section titled “Interfaces”EditorStateConfig
Section titled “EditorStateConfig”The type of object passed to EditorState.create
.
Properties
Section titled “Properties”-
The starting document. Either this or
schema
must be provided.
-
storedMarks?:
null
| readonlyMark
[] -
The initial set of stored marks.
-
plugins?: readonly
ProseMirrorPlugin
<any
>[] -
The plugins that should be active in this state.
PluginSpec<PluginState>
Section titled “PluginSpec<PluginState>”This is the type passed to the Plugin
constructor. It provides a definition for a plugin.
Indexable
Section titled “Indexable”-
[key:
string
]:any
-
Additional properties are allowed on plugin specs, which can be read via
Plugin.spec
.
Properties
Section titled “Properties”-
props?:
EditorProps
<ProseMirrorPlugin
<PluginState
>> -
The view props added by this plugin. Props that are functions will be bound to have the plugin instance as their
this
binding.
-
state?:
StateField
<PluginState
> -
Allows a plugin to define a state field, an extra slot in the state object in which it can keep its own data.
-
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?: (
view
:EditorView
) =>PluginView
-
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.
-
filterTransaction?: (
tr
:Transaction
,state
:EditorState
) =>boolean
-
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
: readonlyTransaction
[],oldState
:EditorState
,newState
:EditorState
) =>undefined
|null
|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.
StateField<T>
Section titled “StateField<T>”A plugin spec 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.
Properties
Section titled “Properties”-
init: (
config
:EditorStateConfig
,instance
:EditorState
) =>T
-
Initialize the value of the field.
config
will be the object passed toEditorState.create
. Note thatinstance
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
) =>any
-
Convert this field to JSON. Optional, can be left off to disable JSON serialization for the field.
-
fromJSON?: (
config
:EditorStateConfig
,value
:any
,state
:EditorState
) =>T
-
Deserialize the JSON representation of this field. Note that the
state
argument is again a half-initialized state.
SelectionBookmark
Section titled “SelectionBookmark”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.
Properties
Section titled “Properties”-
map: (
mapping
:Mappable
) =>SelectionBookmark
-
Map the bookmark through a set of changes.
-
resolve: (
doc
:ProseMirrorNode
) =>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
) if mapping made the bookmark invalid.
Type Aliases
Section titled “Type Aliases”PluginView
Section titled “PluginView” type PluginView = {
update?
: (view
: EditorView
, prevState
: EditorState
) => void
; destroy?
: () => void
; }
A stateful object that can be installed in an editor by a plugin.
Properties
Section titled “Properties”-
update?: (
view
:EditorView
,prevState
:EditorState
) =>void
-
Called whenever the view's state is updated.
-
destroy?: () =>
void
-
Called when the view is destroyed or receives a state with different plugins.
Command()
Section titled “Command()”-
type Command = (
state
:EditorState
,dispatch?
: (tr
:Transaction
) =>void
,view?
:EditorView
) =>boolean
-
Commands are functions that take a state and a an optional transaction dispatch function and...
- determine whether they apply to this state
- if not, return false
- if
dispatch
was passed, perform their effect, possibly by passing a transaction todispatch
- return true
In some cases, the editor view is passed as a third argument.
References
Section titled “References”Plugin
Section titled “Plugin”Renames and re-exports ProseMirrorPlugin