1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! `@lexical/headless` (`packages/lexical-headless/src`): an editor for use
//! without a view, such as on a server, which refuses a root element.
use crate::{CreateEditorArgs, Editor, LexicalError, create_editor};
use std::rc::Rc;
/// `createHeadlessEditor(editorConfig)`: [`create_editor`], marked
/// headless (`_headless`), whose [`set_root_element`](Editor::set_root_element)
/// fails with JS's message. JS also makes `registerDecoratorListener`,
/// `registerRootListener`, `registerMutationListener`, `getRootElement`,
/// `getElementByKey`, `focus` and `blur` throw; here, as in Swift, they
/// behave as on any editor without a root element, which a headless one
/// can't get.
pub fn create_headless_editor(editor_config: CreateEditorArgs) -> Editor {
let editor = create_editor(editor_config);
editor.set_headless(Rc::new(|_| {
Err(LexicalError::new(
"setRootElement is not supported in headless mode",
))
}));
editor
}
/// `HeadlessExtension.ts` (the `extension` feature).
#[cfg(feature = "extension")]
mod headless_extension {
use crate::Editor;
use crate::error::invariant;
use crate::extension_core::{ExtensionBuildState, LexicalExtension, define_extension};
use std::rc::Rc;
/// `HeadlessExtension`.
pub type HeadlessExtensionType = LexicalExtension<(), (), ()>;
fn make() -> Rc<HeadlessExtensionType> {
define_extension(LexicalExtension {
build: Some(Rc::new(
|editor: &Editor, _config: &(), _state: &ExtensionBuildState<()>| {
// `markEditorHeadless(editor)`.
editor.set_headless(Rc::new(|next_root_element| {
invariant!(
next_root_element.is_none(),
"HeadlessExtension: setRootElement is not supported in headless mode"
);
Ok(())
}));
Ok(())
},
)),
..LexicalExtension::new("@lexical/headless/Headless", ())
})
}
thread_local! {
static HEADLESS_EXTENSION: Rc<HeadlessExtensionType> = make();
}
/// `HeadlessExtension`: its `build` marks the editor headless
/// (`markEditorHeadless`), after which setting a root element fails,
/// though setting none is allowed.
pub fn headless_extension() -> Rc<HeadlessExtensionType> {
HEADLESS_EXTENSION.with(Rc::clone)
}
}
#[cfg(feature = "extension")]
pub use headless_extension::{HeadlessExtensionType, headless_extension};