pub struct Editor(/* private fields */);Expand description
A Lexical editor. A handle; cloning it shares the same editor. Without a
DOM it is what JS calls a headless editor. It is neither Send nor
Sync.
Implementations§
Source§impl Editor
impl Editor
Sourcepub const VERSION: &'static str = "lexical-rust@0.53.0"
pub const VERSION: &'static str = "lexical-rust@0.53.0"
LexicalEditor.version: the port’s own, as Swift’s is
lexical-swift@<upstream>, for the upstream release in
UPSTREAM_VERSION.
Sourcepub fn config(&self) -> &EditorConfig
pub fn config(&self) -> &EditorConfig
editor._config.
Sourcepub fn is_composing(&self) -> bool
pub fn is_composing(&self) -> bool
isComposing(): whether an input method is composing
(_compositionKey is set).
Sourcepub fn get_namespace(&self) -> &str
pub fn get_namespace(&self) -> &str
_config.namespace: the namespace the editor was created with.
Sourcepub fn parent_editor(&self) -> Option<&Editor>
pub fn parent_editor(&self) -> Option<&Editor>
_parentEditor: the editor this one is nested in
(CreateEditorArgs::parent_editor), if any.
Sourcepub fn get_key(&self) -> &str
pub fn get_key(&self) -> &str
getKey(): the editor’s own random key (createUID()), which a
drag from it is marked with ($writeDragSourceToDataTransfer).
Sourcepub fn blur(&self)
pub fn blur(&self)
blur(): the root element gives up the focus and its selection
(RootElement::blur).
Sourcepub fn focus(
&self,
callback: Option<Box<dyn FnOnce()>>,
options: EditorFocusOptions,
) -> Result<(), LexicalError>
pub fn focus( &self, callback: Option<Box<dyn FnOnce()>>, options: EditorFocusOptions, ) -> Result<(), LexicalError>
focus(callbackFn, options): with a root element, an update (tagged
FOCUS_TAG) that marks the selection dirty, so the view applies
it again, or selects the end (or, with
EditorFocusOptions::RootStart, the start) of the root when there
is none; callback runs once it commits. Without a root element it
does nothing. (JS also turns autocapitalize off on the DOM root
around the update, which only iOS’s keyboard reads.)
Sourcepub fn is_headless(&self) -> bool
pub fn is_headless(&self) -> bool
_headless: whether the editor never reconciles, as
create_headless_editor makes it
(and a nested editor of a headless one is).
Sourcepub fn is_editable(&self) -> bool
pub fn is_editable(&self) -> bool
isEditable().
Sourcepub fn set_editable(&self, editable: bool)
pub fn set_editable(&self, editable: bool)
setEditable(editable): when that changes the editor’s mode, the
editable listeners hear of it.
Sourcepub fn register_editable_listener<R: ListenerCleanup>(
&self,
listener: impl Fn(bool) -> R + 'static,
) -> Unregister
pub fn register_editable_listener<R: ListenerCleanup>( &self, listener: impl Fn(bool) -> R + 'static, ) -> Unregister
registerEditableListener(listener): listener hears each change of
is_editable.
Sourcepub fn get_decorators(&self) -> Decorators
pub fn get_decorators(&self) -> Decorators
getDecorators(): what each decorator decorated to
(LexicalNode::decorate), as the reconciler last collected it.
It is empty until the editor has a root element.
Sourcepub fn register_decorator_listener<R: ListenerCleanup>(
&self,
listener: impl Fn(&Decorators) -> R + 'static,
) -> Unregister
pub fn register_decorator_listener<R: ListenerCleanup>( &self, listener: impl Fn(&Decorators) -> R + 'static, ) -> Unregister
registerDecoratorListener(listener): listener hears each commit
that changed get_decorators, with the new
decorators.
Sourcepub fn get_root_element(&self) -> Option<RootElementRef>
pub fn get_root_element(&self) -> Option<RootElementRef>
getRootElement().
Sourcepub fn set_root_element(
&self,
next_root_element: Option<RootElementRef>,
) -> Result<(), LexicalError>
pub fn set_root_element( &self, next_root_element: Option<RootElementRef>, ) -> Result<(), LexicalError>
setRootElement(nextRootElement), its model half: the editor is
reset (its state is empty again) and the pending state, or the
current one, is committed with history-merge, the queued updates
and tags kept. With a root, every node is reconciled, so mutation
listeners hear of each as created; then the root listeners run.
Styling and events are the app’s view’s job.
Sourcepub fn register_root_listener(
&self,
listener: impl Fn(Option<&RootElementRef>, Option<&RootElementRef>) -> Option<Unregister> + 'static,
) -> Unregister
pub fn register_root_listener( &self, listener: impl Fn(Option<&RootElementRef>, Option<&RootElementRef>) -> Option<Unregister> + 'static, ) -> Unregister
registerRootListener(listener): listener is called at once with
the root element (and None), then with the next and previous root
each time it changes, and with None and the root when it is
unregistered. What it returns runs before its next call.
Sourcepub fn register_mutation_listener(
&self,
node_type: &str,
listener: impl Fn(&NodeMutations, &MutationListenerPayload) + 'static,
options: MutationListenerOptions,
) -> Result<Unregister, LexicalError>
pub fn register_mutation_listener( &self, node_type: &str, listener: impl Fn(&NodeMutations, &MutationListenerPayload) + 'static, options: MutationListenerOptions, ) -> Result<Unregister, LexicalError>
registerMutationListener(klass, listener, options), by the node
type: after each commit that the reconciler ran in (the editor has a
root element), listener gets the class’s nodes it created,
updated or destroyed. Unless skip_initialization, it is called at
once with the class’s nodes in the editor’s state as created. Fails
for a type the editor doesn’t register.
Sourcepub fn get_registered_node(&self, node_type: &str) -> Option<NodeClass>
pub fn get_registered_node(&self, node_type: &str) -> Option<NodeClass>
editor._nodes.get(type): the class the editor registers under the
type.
Sourcepub fn resolve_registered_node_after_replacements(
&self,
node_type: &str,
) -> Result<&'static str, LexicalError>
pub fn resolve_registered_node_after_replacements( &self, node_type: &str, ) -> Result<&'static str, LexicalError>
resolveRegisteredNodeAfterReplacements(registeredNode), by type:
the type of the class a registered class’s replacements end at
(following withKlass). Fails for a withKlass that isn’t
registered.
Sourcepub fn has_node(&self, node_type: &str) -> bool
pub fn has_node(&self, node_type: &str) -> bool
hasNode(klass), by the node type: whether the editor registers it.
Sourcepub fn has_nodes(&self, node_types: &[&str]) -> bool
pub fn has_nodes(&self, node_types: &[&str]) -> bool
hasNodes(klasses), by the node types: whether the editor registers
all of them.
Sourcepub fn set_selection_movement(
&self,
movement: Option<Rc<dyn SelectionMovement>>,
)
pub fn set_selection_movement( &self, movement: Option<Rc<dyn SelectionMovement>>, )
Sets what stands in for the DOM’s Selection.modify in this editor:
where a caret lands when modify, deleteCharacter, deleteWord or
deleteLine move it within text, which only text layout knows.
Without one (the default) the editor behaves as JS does with no root
element: those moves fail with window object not found.
Sourcepub fn get_editor_state(&self) -> EditorState
pub fn get_editor_state(&self) -> EditorState
getEditorState(): the committed state.
Sourcepub fn to_json(&self) -> Value
pub fn to_json(&self) -> Value
toJSON(): {editorState}, this editor’s committed state in the
form the export around it is writing
(is_compact_export), which is how a
nested editor stays in the form of the document containing it.
Sourcepub fn parse_editor_state(
&self,
json: &str,
) -> Result<EditorState, LexicalError>
pub fn parse_editor_state( &self, json: &str, ) -> Result<EditorState, LexicalError>
parseEditorState(json) from the JSON text.
Sourcepub fn parse_editor_state_json(
&self,
json: &Value,
) -> Result<EditorState, LexicalError>
pub fn parse_editor_state_json( &self, json: &Value, ) -> Result<EditorState, LexicalError>
parseEditorState(serializedEditorState) from parsed JSON.
Sourcepub fn parse_editor_state_json_with(
&self,
json: &Value,
update_fn: impl FnOnce() -> Result<(), LexicalError>,
) -> Result<EditorState, LexicalError>
pub fn parse_editor_state_json_with( &self, json: &Value, update_fn: impl FnOnce() -> Result<(), LexicalError>, ) -> Result<EditorState, LexicalError>
parseEditorState(serializedEditorState, updateFn): runs update_fn
on the parsed state before it is made read-only, as an update of
that state ($getRoot() is its root); nothing it changes is marked
dirty in this editor, nor in an update running here.
Sourcepub fn update(
&self,
update_fn: impl FnOnce() -> Result<(), LexicalError> + 'static,
options: UpdateOptions,
) -> Result<(), LexicalError>
pub fn update( &self, update_fn: impl FnOnce() -> Result<(), LexicalError> + 'static, options: UpdateOptions, ) -> Result<(), LexicalError>
update(updateFn, options). Inside another update of this editor (or
a listener of its commit) the update is queued and runs after it, as
in JS, which is why update_fn is 'static.
A non-discrete update is committed by the next read
or discrete update, where JS commits it in a microtask. An update
that fails is discarded, the editor keeps its previous state, and the
error is returned (JS reports it to onError).
Sourcepub fn set_editor_state(
&self,
editor_state: &EditorState,
tag: Option<&str>,
) -> Result<(), LexicalError>
pub fn set_editor_state( &self, editor_state: &EditorState, tag: Option<&str>, ) -> Result<(), LexicalError>
setEditorState(editorState, {tag}): makes editor_state the
editor’s state, through an update, so a state from
parse_editor_state has its text
normalized and its transforms run, as in JS. The editor keeps its
state and this returns the error when the update fails, such as for
a state with nothing but the root (an invariant in JS’s DEV build).
Sourcepub fn read<R>(&self, f: impl FnOnce() -> R) -> Result<R, LexicalError>
pub fn read<R>(&self, f: impl FnOnce() -> R) -> Result<R, LexicalError>
read(callbackFn): commits any pending update, then runs f with
the editor’s state active, read-only. Fails when committing runs a
queued update that fails.
Sourcepub fn read_with_mode<R>(
&self,
mode: EditorReadMode,
f: impl FnOnce() -> R,
) -> Result<R, LexicalError>
pub fn read_with_mode<R>( &self, mode: EditorReadMode, f: impl FnOnce() -> R, ) -> Result<R, LexicalError>
read(mode, callbackFn): read for
EditorReadMode::ForceCommit; Latest reads the committed state
without committing first, and Pending the state of the update in
progress or queued (the committed one without one), without
committing it.
Sourcepub fn register_node_transform(
&self,
node_type: &'static str,
transform: impl Fn(&NodeRef) -> Result<(), LexicalError> + 'static,
) -> Unregister
pub fn register_node_transform( &self, node_type: &'static str, transform: impl Fn(&NodeRef) -> Result<(), LexicalError> + 'static, ) -> Unregister
registerNodeTransform(klass, listener), by the node type. Nodes of
that type already in the editor are marked dirty, so the transform
runs on them with the next update. A class replaced by a
with_klass (LexicalNodeReplacement) gets it on that class too.
Sourcepub fn register_update_listener<R: ListenerCleanup>(
&self,
listener: impl Fn(&UpdateListenerPayload) -> R + 'static,
) -> Unregister
pub fn register_update_listener<R: ListenerCleanup>( &self, listener: impl Fn(&UpdateListenerPayload) -> R + 'static, ) -> Unregister
registerUpdateListener(listener).
Sourcepub fn register_text_content_listener<R: ListenerCleanup>(
&self,
listener: impl Fn(&str) -> R + 'static,
) -> Unregister
pub fn register_text_content_listener<R: ListenerCleanup>( &self, listener: impl Fn(&str) -> R + 'static, ) -> Unregister
registerTextContentListener(listener).
Sourcepub fn register_command<P: 'static>(
&self,
command: &LexicalCommand<P>,
listener: impl Fn(&P, &Editor) -> Result<bool, LexicalError> + 'static,
priority: i8,
) -> Unregister
pub fn register_command<P: 'static>( &self, command: &LexicalCommand<P>, listener: impl Fn(&P, &Editor) -> Result<bool, LexicalError> + 'static, priority: i8, ) -> Unregister
registerCommand(command, listener, priority), priority being one
of the COMMAND_PRIORITY_* constants: 0 to 4 add the listener to the
end of that priority’s queue, and the COMMAND_PRIORITY_BEFORE_*
ones (-8 to -4) to its beginning (normalizePriority’s priority & 7).
Sourcepub fn dispatch_command<P: 'static>(
&self,
command: &LexicalCommand<P>,
payload: P,
) -> Result<bool, LexicalError>
pub fn dispatch_command<P: 'static>( &self, command: &LexicalCommand<P>, payload: P, ) -> Result<bool, LexicalError>
dispatchCommand(command, payload): runs the command’s listeners, by
priority (within each, this editor’s, then its parent editors’),
until one handles it, and returns whether one did.