use super::list_item_node::{create_list_item_node, is_list_item_node};
use super::list_node::list_dom::ol_start;
use super::list_node::{ListType, create_list_node, is_list_node};
use crate::LexicalError;
use crate::dom::DomNode;
use crate::html::import::sel::is_element_of_tag;
use crate::html::import::{
ChildSchema, DomImportContext, DomImportRule, ImportChildrenOpts, OnReject, define_import_rule,
is_block_level, propagate_text_align_to_block_children, sel,
};
use crate::lexical_dom::{set_direction_from_dom, set_format_from_dom};
use crate::node::NodeRef;
use crate::nodes::line_break::create_line_break_node;
use crate::nodes::paragraph::ParagraphNode;
use std::rc::Rc;
fn is_dom_checklist(dom_node: &DomNode) -> bool {
dom_node.get_attribute("__lexicallisttype").as_deref() == Some("check")
|| dom_node.class_list().contains("contains-task-list")
|| dom_node.get_attribute("data-is-checklist").as_deref() == Some("1")
|| dom_node
.children()
.iter()
.any(|child| child.has_attribute("aria-checked"))
}
fn normalize_list_children(children: Vec<NodeRef>) -> Result<Vec<NodeRef>, LexicalError> {
let mut out = Vec::new();
for child in children {
if is_list_item_node(&child) {
let inner_children = child.get_children();
out.push(child);
if inner_children.len() > 1 {
for inner in inner_children.into_iter().filter(is_list_node) {
let item = create_list_item_node(None);
item.append(&[inner])?;
out.push(item);
}
}
} else {
let item = create_list_item_node(None);
item.append(&[child])?;
out.push(item);
}
}
Ok(out)
}
fn list_rule() -> DomImportRule {
define_import_rule(
Some("@lexical/list/list"),
sel::tag(&["ol", "ul"]),
|ctx, el, _| {
let node = if is_element_of_tag(el, "ol") {
create_list_node(ListType::Number, ol_start(el))
} else if is_dom_checklist(el) {
create_list_node(ListType::Check, 1.0)
} else {
create_list_node(ListType::Bullet, 1.0)
};
set_direction_from_dom(&node, el);
let children =
normalize_list_children(ctx.import_children(el, &ImportChildrenOpts::default())?)?;
node.splice(
0,
0,
&propagate_text_align_to_block_children(children, Some(el)),
)?;
Ok(vec![node])
},
)
}
fn lift_format_from_single_paragraph(
list_item_node: &NodeRef,
children: Vec<NodeRef>,
) -> Vec<NodeRef> {
if let [first_child] = children.as_slice()
&& first_child.get_latest().is::<ParagraphNode>()
&& list_item_node.element_data().get_format_type().is_empty()
{
let format_type = first_child.element_data().get_format_type();
if !format_type.is_empty() {
list_item_node.set_element_format(format_type);
return first_child.get_children();
}
}
children
}
fn flatten_list_item_blocks(children: Vec<NodeRef>) -> Result<Vec<NodeRef>, LexicalError> {
let is_boundary = |node: &NodeRef| is_block_level(node) && !is_list_node(node);
if !children.iter().any(is_boundary) {
return Ok(children);
}
let lifts = crate::structure::is_structure_enforced_on_active_editor();
let mut segments: Vec<(Vec<NodeRef>, bool)> = Vec::new();
let mut inline_run: Vec<NodeRef> = Vec::new();
for child in children {
if is_boundary(&child) {
if !inline_run.is_empty() {
segments.push((std::mem::take(&mut inline_run), false));
}
segments.push(if child.is_element() {
(child.get_children(), false)
} else {
(vec![child], lifts)
});
} else {
inline_run.push(child);
}
}
if !inline_run.is_empty() {
segments.push((inline_run, false));
}
let mut out = Vec::new();
for i in 0..segments.len() {
if !out.is_empty() && !segments[i - 1].1 && !segments[i].1 {
out.push(create_line_break_node());
}
out.extend(segments[i].0.iter().cloned());
}
Ok(out)
}
fn import_list_item(
ctx: &DomImportContext,
el: &DomNode,
checked: Option<bool>,
) -> Result<Vec<NodeRef>, LexicalError> {
let node = create_list_item_node(checked);
set_format_from_dom(&node, el);
set_direction_from_dom(&node, el);
let children = lift_format_from_single_paragraph(
&node,
ctx.import_children(el, &ImportChildrenOpts::default())?,
);
node.splice(0, 0, &flatten_list_item_blocks(children)?)?;
Ok(vec![node])
}
fn list_item_rule() -> DomImportRule {
define_import_rule(Some("@lexical/list/li"), sel::tag(&["li"]), |ctx, el, _| {
let checked = match el.get_attribute("aria-checked").as_deref() {
Some("true") => Some(true),
Some("false") => Some(false),
_ => None,
};
import_list_item(ctx, el, checked)
})
}
fn checkbox_child(el: &DomNode) -> Option<DomNode> {
el.children().into_iter().find(|child| {
is_element_of_tag(child, "input")
&& child.get_attribute("type").as_deref() == Some("checkbox")
})
}
fn task_list_item_rule() -> DomImportRule {
define_import_rule(
Some("@lexical/list/li-task-list-item"),
sel::tag(&["li"]).class_all(&["task-list-item"]),
|ctx, el, next| match checkbox_child(el) {
Some(input) => import_list_item(ctx, el, Some(input.has_attribute("checked"))),
None => next(),
},
)
}
fn joplin_checklist_item_rule() -> DomImportRule {
define_import_rule(
Some("@lexical/list/li-joplin-checkbox"),
sel::tag(&["li"]).class_all(&["joplin-checkbox"]),
|ctx, el, next| {
let input = el
.children()
.into_iter()
.find(|child| child.class_list().contains("checkbox-wrapper"))
.and_then(|wrapper| checkbox_child(&wrapper));
match input {
Some(input) => import_list_item(ctx, el, Some(input.has_attribute("checked"))),
None => next(),
}
},
)
}
pub fn list_schema() -> ChildSchema {
ChildSchema {
name: Some("ListSchema".to_owned()),
accepts: Rc::new(|child, _| is_list_item_node(child) || is_list_node(child)),
package_run: Some(Rc::new(|run, _, _| {
let item = create_list_item_node(None);
item.splice(0, 0, &run)?;
Ok(vec![item])
})),
on_reject: OnReject::default(),
finalize: None,
}
}
pub fn list_import_rules() -> Vec<DomImportRule> {
vec![
task_list_item_rule(),
joplin_checklist_item_rule(),
list_rule(),
list_item_rule(),
]
}