canvist_core 0.2.0

Platform-agnostic editing engine for canvist: editor state, commands, selection motion, keymaps, input rules, and history
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Structural building blocks shared by commands: splitting containers,
//! lifting and wrapping sibling ranges, merging text blocks across
//! containers, and removing what becomes empty.
//!
//! Each helper emits model steps through an [`EditorTransaction`] and reads
//! the document as changed so far, so commands can chain them freely.

use std::ops::Range;

use canvist_model::Attrs;
use canvist_model::BlockId;
use canvist_model::BlockTree;
use canvist_model::BlockTypeId;
use canvist_model::ContentKind;
use canvist_model::Document;
use canvist_model::Point;
use canvist_model::Schema;
use canvist_model::Step;
use canvist_model::StepError;
use canvist_model::TreeContent;

use crate::EditorError;
use crate::EditorTransaction;
use crate::position;
use crate::position::parent_of;

/// The kind of block `id`.
pub(crate) fn kind_of(doc: &Document, id: BlockId) -> Option<BlockTypeId> {
	doc.block(id).map(canvist_model::Block::kind)
}

/// The type and attributes of block `id`.
pub(crate) fn type_of(doc: &Document, id: BlockId) -> Option<(BlockTypeId, Attrs)> {
	doc.block(id)
		.map(|block| (block.kind(), block.attrs().clone()))
}

/// Whether `parent` (`None` is the root) may hold a block of `kind`.
pub(crate) fn allows(doc: &Document, parent: Option<BlockId>, kind: BlockTypeId) -> bool {
	let parent_kind = match parent {
		Some(id) => {
			let Some(kind) = kind_of(doc, id) else {
				return false;
			};
			Some(kind)
		}
		None => None,
	};

	doc.schema().allows_child(parent_kind, kind)
}

/// The first text block type `parent` accepts: what Enter creates there.
pub(crate) fn default_text_type(doc: &Document, parent: Option<BlockId>) -> Option<BlockTypeId> {
	match parent {
		Some(id) => text_type_in(doc.schema(), Some(kind_of(doc, id)?)),
		None => text_type_in(doc.schema(), None),
	}
}

/// The first text block type a `parent` type accepts (`None` is the root).
pub(crate) fn text_type_in(schema: &Schema, parent: Option<BlockTypeId>) -> Option<BlockTypeId> {
	let Some(parent) = parent else {
		return Some(schema.default_text_block());
	};
	let ContentKind::Blocks(allowed) = &schema.block(parent).content else {
		return None;
	};

	allowed
		.iter()
		.copied()
		.find(|&kind| schema.block(kind).content == ContentKind::Inline)
}

/// The default attributes of a block type.
pub(crate) fn default_attrs(doc: &Document, kind: BlockTypeId) -> Attrs {
	doc.schema().block(kind).attrs.clone()
}

/// An empty block tree of `kind` with a fresh id and default attributes.
pub(crate) fn fresh_block(
	tr: &mut EditorTransaction<'_>,
	kind: BlockTypeId,
) -> Result<BlockTree, EditorError> {
	let id = tr.allocate_id()?;
	let attrs = default_attrs(tr.doc(), kind);

	let content = match tr.doc().schema().block(kind).content {
		ContentKind::Inline => TreeContent::Inline(canvist_model::InlineText::new()),
		ContentKind::Blocks(_) => TreeContent::Blocks(Vec::new()),
		ContentKind::Leaf => TreeContent::Leaf,
	};

	Ok(BlockTree {
		id,
		kind,
		attrs,
		content,
	})
}

/// An empty copy of container `id` (same type and attributes, fresh id).
pub(crate) fn empty_copy(
	tr: &mut EditorTransaction<'_>,
	id: BlockId,
) -> Result<BlockTree, EditorError> {
	let block = tr.doc().block(id).ok_or(StepError::UnknownBlock(id))?;
	let (kind, attrs) = (block.kind(), block.attrs().clone());
	let id = tr.allocate_id()?;
	Ok(BlockTree::container(id, kind, Vec::new()).with_attrs(attrs))
}

/// The position of `id` among its siblings.
pub(crate) fn index_of(doc: &Document, id: BlockId) -> Result<usize, EditorError> {
	Ok(doc.index_in_parent(id).ok_or(StepError::UnknownBlock(id))?)
}

/// Move `container`'s children from `at` onwards into a new copy of it
/// placed right after it. Returns the copy, or `None` if nothing moved.
pub(crate) fn split_container(
	tr: &mut EditorTransaction<'_>,
	container: BlockId,
	at: usize,
) -> Result<Option<BlockId>, EditorError> {
	let len = tr.doc().children(Some(container)).len();

	if at >= len {
		return Ok(None);
	}

	let parent = parent_of(tr.doc(), container);
	let index = index_of(tr.doc(), container)?;
	let copy = empty_copy(tr, container)?;
	let copy_id = copy.id;
	tr.insert_blocks(parent, index + 1, vec![copy])?;
	tr.move_blocks((Some(container), at, len - at), (Some(copy_id), 0))?;
	Ok(Some(copy_id))
}

/// Whether the children `range` of `container` may move into its parent.
pub(crate) fn can_lift(doc: &Document, container: BlockId, range: &Range<usize>) -> bool {
	let Some(block) = doc.block(container) else {
		return false;
	};
	let target = block.parent();
	let children = doc.children(Some(container));

	children.get(range.clone()).is_some_and(|moved| {
		moved
			.iter()
			.filter_map(|&child| kind_of(doc, child))
			.all(|kind| allows(doc, target, kind))
	})
}

/// Lift the children `range` of `container` out into its parent, splitting
/// the container around them. The caller checks [`can_lift`].
pub(crate) fn lift(
	tr: &mut EditorTransaction<'_>,
	container: BlockId,
	range: Range<usize>,
) -> Result<(), EditorError> {
	let target = parent_of(tr.doc(), container);
	let index = index_of(tr.doc(), container)?;
	split_container(tr, container, range.end)?;
	tr.move_blocks(
		(Some(container), range.start, range.len()),
		(target, index + 1),
	)?;

	if tr.doc().children(Some(container)).is_empty() {
		tr.remove_blocks(target, index, 1)?;
	}

	Ok(())
}

/// Whether children `range` of `parent` can be wrapped in a `kind` block.
pub(crate) fn can_wrap(
	doc: &Document,
	parent: Option<BlockId>,
	range: &Range<usize>,
	kind: BlockTypeId,
) -> bool {
	let children = doc.children(parent);

	allows(doc, parent, kind)
		&& children.get(range.clone()).is_some_and(|wrapped| {
			wrapped.iter().all(|&child| {
				kind_of(doc, child)
					.is_some_and(|child| doc.schema().allows_child(Some(kind), child))
			})
		})
}

/// Wrap children `range` of `parent` in a new `kind` block with `attrs`,
/// returning the wrapper. The caller checks [`can_wrap`].
pub(crate) fn wrap(
	tr: &mut EditorTransaction<'_>,
	parent: Option<BlockId>,
	range: Range<usize>,
	kind: BlockTypeId,
	attrs: Attrs,
) -> Result<BlockId, EditorError> {
	let wrapper = fresh_block(tr, kind)?.with_attrs(attrs);
	let id = wrapper.id;
	tr.insert_blocks(parent, range.start, vec![wrapper])?;
	tr.move_blocks((parent, range.start + 1, range.len()), (Some(id), 0))?;
	Ok(id)
}

/// Remove block `id` and its descendants.
pub(crate) fn remove(tr: &mut EditorTransaction<'_>, id: BlockId) -> Result<(), EditorError> {
	let parent = parent_of(tr.doc(), id);
	let index = index_of(tr.doc(), id)?;
	tr.remove_blocks(parent, index, 1)?;
	Ok(())
}

/// Remove `container` and each ancestor that is left without children.
pub(crate) fn prune_empty(
	tr: &mut EditorTransaction<'_>,
	container: Option<BlockId>,
) -> Result<(), EditorError> {
	let mut current = container;

	while let Some(id) = current {
		let empty = tr.doc().contains(id)
			&& tr.doc().inline(id).is_none()
			&& !position::is_leaf(tr.doc(), id)
			&& tr.doc().children(Some(id)).is_empty();

		if !empty {
			break;
		}

		current = parent_of(tr.doc(), id);
		remove(tr, id)?;
	}

	Ok(())
}

/// Append the content of text block `source` to text block `target`, which
/// comes before it in the document, and remove `source`.
///
/// Uses a `Join` step whenever the blocks can become siblings, so points in
/// `source` map onto `target`. When `source`'s old container has the same
/// type as `target`'s (two list items, two quotes), its remaining children
/// follow into `target`'s container, the way joining list items works in
/// every word processor. Returns the point where the two texts meet.
pub(crate) fn merge_text(
	tr: &mut EditorTransaction<'_>,
	target: BlockId,
	source: BlockId,
) -> Result<Point, EditorError> {
	let doc = tr.doc();
	let meet = position::end_of(doc, target);
	let target_parent = parent_of(doc, target);
	let source_parent = parent_of(doc, source);
	let source_kind = kind_of(doc, source).ok_or(StepError::UnknownBlock(source))?;
	let source_index = index_of(doc, source)?;
	let adjacent = target_parent == source_parent
		&& source_index
			.checked_sub(1)
			.is_some_and(|previous| doc.children(source_parent)[previous] == target);

	if adjacent {
		tr.join(target, source)?;
	} else if allows(doc, target_parent, source_kind) {
		let target_index = index_of(doc, target)?;
		tr.move_blocks(
			(source_parent, source_index, 1),
			(target_parent, target_index + 1),
		)?;
		tr.join(target, source)?;
	} else {
		let content = doc.inline(source).cloned().unwrap_or_default();
		tr.step(Step::InsertInline {
			block: target,
			offset: meet.offset,
			content,
		})?;

		remove(tr, source)?;
	}

	adopt_children(tr, target_parent, source_parent)?;
	prune_empty(tr, source_parent)?;
	Ok(meet)
}

/// Move the children of `from` to the end of `into` when both containers
/// share a type, then drop `from`.
fn adopt_children(
	tr: &mut EditorTransaction<'_>,
	into: Option<BlockId>,
	from: Option<BlockId>,
) -> Result<(), EditorError> {
	let (Some(into), Some(from)) = (into, from) else {
		return Ok(());
	};
	let doc = tr.doc();
	let same_kind = into != from && doc.contains(from) && kind_of(doc, into) == kind_of(doc, from);
	let count = doc.children(Some(from)).len();

	if !same_kind || count == 0 || doc.is_ancestor(from, into) {
		return Ok(());
	}

	let end = doc.children(Some(into)).len();
	tr.move_blocks((Some(from), 0, count), (Some(into), end))?;
	Ok(())
}

/// Delete everything between two text points, merging their blocks.
/// `from` must come before `to`. Returns where the caret belongs.
pub(crate) fn delete_range(
	tr: &mut EditorTransaction<'_>,
	from: Point,
	to: Point,
) -> Result<Point, EditorError> {
	if from.block == to.block {
		if from.offset < to.offset {
			tr.delete_text(from.block, from.offset..to.offset)?;
		}

		return Ok(from);
	}

	let covered = covered_subtrees(tr.doc(), from.block, to.block);
	tr.delete_text(to.block, 0..to.offset)?;
	let from_end = position::text_len(tr.doc(), from.block);
	tr.delete_text(from.block, from.offset..from_end)?;

	for id in covered.into_iter().rev() {
		remove(tr, id)?;
	}

	merge_text(tr, from.block, to.block)
}

/// The largest subtrees lying entirely between text blocks `first` and
/// `last`, in document order.
fn covered_subtrees(doc: &Document, first: BlockId, last: BlockId) -> Vec<BlockId> {
	let mut covered = Vec::new();
	let mut current = doc.next_block(first);

	while let Some(block) = current {
		if block == last {
			break;
		}

		if doc.is_ancestor(block, last) {
			current = doc.next_block(block);
			continue;
		}

		covered.push(block);
		current = doc.next_block(position::last_descendant(doc, block));
	}

	covered
}

#[cfg(test)]
mod tests {
	use canvist_model::Block;
	use canvist_model::Selection;

	use super::*;
	use crate::EditKind;
	use crate::testing;

	fn texts(doc: &Document) -> Vec<BlockId> {
		doc.text_blocks().map(Block::id).collect()
	}

	#[test]
	fn deleting_across_a_list_merges_the_ends() {
		let mut state = testing::nested_state();
		let ids = texts(state.doc());
		let update = state
			.change(EditKind::Deleting, |tr| {
				let caret = delete_range(tr, Point::new(ids[0], 2), Point::new(ids[3], 1))?;
				tr.set_selection(Selection::caret(caret));
				Ok(true)
			})
			.expect("valid range")
			.expect("applied");

		assert_eq!(state.doc().plain_text(), "inutro");
		assert_eq!(state.doc().children(None).len(), 1);
		assert_eq!(update.selection, Selection::caret(Point::new(ids[0], 2)));
	}

	#[test]
	fn lifting_the_middle_of_a_container_splits_it() {
		let mut state = testing::nested_state();
		let quote = state.schema().block_type("blockquote").expect("quote");
		state
			.change(EditKind::Structure, |tr| {
				let wrapper = wrap(tr, None, 0..3, quote, Attrs::new())?;
				lift(tr, wrapper, 1..2)?;
				Ok(true)
			})
			.expect("valid lift");

		let kinds: Vec<_> = state
			.doc()
			.children(None)
			.iter()
			.filter_map(|&id| kind_of(state.doc(), id))
			.map(|kind| state.schema().block(kind).name.to_string())
			.collect();
		assert_eq!(kinds, ["blockquote", "bulletList", "blockquote"]);
	}
}