use super::named_signals::named_signals_config;
use super::signals::effect;
use crate::extension_core::{
ExtensionBuildState, ExtensionRegisterState, LexicalExtension, define_extension,
};
use crate::{Editor, LexicalError, NodeRef, Unregister};
use std::rc::Rc;
fn delete_empty_inline(node: &NodeRef) -> Result<(), LexicalError> {
if node.is_element() && node.is_inline() && node.is_empty() {
node.remove(false)?;
if node.can_be_empty() {
crate::warnings::console_warn(&format!(
"Empty inline elements are removed from the EditorState, so returning 'true' from {}.canBeEmpty() is not allowed",
node.get_type()
));
}
}
Ok(())
}
pub fn register_normalize_inline_elements(editor: &Editor) -> Unregister {
let unregisters: Vec<Unregister> = editor
.registered_nodes()
.values()
.filter(|class| (class.is_inline_element)())
.map(|class| editor.add_node_transform(class.node_type, delete_empty_inline))
.collect();
Box::new(move || {
for unregister in unregisters {
unregister();
}
})
}
named_signals_config! {
pub struct NormalizeInlineElementsConfig;
pub struct NormalizeInlineElementsConfigOverrides;
pub struct NormalizeInlineElementsOutput {
disabled: bool,
}
}
pub type NormalizeInlineElementsExtensionType =
LexicalExtension<NormalizeInlineElementsConfig, NormalizeInlineElementsOutput, ()>;
fn make() -> Rc<NormalizeInlineElementsExtensionType> {
define_extension(LexicalExtension {
build: Some(Rc::new(
|_: &Editor, config: &NormalizeInlineElementsConfig, _: &ExtensionBuildState<()>| {
Ok(NormalizeInlineElementsOutput::of(config))
},
)),
register: Some(Rc::new(
|editor: &Editor,
_: &NormalizeInlineElementsConfig,
state: &ExtensionRegisterState<(), NormalizeInlineElementsOutput>| {
let stores = state
.get_output()
.expect("NormalizeInlineElementsExtension has a build");
let editor = editor.weak();
Ok(effect(move || {
if stores.disabled.value() {
return None;
}
let editor = editor.upgrade().map(Editor)?;
Some(register_normalize_inline_elements(&editor))
}))
},
)),
..LexicalExtension::new(
"@lexical/NormalizeInlineElements",
NormalizeInlineElementsConfig { disabled: false },
)
})
}
thread_local! {
static NORMALIZE_INLINE_ELEMENTS_EXTENSION: Rc<NormalizeInlineElementsExtensionType> = make();
}
pub fn normalize_inline_elements_extension() -> Rc<NormalizeInlineElementsExtensionType> {
NORMALIZE_INLINE_ELEMENTS_EXTENSION.with(Rc::clone)
}