foldkit-richtext 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/clipboard.d.ts +224 -0
- package/dist/clipboard.d.ts.map +1 -0
- package/dist/clipboard.js +208 -0
- package/dist/clipboard.js.map +1 -0
- package/dist/code.d.ts +27 -0
- package/dist/code.d.ts.map +1 -0
- package/dist/code.js +45 -0
- package/dist/code.js.map +1 -0
- package/dist/command.d.ts +300 -0
- package/dist/command.d.ts.map +1 -0
- package/dist/command.js +1438 -0
- package/dist/command.js.map +1 -0
- package/dist/decoration.d.ts +44 -0
- package/dist/decoration.d.ts.map +1 -0
- package/dist/decoration.js +96 -0
- package/dist/decoration.js.map +1 -0
- package/dist/document.d.ts +1045 -0
- package/dist/document.d.ts.map +1 -0
- package/dist/document.js +544 -0
- package/dist/document.js.map +1 -0
- package/dist/footnote.d.ts +35 -0
- package/dist/footnote.d.ts.map +1 -0
- package/dist/footnote.js +100 -0
- package/dist/footnote.js.map +1 -0
- package/dist/history.d.ts +470 -0
- package/dist/history.d.ts.map +1 -0
- package/dist/history.js +81 -0
- package/dist/history.js.map +1 -0
- package/dist/html.d.ts +27 -0
- package/dist/html.d.ts.map +1 -0
- package/dist/html.js +99 -0
- package/dist/html.js.map +1 -0
- package/dist/indent.d.ts +16 -0
- package/dist/indent.d.ts.map +1 -0
- package/dist/indent.js +101 -0
- package/dist/indent.js.map +1 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +24 -0
- package/dist/input.d.ts +54 -0
- package/dist/input.d.ts.map +1 -0
- package/dist/input.js +37 -0
- package/dist/input.js.map +1 -0
- package/dist/kit.d.ts +179 -0
- package/dist/kit.d.ts.map +1 -0
- package/dist/kit.js +202 -0
- package/dist/kit.js.map +1 -0
- package/dist/marks.d.ts +79 -0
- package/dist/marks.d.ts.map +1 -0
- package/dist/marks.js +127 -0
- package/dist/marks.js.map +1 -0
- package/dist/media.d.ts +30 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/media.js +148 -0
- package/dist/media.js.map +1 -0
- package/dist/migration.d.ts +55 -0
- package/dist/migration.d.ts.map +1 -0
- package/dist/migration.js +124 -0
- package/dist/migration.js.map +1 -0
- package/dist/outline.d.ts +15 -0
- package/dist/outline.d.ts.map +1 -0
- package/dist/outline.js +39 -0
- package/dist/outline.js.map +1 -0
- package/dist/rendering.d.ts +96 -0
- package/dist/rendering.d.ts.map +1 -0
- package/dist/rendering.js +106 -0
- package/dist/rendering.js.map +1 -0
- package/dist/replicated.d.ts +340 -0
- package/dist/replicated.d.ts.map +1 -0
- package/dist/replicated.js +1727 -0
- package/dist/replicated.js.map +1 -0
- package/dist/search.d.ts +28 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +61 -0
- package/dist/search.js.map +1 -0
- package/dist/standard.d.ts +61 -0
- package/dist/standard.d.ts.map +1 -0
- package/dist/standard.js +231 -0
- package/dist/standard.js.map +1 -0
- package/dist/table.d.ts +38 -0
- package/dist/table.d.ts.map +1 -0
- package/dist/table.js +160 -0
- package/dist/table.js.map +1 -0
- package/dist/transaction.d.ts +712 -0
- package/dist/transaction.d.ts.map +1 -0
- package/dist/transaction.js +993 -0
- package/dist/transaction.js.map +1 -0
- package/dist/transform.d.ts +48 -0
- package/dist/transform.d.ts.map +1 -0
- package/dist/transform.js +94 -0
- package/dist/transform.js.map +1 -0
- package/dist/url.d.ts +16 -0
- package/dist/url.d.ts.map +1 -0
- package/dist/url.js +32 -0
- package/dist/url.js.map +1 -0
- package/package.json +6 -6
- package/dist/index.d.mts +0 -3787
- package/dist/index.d.mts.map +0 -1
- package/dist/index.mjs +0 -6688
- package/dist/index.mjs.map +0 -1
- package/dist/rolldown-runtime-D8jQpDG7.mjs +0 -12
package/dist/standard.js
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { Option, Schema } from "effect";
|
|
2
|
+
import { blockContent, rangeStart, textContent } from "./document.js";
|
|
3
|
+
import { Bold, Code, Italic, mark, markProps } from "./marks.js";
|
|
4
|
+
import { atom, block, blocksOf, node } from "./kit.js";
|
|
5
|
+
import { rendering } from "./rendering.js";
|
|
6
|
+
import { markExtent } from "./command.js";
|
|
7
|
+
import { safeUrl } from "./url.js";
|
|
8
|
+
import { ImageAsset } from "foldkit-media";
|
|
9
|
+
// src/standard.ts
|
|
10
|
+
/**
|
|
11
|
+
* The standard semantic vocabulary (§124 §2, §125): the node kinds and marks a
|
|
12
|
+
* document uses to mean what Markdown and HTML also mean, so an interpreter is a
|
|
13
|
+
* mapping from this vocabulary rather than a private one. Declarations only — no
|
|
14
|
+
* renderers, no executable code (§34) — and an application spreads them into its Kit
|
|
15
|
+
* and adds its own:
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* RichText.kit({
|
|
19
|
+
* nodes: [...RichText.standardNodes, Callout],
|
|
20
|
+
* marks: [...RichText.standardMarks, Highlight],
|
|
21
|
+
* })
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* `Paragraph` and `Heading` are built-in text blocks, `Code` is the inline-code mark,
|
|
25
|
+
* and the rest are ordinary declarations. `HardBreak` is deliberately absent: it is
|
|
26
|
+
* inline content, and the model has no inline atoms yet (§116, deferred).
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* A struck-through span. It expands `after`, so typing at its edge continues it, as
|
|
30
|
+
* bold and italic do.
|
|
31
|
+
*/
|
|
32
|
+
const Strikethrough = mark("Strikethrough", { expand: "after" });
|
|
33
|
+
/**
|
|
34
|
+
* A hyperlink. Its `href` is a prop rather than a mark name, which is what lets the
|
|
35
|
+
* serializer render a real `<a href>` (§121); it expands `none`, because text typed
|
|
36
|
+
* after a link is new text, not a continuation of the link.
|
|
37
|
+
*/
|
|
38
|
+
const Link = mark("Link", {
|
|
39
|
+
Props: Schema.Struct({ href: Schema.String }),
|
|
40
|
+
expand: "none"
|
|
41
|
+
});
|
|
42
|
+
/**
|
|
43
|
+
* The link a selection starts in, which is what a link editor opens on: at a caret, the
|
|
44
|
+
* link around it; over a range, the link its start is in. Undefined outside a link, and for
|
|
45
|
+
* a node selection, which covers blocks rather than text.
|
|
46
|
+
*/
|
|
47
|
+
const linkAt = (document, selection) => {
|
|
48
|
+
if (selection?.type !== "Range") return void 0;
|
|
49
|
+
const start = rangeStart(document, selection);
|
|
50
|
+
const extent = start === void 0 ? void 0 : markExtent(document, start, Link.name);
|
|
51
|
+
if (extent === void 0) return void 0;
|
|
52
|
+
const href = markProps(extent.mark)?.href;
|
|
53
|
+
return {
|
|
54
|
+
href: typeof href === "string" ? href : "",
|
|
55
|
+
selection: extent.selection
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
/** Every mark the standard vocabulary names, in menu order. */
|
|
59
|
+
const standardMarks = [
|
|
60
|
+
Bold,
|
|
61
|
+
Italic,
|
|
62
|
+
Code,
|
|
63
|
+
Strikethrough,
|
|
64
|
+
Link
|
|
65
|
+
];
|
|
66
|
+
/**
|
|
67
|
+
* The standard node kinds. Constraints are what make a kind more than a name: a
|
|
68
|
+
* `List` accepts only `ListItem`s and a `Table` only `TableRow`s, so `validate`
|
|
69
|
+
* reports a document that says otherwise (§125); a `CodeBlock` carries text with no
|
|
70
|
+
* formatting, and its `language` is a prop, not a mark.
|
|
71
|
+
*/
|
|
72
|
+
const standardNodes = [
|
|
73
|
+
block("Paragraph"),
|
|
74
|
+
block("Heading"),
|
|
75
|
+
node("Quote", { children: blockContent }),
|
|
76
|
+
node("List", {
|
|
77
|
+
Props: Schema.Struct({
|
|
78
|
+
ordered: Schema.optional(Schema.Boolean),
|
|
79
|
+
start: Schema.optional(Schema.Number)
|
|
80
|
+
}),
|
|
81
|
+
children: blocksOf("ListItem", "TaskItem")
|
|
82
|
+
}),
|
|
83
|
+
node("ListItem", {
|
|
84
|
+
children: blockContent,
|
|
85
|
+
within: ["List"]
|
|
86
|
+
}),
|
|
87
|
+
node("TaskItem", {
|
|
88
|
+
Props: Schema.Struct({ checked: Schema.Boolean }),
|
|
89
|
+
children: blockContent,
|
|
90
|
+
splitProps: { checked: false },
|
|
91
|
+
within: ["List"]
|
|
92
|
+
}),
|
|
93
|
+
node("CodeBlock", {
|
|
94
|
+
Props: Schema.Struct({ language: Schema.optional(Schema.String) }),
|
|
95
|
+
children: textContent,
|
|
96
|
+
marks: "none",
|
|
97
|
+
lineBreaks: true,
|
|
98
|
+
indents: true
|
|
99
|
+
}),
|
|
100
|
+
atom("ThematicBreak"),
|
|
101
|
+
atom("Image", { Props: Schema.Struct({
|
|
102
|
+
src: Schema.String,
|
|
103
|
+
alt: Schema.optional(Schema.String),
|
|
104
|
+
caption: Schema.optional(Schema.String),
|
|
105
|
+
width: Schema.optional(Schema.Number.check(Schema.isGreaterThan(0))),
|
|
106
|
+
align: Schema.optional(Schema.Literals([
|
|
107
|
+
"start",
|
|
108
|
+
"center",
|
|
109
|
+
"end"
|
|
110
|
+
])),
|
|
111
|
+
asset: Schema.optional(ImageAsset)
|
|
112
|
+
}) }),
|
|
113
|
+
node("Table", { children: blocksOf("TableRow") }),
|
|
114
|
+
node("TableRow", {
|
|
115
|
+
Props: Schema.Struct({ header: Schema.optional(Schema.Boolean) }),
|
|
116
|
+
children: blocksOf("TableCell"),
|
|
117
|
+
within: ["Table"]
|
|
118
|
+
}),
|
|
119
|
+
node("TableCell", {
|
|
120
|
+
children: blockContent,
|
|
121
|
+
isolating: true,
|
|
122
|
+
within: ["TableRow"]
|
|
123
|
+
})
|
|
124
|
+
];
|
|
125
|
+
const decodeAsset = Schema.decodeUnknownOption(ImageAsset);
|
|
126
|
+
/**
|
|
127
|
+
* An `Image`'s `width` and `height` attributes from its props' `asset`, at its drawn `width`
|
|
128
|
+
* when it has one, so a page keeps the box before the file arrives; none without facts. The
|
|
129
|
+
* standard rendering writes them; a rendering of its own (a figure) spreads them on its `img`.
|
|
130
|
+
* The facts are trusted to be `src`'s: whatever changes `src` replaces or clears `asset`.
|
|
131
|
+
*/
|
|
132
|
+
const imageBox = (props) => Option.match(decodeAsset(props["asset"]), {
|
|
133
|
+
onNone: () => ({}),
|
|
134
|
+
onSome: ({ width, height }) => {
|
|
135
|
+
const drawn = props["width"];
|
|
136
|
+
const shown = typeof drawn === "number" && drawn > 0 ? drawn : width;
|
|
137
|
+
return {
|
|
138
|
+
width: String(Math.round(shown)),
|
|
139
|
+
height: String(Math.round(shown * height / width))
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
/**
|
|
144
|
+
* A URL attribute, or none when the value fails the URL policy. Import already applies the
|
|
145
|
+
* policy, but a document also arrives decoded, synchronized, or edited through `SetMark`,
|
|
146
|
+
* and this is where a `javascript:` href would become live.
|
|
147
|
+
*/
|
|
148
|
+
const urlAttribute = (name, value) => {
|
|
149
|
+
const safe = typeof value === "string" ? safeUrl(value) : void 0;
|
|
150
|
+
return safe === void 0 ? {} : { [name]: safe };
|
|
151
|
+
};
|
|
152
|
+
/**
|
|
153
|
+
* How the standard vocabulary renders (§121): the element each kind is, with the props
|
|
154
|
+
* that belong in attributes read from the block, so a `Link` is an `<a href>`, an `Image`
|
|
155
|
+
* carries its source, and a `List` is an `<ol>` or a `<ul>`. The shipped marks already
|
|
156
|
+
* nest in `strong`/`em`/`code`; this adds the two that do not. An application composes it
|
|
157
|
+
* with `renderingOver` and names its own kinds beside it:
|
|
158
|
+
*
|
|
159
|
+
* ```ts
|
|
160
|
+
* renderingOver(RichText.standardRendering, { nodes: { Callout: { tag: 'aside' } } })
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
const standardRendering = rendering({
|
|
164
|
+
marks: {
|
|
165
|
+
Strikethrough: {
|
|
166
|
+
tag: "s",
|
|
167
|
+
attributes: {}
|
|
168
|
+
},
|
|
169
|
+
Link: (mark) => ({
|
|
170
|
+
tag: "a",
|
|
171
|
+
attributes: urlAttribute("href", markProps(mark)?.href)
|
|
172
|
+
})
|
|
173
|
+
},
|
|
174
|
+
nodes: {
|
|
175
|
+
Quote: {
|
|
176
|
+
tag: "blockquote",
|
|
177
|
+
attributes: {}
|
|
178
|
+
},
|
|
179
|
+
List: (block) => {
|
|
180
|
+
const ordered = block.props.ordered === true;
|
|
181
|
+
const start = block.props.start;
|
|
182
|
+
return {
|
|
183
|
+
tag: ordered ? "ol" : "ul",
|
|
184
|
+
attributes: ordered && typeof start === "number" ? { start: String(start) } : {}
|
|
185
|
+
};
|
|
186
|
+
},
|
|
187
|
+
ListItem: {
|
|
188
|
+
tag: "li",
|
|
189
|
+
attributes: {}
|
|
190
|
+
},
|
|
191
|
+
TaskItem: (block) => ({
|
|
192
|
+
tag: "li",
|
|
193
|
+
attributes: { "data-task": block.props.checked === true ? "checked" : "unchecked" }
|
|
194
|
+
}),
|
|
195
|
+
CodeBlock: (block) => ({
|
|
196
|
+
tag: "pre",
|
|
197
|
+
attributes: { "data-language": String(block.props.language ?? "") }
|
|
198
|
+
}),
|
|
199
|
+
ThematicBreak: {
|
|
200
|
+
tag: "hr",
|
|
201
|
+
attributes: {}
|
|
202
|
+
},
|
|
203
|
+
Image: (block) => ({
|
|
204
|
+
tag: "img",
|
|
205
|
+
attributes: {
|
|
206
|
+
...urlAttribute("src", block.props.src),
|
|
207
|
+
alt: String(block.props.alt ?? ""),
|
|
208
|
+
...imageBox(block.props)
|
|
209
|
+
}
|
|
210
|
+
}),
|
|
211
|
+
Table: {
|
|
212
|
+
tag: "table",
|
|
213
|
+
attributes: {},
|
|
214
|
+
inner: "tbody"
|
|
215
|
+
},
|
|
216
|
+
TableRow: (block) => block.props.header === true ? {
|
|
217
|
+
tag: "tr",
|
|
218
|
+
attributes: { "data-header": "" }
|
|
219
|
+
} : {
|
|
220
|
+
tag: "tr",
|
|
221
|
+
attributes: {}
|
|
222
|
+
},
|
|
223
|
+
TableCell: {
|
|
224
|
+
tag: "td",
|
|
225
|
+
attributes: {}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
export { Link, Strikethrough, imageBox, linkAt, standardMarks, standardNodes, standardRendering };
|
|
230
|
+
|
|
231
|
+
//# sourceMappingURL=standard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"standard.js","names":[],"sources":["../src/standard.ts"],"sourcesContent":["/**\n * The standard semantic vocabulary (§124 §2, §125): the node kinds and marks a\n * document uses to mean what Markdown and HTML also mean, so an interpreter is a\n * mapping from this vocabulary rather than a private one. Declarations only — no\n * renderers, no executable code (§34) — and an application spreads them into its Kit\n * and adds its own:\n *\n * ```ts\n * RichText.kit({\n * nodes: [...RichText.standardNodes, Callout],\n * marks: [...RichText.standardMarks, Highlight],\n * })\n * ```\n *\n * `Paragraph` and `Heading` are built-in text blocks, `Code` is the inline-code mark,\n * and the rest are ordinary declarations. `HardBreak` is deliberately absent: it is\n * inline content, and the model has no inline atoms yet (§116, deferred).\n */\nimport { Option, Schema } from 'effect'\nimport { ImageAsset } from 'foldkit-media'\nimport { markExtent } from './command.js'\nimport { blockContent, rangeStart, textContent, type Document, type Selection } from './document.js'\nimport { atom, block, blocksOf, node, type NodeDefinition } from './kit.js'\nimport { Bold, Code, Italic, mark, markProps, type MarkDef } from './marks.js'\nimport { rendering, type Rendering } from './rendering.js'\nimport { safeUrl } from './url.js'\n\n/**\n * A struck-through span. It expands `after`, so typing at its edge continues it, as\n * bold and italic do.\n */\nexport const Strikethrough = mark('Strikethrough', { expand: 'after' })\n\n/**\n * A hyperlink. Its `href` is a prop rather than a mark name, which is what lets the\n * serializer render a real `<a href>` (§121); it expands `none`, because text typed\n * after a link is new text, not a continuation of the link.\n */\nexport const Link = mark('Link', {\n Props: Schema.Struct({ href: Schema.String }),\n expand: 'none',\n})\n\n/** The link a selection starts in: its `href`, and the range of runs it spans. */\nexport interface LinkAt {\n /** The stored `href`, or `''` when the stored one is not a string. */\n readonly href: string\n readonly selection: Extract<Selection, { readonly type: 'Range' }>\n}\n\n/**\n * The link a selection starts in, which is what a link editor opens on: at a caret, the\n * link around it; over a range, the link its start is in. Undefined outside a link, and for\n * a node selection, which covers blocks rather than text.\n */\nexport const linkAt = (document: Document, selection: Selection | null): LinkAt | undefined => {\n if (selection?.type !== 'Range') return undefined\n const start = rangeStart(document, selection)\n const extent = start === undefined ? undefined : markExtent(document, start, Link.name)\n if (extent === undefined) return undefined\n const href = markProps(extent.mark)?.href\n return { href: typeof href === 'string' ? href : '', selection: extent.selection }\n}\n\n/** Every mark the standard vocabulary names, in menu order. */\nexport const standardMarks: ReadonlyArray<MarkDef> = [Bold, Italic, Code, Strikethrough, Link]\n\n/**\n * The standard node kinds. Constraints are what make a kind more than a name: a\n * `List` accepts only `ListItem`s and a `Table` only `TableRow`s, so `validate`\n * reports a document that says otherwise (§125); a `CodeBlock` carries text with no\n * formatting, and its `language` is a prop, not a mark.\n */\nexport const standardNodes: ReadonlyArray<NodeDefinition> = [\n block('Paragraph'),\n block('Heading'),\n node('Quote', { children: blockContent }),\n node('List', {\n // An ordered list, and where its numbering starts; both are Markdown's.\n Props: Schema.Struct({\n ordered: Schema.optional(Schema.Boolean),\n start: Schema.optional(Schema.Number),\n }),\n children: blocksOf('ListItem', 'TaskItem'),\n }),\n node('ListItem', { children: blockContent, within: ['List'] }),\n node('TaskItem', {\n Props: Schema.Struct({ checked: Schema.Boolean }),\n children: blockContent,\n // A task split off a done one is a new task, not a done one.\n splitProps: { checked: false },\n within: ['List'],\n }),\n node('CodeBlock', {\n Props: Schema.Struct({ language: Schema.optional(Schema.String) }),\n children: textContent,\n marks: 'none',\n lineBreaks: true,\n indents: true,\n }),\n atom('ThematicBreak'),\n atom('Image', {\n // What a figure needs beside its source: a caption under it, the width it is shown at in\n // CSS pixels, and which way it sits. The standard rendering draws the `img` alone; a\n // rendering that draws a figure reads the rest (`examples/richtext-playground`).\n // `asset` is what `src` names, its size, hash and placeholder, kept as stored (`null` for\n // what it lacks): known where the image entered (an upload, a file a reader added), it\n // lets a page hold the image's box before the file arrives. Optional, so a document saved\n // without it is as valid as ever.\n Props: Schema.Struct({\n src: Schema.String,\n alt: Schema.optional(Schema.String),\n caption: Schema.optional(Schema.String),\n width: Schema.optional(Schema.Number.check(Schema.isGreaterThan(0))),\n align: Schema.optional(Schema.Literals(['start', 'center', 'end'])),\n asset: Schema.optional(ImageAsset),\n }),\n }),\n node('Table', { children: blocksOf('TableRow') }),\n node('TableRow', {\n // GFM's first row is the header; saying so explicitly is what lets HTML and Markdown\n // agree about it instead of each assuming.\n Props: Schema.Struct({ header: Schema.optional(Schema.Boolean) }),\n children: blocksOf('TableCell'),\n within: ['Table'],\n }),\n // A cell's content stays in the cell: Backspace at its start lifts nothing out of it.\n node('TableCell', { children: blockContent, isolating: true, within: ['TableRow'] }),\n]\n\nconst decodeAsset = Schema.decodeUnknownOption(ImageAsset)\n\n/**\n * An `Image`'s `width` and `height` attributes from its props' `asset`, at its drawn `width`\n * when it has one, so a page keeps the box before the file arrives; none without facts. The\n * standard rendering writes them; a rendering of its own (a figure) spreads them on its `img`.\n * The facts are trusted to be `src`'s: whatever changes `src` replaces or clears `asset`.\n */\nexport const imageBox = (\n props: Readonly<Record<string, unknown>>,\n): Readonly<Record<string, string>> =>\n Option.match(decodeAsset(props['asset']), {\n onNone: () => ({}),\n onSome: ({ width, height }) => {\n const drawn = props['width']\n const shown = typeof drawn === 'number' && drawn > 0 ? drawn : width\n return {\n width: String(Math.round(shown)),\n height: String(Math.round((shown * height) / width)),\n }\n },\n })\n\n/**\n * A URL attribute, or none when the value fails the URL policy. Import already applies the\n * policy, but a document also arrives decoded, synchronized, or edited through `SetMark`,\n * and this is where a `javascript:` href would become live.\n */\nconst urlAttribute = (name: string, value: unknown): Readonly<Record<string, string>> => {\n const safe = typeof value === 'string' ? safeUrl(value) : undefined\n return safe === undefined ? {} : { [name]: safe }\n}\n\n/**\n * How the standard vocabulary renders (§121): the element each kind is, with the props\n * that belong in attributes read from the block, so a `Link` is an `<a href>`, an `Image`\n * carries its source, and a `List` is an `<ol>` or a `<ul>`. The shipped marks already\n * nest in `strong`/`em`/`code`; this adds the two that do not. An application composes it\n * with `renderingOver` and names its own kinds beside it:\n *\n * ```ts\n * renderingOver(RichText.standardRendering, { nodes: { Callout: { tag: 'aside' } } })\n * ```\n */\nexport const standardRendering: Rendering = rendering({\n marks: {\n Strikethrough: { tag: 's', attributes: {} },\n Link: mark => ({ tag: 'a', attributes: urlAttribute('href', markProps(mark)?.href) }),\n },\n nodes: {\n Quote: { tag: 'blockquote', attributes: {} },\n List: block => {\n const ordered = block.props.ordered === true\n const start = block.props.start\n return {\n tag: ordered ? 'ol' : 'ul',\n attributes: ordered && typeof start === 'number' ? { start: String(start) } : {},\n }\n },\n ListItem: { tag: 'li', attributes: {} },\n TaskItem: block => ({\n tag: 'li',\n attributes: { 'data-task': block.props.checked === true ? 'checked' : 'unchecked' },\n }),\n CodeBlock: block => ({\n tag: 'pre',\n attributes: { 'data-language': String(block.props.language ?? '') },\n }),\n ThematicBreak: { tag: 'hr', attributes: {} },\n Image: block => ({\n tag: 'img',\n attributes: {\n ...urlAttribute('src', block.props.src),\n alt: String(block.props.alt ?? ''),\n ...imageBox(block.props),\n },\n }),\n Table: { tag: 'table', attributes: {}, inner: 'tbody' },\n // A row says it is the header; its cells do not have to be told apart by their parent.\n TableRow: block =>\n block.props.header === true\n ? { tag: 'tr', attributes: { 'data-header': '' } }\n : { tag: 'tr', attributes: {} },\n TableCell: { tag: 'td', attributes: {} },\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,MAAa,gBAAgB,KAAK,iBAAiB,EAAE,QAAQ,QAAQ,CAAC;;;;;;AAOtE,MAAa,OAAO,KAAK,QAAQ;CAC/B,OAAO,OAAO,OAAO,EAAE,MAAM,OAAO,OAAO,CAAC;CAC5C,QAAQ;AACV,CAAC;;;;;;AAcD,MAAa,UAAU,UAAoB,cAAoD;CAC7F,IAAI,WAAW,SAAS,SAAS,OAAO,KAAA;CACxC,MAAM,QAAQ,WAAW,UAAU,SAAS;CAC5C,MAAM,SAAS,UAAU,K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AAK,UAAU,OAAO;IACtB,YAAY,WAAW,OAAO,UAAU,WAAW,EAAE,OAAO,OAAO,KAAK,EAAE,IAAI,CAAC;GACjF;EACF;EACA,UAAU;GAAE,KAAK;GAAM,YAAY,CAAC;EAAE;EACtC,WAAU,WAAU;GAClB,KAAK;GACL,YAAY,EAAE,aAAa,MAAM,MAAM,YAAY,OAAO,YAAY,YAAY;EACpF;EACA,YAAW,WAAU;GACnB,KAAK;GACL,YAAY,EAAE,iBAAiB,OAAO,MAAM,MAAM,YAAY,EAAE,EAAE;EACpE;EACA,eAAe;GAAE,KAAK;GAAM,YAAY,CAAC;EAAE;EAC3C,QAAO,WAAU;GACf,KAAK;GACL,YAAY;IACV,GAAG,aAAa,OAAO,MAAM,MAAM,GAAG;IACtC,KAAK,OAAO,MAAM,MAAM,OAAO,EAAE;IACjC,GAAG,SAAS,MAAM,KAAK;GACzB;EACF;EACA,OAAO;GAAE,KAAK;GAAS,YAAY,CAAC;GAAG,OAAO;EAAQ;EAEtD,WAAU,UACR,MAAM,MAAM,WAAW,OACnB;GAAE,KAAK;GAAM,YAAY,EAAE,eAAe,GAAG;EAAE,IAC/C;GAAE,KAAK;GAAM,YAAY,CAAC;EAAE;EAClC,WAAW;GAAE,KAAK;GAAM,YAAY,CAAC;EAAE;CACzC;AACF,CAAC"}
|
package/dist/table.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Document, EditorState, NodeBlock, NodeId, Selection } from "./document.js";
|
|
2
|
+
import { TransactionResult } from "./transaction.js";
|
|
3
|
+
/** Where a caret stands in a table: the table, its row and the cell's index in that row. */
|
|
4
|
+
export interface TableAt {
|
|
5
|
+
readonly table: NodeBlock;
|
|
6
|
+
readonly rows: ReadonlyArray<NodeBlock>;
|
|
7
|
+
readonly row: number;
|
|
8
|
+
readonly column: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The table cell the selection starts in, the innermost one when tables nest, or undefined
|
|
12
|
+
* outside a table. A range is read from its start, as the other block commands read it.
|
|
13
|
+
*/
|
|
14
|
+
export declare const tableAt: (document: Document, selection: Selection | null) => TableAt | undefined;
|
|
15
|
+
/** A table edit, or that the edit would leave the table empty and it should be deleted whole. */
|
|
16
|
+
export type TableEdit = TransactionResult | {
|
|
17
|
+
readonly deleteTable: NodeId;
|
|
18
|
+
};
|
|
19
|
+
/** A row above or below the caret's, as wide as the table; the caret goes to it, same column. */
|
|
20
|
+
export declare const insertRow: (state: EditorState, at: TableAt, where: 'before' | 'after', mint: () => string) => TransactionResult;
|
|
21
|
+
/**
|
|
22
|
+
* A cell beside the caret's in every row, before or after it; a row too short to have that
|
|
23
|
+
* column gets its cell at its end. The caret goes to the new cell in its own row.
|
|
24
|
+
*/
|
|
25
|
+
export declare const insertColumn: (state: EditorState, at: TableAt, where: 'before' | 'after', mint: () => string) => TransactionResult;
|
|
26
|
+
/** The caret's row, gone; the caret goes to the same column of the row after it, or before. */
|
|
27
|
+
export declare const deleteRow: (state: EditorState, at: TableAt) => TableEdit;
|
|
28
|
+
/**
|
|
29
|
+
* The caret's column, gone from every row that has it; a row it leaves empty goes too. The
|
|
30
|
+
* caret goes to the cell that takes the column's place in its row, or the one before it.
|
|
31
|
+
*/
|
|
32
|
+
export declare const deleteColumn: (state: EditorState, at: TableAt) => TableEdit;
|
|
33
|
+
/**
|
|
34
|
+
* The caret to the start of the next cell, across rows, or the previous one: Tab and Shift+Tab.
|
|
35
|
+
* Tab in the last cell adds a row and goes to it; Shift+Tab in the first stays where it is.
|
|
36
|
+
*/
|
|
37
|
+
export declare const moveToCell: (state: EditorState, at: TableAt, direction: 'next' | 'previous', mint: () => string) => TransactionResult;
|
|
38
|
+
//# sourceMappingURL=table.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table.d.ts","names":[],"sources":["../src/table.ts"],"mappings":";;;iBAwBiB;WACN,OAAO;WACP,MAAM,cAAc;WACpB;WACA;;;;;;qBAaE,UAAO,UAAc,UAAQ,WAAa,qBAAmB;;YA8D9D,YAAY;WAA+B,aAAa;;;qBAuBvD,YAAS,OACb,aAAW,IACd,SAAO,2BACc,uBAExB;;;;;qBAaU,eAAY,OAChB,aAAW,IACd,SAAO,2BACc,uBAExB;;qBAYU,YAAS,OAAW,aAAW,IAAM,YAAU;;;;;qBAc/C,eAAY,OAAW,aAAW,IAAM,YAAU;;;;;qBAqBlD,aAAU,OACd,aAAW,IACd,SAAO,gCACmB,uBAE7B"}
|
package/dist/table.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { NodeId, blockAtPath, eachBlock, locateRun, rangeStart } from "./document.js";
|
|
2
|
+
import { withFreshIds } from "./clipboard.js";
|
|
3
|
+
import { Edit, apply } from "./transaction.js";
|
|
4
|
+
// src/table.ts
|
|
5
|
+
/**
|
|
6
|
+
* Editing a table as a grid: rows and columns added and removed beside the caret's cell, and the
|
|
7
|
+
* caret moved from cell to cell, as Tab and Shift+Tab do. A table is the standard vocabulary's
|
|
8
|
+
* `Table` of `TableRow`s of `TableCell`s, each cell holding blocks; a column is the cells at one
|
|
9
|
+
* index across the rows, which may be ragged, so a column that a short row lacks is skipped
|
|
10
|
+
* rather than refused. Each change is one transaction, so it is one undo step.
|
|
11
|
+
*/
|
|
12
|
+
const isKind = (block, kind) => block?.type === "Node" && block.kind === kind;
|
|
13
|
+
/** The cells of a row, which are its nested blocks. */
|
|
14
|
+
const cellsOf = (row) => row.blocks ?? [];
|
|
15
|
+
/**
|
|
16
|
+
* The table cell the selection starts in, the innermost one when tables nest, or undefined
|
|
17
|
+
* outside a table. A range is read from its start, as the other block commands read it.
|
|
18
|
+
*/
|
|
19
|
+
const tableAt = (document, selection) => {
|
|
20
|
+
if (selection?.type !== "Range") return void 0;
|
|
21
|
+
const start = rangeStart(document, selection);
|
|
22
|
+
const run = start === void 0 ? void 0 : locateRun(document, start.node);
|
|
23
|
+
if (run === void 0) return void 0;
|
|
24
|
+
for (let depth = run.path.length - 1; depth >= 3; depth--) {
|
|
25
|
+
const cell = blockAtPath(document, run.path.slice(0, depth));
|
|
26
|
+
const row = blockAtPath(document, run.path.slice(0, depth - 1));
|
|
27
|
+
const table = blockAtPath(document, run.path.slice(0, depth - 2));
|
|
28
|
+
if (isKind(cell, "TableCell") && isKind(row, "TableRow") && isKind(table, "Table")) return {
|
|
29
|
+
table,
|
|
30
|
+
rows: (table.blocks ?? []).filter((candidate) => candidate.type === "Node"),
|
|
31
|
+
row: run.path[depth - 2],
|
|
32
|
+
column: run.path[depth - 1]
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/** An empty cell, under placeholder identities a caller freshens. */
|
|
37
|
+
const emptyCell = () => ({
|
|
38
|
+
type: "Node",
|
|
39
|
+
kind: "TableCell",
|
|
40
|
+
id: NodeId.make("cell"),
|
|
41
|
+
props: {},
|
|
42
|
+
children: [],
|
|
43
|
+
blocks: [{
|
|
44
|
+
type: "Paragraph",
|
|
45
|
+
id: NodeId.make("paragraph"),
|
|
46
|
+
children: [{
|
|
47
|
+
type: "Text",
|
|
48
|
+
id: NodeId.make("text"),
|
|
49
|
+
text: "",
|
|
50
|
+
marks: []
|
|
51
|
+
}]
|
|
52
|
+
}]
|
|
53
|
+
});
|
|
54
|
+
const fresh = (block, mint) => withFreshIds({
|
|
55
|
+
version: 1,
|
|
56
|
+
blocks: [block]
|
|
57
|
+
}, mint).blocks[0];
|
|
58
|
+
const firstRun = (block) => {
|
|
59
|
+
if (block === void 0) return void 0;
|
|
60
|
+
let found;
|
|
61
|
+
eachBlock([block], (inner) => {
|
|
62
|
+
found ??= inner.children[0];
|
|
63
|
+
});
|
|
64
|
+
return found;
|
|
65
|
+
};
|
|
66
|
+
/** The selection at the start of a cell's first text, or the selection as it was. */
|
|
67
|
+
const caretIn = (cell, otherwise) => {
|
|
68
|
+
const run = firstRun(cell);
|
|
69
|
+
if (run === void 0) return otherwise;
|
|
70
|
+
const at = {
|
|
71
|
+
node: run.id,
|
|
72
|
+
offset: 0,
|
|
73
|
+
affinity: "after"
|
|
74
|
+
};
|
|
75
|
+
return {
|
|
76
|
+
type: "Range",
|
|
77
|
+
anchor: at,
|
|
78
|
+
focus: at
|
|
79
|
+
};
|
|
80
|
+
};
|
|
81
|
+
/** The widest row's cell count: what a new row is given so the grid stays as wide. */
|
|
82
|
+
const widthOf = (rows) => Math.max(1, ...rows.map((row) => cellsOf(row).length));
|
|
83
|
+
/** A row as wide as the table, under fresh identities, and the operation that puts it there. */
|
|
84
|
+
const newRow = (at, index, mint) => {
|
|
85
|
+
const row = fresh({
|
|
86
|
+
type: "Node",
|
|
87
|
+
kind: "TableRow",
|
|
88
|
+
id: NodeId.make("row"),
|
|
89
|
+
props: {},
|
|
90
|
+
children: [],
|
|
91
|
+
blocks: Array.from({ length: widthOf(at.rows) }, emptyCell)
|
|
92
|
+
}, mint);
|
|
93
|
+
return {
|
|
94
|
+
row,
|
|
95
|
+
operation: Edit.insertBlock(row, index, at.table.id)
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
/** A row above or below the caret's, as wide as the table; the caret goes to it, same column. */
|
|
99
|
+
const insertRow = (state, at, where, mint) => {
|
|
100
|
+
const { row, operation } = newRow(at, at.row + (where === "after" ? 1 : 0), mint);
|
|
101
|
+
const cells = row.type === "Node" ? row.blocks ?? [] : [];
|
|
102
|
+
return apply(state, [operation, Edit.setSelection(caretIn(cells[Math.min(at.column, cells.length - 1)], state.selection))]);
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* A cell beside the caret's in every row, before or after it; a row too short to have that
|
|
106
|
+
* column gets its cell at its end. The caret goes to the new cell in its own row.
|
|
107
|
+
*/
|
|
108
|
+
const insertColumn = (state, at, where, mint) => {
|
|
109
|
+
const index = at.column + (where === "after" ? 1 : 0);
|
|
110
|
+
let caretCell;
|
|
111
|
+
const operations = at.rows.map((row, rowIndex) => {
|
|
112
|
+
const cell = fresh(emptyCell(), mint);
|
|
113
|
+
if (rowIndex === at.row) caretCell = cell;
|
|
114
|
+
return Edit.insertBlock(cell, Math.min(index, cellsOf(row).length), row.id);
|
|
115
|
+
});
|
|
116
|
+
return apply(state, [...operations, Edit.setSelection(caretIn(caretCell, state.selection))]);
|
|
117
|
+
};
|
|
118
|
+
/** The caret's row, gone; the caret goes to the same column of the row after it, or before. */
|
|
119
|
+
const deleteRow = (state, at) => {
|
|
120
|
+
if (at.rows.length === 1) return { deleteTable: at.table.id };
|
|
121
|
+
const next = at.rows[at.row + 1] ?? at.rows[at.row - 1];
|
|
122
|
+
const cells = cellsOf(next);
|
|
123
|
+
return apply(state, [Edit.deleteBlock(at.rows[at.row].id), Edit.setSelection(caretIn(cells[Math.min(at.column, cells.length - 1)], state.selection))]);
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* The caret's column, gone from every row that has it; a row it leaves empty goes too. The
|
|
127
|
+
* caret goes to the cell that takes the column's place in its row, or the one before it.
|
|
128
|
+
*/
|
|
129
|
+
const deleteColumn = (state, at) => {
|
|
130
|
+
if (at.rows.every((row) => cellsOf(row).length <= 1)) return { deleteTable: at.table.id };
|
|
131
|
+
const operations = [];
|
|
132
|
+
for (const row of at.rows) {
|
|
133
|
+
const cells = cellsOf(row);
|
|
134
|
+
const cell = cells[at.column];
|
|
135
|
+
if (cell === void 0) continue;
|
|
136
|
+
operations.push(Edit.deleteBlock(cells.length === 1 ? row.id : cell.id));
|
|
137
|
+
}
|
|
138
|
+
const own = cellsOf(at.rows[at.row]);
|
|
139
|
+
const landing = own.length === 1 ? void 0 : own[at.column + 1] ?? own[at.column - 1];
|
|
140
|
+
return apply(state, [...operations, ...landing === void 0 ? [] : [Edit.setSelection(caretIn(landing, state.selection))]]);
|
|
141
|
+
};
|
|
142
|
+
/**
|
|
143
|
+
* The caret to the start of the next cell, across rows, or the previous one: Tab and Shift+Tab.
|
|
144
|
+
* Tab in the last cell adds a row and goes to it; Shift+Tab in the first stays where it is.
|
|
145
|
+
*/
|
|
146
|
+
const moveToCell = (state, at, direction, mint) => {
|
|
147
|
+
const order = at.rows.flatMap((row, rowIndex) => cellsOf(row).map((cell, column) => ({
|
|
148
|
+
cell,
|
|
149
|
+
rowIndex,
|
|
150
|
+
column
|
|
151
|
+
})));
|
|
152
|
+
const target = order[order.findIndex((entry) => entry.rowIndex === at.row && entry.column === at.column) + (direction === "next" ? 1 : -1)];
|
|
153
|
+
if (target !== void 0) return apply(state, [Edit.setSelection(caretIn(target.cell, state.selection))]);
|
|
154
|
+
if (direction === "previous") return apply(state, []);
|
|
155
|
+
const { row, operation } = newRow(at, at.rows.length, mint);
|
|
156
|
+
return apply(state, [operation, Edit.setSelection(caretIn(row, state.selection))]);
|
|
157
|
+
};
|
|
158
|
+
export { deleteColumn, deleteRow, insertColumn, insertRow, moveToCell, tableAt };
|
|
159
|
+
|
|
160
|
+
//# sourceMappingURL=table.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table.js","names":[],"sources":["../src/table.ts"],"sourcesContent":["/**\n * Editing a table as a grid: rows and columns added and removed beside the caret's cell, and the\n * caret moved from cell to cell, as Tab and Shift+Tab do. A table is the standard vocabulary's\n * `Table` of `TableRow`s of `TableCell`s, each cell holding blocks; a column is the cells at one\n * index across the rows, which may be ragged, so a column that a short row lacks is skipped\n * rather than refused. Each change is one transaction, so it is one undo step.\n */\nimport { withFreshIds } from './clipboard.js'\nimport {\n NodeId,\n blockAtPath,\n eachBlock,\n locateRun,\n rangeStart,\n type Block,\n type Document,\n type EditorState,\n type NodeBlock,\n type Selection,\n type Text,\n} from './document.js'\nimport { Edit, apply, type Operation, type TransactionResult } from './transaction.js'\n\n/** Where a caret stands in a table: the table, its row and the cell's index in that row. */\nexport interface TableAt {\n readonly table: NodeBlock\n readonly rows: ReadonlyArray<NodeBlock>\n readonly row: number\n readonly column: number\n}\n\nconst isKind = (block: Block | undefined, kind: string): block is NodeBlock =>\n block?.type === 'Node' && block.kind === kind\n\n/** The cells of a row, which are its nested blocks. */\nconst cellsOf = (row: NodeBlock): ReadonlyArray<Block> => row.blocks ?? []\n\n/**\n * The table cell the selection starts in, the innermost one when tables nest, or undefined\n * outside a table. A range is read from its start, as the other block commands read it.\n */\nexport const tableAt = (document: Document, selection: Selection | null): TableAt | undefined => {\n if (selection?.type !== 'Range') return undefined\n const start = rangeStart(document, selection)\n const run = start === undefined ? undefined : locateRun(document, start.node)\n if (run === undefined) return undefined\n // The cell is an ancestor of the caret's text block, its row the cell's parent, and the\n // table the row's: so the cell's path is at least three long.\n for (let depth = run.path.length - 1; depth >= 3; depth--) {\n const cell = blockAtPath(document, run.path.slice(0, depth))\n const row = blockAtPath(document, run.path.slice(0, depth - 1))\n const table = blockAtPath(document, run.path.slice(0, depth - 2))\n if (isKind(cell, 'TableCell') && isKind(row, 'TableRow') && isKind(table, 'Table')) {\n return {\n table,\n rows: (table.blocks ?? []).filter(\n (candidate): candidate is NodeBlock => candidate.type === 'Node',\n ),\n row: run.path[depth - 2]!,\n column: run.path[depth - 1]!,\n }\n }\n }\n return undefined\n}\n\n/** An empty cell, under placeholder identities a caller freshens. */\nconst emptyCell = (): Block => ({\n type: 'Node',\n kind: 'TableCell',\n id: NodeId.make('cell'),\n props: {},\n children: [],\n blocks: [\n {\n type: 'Paragraph',\n id: NodeId.make('paragraph'),\n children: [{ type: 'Text', id: NodeId.make('text'), text: '', marks: [] }],\n },\n ],\n})\n\nconst fresh = (block: Block, mint: () => string): Block =>\n withFreshIds({ version: 1, blocks: [block] }, mint).blocks[0]!\n\nconst firstRun = (block: Block | undefined): Text | undefined => {\n if (block === undefined) return undefined\n let found: Text | undefined\n eachBlock([block], inner => {\n found ??= inner.children[0]\n })\n return found\n}\n\n/** The selection at the start of a cell's first text, or the selection as it was. */\nconst caretIn = (cell: Block | undefined, otherwise: Selection | null): Selection | null => {\n const run = firstRun(cell)\n if (run === undefined) return otherwise\n const at = { node: run.id, offset: 0, affinity: 'after' as const }\n return { type: 'Range', anchor: at, focus: at }\n}\n\n/** A table edit, or that the edit would leave the table empty and it should be deleted whole. */\nexport type TableEdit = TransactionResult | { readonly deleteTable: NodeId }\n\n/** The widest row's cell count: what a new row is given so the grid stays as wide. */\nconst widthOf = (rows: ReadonlyArray<NodeBlock>): number =>\n Math.max(1, ...rows.map(row => cellsOf(row).length))\n\n/** A row as wide as the table, under fresh identities, and the operation that puts it there. */\nconst newRow = (at: TableAt, index: number, mint: () => string) => {\n const row = fresh(\n {\n type: 'Node',\n kind: 'TableRow',\n id: NodeId.make('row'),\n props: {},\n children: [],\n blocks: Array.from({ length: widthOf(at.rows) }, emptyCell),\n },\n mint,\n )\n return { row, operation: Edit.insertBlock(row, index, at.table.id) }\n}\n\n/** A row above or below the caret's, as wide as the table; the caret goes to it, same column. */\nexport const insertRow = (\n state: EditorState,\n at: TableAt,\n where: 'before' | 'after',\n mint: () => string,\n): TransactionResult => {\n const { row, operation } = newRow(at, at.row + (where === 'after' ? 1 : 0), mint)\n const cells = row.type === 'Node' ? (row.blocks ?? []) : []\n return apply(state, [\n operation,\n Edit.setSelection(caretIn(cells[Math.min(at.column, cells.length - 1)], state.selection)),\n ])\n}\n\n/**\n * A cell beside the caret's in every row, before or after it; a row too short to have that\n * column gets its cell at its end. The caret goes to the new cell in its own row.\n */\nexport const insertColumn = (\n state: EditorState,\n at: TableAt,\n where: 'before' | 'after',\n mint: () => string,\n): TransactionResult => {\n const index = at.column + (where === 'after' ? 1 : 0)\n let caretCell: Block | undefined\n const operations: Array<Operation> = at.rows.map((row, rowIndex) => {\n const cell = fresh(emptyCell(), mint)\n if (rowIndex === at.row) caretCell = cell\n return Edit.insertBlock(cell, Math.min(index, cellsOf(row).length), row.id)\n })\n return apply(state, [...operations, Edit.setSelection(caretIn(caretCell, state.selection))])\n}\n\n/** The caret's row, gone; the caret goes to the same column of the row after it, or before. */\nexport const deleteRow = (state: EditorState, at: TableAt): TableEdit => {\n if (at.rows.length === 1) return { deleteTable: at.table.id }\n const next = at.rows[at.row + 1] ?? at.rows[at.row - 1]!\n const cells = cellsOf(next)\n return apply(state, [\n Edit.deleteBlock(at.rows[at.row]!.id),\n Edit.setSelection(caretIn(cells[Math.min(at.column, cells.length - 1)], state.selection)),\n ])\n}\n\n/**\n * The caret's column, gone from every row that has it; a row it leaves empty goes too. The\n * caret goes to the cell that takes the column's place in its row, or the one before it.\n */\nexport const deleteColumn = (state: EditorState, at: TableAt): TableEdit => {\n if (at.rows.every(row => cellsOf(row).length <= 1)) return { deleteTable: at.table.id }\n const operations: Array<Operation> = []\n for (const row of at.rows) {\n const cells = cellsOf(row)\n const cell = cells[at.column]\n if (cell === undefined) continue\n operations.push(Edit.deleteBlock(cells.length === 1 ? row.id : cell.id))\n }\n const own = cellsOf(at.rows[at.row]!)\n const landing = own.length === 1 ? undefined : (own[at.column + 1] ?? own[at.column - 1])\n return apply(state, [\n ...operations,\n ...(landing === undefined ? [] : [Edit.setSelection(caretIn(landing, state.selection))]),\n ])\n}\n\n/**\n * The caret to the start of the next cell, across rows, or the previous one: Tab and Shift+Tab.\n * Tab in the last cell adds a row and goes to it; Shift+Tab in the first stays where it is.\n */\nexport const moveToCell = (\n state: EditorState,\n at: TableAt,\n direction: 'next' | 'previous',\n mint: () => string,\n): TransactionResult => {\n const order = at.rows.flatMap((row, rowIndex) =>\n cellsOf(row).map((cell, column) => ({ cell, rowIndex, column })),\n )\n const here = order.findIndex(entry => entry.rowIndex === at.row && entry.column === at.column)\n const target = order[here + (direction === 'next' ? 1 : -1)]\n if (target !== undefined) {\n return apply(state, [Edit.setSelection(caretIn(target.cell, state.selection))])\n }\n if (direction === 'previous') return apply(state, [])\n const { row, operation } = newRow(at, at.rows.length, mint)\n return apply(state, [operation, Edit.setSelection(caretIn(row, state.selection))])\n}\n"],"mappings":";;;;;;;;;;;AA+BA,MAAM,UAAU,OAA0B,SACxC,OAAO,SAAS,UAAU,MAAM,SAAS;;AAG3C,MAAM,WAAW,QAAyC,IAAI,UAAU,CAAC;;;;;AAMzE,MAAa,WAAW,UAAoB,cAAqD;CAC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