use super::format_list::{
handle_list_insert_paragraph, insert_list, remove_list, update_children_list_item_value,
};
use super::list_item_node::is_list_item_node;
use super::list_node::{ListType, is_list_node};
use super::utils::get_list_depth;
use crate::commands::{INSERT_PARAGRAPH_COMMAND, KEY_BACKSPACE_COMMAND};
use crate::editor::{COMMAND_PRIORITY_BEFORE_EDITOR, COMMAND_PRIORITY_LOW, LexicalCommand};
use crate::lexical_selection::{SelectionRef, Which, get_selection};
use crate::lexical_utils::get_node_by_key;
use crate::node::{NodeKey, NodeRef};
use crate::selection_insert_nodes::find_matching_parent;
use crate::{Editor, LexicalError, Unregister};
#[derive(Clone, Debug, PartialEq)]
pub struct UpdateListStartPayload {
pub list_node_key: NodeKey,
pub new_start: f64,
}
pub const UPDATE_LIST_START_COMMAND: LexicalCommand<UpdateListStartPayload> =
LexicalCommand::constant(u64::MAX - 20, "UPDATE_LIST_START_COMMAND");
pub const INSERT_UNORDERED_LIST_COMMAND: LexicalCommand<()> =
LexicalCommand::constant(u64::MAX - 21, "INSERT_UNORDERED_LIST_COMMAND");
pub const INSERT_ORDERED_LIST_COMMAND: LexicalCommand<()> =
LexicalCommand::constant(u64::MAX - 22, "INSERT_ORDERED_LIST_COMMAND");
pub const REMOVE_LIST_COMMAND: LexicalCommand<()> =
LexicalCommand::constant(u64::MAX - 23, "REMOVE_LIST_COMMAND");
pub const INSERT_CHECK_LIST_COMMAND: LexicalCommand<()> =
LexicalCommand::constant(u64::MAX - 24, "INSERT_CHECK_LIST_COMMAND");
#[derive(Clone, Copy, Debug, Default)]
pub struct RegisterListOptions {
pub restore_numbering: bool,
}
fn merge(unregisters: Vec<Unregister>) -> Unregister {
Box::new(move || {
for unregister in unregisters {
unregister();
}
})
}
pub fn register_list(editor: &Editor, options: RegisterListOptions) -> Unregister {
merge(vec![
editor.register_command(
&INSERT_ORDERED_LIST_COMMAND,
|_, _| {
insert_list(ListType::Number)?;
Ok(true)
},
COMMAND_PRIORITY_LOW,
),
editor.register_command(
&UPDATE_LIST_START_COMMAND,
|payload, _| {
let Some(list_node) = get_node_by_key(&payload.list_node_key).filter(is_list_node)
else {
return Ok(false);
};
if list_node.get_list_type() == Some(ListType::Number) {
list_node.set_list_start(payload.new_start);
update_children_list_item_value(&list_node);
}
Ok(true)
},
COMMAND_PRIORITY_LOW,
),
editor.register_command(
&INSERT_UNORDERED_LIST_COMMAND,
|_, _| {
insert_list(ListType::Bullet)?;
Ok(true)
},
COMMAND_PRIORITY_LOW,
),
editor.register_command(
&REMOVE_LIST_COMMAND,
|_, _| {
remove_list()?;
Ok(true)
},
COMMAND_PRIORITY_LOW,
),
editor.register_command(
&INSERT_PARAGRAPH_COMMAND,
move |_, _| handle_list_insert_paragraph(options.restore_numbering),
COMMAND_PRIORITY_LOW,
),
editor.register_command(
&KEY_BACKSPACE_COMMAND,
|event, _| {
let Some(range) = get_selection().and_then(|s| s.range()) else {
return Ok(false);
};
if !range.is_collapsed() || range.anchor.offset != 0 {
return Ok(false);
}
let mut current = range.anchor.get_node()?;
while !is_list_item_node(¤t) {
if current.get_previous_sibling().is_some() {
return Ok(false);
}
let Some(parent) = current.get_parent() else {
return Ok(false);
};
current = parent;
}
let selection = get_selection().expect("a range selection");
if current.collapse_at_start(&selection)? {
event.prevent_default();
return Ok(true);
}
Ok(false)
},
COMMAND_PRIORITY_BEFORE_EDITOR,
),
editor.register_node_transform("listitem", |node| {
match node.get_first_child() {
Some(first_child) => {
if first_child.is_text() {
let text = first_child.text_data();
let element = node.element_data();
if element.get_text_style() != text.get_style() {
node.set_text_style(text.get_style());
}
if element.get_text_format() != text.get_format() {
node.set_text_format(text.get_format());
}
}
}
None => {
if let Some(selection) = get_selection().filter(SelectionRef::is_range) {
let range = selection.range().expect("a range selection");
let element = node.element_data();
if (range.style != element.get_text_style()
|| range.format != element.get_text_format())
&& range.is_collapsed()
&& selection
.point(Which::Anchor)
.expect("a range")
.get_node()?
== *node
{
node.set_text_style(&range.style);
node.set_text_format(range.format);
}
}
}
}
Ok(())
}),
editor.register_node_transform("text", |node| {
if let Some(list_item_parent_node) = node.get_parent().filter(is_list_item_node)
&& list_item_parent_node.get_first_child().as_ref() == Some(node)
{
let text = node.text_data();
let element = list_item_parent_node.element_data();
if text.get_style() != element.get_text_style()
|| text.get_format() != element.get_text_format()
{
list_item_parent_node.set_text_style(text.get_style());
list_item_parent_node.set_text_format(text.get_format());
}
}
Ok(())
}),
])
}
pub fn register_check_list(editor: &Editor) -> Unregister {
editor.register_command(
&INSERT_CHECK_LIST_COMMAND,
|_, _| {
insert_list(ListType::Check)?;
Ok(true)
},
COMMAND_PRIORITY_LOW,
)
}
pub fn register_list_strict_indent_transform(editor: &Editor) -> Unregister {
fn format_list_indent_strict(list_item_node: &NodeRef) -> Result<(), LexicalError> {
let list_node = list_item_node.get_parent();
let Some(list_node) = list_node.filter(is_list_node) else {
return Ok(());
};
if list_item_node
.get_first_child()
.is_some_and(|c| is_list_node(&c))
{
return Ok(());
}
let starting_list_item_node = find_matching_parent(list_item_node, |node| {
is_list_item_node(node)
&& node.get_parent().is_some_and(|p| is_list_node(&p))
&& node
.get_previous_sibling()
.is_some_and(|s| is_list_item_node(&s))
});
match starting_list_item_node {
None => {
if list_item_node.get_indent()? > 0.0 {
list_item_node.set_indent(0.0)?;
}
}
Some(starting) => {
if let Some(prev_list_item_node) =
starting.get_previous_sibling().filter(is_list_item_node)
{
let end_list_item_node = find_children_end_list_item_node(&prev_list_item_node);
if let Some(end_list_node) =
end_list_item_node.get_parent().filter(is_list_node)
{
let prev_depth = get_list_depth(&end_list_node)?;
let depth = get_list_depth(&list_node)?;
if prev_depth + 1 < depth {
list_item_node.set_indent(prev_depth as f64)?;
}
}
}
}
}
Ok(())
}
editor.register_node_transform("list", |list_node| {
let mut queue = std::collections::VecDeque::from([list_node.clone()]);
while let Some(node) = queue.pop_front() {
if !is_list_node(&node) {
continue;
}
for child in node.get_children() {
if is_list_item_node(&child) {
format_list_indent_strict(&child)?;
if let Some(first_child) = child.get_first_child().filter(is_list_node) {
queue.push_back(first_child);
}
}
}
}
Ok(())
})
}
fn find_children_end_list_item_node(list_item_node: &NodeRef) -> NodeRef {
let mut current = list_item_node.clone();
let mut first_child = current.get_first_child();
while let Some(list) = first_child.filter(is_list_node) {
match list.get_last_child().filter(is_list_item_node) {
Some(last_child) => {
first_child = last_child.get_first_child();
current = last_child;
}
None => break,
}
}
current
}