#![allow(dead_code)]
use crate::extension::{LexicalEditorWithDispose, build_editor_from_extensions};
use crate::table::{
TableCellHeaderStates, compute_table_map_skip_cell_check, create_table_cell_node,
create_table_node, create_table_row_node, is_table_node, table_extension, table_nodes,
};
use crate::{
AnyLexicalExtensionArgument, Editor, InitialEditorStateType, LexicalError, LexicalExtension,
NodeRef, UpdateOptions, create_paragraph_node, create_text_node, define_extension, get_root,
};
use std::rc::Rc;
pub(super) fn discrete() -> UpdateOptions {
UpdateOptions {
discrete: true,
..UpdateOptions::default()
}
}
pub(super) fn update(editor: &Editor, f: impl FnOnce() -> Result<(), LexicalError> + 'static) {
editor.update(f, discrete()).unwrap();
}
pub(super) fn read<R>(editor: &Editor, f: impl FnOnce() -> R) -> R {
editor.read(f).unwrap()
}
pub(super) fn read_latest<R>(editor: &Editor, f: impl FnOnce() -> R) -> R {
editor
.read_with_mode(crate::EditorReadMode::Latest, f)
.unwrap()
}
pub(super) fn build_with(
name: &str,
dependencies: Vec<AnyLexicalExtensionArgument>,
theme: &str,
) -> LexicalEditorWithDispose {
build_editor_from_extensions([define_extension(LexicalExtension {
dependencies,
theme: Some(
crate::json::parse(theme)
.unwrap()
.as_object()
.unwrap()
.clone(),
),
..LexicalExtension::<(), (), ()>::new(name, ())
})
.into()])
.unwrap()
}
pub(super) fn build_table_editor(name: &str) -> LexicalEditorWithDispose {
build_with(
name,
vec![table_extension().into()],
r#"{"tableScrollableWrapper":""}"#,
)
}
pub(super) fn build_with_table_nodes(
name: &str,
initial: impl Fn() -> Result<(), LexicalError> + 'static,
) -> LexicalEditorWithDispose {
build_editor_from_extensions([define_extension(LexicalExtension {
initial_editor_state: Some(InitialEditorStateType::Update(Rc::new(move |_| initial()))),
nodes: table_nodes(),
..LexicalExtension::<(), (), ()>::new(name, ())
})
.into()])
.unwrap()
}
pub(super) fn text_paragraph(text: &str) -> NodeRef {
let paragraph = create_paragraph_node();
paragraph.append(&[create_text_node(text)]).unwrap();
paragraph
}
pub(super) fn cell_with(header_state: i32, children: &[NodeRef]) -> NodeRef {
let cell = create_table_cell_node(header_state, 1.0, None);
cell.append(children).unwrap();
cell
}
pub(super) fn text_cell(text: &str) -> NodeRef {
cell_with(TableCellHeaderStates::NO_STATUS, &[text_paragraph(text)])
}
pub(super) fn empty_cell(header_state: i32) -> NodeRef {
cell_with(header_state, &[create_paragraph_node()])
}
pub(super) fn row(cells: &[NodeRef]) -> NodeRef {
let row = create_table_row_node(None);
row.append(cells).unwrap();
row
}
pub(super) fn table(rows: &[NodeRef]) -> NodeRef {
let table = create_table_node();
table.append(rows).unwrap();
table
}
pub(super) fn table_of(rows: Vec<Vec<NodeRef>>) -> NodeRef {
table(&rows.iter().map(|cells| row(cells)).collect::<Vec<_>>())
}
pub(super) fn first_table() -> NodeRef {
get_root()
.get_children()
.into_iter()
.find(is_table_node)
.expect("a table")
}
pub(super) fn cells_of(table: &NodeRef) -> Vec<Vec<NodeRef>> {
table
.get_children()
.iter()
.map(NodeRef::get_children)
.collect()
}
pub(super) fn cell_texts(table: &NodeRef) -> Vec<Vec<String>> {
cells_of(table)
.iter()
.map(|cells| cells.iter().map(NodeRef::get_text_content).collect())
.collect()
}
pub(super) fn grid(table: &NodeRef) -> crate::table::TableMap {
compute_table_map_skip_cell_check(table, None, None)
.unwrap()
.0
}