use super::editor_state_extension::editor_state_extension;
use super::node_selection_extension::node_selection_extension;
use crate::extension_core::{LexicalExtension, define_extension};
use crate::generated_json::{export_compact_line_break_node, export_line_break_node};
use crate::json::Object;
use crate::lexical_utils::set_node_key;
use crate::node::{LexicalNode, NodeBase, NodeRef};
use crate::utils::insert_node_to_nearest_root;
use crate::{
CLICK_COMMAND, COMMAND_PRIORITY_EDITOR, COMMAND_PRIORITY_LOW, LexicalCommand, LexicalError,
NodeClass, create_node_selection, get_node_by_key, get_selection, merge_register,
set_selection,
};
use std::rc::Rc;
pub const INSERT_HORIZONTAL_RULE_COMMAND: LexicalCommand<()> =
LexicalCommand::constant(u64::MAX - 57, "INSERT_HORIZONTAL_RULE_COMMAND");
#[derive(Clone, Debug, Default)]
pub struct HorizontalRuleNode {
base: NodeBase,
}
pub fn create_horizontal_rule_node() -> NodeRef {
set_node_key(Box::new(HorizontalRuleNode::default()))
}
pub fn is_horizontal_rule_node(node: &NodeRef) -> bool {
node.get_latest().is::<HorizontalRuleNode>()
}
impl LexicalNode for HorizontalRuleNode {
fn get_type(&self) -> &'static str {
"horizontalrule"
}
#[cfg(feature = "html")]
fn create_dom(
&self,
_this: &NodeRef,
config: &crate::EditorConfig,
_editor: &crate::Editor,
) -> Result<crate::dom::DomNode, crate::LexicalError> {
let element = crate::lexical_dom::get_document().create_element("hr");
crate::lexical_dom::add_theme_class_names(&element, &config.theme, "hr");
Ok(element)
}
#[cfg(feature = "html")]
fn export_dom(
&self,
_this: &NodeRef,
_editor: &crate::Editor,
) -> Result<crate::lexical_dom::DomExportOutput, crate::LexicalError> {
Ok(crate::lexical_dom::DomExportOutput::element(
crate::lexical_dom::get_document().create_element("hr"),
))
}
#[cfg(feature = "html")]
fn import_dom(&self) -> Option<crate::lexical_dom::DomConversionMap> {
Some(vec![(
"hr".to_owned(),
crate::lexical_dom::importer(0, |_| {
Ok(Some(crate::lexical_dom::DomConversionOutput::node(Some(
create_horizontal_rule_node(),
))))
}),
)])
}
fn base(&self) -> &NodeBase {
&self.base
}
fn base_mut(&mut self) -> &mut NodeBase {
&mut self.base
}
fn is_decorator(&self) -> bool {
true
}
fn is_inline(&self) -> bool {
false
}
fn get_text_content(&self) -> String {
"\n".to_owned()
}
fn generated_export_json(&self, compact: bool) -> Option<Object> {
Some(if compact {
export_compact_line_break_node(self.get_type())
} else {
export_line_break_node(self.get_type())
})
}
fn generated_update_from_json(&mut self, _json: &Object) -> bool {
true
}
}
fn toggle_node_selection(node: &NodeRef, shift_key: bool) -> Result<(), LexicalError> {
let selection = get_selection();
let was_selected = node.is_selected(None)?;
let key = node.get_key();
let node_selection = match selection {
Some(selection) if shift_key && selection.is_node() => selection,
_ => {
let node_selection = create_node_selection();
set_selection(Some(node_selection.clone()));
node_selection
}
};
if was_selected {
node_selection.delete(key);
} else {
node_selection.add(key);
}
Ok(())
}
pub type HorizontalRuleExtensionType = LexicalExtension;
fn make() -> Rc<HorizontalRuleExtensionType> {
define_extension(LexicalExtension {
dependencies: vec![
editor_state_extension().into(),
node_selection_extension().into(),
],
nodes: vec![NodeClass::of::<HorizontalRuleNode>()],
register: Some(Rc::new(|editor, _, _| {
Ok(merge_register(vec![
editor.register_command(
&INSERT_HORIZONTAL_RULE_COMMAND,
|_, _| {
let Some(range) = get_selection().and_then(|s| s.range()) else {
return Ok(false);
};
range.focus.get_node()?;
insert_node_to_nearest_root(&create_horizontal_rule_node())?;
Ok(true)
},
COMMAND_PRIORITY_EDITOR,
),
editor.register_command(
&CLICK_COMMAND,
|event, _| {
let node = event.target_key.as_deref().and_then(get_node_by_key);
if let Some(node) = node.filter(is_horizontal_rule_node) {
toggle_node_selection(&node, event.shift_key)?;
return Ok(true);
}
Ok(false)
},
COMMAND_PRIORITY_LOW,
),
]))
})),
..LexicalExtension::new("@lexical/extension/HorizontalRule", ())
})
}
thread_local! {
static HORIZONTAL_RULE_EXTENSION: Rc<HorizontalRuleExtensionType> = make();
}
pub fn horizontal_rule_extension() -> Rc<HorizontalRuleExtensionType> {
HORIZONTAL_RULE_EXTENSION.with(Rc::clone)
}