use super::generated_json::{export_compact_mark_node, export_mark_node, update_mark_node};
use crate::json::Object;
use crate::lexical_selection::SelectionRef;
use crate::lexical_utils::{set_node_key, with_writable};
use crate::node::{LexicalNode, NodeBase, NodeRef};
use crate::nodes::element::ElementData;
use crate::schema::{NodeSchema, array_value, node_schema, string_value, with_field};
use crate::{LexicalError, utf16};
#[derive(Clone, Debug, Default)]
pub struct MarkNode {
base: NodeBase,
pub(super) element: ElementData,
pub(super) ids: Vec<String>,
}
impl MarkNode {
pub fn ids(&self) -> &[String] {
&self.ids
}
}
pub fn create_mark_node(ids: &[String]) -> NodeRef {
crate::lexical_utils::replaced(set_node_key(Box::new(MarkNode {
ids: ids.to_vec(),
..MarkNode::default()
})))
}
pub fn is_mark_node(node: &NodeRef) -> bool {
node.get_latest().is::<MarkNode>()
}
fn with_ids(node: &NodeRef, f: impl FnOnce(&mut Vec<String>)) {
if is_mark_node(node) {
with_writable(node.get_key(), |n| {
f(&mut n.downcast_mut::<MarkNode>().expect("a mark").ids)
});
}
}
impl NodeRef {
pub fn has_id(&self, id: &str) -> bool {
self.get_ids()
.is_some_and(|ids| ids.iter().any(|i| i == id))
}
pub fn get_ids(&self) -> Option<Vec<String>> {
self.get_latest()
.downcast_ref::<MarkNode>()
.map(|m| m.ids.clone())
}
pub fn set_ids(&self, ids: &[String]) {
with_ids(self, |i| *i = ids.to_vec());
}
pub fn add_id(&self, id: &str) {
with_ids(self, |ids| {
if !ids.iter().any(|i| i == id) {
ids.push(id.to_owned());
}
});
}
pub fn delete_id(&self, id: &str) {
with_ids(self, |ids| {
if let Some(idx) = ids.iter().position(|i| i == id) {
ids.remove(idx);
}
});
}
}
thread_local! {
static MARK_NODE_SCHEMA: NodeSchema = node_schema(vec![(
"ids",
with_field(
array_value(string_value("")).expect("an array of strings"),
|n: &MarkNode| &n.ids,
|n: &mut MarkNode| &mut n.ids,
)
.expect("a field"),
)])
.expect("MarkNode's schema");
}
pub fn mark_node_schema() -> NodeSchema {
MARK_NODE_SCHEMA.with(Clone::clone)
}
impl LexicalNode for MarkNode {
fn json_schema_chain(&self) -> Vec<NodeSchema> {
vec![
mark_node_schema(),
crate::nodes::element::element_node_schema(),
]
}
fn static_node_config_chain(&self) -> Vec<crate::structure::NodeConfigLink> {
use crate::structure::{NodeChildrenConfig, NodeConfigLink, StaticNodeConfig};
vec![NodeConfigLink::new(
"mark",
StaticNodeConfig {
inline: Some(true),
children: Some(NodeChildrenConfig::Inline),
..StaticNodeConfig::default()
},
)]
}
fn get_type(&self) -> &'static str {
"mark"
}
fn base(&self) -> &NodeBase {
&self.base
}
fn base_mut(&mut self) -> &mut NodeBase {
&mut self.base
}
fn element(&self) -> Option<&ElementData> {
Some(&self.element)
}
fn element_mut(&mut self) -> Option<&mut ElementData> {
Some(&mut self.element)
}
#[cfg(feature = "html")]
fn create_dom(
&self,
_this: &NodeRef,
config: &crate::EditorConfig,
_editor: &crate::Editor,
) -> Result<crate::dom::DomNode, crate::LexicalError> {
use crate::lexical_dom::{add_theme_class_names, get_document};
let element = get_document().create_element("mark");
add_theme_class_names(&element, &config.theme, "mark");
if self.ids.len() > 1 {
add_theme_class_names(&element, &config.theme, "markOverlap");
}
Ok(element)
}
fn is_inline(&self) -> bool {
true
}
fn can_be_empty(&self) -> bool {
false
}
fn can_insert_text_before(&self) -> bool {
false
}
fn can_insert_text_after(&self) -> bool {
false
}
fn insert_new_after(
&self,
this: &NodeRef,
_: &SelectionRef,
restore_selection: bool,
) -> Result<Option<NodeRef>, LexicalError> {
let mark_node = create_mark_node(&self.ids);
this.insert_after(&mark_node, restore_selection)?;
Ok(Some(mark_node))
}
fn extract_with_child(
&self,
this: &NodeRef,
_: &NodeRef,
selection: Option<&SelectionRef>,
destination: &str,
) -> Result<bool, LexicalError> {
let Some(selection) = selection.filter(|s| s.is_range()) else {
return Ok(false);
};
if destination == "html" {
return Ok(false);
}
let range = selection.range().expect("a range selection");
let anchor_node = range.anchor.get_node()?;
let focus_node = range.focus.get_node()?;
let (anchor, focus) = (range.anchor.offset as i64, range.focus.offset as i64);
let selection_length = if selection.is_backward()? {
anchor - focus
} else {
focus - anchor
};
Ok(this.is_parent_of(&anchor_node)
&& this.is_parent_of(&focus_node)
&& utf16::len(&this.get_text_content()) as i64 == selection_length)
}
fn exclude_from_copy(&self, destination: &str) -> bool {
destination != "clone"
}
fn generated_export_json(&self, compact: bool) -> Option<Object> {
Some(if compact {
export_compact_mark_node(self)
} else {
export_mark_node(self)
})
}
fn generated_update_from_json(&mut self, json: &Object) -> bool {
update_mark_node(self, json);
true
}
}
#[cfg(all(test, feature = "html"))]
mod tests {
use crate::json::Value;
use crate::{CreateEditorArgs, create_editor};
#[test]
fn create_dom_adds_the_theme_classes() {
let mut theme = crate::json::Object::new();
theme.insert("mark", Value::from("m"));
theme.insert("markOverlap", Value::from("o"));
let editor = create_editor(CreateEditorArgs {
nodes: super::super::mark_nodes(),
theme,
..CreateEditorArgs::default()
});
let html = |ids: &[&str]| {
let ids: Vec<String> = ids.iter().map(|id| id.to_string()).collect();
let mark = super::MarkNode {
ids,
..super::MarkNode::default()
};
crate::node::LexicalNode::create_dom(
&mark,
&crate::lexical_utils::get_root(),
editor.config(),
&editor,
)
.map(|dom| dom.outer_html())
};
let read_editor = editor.clone();
let classes = read_editor
.read(|| [html(&[]), html(&["a"]), html(&["a", "b"])])
.unwrap()
.map(Result::unwrap);
assert_eq!(
classes,
[
r#"<mark class="m"></mark>"#,
r#"<mark class="m"></mark>"#,
r#"<mark class="m o"></mark>"#,
]
);
}
}