foldkit-richtext 0.5.3 → 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.
Files changed (100) hide show
  1. package/dist/clipboard.d.ts +224 -0
  2. package/dist/clipboard.d.ts.map +1 -0
  3. package/dist/clipboard.js +208 -0
  4. package/dist/clipboard.js.map +1 -0
  5. package/dist/code.d.ts +27 -0
  6. package/dist/code.d.ts.map +1 -0
  7. package/dist/code.js +45 -0
  8. package/dist/code.js.map +1 -0
  9. package/dist/command.d.ts +300 -0
  10. package/dist/command.d.ts.map +1 -0
  11. package/dist/command.js +1438 -0
  12. package/dist/command.js.map +1 -0
  13. package/dist/decoration.d.ts +44 -0
  14. package/dist/decoration.d.ts.map +1 -0
  15. package/dist/decoration.js +96 -0
  16. package/dist/decoration.js.map +1 -0
  17. package/dist/document.d.ts +1045 -0
  18. package/dist/document.d.ts.map +1 -0
  19. package/dist/document.js +544 -0
  20. package/dist/document.js.map +1 -0
  21. package/dist/footnote.d.ts +35 -0
  22. package/dist/footnote.d.ts.map +1 -0
  23. package/dist/footnote.js +100 -0
  24. package/dist/footnote.js.map +1 -0
  25. package/dist/history.d.ts +470 -0
  26. package/dist/history.d.ts.map +1 -0
  27. package/dist/history.js +81 -0
  28. package/dist/history.js.map +1 -0
  29. package/dist/html.d.ts +27 -0
  30. package/dist/html.d.ts.map +1 -0
  31. package/dist/html.js +99 -0
  32. package/dist/html.js.map +1 -0
  33. package/dist/indent.d.ts +16 -0
  34. package/dist/indent.d.ts.map +1 -0
  35. package/dist/indent.js +101 -0
  36. package/dist/indent.js.map +1 -0
  37. package/dist/index.d.ts +24 -0
  38. package/dist/index.js +24 -0
  39. package/dist/input.d.ts +54 -0
  40. package/dist/input.d.ts.map +1 -0
  41. package/dist/input.js +37 -0
  42. package/dist/input.js.map +1 -0
  43. package/dist/kit.d.ts +179 -0
  44. package/dist/kit.d.ts.map +1 -0
  45. package/dist/kit.js +202 -0
  46. package/dist/kit.js.map +1 -0
  47. package/dist/marks.d.ts +79 -0
  48. package/dist/marks.d.ts.map +1 -0
  49. package/dist/marks.js +127 -0
  50. package/dist/marks.js.map +1 -0
  51. package/dist/media.d.ts +30 -0
  52. package/dist/media.d.ts.map +1 -0
  53. package/dist/media.js +148 -0
  54. package/dist/media.js.map +1 -0
  55. package/dist/migration.d.ts +55 -0
  56. package/dist/migration.d.ts.map +1 -0
  57. package/dist/migration.js +124 -0
  58. package/dist/migration.js.map +1 -0
  59. package/dist/outline.d.ts +15 -0
  60. package/dist/outline.d.ts.map +1 -0
  61. package/dist/outline.js +39 -0
  62. package/dist/outline.js.map +1 -0
  63. package/dist/rendering.d.ts +96 -0
  64. package/dist/rendering.d.ts.map +1 -0
  65. package/dist/rendering.js +106 -0
  66. package/dist/rendering.js.map +1 -0
  67. package/dist/replicated.d.ts +340 -0
  68. package/dist/replicated.d.ts.map +1 -0
  69. package/dist/replicated.js +1727 -0
  70. package/dist/replicated.js.map +1 -0
  71. package/dist/search.d.ts +28 -0
  72. package/dist/search.d.ts.map +1 -0
  73. package/dist/search.js +61 -0
  74. package/dist/search.js.map +1 -0
  75. package/dist/standard.d.ts +61 -0
  76. package/dist/standard.d.ts.map +1 -0
  77. package/dist/standard.js +231 -0
  78. package/dist/standard.js.map +1 -0
  79. package/dist/table.d.ts +38 -0
  80. package/dist/table.d.ts.map +1 -0
  81. package/dist/table.js +160 -0
  82. package/dist/table.js.map +1 -0
  83. package/dist/transaction.d.ts +712 -0
  84. package/dist/transaction.d.ts.map +1 -0
  85. package/dist/transaction.js +993 -0
  86. package/dist/transaction.js.map +1 -0
  87. package/dist/transform.d.ts +48 -0
  88. package/dist/transform.d.ts.map +1 -0
  89. package/dist/transform.js +94 -0
  90. package/dist/transform.js.map +1 -0
  91. package/dist/url.d.ts +16 -0
  92. package/dist/url.d.ts.map +1 -0
  93. package/dist/url.js +32 -0
  94. package/dist/url.js.map +1 -0
  95. package/package.json +6 -6
  96. package/dist/index.d.mts +0 -3787
  97. package/dist/index.d.mts.map +0 -1
  98. package/dist/index.mjs +0 -6688
  99. package/dist/index.mjs.map +0 -1
  100. package/dist/rolldown-runtime-D8jQpDG7.mjs +0 -12
package/dist/kit.js ADDED
@@ -0,0 +1,202 @@
1
+ import { blockAtPath, blockContent, eachBlock, textContent } from "./document.js";
2
+ import { markName as markName$1, markProps, propsFailure } from "./marks.js";
3
+ // src/kit.ts
4
+ /** Declares that a node holds nested blocks, only of the named kinds. */
5
+ const blocksOf = (...kinds) => Object.freeze({
6
+ mode: blockContent,
7
+ of: [...kinds]
8
+ });
9
+ /** Declares a block node kind: a top-level node containing text runs. */
10
+ const block = (name) => ({
11
+ name,
12
+ kind: "block",
13
+ children: "text"
14
+ });
15
+ /**
16
+ * Declares an atom: an addressable node with no text content. Its optional props
17
+ * decode at the same boundary as a `node`'s, so an `Image` is an atom carrying a
18
+ * source rather than a run holder.
19
+ */
20
+ const atom = (name, options = {}) => ({
21
+ name,
22
+ kind: "atom",
23
+ children: "none",
24
+ props: options.Props
25
+ });
26
+ /**
27
+ * Declares an application node kind: a `Node` block whose props this schema
28
+ * validates and whose content the `children` declaration states — a mode, or block
29
+ * content restricted to the kinds `blocksOf` names. `marks: 'none'` forbids marks on
30
+ * its runs. The document codec keeps those props as JSON; the Kit is where an
31
+ * application's types meet them.
32
+ */
33
+ const node = (name, options = {}) => ({
34
+ name,
35
+ kind: "node",
36
+ children: options.children ?? textContent,
37
+ marks: options.marks ?? "all",
38
+ props: options.Props,
39
+ isolating: options.isolating ?? false,
40
+ splitProps: options.splitProps,
41
+ within: options.within,
42
+ lineBreaks: options.lineBreaks ?? false,
43
+ indents: options.indents ?? false
44
+ });
45
+ const kit = (definition) => Object.freeze({
46
+ nodes: [...definition.nodes],
47
+ marks: [...definition.marks]
48
+ });
49
+ /** Whether a block is held as a built-in block or as an application node. */
50
+ const heldShape = (block) => block.type === "Node" ? "node" : "block";
51
+ /**
52
+ * The content a block actually holds. An empty application node is `either`: the
53
+ * document cannot say whether it is an atom or a run holder that happens to have
54
+ * no runs, so a declaration of either kind agrees with it.
55
+ */
56
+ const heldContent = (block) => {
57
+ if (block.type !== "Node") return "text";
58
+ if (block.blocks !== void 0) return "blocks";
59
+ return block.children.length > 0 ? "text" : "either";
60
+ };
61
+ /** The content mode a declaration names, or `'none'` for an atom. */
62
+ const declaredContent = (definition) => definition.kind === "atom" ? "none" : typeof definition.children === "string" ? definition.children : blockContent;
63
+ /**
64
+ * Whether a declared definition agrees with the block it is declared for, and
65
+ * why not. A `block` declaration expects a built-in block and a `node` or `atom`
66
+ * one expects an application node; then the declared content mode must match what
67
+ * the block holds, which is what makes an atom's no-children rule real.
68
+ */
69
+ const definitionMismatch = (definition, block) => {
70
+ const expected = definition.kind === "block" ? "block" : "node";
71
+ const held = heldShape(block);
72
+ if (held !== expected) return `"${definition.name}" is declared as ${definition.kind}, but the document holds it as ${held}`;
73
+ const declared = declaredContent(definition);
74
+ const content = heldContent(block);
75
+ return (content === "either" ? declared !== "blocks" : declared === content) ? void 0 : `"${definition.name}" is declared to hold ${declared}, but the document holds ${content}`;
76
+ };
77
+ /**
78
+ * A block's semantic kind: an application node's declared kind, a preserved node's
79
+ * original type, or a built-in block's own type. This is what a `blocksOf`
80
+ * constraint names, and what a `NodeRegistry` is looked up by.
81
+ */
82
+ const blockKind = (block) => block.type === "Node" ? block.kind : block.type === "Unknown" ? block.originalType : block.type;
83
+ /**
84
+ * The children a constrained kind does not accept. A declaration without a
85
+ * constraint accepts any block, and a block that holds runs has no children to
86
+ * check; `definitionMismatch` is what reports the mode itself.
87
+ */
88
+ const childKindDiagnostics = (definition, block) => {
89
+ if (definition.kind !== "node" || typeof definition.children === "string") return [];
90
+ if (block.type !== "Node" || block.blocks === void 0) return [];
91
+ const allowed = definition.children.of;
92
+ const permitted = new Set(allowed);
93
+ return block.blocks.filter((child) => !permitted.has(blockKind(child))).map((child) => ({
94
+ code: "UnexpectedChild",
95
+ node: child.id,
96
+ detail: blockKind(child),
97
+ message: `"${block.kind}" accepts only ${allowed.join(", ")}`
98
+ }));
99
+ };
100
+ /**
101
+ * Whether a kind may stand in a parent of `parentKind`, or at the top level when that is
102
+ * undefined: anywhere, unless its declaration names where (§149).
103
+ */
104
+ const standsWithin = (declared, parentKind) => declared?.kind !== "node" || declared.within === void 0 || parentKind !== void 0 && declared.within.includes(parentKind);
105
+ /** Structural summary of a Kit, for tooling and tests. */
106
+ const inspectKit = (definition) => ({
107
+ blocks: definition.nodes.filter((node) => node.kind === "block").length,
108
+ atoms: definition.nodes.filter((node) => node.kind === "atom").length,
109
+ nodes: definition.nodes.filter((node) => node.kind === "node").length,
110
+ marks: definition.marks.length
111
+ });
112
+ /** Builds the registry an edit consults. A later declaration for a name wins, as a Map does. */
113
+ const nodeRegistry = (definitions) => {
114
+ const byName = new Map(definitions.map((definition) => [definition.name, definition]));
115
+ return Object.freeze({ definitionFor: (kind) => byName.get(kind) });
116
+ };
117
+ /**
118
+ * Checks a document against a Kit's vocabulary without changing it, walking
119
+ * nested blocks so a container's children are checked too. Reports preserved
120
+ * unknown blocks, known blocks the Kit does not declare, declarations that
121
+ * disagree with how the document holds a block, application nodes whose props
122
+ * their schema refuses, and marks outside the Kit or whose props their definition
123
+ * refuses. Content diagnostics never strip or repair the document; callers decide
124
+ * whether to block publishing or surface a placeholder.
125
+ */
126
+ const validate = (document, definition) => {
127
+ const byName = new Map(definition.nodes.map((node) => [node.name, node]));
128
+ const declaredMarks = new Map(definition.marks.map((mark) => [mark.name, mark]));
129
+ const diagnostics = [];
130
+ eachBlock(document.children, (block, path) => {
131
+ if (block.type === "Unknown") {
132
+ diagnostics.push({
133
+ code: "UnknownNode",
134
+ node: block.id,
135
+ detail: block.originalType,
136
+ message: `Unimplemented node type "${block.originalType}"`
137
+ });
138
+ return;
139
+ }
140
+ const kind = blockKind(block);
141
+ const declared = byName.get(kind);
142
+ if (declared === void 0) diagnostics.push({
143
+ code: "UnsupportedNode",
144
+ node: block.id,
145
+ detail: kind,
146
+ message: `The Kit does not declare ${block.type === "Node" ? "node " : ""}"${kind}"`
147
+ });
148
+ else {
149
+ const mismatch = definitionMismatch(declared, block);
150
+ if (mismatch !== void 0) diagnostics.push({
151
+ code: "MismatchedDefinition",
152
+ node: block.id,
153
+ detail: kind,
154
+ message: mismatch
155
+ });
156
+ else {
157
+ if (block.type === "Node" && declared.kind !== "block" && propsFailure(declared.props, block.props)) diagnostics.push({
158
+ code: "InvalidProps",
159
+ node: block.id,
160
+ detail: kind,
161
+ message: `"${kind}" props do not match its declared schema`
162
+ });
163
+ diagnostics.push(...childKindDiagnostics(declared, block));
164
+ const parent = path.length > 1 ? blockAtPath(document, path.slice(0, -1)) : void 0;
165
+ const parentKind = parent === void 0 ? void 0 : blockKind(parent);
166
+ if (!standsWithin(declared, parentKind)) diagnostics.push({
167
+ code: "MisplacedNode",
168
+ node: block.id,
169
+ detail: kind,
170
+ message: `"${kind}" cannot stand ${parentKind === void 0 ? "at the top level" : `in "${parentKind}"`}`
171
+ });
172
+ }
173
+ }
174
+ const forbidsMarks = declared?.kind === "node" && declared.marks === "none";
175
+ for (const run of block.children) for (const mark of run.marks) {
176
+ const name = markName$1(mark);
177
+ if (forbidsMarks) diagnostics.push({
178
+ code: "ForbiddenMark",
179
+ node: run.id,
180
+ detail: name,
181
+ message: `"${kind}" accepts no marks`
182
+ });
183
+ const declaredMark = declaredMarks.get(name);
184
+ if (declaredMark === void 0) diagnostics.push({
185
+ code: "UnknownMark",
186
+ node: run.id,
187
+ detail: name,
188
+ message: `The Kit does not declare mark "${name}"`
189
+ });
190
+ else if (propsFailure(declaredMark.Props, markProps(mark))) diagnostics.push({
191
+ code: "InvalidProps",
192
+ node: run.id,
193
+ detail: name,
194
+ message: `"${name}" props do not match its declared schema`
195
+ });
196
+ }
197
+ });
198
+ return diagnostics;
199
+ };
200
+ export { atom, block, blockKind, blocksOf, inspectKit, kit, node, nodeRegistry, standsWithin, validate };
201
+
202
+ //# sourceMappingURL=kit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kit.js","names":["markName"],"sources":["../src/kit.ts"],"sourcesContent":["import type { Schema } from 'effect'\nimport {\n blockAtPath,\n blockContent,\n eachBlock,\n textContent,\n type Block,\n type ContentMode,\n type Document,\n type NodeBlock,\n type NodeId,\n type PropsSchema,\n} from './document.js'\nimport { markName, markProps, propsFailure, type MarkDef } from './marks.js'\n\n/** Whether a kind's runs may carry formatting marks; `all` unless declared otherwise. */\nexport type MarksPolicy = 'all' | 'none'\n\n/**\n * Block content that accepts only the named kinds, so a `List` can declare that its\n * children are `ListItem`s and `validate` can report one that is not (§13, §125).\n */\nexport interface BlockConstraint {\n readonly mode: typeof blockContent\n readonly of: ReadonlyArray<string>\n}\n\n/** Declares that a node holds nested blocks, only of the named kinds. */\nexport const blocksOf = (...kinds: ReadonlyArray<string>): BlockConstraint =>\n Object.freeze({ mode: blockContent, of: [...kinds] })\n\n/** A kind's declared content: a mode, or block content restricted to named kinds. */\nexport type ChildrenDeclaration = ContentMode | BlockConstraint\n\n/**\n * The node shapes this version can declare: a `block` is one of the built-in\n * text blocks, an `atom` is an addressable node holding nothing (with optional\n * props), and an application `node` holds runs or nested blocks plus props its own\n * schema validates (§13).\n */\nexport type NodeDefinition =\n | { readonly name: string; readonly kind: 'block'; readonly children: 'text' }\n | {\n readonly name: string\n readonly kind: 'atom'\n readonly children: 'none'\n /** An atom's props decode at this boundary too, so an `Image` carries a source. */\n readonly props?: PropsSchema | undefined\n }\n | {\n readonly name: string\n readonly kind: 'node'\n /** `textContent` holds runs; `blockContent` or `blocksOf(…)` holds nested blocks. */\n readonly children: ChildrenDeclaration\n /** `none` forbids marks on this kind's runs, as a `CodeBlock` needs. */\n readonly marks: MarksPolicy\n /** Validates a `Node` block's `props` at this boundary, not in the codec. */\n readonly props?: PropsSchema | undefined\n /**\n * A boundary a lift never crosses: a table cell's content stays in the cell, where a\n * quote's or a list item's would be lifted out.\n */\n readonly isolating?: boolean | undefined\n /** Props a copy split off this node starts with, over the original's (§135). */\n readonly splitProps?: NodeBlock['props'] | undefined\n /**\n * The kinds this one may stand in (§149); absent, any that holds it. A kind that names\n * them never stands at the top level, as a `ListItem` stands only in a `List`.\n */\n readonly within?: ReadonlyArray<string> | undefined\n /** Its text keeps line breaks: Enter inserts one rather than splitting, as in code. */\n readonly lineBreaks?: boolean | undefined\n /** Tab indents its lines, and a new line starts as indented as the last, as in code. */\n readonly indents?: boolean | undefined\n }\n\n/**\n * A declared application node, with the caller's name, prop schema, and content\n * mode kept in the type: the erasure to `any` happens only where heterogeneous\n * definitions are collected, not at the authoring call.\n */\nexport interface NodeDefinitionOf<\n Name extends string,\n Props extends PropsSchema | undefined,\n Children extends ChildrenDeclaration = typeof textContent,\n> {\n readonly name: Name\n readonly kind: 'node'\n readonly children: Children\n readonly marks: MarksPolicy\n readonly props: Props\n readonly isolating: boolean\n readonly splitProps: NodeBlock['props'] | undefined\n readonly within: ReadonlyArray<string> | undefined\n readonly lineBreaks: boolean\n readonly indents: boolean\n}\n\n/** Declares a block node kind: a top-level node containing text runs. */\nexport const block = (name: string): NodeDefinition => ({\n name,\n kind: 'block',\n children: 'text',\n})\n\n/**\n * Declares an atom: an addressable node with no text content. Its optional props\n * decode at the same boundary as a `node`'s, so an `Image` is an atom carrying a\n * source rather than a run holder.\n */\nexport const atom = (\n name: string,\n options: { readonly Props?: PropsSchema | undefined } = {},\n): NodeDefinition => ({ name, kind: 'atom', children: 'none', props: options.Props })\n\n/**\n * Declares an application node kind: a `Node` block whose props this schema\n * validates and whose content the `children` declaration states — a mode, or block\n * content restricted to the kinds `blocksOf` names. `marks: 'none'` forbids marks on\n * its runs. The document codec keeps those props as JSON; the Kit is where an\n * application's types meet them.\n */\nexport const node = <\n const Name extends string,\n Props extends PropsSchema | undefined = undefined,\n Children extends ChildrenDeclaration = typeof textContent,\n>(\n name: Name,\n options: {\n readonly Props?: Props\n readonly children?: Children\n readonly marks?: MarksPolicy\n /** A boundary a lift never crosses, as a table cell is. */\n readonly isolating?: boolean\n /**\n * Props a copy split off this node starts with, laid over the original's: Enter in a\n * checked task item starts an unchecked one.\n */\n readonly splitProps?: Props extends PropsSchema\n ? Partial<Schema.Schema.Type<Props>>\n : NodeBlock['props']\n /** The kinds this one may stand in; it then never stands at the top level (§149). */\n readonly within?: ReadonlyArray<string>\n /**\n * Its text keeps line breaks, as code does: Enter inserts a newline instead of splitting\n * the block, and Enter on an empty last line leaves it for a paragraph after. A callout's\n * text can keep them too, for a note of several lines.\n */\n readonly lineBreaks?: boolean\n /**\n * Its lines are indented, as code's are: Tab and Shift+Tab indent and outdent them, and a\n * line broken with Enter starts as indented as the one before.\n */\n readonly indents?: boolean\n } = {},\n): NodeDefinitionOf<Name, Props, Children> => ({\n name,\n kind: 'node',\n children: (options.children ?? textContent) as Children,\n marks: options.marks ?? 'all',\n props: options.Props as Props,\n isolating: options.isolating ?? false,\n splitProps: options.splitProps,\n within: options.within,\n lineBreaks: options.lineBreaks ?? false,\n indents: options.indents ?? false,\n})\n\n/**\n * The vocabulary one editor accepts: which node kinds and marks are available.\n * A Kit is a definition, never document state, and it never holds renderers or\n * executable code. Transforms and metadata arrive later.\n */\nexport interface Kit {\n readonly nodes: ReadonlyArray<NodeDefinition>\n /** Mark definitions, not just names: a Kit carries each mark's boundary policy. */\n readonly marks: ReadonlyArray<MarkDef>\n}\n\nexport const kit = (definition: {\n readonly nodes: ReadonlyArray<NodeDefinition>\n readonly marks: ReadonlyArray<MarkDef>\n}): Kit => Object.freeze({ nodes: [...definition.nodes], marks: [...definition.marks] })\n\nexport interface Diagnostic {\n readonly code:\n | 'UnknownNode'\n | 'UnsupportedNode'\n | 'UnknownMark'\n | 'InvalidProps'\n | 'MismatchedDefinition'\n | 'UnexpectedChild'\n | 'MisplacedNode'\n | 'ForbiddenMark'\n readonly message: string\n readonly node?: NodeId\n readonly detail?: string\n}\n\n/** Whether a block is held as a built-in block or as an application node. */\nconst heldShape = (block: Block): 'block' | 'node' => (block.type === 'Node' ? 'node' : 'block')\n\n/**\n * The content a block actually holds. An empty application node is `either`: the\n * document cannot say whether it is an atom or a run holder that happens to have\n * no runs, so a declaration of either kind agrees with it.\n */\nconst heldContent = (block: Block): 'text' | 'blocks' | 'either' => {\n if (block.type !== 'Node') return 'text'\n if (block.blocks !== undefined) return 'blocks'\n return block.children.length > 0 ? 'text' : 'either'\n}\n\n/** The content mode a declaration names, or `'none'` for an atom. */\nconst declaredContent = (definition: NodeDefinition): ContentMode | 'none' =>\n definition.kind === 'atom'\n ? 'none'\n : typeof definition.children === 'string'\n ? definition.children\n : blockContent\n\n/**\n * Whether a declared definition agrees with the block it is declared for, and\n * why not. A `block` declaration expects a built-in block and a `node` or `atom`\n * one expects an application node; then the declared content mode must match what\n * the block holds, which is what makes an atom's no-children rule real.\n */\nconst definitionMismatch = (definition: NodeDefinition, block: Block): string | undefined => {\n const expected = definition.kind === 'block' ? 'block' : 'node'\n const held = heldShape(block)\n if (held !== expected) {\n return `\"${definition.name}\" is declared as ${definition.kind}, but the document holds it as ${held}`\n }\n const declared = declaredContent(definition)\n const content = heldContent(block)\n const agrees = content === 'either' ? declared !== 'blocks' : declared === content\n return agrees\n ? undefined\n : `\"${definition.name}\" is declared to hold ${declared}, but the document holds ${content}`\n}\n\n/**\n * A block's semantic kind: an application node's declared kind, a preserved node's\n * original type, or a built-in block's own type. This is what a `blocksOf`\n * constraint names, and what a `NodeRegistry` is looked up by.\n */\nexport const blockKind = (block: Block): string =>\n block.type === 'Node' ? block.kind : block.type === 'Unknown' ? block.originalType : block.type\n\n/**\n * The children a constrained kind does not accept. A declaration without a\n * constraint accepts any block, and a block that holds runs has no children to\n * check; `definitionMismatch` is what reports the mode itself.\n */\nconst childKindDiagnostics = (\n definition: NodeDefinition,\n block: Block,\n): ReadonlyArray<Diagnostic> => {\n if (definition.kind !== 'node' || typeof definition.children === 'string') return []\n if (block.type !== 'Node' || block.blocks === undefined) return []\n const allowed = definition.children.of\n const permitted = new Set(allowed)\n return block.blocks\n .filter(child => !permitted.has(blockKind(child)))\n .map(child => ({\n code: 'UnexpectedChild' as const,\n node: child.id,\n detail: blockKind(child),\n message: `\"${block.kind}\" accepts only ${allowed.join(', ')}`,\n }))\n}\n\n/**\n * Whether a kind may stand in a parent of `parentKind`, or at the top level when that is\n * undefined: anywhere, unless its declaration names where (§149).\n */\nexport const standsWithin = (\n declared: NodeDefinition | undefined,\n parentKind: string | undefined,\n): boolean =>\n declared?.kind !== 'node' ||\n declared.within === undefined ||\n (parentKind !== undefined && declared.within.includes(parentKind))\n\n/** Structural summary of a Kit, for tooling and tests. */\nexport const inspectKit = (definition: Kit) => ({\n blocks: definition.nodes.filter(node => node.kind === 'block').length,\n atoms: definition.nodes.filter(node => node.kind === 'atom').length,\n nodes: definition.nodes.filter(node => node.kind === 'node').length,\n marks: definition.marks.length,\n})\n\n/**\n * The node vocabulary an edit may target, the way `MarkRegistry` is the mark\n * vocabulary a mark edit consults. `run` reads a kind's declaration from it to refuse\n * an edit a constraint forbids (§117, §125); `apply` still takes none, so a durable\n * transaction never depends on a vocabulary that may have moved.\n */\nexport interface NodeRegistry {\n /** The declaration for a block kind, or undefined when this vocabulary has none. */\n readonly definitionFor: (kind: string) => NodeDefinition | undefined\n}\n\n/** Builds the registry an edit consults. A later declaration for a name wins, as a Map does. */\nexport const nodeRegistry = (definitions: ReadonlyArray<NodeDefinition>): NodeRegistry => {\n const byName = new Map(definitions.map(definition => [definition.name, definition]))\n return Object.freeze({ definitionFor: (kind: string) => byName.get(kind) })\n}\n\n/**\n * Checks a document against a Kit's vocabulary without changing it, walking\n * nested blocks so a container's children are checked too. Reports preserved\n * unknown blocks, known blocks the Kit does not declare, declarations that\n * disagree with how the document holds a block, application nodes whose props\n * their schema refuses, and marks outside the Kit or whose props their definition\n * refuses. Content diagnostics never strip or repair the document; callers decide\n * whether to block publishing or surface a placeholder.\n */\nexport const validate = (document: Document, definition: Kit): ReadonlyArray<Diagnostic> => {\n const byName = new Map(definition.nodes.map(node => [node.name, node]))\n const declaredMarks = new Map(definition.marks.map(mark => [mark.name, mark]))\n const diagnostics: Array<Diagnostic> = []\n eachBlock(document.children, (block, path) => {\n if (block.type === 'Unknown') {\n diagnostics.push({\n code: 'UnknownNode',\n node: block.id,\n detail: block.originalType,\n message: `Unimplemented node type \"${block.originalType}\"`,\n })\n return\n }\n const kind = blockKind(block)\n const declared = byName.get(kind)\n if (declared === undefined) {\n diagnostics.push({\n code: 'UnsupportedNode',\n node: block.id,\n detail: kind,\n message: `The Kit does not declare ${block.type === 'Node' ? 'node ' : ''}\"${kind}\"`,\n })\n } else {\n const mismatch = definitionMismatch(declared, block)\n if (mismatch !== undefined) {\n diagnostics.push({\n code: 'MismatchedDefinition',\n node: block.id,\n detail: kind,\n message: mismatch,\n })\n } else {\n if (\n block.type === 'Node' &&\n declared.kind !== 'block' &&\n propsFailure(declared.props, block.props)\n ) {\n diagnostics.push({\n code: 'InvalidProps',\n node: block.id,\n detail: kind,\n message: `\"${kind}\" props do not match its declared schema`,\n })\n }\n diagnostics.push(...childKindDiagnostics(declared, block))\n const parent = path.length > 1 ? blockAtPath(document, path.slice(0, -1)) : undefined\n const parentKind = parent === undefined ? undefined : blockKind(parent)\n if (!standsWithin(declared, parentKind)) {\n diagnostics.push({\n code: 'MisplacedNode',\n node: block.id,\n detail: kind,\n message: `\"${kind}\" cannot stand ${parentKind === undefined ? 'at the top level' : `in \"${parentKind}\"`}`,\n })\n }\n }\n }\n // A kind declared mark-free has no formatting to carry, so any mark it does is\n // reported as well as checked against the vocabulary that declared it.\n const forbidsMarks = declared?.kind === 'node' && declared.marks === 'none'\n for (const run of block.children) {\n for (const mark of run.marks) {\n const name = markName(mark)\n if (forbidsMarks) {\n diagnostics.push({\n code: 'ForbiddenMark',\n node: run.id,\n detail: name,\n message: `\"${kind}\" accepts no marks`,\n })\n }\n const declaredMark = declaredMarks.get(name)\n if (declaredMark === undefined) {\n diagnostics.push({\n code: 'UnknownMark',\n node: run.id,\n detail: name,\n message: `The Kit does not declare mark \"${name}\"`,\n })\n } else if (propsFailure(declaredMark.Props, markProps(mark))) {\n diagnostics.push({\n code: 'InvalidProps',\n node: run.id,\n detail: name,\n message: `\"${name}\" props do not match its declared schema`,\n })\n }\n }\n }\n })\n return diagnostics\n}\n"],"mappings":";;;;AA4BA,MAAa,YAAY,GAAG,UAC1B,OAAO,OAAO;CAAE,MAAM;CAAc,IAAI,CAAC,GAAG,KAAK;AAAE,CAAC;;AAsEtD,MAAa,SAAS,UAAkC;CACtD;CACA,MAAM;CACN,UAAU;AACZ;;;;;;AAOA,MAAa,QACX,MACA,UAAwD,CAAC,OACrC;CAAE;CAAM,MAAM;CAAQ,UAAU;CAAQ,OAAO,QAAQ;AAAM;;;;;;;;AASnF,MAAa,QAKX,MACA,UA0BI,CAAC,OACwC;CAC7C;CACA,MAAM;CACN,UAAW,QAAQ,YAAY;CAC/B,OAAO,QAAQ,SAAS;CACxB,OAAO,QAAQ;CACf,WAAW,QAAQ,aAAa;CAChC,YAAY,QAAQ;CACpB,QAAQ,QAAQ;CAChB,YAAY,QAAQ,cAAc;CAClC,SAAS,QAAQ,WAAW;AAC9B;AAaA,MAAa,OAAO,eAGT,OAAO,OAAO;CAAE,OAAO,CAAC,GAAG,WAAW,KAAK;CAAG,OAAO,CAAC,GAAG,WAAW,KAAK;AAAE,CAAC;;AAkBvF,MAAM,aAAa,UAAoC,MAAM,SAAS,SAAS,SAAS;;;;;;AAOxF,MAAM,eAAe,UAA+C;CAClE,IAAI,MAAM,SAAS,QAAQ,OAAO;CAClC,IAAI,MAAM,WAAW,KAAA,GAAW,OAAO;CACvC,OAAO,MAAM,SAAS,SAAS,IAAI,SAAS;AAC9C;;AAGA,MAAM,mBAAmB,eACvB,WAAW,SAAS,SAChB,SACA,OAAO,WAAW,aAAa,WAC7B,WAAW,WACX;;;;;;;AAQR,MAAM,sBAAsB,YAA4B,UAAqC;CAC3F,MAAM,WAAW,WAAW,SAAS,UAAU,UAAU;CACzD,MAAM,OAAO,UAAU,KAAK;CAC5B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AY,UAAU,KAAK,MAAM,GAAG,EAAE,CAAC,IAAI,KAAA;IAC5E,MAAM,aAAa,WAAW,KAAA,IAAY,KAAA,IAAY,UAAU,MAAM;IACtE,IAAI,CAAC,aAAa,UAAU,UAAU,GACpC,YAAY,KAAK;KACf,MAAM;KACN,MAAM,MAAM;KACZ,QAAQ;KACR,SAAS,IAAI,KAAK,iBAAiB,eAAe,KAAA,IAAY,qBAAqB,OAAO,WAAW;IACvG,CAAC;GAEL;EACF;EAGA,MAAM,eAAe,UAAU,SAAS,UAAU,SAAS,UAAU;EACrE,KAAK,MAAM,OAAO,MAAM,UACtB,KAAK,MAAM,QAAQ,IAAI,OAAO;GAC5B,MAAM,OAAOA,WAAS,IAAI;GAC1B,IAAI,cACF,YAAY,KAAK;IACf,MAAM;IACN,MAAM,IAAI;IACV,QAAQ;IACR,SAAS,IAAI,KAAK;GACpB,CAAC;GAEH,MAAM,eAAe,cAAc,IAAI,IAAI;GAC3C,IAAI,iBAAiB,KAAA,GACnB,YAAY,KAAK;IACf,MAAM;IACN,MAAM,IAAI;IACV,QAAQ;IACR,SAAS,kCAAkC,KAAK;GAClD,CAAC;QACI,IAAI,aAAa,aAAa,OAAO,UAAU,IAAI,CAAC,GACzD,YAAY,KAAK;IACf,MAAM;IACN,MAAM,IAAI;IACV,QAAQ;IACR,SAAS,IAAI,KAAK;GACpB,CAAC;EAEL;CAEJ,CAAC;CACD,OAAO;AACT"}
@@ -0,0 +1,79 @@
1
+ import { Schema } from "effect";
2
+ import { Document, MarkValue, Position, PropsSchema, RunMark, markName } from "./document.js";
3
+ /** Which edges a mark continues across when typing at a run boundary. */
4
+ export type MarkExpansion = 'before' | 'after' | 'both' | 'none';
5
+ /**
6
+ * A mark definition: its name, where it continues across a boundary, and the
7
+ * schema its props must satisfy when it has any. A Kit declares the vocabulary
8
+ * an editor accepts, so an application can say that its own mark behaves like a
9
+ * link (nothing expands, an `href` prop) or like bold (it continues).
10
+ */
11
+ export interface MarkDef<Props extends PropsSchema | undefined = PropsSchema | undefined> {
12
+ readonly name: string;
13
+ readonly expand: MarkExpansion;
14
+ /** Validates a mark value's props at the Kit boundary, not in the codec. */
15
+ readonly Props: Props;
16
+ /** Encodes decoded props into the JSON value stored on a text run. */
17
+ readonly of: Props extends Schema.Codec<infer A, any, any> ? (props: A) => MarkValue : () => MarkValue;
18
+ }
19
+ /**
20
+ * Declares a mark. Expansion defaults to `both`: the conservative choice for a
21
+ * name no policy knows. A definition with `Props` gets an `of` that builds the
22
+ * value; a definition without props builds a value that carries only its name.
23
+ */
24
+ export declare const mark: <Props extends PropsSchema | undefined = undefined>(name: string, options?: {
25
+ readonly Props?: Props;
26
+ readonly expand?: MarkExpansion;
27
+ }) => MarkDef<Props>;
28
+ export declare const Bold: MarkDef;
29
+ export declare const Italic: MarkDef;
30
+ export declare const Code: MarkDef;
31
+ /** The marks this vocabulary defines, and the policy a document without a Kit gets. */
32
+ export declare const shippedMarks: ReadonlyArray<MarkDef>;
33
+ /**
34
+ * Whether props fail to decode against a node's or a mark's declared schema. The
35
+ * diagnostic is deliberately stable: a schema's own message can name internals
36
+ * an API boundary should not leak, so only the verdict travels.
37
+ */
38
+ export declare const propsFailure: (props: PropsSchema | undefined, value: unknown) => boolean;
39
+ /**
40
+ * The expansion policy of a set of definitions, and which names it declares. A
41
+ * mark the registry does not declare expands both ways: preservation never
42
+ * retargets it away. Declared names are what an edit may add.
43
+ */
44
+ export interface MarkRegistry {
45
+ readonly expansionOf: (name: string) => MarkExpansion;
46
+ readonly declares: (name: string) => boolean;
47
+ /** Whether an edit may add this mark: its name is declared and its props decode. */
48
+ readonly accepts: (mark: RunMark) => boolean;
49
+ }
50
+ export declare const markRegistry: (definitions: ReadonlyArray<MarkDef>) => MarkRegistry;
51
+ /** The policy of the shipped vocabulary. */
52
+ export declare const shippedRegistry: MarkRegistry;
53
+ /** A mark's props, or undefined when it carries none. */
54
+ export declare const markProps: (mark: RunMark) => MarkValue['props'] | undefined;
55
+ /**
56
+ * Whether two marks are the same mark with the same props. This is the
57
+ * equivalence normalization merges on, so props compare structurally and key
58
+ * order does not matter.
59
+ */
60
+ export declare const sameMark: (left: RunMark, right: RunMark) => boolean;
61
+ /** Order-insensitive mark-set equality; a run carries a name at most once. */
62
+ export declare const sameMarkSet: (left: ReadonlyArray<RunMark>, right: ReadonlyArray<RunMark>) => boolean;
63
+ /**
64
+ * Retargets a boundary insertion to the neighboring run when the current run
65
+ * carries marks that forbid the edge and the neighbor carries exactly the
66
+ * marks that remain. Interior positions, missing neighbors, and mismatched
67
+ * neighbors return the input reference unchanged; unknown node ids are left
68
+ * for `apply` to diagnose. Never invents runs: mixed-mark edges stay put.
69
+ */
70
+ export declare const resolveInsertion: (document: Document, position: Position, registry?: MarkRegistry) => Position;
71
+ /**
72
+ * The marks text typed at the outer edge of a block should carry, when they differ from its run's:
73
+ * a mark that does not extend past that edge (a link, a footnote reference) stops there. There is
74
+ * no neighbouring run to type into instead, as `resolveInsertion` finds inside a block, so the
75
+ * text needs a run of its own. Undefined anywhere else, and where every mark extends.
76
+ */
77
+ export declare const edgeMarks: (document: Document, position: Position, registry?: MarkRegistry) => ReadonlyArray<RunMark> | undefined;
78
+ export { markName };
79
+ //# sourceMappingURL=marks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"marks.d.ts","names":[],"sources":["../src/marks.ts"],"mappings":";;;YAcY;;;;;;;iBAUK,QAAQ,cAAc,0BAA0B;WACtD;WACA,QAAQ;;WAER,OAAO;;WAEP,IAAI,cAAc,OAAO,YAAY,gBACzC,OAAO,MAAM,kBACR;;;;;;;qBAQC,OAAQ,cAAc,qCAAmC,cACxD;WACQ,QAAQ;WAAgB,SAAS;MACpD,QAAQ;qBAoBE,MAAM;qBACN,QAAQ;qBACR,MAAM;;qBAGN,cAAc,cAAc;;;;;;qBAO5B,eAAY,OAAW,yBAAuB;;;;;;iBAiB1C;WACN,cAAc,iBAAiB;WAC/B,WAAW;;WAEX,UAAU,MAAM;;qBAGd,eAAY,aAAiB,cAAc,aAAW;;qBAatD,iBAAiB;;qBAGjB,YAAS,MAAU,YAAU;;;;;;qBAQ7B,WAAQ,MAAU,SAAO,OAAS;;qBAIlC,cAAW,MAAU,cAAc,UAAQ,OAAS,cAAc;;;;;;;;qBAUlE,mBAAgB,UACjB,UAAQ,UACR,UAAQ,WACR,iBACT;;;;;;;qBAwCU,YAAS,UACV,UAAQ,UACR,UAAQ,WACR,iBACT,cAAc"}
package/dist/marks.js ADDED
@@ -0,0 +1,127 @@
1
+ import { Equal, Schema } from "effect";
2
+ import { eachBlock, markName } from "./document.js";
3
+ // src/marks.ts
4
+ const decodeJsonProps = Schema.decodeUnknownSync(Schema.JsonObject);
5
+ /**
6
+ * Declares a mark. Expansion defaults to `both`: the conservative choice for a
7
+ * name no policy knows. A definition with `Props` gets an `of` that builds the
8
+ * value; a definition without props builds a value that carries only its name.
9
+ */
10
+ const mark = (name, options = {}) => {
11
+ if (name.length === 0) throw new Error("RichText.mark: a mark needs a name");
12
+ const encodeProps = options.Props === void 0 ? void 0 : Schema.encodeUnknownSync(options.Props, { onExcessProperty: "error" });
13
+ return {
14
+ name,
15
+ expand: options.expand ?? "both",
16
+ Props: options.Props,
17
+ of: ((props) => {
18
+ if (encodeProps === void 0) return { name };
19
+ return {
20
+ name,
21
+ props: decodeJsonProps(encodeProps(props))
22
+ };
23
+ })
24
+ };
25
+ };
26
+ const Bold = mark("Bold", { expand: "after" });
27
+ const Italic = mark("Italic", { expand: "after" });
28
+ const Code = mark("Code", { expand: "none" });
29
+ /** The marks this vocabulary defines, and the policy a document without a Kit gets. */
30
+ const shippedMarks = [
31
+ Bold,
32
+ Italic,
33
+ Code
34
+ ];
35
+ /**
36
+ * Whether props fail to decode against a node's or a mark's declared schema. The
37
+ * diagnostic is deliberately stable: a schema's own message can name internals
38
+ * an API boundary should not leak, so only the verdict travels.
39
+ */
40
+ const propsFailure = (props, value) => {
41
+ if (props === void 0) return false;
42
+ try {
43
+ Schema.decodeUnknownSync(props, { onExcessProperty: "error" })(value);
44
+ return false;
45
+ } catch {
46
+ return true;
47
+ }
48
+ };
49
+ const markRegistry = (definitions) => {
50
+ const byName = new Map(definitions.map((definition) => [definition.name, definition]));
51
+ return {
52
+ expansionOf: (name) => byName.get(name)?.expand ?? "both",
53
+ declares: (name) => byName.has(name),
54
+ accepts: (mark) => {
55
+ const definition = byName.get(markName(mark));
56
+ return definition !== void 0 && !propsFailure(definition.Props, markProps(mark));
57
+ }
58
+ };
59
+ };
60
+ /** The policy of the shipped vocabulary. */
61
+ const shippedRegistry = markRegistry(shippedMarks);
62
+ /** A mark's props, or undefined when it carries none. */
63
+ const markProps = (mark) => typeof mark === "string" ? void 0 : mark.props;
64
+ /**
65
+ * Whether two marks are the same mark with the same props. This is the
66
+ * equivalence normalization merges on, so props compare structurally and key
67
+ * order does not matter.
68
+ */
69
+ const sameMark = (left, right) => markName(left) === markName(right) && Equal.equals(markProps(left), markProps(right));
70
+ /** Order-insensitive mark-set equality; a run carries a name at most once. */
71
+ const sameMarkSet = (left, right) => left.length === right.length && left.every((mark) => right.some((other) => sameMark(mark, other)));
72
+ /**
73
+ * Retargets a boundary insertion to the neighboring run when the current run
74
+ * carries marks that forbid the edge and the neighbor carries exactly the
75
+ * marks that remain. Interior positions, missing neighbors, and mismatched
76
+ * neighbors return the input reference unchanged; unknown node ids are left
77
+ * for `apply` to diagnose. Never invents runs: mixed-mark edges stay put.
78
+ */
79
+ const resolveInsertion = (document, position, registry = shippedRegistry) => {
80
+ let resolved = position;
81
+ eachBlock(document.children, (block) => {
82
+ const index = block.children.findIndex((run) => run.id === position.node);
83
+ const run = block.children[index];
84
+ if (run === void 0) return;
85
+ const leavingLeft = position.affinity === "before" && position.offset === 0;
86
+ const leavingRight = position.affinity === "after" && position.offset === run.text.length;
87
+ if (!leavingLeft && !leavingRight) return;
88
+ const neighbor = leavingLeft ? block.children[index - 1] : block.children[index + 1];
89
+ if (neighbor === void 0) return;
90
+ const desired = extending(run.marks, leavingLeft ? "before" : "after", registry);
91
+ if (!sameMarkSet(desired, neighbor.marks)) return;
92
+ resolved = {
93
+ node: neighbor.id,
94
+ offset: leavingLeft ? neighbor.text.length : 0,
95
+ affinity: position.affinity
96
+ };
97
+ });
98
+ return resolved;
99
+ };
100
+ /** The marks of a run that extend past its edge in this direction. */
101
+ const extending = (marks, direction, registry) => marks.filter((mark) => {
102
+ const expand = registry.expansionOf(markName(mark));
103
+ return expand === "both" || expand === direction;
104
+ });
105
+ /**
106
+ * The marks text typed at the outer edge of a block should carry, when they differ from its run's:
107
+ * a mark that does not extend past that edge (a link, a footnote reference) stops there. There is
108
+ * no neighbouring run to type into instead, as `resolveInsertion` finds inside a block, so the
109
+ * text needs a run of its own. Undefined anywhere else, and where every mark extends.
110
+ */
111
+ const edgeMarks = (document, position, registry = shippedRegistry) => {
112
+ let found;
113
+ eachBlock(document.children, (block) => {
114
+ const index = block.children.findIndex((run) => run.id === position.node);
115
+ const run = block.children[index];
116
+ if (run === void 0 || run.text.length === 0) return;
117
+ const atEnd = index === block.children.length - 1 && position.offset === run.text.length;
118
+ const atStart = index === 0 && position.offset === 0;
119
+ if (!atEnd && !atStart) return;
120
+ const desired = extending(run.marks, atEnd ? "after" : "before", registry);
121
+ if (desired.length !== run.marks.length) found = desired;
122
+ });
123
+ return found;
124
+ };
125
+ export { Bold, Code, Italic, edgeMarks, mark, markName, markProps, markRegistry, propsFailure, resolveInsertion, sameMark, sameMarkSet, shippedMarks, shippedRegistry };
126
+
127
+ //# sourceMappingURL=marks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"marks.js","names":[],"sources":["../src/marks.ts"],"sourcesContent":["import { Equal, Schema } from 'effect'\nimport {\n eachBlock,\n markName,\n type Document,\n type MarkValue,\n type Position,\n type PropsSchema,\n type RunMark,\n} from './document.js'\n\nexport { markName }\n\n/** Which edges a mark continues across when typing at a run boundary. */\nexport type MarkExpansion = 'before' | 'after' | 'both' | 'none'\n\nconst decodeJsonProps = Schema.decodeUnknownSync(Schema.JsonObject)\n\n/**\n * A mark definition: its name, where it continues across a boundary, and the\n * schema its props must satisfy when it has any. A Kit declares the vocabulary\n * an editor accepts, so an application can say that its own mark behaves like a\n * link (nothing expands, an `href` prop) or like bold (it continues).\n */\nexport interface MarkDef<Props extends PropsSchema | undefined = PropsSchema | undefined> {\n readonly name: string\n readonly expand: MarkExpansion\n /** Validates a mark value's props at the Kit boundary, not in the codec. */\n readonly Props: Props\n /** Encodes decoded props into the JSON value stored on a text run. */\n readonly of: Props extends Schema.Codec<infer A, any, any>\n ? (props: A) => MarkValue\n : () => MarkValue\n}\n\n/**\n * Declares a mark. Expansion defaults to `both`: the conservative choice for a\n * name no policy knows. A definition with `Props` gets an `of` that builds the\n * value; a definition without props builds a value that carries only its name.\n */\nexport const mark = <Props extends PropsSchema | undefined = undefined>(\n name: string,\n options: { readonly Props?: Props; readonly expand?: MarkExpansion } = {},\n): MarkDef<Props> => {\n if (name.length === 0) throw new Error('RichText.mark: a mark needs a name')\n const encodeProps =\n options.Props === undefined\n ? undefined\n : Schema.encodeUnknownSync(options.Props, { onExcessProperty: 'error' })\n return {\n name,\n expand: options.expand ?? 'both',\n Props: options.Props as Props,\n of: ((props?: unknown): MarkValue => {\n if (encodeProps === undefined) return { name }\n // The stored value is the schema's *encoded* form: a transforming codec\n // (`NumberFromString`) must persist `'42'`, which is what Kit validation\n // decodes. Decoding the result against JSON keeps the boundary honest.\n return { name, props: decodeJsonProps(encodeProps(props)) }\n }) as MarkDef<Props>['of'],\n }\n}\n\nexport const Bold: MarkDef = mark('Bold', { expand: 'after' })\nexport const Italic: MarkDef = mark('Italic', { expand: 'after' })\nexport const Code: MarkDef = mark('Code', { expand: 'none' })\n\n/** The marks this vocabulary defines, and the policy a document without a Kit gets. */\nexport const shippedMarks: ReadonlyArray<MarkDef> = [Bold, Italic, Code]\n\n/**\n * Whether props fail to decode against a node's or a mark's declared schema. The\n * diagnostic is deliberately stable: a schema's own message can name internals\n * an API boundary should not leak, so only the verdict travels.\n */\nexport const propsFailure = (props: PropsSchema | undefined, value: unknown): boolean => {\n if (props === undefined) return false\n try {\n // Strict, like the persisted-content boundary: a field the schema does not\n // declare is a failure, not something silently kept beside the props.\n Schema.decodeUnknownSync(props, { onExcessProperty: 'error' })(value)\n return false\n } catch {\n return true\n }\n}\n\n/**\n * The expansion policy of a set of definitions, and which names it declares. A\n * mark the registry does not declare expands both ways: preservation never\n * retargets it away. Declared names are what an edit may add.\n */\nexport interface MarkRegistry {\n readonly expansionOf: (name: string) => MarkExpansion\n readonly declares: (name: string) => boolean\n /** Whether an edit may add this mark: its name is declared and its props decode. */\n readonly accepts: (mark: RunMark) => boolean\n}\n\nexport const markRegistry = (definitions: ReadonlyArray<MarkDef>): MarkRegistry => {\n const byName = new Map(definitions.map(definition => [definition.name, definition]))\n return {\n expansionOf: name => byName.get(name)?.expand ?? 'both',\n declares: name => byName.has(name),\n accepts: mark => {\n const definition = byName.get(markName(mark))\n return definition !== undefined && !propsFailure(definition.Props, markProps(mark))\n },\n }\n}\n\n/** The policy of the shipped vocabulary. */\nexport const shippedRegistry: MarkRegistry = markRegistry(shippedMarks)\n\n/** A mark's props, or undefined when it carries none. */\nexport const markProps = (mark: RunMark): MarkValue['props'] | undefined =>\n typeof mark === 'string' ? undefined : mark.props\n\n/**\n * Whether two marks are the same mark with the same props. This is the\n * equivalence normalization merges on, so props compare structurally and key\n * order does not matter.\n */\nexport const sameMark = (left: RunMark, right: RunMark): boolean =>\n markName(left) === markName(right) && Equal.equals(markProps(left), markProps(right))\n\n/** Order-insensitive mark-set equality; a run carries a name at most once. */\nexport const sameMarkSet = (left: ReadonlyArray<RunMark>, right: ReadonlyArray<RunMark>): boolean =>\n left.length === right.length && left.every(mark => right.some(other => sameMark(mark, other)))\n\n/**\n * Retargets a boundary insertion to the neighboring run when the current run\n * carries marks that forbid the edge and the neighbor carries exactly the\n * marks that remain. Interior positions, missing neighbors, and mismatched\n * neighbors return the input reference unchanged; unknown node ids are left\n * for `apply` to diagnose. Never invents runs: mixed-mark edges stay put.\n */\nexport const resolveInsertion = (\n document: Document,\n position: Position,\n registry: MarkRegistry = shippedRegistry,\n): Position => {\n let resolved = position\n // Nested blocks too: a list item's or a table cell's runs follow the same rule.\n eachBlock(document.children, block => {\n const index = block.children.findIndex(run => run.id === position.node)\n const run = block.children[index]\n if (run === undefined) return\n const leavingLeft = position.affinity === 'before' && position.offset === 0\n const leavingRight = position.affinity === 'after' && position.offset === run.text.length\n if (!leavingLeft && !leavingRight) return\n const neighbor = leavingLeft ? block.children[index - 1] : block.children[index + 1]\n if (neighbor === undefined) return\n const desired = extending(run.marks, leavingLeft ? 'before' : 'after', registry)\n if (!sameMarkSet(desired, neighbor.marks)) return\n resolved = {\n node: neighbor.id,\n offset: leavingLeft ? neighbor.text.length : 0,\n affinity: position.affinity,\n }\n })\n return resolved\n}\n\n/** The marks of a run that extend past its edge in this direction. */\nconst extending = (\n marks: ReadonlyArray<RunMark>,\n direction: 'before' | 'after',\n registry: MarkRegistry,\n): ReadonlyArray<RunMark> =>\n marks.filter(mark => {\n const expand = registry.expansionOf(markName(mark))\n return expand === 'both' || expand === direction\n })\n\n/**\n * The marks text typed at the outer edge of a block should carry, when they differ from its run's:\n * a mark that does not extend past that edge (a link, a footnote reference) stops there. There is\n * no neighbouring run to type into instead, as `resolveInsertion` finds inside a block, so the\n * text needs a run of its own. Undefined anywhere else, and where every mark extends.\n */\nexport const edgeMarks = (\n document: Document,\n position: Position,\n registry: MarkRegistry = shippedRegistry,\n): ReadonlyArray<RunMark> | undefined => {\n let found: ReadonlyArray<RunMark> | undefined\n eachBlock(document.children, block => {\n const index = block.children.findIndex(run => run.id === position.node)\n const run = block.children[index]\n if (run === undefined || run.text.length === 0) return\n const atEnd = index === block.children.length - 1 && position.offset === run.text.length\n const atStart = index === 0 && position.offset === 0\n if (!atEnd && !atStart) return\n const desired = extending(run.marks, atEnd ? 'after' : 'before', registry)\n if (desired.length !== run.marks.length) found = desired\n })\n return found\n}\n"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W;AACzC,CAAC;;;;;;;AAQH,MAAa,aACX,UACA,UACA,WAAyB,oBACc;CACvC,IAAI;CACJ,UAAU,SAAS,WAAU,UAAS;EACpC,MAAM,QAAQ,MAAM,SAAS,WAAU,QAAO,IAAI,OAAO,SAAS,IAAI;EACtE,MAAM,MAAM,MAAM,SAAS;EAC3B,IAAI,QAAQ,KAAA,KAAa,IAAI,KAAK,WAAW,GAAG;EAChD,MAAM,QAAQ,UAAU,MAAM,SAAS,SAAS,KAAK,SAAS,WAAW,IAAI,KAAK;EAClF,MAAM,UAAU,UAAU,KAAK,SAAS,WAAW;EACnD,IAAI,CAAC,SAAS,CAAC,SAAS;EACxB,MAAM,UAAU,UAAU,IAAI,OAAO,QAAQ,UAAU,UAAU,QAAQ;EACzE,IAAI,QAAQ,WAAW,IAAI,MAAM,QAAQ,QAAQ;CACnD,CAAC;CACD,OAAO;AACT"}
@@ -0,0 +1,30 @@
1
+ import { Block } from "./document.js";
2
+ import { Rendering } from "./rendering.js";
3
+ /** A video, played with the browser's controls, at `width` CSS pixels when it says. */
4
+ export declare const Video: import("./kit.js").NodeDefinition;
5
+ /** A sound, played with the browser's controls. */
6
+ export declare const Audio: import("./kit.js").NodeDefinition;
7
+ /** A file to download, under its name, with its size and type when they are known. */
8
+ export declare const Attachment: import("./kit.js").NodeDefinition;
9
+ export declare const mediaNodes: import("./kit.js").NodeDefinition[];
10
+ /** A size as a reader says it: `820 B`, `12 KB`, `3.4 MB`. */
11
+ export declare const fileSize: (bytes: number) => string;
12
+ /**
13
+ * How media draws: a `video` or `audio` with `controls`, which loads only its metadata until it
14
+ * is played, and an attachment as a download link showing its name and size. A source the policy
15
+ * refuses leaves the element without one, so it draws nothing playable.
16
+ */
17
+ export declare const mediaRendering: Rendering;
18
+ /** A file a reader added, as its type says: where it is, and what it is called. */
19
+ export interface MediaFile {
20
+ readonly src: string;
21
+ readonly name: string;
22
+ readonly type: string;
23
+ readonly size: number;
24
+ }
25
+ /**
26
+ * The block for a file a reader added: an `Image`, `Video` or `Audio` when its type is one, and
27
+ * an `Attachment` otherwise. Its identity is a placeholder, which `InsertBlock` replaces.
28
+ */
29
+ export declare const mediaBlock: (file: MediaFile) => Block;
30
+ //# sourceMappingURL=media.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"media.d.ts","names":[],"sources":["../src/media.ts"],"mappings":";;;qBAgBa,0BAAK;;qBAKL,0BAAK;;qBAKL,+BAAU;qBASV,+BAAU;;qBAoBV,WAAQ;;;;;;qBAWR,gBAAgB;;iBAmDZ;WACN;WACA;WACA;WACA;;;;;;qBAOE,aAAU,MAAU,cAAY"}