Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2
Open History to see the last commit

note-editing.ts

167 lines5.7 KB

1import { parseLine } from "./note-body";
2import type { NoteBlock } from "./note-body";
3​
4/**
5 * The editor's text operations as pure functions: the editor shows a title
6 * field and one field per body block, and these decide what Enter,
7 * Backspace, typing and pasting do to them.
8 */
9​
10export interface NoteText {
11 title: string;
12 blocks: NoteBlock[];
14​
15/** Where the caret goes after an edit. `field` -1 is the title, 0… the body blocks. */
16export interface Caret {
17 field: number;
18 offset: number;
20​
21export interface NoteTextEdit extends NoteText {
22 caret: Caret;
24​
25const TITLE = -1;
26​
27function replaceAt<T>(items: T[], index: number, item: T): T[] {
28 return items.map((existing, i) => (i === index ? item : existing));
30​
31function withText(block: NoteBlock, text: string): NoteBlock {
32 return { ...block, text };
34​
35/** The block a new line after `block` starts as: lists continue, everything else is a paragraph. */
36function continuation(block: NoteBlock, text: string): NoteBlock {
37 if (block.type === "bullet") return { type: "bullet", text };
38 if (block.type === "checklist") return { type: "checklist", text, checked: false };
39 return { type: "paragraph", text };
41​
42function isEmptyBody(blocks: NoteBlock[]): boolean {
43 return blocks.length === 1 && blocks[0]!.type === "paragraph" && blocks[0]!.text === "";
45​
46/** Typing or pasting into the title. Pasted lines after the first go into the body. */
47export function setTitle({ blocks }: NoteText, value: string, caretOffset: number): NoteTextEdit {
48 const [title = "", ...rest] = value.split("\n");
49 if (rest.length === 0) return { title, blocks, caret: { field: TITLE, offset: caretOffset } };
50 const pasted = rest.map(parseLine);
51 return {
52 title,
53 blocks: isEmptyBody(blocks) ? pasted : [...pasted, ...blocks],
54 caret: { field: pasted.length - 1, offset: pasted.at(-1)!.text.length },
55 };
57​
58/** Typing or pasting into a body block. Pasted lines become blocks of their own. */
59export function setBlockText(
60 { title, blocks }: NoteText,
61 index: number,
62 value: string,
63 caretOffset: number,
64): NoteTextEdit {
65 const block = blocks[index]!;
66 const [first = "", ...rest] = value.split("\n");
67 // Markdown-style shortcuts: "- " starts a bullet, "[ ] " a checklist item, "# " a heading.
68 const updated = block.type === "paragraph" ? parseLine(first) : withText(block, first);
69 const shortcut = updated.type !== block.type;
70 const pasted = rest.map(parseLine);
71 return {
72 title,
73 blocks: [...blocks.slice(0, index), updated, ...pasted, ...blocks.slice(index + 1)],
74 caret:
75 pasted.length > 0
76 ? { field: index + pasted.length, offset: pasted.at(-1)!.text.length }
77 : { field: index, offset: shortcut ? updated.text.length : caretOffset },
78 };
80​
81/** Enter in the title: the rest of the title becomes the first body line. */
82export function splitTitle({ title, blocks }: NoteText, start: number, end: number): NoteTextEdit {
83 const after = title.slice(end);
84 const first: NoteBlock = { type: "paragraph", text: after };
85 return {
86 title: title.slice(0, start),
87 blocks: isEmptyBody(blocks) ? [first] : [first, ...blocks],
88 caret: { field: 0, offset: 0 },
89 };
91​
92/** Enter in a block: split it at the caret. Enter on an empty list item ends the list. */
93export function splitBlock(
94 { title, blocks }: NoteText,
95 index: number,
96 start: number,
97 end: number,
98): NoteTextEdit {
99 const block = blocks[index]!;
100 if (block.type !== "paragraph" && block.text === "") {
101 const paragraph: NoteBlock = { type: "paragraph", text: "" };
102 return {
103 title,
104 blocks: replaceAt(blocks, index, paragraph),
105 caret: { field: index, offset: 0 },
106 };
107 }
108 const before = withText(block, block.text.slice(0, start));
109 const after = continuation(block, block.text.slice(end));
110 return {
111 title,
112 blocks: [...blocks.slice(0, index), before, after, ...blocks.slice(index + 1)],
113 caret: { field: index + 1, offset: 0 },
114 };
116​
117/**
118 * Backspace at the very start of a block: a list item or heading turns into
119 * a paragraph first; a paragraph joins the line above (or the title).
120 */
121export function joinWithPrevious({ title, blocks }: NoteText, index: number): NoteTextEdit {
122 const block = blocks[index]!;
123 if (block.type !== "paragraph") {
124 return {
125 title,
126 blocks: replaceAt(blocks, index, { type: "paragraph", text: block.text }),
127 caret: { field: index, offset: 0 },
128 };
129 }
130 const rest = blocks.filter((_, i) => i !== index);
131 const remaining = rest.length > 0 ? rest : [{ type: "paragraph", text: "" } satisfies NoteBlock];
132 if (index === 0) {
133 return {
134 title: title + block.text,
135 blocks: remaining,
136 caret: { field: TITLE, offset: title.length },
137 };
138 }
139 const previous = blocks[index - 1]!;
140 return {
141 title,
142 blocks: replaceAt(rest, index - 1, withText(previous, previous.text + block.text)),
143 caret: { field: index - 1, offset: previous.text.length },
144 };
146​
147/** The checklist button: turn the block into a checklist item, or back into a paragraph. */
148export function toggleChecklist({ title, blocks }: NoteText, index: number): NoteTextEdit {
149 const block = blocks[index]!;
150 const toggled: NoteBlock =
151 block.type === "checklist"
152 ? { type: "paragraph", text: block.text }
153 : { type: "checklist", text: block.text, checked: false };
154 return {
155 title,
156 blocks: replaceAt(blocks, index, toggled),
157 caret: { field: index, offset: block.text.length },
158 };
160​
161/** Ticks or unticks a checklist item. */
162export function toggleChecked(blocks: NoteBlock[], index: number): NoteBlock[] {
163 const block = blocks[index]!;
164 return block.type === "checklist"
165 ? replaceAt(blocks, index, { ...block, checked: !block.checked })
166 : blocks;