use crate::LexicalError;
use crate::caret::{
CaretDirection, CaretRange, PointCaret, compare_point_caret_next, get_adjacent_child_caret,
get_child_caret,
};
use crate::caret_utils::{
caret_from_point, caret_range_from_selection, get_caret_range_in_direction,
get_text_point_caret_slice_for_node, normalize_caret,
};
use crate::error::invariant;
use crate::lexical_utils::get_node_by_key;
use crate::node::{NodeKey, NodeRef};
use crate::nodes::line_break::LineBreakNode;
use crate::ordered::OrderedSet;
use crate::updates::{get_active_editor_state, is_currently_read_only_mode};
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PointType {
Text,
Element,
}
impl PointType {
pub fn as_str(self) -> &'static str {
match self {
PointType::Text => "text",
PointType::Element => "element",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Point {
pub key: NodeKey,
pub offset: usize,
pub point_type: PointType,
}
impl Point {
pub fn new(key: impl Into<NodeKey>, offset: usize, point_type: PointType) -> Self {
Self {
key: key.into(),
offset,
point_type,
}
}
pub fn is_before(&self, b: &Point) -> Result<bool, LexicalError> {
is_before(self, b)
}
pub fn get_node(&self) -> Result<NodeRef, LexicalError> {
get_node_by_key(&self.key).ok_or_else(|| LexicalError::new("Point.getNode: node not found"))
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Which {
Anchor,
Focus,
}
#[derive(Clone, Debug)]
pub struct RangeSelection {
pub anchor: Point,
pub focus: Point,
pub format: f64,
pub style: String,
pub(crate) dirty: bool,
pub(crate) cached_is_backward: Option<bool>,
pub(crate) cached_nodes: Option<Vec<NodeRef>>,
}
impl RangeSelection {
pub fn new(anchor: Point, focus: Point, format: f64, style: impl Into<String>) -> Self {
Self {
anchor,
focus,
format,
style: style.into(),
dirty: false,
cached_is_backward: None,
cached_nodes: None,
}
}
pub fn point(&self, which: Which) -> &Point {
match which {
Which::Anchor => &self.anchor,
Which::Focus => &self.focus,
}
}
fn point_mut(&mut self, which: Which) -> &mut Point {
match which {
Which::Anchor => &mut self.anchor,
Which::Focus => &mut self.focus,
}
}
pub fn is(&self, other: &RangeSelection) -> bool {
self.anchor == other.anchor
&& self.focus == other.focus
&& self.format == other.format
&& self.style == other.style
}
pub fn is_collapsed(&self) -> bool {
self.anchor == self.focus
}
}
#[derive(Clone, Debug, Default)]
pub struct NodeSelection {
pub(crate) nodes: OrderedSet,
pub(crate) dirty: bool,
pub(crate) cached_nodes: Option<Vec<NodeRef>>,
}
impl NodeSelection {
pub fn keys(&self) -> impl Iterator<Item = &str> {
self.nodes.iter()
}
pub fn add(&mut self, key: &str) {
self.dirty = true;
self.nodes.add(key);
self.cached_nodes = None;
}
pub fn delete(&mut self, key: &str) {
self.dirty = true;
self.nodes.delete(key);
self.cached_nodes = None;
}
pub fn has(&self, key: &str) -> bool {
self.nodes.contains(key)
}
pub fn clear(&mut self) {
self.dirty = true;
self.nodes = OrderedSet::new();
self.cached_nodes = None;
}
}
#[derive(Clone, Debug)]
pub enum Selection {
Range(RangeSelection),
Node(NodeSelection),
}
impl Selection {
fn dirty_mut(&mut self) -> &mut bool {
match self {
Selection::Range(s) => &mut s.dirty,
Selection::Node(s) => &mut s.dirty,
}
}
pub fn is(&self, other: &Selection) -> bool {
match (self, other) {
(Selection::Range(a), Selection::Range(b)) => a.is(b),
(Selection::Node(a), Selection::Node(b)) => {
a.nodes.len() == b.nodes.len() && a.nodes.iter().all(|k| b.nodes.contains(k))
}
_ => false,
}
}
}
#[derive(Clone, Debug)]
pub struct SelectionRef(Rc<RefCell<Selection>>);
impl SelectionRef {
pub fn new(selection: Selection) -> Self {
Self(Rc::new(RefCell::new(selection)))
}
pub fn get(&self) -> Selection {
self.0.borrow().clone()
}
pub fn is_range(&self) -> bool {
matches!(*self.0.borrow(), Selection::Range(_))
}
pub fn is_node(&self) -> bool {
matches!(*self.0.borrow(), Selection::Node(_))
}
pub fn range(&self) -> Option<RangeSelection> {
match &*self.0.borrow() {
Selection::Range(r) => Some(r.clone()),
Selection::Node(_) => None,
}
}
pub(crate) fn with_range<R>(&self, f: impl FnOnce(&mut RangeSelection) -> R) -> Option<R> {
match &mut *self.0.borrow_mut() {
Selection::Range(r) => Some(f(r)),
Selection::Node(_) => None,
}
}
pub(crate) fn with_node<R>(&self, f: impl FnOnce(&mut NodeSelection) -> R) -> Option<R> {
match &mut *self.0.borrow_mut() {
Selection::Node(n) => Some(f(n)),
Selection::Range(_) => None,
}
}
pub fn add(&self, key: &str) {
self.with_node(|n| n.add(key));
}
pub fn delete(&self, key: &str) {
self.with_node(|n| n.delete(key));
}
pub fn clear(&self) {
self.with_node(NodeSelection::clear);
}
pub fn node_keys(&self) -> Vec<String> {
self.with_node(|n| n.keys().map(str::to_owned).collect())
.unwrap_or_default()
}
pub fn point(&self, which: Which) -> Option<Point> {
self.with_range(|r| r.point(which).clone())
}
pub fn is_dirty(&self) -> bool {
*self.0.borrow_mut().dirty_mut()
}
pub(crate) fn set_dirty(&self, dirty: bool) {
*self.0.borrow_mut().dirty_mut() = dirty;
}
pub(crate) fn set_cached_nodes(&self, nodes: Option<Vec<NodeRef>>) {
match &mut *self.0.borrow_mut() {
Selection::Range(r) => r.cached_nodes = nodes,
Selection::Node(n) => n.cached_nodes = nodes,
}
}
fn get_cached_nodes(&self) -> Option<Vec<NodeRef>> {
match &*self.0.borrow() {
Selection::Range(r) => r.cached_nodes.clone(),
Selection::Node(n) => n.cached_nodes.clone(),
}
}
pub fn get_nodes(&self) -> Result<Vec<NodeRef>, LexicalError> {
if let Some(cached) = self.get_cached_nodes() {
return Ok(cached);
}
let nodes = match self.range() {
Some(range) => {
let caret_range = get_caret_range_in_direction(
&caret_range_from_selection(&range)?,
CaretDirection::Next,
)?;
let nodes = get_nodes_from_caret_range_compat(&caret_range)?;
invariant!(
!(range.is_collapsed() && nodes.len() > 1),
"RangeSelection.getNodes() returned {} > 1 nodes in a collapsed selection",
nodes.len()
);
nodes
}
None => self
.node_keys()
.iter()
.filter_map(|key| get_node_by_key(key))
.collect(),
};
if !is_currently_read_only_mode() {
self.set_cached_nodes(Some(nodes.clone()));
}
Ok(nodes)
}
pub fn get_text_content(&self) -> Result<String, LexicalError> {
let Some(range) = self.range() else {
return Ok(self
.get_nodes()?
.iter()
.map(NodeRef::get_text_content)
.collect());
};
if range.is_collapsed() {
return Ok(String::new());
}
let nodes = self.get_nodes()?;
let slices = caret_range_from_selection(&range)?.get_text_slices();
let mut text_content = String::new();
let mut prev_was_element = true;
for node in &nodes {
if node.is_element() && !node.is_inline() {
if !prev_was_element {
text_content.push('\n');
}
let mut slot_text = String::new();
for slot_name in crate::slot::get_slot_names(node) {
if let Some(slot) = crate::slot::get_slot(node, &slot_name) {
slot_text.push_str(&slot.get_text_content());
}
}
if !slot_text.is_empty() {
text_content.push_str(&slot_text);
prev_was_element = false;
} else {
prev_was_element = !node.is_empty();
}
} else {
prev_was_element = false;
if node.is_text() {
match get_text_point_caret_slice_for_node(&slices, node) {
Some(slice) => text_content.push_str(&slice.get_text_content()),
None => text_content.push_str(&node.get_text_content()),
}
} else if node.is_decorator() || node.get_latest().is::<LineBreakNode>() {
text_content.push_str(&node.get_text_content());
}
}
}
Ok(text_content)
}
pub fn clone_selection(&self) -> SelectionRef {
let mut value = self.get();
match &mut value {
Selection::Range(r) => {
r.dirty = false;
r.cached_is_backward = None;
r.cached_nodes = None;
}
Selection::Node(n) => {
n.dirty = false;
n.cached_nodes = None;
}
}
SelectionRef::new(value)
}
pub fn ptr_eq(&self, other: &SelectionRef) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
pub fn point_set(
&self,
which: Which,
key: &str,
offset: usize,
point_type: PointType,
) -> Result<(), LexicalError> {
let node = get_node_by_key(key);
let matches = match (&node, point_type) {
(Some(n), PointType::Text) => n.is_text(),
(Some(n), PointType::Element) => n.is_element(),
(None, _) => false,
};
invariant!(
matches,
"PointType.set: node with key {key} is {} and can not be used for a {} point",
node.map_or("[not found]", |n| n.get_type()),
point_type.as_str()
);
let read_only = is_currently_read_only_mode();
let old_key = self.point(which).map(|p| p.key);
if !read_only && old_key.is_some() && crate::lexical_utils::get_composition_key() == old_key
{
crate::lexical_utils::set_composition_key(Some(key));
}
self.with_range(|r| {
*r.point_mut(which) = Point::new(key, offset, point_type);
if !read_only {
r.cached_nodes = None;
r.cached_is_backward = None;
r.dirty = true;
}
});
Ok(())
}
pub(crate) fn set_text_node_range(
&self,
anchor: &NodeRef,
anchor_offset: usize,
focus: &NodeRef,
focus_offset: usize,
) -> Result<(), LexicalError> {
self.point_set(
Which::Anchor,
anchor.get_key(),
anchor_offset,
PointType::Text,
)?;
self.point_set(Which::Focus, focus.get_key(), focus_offset, PointType::Text)
}
pub fn is_backward(&self) -> Result<bool, LexicalError> {
let range = self.range().expect("a range selection");
if let Some(cached) = range.cached_is_backward {
return Ok(cached);
}
let is_backward = is_before(&range.focus, &range.anchor)?;
if !is_currently_read_only_mode() {
self.with_range(|r| r.cached_is_backward = Some(is_backward));
}
Ok(is_backward)
}
}
fn is_before(a: &Point, b: &Point) -> Result<bool, LexicalError> {
if a.key == b.key {
return Ok(a.offset < b.offset);
}
let a_caret = normalize_caret(&caret_from_point(a, CaretDirection::Next)?);
let b_caret = normalize_caret(&caret_from_point(b, CaretDirection::Next)?);
Ok(compare_point_caret_next(&a_caret, &b_caret)?.is_lt())
}
fn get_nodes_from_caret_range_compat(range: &CaretRange) -> Result<Vec<NodeRef>, LexicalError> {
let mut nodes: Vec<NodeRef> = Vec::new();
let [before_slice, after_slice] = range.get_text_slices();
if let Some(slice) = &before_slice {
nodes.push(slice.caret.origin().clone());
}
let mut seen_ancestors: Vec<NodeRef> = Vec::new();
let mut open_elements = 0usize;
for caret in range.iter() {
let origin = caret.origin().clone();
if caret.is_child_caret() {
if nodes.is_empty() {
if !seen_ancestors.contains(&origin) {
seen_ancestors.push(origin);
}
} else {
open_elements += 1;
nodes.push(origin);
}
} else if !origin.is_element() || open_elements == 0 {
nodes.push(origin);
} else {
open_elements -= 1;
}
}
if let Some(slice) = &after_slice {
nodes.push(slice.caret.origin().clone());
}
if range.focus.is_sibling_caret()
&& range.focus.origin().is_element()
&& range.focus.get_node_at_caret().is_none()
{
let mut reverse_caret = get_child_caret(range.focus.origin(), CaretDirection::Previous);
while let Some(rc) = reverse_caret.filter(|rc| {
rc.is_child_caret()
&& seen_ancestors.contains(rc.origin())
&& !rc.origin().is_empty()
&& nodes.last() == Some(rc.origin())
}) {
seen_ancestors.retain(|n| n != rc.origin());
nodes.pop();
reverse_caret = get_adjacent_child_caret(Some(&rc));
}
}
while nodes.len() > 1 {
let last_included_node = nodes.last().expect("len > 1");
if last_included_node.is_element()
&& !(open_elements > 0
|| last_included_node.is_empty()
|| seen_ancestors.contains(last_included_node))
{
nodes.pop();
continue;
}
break;
}
if nodes.is_empty() && range.is_collapsed() {
let norm_caret = normalize_caret(&range.anchor);
let flipped_norm_caret = normalize_caret(&range.anchor.get_flipped()?);
let get_candidate = |caret: &PointCaret| -> Option<NodeRef> {
if caret.is_text_point_caret() {
Some(caret.origin().clone())
} else {
caret.get_node_at_caret()
}
};
let node = get_candidate(&norm_caret)
.or_else(|| get_candidate(&flipped_norm_caret))
.unwrap_or_else(|| {
if range.anchor.get_node_at_caret().is_some() {
norm_caret.origin().clone()
} else {
flipped_norm_caret.origin().clone()
}
});
nodes.push(node);
}
Ok(nodes)
}
pub fn get_selection() -> Option<SelectionRef> {
get_active_editor_state().0.selection.borrow().clone()
}
pub fn set_selection(selection: Option<SelectionRef>) {
crate::updates::error_on_read_only();
if let Some(selection) = &selection {
selection.set_dirty(true);
selection.set_cached_nodes(None);
if selection.is_range()
&& crate::updates::get_active_editor().is_some_and(|e| e.0.slots_used.get())
{
clamp_range_selection_to_slot_frame(selection)
.expect("clamping a selection to its slot frame");
}
}
*get_active_editor_state().0.selection.borrow_mut() = selection;
}
pub fn get_point_slot_frame(point: &Point) -> Option<NodeRef> {
get_node_by_key(&point.key).and_then(|node| crate::slot::get_slot_frame(&node))
}
pub fn is_same_frame(a: Option<&NodeRef>, b: Option<&NodeRef>) -> bool {
a == b
}
fn set_point_to_frame_edge(
selection: &SelectionRef,
which: Which,
frame: &NodeRef,
at_end: bool,
) -> Result<(), LexicalError> {
if frame.is_element() {
let offset = if at_end { frame.get_children_size() } else { 0 };
selection.point_set(which, frame.get_key(), offset, PointType::Element)
} else {
let offset = if at_end {
frame.get_text_content_size()
} else {
0
};
selection.point_set(which, frame.get_key(), offset, PointType::Text)
}
}
fn get_slot_host_in_frame(frame: &NodeRef, target: Option<&NodeRef>) -> Option<NodeRef> {
let mut host = crate::slot::get_slot_host(frame);
while let Some(h) = host {
let host_frame = crate::slot::get_slot_frame(&h);
if is_same_frame(host_frame.as_ref(), target) {
return (host_frame.is_some() || h.is_attached()).then_some(h);
}
host = host_frame.and_then(|f| crate::slot::get_slot_host(&f));
}
None
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum SlotBoundaryMode {
Selection,
Deletion,
}
pub(crate) fn set_point_at_slot_boundary(
selection: &SelectionRef,
which: Which,
anchor_node: Option<&NodeRef>,
anchor_frame: Option<&NodeRef>,
focus_frame: Option<&NodeRef>,
mode: SlotBoundaryMode,
resolve_focus_after_anchor: impl FnOnce() -> Result<bool, LexicalError>,
) -> Result<bool, LexicalError> {
let host = focus_frame.and_then(|f| get_slot_host_in_frame(f, anchor_frame));
if host.is_none() && anchor_frame.is_none() {
return Ok(false);
}
let focus_after_anchor = resolve_focus_after_anchor()?;
let host_parent = host.as_ref().and_then(NodeRef::get_parent);
let (parent, offset) = match (&host, &host_parent) {
(Some(host), Some(host_parent))
if !anchor_node.is_some_and(|a| host == a || host.is_parent_of(a)) =>
{
let index = host.get_index_within_parent().unwrap_or(0);
let past = focus_after_anchor == (mode == SlotBoundaryMode::Selection);
(host_parent.clone(), index + usize::from(past))
}
_ => {
let edge = host.clone().or_else(|| anchor_frame.cloned());
let Some(edge) = edge.filter(NodeRef::is_element) else {
if let Some(edge) = host.or_else(|| anchor_frame.cloned()) {
set_point_to_frame_edge(selection, which, &edge, focus_after_anchor)?;
}
return Ok(true);
};
let offset = if focus_after_anchor {
edge.get_children_size()
} else {
0
};
(edge, offset)
}
};
if mode == SlotBoundaryMode::Deletion {
set_deletion_focus(selection, which, parent, offset, !focus_after_anchor)?;
} else {
selection.point_set(which, parent.get_key(), offset, PointType::Element)?;
}
Ok(true)
}
fn set_deletion_focus(
selection: &SelectionRef,
which: Which,
mut parent: NodeRef,
mut offset: usize,
is_backward: bool,
) -> Result<(), LexicalError> {
loop {
let child = if is_backward {
parent.get_child_at_index(offset)
} else {
offset
.checked_sub(1)
.and_then(|i| parent.get_child_at_index(i))
};
let Some(child) = child else { break };
if child.is_text() {
let at = if is_backward {
0
} else {
child.get_text_content_size()
};
return selection.point_set(which, child.get_key(), at, PointType::Text);
}
if !child.is_element() || crate::lexical_utils::is_root_or_shadow_root(&child) {
break;
}
offset = if is_backward {
0
} else {
child.get_children_size()
};
parent = child;
}
selection.point_set(which, parent.get_key(), offset, PointType::Element)
}
pub fn clamp_range_selection_to_slot_frame(selection: &SelectionRef) -> Result<bool, LexicalError> {
let Some(range) = selection.range() else {
return Ok(false);
};
let anchor_frame = get_point_slot_frame(&range.anchor);
let focus_frame = get_point_slot_frame(&range.focus);
if is_same_frame(anchor_frame.as_ref(), focus_frame.as_ref()) {
return Ok(false);
}
let anchor_node = get_node_by_key(&range.anchor.key);
let clamped = set_point_at_slot_boundary(
selection,
Which::Focus,
anchor_node.as_ref(),
anchor_frame.as_ref(),
focus_frame.as_ref(),
SlotBoundaryMode::Selection,
|| {
let anchor = caret_from_point(&range.anchor, CaretDirection::Next)?;
let focus = caret_from_point(&range.focus, CaretDirection::Next)?;
Ok(
crate::caret::compare_point_caret_next_across_slots(&anchor, &focus)
!= Some(std::cmp::Ordering::Greater),
)
},
)?;
if clamped {
selection.set_dirty(true);
}
Ok(clamped)
}
pub fn create_range_selection() -> SelectionRef {
SelectionRef::new(Selection::Range(RangeSelection::new(
Point::new("root", 0, PointType::Element),
Point::new("root", 0, PointType::Element),
0.0,
"",
)))
}
pub fn create_node_selection() -> SelectionRef {
SelectionRef::new(Selection::Node(NodeSelection::default()))
}
pub(crate) fn apply_selection_transforms(
next_editor_state: &crate::EditorState,
editor: &crate::Editor,
) -> Result<(), LexicalError> {
let prev_selection = editor.get_editor_state().selection();
let Some(next_selection) = next_editor_state.selection() else {
return Ok(());
};
let Some(range) = next_selection.range() else {
return Ok(());
};
let mut anchor_node = None;
if range.anchor.point_type == PointType::Text {
let node = range.anchor.get_node()?;
node.get_latest()
.selection_transform(&node, prev_selection.as_ref(), &next_selection)?;
anchor_node = Some(node);
}
if range.focus.point_type == PointType::Text {
let node = range.focus.get_node()?;
if anchor_node.as_ref() != Some(&node) {
node.get_latest().selection_transform(
&node,
prev_selection.as_ref(),
&next_selection,
)?;
}
}
Ok(())
}
pub(crate) fn internal_make_range_selection(anchor: Point, focus: Point) -> SelectionRef {
let mut range = RangeSelection::new(anchor, focus, 0.0, "");
range.dirty = true;
let selection = SelectionRef::new(Selection::Range(range));
*get_active_editor_state().0.selection.borrow_mut() = Some(selection.clone());
selection
}
fn select_point_on_node(
selection: &SelectionRef,
which: Which,
node: &NodeRef,
) -> Result<(), LexicalError> {
let mut key = node.get_key().to_owned();
let mut offset = selection.point(which).expect("a range selection").offset;
let mut point_type = PointType::Element;
if node.is_text() {
point_type = PointType::Text;
offset = offset.min(node.get_text_content_size());
} else if !node.is_element() {
match node.get_next_sibling().filter(NodeRef::is_text) {
Some(next) => {
key = next.get_key().to_owned();
offset = 0;
point_type = PointType::Text;
}
None => {
if let Some(parent) = node.get_parent() {
key = parent.get_key().to_owned();
offset = node.get_index_within_parent().map_or(0, |i| i + 1);
}
}
}
}
selection.point_set(which, &key, offset, point_type)
}
pub(crate) fn move_selection_point_to_end(
selection: &SelectionRef,
which: Which,
node: &NodeRef,
) -> Result<(), LexicalError> {
if node.is_element() {
match node.get_last_descendant() {
Some(last) if last.is_element() || last.is_text() => {
select_point_on_node(selection, which, &last)
}
_ => select_point_on_node(selection, which, node),
}
} else {
select_point_on_node(selection, which, node)
}
}
pub(crate) fn selection_touches_element(selection: &SelectionRef, parent: &NodeRef) -> bool {
selection
.with_range(|r| r.anchor.key == parent.get_key() || r.focus.key == parent.get_key())
.unwrap_or(false)
}
pub(crate) fn update_element_selection_on_create_delete_node(
selection: &SelectionRef,
parent: &NodeRef,
node_offset: usize,
times: isize,
) -> Result<(), LexicalError> {
if !selection_touches_element(selection, parent) {
return Ok(());
}
for which in [Which::Anchor, Which::Focus] {
let point = selection.point(which).expect("a range selection");
if point.key == parent.get_key()
&& ((node_offset <= point.offset && times > 0)
|| (node_offset < point.offset && times < 0))
{
let offset = (point.offset as isize + times).max(0) as usize;
selection.point_set(which, parent.get_key(), offset, PointType::Element)?;
}
let point = selection.point(which).expect("a range selection");
let node = point.get_node()?;
if node.is_element() {
let size = node.get_children_size();
let at_end = point.offset >= size;
let child = if at_end {
size.checked_sub(1).and_then(|i| node.get_child_at_index(i))
} else {
node.get_child_at_index(point.offset)
};
if let Some(child) = child.filter(NodeRef::is_text) {
let offset = if at_end {
child.get_text_content_size()
} else {
0
};
selection.point_set(which, child.get_key(), offset, PointType::Text)?;
}
}
}
Ok(())
}
pub(crate) fn move_selection_point_to_sibling(
selection: &SelectionRef,
which: Which,
node: &NodeRef,
parent: &NodeRef,
prev_sibling: Option<&NodeRef>,
next_sibling: Option<&NodeRef>,
) -> Result<(), LexicalError> {
let sibling = prev_sibling.or(next_sibling);
match sibling.filter(|s| s.is_text() || s.is_element()) {
Some(sibling) => {
let is_text = sibling.is_text();
let offset = if prev_sibling.is_none() {
0
} else if is_text {
sibling.get_text_content_size()
} else {
sibling.get_children_size()
};
let point_type = if is_text {
PointType::Text
} else {
PointType::Element
};
selection.point_set(which, sibling.get_key(), offset, point_type)
}
None => {
let offset = node
.get_index_within_parent()
.unwrap_or_else(|| parent.get_children_size());
selection.point_set(which, parent.get_key(), offset, PointType::Element)
}
}
}
pub(crate) fn adjust_point_offset_for_merged_sibling(
selection: &SelectionRef,
which: Which,
is_before: bool,
key: &str,
target: &NodeRef,
text_length: usize,
) -> Result<(), LexicalError> {
let point = selection.point(which).expect("a range selection");
if point.point_type == PointType::Text {
let offset = point.offset + if is_before { 0 } else { text_length };
selection.point_set(which, key, offset, PointType::Text)
} else if target
.get_index_within_parent()
.is_some_and(|i| point.offset > i)
{
selection.point_set(which, &point.key, point.offset - 1, PointType::Element)
} else {
Ok(())
}
}
pub(crate) fn has_ancestor(child: &NodeRef, target: &NodeRef) -> bool {
let mut parent = child.get_parent();
while let Some(p) = parent {
if &p == target {
return true;
}
parent = p.get_parent();
}
false
}
pub(crate) fn maybe_move_children_selection_to_parent(
parent: &NodeRef,
) -> Result<Option<SelectionRef>, LexicalError> {
let selection = get_selection();
let Some(range) = selection.as_ref().filter(|s| s.is_range()) else {
return Ok(selection);
};
if !parent.is_element() {
return Ok(selection);
}
for which in [Which::Anchor, Which::Focus] {
let node = range.point(which).expect("a range selection").get_node()?;
if has_ancestor(&node, parent) {
range.point_set(which, parent.get_key(), 0, PointType::Element)?;
}
}
Ok(selection)
}
pub(crate) fn has_dirty_selection(
pending: Option<&SelectionRef>,
current: Option<&SelectionRef>,
) -> bool {
match (pending, current) {
(Some(pending), current) => {
pending.is_dirty() || current.is_none_or(|c| !pending.get().is(&c.get()))
}
(None, current) => current.is_some(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{CreateEditorArgs, UpdateOptions, create_editor, get_root, json, reset_random_key};
#[test]
fn get_nodes_and_text_content_match_js() {
reset_random_key();
let editor = create_editor(CreateEditorArgs::default());
let discrete = || UpdateOptions {
discrete: true,
..UpdateOptions::default()
};
editor
.update(
|| {
let nodes = json::parse(
r#"[{"type":"paragraph","children":[{"type":"text","text":"one"},{"type":"text","text":"two","format":1}]},{"type":"paragraph","children":[{"type":"text","text":"three"},{"type":"linebreak"},{"type":"text","text":"x"}]},{"type":"paragraph","children":[]}]"#,
)
.unwrap();
let nodes: Vec<NodeRef> = nodes
.as_array()
.unwrap()
.iter()
.map(|n| crate::parse_serialized_node(n).unwrap())
.collect();
get_root().append(&nodes)
},
discrete(),
)
.unwrap();
use PointType::{Element as E, Text as T};
type Spec = (&'static str, usize, PointType);
type Case = (Spec, Spec, &'static [&'static str], &'static str, bool);
let cases: &[Case] = &[
(
("2", 1, T),
("5", 2, T),
&["2", "3", "1", "4", "5"],
"netwo\nth",
false,
),
(
("1", 0, E),
("8", 0, E),
&["2", "3", "1", "4", "5", "6", "7", "8"],
"onetwo\nthree\nx\n",
false,
),
(
("5", 4, T),
("2", 0, T),
&["2", "3", "1", "4", "5"],
"onetwo\nthre",
true,
),
(("2", 1, T), ("2", 1, T), &["2"], "", false),
(
("root", 0, E),
("root", 3, E),
&["2", "3", "1", "4", "5", "6", "7", "8"],
"onetwo\nthree\nx\n",
false,
),
(("1", 1, E), ("4", 0, E), &["3", "1", "4"], "two\n", false),
(("3", 3, T), ("4", 0, E), &["3", "1", "4"], "\n", false),
(("4", 1, E), ("4", 3, E), &["6", "7"], "\nx", false),
(("1", 2, E), ("1", 2, E), &["3"], "", false),
(
("8", 0, E),
("1", 0, E),
&["2", "3", "1", "4", "5", "6", "7", "8"],
"onetwo\nthree\nx\n",
true,
),
];
editor
.update(
move || {
for (anchor, focus, nodes, text, backward) in cases {
let selection = create_range_selection();
selection.point_set(Which::Anchor, anchor.0, anchor.1, anchor.2)?;
selection.point_set(Which::Focus, focus.0, focus.1, focus.2)?;
set_selection(Some(selection.clone()));
let keys: Vec<String> = selection
.get_nodes()?
.iter()
.map(|n| n.get_key().to_owned())
.collect();
assert_eq!(keys, *nodes, "{anchor:?} {focus:?}");
assert_eq!(selection.get_text_content()?, *text, "{anchor:?} {focus:?}");
assert_eq!(selection.is_backward()?, *backward, "{anchor:?} {focus:?}");
}
let selection = create_node_selection();
for key in ["4", "2", "99"] {
selection.with_node(|n| n.add(key));
}
let keys: Vec<String> = selection
.get_nodes()?
.iter()
.map(|n| n.get_key().to_owned())
.collect();
assert_eq!(keys, ["4", "2"]);
assert_eq!(selection.get_text_content()?, "three\nxone");
Ok(())
},
discrete(),
)
.unwrap();
}
}