#![allow(dead_code)]
use crate::{
Editor, LexicalError, LexicalNode, NodeRef, Point, PointType, Which, create_text_node,
get_selection,
};
use std::rc::Rc;
pub(crate) fn set_anchor_point(
key: &str,
offset: usize,
point_type: PointType,
) -> Result<(), LexicalError> {
set_point(Which::Anchor, key, offset, point_type)
}
pub(crate) fn set_focus_point(
key: &str,
offset: usize,
point_type: PointType,
) -> Result<(), LexicalError> {
set_point(Which::Focus, key, offset, point_type)
}
fn set_point(
which: Which,
key: &str,
offset: usize,
point_type: PointType,
) -> Result<(), LexicalError> {
match get_selection().filter(crate::SelectionRef::is_range) {
Some(selection) => selection.point_set(which, key, offset, point_type),
None => {
let dummy = create_text_node("");
dummy.select(None, None)?;
set_point(which, key, offset, point_type)
}
}
}
pub(crate) fn point_string(point: &Point) -> String {
format!(
"{}:{}:{}",
point.key,
point.offset,
point.point_type.as_str()
)
}
pub(crate) fn points_of(selection: &crate::SelectionRef) -> (String, String) {
(
point_string(&selection.point(Which::Anchor).expect("a range selection")),
point_string(&selection.point(Which::Focus).expect("a range selection")),
)
}
pub(crate) fn create_text_node_with_key(text: &str, key: &str) -> NodeRef {
let mut node: Box<dyn LexicalNode> = Box::new(crate::nodes::text::TextNode::default());
node.text_mut().expect("a text node").text = text.to_owned();
let editor_state = crate::updates::get_active_editor_state();
node.base_mut().key = key.to_owned();
node.base_mut().written_in = editor_state.0.id;
editor_state
.0
.node_map
.borrow_mut()
.insert(key.to_owned(), Rc::from(node));
if let Some(editor) = crate::updates::get_active_editor() {
editor.mark_dirty(key, false);
}
NodeRef::new(key)
}
pub(crate) fn create_paragraph_with_nodes(nodes: &[(&str, &str, bool)]) -> NodeRef {
let paragraph = crate::create_paragraph_node();
for &(key, text, mergeable) in nodes {
let text_node = create_text_node_with_key(text, key);
if !mergeable {
text_node.toggle_unmergeable();
}
paragraph.append(&[text_node]).unwrap();
}
paragraph
}
#[cfg(feature = "html")]
pub(crate) fn rendered_html(editor: &Editor) -> String {
editor
.read_with_mode(crate::EditorReadMode::Latest, || {
let container = crate::lexical_dom::get_document().create_element("div");
for child in crate::get_root().get_children() {
container.append_child(&render_dom(&child, editor));
}
container.inner_html()
})
.unwrap()
}
#[cfg(feature = "html")]
fn render_dom(node: &NodeRef, editor: &Editor) -> crate::dom::DomNode {
let dom = node
.get_latest()
.create_dom(node, editor.config(), editor)
.unwrap();
if node.is_text() {
dom.set_attribute("data-lexical-text", "true");
} else if node.get_latest().is_decorator() {
dom.set_attribute("data-lexical-decorator", "true");
dom.set_attribute("contenteditable", "false");
}
if !node.is_element() {
return dom;
}
if let Some(direction) = node.element_data().get_direction() {
dom.set_attribute("dir", direction.as_str());
} else if let Some(parent) = node.get_parent()
&& crate::is_root_or_shadow_root(&parent)
&& parent.element_data().get_direction().is_none()
{
dom.set_attribute("dir", "auto");
}
let indent = node.element_data().get_indent();
if indent != 0.0 {
dom.style().set_property(
"padding-inline-start",
&format!(
"calc({} * var(--lexical-indent-base-value, 40px))",
crate::json::number_to_string(indent)
),
"",
);
}
let children = node.get_children();
for child in &children {
dom.append_child(&render_dom(child, editor));
}
if !node.is_inline() {
let managed = match children.last() {
None => true,
Some(last) => {
last.get_type() == "linebreak"
|| (last.get_latest().is_decorator() && last.is_inline())
}
};
if managed {
let br = crate::lexical_dom::get_document().create_element("br");
br.set_attribute("data-lexical-managed-linebreak", "true");
dom.append_child(&br);
}
}
dom
}
#[derive(Clone, Debug, Default)]
pub(crate) struct BlockTestDecoratorNode {
base: crate::NodeBase,
}
impl LexicalNode for BlockTestDecoratorNode {
fn get_type(&self) -> &'static str {
"test_decorator"
}
fn base(&self) -> &crate::NodeBase {
&self.base
}
fn base_mut(&mut self) -> &mut crate::NodeBase {
&mut self.base
}
fn is_decorator(&self) -> bool {
true
}
fn is_inline(&self) -> bool {
false
}
fn get_text_content(&self) -> String {
"Hello world".to_owned()
}
fn decorate(&self, _editor: &Editor) -> Option<Rc<dyn std::any::Any>> {
Some(Rc::new("Hello world"))
}
#[cfg(feature = "html")]
fn create_dom(
&self,
_this: &NodeRef,
_config: &crate::EditorConfig,
_editor: &Editor,
) -> Result<crate::dom::DomNode, LexicalError> {
Ok(crate::lexical_dom::get_document().create_element("span"))
}
}