//! Side-by-side groups of independently editable blocks. use gpdoc_plugin::core::{BoundaryEdit, ReplaceAroundStep}; use gpdoc_plugin::prelude::*; use gpdoc_plugin::selection::{Located, path_at, selected_siblings}; #[cfg(feature = "gpui")] mod view; #[cfg(feature = "gpui")] pub use view::{ColumnTrack, column_layout}; const DEFAULT_WEIGHT: i64 = 10_000; /// Wrap the current block or adjacent selected blocks in the first column. #[derive(Clone, Copy, Debug)] pub enum Set { Two = 2, Three, Four, Five, } impl Command for Set { const NAME: &'static str = "setColumns"; } unit_command!(pub AddAfter, "addLayoutColumnAfter"); unit_command!(pub Unset, "unsetColumns"); pub fn columns() -> Plugin { let plugin = Plugin::new("columns", |p| { p.node( NodeSpec::block("columns") .group("block") .content("column{2,}"), ); p.node( NodeSpec::block("column") .content("block+") .attrs(AttrSpecs::from([( "weight".into(), AttrSpec::with_default(AttrType::Integer, DEFAULT_WEIGHT.into()), )])), ); p.drag_handles_in_children("columns"); p.command::(set); p.command::(|ctx, _| { let Some(col) = containing(ctx, "column") else { return Ok(None); }; let at = col.end(); edit( ctx, Edit::ReplaceNodes { range: DocPos(at)..DocPos(at), nodes: vec![empty_column(ctx)], }, Selection::caret(DocPos(at + 2)), ) }); p.command::(|ctx, _| { let Some(group) = containing(ctx, "columns") else { return Ok(None); }; // Remove only wrapper boundaries so positions inside every column survive. let before = group.before; let mut at = before; let mut boundaries = Vec::new(); for column in group.node.content.children() { boundaries.push(BoundaryEdit { range: DocPos(at)..DocPos(at + 2), insert: vec![], }); at += column.size(); } boundaries.push(BoundaryEdit { range: DocPos(at)..DocPos(group.end()), insert: vec![], }); let first = group.node.child(0).child(0); let selection = if first.inline_content() { Selection::caret(DocPos(before + 1)) } else { Selection::Node { before: DocPos(before), } }; edit(ctx, Edit::Boundaries(boundaries), selection) }); }); #[cfg(feature = "gpui")] let plugin = { use gpdoc::PluginViewExt; plugin .with_metadata(view::DRAG_LAYOUT) .gpui_default_view(|| column_layout(|track, _, _| track)) }; plugin } fn containing<'a>(ctx: CommandContext<'a>, kind: &str) -> Option> { let range = ctx.selection().bounds(ctx.document()); path_at(ctx.document().blocks(), range.start) .into_iter() .rev() .find(|b| b.node.kind == kind && range.end.0 <= b.end()) } fn empty_column(ctx: CommandContext<'_>) -> Node { Node::container( "column", vec![Node::textblock(ctx.config().default_text_block(), "")], ) } fn set(ctx: CommandContext<'_>, count: Set) -> Result, Error> { let blocks = selected_siblings(ctx); let (Some(first), Some(last)) = (blocks.first(), blocks.last()) else { return Ok(None); }; let from = first.before; let to = last.end(); let empty = ctx.schema().bind_node(&empty_column(ctx))?; let mut cols = vec![empty.clone(); count as usize]; // The replacement gap supplies the first column's original blocks. cols[0] = empty.copy(Fragment::default()); let group = ctx .schema() .node("columns", Attrs::new(), cols.into(), vec![])?; let caret = if blocks.len() == 1 && first.node.inline_content() && first.node.content.size() == 0 { from + 3 } else { to + 5 }; edit( ctx, Step::ReplaceAround(ReplaceAroundStep { from, to, gap_from: from, gap_to: to, slice: Slice::new(group.into(), 0, 0), insert: 2, structure: true, }), Selection::caret(DocPos(caret)), ) } fn edit( ctx: CommandContext<'_>, edit: impl Into, selection: Selection, ) -> Result, Error> { let mut tx = ctx.one_step(edit); if tx.document().is_err() { return Ok(None); } tx.select(selection); Ok(Some(tx)) }