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/media.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { NodeId } from "./document.js";
|
|
3
|
+
import { atom } from "./kit.js";
|
|
4
|
+
import { rendering } from "./rendering.js";
|
|
5
|
+
import { safeUrl } from "./url.js";
|
|
6
|
+
// src/media.ts
|
|
7
|
+
/**
|
|
8
|
+
* Media beside the standard `Image`: a video and a sound played with the browser's own controls,
|
|
9
|
+
* and a file to download. Opt-in vocabulary like footnotes: add `mediaNodes` to a kit and draw
|
|
10
|
+
* them with `mediaRendering` under the rendering. Each is an atom holding where its media is
|
|
11
|
+
* (`src`); where that is, a `data:` URL read from a file or a URL an upload returned, is the
|
|
12
|
+
* application's to decide.
|
|
13
|
+
*/
|
|
14
|
+
const Width = Schema.optional(Schema.Number.check(Schema.isGreaterThan(0)));
|
|
15
|
+
/** A video, played with the browser's controls, at `width` CSS pixels when it says. */
|
|
16
|
+
const Video = atom("Video", { Props: Schema.Struct({
|
|
17
|
+
src: Schema.String,
|
|
18
|
+
title: Schema.optional(Schema.String),
|
|
19
|
+
width: Width
|
|
20
|
+
}) });
|
|
21
|
+
/** A sound, played with the browser's controls. */
|
|
22
|
+
const Audio = atom("Audio", { Props: Schema.Struct({
|
|
23
|
+
src: Schema.String,
|
|
24
|
+
title: Schema.optional(Schema.String)
|
|
25
|
+
}) });
|
|
26
|
+
/** A file to download, under its name, with its size and type when they are known. */
|
|
27
|
+
const Attachment = atom("Attachment", { Props: Schema.Struct({
|
|
28
|
+
src: Schema.String,
|
|
29
|
+
name: Schema.String,
|
|
30
|
+
size: Schema.optional(Schema.Number.check(Schema.isGreaterThanOrEqualTo(0))),
|
|
31
|
+
type: Schema.optional(Schema.String)
|
|
32
|
+
}) });
|
|
33
|
+
const mediaNodes = [
|
|
34
|
+
Video,
|
|
35
|
+
Audio,
|
|
36
|
+
Attachment
|
|
37
|
+
];
|
|
38
|
+
/** Types a page could run as script or markup if one were ever opened rather than saved. */
|
|
39
|
+
const ACTIVE = /^(text\/html|application\/xhtml\+xml|image\/svg\+xml|text\/xml|application\/xml)\b/i;
|
|
40
|
+
/**
|
|
41
|
+
* Where a media element may load from: what the URL policy allows, or a `data:` URL whose type
|
|
42
|
+
* `accepts` admits. The policy refuses `data:` in general, since a link to one could carry a
|
|
43
|
+
* page; a video's own bytes cannot.
|
|
44
|
+
*/
|
|
45
|
+
const mediaUrl = (value, accepts) => {
|
|
46
|
+
if (typeof value !== "string") return void 0;
|
|
47
|
+
const data = /^data:([^;,]*)[;,]/i.exec(value.trim());
|
|
48
|
+
if (data === null) return safeUrl(value);
|
|
49
|
+
return accepts(data[1] ?? "") ? value.trim() : void 0;
|
|
50
|
+
};
|
|
51
|
+
const text = (value) => typeof value === "string" ? value : "";
|
|
52
|
+
/** A size as a reader says it: `820 B`, `12 KB`, `3.4 MB`. */
|
|
53
|
+
const fileSize = (bytes) => {
|
|
54
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
55
|
+
if (bytes < 1048576) return `${Math.round(bytes / 1024)} KB`;
|
|
56
|
+
return `${(bytes / 1048576).toFixed(1)} MB`;
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* How media draws: a `video` or `audio` with `controls`, which loads only its metadata until it
|
|
60
|
+
* is played, and an attachment as a download link showing its name and size. A source the policy
|
|
61
|
+
* refuses leaves the element without one, so it draws nothing playable.
|
|
62
|
+
*/
|
|
63
|
+
const mediaRendering = rendering({ nodes: {
|
|
64
|
+
Video: (block) => {
|
|
65
|
+
const src = mediaUrl(block.props.src, (type) => type.toLowerCase().startsWith("video/"));
|
|
66
|
+
const width = block.props.width;
|
|
67
|
+
return {
|
|
68
|
+
tag: "video",
|
|
69
|
+
attributes: {
|
|
70
|
+
...src === void 0 ? {} : { src },
|
|
71
|
+
controls: "",
|
|
72
|
+
preload: "metadata",
|
|
73
|
+
title: text(block.props.title),
|
|
74
|
+
...typeof width === "number" ? { width: String(width) } : {}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
},
|
|
78
|
+
Audio: (block) => {
|
|
79
|
+
const src = mediaUrl(block.props.src, (type) => type.toLowerCase().startsWith("audio/"));
|
|
80
|
+
return {
|
|
81
|
+
tag: "audio",
|
|
82
|
+
attributes: {
|
|
83
|
+
...src === void 0 ? {} : { src },
|
|
84
|
+
controls: "",
|
|
85
|
+
preload: "metadata",
|
|
86
|
+
title: text(block.props.title)
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
Attachment: (block) => {
|
|
91
|
+
const href = mediaUrl(block.props.src, (type) => !ACTIVE.test(type));
|
|
92
|
+
const name = text(block.props.name);
|
|
93
|
+
const size = block.props.size;
|
|
94
|
+
return {
|
|
95
|
+
tag: "a",
|
|
96
|
+
attributes: {
|
|
97
|
+
...href === void 0 ? {} : { href },
|
|
98
|
+
download: name,
|
|
99
|
+
"data-attachment": ""
|
|
100
|
+
},
|
|
101
|
+
adornments: [{
|
|
102
|
+
tag: "span",
|
|
103
|
+
attributes: { "data-attachment-name": "" },
|
|
104
|
+
text: name
|
|
105
|
+
}, ...typeof size === "number" ? [{
|
|
106
|
+
tag: "span",
|
|
107
|
+
attributes: { "data-attachment-size": "" },
|
|
108
|
+
text: fileSize(size)
|
|
109
|
+
}] : []]
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
} });
|
|
113
|
+
/**
|
|
114
|
+
* The block for a file a reader added: an `Image`, `Video` or `Audio` when its type is one, and
|
|
115
|
+
* an `Attachment` otherwise. Its identity is a placeholder, which `InsertBlock` replaces.
|
|
116
|
+
*/
|
|
117
|
+
const mediaBlock = (file) => {
|
|
118
|
+
const family = file.type.toLowerCase().split("/")[0];
|
|
119
|
+
const title = file.name.replace(/\.[^.]+$/, "");
|
|
120
|
+
const node = (kind, props) => ({
|
|
121
|
+
type: "Node",
|
|
122
|
+
kind,
|
|
123
|
+
id: NodeId.make("media"),
|
|
124
|
+
props,
|
|
125
|
+
children: []
|
|
126
|
+
});
|
|
127
|
+
if (family === "image") return node("Image", {
|
|
128
|
+
src: file.src,
|
|
129
|
+
alt: title
|
|
130
|
+
});
|
|
131
|
+
if (family === "video") return node("Video", {
|
|
132
|
+
src: file.src,
|
|
133
|
+
title
|
|
134
|
+
});
|
|
135
|
+
if (family === "audio") return node("Audio", {
|
|
136
|
+
src: file.src,
|
|
137
|
+
title
|
|
138
|
+
});
|
|
139
|
+
return node("Attachment", {
|
|
140
|
+
src: file.src,
|
|
141
|
+
name: file.name,
|
|
142
|
+
size: file.size,
|
|
143
|
+
...file.type === "" ? {} : { type: file.type }
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
export { Attachment, Audio, Video, fileSize, mediaBlock, mediaNodes, mediaRendering };
|
|
147
|
+
|
|
148
|
+
//# sourceMappingURL=media.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media.js","names":[],"sources":["../src/media.ts"],"sourcesContent":["/**\n * Media beside the standard `Image`: a video and a sound played with the browser's own controls,\n * and a file to download. Opt-in vocabulary like footnotes: add `mediaNodes` to a kit and draw\n * them with `mediaRendering` under the rendering. Each is an atom holding where its media is\n * (`src`); where that is, a `data:` URL read from a file or a URL an upload returned, is the\n * application's to decide.\n */\nimport { Schema } from 'effect'\nimport { NodeId, type Block } from './document.js'\nimport { atom } from './kit.js'\nimport { rendering, type Rendering } from './rendering.js'\nimport { safeUrl } from './url.js'\n\nconst Width = Schema.optional(Schema.Number.check(Schema.isGreaterThan(0)))\n\n/** A video, played with the browser's controls, at `width` CSS pixels when it says. */\nexport const Video = atom('Video', {\n Props: Schema.Struct({ src: Schema.String, title: Schema.optional(Schema.String), width: Width }),\n})\n\n/** A sound, played with the browser's controls. */\nexport const Audio = atom('Audio', {\n Props: Schema.Struct({ src: Schema.String, title: Schema.optional(Schema.String) }),\n})\n\n/** A file to download, under its name, with its size and type when they are known. */\nexport const Attachment = atom('Attachment', {\n Props: Schema.Struct({\n src: Schema.String,\n name: Schema.String,\n size: Schema.optional(Schema.Number.check(Schema.isGreaterThanOrEqualTo(0))),\n type: Schema.optional(Schema.String),\n }),\n})\n\nexport const mediaNodes = [Video, Audio, Attachment]\n\n/** Types a page could run as script or markup if one were ever opened rather than saved. */\nconst ACTIVE = /^(text\\/html|application\\/xhtml\\+xml|image\\/svg\\+xml|text\\/xml|application\\/xml)\\b/i\n\n/**\n * Where a media element may load from: what the URL policy allows, or a `data:` URL whose type\n * `accepts` admits. The policy refuses `data:` in general, since a link to one could carry a\n * page; a video's own bytes cannot.\n */\nconst mediaUrl = (value: unknown, accepts: (type: string) => boolean): string | undefined => {\n if (typeof value !== 'string') return undefined\n const data = /^data:([^;,]*)[;,]/i.exec(value.trim())\n if (data === null) return safeUrl(value)\n return accepts(data[1] ?? '') ? value.trim() : undefined\n}\n\nconst text = (value: unknown): string => (typeof value === 'string' ? value : '')\n\n/** A size as a reader says it: `820 B`, `12 KB`, `3.4 MB`. */\nexport const fileSize = (bytes: number): string => {\n if (bytes < 1024) return `${bytes} B`\n if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`\n return `${(bytes / (1024 * 1024)).toFixed(1)} MB`\n}\n\n/**\n * How media draws: a `video` or `audio` with `controls`, which loads only its metadata until it\n * is played, and an attachment as a download link showing its name and size. A source the policy\n * refuses leaves the element without one, so it draws nothing playable.\n */\nexport const mediaRendering: Rendering = rendering({\n nodes: {\n Video: block => {\n const src = mediaUrl(block.props.src, type => type.toLowerCase().startsWith('video/'))\n const width = block.props.width\n return {\n tag: 'video',\n attributes: {\n ...(src === undefined ? {} : { src }),\n controls: '',\n preload: 'metadata',\n title: text(block.props.title),\n ...(typeof width === 'number' ? { width: String(width) } : {}),\n },\n }\n },\n Audio: block => {\n const src = mediaUrl(block.props.src, type => type.toLowerCase().startsWith('audio/'))\n return {\n tag: 'audio',\n attributes: {\n ...(src === undefined ? {} : { src }),\n controls: '',\n preload: 'metadata',\n title: text(block.props.title),\n },\n }\n },\n Attachment: block => {\n const href = mediaUrl(block.props.src, type => !ACTIVE.test(type))\n const name = text(block.props.name)\n const size = block.props.size\n return {\n tag: 'a',\n attributes: {\n ...(href === undefined ? {} : { href }),\n download: name,\n 'data-attachment': '',\n },\n adornments: [\n { tag: 'span', attributes: { 'data-attachment-name': '' }, text: name },\n ...(typeof size === 'number'\n ? [{ tag: 'span', attributes: { 'data-attachment-size': '' }, text: fileSize(size) }]\n : []),\n ],\n }\n },\n },\n})\n\n/** A file a reader added, as its type says: where it is, and what it is called. */\nexport interface MediaFile {\n readonly src: string\n readonly name: string\n readonly type: string\n readonly size: number\n}\n\n/**\n * The block for a file a reader added: an `Image`, `Video` or `Audio` when its type is one, and\n * an `Attachment` otherwise. Its identity is a placeholder, which `InsertBlock` replaces.\n */\nexport const mediaBlock = (file: MediaFile): Block => {\n const family = file.type.toLowerCase().split('/')[0]\n const title = file.name.replace(/\\.[^.]+$/, '')\n const node = (kind: string, props: Readonly<Record<string, string | number>>): Block => ({\n type: 'Node',\n kind,\n id: NodeId.make('media'),\n props,\n children: [],\n })\n if (family === 'image') return node('Image', { src: file.src, alt: title })\n if (family === 'video') return node('Video', { src: file.src, title })\n if (family === 'audio') return node('Audio', { src: file.src, title })\n return node('Attachment', {\n src: file.src,\n name: file.name,\n size: file.size,\n ...(file.type === '' ? {} : { type: file.type }),\n })\n}\n"],"mappings":";;;;;;;;;;;;;AAaA,MAAM,QAAQ,OAAO,SAAS,OAAO,OAAO,MAAM,OAAO,cAAc,CAAC,CAAC,CAAC;;AAG1E,MAAa,QAAQ,KAAK,SAAS,EACjC,OAAO,OAAO,OAAO;CAAE,KAAK,OAAO;CAAQ,OAAO,OAAO,SAAS,OAAO,MAAM;CAAG,OAAO;AAAM,CAAC,EAClG,CAAC;;AAGD,MAAa,QAAQ,KAAK,SAAS,EACjC,OAAO,OAAO,OAAO;CAAE,KAAK,OAAO;CAAQ,OAAO,OAAO,SAAS,OAAO,MAAM;AAAE,CAAC,EACpF,CAAC;;AAGD,MAAa,aAAa,KAAK,cAAc,EAC3C,OAAO,OAAO,OAAO;CACnB,KAAK,OAAO;CACZ,MAAM,OAAO;CACb,MAAM,OAAO,SAAS,OAAO,OAAO,MAAM,OAAO,uBAAuB,CAAC,CAAC,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I,EAAE,MAAM,KAAK,KAAK;CAChD,CAAC;AACH"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { Block, ContentMode, Document, NodeBlock, NodeId } from "./document.js";
|
|
3
|
+
/**
|
|
4
|
+
* Migrations move semantic data forward when a deployment changes its
|
|
5
|
+
* vocabulary (§73): a preserved node that is now implemented, a prop renamed, a
|
|
6
|
+
* block kind replaced. They operate on blocks, never on DOM, and they run at a
|
|
7
|
+
* boundary the application chooses — loading, publishing, or an explicit
|
|
8
|
+
* upgrade — never automatically on every read.
|
|
9
|
+
*
|
|
10
|
+
* Two rules keep a migration safe to run anywhere:
|
|
11
|
+
*
|
|
12
|
+
* - **Identity survives.** A migration returns the same `id`, so positions,
|
|
13
|
+
* references, and selections keep addressing the same node.
|
|
14
|
+
* - **Declining is allowed.** Returning `undefined` keeps the block as it is,
|
|
15
|
+
* which is what a migration does when the data is not its shape after all.
|
|
16
|
+
*/
|
|
17
|
+
export interface Migration {
|
|
18
|
+
readonly name: string;
|
|
19
|
+
/** The type, kind, or preserved `originalType` this migration reads. */
|
|
20
|
+
readonly from: string;
|
|
21
|
+
/** The kind it produces, for a report; the returned block is the truth. */
|
|
22
|
+
readonly to?: string | undefined;
|
|
23
|
+
readonly migrate: (block: Block) => Block | undefined;
|
|
24
|
+
}
|
|
25
|
+
export interface MigrationApplied {
|
|
26
|
+
readonly name: string;
|
|
27
|
+
readonly node: NodeId;
|
|
28
|
+
}
|
|
29
|
+
export interface MigrationReport {
|
|
30
|
+
readonly document: Document;
|
|
31
|
+
/** Every block a migration rewrote, in the order it happened. */
|
|
32
|
+
readonly applied: ReadonlyArray<MigrationApplied>;
|
|
33
|
+
/** Migrations that found no block of their `from` in this document. */
|
|
34
|
+
readonly unused: ReadonlyArray<string>;
|
|
35
|
+
}
|
|
36
|
+
/** Declares a migration, checking at construction that it is shaped like one. */
|
|
37
|
+
export declare const migration: (name: string, from: string, migrate: (block: Block) => Block | undefined, to?: string) => Migration;
|
|
38
|
+
/**
|
|
39
|
+
* Runs each migration over every block it reads, in list order, descending into
|
|
40
|
+
* containers. A migration that changes a block's identity is a bug and throws
|
|
41
|
+
* rather than silently breaking every reference to that node.
|
|
42
|
+
*/
|
|
43
|
+
export declare const migrate: (document: Document, migrations: ReadonlyArray<Migration>) => MigrationReport;
|
|
44
|
+
/**
|
|
45
|
+
* A migration from a preserved unknown block into a declared node kind: the
|
|
46
|
+
* props are decoded by the target's own schema, and a block whose data does not
|
|
47
|
+
* decode is left alone rather than half-converted. The target's content mode
|
|
48
|
+
* decides whether the promoted node holds runs or (empty) nested blocks, so
|
|
49
|
+
* promoting into a container kind produces a valid block rather than one the
|
|
50
|
+
* Kit will call a mismatch.
|
|
51
|
+
*/
|
|
52
|
+
export declare const promoteUnknown: (name: string, from: string, to: string, Props: Schema.Codec<any, any, never>, children?: ContentMode) => Migration;
|
|
53
|
+
/** A node block's props as the codec stores them: plain JSON, nothing more. */
|
|
54
|
+
export declare const nodeProps: (block: NodeBlock) => Readonly<Record<string, unknown>>;
|
|
55
|
+
//# sourceMappingURL=migration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migration.d.ts","names":[],"sources":["../src/migration.ts"],"mappings":";;;;;;;;;;;;;;;;iBAyBiB;WACN;;WAEA;;WAEA;WACA,UAAU,OAAO,UAAU;;iBAGrB;WACN;WACA,MAAM;;iBAGA;WACN,UAAU;;WAEV,SAAS,cAAc;;WAEvB,QAAQ;;;qBAaN,YAAS,cACR,cACA,UACF,OAAO,UAAU,mBAAiB,gBAE3C;;;;;;qBAsDU,UAAO,UACR,UAAQ,YACN,cAAc,eACzB;;;;;;;;;qBA4BU,iBAAc,cACb,cACA,YACF,OACH,OAAO,wBAAsB,WAC1B,gBACT;;qBAmCU,YAAS,OAAW,cAAY,SAAS"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { Block, Document, blockContent, textContent } from "./document.js";
|
|
3
|
+
// src/migration.ts
|
|
4
|
+
const matches = (block, from) => block.type === "Unknown" ? block.originalType === from : block.type === "Node" ? block.kind === from : block.type === from;
|
|
5
|
+
const decodeMigratedDocument = Schema.decodeUnknownSync(Document, { onExcessProperty: "error" });
|
|
6
|
+
/** Declares a migration, checking at construction that it is shaped like one. */
|
|
7
|
+
const migration = (name, from, migrate, to) => {
|
|
8
|
+
if (name.length === 0) throw new Error("Migration.make: a migration needs a name");
|
|
9
|
+
if (from.length === 0) throw new Error(`Migration "${name}": from must not be empty`);
|
|
10
|
+
return {
|
|
11
|
+
name,
|
|
12
|
+
from,
|
|
13
|
+
migrate,
|
|
14
|
+
to
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Runs one migration over a block list, recursing into containers so a nested
|
|
19
|
+
* block is migrated where it sits. A later migration sees what an earlier one
|
|
20
|
+
* produced, because the caller runs them in list order.
|
|
21
|
+
*/
|
|
22
|
+
const migrateBlocks = (blocks, migration, applied) => {
|
|
23
|
+
let changed = false;
|
|
24
|
+
const next = blocks.map((block) => {
|
|
25
|
+
let current = block;
|
|
26
|
+
if (current.type === "Node" && current.blocks !== void 0) {
|
|
27
|
+
const nested = migrateBlocks(current.blocks, migration, applied);
|
|
28
|
+
if (nested.changed) {
|
|
29
|
+
changed = true;
|
|
30
|
+
current = {
|
|
31
|
+
...current,
|
|
32
|
+
blocks: nested.blocks
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (!matches(current, migration.from)) return current;
|
|
37
|
+
const migrated = migration.migrate(current);
|
|
38
|
+
if (migrated === void 0) return current;
|
|
39
|
+
if (migrated.id !== current.id) throw new Error(`Migration "${migration.name}" changed a block's identity`);
|
|
40
|
+
try {
|
|
41
|
+
Schema.decodeUnknownSync(Block, { onExcessProperty: "error" })(migrated);
|
|
42
|
+
} catch {
|
|
43
|
+
throw new Error(`Migration "${migration.name}" produced an invalid block`);
|
|
44
|
+
}
|
|
45
|
+
applied.push({
|
|
46
|
+
name: migration.name,
|
|
47
|
+
node: current.id
|
|
48
|
+
});
|
|
49
|
+
changed = true;
|
|
50
|
+
return migrated;
|
|
51
|
+
});
|
|
52
|
+
return {
|
|
53
|
+
blocks: changed ? next : blocks,
|
|
54
|
+
changed
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Runs each migration over every block it reads, in list order, descending into
|
|
59
|
+
* containers. A migration that changes a block's identity is a bug and throws
|
|
60
|
+
* rather than silently breaking every reference to that node.
|
|
61
|
+
*/
|
|
62
|
+
const migrate = (document, migrations) => {
|
|
63
|
+
const applied = [];
|
|
64
|
+
const unused = [];
|
|
65
|
+
let next = document;
|
|
66
|
+
for (const migration of migrations) {
|
|
67
|
+
const result = migrateBlocks(next.children, migration, applied);
|
|
68
|
+
if (!result.changed) {
|
|
69
|
+
unused.push(migration.name);
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
next = {
|
|
73
|
+
...next,
|
|
74
|
+
children: result.blocks
|
|
75
|
+
};
|
|
76
|
+
try {
|
|
77
|
+
decodeMigratedDocument(next);
|
|
78
|
+
} catch {
|
|
79
|
+
throw new Error(`Migration "${migration.name}" produced an invalid document`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
document: next,
|
|
84
|
+
applied,
|
|
85
|
+
unused
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
89
|
+
* A migration from a preserved unknown block into a declared node kind: the
|
|
90
|
+
* props are decoded by the target's own schema, and a block whose data does not
|
|
91
|
+
* decode is left alone rather than half-converted. The target's content mode
|
|
92
|
+
* decides whether the promoted node holds runs or (empty) nested blocks, so
|
|
93
|
+
* promoting into a container kind produces a valid block rather than one the
|
|
94
|
+
* Kit will call a mismatch.
|
|
95
|
+
*/
|
|
96
|
+
const promoteUnknown = (name, from, to, Props, children = textContent) => migration(name, from, (block) => {
|
|
97
|
+
if (block.type !== "Unknown") return void 0;
|
|
98
|
+
const decoded = decodeProps(Props, block.props);
|
|
99
|
+
if (decoded === void 0) return void 0;
|
|
100
|
+
const promoted = {
|
|
101
|
+
type: "Node",
|
|
102
|
+
kind: to,
|
|
103
|
+
id: block.id,
|
|
104
|
+
props: decoded,
|
|
105
|
+
children: []
|
|
106
|
+
};
|
|
107
|
+
return children === blockContent ? {
|
|
108
|
+
...promoted,
|
|
109
|
+
blocks: []
|
|
110
|
+
} : promoted;
|
|
111
|
+
}, to);
|
|
112
|
+
const decodeProps = (Props, props) => {
|
|
113
|
+
try {
|
|
114
|
+
const decoded = Schema.decodeUnknownSync(Props)(props);
|
|
115
|
+
return Schema.decodeUnknownSync(Schema.JsonObject)(decoded);
|
|
116
|
+
} catch {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
/** A node block's props as the codec stores them: plain JSON, nothing more. */
|
|
121
|
+
const nodeProps = (block) => block.props;
|
|
122
|
+
export { migrate, migration, nodeProps, promoteUnknown };
|
|
123
|
+
|
|
124
|
+
//# sourceMappingURL=migration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migration.js","names":[],"sources":["../src/migration.ts"],"sourcesContent":["import { Schema } from 'effect'\nimport {\n Block,\n NodeBlock,\n NodeId,\n Document,\n blockContent,\n textContent,\n type ContentMode,\n} from './document.js'\n\n/**\n * Migrations move semantic data forward when a deployment changes its\n * vocabulary (§73): a preserved node that is now implemented, a prop renamed, a\n * block kind replaced. They operate on blocks, never on DOM, and they run at a\n * boundary the application chooses — loading, publishing, or an explicit\n * upgrade — never automatically on every read.\n *\n * Two rules keep a migration safe to run anywhere:\n *\n * - **Identity survives.** A migration returns the same `id`, so positions,\n * references, and selections keep addressing the same node.\n * - **Declining is allowed.** Returning `undefined` keeps the block as it is,\n * which is what a migration does when the data is not its shape after all.\n */\nexport interface Migration {\n readonly name: string\n /** The type, kind, or preserved `originalType` this migration reads. */\n readonly from: string\n /** The kind it produces, for a report; the returned block is the truth. */\n readonly to?: string | undefined\n readonly migrate: (block: Block) => Block | undefined\n}\n\nexport interface MigrationApplied {\n readonly name: string\n readonly node: NodeId\n}\n\nexport interface MigrationReport {\n readonly document: Document\n /** Every block a migration rewrote, in the order it happened. */\n readonly applied: ReadonlyArray<MigrationApplied>\n /** Migrations that found no block of their `from` in this document. */\n readonly unused: ReadonlyArray<string>\n}\n\nconst matches = (block: Block, from: string): boolean =>\n block.type === 'Unknown'\n ? block.originalType === from\n : block.type === 'Node'\n ? block.kind === from\n : block.type === from\n\nconst decodeMigratedDocument = Schema.decodeUnknownSync(Document, { onExcessProperty: 'error' })\n\n/** Declares a migration, checking at construction that it is shaped like one. */\nexport const migration = (\n name: string,\n from: string,\n migrate: (block: Block) => Block | undefined,\n to?: string,\n): Migration => {\n if (name.length === 0) throw new Error('Migration.make: a migration needs a name')\n if (from.length === 0) throw new Error(`Migration \"${name}\": from must not be empty`)\n return { name, from, migrate, to }\n}\n\n/**\n * Runs one migration over a block list, recursing into containers so a nested\n * block is migrated where it sits. A later migration sees what an earlier one\n * produced, because the caller runs them in list order.\n */\nconst migrateBlocks = (\n blocks: ReadonlyArray<Block>,\n migration: Migration,\n applied: Array<MigrationApplied>,\n): { readonly blocks: ReadonlyArray<Block>; readonly changed: boolean } => {\n let changed = false\n const next = blocks.map(block => {\n let current = block\n if (current.type === 'Node' && current.blocks !== undefined) {\n const nested = migrateBlocks(current.blocks, migration, applied)\n if (nested.changed) {\n changed = true\n current = { ...current, blocks: nested.blocks }\n }\n }\n if (!matches(current, migration.from)) return current\n const migrated = migration.migrate(current)\n if (migrated === undefined) return current\n // A migration that changes a block's identity is a bug: it would silently\n // break every reference to that node.\n if (migrated.id !== current.id) {\n throw new Error(`Migration \"${migration.name}\" changed a block's identity`)\n }\n // What a migration returns becomes persisted content, so it must still\n // decode as a block: a migration cannot write non-JSON props or an\n // unknown shape into the document.\n try {\n Schema.decodeUnknownSync(Block, { onExcessProperty: 'error' })(migrated)\n } catch {\n throw new Error(`Migration \"${migration.name}\" produced an invalid block`)\n }\n applied.push({ name: migration.name, node: current.id })\n changed = true\n return migrated\n })\n return { blocks: changed ? next : blocks, changed }\n}\n\n/**\n * Runs each migration over every block it reads, in list order, descending into\n * containers. A migration that changes a block's identity is a bug and throws\n * rather than silently breaking every reference to that node.\n */\nexport const migrate = (\n document: Document,\n migrations: ReadonlyArray<Migration>,\n): MigrationReport => {\n const applied: Array<MigrationApplied> = []\n const unused: Array<string> = []\n let next = document\n for (const migration of migrations) {\n const result = migrateBlocks(next.children, migration, applied)\n if (!result.changed) {\n unused.push(migration.name)\n continue\n }\n next = { ...next, children: result.blocks }\n try {\n decodeMigratedDocument(next)\n } catch {\n throw new Error(`Migration \"${migration.name}\" produced an invalid document`)\n }\n }\n return { document: next, applied, unused }\n}\n\n/**\n * A migration from a preserved unknown block into a declared node kind: the\n * props are decoded by the target's own schema, and a block whose data does not\n * decode is left alone rather than half-converted. The target's content mode\n * decides whether the promoted node holds runs or (empty) nested blocks, so\n * promoting into a container kind produces a valid block rather than one the\n * Kit will call a mismatch.\n */\nexport const promoteUnknown = (\n name: string,\n from: string,\n to: string,\n Props: Schema.Codec<any, any, never>,\n children: ContentMode = textContent,\n): Migration =>\n migration(\n name,\n from,\n block => {\n if (block.type !== 'Unknown') return undefined\n const decoded = decodeProps(Props, block.props)\n if (decoded === undefined) return undefined\n const promoted: NodeBlock = {\n type: 'Node',\n kind: to,\n id: block.id,\n props: decoded,\n children: [],\n }\n return children === blockContent ? { ...promoted, blocks: [] } : promoted\n },\n to,\n )\n\nconst decodeProps = (\n Props: Schema.Codec<any, any, never>,\n props: Readonly<Record<string, unknown>>,\n): NodeBlock['props'] | undefined => {\n try {\n const decoded: unknown = Schema.decodeUnknownSync(Props)(props)\n // Persisted props stay JSON: a schema that produces something else is\n // declined rather than written into the document.\n return Schema.decodeUnknownSync(Schema.JsonObject)(decoded)\n } catch {\n return undefined\n }\n}\n\n/** A node block's props as the codec stores them: plain JSON, nothing more. */\nexport const nodeProps = (block: NodeBlock): Readonly<Record<string, unknown>> => block.props\n"],"mappings":";;;AA+CA,MAAM,WAAW,OAAc,SAC7B,MAAM,SAAS,YACX,MAAM,iBAAiB,OACvB,MAAM,SAAS,SACb,MAAM,SAAS,OACf,MAAM,SAAS;AAEvB,MAAM,yBAAyB,OAAO,kBAAkB,UAAU,EAAE,kBAAkB,QAAQ,CAAC;;AAG/F,MAAa,aACX,MACA,MACA,SACA,OACc;CACd,IAAI,KAAK,WAAW,GAAG,MAAM,IAAI,MAAM,0CAA0C;CACjF,IAAI,KAAK,WAAW,GAAG,MAAM,IAAI,MAAM,cAAc,KAAK,0BAA0B;CACpF,OAAO;EAAE;EAAM;EAAM;EAAS;CAAG;AACnC;;;;;;AAOA,MAAM,iBACJ,QACA,WACA,YACyE;CACzE,IAAI,UAAU;CACd,MAAM,OAAO,OAAO,KAAI,UAAS;EAC/B,IAAI,UAAU;EACd,IAAI,QAAQ,SAAS,UAAU,QAAQ,WAAW,KAAA,GAAW;GAC3D,MAAM,SAAS,cAAc,QAAQ,QAAQ,WAAW,OAAO;GAC/D,IAAI,OAAO,SAAS;IAClB,UAAU;IACV,UAAU;KAAE,GAAG;KAAS,QAAQ,OAAO;IAAO;GAChD;EACF;EACA,IAAI,CAAC,QAAQ,SAAS,UAAU,IAAI,GAAG,OAAO;EAC9C,MAAM,WAAW,UAAU,QAAQ,OAAO;EAC1C,IAAI,aAAa,KAAA,GAAW,OAAO;EAGnC,IAAI,SAAS,OAAO,QAAQ,IAC1B,MAAM,IAAI,MAAM,cAAc,UAAU,KAAK,6BAA6B;EAK5E,IAAI;GACF,OAAO,kBAAkB,OAAO,EAAE,kBAAkB,QAAQ,CAAC,CAAC,CAAC,QAAQ;EACzE,QAAQ;GACN,MAAM,IAAI,MAAM,cAAc,UAAU,KAAK,4BAA4B;EAC3E;EACA,QAAQ,KAAK;GAAE,MAAM,UAAU;GAAM,MAAM,QA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|
|
2
|
+
/** One heading: its identity, its level, and its text. */
|
|
3
|
+
export interface OutlineEntry {
|
|
4
|
+
readonly node: NodeId;
|
|
5
|
+
readonly level: number;
|
|
6
|
+
readonly text: string;
|
|
7
|
+
}
|
|
8
|
+
/** Every heading with text, in document order, inside containers too. */
|
|
9
|
+
export declare const outlineOf: (document: Document) => ReadonlyArray<OutlineEntry>;
|
|
10
|
+
/**
|
|
11
|
+
* The heading of the section the selection starts in: the last heading with text at or before
|
|
12
|
+
* its block. Undefined with no selection, or before the first heading.
|
|
13
|
+
*/
|
|
14
|
+
export declare const sectionAt: (document: Document, selection: Selection | null) => NodeId | undefined;
|
|
15
|
+
//# sourceMappingURL=outline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outline.d.ts","names":[],"sources":["../src/outline.ts"],"mappings":";;iBAOiB;WACN,MAAM;WACN;WACA;;;qBAIE,YAAS,UAAc,aAAW,cAAc;;;;;qBAchD,YAAS,UAAc,UAAQ,WAAa,qBAAmB"}
|
package/dist/outline.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { eachBlock, rangeStart } from "./document.js";
|
|
2
|
+
// src/outline.ts
|
|
3
|
+
/**
|
|
4
|
+
* The document's outline: its headings in document order, which a table of contents draws, and
|
|
5
|
+
* the section a selection is in. Both are reads; nothing here changes the document.
|
|
6
|
+
*/
|
|
7
|
+
/** Every heading with text, in document order, inside containers too. */
|
|
8
|
+
const outlineOf = (document) => {
|
|
9
|
+
const entries = [];
|
|
10
|
+
eachBlock(document.children, (block) => {
|
|
11
|
+
if (block.type !== "Heading") return;
|
|
12
|
+
const text = block.children.map((run) => run.text).join("");
|
|
13
|
+
if (text.trim() !== "") entries.push({
|
|
14
|
+
node: block.id,
|
|
15
|
+
level: block.level,
|
|
16
|
+
text
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
return entries;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* The heading of the section the selection starts in: the last heading with text at or before
|
|
23
|
+
* its block. Undefined with no selection, or before the first heading.
|
|
24
|
+
*/
|
|
25
|
+
const sectionAt = (document, selection) => {
|
|
26
|
+
if (selection?.type !== "Range") return void 0;
|
|
27
|
+
const start = rangeStart(document, selection);
|
|
28
|
+
if (start === void 0) return void 0;
|
|
29
|
+
let last;
|
|
30
|
+
let found;
|
|
31
|
+
eachBlock(document.children, (block) => {
|
|
32
|
+
if (block.type === "Heading" && block.children.some((run) => run.text.trim() !== "")) last = block.id;
|
|
33
|
+
if (block.children.some((run) => run.id === start.node)) found = last;
|
|
34
|
+
});
|
|
35
|
+
return found;
|
|
36
|
+
};
|
|
37
|
+
export { outlineOf, sectionAt };
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=outline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"outline.js","names":[],"sources":["../src/outline.ts"],"sourcesContent":["/**\n * The document's outline: its headings in document order, which a table of contents draws, and\n * the section a selection is in. Both are reads; nothing here changes the document.\n */\nimport { eachBlock, rangeStart, type Document, type NodeId, type Selection } from './document.js'\n\n/** One heading: its identity, its level, and its text. */\nexport interface OutlineEntry {\n readonly node: NodeId\n readonly level: number\n readonly text: string\n}\n\n/** Every heading with text, in document order, inside containers too. */\nexport const outlineOf = (document: Document): ReadonlyArray<OutlineEntry> => {\n const entries: Array<OutlineEntry> = []\n eachBlock(document.children, block => {\n if (block.type !== 'Heading') return\n const text = block.children.map(run => run.text).join('')\n if (text.trim() !== '') entries.push({ node: block.id, level: block.level, text })\n })\n return entries\n}\n\n/**\n * The heading of the section the selection starts in: the last heading with text at or before\n * its block. Undefined with no selection, or before the first heading.\n */\nexport const sectionAt = (document: Document, selection: Selection | null): NodeId | undefined => {\n if (selection?.type !== 'Range') return undefined\n const start = rangeStart(document, selection)\n if (start === undefined) return undefined\n let last: NodeId | undefined\n let found: NodeId | undefined\n eachBlock(document.children, block => {\n if (block.type === 'Heading' && block.children.some(run => run.text.trim() !== '')) {\n last = block.id\n }\n if (block.children.some(run => run.id === start.node)) {\n found = last\n }\n })\n return found\n}\n"],"mappings":";;;;;;;AAcA,MAAa,aAAa,aAAoD;CAC5E,MAAM,UAA+B,CAAC;CACtC,UAAU,SAAS,WAAU,UAAS;EACpC,IAAI,MAAM,SAAS,WAAW;EAC9B,MAAM,OAAO,MAAM,SAAS,KAAI,QAAO,IAAI,IAAI,CAAC,CAAC,KAAK,EAAE;EACxD,IAAI,KAAK,KAAK,MAAM,IAAI,QAAQ,KAAK;GAAE,MAAM,MAAM;GAAI,OAAO,MAAM;GAAO;EAAK,CAAC;CACnF,CAAC;CACD,OAAO;AACT;;;;;AAMA,MAAa,aAAa,UAAoB,cAAoD;CAChG,IAAI,WAAW,SAAS,SAAS,OAAO,KAAA;CACxC,MAAM,QAAQ,WAAW,UAAU,SAAS;CAC5C,IAAI,UAAU,KAAA,GAAW,OAAO,KAAA;CAChC,IAAI;CACJ,IAAI;CACJ,UAAU,SAAS,WAAU,UAAS;EACpC,IAAI,MAAM,SAAS,aAAa,MAAM,SAAS,MAAK,QAAO,IAAI,KAAK,KAAK,MAAM,EAAE,GAC/E,OAAO,MAAM;EAEf,IAAI,MAAM,SAAS,MAAK,QAAO,IAAI,OAAO,MAAM,IAAI,GAClD,QAAQ;CAEZ,CAAC;CACD,OAAO;AACT"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { NodeBlock, RunMark, Text } from "./document.js";
|
|
2
|
+
/**
|
|
3
|
+
* How an element is rendered: a tag and its attributes. Data, not a view (§121),
|
|
4
|
+
* so the DOM-free serializer writes it and the DOM interpreters build it without
|
|
5
|
+
* the core learning about Foldkit or the DOM.
|
|
6
|
+
*/
|
|
7
|
+
export interface ElementRendering {
|
|
8
|
+
readonly tag: string;
|
|
9
|
+
/** Values are escaped when written; a name that would end the markup is refused. */
|
|
10
|
+
readonly attributes: Readonly<Record<string, string>>;
|
|
11
|
+
/**
|
|
12
|
+
* An element the nested blocks go inside, where HTML's parser would insert one anyway: a
|
|
13
|
+
* `table`'s rows go in a `tbody`. Without it, markup sent from a server parses to a tree
|
|
14
|
+
* the renderer did not write, and Foldkit refuses to serialize it.
|
|
15
|
+
*/
|
|
16
|
+
readonly inner?: string | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Elements drawn inside the block before its content that are not content: a task's
|
|
19
|
+
* checkbox, an image's picture and caption, a callout's icon. They hold no position, are
|
|
20
|
+
* never edited as text, and an HTML import skips them, so a block's text stays its runs'.
|
|
21
|
+
*/
|
|
22
|
+
readonly adornments?: ReadonlyArray<Adornment> | undefined;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* One element of a block's adornments. `toggles` names a boolean prop of the block that a click
|
|
26
|
+
* on the element flips, through the editor's own Message (a task's `checked`, a toggle's
|
|
27
|
+
* `open`), so the document stays the one place that state lives. `action` names something a
|
|
28
|
+
* click on it asks the application for that is not an edit, a code block's Copy or its language
|
|
29
|
+
* menu: the editor reports it, with the block, and changes nothing.
|
|
30
|
+
*/
|
|
31
|
+
export interface Adornment {
|
|
32
|
+
readonly tag: string;
|
|
33
|
+
readonly attributes: Readonly<Record<string, string>>;
|
|
34
|
+
readonly text?: string | undefined;
|
|
35
|
+
readonly toggles?: string | undefined;
|
|
36
|
+
readonly action?: string | undefined;
|
|
37
|
+
}
|
|
38
|
+
/** The attribute that marks an element as an adornment, which every interpreter skips as content. */
|
|
39
|
+
export declare const ADORNMENT_ATTRIBUTE = "data-adornment";
|
|
40
|
+
/** The attribute naming the prop an adornment's click flips. */
|
|
41
|
+
export declare const TOGGLES_ATTRIBUTE = "data-toggles";
|
|
42
|
+
/** The attribute naming what an adornment's click asks the application for. */
|
|
43
|
+
export declare const ACTION_ATTRIBUTE = "data-action";
|
|
44
|
+
/** An adornment's attributes as every interpreter writes them: its own, then the markers. */
|
|
45
|
+
export declare const adornmentAttributes: (adornment: Adornment) => ReadonlyArray<readonly [string, string]>;
|
|
46
|
+
/** Whether two renderings draw the same element: tag, attributes, holder and adornments. */
|
|
47
|
+
export declare const sameElementRendering: (left: ElementRendering, right: ElementRendering) => boolean;
|
|
48
|
+
/**
|
|
49
|
+
* A rendering entry: fixed data, or computed from the value being rendered, which
|
|
50
|
+
* is how a mark's attributes reach its props. `undefined` means the entry does not
|
|
51
|
+
* apply, so the name falls back the way an entry-less name does.
|
|
52
|
+
*/
|
|
53
|
+
export type RenderingEntry<Value> = ElementRendering | ((value: Value) => ElementRendering | undefined);
|
|
54
|
+
/**
|
|
55
|
+
* A vocabulary of renderings built *over* a Kit, never inside it (§34, §121). A
|
|
56
|
+
* name with no entry renders the shipped way: its mark nests in `strong`, `em`, or
|
|
57
|
+
* `code`, an application node keeps its default element, and any other name rides
|
|
58
|
+
* on `data-marks` — which is what keeps the slice format and an HTML round trip
|
|
59
|
+
* lossless rather than lossy.
|
|
60
|
+
*/
|
|
61
|
+
export interface Rendering {
|
|
62
|
+
readonly marks: Readonly<Record<string, RenderingEntry<RunMark>>>;
|
|
63
|
+
readonly nodes: Readonly<Record<string, RenderingEntry<NodeBlock>>>;
|
|
64
|
+
}
|
|
65
|
+
export declare const rendering: (definition?: {
|
|
66
|
+
readonly marks?: Readonly<Record<string, RenderingEntry<RunMark>>>;
|
|
67
|
+
readonly nodes?: Readonly<Record<string, RenderingEntry<NodeBlock>>>;
|
|
68
|
+
}) => Rendering;
|
|
69
|
+
/** The behaviour without a renderer: shipped tags for shipped marks, names otherwise. */
|
|
70
|
+
export declare const noRendering: Rendering;
|
|
71
|
+
/**
|
|
72
|
+
* A registry built over another: an entry named here wins, and every name the base
|
|
73
|
+
* renders keeps its entry, so an application extends a vocabulary like the standard one
|
|
74
|
+
* without restating it.
|
|
75
|
+
*/
|
|
76
|
+
export declare const renderingOver: (base: Rendering, definition?: {
|
|
77
|
+
readonly marks?: Readonly<Record<string, RenderingEntry<RunMark>>>;
|
|
78
|
+
readonly nodes?: Readonly<Record<string, RenderingEntry<NodeBlock>>>;
|
|
79
|
+
}) => Rendering;
|
|
80
|
+
/** What a run's marks nest in, and which names nest nowhere. */
|
|
81
|
+
export interface RunRendering {
|
|
82
|
+
/** Elements to wrap the run's text in, innermost first. */
|
|
83
|
+
readonly nest: ReadonlyArray<ElementRendering>;
|
|
84
|
+
/** Names with neither an entry nor a shipped tag, sorted so output is stable. */
|
|
85
|
+
readonly unrendered: ReadonlyArray<string>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Resolves a run's marks to elements. Order is the vocabulary's, then names
|
|
89
|
+
* alphabetically, never the order marks happen to be stored in — the same set of
|
|
90
|
+
* marks must serialize the same way however it was assembled. An entry for a
|
|
91
|
+
* shipped name replaces that name's tag but keeps its place in the nesting.
|
|
92
|
+
*/
|
|
93
|
+
export declare const runRendering: (definition: Rendering, run: Text) => RunRendering;
|
|
94
|
+
/** The element a node kind renders as, or undefined when no entry applies. */
|
|
95
|
+
export declare const nodeRendering: (definition: Rendering, block: NodeBlock) => ElementRendering | undefined;
|
|
96
|
+
//# sourceMappingURL=rendering.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rendering.d.ts","names":[],"sources":["../src/rendering.ts"],"mappings":";;;;;;iBAQiB;WACN;;WAEA,YAAY,SAAS;;;;;;WAMrB;;;;;;WAMA,aAAa,cAAc;;;;;;;;;iBAUrB;WACN;WACA,YAAY,SAAS;WACrB;WACA;WACA;;;qBAIE;;qBAEA;;qBAEA;;qBAGA,sBAAmB,WACnB,cACV;;qBAWU,uBAAoB,MAAU,kBAAgB,OAAS;;;;;;YAgCxD,eAAe,SACvB,qBACE,OAAO,UAAU;;;;;;;;iBASN;WACN,OAAO,SAAS,eAAe,eAAe;WAC9C,OAAO,SAAS,eAAe,eAAe;;qBAiC5C,YAAS;WAET,QAAQ,SAAS,eAAe,eAAe;WAC/C,QAAQ,SAAS,eAAe,eAAe;MAEzD;;qBAIU,aAAa;;;;;;qBAOb,gBAAa,MAClB,WAAS;WAEJ,QAAQ,SAAS,eAAe,eAAe;WAC/C,QAAQ,SAAS,eAAe,eAAe;MAEzD;;iBAOc;;WAEN,MAAM,cAAc;;WAEpB,YAAY;;;;;;;;qBAWV,eAAY,YAAgB,WAAS,KAAO,SAAO;;qBA2BnD,gBAAa,YACZ,WAAS,OACd,cACN"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { markName } from "./marks.js";
|
|
2
|
+
// src/rendering.ts
|
|
3
|
+
/** The attribute that marks an element as an adornment, which every interpreter skips as content. */
|
|
4
|
+
const ADORNMENT_ATTRIBUTE = "data-adornment";
|
|
5
|
+
/** The attribute naming the prop an adornment's click flips. */
|
|
6
|
+
const TOGGLES_ATTRIBUTE = "data-toggles";
|
|
7
|
+
/** The attribute naming what an adornment's click asks the application for. */
|
|
8
|
+
const ACTION_ATTRIBUTE = "data-action";
|
|
9
|
+
/** An adornment's attributes as every interpreter writes them: its own, then the markers. */
|
|
10
|
+
const adornmentAttributes = (adornment) => [
|
|
11
|
+
...Object.entries(adornment.attributes).filter(([name]) => name !== "data-adornment" && name !== "data-toggles" && name !== "data-action"),
|
|
12
|
+
[ADORNMENT_ATTRIBUTE, ""],
|
|
13
|
+
...adornment.toggles === void 0 ? [] : [[TOGGLES_ATTRIBUTE, adornment.toggles]],
|
|
14
|
+
...adornment.action === void 0 ? [] : [[ACTION_ATTRIBUTE, adornment.action]]
|
|
15
|
+
];
|
|
16
|
+
/** Whether two renderings draw the same element: tag, attributes, holder and adornments. */
|
|
17
|
+
const sameElementRendering = (left, right) => left.tag === right.tag && left.inner === right.inner && sameRecord(left.attributes, right.attributes) && (left.adornments ?? []).length === (right.adornments ?? []).length && (left.adornments ?? []).every((adornment, at) => {
|
|
18
|
+
const other = right.adornments[at];
|
|
19
|
+
return adornment.tag === other.tag && adornment.text === other.text && adornment.toggles === other.toggles && adornment.action === other.action && sameRecord(adornment.attributes, other.attributes);
|
|
20
|
+
});
|
|
21
|
+
const sameRecord = (left, right) => {
|
|
22
|
+
const names = Object.keys(left);
|
|
23
|
+
return names.length === Object.keys(right).length && names.every((name) => Object.hasOwn(right, name) && left[name] === right[name]);
|
|
24
|
+
};
|
|
25
|
+
/** The nesting the shipped marks get, innermost first. */
|
|
26
|
+
const BUILT_IN_MARK_TAGS = [
|
|
27
|
+
["Bold", "strong"],
|
|
28
|
+
["Italic", "em"],
|
|
29
|
+
["Code", "code"]
|
|
30
|
+
];
|
|
31
|
+
/**
|
|
32
|
+
* Own-property lookup: a mark or node name is document content, so a plain or
|
|
33
|
+
* hand-built `Rendering` must not let `__proto__` resolve to a prototype.
|
|
34
|
+
*/
|
|
35
|
+
const entryFor = (entries, name) => Object.hasOwn(entries, name) ? entries[name] : void 0;
|
|
36
|
+
const resolve = (entry, value) => typeof entry === "function" ? entry(value) : entry;
|
|
37
|
+
/** A name's own entry map, with no prototype for a content-chosen name to reach. */
|
|
38
|
+
const entryMap = (entries) => {
|
|
39
|
+
const map = Object.create(null);
|
|
40
|
+
for (const [name, entry] of Object.entries(entries ?? {})) map[name] = entry;
|
|
41
|
+
return Object.freeze(map);
|
|
42
|
+
};
|
|
43
|
+
const rendering = (definition = {}) => Object.freeze({
|
|
44
|
+
marks: entryMap(definition.marks),
|
|
45
|
+
nodes: entryMap(definition.nodes)
|
|
46
|
+
});
|
|
47
|
+
/** The behaviour without a renderer: shipped tags for shipped marks, names otherwise. */
|
|
48
|
+
const noRendering = rendering();
|
|
49
|
+
/**
|
|
50
|
+
* A registry built over another: an entry named here wins, and every name the base
|
|
51
|
+
* renders keeps its entry, so an application extends a vocabulary like the standard one
|
|
52
|
+
* without restating it.
|
|
53
|
+
*/
|
|
54
|
+
const renderingOver = (base, definition = {}) => rendering({
|
|
55
|
+
marks: {
|
|
56
|
+
...base.marks,
|
|
57
|
+
...definition.marks
|
|
58
|
+
},
|
|
59
|
+
nodes: {
|
|
60
|
+
...base.nodes,
|
|
61
|
+
...definition.nodes
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
const byName = (left, right) => left < right ? -1 : left > right ? 1 : 0;
|
|
65
|
+
/**
|
|
66
|
+
* Resolves a run's marks to elements. Order is the vocabulary's, then names
|
|
67
|
+
* alphabetically, never the order marks happen to be stored in — the same set of
|
|
68
|
+
* marks must serialize the same way however it was assembled. An entry for a
|
|
69
|
+
* shipped name replaces that name's tag but keeps its place in the nesting.
|
|
70
|
+
*/
|
|
71
|
+
const runRendering = (definition, run) => {
|
|
72
|
+
const nest = [];
|
|
73
|
+
const unrendered = [];
|
|
74
|
+
for (const [name, tag] of BUILT_IN_MARK_TAGS) {
|
|
75
|
+
const mark = run.marks.find((candidate) => markName(candidate) === name);
|
|
76
|
+
if (mark === void 0) continue;
|
|
77
|
+
const entry = entryFor(definition.marks, name);
|
|
78
|
+
nest.push(entry === void 0 ? {
|
|
79
|
+
tag,
|
|
80
|
+
attributes: {}
|
|
81
|
+
} : resolve(entry, mark) ?? {
|
|
82
|
+
tag,
|
|
83
|
+
attributes: {}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const others = run.marks.filter((mark) => !BUILT_IN_MARK_TAGS.some(([name]) => name === markName(mark))).sort((left, right) => byName(markName(left), markName(right)));
|
|
87
|
+
for (const mark of others) {
|
|
88
|
+
const name = markName(mark);
|
|
89
|
+
const entry = entryFor(definition.marks, name);
|
|
90
|
+
const element = entry === void 0 ? void 0 : resolve(entry, mark);
|
|
91
|
+
if (element === void 0) unrendered.push(name);
|
|
92
|
+
else nest.push(element);
|
|
93
|
+
}
|
|
94
|
+
return {
|
|
95
|
+
nest,
|
|
96
|
+
unrendered
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
/** The element a node kind renders as, or undefined when no entry applies. */
|
|
100
|
+
const nodeRendering = (definition, block) => {
|
|
101
|
+
const entry = entryFor(definition.nodes, block.kind);
|
|
102
|
+
return entry === void 0 ? void 0 : resolve(entry, block);
|
|
103
|
+
};
|
|
104
|
+
export { ACTION_ATTRIBUTE, ADORNMENT_ATTRIBUTE, TOGGLES_ATTRIBUTE, adornmentAttributes, noRendering, nodeRendering, rendering, renderingOver, runRendering, sameElementRendering };
|
|
105
|
+
|
|
106
|
+
//# sourceMappingURL=rendering.js.map
|