Skip to content

prosekit/core/test

TestEditor

An editor for testing purposes.

constructor

new TestEditor<E extends Extension>(instance: EditorInstance): TestEditor<E>

get commands(): ToCommandAction<{[KeyType in string | number | symbol]: UnionToIntersection<ExtractTyping<E>["Commands"] extends undefined ? never : ExtractTyping<E>["Commands"]>[KeyType]}>

All CommandActions defined by the editor.

get focused(): boolean

Whether the editor is focused.

get marks(): ToMarkAction<SimplifyDeeper<{[KeyType in string | number | symbol]: UnionToIntersection<ExtractTyping<E>["Marks"] extends undefined ? never : ExtractTyping<E>["Marks"]>[KeyType]}>>

All MarkActions defined by the editor.

get mounted(): boolean

Whether the editor is mounted.

get nodes(): ToNodeAction<SimplifyDeeper<{[KeyType in string | number | symbol]: UnionToIntersection<ExtractTyping<E>["Nodes"] extends undefined ? never : ExtractTyping<E>["Nodes"]>[KeyType]}>>

All NodeActions defined by the editor.

get schema(): Schema<ExtractNodeNames<E>, ExtractMarkNames<E>>

The editor schema.

get state(): EditorState

The editor's current state.

get view(): EditorView

The editor view.

blur

Blur the editor.

ts
const blur: () => void

canExec

Check if the given command can be executed. Return true if the command can be executed, otherwise false.

ts
const canExec: (command: Command) => boolean

dispatchEvent

ts
const dispatchEvent: (event: Event) => void

exec

Execute the given command. Return true if the command was successfully executed, otherwise false.

ts
const exec: (command: Command) => boolean

focus

Focus the editor.

ts
const focus: () => void

mount

Mount the editor to the given HTML element. Pass null or undefined to unmount the editor.

ts
const mount: (place: undefined | null | HTMLElement) => void

set

Set the editor state to the given document. You can use special tokens <a> and <b> to set the anchor and head positions of the selection.

Example

ts
const editor = createTestEditor({ extension })
const n = editor.nodes
const doc = n.doc(n.paragraph('<a>Hello<b> world!'))
editor.set(doc) // "Hello" is selected.
ts
const set: (doc: Node) => void

setContent

Update the editor's document and selection.

ts
const setContent: (content: string | Node | NodeJSON | HTMLElement, selection?: Selection | "start" | SelectionJSON | "end") => void

unmount

Unmount the editor. This is equivalent to mount(null).

ts
const unmount: () => void

updateState

Update the editor's state.

Remarks

This is an advanced method. Use it only if you have a specific reason to directly manipulate the editor's state.

ts
const updateState: (state: EditorState) => void

use

Register an extension to the editor. Return a function to unregister the extension.

ts
const use: (extension: Extension) => VoidFunction

createTestEditor

ts
function createTestEditor<E extends Extension>(options: EditorOptions<E>): TestEditor<E>