|
| 1 | +import {Cursor} from '../slice/Cursor'; |
| 2 | +import {Anchor, SliceBehavior} from '../constants'; |
| 3 | +import {tick, type ITimestampStruct} from '../../../json-crdt-patch/clock'; |
| 4 | +import {PersistedSlice} from '../slice/PersistedSlice'; |
| 5 | +import type {Range} from '../slice/Range'; |
| 6 | +import type {Peritext} from '../Peritext'; |
| 7 | +import type {Printable} from '../../../util/print/types'; |
| 8 | +import type {Point} from '../point/Point'; |
| 9 | +import type {SliceType} from '../types'; |
| 10 | + |
| 11 | +export class Editor implements Printable { |
| 12 | + /** |
| 13 | + * Cursor is the the current user selection. It can be a caret or a |
| 14 | + * range. If range is collapsed to a single point, it is a caret. |
| 15 | + */ |
| 16 | + public readonly cursor: Cursor; |
| 17 | + |
| 18 | + constructor(public readonly txt: Peritext) { |
| 19 | + const point = txt.point(txt.str.id, Anchor.After); |
| 20 | + const cursorId = txt.str.id; // TODO: should be autogenerated to something else |
| 21 | + this.cursor = new Cursor(cursorId, txt, point, point.clone()); |
| 22 | + } |
| 23 | + |
| 24 | + /** @deprecated */ |
| 25 | + public setCursor(start: number, length: number = 0): void { |
| 26 | + this.cursor.setAt(start, length); |
| 27 | + } |
| 28 | + |
| 29 | + /** @deprecated */ |
| 30 | + public getCursorText(): string { |
| 31 | + return this.cursor.text(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Ensures there is no range selection. If user has selected a range, |
| 36 | + * the contents is removed and the cursor is set at the start of the range as cursor. |
| 37 | + * |
| 38 | + * @todo If block boundaries are withing the range, remove the blocks. |
| 39 | + * |
| 40 | + * @returns Returns the cursor position after the operation. |
| 41 | + */ |
| 42 | + public collapseSelection(): ITimestampStruct { |
| 43 | + const cursor = this.cursor; |
| 44 | + const isCaret = cursor.isCollapsed(); |
| 45 | + if (!isCaret) { |
| 46 | + const {start, end} = cursor; |
| 47 | + const txt = this.txt; |
| 48 | + const deleteStartId = start.anchor === Anchor.Before ? start.id : start.nextId(); |
| 49 | + const deleteEndId = end.anchor === Anchor.After ? end.id : end.prevId(); |
| 50 | + const str = txt.str; |
| 51 | + if (!deleteStartId || !deleteEndId) throw new Error('INVALID_RANGE'); |
| 52 | + const range = str.findInterval2(deleteStartId, deleteEndId); |
| 53 | + const model = txt.model; |
| 54 | + const api = model.api; |
| 55 | + api.builder.del(str.id, range); |
| 56 | + api.apply(); |
| 57 | + if (start.anchor === Anchor.After) cursor.setCaret(start.id); |
| 58 | + else cursor.setCaret(start.prevId() || str.id); |
| 59 | + } |
| 60 | + return cursor.start.id; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Insert inline text at current cursor position. If cursor selects a range, |
| 65 | + * the range is removed and the text is inserted at the start of the range. |
| 66 | + */ |
| 67 | + public insert(text: string): void { |
| 68 | + if (!text) return; |
| 69 | + const after = this.collapseSelection(); |
| 70 | + const textId = this.txt.ins(after, text); |
| 71 | + this.cursor.setCaret(textId, text.length - 1); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Deletes the previous character at current cursor position. If cursor |
| 76 | + * selects a range, deletes the whole range. |
| 77 | + */ |
| 78 | + public delete(): void { |
| 79 | + const isCollapsed = this.cursor.isCollapsed(); |
| 80 | + if (isCollapsed) { |
| 81 | + const range = this.txt.findCharBefore(this.cursor.start); |
| 82 | + if (!range) return; |
| 83 | + this.cursor.set(range.start, range.end); |
| 84 | + } |
| 85 | + this.collapseSelection(); |
| 86 | + } |
| 87 | + |
| 88 | + public start(): Point | undefined { |
| 89 | + const txt = this.txt; |
| 90 | + const str = txt.str; |
| 91 | + if (!str.length()) return; |
| 92 | + const firstChunk = str.first(); |
| 93 | + if (!firstChunk) return; |
| 94 | + const firstId = firstChunk.id; |
| 95 | + const start = txt.point(firstId, Anchor.Before); |
| 96 | + return start; |
| 97 | + } |
| 98 | + |
| 99 | + public end(): Point | undefined { |
| 100 | + const txt = this.txt; |
| 101 | + const str = txt.str; |
| 102 | + if (!str.length()) return; |
| 103 | + const lastChunk = str.last(); |
| 104 | + if (!lastChunk) return; |
| 105 | + const lastId = lastChunk.span > 1 ? tick(lastChunk.id, lastChunk.span - 1) : lastChunk.id; |
| 106 | + const end = txt.point(lastId, Anchor.After); |
| 107 | + return end; |
| 108 | + } |
| 109 | + |
| 110 | + public all(): Range | undefined { |
| 111 | + const start = this.start(); |
| 112 | + const end = this.end(); |
| 113 | + if (!start || !end) return; |
| 114 | + return this.txt.range(start, end); |
| 115 | + } |
| 116 | + |
| 117 | + public selectAll(): void { |
| 118 | + const range = this.all(); |
| 119 | + if (range) this.cursor.setRange(range); |
| 120 | + } |
| 121 | + |
| 122 | + public insertSlice(type: SliceType, data?: unknown | ITimestampStruct): PersistedSlice { |
| 123 | + return this.txt.insSlice(this.cursor, SliceBehavior.Stack, type, data); |
| 124 | + } |
| 125 | +} |
0 commit comments