use crate::json::{Object, Value};
use crate::lexical_selection::SelectionRef;
use crate::lexical_utils::get_root;
use crate::node::{LexicalNode, NodeKey, NodeRef};
use crate::nodes::root::RootNode;
use crate::ordered::KeyMap;
use crate::serialized_export::{export_node_json, with_compact_export};
use crate::updates::read_editor_state;
use std::cell::{Cell, RefCell};
use std::rc::Rc;
pub(crate) type NodeMap = KeyMap<NodeKey, Rc<dyn LexicalNode>>;
#[derive(Clone, Debug)]
pub struct EditorState(pub(crate) Rc<EditorStateInner>);
#[derive(Debug)]
pub(crate) struct EditorStateInner {
pub(crate) node_map: RefCell<NodeMap>,
pub(crate) selection: RefCell<Option<SelectionRef>>,
pub(crate) read_only: Cell<bool>,
pub(crate) flush_sync: Cell<bool>,
pub(crate) parsed: Cell<bool>,
pub(crate) slots_used: Cell<bool>,
pub(crate) id: u64,
pub(crate) type_to_node_map: RefCell<Option<Rc<crate::TypeToNodeMap>>>,
}
thread_local! {
static NEXT_EDITOR_STATE_ID: Cell<u64> = const { Cell::new(1) };
}
fn new_editor_state(node_map: NodeMap) -> EditorState {
EditorState(Rc::new(EditorStateInner {
node_map: RefCell::new(node_map),
selection: RefCell::new(None),
read_only: Cell::new(false),
flush_sync: Cell::new(false),
parsed: Cell::new(false),
slots_used: Cell::new(false),
id: NEXT_EDITOR_STATE_ID.with(|n| n.replace(n.get() + 1)),
type_to_node_map: RefCell::new(None),
}))
}
pub(crate) fn create_empty_editor_state() -> EditorState {
let mut node_map = NodeMap::default();
node_map.insert("root".to_owned(), Rc::new(RootNode::new()));
new_editor_state(node_map)
}
pub(crate) fn clone_editor_state(current: &EditorState) -> EditorState {
let state = new_editor_state(current.0.node_map.borrow().clone());
state.0.slots_used.set(current.0.slots_used.get());
state
}
fn export_node_to_json(node: &NodeRef) -> Value {
let mut serialized_node: Object = export_node_json(&*node.get_latest());
if node.is_element() {
let children: Vec<Value> = node
.get_children()
.iter()
.map(export_node_to_json)
.collect();
serialized_node.insert("children", Value::Array(children));
}
let slot_names = crate::slot::get_slot_names(node);
if !slot_names.is_empty() {
let mut serialized_slots = Object::new();
for name in slot_names {
let Some(slot_node) = crate::slot::get_slot(node, &name) else {
panic!(
"LexicalNode: Node {} has slot \"{name}\" but it resolved to no node during export.",
node.get_type()
);
};
serialized_slots.insert(&name, export_node_to_json(&slot_node));
}
serialized_node.insert("$slots", Value::Object(serialized_slots));
}
Value::Object(serialized_node)
}
impl EditorState {
pub fn is_empty(&self) -> bool {
self.0.node_map.borrow().len() <= 1 && self.0.selection.borrow().is_none()
}
pub fn selection(&self) -> Option<SelectionRef> {
self.0.selection.borrow().clone()
}
pub(crate) fn keys_in_creation_order(&self) -> Vec<String> {
let mut keys: Vec<String> = self.0.node_map.borrow().keys().cloned().collect();
keys.sort_by_key(|k| (k != "root", k.parse::<u64>().unwrap_or(u64::MAX), k.clone()));
keys
}
pub fn clone_with_selection(&self, selection: Option<Option<SelectionRef>>) -> EditorState {
let state = new_editor_state(self.0.node_map.borrow().clone());
*state.0.selection.borrow_mut() = selection.unwrap_or_else(|| self.selection());
state.0.slots_used.set(self.0.slots_used.get());
state.0.read_only.set(true);
state.0.parsed.set(self.0.parsed.get());
state
}
pub fn ptr_eq(&self, other: &EditorState) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
pub fn read<R>(&self, f: impl FnOnce() -> R) -> R {
read_editor_state(None, self, f)
}
pub fn to_json(&self, compact: bool) -> Value {
with_compact_export(compact, || {
read_editor_state(None, self, || {
let mut json = Object::new();
json.insert("root", export_node_to_json(&get_root()));
Value::Object(json)
})
})
}
}