use crate::LexicalError;
use crate::constants::IS_UNMERGEABLE;
use crate::generated_json::{export_compact_tab_node, export_tab_node, update_tab_node};
use crate::json::{Object, Value};
use crate::node::{LexicalNode, NodeBase, NodeRef};
use crate::nodes::text::{TextData, text_of};
use crate::schema::{
NodeSchema, NumberValueOptions, enum_value, node_schema, number_value, string_value,
with_accessors,
};
use std::rc::Rc;
pub fn create_tab_node() -> NodeRef {
crate::lexical_utils::replaced(crate::lexical_utils::set_node_key(Box::new(
TabNode::default(),
)))
}
#[derive(Clone, Debug)]
pub struct TabNode {
base: NodeBase,
text: TextData,
}
impl Default for TabNode {
fn default() -> Self {
Self {
base: NodeBase::default(),
text: TextData {
text: "\t".to_owned(),
detail: IS_UNMERGEABLE as f64,
..TextData::default()
},
}
}
}
thread_local! {
static TAB_NODE_SCHEMA: NodeSchema = build_tab_node_schema().expect("TabNode's schema");
}
pub fn tab_node_schema() -> NodeSchema {
TAB_NODE_SCHEMA.with(Clone::clone)
}
fn build_tab_node_schema() -> Result<NodeSchema, LexicalError> {
node_schema(vec![
(
"detail",
with_accessors(
number_value(IS_UNMERGEABLE as f64, NumberValueOptions::default())?,
Some(Rc::new(|node| text_of(node).detail.into())),
None,
)?,
),
(
"mode",
with_accessors(
enum_value(vec!["normal".into()], None)?,
Some(Rc::new(|node| {
if text_of(node).mode == 0 {
"normal".into()
} else {
Value::Undefined
}
})),
None,
)?,
),
(
"text",
with_accessors(
string_value("\t"),
Some(Rc::new(|node| text_of(node).text.as_str().into())),
None,
)?,
),
])
}
impl LexicalNode for TabNode {
fn json_schema_chain(&self) -> Vec<NodeSchema> {
vec![tab_node_schema(), crate::nodes::text::text_node_schema()]
}
fn get_type(&self) -> &'static str {
"tab"
}
fn base(&self) -> &NodeBase {
&self.base
}
fn base_mut(&mut self) -> &mut NodeBase {
&mut self.base
}
fn text(&self) -> Option<&TextData> {
Some(&self.text)
}
fn text_mut(&mut self) -> Option<&mut TextData> {
Some(&mut self.text)
}
fn can_insert_text_before(&self) -> bool {
false
}
fn can_insert_text_after(&self) -> bool {
false
}
fn generated_export_json(&self, compact: bool) -> Option<Object> {
Some(if compact {
export_compact_tab_node(self.get_type(), &self.text)
} else {
export_tab_node(self.get_type(), &self.text)
})
}
fn generated_update_from_json(&mut self, json: &Object) -> bool {
update_tab_node(&mut self.text, json);
true
}
#[cfg(feature = "html")]
fn create_dom(
&self,
_this: &NodeRef,
config: &crate::EditorConfig,
_editor: &crate::Editor,
) -> Result<crate::dom::DomNode, crate::LexicalError> {
let dom = crate::nodes::text::create_dom(self, config)?;
if let Some(class_names) =
crate::lexical_dom::get_cached_class_name_array(&config.theme, "tab")
&& !class_names.is_empty()
{
dom.class_list().add(&class_names);
}
Ok(dom)
}
}