use super::utils::get_css_from_style_object;
use crate::caret::TextPointCaretSlice;
use crate::caret_utils::{caret_range_from_selection, get_text_point_caret_slice_for_node};
use crate::error::invariant;
use crate::lexical_selection::get_selection;
use crate::lexical_utils::get_node_by_key;
use crate::node::LexicalNode;
use crate::nodes::text::create_text_node;
use crate::selection_format::split_text_point_caret_slice;
use crate::selection_insert_nodes::get_previous_selection;
use crate::selection_insert_text::is_token_or_segmented;
use crate::set_dom_style::get_style_object_from_css;
use crate::{Editor, LexicalError, NodeRef, Point, PointType, SelectionRef, Which, utf16};
use std::collections::HashSet;
use std::rc::Rc;
pub fn slice_selected_text_node_content(
selection: &SelectionRef,
text_node: &NodeRef,
) -> Result<Rc<dyn LexicalNode>, LexicalError> {
let latest = text_node.get_latest();
let Some(range) = selection.range() else {
return Ok(latest);
};
let key = text_node.get_key();
if !((range.anchor.key == key || range.focus.key == key)
&& text_node.is_selected(Some(selection))?
&& !is_token_or_segmented(text_node))
{
return Ok(latest);
}
let (start, end) = if selection.is_backward()? {
(&range.focus, &range.anchor)
} else {
(&range.anchor, &range.focus)
};
let slices = if start.point_type == PointType::Element || end.point_type == PointType::Element {
Some(caret_range_from_selection(&range)?.get_text_slices())
} else {
None
};
let slice = slices
.as_ref()
.and_then(|slices| get_text_point_caret_slice_for_node(slices, text_node));
let (start_offset, end_offset) = match slice {
Some(slice) => {
let (start, end) = slice.get_slice_indices();
(start, Some(end))
}
None => (
if start.key == key { start.offset } else { 0 },
(end.key == key).then_some(end.offset),
),
};
let current = &latest.text().expect("a text node").text;
let text = utf16::slice(current, start_offset, end_offset.unwrap_or(usize::MAX));
if text == *current {
return Ok(latest);
}
let mut clone = latest.clone_node();
clone.text_mut().expect("a text node").text = text;
Ok(Rc::from(clone))
}
pub fn is_at_node_end(point: &Point) -> Result<bool, LexicalError> {
let node = point.get_node()?;
if point.point_type == PointType::Text {
return Ok(point.offset == node.get_text_content_size());
}
invariant!(
node.is_element(),
"isAtNodeEnd: node must be a TextNode or ElementNode"
);
Ok(point.offset == node.get_children_size())
}
pub fn trim_text_content_from_anchor(
editor: &Editor,
anchor: &Point,
del_count: usize,
) -> Result<(), LexicalError> {
let mut current_node = Some(anchor.get_node()?);
let mut remaining = del_count as isize;
if let Some(node) = current_node.clone().filter(NodeRef::is_element)
&& let Some(descendant) = node.get_descendant_by_index(anchor.offset)
{
current_node = Some(descendant);
}
while remaining > 0 {
let Some(mut node) = current_node.clone() else {
break;
};
if node.is_element()
&& let Some(last_descendant) = node.get_last_descendant()
{
node = last_descendant;
}
let mut next_node = node.get_previous_sibling();
let mut additional_element_whitespace = 0;
if next_node.is_none() {
let mut parent = Some(node.get_parent_or_throw()?);
let mut parent_sibling = parent.as_ref().and_then(NodeRef::get_previous_sibling);
while parent_sibling.is_none() {
parent = parent.and_then(|p| p.get_parent());
let Some(p) = &parent else {
next_node = None;
break;
};
parent_sibling = p.get_previous_sibling();
}
if let Some(parent) = &parent {
additional_element_whitespace = if parent.is_inline() { 0 } else { 2 };
next_node = parent_sibling;
}
}
let mut text = node.get_text_content();
if text.is_empty() && node.is_element() && !node.is_inline() {
text = "\n\n".to_owned();
}
let current_node_size = utf16::len(&text) as isize;
if !node.is_text() || remaining >= current_node_size {
let parent = node.get_parent();
node.remove(false)?;
if let Some(parent) = parent
&& parent.get_children_size() == 0
&& !parent.is_root()
{
parent.remove(false)?;
}
remaining -= current_node_size + additional_element_whitespace;
current_node = next_node;
continue;
}
let key = node.get_key().to_owned();
let prev_text_content = editor.get_editor_state().read(|| {
get_node_by_key(&key)
.filter(|prev| prev.is_text() && prev.is_simple_text())
.map(|prev| prev.get_text_content())
});
let offset = current_node_size - remaining;
if let Some(prev_text_content) = prev_text_content.filter(|prev| *prev != text) {
let prev_selection = get_previous_selection();
let mut target = node.clone();
if !node.is_simple_text() {
let text_node = create_text_node(&prev_text_content);
node.replace(&text_node, false)?;
target = text_node;
} else {
node.set_text_content(&prev_text_content);
}
if let Some(prev) = prev_selection.and_then(|s| s.range())
&& prev.is_collapsed()
{
let prev_offset = prev.anchor.offset;
target.select(Some(prev_offset), Some(prev_offset))?;
}
} else if node.is_simple_text() {
let is_selected = anchor.key == key;
let mut anchor_offset = anchor.offset as isize;
if anchor_offset < remaining {
anchor_offset = current_node_size;
}
let split_start = if is_selected {
anchor_offset - remaining
} else {
0
};
let split_end = if is_selected { anchor_offset } else { offset };
let parts = node.split_text(&[split_start as i64, split_end as i64])?;
let excess_node = if is_selected && split_start == 0 {
parts.first()
} else {
parts.get(1)
};
excess_node
.ok_or_else(|| LexicalError::new("$trimTextContentFromAnchor: no text to remove"))?
.remove(false)?;
} else {
let text_node = create_text_node(&utf16::slice(&text, 0, offset as usize));
node.replace(&text_node, false)?;
}
remaining = 0;
}
Ok(())
}
#[deprecated(note = "a no-op: node styles are parsed on demand")]
pub fn add_node_style(_node: &NodeRef) {
crate::warnings::warn_only_once(
"$addNodeStyle is a deprecated no-op and calls should be removed",
);
}
pub enum StylePatchTarget<'a> {
Text(&'a NodeRef),
Selection(&'a SelectionRef),
Element(&'a NodeRef),
}
pub type StylePatchFn = dyn Fn(Option<&str>, &StylePatchTarget<'_>) -> String;
#[derive(Clone)]
pub enum StylePatchValue {
Value(String),
Remove,
Compute(Rc<StylePatchFn>),
}
impl From<&str> for StylePatchValue {
fn from(value: &str) -> Self {
StylePatchValue::Value(value.to_owned())
}
}
fn patch_style<K: AsRef<str>>(
target: StylePatchTarget<'_>,
patch: &[(K, StylePatchValue)],
) -> Result<(), LexicalError> {
let prev_css = match &target {
StylePatchTarget::Selection(selection) => {
let range = selection.range().filter(|r| r.is_collapsed());
invariant!(
range.is_some(),
"$patchStyle must only be called with a TextNode, ElementNode, or collapsed RangeSelection"
);
range.expect("checked above").style
}
StylePatchTarget::Text(node) => node.text_data().style,
StylePatchTarget::Element(node) => node.element_data().get_text_style().to_owned(),
};
let prev_styles = get_style_object_from_css(&prev_css);
let mut new_styles = prev_styles.clone();
for (key, value) in patch {
let key = key.as_ref();
match value {
StylePatchValue::Compute(f) => new_styles.insert(key, f(prev_styles.get(key), &target)),
StylePatchValue::Remove => {
new_styles.remove(key);
}
StylePatchValue::Value(value) => new_styles.insert(key, value.as_str()),
}
}
let new_css_text = get_css_from_style_object(&new_styles);
match target {
StylePatchTarget::Selection(selection) => selection.set_style(&new_css_text),
StylePatchTarget::Text(node) => node.set_style(&new_css_text),
StylePatchTarget::Element(node) => node.set_text_style(&new_css_text),
}
Ok(())
}
pub fn patch_style_text<K: AsRef<str>>(
selection: &SelectionRef,
patch: &[(K, StylePatchValue)],
) -> Result<(), LexicalError> {
let mut patched_element_keys: HashSet<String> = HashSet::new();
if let Some(range) = selection.range()
&& range.is_collapsed()
{
patch_style(StylePatchTarget::Selection(selection), patch)?;
let empty_node = range.anchor.get_node()?;
if empty_node.is_element() && empty_node.is_empty() {
patched_element_keys.insert(empty_node.get_key().to_owned());
patch_style(StylePatchTarget::Element(&empty_node), patch)?;
}
}
for_each_selected_text_node_in_selection(Some(selection), |text_node| {
patch_style(StylePatchTarget::Text(text_node), patch)
})?;
for node in selection.get_nodes()? {
if !node.is_element() || !node.can_be_empty() || node.get_children_size() != 0 {
continue;
}
if !patched_element_keys.insert(node.get_key().to_owned()) {
continue;
}
patch_style(StylePatchTarget::Element(&node), patch)?;
}
Ok(())
}
pub fn for_each_selected_text_node(
f: impl FnMut(&NodeRef) -> Result<(), LexicalError>,
) -> Result<(), LexicalError> {
for_each_selected_text_node_in_selection(get_selection().as_ref(), f)
}
fn for_each_selected_text_node_in_selection(
selection: Option<&SelectionRef>,
mut f: impl FnMut(&NodeRef) -> Result<(), LexicalError>,
) -> Result<(), LexicalError> {
let Some(selection) = selection else {
return Ok(());
};
let slices: Vec<Option<TextPointCaretSlice>> = match selection.range() {
Some(range) => caret_range_from_selection(&range)?
.get_text_slices()
.to_vec(),
None => Vec::new(),
};
let range_selection = selection.is_range().then_some(selection);
for node in selection.get_nodes()? {
if !node.is_text() || !node.can_have_format() {
continue;
}
let slice = get_text_point_caret_slice_for_node(&slices, &node);
let empty = match slice {
Some(slice) => slice.distance == 0,
None => node.get_text_content_size() == 0,
};
if empty {
continue;
}
match slice {
Some(slice) if !is_token_or_segmented(&node) => {
let split = split_text_point_caret_slice(slice, range_selection)?
.ok_or_else(|| LexicalError::new("an empty text slice"))?;
f(&split)?;
}
_ => f(&node)?,
}
}
if let Some(range) = selection.range()
&& range.anchor.point_type == PointType::Text
&& range.focus.point_type == PointType::Text
&& range.anchor.key == range.focus.key
{
ensure_forward_range_selection(selection)?;
}
Ok(())
}
pub fn ensure_forward_range_selection(selection: &SelectionRef) -> Result<(), LexicalError> {
let Some(range) = selection.range() else {
return Ok(());
};
if selection.is_backward()? {
let (anchor, focus) = (range.anchor, range.focus);
selection.point_set(Which::Anchor, &focus.key, focus.offset, focus.point_type)?;
selection.point_set(Which::Focus, &anchor.key, anchor.offset, anchor.point_type)?;
}
Ok(())
}