use canvist_core::CommandResult;
use canvist_core::EditKind;
use canvist_core::EditorError;
use canvist_core::EditorState;
use canvist_core::EditorTransaction;
use canvist_core::position;
use canvist_model::Attrs;
use canvist_model::BlockId;
use canvist_model::BlockTree;
use canvist_model::BlockTypeId;
use canvist_model::Document;
use canvist_model::GridCell;
use canvist_model::GridRect;
use canvist_model::InlineText;
use canvist_model::Point;
use canvist_model::Selection;
use canvist_model::StepError;
use canvist_model::Value;
use super::context::CELL;
use super::context::HEADER;
use super::context::ROW;
use super::context::TABLE;
use super::context::TableContext;
use super::context::colwidth;
use super::context::current;
use super::context::empty_cell;
use super::context::is_named;
use super::context::selected;
use super::context::span;
use super::context::with_colwidth;
use super::context::with_span;
#[derive(Clone, Copy)]
pub(crate) struct CellKinds {
cell: BlockTypeId,
header: BlockTypeId,
}
impl CellKinds {
pub(crate) fn read(state: &EditorState) -> Result<Self, EditorError> {
Ok(Self {
cell: canvist_core::block_type(state.schema(), TABLE, CELL)?,
header: canvist_core::block_type(state.schema(), TABLE, HEADER)?,
})
}
pub(crate) const fn pick(self, header: bool) -> BlockTypeId {
if header { self.header } else { self.cell }
}
}
fn between_headers(doc: &Document, context: &TableContext, neighbours: [Option<usize>; 2]) -> bool {
let mut found = neighbours.into_iter().flatten().peekable();
found.peek().is_some() && found.all(|index| is_named(doc, context.block(index), &[HEADER]))
}
fn update_cell(
tr: &mut EditorTransaction<'_>,
cell: BlockId,
change: impl FnOnce(&Document, Attrs) -> Attrs,
) -> Result<(), EditorError> {
let Some(block) = tr.doc().block(cell) else {
return Ok(());
};
let (kind, attrs) = (block.kind(), block.attrs().clone());
let attrs = change(tr.doc(), attrs);
tr.set_block(cell, kind, attrs)?;
Ok(())
}
pub(crate) fn add_column(state: &mut EditorState, after: bool) -> CommandResult {
let Some((context, rect)) = selected(state) else {
return Ok(None);
};
let kinds = CellKinds::read(state)?;
let column = if after { rect.right } else { rect.left };
state.change(EditKind::Structure, |tr| {
insert_column(tr, &context, column, kinds)?;
Ok(true)
})
}
fn insert_column(
tr: &mut EditorTransaction<'_>,
context: &TableContext,
column: usize,
kinds: CellKinds,
) -> Result<(), EditorError> {
let map = &context.map;
let mut widened = Vec::new();
for row in 0..map.height() {
let straddling = map
.cell_at(row, column)
.filter(|&index| map.cells()[index].column < column);
if let Some(index) = straddling {
if !widened.contains(&index) {
widened.push(index);
let offset = column - map.cells()[index].column;
update_cell(tr, context.block(index), |doc, attrs| {
let block = context.block(index);
let mut widths = colwidth(doc, block);
widths.insert(offset, 0);
with_colwidth(
with_span(attrs, "colspan", span(doc, block, "colspan") + 1),
&widths,
)
})?;
}
continue;
}
let neighbours = [
column
.checked_sub(1)
.and_then(|left| map.cell_at(row, left)),
map.cell_at(row, column),
];
let kind = kinds.pick(between_headers(tr.doc(), context, neighbours));
let cell = empty_cell(tr, kind, Attrs::new())?;
tr.insert_blocks(
Some(context.rows[row]),
map.insert_index(row, column),
vec![cell],
)?;
}
Ok(())
}
pub(crate) fn delete_column(state: &mut EditorState) -> CommandResult {
let Some((context, rect)) = selected(state) else {
return Ok(None);
};
if rect.left == 0 && rect.right >= context.map.width() {
return delete_table(state);
}
state.change(EditKind::Structure, |tr| {
for column in (rect.left..rect.right).rev() {
remove_column(tr, context.table, column)?;
}
caret_near(tr, context.table, (rect.top, rect.left));
Ok(true)
})
}
fn remove_column(
tr: &mut EditorTransaction<'_>,
table: BlockId,
column: usize,
) -> Result<(), EditorError> {
let context = TableContext::new(tr.doc(), table);
for (index, cell) in context.map.cells().iter().enumerate().rev() {
if !cell.columns().contains(&column) {
continue;
}
let block = context.block(index);
if cell.colspan == 1 {
tr.remove_blocks(Some(context.rows[cell.row]), cell.index, 1)?;
continue;
}
let offset = column - cell.column;
update_cell(tr, block, |doc, attrs| {
let mut widths = colwidth(doc, block);
widths.remove(offset);
with_colwidth(with_span(attrs, "colspan", cell.colspan - 1), &widths)
})?;
}
Ok(())
}
pub(crate) fn add_row(state: &mut EditorState, after: bool) -> CommandResult {
let Some((context, rect)) = selected(state) else {
return Ok(None);
};
let kinds = CellKinds::read(state)?;
let row_kind = canvist_core::block_type(state.schema(), TABLE, ROW)?;
let row = if after { rect.bottom } else { rect.top };
state.change(EditKind::Structure, |tr| {
let cells = new_row_cells(tr, &context, row, kinds)?;
let id = tr.allocate_id()?;
tr.insert_blocks(
Some(context.table),
row,
vec![BlockTree::container(id, row_kind, cells)],
)?;
Ok(true)
})
}
fn new_row_cells(
tr: &mut EditorTransaction<'_>,
context: &TableContext,
row: usize,
kinds: CellKinds,
) -> Result<Vec<BlockTree>, EditorError> {
let map = &context.map;
let mut cells = Vec::new();
let mut column = 0;
while column < map.width() {
let straddling = map
.cell_at(row, column)
.filter(|&index| map.cells()[index].row < row);
if let Some(index) = straddling {
let cell = map.cells()[index];
update_cell(tr, context.block(index), |_, attrs| {
with_span(attrs, "rowspan", cell.rowspan + 1)
})?;
column = cell.column + cell.colspan;
continue;
}
let neighbours = [
row.checked_sub(1)
.and_then(|above| map.cell_at(above, column)),
map.cell_at(row, column),
];
let kind = kinds.pick(between_headers(tr.doc(), context, neighbours));
cells.push(empty_cell(tr, kind, Attrs::new())?);
column += 1;
}
Ok(cells)
}
pub(crate) fn delete_row(state: &mut EditorState) -> CommandResult {
let Some((context, rect)) = selected(state) else {
return Ok(None);
};
if rect.top == 0 && rect.bottom >= context.map.height() {
return delete_table(state);
}
state.change(EditKind::Structure, |tr| {
for row in (rect.top..rect.bottom).rev() {
remove_row(tr, context.table, row)?;
}
caret_near(tr, context.table, (rect.top, rect.left));
Ok(true)
})
}
fn remove_row(
tr: &mut EditorTransaction<'_>,
table: BlockId,
row: usize,
) -> Result<(), EditorError> {
let context = TableContext::new(tr.doc(), table);
let map = &context.map;
let mut moved = 0;
for (index, cell) in map.cells().iter().enumerate() {
if !cell.rows().contains(&row) || cell.rowspan == 1 {
continue;
}
let block = context.block(index);
update_cell(tr, block, |_, attrs| {
with_span(attrs, "rowspan", cell.rowspan - 1)
})?;
if cell.row == row {
let to = map.insert_index(row + 1, cell.column) + moved;
tr.move_blocks(
(Some(context.rows[row]), cell.index - moved, 1),
(Some(context.rows[row + 1]), to),
)?;
moved += 1;
}
}
tr.remove_blocks(Some(table), row, 1)?;
Ok(())
}
fn caret_near(tr: &mut EditorTransaction<'_>, table: BlockId, (row, column): (usize, usize)) {
let context = TableContext::new(tr.doc(), table);
let map = &context.map;
let row = row.min(map.height().saturating_sub(1));
let column = column.min(map.width().saturating_sub(1));
let selection = match map.cell_at(row, column) {
Some(index) => Selection::caret(Point::new(first_text(tr.doc(), context.block(index)), 0)),
None => position::valid_selection(tr.doc(), tr.selection_before()),
};
tr.set_selection(selection);
}
pub(crate) fn delete_table(state: &mut EditorState) -> CommandResult {
let Some((context, _)) = current(state) else {
return Ok(None);
};
let doc = state.doc();
let parent = position::parent_of(doc, context.table);
let Some(index) = doc.index_in_parent(context.table) else {
return Ok(None);
};
let alone = doc.children(parent).len() == 1;
let paragraph = doc.schema().default_text_block();
state.change(EditKind::Structure, |tr| {
tr.remove_blocks(parent, index, 1)?;
if alone {
let id = tr.allocate_id()?;
tr.insert_blocks(
parent,
index,
vec![BlockTree::text(id, paragraph, InlineText::new())],
)?;
}
let near = tr
.doc()
.children(parent)
.get(index.min(tr.doc().children(parent).len().saturating_sub(1)))
.copied();
let caret = near.map_or_else(
|| position::start_selection(tr.doc()),
|block| Selection::caret(Point::new(block, 0)),
);
tr.set_selection(position::valid_selection(tr.doc(), caret));
Ok(true)
})
}
pub(crate) fn merge_cells(state: &mut EditorState) -> CommandResult {
let Some((context, rect)) = selected(state) else {
return Ok(None);
};
let cells = context.blocks_in(rect);
let target = context
.map
.cell_at(rect.top, rect.left)
.map(|index| context.block(index));
let Some(target) = target.filter(|_| cells.len() >= 2) else {
return Ok(None);
};
let widths = merged_widths(state.doc(), &context, rect);
state.change(EditKind::Structure, |tr| {
move_into(tr, target, &cells)?;
update_cell(tr, target, |_, attrs| {
let attrs = with_span(attrs, "colspan", rect.right - rect.left);
with_colwidth(with_span(attrs, "rowspan", rect.bottom - rect.top), &widths)
})?;
let start = first_text(tr.doc(), target);
tr.set_selection(Selection::caret(Point::new(start, 0)));
Ok(true)
})
}
fn move_into(
tr: &mut EditorTransaction<'_>,
target: BlockId,
cells: &[BlockId],
) -> Result<(), EditorError> {
let empty_target = is_empty_cell(tr.doc(), target);
let mut moved = false;
for &cell in cells.iter().filter(|&&cell| cell != target) {
if !is_empty_cell(tr.doc(), cell) {
let count = tr.doc().children(Some(cell)).len();
let end = tr.doc().children(Some(target)).len();
tr.move_blocks((Some(cell), 0, count), (Some(target), end))?;
moved = true;
}
let (row, index) = location(tr.doc(), cell)?;
tr.remove_blocks(row, index, 1)?;
}
if empty_target && moved {
tr.remove_blocks(Some(target), 0, 1)?;
}
Ok(())
}
fn merged_widths(doc: &Document, context: &TableContext, rect: GridRect) -> Vec<i64> {
(rect.left..rect.right)
.map(|column| {
(rect.top..rect.bottom)
.filter_map(|row| context.map.cell_at(row, column))
.map(|index| {
let cell = context.map.cells()[index];
colwidth(doc, context.block(index))[column - cell.column]
})
.find(|&width| width > 0)
.unwrap_or(0)
})
.collect()
}
pub(crate) fn split_cell(state: &mut EditorState) -> CommandResult {
let Some((context, index)) = current(state) else {
return Ok(None);
};
let cell = context.map.cells()[index];
if cell.colspan == 1 && cell.rowspan == 1 {
return Ok(None);
}
let block = context.block(index);
let Some(kind) = state.doc().block(block).map(canvist_model::Block::kind) else {
return Ok(None);
};
let widths = colwidth(state.doc(), block);
state.change(EditKind::Structure, |tr| {
update_cell(tr, block, |_, attrs| {
let attrs = with_span(with_span(attrs, "colspan", 1), "rowspan", 1);
with_colwidth(attrs, &widths[..1])
})?;
for row in cell.rows() {
let top = row == cell.row;
let index = split_index(&context, cell, row);
let trees = (cell.columns().skip(usize::from(top)))
.map(|column| {
let offset = column - cell.column;
let attrs = with_colwidth(Attrs::new(), &widths[offset..=offset]);
empty_cell(tr, kind, attrs)
})
.collect::<Result<Vec<_>, _>>()?;
tr.insert_blocks(Some(context.rows[row]), index, trees)?;
}
Ok(true)
})
}
fn split_index(context: &TableContext, cell: GridCell, row: usize) -> usize {
if row == cell.row {
return cell.index + 1;
}
context.map.insert_index(row, cell.column)
}
pub(crate) fn merge_or_split(state: &mut EditorState) -> CommandResult {
match merge_cells(state)? {
Some(update) => Ok(Some(update)),
None => split_cell(state),
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum HeaderPart {
Row,
Column,
Cells,
}
pub(crate) fn toggle_header(state: &mut EditorState, part: HeaderPart) -> CommandResult {
let Some((context, rect)) = selected(state) else {
return Ok(None);
};
let kinds = CellKinds::read(state)?;
let targets = header_targets(state.doc(), &context, rect, part);
if targets.is_empty() {
return Ok(None);
}
let to_header = !targets
.iter()
.all(|&cell| is_named(state.doc(), cell, &[HEADER]));
let kind = kinds.pick(to_header);
state.change(EditKind::Structure, |tr| {
for &cell in &targets {
let Some(block) = tr.doc().block(cell) else {
continue;
};
if block.kind() != kind {
let attrs = block.attrs().clone();
tr.set_block(cell, kind, attrs)?;
}
}
Ok(true)
})
}
fn header_targets(
doc: &Document,
context: &TableContext,
rect: GridRect,
part: HeaderPart,
) -> Vec<BlockId> {
let cells = context.map.cells();
let all_headers = |first: fn(&GridCell) -> bool| {
cells
.iter()
.enumerate()
.filter(|(_, cell)| first(cell))
.all(|(index, _)| is_named(doc, context.block(index), &[HEADER]))
};
let header_row = all_headers(|cell| cell.row == 0);
let header_column = all_headers(|cell| cell.column == 0);
let selected = context.blocks_in(rect);
(0..cells.len())
.map(|index| (cells[index], context.block(index)))
.filter(|(cell, block)| {
match part {
HeaderPart::Row => cell.row == 0 && !(header_column && cell.column == 0),
HeaderPart::Column => cell.column == 0 && !(header_row && cell.row == 0),
HeaderPart::Cells => selected.contains(block),
}
})
.map(|(_, block)| block)
.collect()
}
pub(crate) fn set_cell_attribute(
state: &mut EditorState,
name: &str,
value: &Value,
) -> CommandResult {
let Some((context, rect)) = selected(state) else {
return Ok(None);
};
let cells = context.blocks_in(rect);
state.change(EditKind::Structure, |tr| {
for &cell in &cells {
update_cell(tr, cell, |_, attrs| with_attr(attrs, name, value))?;
}
Ok(true)
})
}
fn with_attr(mut attrs: Attrs, name: &str, value: &Value) -> Attrs {
if *value == Value::Null {
attrs.remove(name);
} else {
attrs.set(name, value.clone());
}
attrs
}
pub(crate) fn is_empty_cell(doc: &Document, cell: BlockId) -> bool {
match doc.children(Some(cell)) {
[only] => {
doc.block(*only)
.and_then(canvist_model::Block::inline)
.is_some_and(InlineText::is_empty)
}
children => children.is_empty(),
}
}
fn location(doc: &Document, block: BlockId) -> Result<(Option<BlockId>, usize), EditorError> {
let index = doc
.index_in_parent(block)
.ok_or(StepError::UnknownBlock(block))?;
Ok((position::parent_of(doc, block), index))
}
pub(crate) fn first_text(doc: &Document, block: BlockId) -> BlockId {
let mut current = block;
while let Some(&first) = doc.children(Some(current)).first() {
current = first;
}
current
}