foldkit-richtext 0.5.3 → 0.7.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 +7 -7
- 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/html.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { adornmentAttributes, noRendering, nodeRendering, runRendering } from "./rendering.js";
|
|
2
|
+
// src/html.ts
|
|
3
|
+
const escapeText = (text) => text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
4
|
+
const escapeAttribute = (value) => escapeText(value).replace(/"/g, """).replace(/'/g, "'");
|
|
5
|
+
const TAG = /^[A-Za-z][-A-Za-z0-9]*$/;
|
|
6
|
+
const ATTRIBUTE = /^[A-Za-z_:][-A-Za-z0-9_:.]*$/;
|
|
7
|
+
/**
|
|
8
|
+
* HTML void elements: they have no closing tag, so a rendering that names one — a
|
|
9
|
+
* standard `Image` is an `img`, a `ThematicBreak` an `hr` — writes one tag and no
|
|
10
|
+
* children rather than `<img></img>`.
|
|
11
|
+
*/
|
|
12
|
+
const VOID_ELEMENTS = /* @__PURE__ */ new Set([
|
|
13
|
+
"area",
|
|
14
|
+
"base",
|
|
15
|
+
"br",
|
|
16
|
+
"col",
|
|
17
|
+
"embed",
|
|
18
|
+
"hr",
|
|
19
|
+
"img",
|
|
20
|
+
"input",
|
|
21
|
+
"link",
|
|
22
|
+
"meta",
|
|
23
|
+
"source",
|
|
24
|
+
"track",
|
|
25
|
+
"wbr"
|
|
26
|
+
]);
|
|
27
|
+
/**
|
|
28
|
+
* Writes an element. An attribute *value* is content and is escaped; a tag or
|
|
29
|
+
* attribute *name* is refused rather than written when malformed, because a name
|
|
30
|
+
* cannot be escaped — one holding a quote, a space, or `>` would end the attribute
|
|
31
|
+
* and inject markup. A well-formed but unwise name (an event handler built from a
|
|
32
|
+
* mark's props, say) is the application's own decision to make.
|
|
33
|
+
*/
|
|
34
|
+
const renderElement = (element, inner) => {
|
|
35
|
+
if (!TAG.test(element.tag)) throw new Error(`RichText.toHtml: invalid tag "${element.tag}"`);
|
|
36
|
+
const attributes = Object.entries(element.attributes).map(([name, value]) => {
|
|
37
|
+
if (!ATTRIBUTE.test(name)) throw new Error(`RichText.toHtml: invalid attribute name "${name}"`);
|
|
38
|
+
return ` ${name}="${escapeAttribute(value)}"`;
|
|
39
|
+
}).join("");
|
|
40
|
+
if (VOID_ELEMENTS.has(element.tag.toLowerCase())) return `<${element.tag}${attributes}>`;
|
|
41
|
+
return `<${element.tag}${attributes}>${inner}</${element.tag}>`;
|
|
42
|
+
};
|
|
43
|
+
const renderRun = (run, renderer) => {
|
|
44
|
+
const { nest, unrendered } = runRendering(renderer, run);
|
|
45
|
+
let html = escapeText(run.text);
|
|
46
|
+
for (const element of nest) html = renderElement(element, html);
|
|
47
|
+
return unrendered.length === 0 ? html : `<span data-marks="${escapeAttribute(unrendered.join(" "))}">${html}</span>`;
|
|
48
|
+
};
|
|
49
|
+
const blockTag = (block) => {
|
|
50
|
+
if (block.type === "Heading") return `h${block.level}`;
|
|
51
|
+
return block.type === "Node" ? "div" : "p";
|
|
52
|
+
};
|
|
53
|
+
const renderBlock = (block, renderer) => {
|
|
54
|
+
if (block.type === "Unknown") return `<div data-unknown="${escapeAttribute(block.originalType)}"></div>`;
|
|
55
|
+
const runs = block.children.map((run) => renderRun(run, renderer)).join("");
|
|
56
|
+
const nested = block.type === "Node" && block.blocks !== void 0 ? toHtml(block.blocks, renderer) : "";
|
|
57
|
+
const element = (block.type === "Node" ? nodeRendering(renderer, block) : void 0) ?? {
|
|
58
|
+
tag: blockTag(block),
|
|
59
|
+
attributes: block.type === "Node" ? { "data-node": block.kind } : {}
|
|
60
|
+
};
|
|
61
|
+
const adornment = (element.adornments ?? []).map((piece) => renderElement({
|
|
62
|
+
tag: piece.tag,
|
|
63
|
+
attributes: Object.fromEntries(adornmentAttributes(piece))
|
|
64
|
+
}, escapeText(piece.text ?? ""))).join("");
|
|
65
|
+
return renderElement(element, element.inner === void 0 ? `${adornment}${runs}${nested}` : `${adornment}${runs}${renderElement({
|
|
66
|
+
tag: element.inner,
|
|
67
|
+
attributes: {}
|
|
68
|
+
}, nested)}`);
|
|
69
|
+
};
|
|
70
|
+
/** Serializes blocks as HTML, with text and attributes escaped. */
|
|
71
|
+
const toHtml = (blocks, renderer = noRendering) => blocks.map((block) => renderBlock(block, renderer)).join("");
|
|
72
|
+
/**
|
|
73
|
+
* Plain text of blocks, one line per text block; unknown blocks keep a
|
|
74
|
+
* placeholder and a container contributes its children's lines.
|
|
75
|
+
*/
|
|
76
|
+
const toText = (blocks) => {
|
|
77
|
+
const lines = [];
|
|
78
|
+
const walk = (current) => {
|
|
79
|
+
for (const block of current) {
|
|
80
|
+
if (block.type === "Unknown") {
|
|
81
|
+
lines.push(`[${block.originalType}]`);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (block.type === "Node" && block.blocks !== void 0) {
|
|
85
|
+
walk(block.blocks);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
lines.push(block.children.map((run) => run.text).join(""));
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
walk(blocks);
|
|
92
|
+
return lines.join("\n");
|
|
93
|
+
};
|
|
94
|
+
/** Convenience for a whole document, whose blocks are its children. */
|
|
95
|
+
const documentToHtml = (document, renderer) => toHtml(document.children, renderer);
|
|
96
|
+
const documentToText = (document) => toText(document.children);
|
|
97
|
+
export { documentToHtml, documentToText, toHtml, toText };
|
|
98
|
+
|
|
99
|
+
//# sourceMappingURL=html.js.map
|
package/dist/html.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"html.js","names":[],"sources":["../src/html.ts"],"sourcesContent":["import type { Block, Document, Text } from './document.js'\nimport {\n adornmentAttributes,\n noRendering,\n nodeRendering,\n runRendering,\n type ElementRendering,\n type Rendering,\n} from './rendering.js'\n\n/**\n * HTML is an interchange format, not the document model (§70). This serializer\n * exists so a copy can offer HTML to other applications and a preview can be\n * read by anything; nothing here is ever parsed back into authority. Unknown\n * marks survive as `data-marks` on a span and unknown blocks as a placeholder\n * carrying their original type, so a round trip through HTML cannot silently\n * invent formatting the document never had.\n *\n * A renderer (§121) decides which element a declared mark or node kind produces,\n * which is how a Link's `href` becomes a real attribute. Without one, this\n * fallback still carries mark *names*, and a mark's props travel in the slice\n * format, which is the lossless path.\n */\nexport type BlockList = ReadonlyArray<Block>\n\nconst escapeText = (text: string): string =>\n text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')\n\nconst escapeAttribute = (value: string): string =>\n escapeText(value).replace(/\"/g, '"').replace(/'/g, ''')\n\nconst TAG = /^[A-Za-z][-A-Za-z0-9]*$/\nconst ATTRIBUTE = /^[A-Za-z_:][-A-Za-z0-9_:.]*$/\n\n/**\n * HTML void elements: they have no closing tag, so a rendering that names one — a\n * standard `Image` is an `img`, a `ThematicBreak` an `hr` — writes one tag and no\n * children rather than `<img></img>`.\n */\nconst VOID_ELEMENTS = new Set([\n 'area',\n 'base',\n 'br',\n 'col',\n 'embed',\n 'hr',\n 'img',\n 'input',\n 'link',\n 'meta',\n 'source',\n 'track',\n 'wbr',\n])\n\n/**\n * Writes an element. An attribute *value* is content and is escaped; a tag or\n * attribute *name* is refused rather than written when malformed, because a name\n * cannot be escaped — one holding a quote, a space, or `>` would end the attribute\n * and inject markup. A well-formed but unwise name (an event handler built from a\n * mark's props, say) is the application's own decision to make.\n */\nconst renderElement = (element: ElementRendering, inner: string): string => {\n if (!TAG.test(element.tag)) throw new Error(`RichText.toHtml: invalid tag \"${element.tag}\"`)\n const attributes = Object.entries(element.attributes)\n .map(([name, value]) => {\n if (!ATTRIBUTE.test(name))\n throw new Error(`RichText.toHtml: invalid attribute name \"${name}\"`)\n return ` ${name}=\"${escapeAttribute(value)}\"`\n })\n .join('')\n if (VOID_ELEMENTS.has(element.tag.toLowerCase())) return `<${element.tag}${attributes}>`\n return `<${element.tag}${attributes}>${inner}</${element.tag}>`\n}\n\nconst renderRun = (run: Text, renderer: Rendering): string => {\n const { nest, unrendered } = runRendering(renderer, run)\n let html = escapeText(run.text)\n for (const element of nest) html = renderElement(element, html)\n return unrendered.length === 0\n ? html\n : `<span data-marks=\"${escapeAttribute(unrendered.join(' '))}\">${html}</span>`\n}\n\nconst blockTag = (block: Block): string => {\n if (block.type === 'Heading') return `h${block.level}`\n return block.type === 'Node' ? 'div' : 'p'\n}\n\nconst renderBlock = (block: Block, renderer: Rendering): string => {\n if (block.type === 'Unknown')\n return `<div data-unknown=\"${escapeAttribute(block.originalType)}\"></div>`\n const runs = block.children.map(run => renderRun(run, renderer)).join('')\n // A node that accepts nested blocks renders them inside it, so a list keeps\n // its items.\n const nested =\n block.type === 'Node' && block.blocks !== undefined ? toHtml(block.blocks, renderer) : ''\n // An application node carries its kind so a stylesheet can reach it; a renderer\n // entry replaces this default element entirely.\n const element = (block.type === 'Node' ? nodeRendering(renderer, block) : undefined) ?? {\n tag: blockTag(block),\n attributes: block.type === 'Node' ? { 'data-node': block.kind } : {},\n }\n const adornment = (element.adornments ?? [])\n .map(piece =>\n renderElement(\n { tag: piece.tag, attributes: Object.fromEntries(adornmentAttributes(piece)) },\n escapeText(piece.text ?? ''),\n ),\n )\n .join('')\n return renderElement(\n element,\n element.inner === undefined\n ? `${adornment}${runs}${nested}`\n : `${adornment}${runs}${renderElement({ tag: element.inner, attributes: {} }, nested)}`,\n )\n}\n\n/** Serializes blocks as HTML, with text and attributes escaped. */\nexport const toHtml = (blocks: BlockList, renderer: Rendering = noRendering): string =>\n blocks.map(block => renderBlock(block, renderer)).join('')\n\n/**\n * Plain text of blocks, one line per text block; unknown blocks keep a\n * placeholder and a container contributes its children's lines.\n */\nexport const toText = (blocks: BlockList): string => {\n const lines: Array<string> = []\n const walk = (current: BlockList): void => {\n for (const block of current) {\n if (block.type === 'Unknown') {\n lines.push(`[${block.originalType}]`)\n continue\n }\n if (block.type === 'Node' && block.blocks !== undefined) {\n walk(block.blocks)\n continue\n }\n lines.push(block.children.map(run => run.text).join(''))\n }\n }\n walk(blocks)\n return lines.join('\\n')\n}\n\n/** Convenience for a whole document, whose blocks are its children. */\nexport const documentToHtml = (document: Document, renderer?: Rendering): string =>\n toHtml(document.children, renderer)\nexport const documentToText = (document: Document): string => toText(document.children)\n"],"mappings":";;AAyBA,MAAM,cAAc,SAClB,KAAK,QAAQ,MAAM,OAAO,CAAC,CAAC,QAAQ,M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AK,MAAM,SAAS,KAAI,QAAO,IAAI,IAAI,CAAC,CAAC,KAAK,EAAE,CAAC;EACzD;CACF;CACA,KAAK,MAAM;CACX,OAAO,MAAM,KAAK,IAAI;AACxB;;AAGA,MAAa,kBAAkB,UAAoB,aACjD,OAAO,SAAS,UAAU,QAAQ;AACpC,MAAa,kBAAkB,aAA+B,OAAO,SAAS,QAAQ"}
|
package/dist/indent.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Block, EditorState, Position } from "./document.js";
|
|
2
|
+
import { TransactionResult } from "./transaction.js";
|
|
3
|
+
/** What one level of indentation is: two spaces. */
|
|
4
|
+
export declare const INDENT = " ";
|
|
5
|
+
/** A position's offset into its block's whole text, or undefined when it is not the block's. */
|
|
6
|
+
export declare const offsetIn: (block: Block, position: Position) => number | undefined;
|
|
7
|
+
/** The leading spaces and tabs of the line `offset` is on, up to `offset`. */
|
|
8
|
+
export declare const indentationAt: (text: string, offset: number) => string;
|
|
9
|
+
/**
|
|
10
|
+
* Tab: at a caret, a level of indentation where the caret is; over a range, a level at the start
|
|
11
|
+
* of every line it covers, so a block of code moves right together.
|
|
12
|
+
*/
|
|
13
|
+
export declare const indent: (state: EditorState, block: Block) => TransactionResult;
|
|
14
|
+
/** Shift+Tab: up to a level of indentation off the start of every line the selection is on. */
|
|
15
|
+
export declare const outdent: (state: EditorState, block: Block) => TransactionResult;
|
|
16
|
+
//# sourceMappingURL=indent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"indent.d.ts","names":[],"sources":["../src/indent.ts"],"mappings":";;;qBASa;;qBAKA,WAAQ,OAAW,OAAK,UAAY;;qBAYpC,gBAAa,cAAgB;;;;;qBA+B7B,SAAM,OAAW,aAAW,OAAS,UAAQ;;qBA6B7C,UAAO,OAAW,aAAW,OAAS,UAAQ"}
|
package/dist/indent.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { positionInBlock } from "./document.js";
|
|
2
|
+
import { Edit, apply } from "./transaction.js";
|
|
3
|
+
// src/indent.ts
|
|
4
|
+
/**
|
|
5
|
+
* Indentation in a kind whose lines are indented, as code's are (`node(…, { indents })`):
|
|
6
|
+
* Tab indents, Shift+Tab outdents, and Enter starts the new line as indented as the one it
|
|
7
|
+
* broke. A line is the text between two breaks of one block.
|
|
8
|
+
*/
|
|
9
|
+
/** What one level of indentation is: two spaces. */
|
|
10
|
+
const INDENT = " ";
|
|
11
|
+
const textOf = (block) => block.children.map((run) => run.text).join("");
|
|
12
|
+
/** A position's offset into its block's whole text, or undefined when it is not the block's. */
|
|
13
|
+
const offsetIn = (block, position) => {
|
|
14
|
+
let consumed = 0;
|
|
15
|
+
for (const run of block.children) {
|
|
16
|
+
if (run.id === position.node) return consumed + position.offset;
|
|
17
|
+
consumed += run.text.length;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
const lineStart = (text, offset) => text.lastIndexOf("\n", offset - 1) + 1;
|
|
21
|
+
/** The leading spaces and tabs of the line `offset` is on, up to `offset`. */
|
|
22
|
+
const indentationAt = (text, offset) => /^[ \t]*/.exec(text.slice(lineStart(text, offset), offset))?.[0] ?? "";
|
|
23
|
+
/**
|
|
24
|
+
* The starts of the lines a range covers, last first, so editing each leaves the offsets of the
|
|
25
|
+
* ones before it as they were. A line the range only reaches the start of is not covered.
|
|
26
|
+
*/
|
|
27
|
+
const linesCovered = (text, from, to) => {
|
|
28
|
+
const starts = [lineStart(text, from)];
|
|
29
|
+
for (let at = text.indexOf("\n", from); at !== -1 && at + 1 < to; at = text.indexOf("\n", at + 1)) starts.push(at + 1);
|
|
30
|
+
return starts.reverse();
|
|
31
|
+
};
|
|
32
|
+
/** The selection's ends as offsets into the block's text, in order, or undefined. */
|
|
33
|
+
const span = (state, block) => {
|
|
34
|
+
const selection = state.selection;
|
|
35
|
+
if (selection?.type !== "Range") return void 0;
|
|
36
|
+
const anchor = offsetIn(block, selection.anchor);
|
|
37
|
+
const focus = offsetIn(block, selection.focus);
|
|
38
|
+
if (anchor === void 0 || focus === void 0) return void 0;
|
|
39
|
+
return {
|
|
40
|
+
from: Math.min(anchor, focus),
|
|
41
|
+
to: Math.max(anchor, focus)
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Tab: at a caret, a level of indentation where the caret is; over a range, a level at the start
|
|
46
|
+
* of every line it covers, so a block of code moves right together.
|
|
47
|
+
*/
|
|
48
|
+
const indent = (state, block) => {
|
|
49
|
+
const ends = span(state, block);
|
|
50
|
+
if (ends === void 0) return {
|
|
51
|
+
ok: false,
|
|
52
|
+
error: "InvalidSelection"
|
|
53
|
+
};
|
|
54
|
+
const text = textOf(block);
|
|
55
|
+
const at = (offset) => positionInBlock(block, offset);
|
|
56
|
+
if (ends.from === ends.to) {
|
|
57
|
+
const caret = at(ends.from);
|
|
58
|
+
if (caret === void 0) return {
|
|
59
|
+
ok: false,
|
|
60
|
+
error: "InvalidSelection"
|
|
61
|
+
};
|
|
62
|
+
const after = {
|
|
63
|
+
node: caret.node,
|
|
64
|
+
offset: caret.offset + 2,
|
|
65
|
+
affinity: "after"
|
|
66
|
+
};
|
|
67
|
+
return apply(state, [Edit.insertText(caret, " "), Edit.setSelection({
|
|
68
|
+
type: "Range",
|
|
69
|
+
anchor: after,
|
|
70
|
+
focus: after
|
|
71
|
+
})]);
|
|
72
|
+
}
|
|
73
|
+
const operations = [];
|
|
74
|
+
for (const start of linesCovered(text, ends.from, ends.to)) {
|
|
75
|
+
const position = at(start);
|
|
76
|
+
if (position !== void 0) operations.push(Edit.insertText(position, " "));
|
|
77
|
+
}
|
|
78
|
+
return apply(state, operations);
|
|
79
|
+
};
|
|
80
|
+
/** Shift+Tab: up to a level of indentation off the start of every line the selection is on. */
|
|
81
|
+
const outdent = (state, block) => {
|
|
82
|
+
const ends = span(state, block);
|
|
83
|
+
if (ends === void 0) return {
|
|
84
|
+
ok: false,
|
|
85
|
+
error: "InvalidSelection"
|
|
86
|
+
};
|
|
87
|
+
const text = textOf(block);
|
|
88
|
+
const starts = ends.from === ends.to ? [lineStart(text, ends.from)] : linesCovered(text, ends.from, ends.to);
|
|
89
|
+
const operations = [];
|
|
90
|
+
for (const start of starts) {
|
|
91
|
+
const leading = /^(?: {1,2}|\t)/.exec(text.slice(start))?.[0].length ?? 0;
|
|
92
|
+
const from = positionInBlock(block, start);
|
|
93
|
+
if (leading === 0 || from === void 0 || from.offset + leading > runLength(block, from)) continue;
|
|
94
|
+
operations.push(Edit.deleteText(from.node, from.offset, from.offset + leading));
|
|
95
|
+
}
|
|
96
|
+
return apply(state, operations);
|
|
97
|
+
};
|
|
98
|
+
const runLength = (block, position) => block.children.find((run) => run.id === position.node)?.text.length ?? 0;
|
|
99
|
+
export { INDENT, indent, indentationAt, offsetIn, outdent };
|
|
100
|
+
|
|
101
|
+
//# sourceMappingURL=indent.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"indent.js","names":[],"sources":["../src/indent.ts"],"sourcesContent":["/**\n * Indentation in a kind whose lines are indented, as code's are (`node(…, { indents })`):\n * Tab indents, Shift+Tab outdents, and Enter starts the new line as indented as the one it\n * broke. A line is the text between two breaks of one block.\n */\nimport { positionInBlock, type Block, type EditorState, type Position } from './document.js'\nimport { Edit, apply, type Operation, type TransactionResult } from './transaction.js'\n\n/** What one level of indentation is: two spaces. */\nexport const INDENT = ' '\n\nconst textOf = (block: Block): string => block.children.map(run => run.text).join('')\n\n/** A position's offset into its block's whole text, or undefined when it is not the block's. */\nexport const offsetIn = (block: Block, position: Position): number | undefined => {\n let consumed = 0\n for (const run of block.children) {\n if (run.id === position.node) return consumed + position.offset\n consumed += run.text.length\n }\n return undefined\n}\n\nconst lineStart = (text: string, offset: number): number => text.lastIndexOf('\\n', offset - 1) + 1\n\n/** The leading spaces and tabs of the line `offset` is on, up to `offset`. */\nexport const indentationAt = (text: string, offset: number): string =>\n /^[ \\t]*/.exec(text.slice(lineStart(text, offset), offset))?.[0] ?? ''\n\n/**\n * The starts of the lines a range covers, last first, so editing each leaves the offsets of the\n * ones before it as they were. A line the range only reaches the start of is not covered.\n */\nconst linesCovered = (text: string, from: number, to: number): ReadonlyArray<number> => {\n const starts = [lineStart(text, from)]\n for (let at = text.indexOf('\\n', from); at !== -1 && at + 1 < to; at = text.indexOf('\\n', at + 1))\n starts.push(at + 1)\n return starts.reverse()\n}\n\n/** The selection's ends as offsets into the block's text, in order, or undefined. */\nconst span = (\n state: EditorState,\n block: Block,\n): { readonly from: number; readonly to: number } | undefined => {\n const selection = state.selection\n if (selection?.type !== 'Range') return undefined\n const anchor = offsetIn(block, selection.anchor)\n const focus = offsetIn(block, selection.focus)\n if (anchor === undefined || focus === undefined) return undefined\n return { from: Math.min(anchor, focus), to: Math.max(anchor, focus) }\n}\n\n/**\n * Tab: at a caret, a level of indentation where the caret is; over a range, a level at the start\n * of every line it covers, so a block of code moves right together.\n */\nexport const indent = (state: EditorState, block: Block): TransactionResult => {\n const ends = span(state, block)\n if (ends === undefined) return { ok: false, error: 'InvalidSelection' }\n const text = textOf(block)\n const at = (offset: number) => positionInBlock(block, offset)\n if (ends.from === ends.to) {\n const caret = at(ends.from)\n if (caret === undefined) return { ok: false, error: 'InvalidSelection' }\n // The caret goes after the indentation whichever side of the offset it leaned: a caret the\n // browser reports at a line's start leans `before`, and would stay in front of it.\n const after = {\n node: caret.node,\n offset: caret.offset + INDENT.length,\n affinity: 'after' as const,\n }\n return apply(state, [\n Edit.insertText(caret, INDENT),\n Edit.setSelection({ type: 'Range', anchor: after, focus: after }),\n ])\n }\n const operations: Array<Operation> = []\n for (const start of linesCovered(text, ends.from, ends.to)) {\n const position = at(start)\n if (position !== undefined) operations.push(Edit.insertText(position, INDENT))\n }\n return apply(state, operations)\n}\n\n/** Shift+Tab: up to a level of indentation off the start of every line the selection is on. */\nexport const outdent = (state: EditorState, block: Block): TransactionResult => {\n const ends = span(state, block)\n if (ends === undefined) return { ok: false, error: 'InvalidSelection' }\n const text = textOf(block)\n const starts =\n ends.from === ends.to ? [lineStart(text, ends.from)] : linesCovered(text, ends.from, ends.to)\n const operations: Array<Operation> = []\n for (const start of starts) {\n const leading = /^(?: {1,2}|\\t)/.exec(text.slice(start))?.[0].length ?? 0\n const from = positionInBlock(block, start)\n // An indentation split across runs is left alone: code runs carry no marks, so it is one.\n if (leading === 0 || from === undefined || from.offset + leading > runLength(block, from))\n continue\n operations.push(Edit.deleteText(from.node, from.offset, from.offset + leading))\n }\n return apply(state, operations)\n}\n\nconst runLength = (block: Block, position: Position): number =>\n block.children.find(run => run.id === position.node)?.text.length ?? 0\n"],"mappings":";;;;;;;;;AASA,MAAa,SAAS;AAEtB,MAAM,UAAU,UAAyB,MAAM,SAAS,KAAI,QAAO,IAAI,IAAI,CAAC,CAAC,KAAK,EAAE;;AAGpF,MAAa,YAAY,OAAc,aAA2C;CAChF,IAAI,WAAW;CACf,KAAK,MAAM,OAAO,MAAM,UAAU;EAChC,IAAI,IAAI,OAAO,SAAS,MAAM,OAAO,WAAW,SAAS;EACzD,YAAY,IAAI,KAAK;CACvB;AAEF;AAEA,MAAM,aAAa,MAAc,WAA2B,KAAK,YAAY,MAAM,SAAS,CAAC,IAAI;;AAGjG,MAAa,iBAAiB,MAAc,WAC1C,UAAU,KAAK,KAAK,MAAM,UAAU,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC,GAAG,MAAM;;;;;AAMtE,MAAM,gBAAgB,MAAc,MAAc,OAAsC;CACtF,MAAM,SAAS,CAAC,UAAU,MAAM,IAAI,CAAC;CACrC,KAAK,IAAI,KAAK,KAAK,QAAQ,MAAM,IAAI,GAAG,OAAO,MAAM,KAAK,IAAI,IAAI,KAAK,KAAK,QAAQ,MAAM,KAAK,CAAC,GAC9F,OAAO,KAAK,KAAK,CAAC;CACpB,OAAO,OAAO,QAAQ;AACxB;;AAGA,MAAM,QACJ,OACA,UAC+D;CAC/D,MAAM,YAAY,MAAM;CACxB,IAAI,WAAW,SAAS,SAAS,OAAO,KAAA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AAK;CACzB,MAAM,SACJ,KAAK,SAAS,KAAK,KAAK,CAAC,UAAU,MAAM,KAAK,IAAI,CAAC,IAAI,aAAa,MAAM,KAAK,MAAM,KAAK,EAAE;CAC9F,MAAM,aAA+B,CAAC;CACtC,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,UAAU,iBAAiB,KAAK,KAAK,MAAM,KAAK,CAAC,CAAC,GAAG,EAAE,CAAC,UAAU;EACxE,MAAM,OAAO,gBAAgB,OAAO,KAAK;EAEzC,IAAI,YAAY,KAAK,SAAS,KAAA,KAAa,KAAK,SAAS,UAAU,UAAU,OAAO,IAAI,GACtF;EACF,WAAW,KAAK,KAAK,WAAW,KAAK,MAAM,KAAK,QAAQ,KAAK,SAAS,OAAO,CAAC;CAChF;CACA,OAAO,MAAM,OAAO,UAAU;AAChC;AAEA,MAAM,aAAa,OAAc,aAC/B,MAAM,SAAS,MAAK,QAAO,IAAI,OAAO,SAAS,IAAI,CAAC,EAAE,KAAK,UAAU"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BlockList, documentToHtml, documentToText, toHtml, toText } from "./html.js";
|
|
2
|
+
import { Block, BlockPath, ContentMode, DefaultDocumentLimits, Document, DocumentLimits, EditorState, Heading, LocatedRun, Mark, MarkValue, Node, NodeBlock, NodeId, NodeReference, Paragraph, Position, RunMark, RunPlace, Selection, Text, UnknownBlock, UnknownMark, UnknownNode, blockAtPath, blockContent, count, decodeDocument, eachBlock, findUnknownMarks, findUnknownNodes, inspect, isBlank, locateBlock, locateRun, markName, positionInBlock, rangeStart, selectionIsValid, siblingsOf, textBefore, textContent, textRangeBefore } from "./document.js";
|
|
3
|
+
import { Slice, deserializeSlice, emptySlice, plainTextOf, serializeSlice, sliceFromText, sliceOf, withFreshIds } from "./clipboard.js";
|
|
4
|
+
import { Bold, Code, Italic, MarkDef, MarkExpansion, MarkRegistry, mark, markProps, markRegistry, resolveInsertion, sameMark, sameMarkSet, shippedMarks, shippedRegistry } from "./marks.js";
|
|
5
|
+
import { BlockConstraint, ChildrenDeclaration, Diagnostic, Kit, MarksPolicy, NodeDefinition, NodeDefinitionOf, NodeRegistry, atom, block, blockKind, blocksOf, inspectKit, kit, node, nodeRegistry, validate } from "./kit.js";
|
|
6
|
+
import { INDENT } from "./indent.js";
|
|
7
|
+
import { SEARCH_CURRENT_DECORATION, SEARCH_DECORATION, SearchMatch, SearchOptions, findMatches, searchDecorations } from "./search.js";
|
|
8
|
+
import { Footnote, FootnoteDefinition, FootnoteRef, footnoteAt, footnotesOf, nextFootnoteLabel, referenceLabel } from "./footnote.js";
|
|
9
|
+
import { TableAt, tableAt } from "./table.js";
|
|
10
|
+
import { ChangeSet, CollapseStep, Edit, MAX_NORMALIZATION_PASSES, Operation, PositionStep, RelocateStep, RetypeTarget, SplitStep, TextBlock, TextTarget, Transaction, TransactionResult, apply, mapPosition } from "./transaction.js";
|
|
11
|
+
import { ACTION_ATTRIBUTE, ADORNMENT_ATTRIBUTE, Adornment, ElementRendering, Rendering, RenderingEntry, RunRendering, TOGGLES_ATTRIBUTE, adornmentAttributes, noRendering, nodeRendering, rendering, renderingOver, runRendering, sameElementRendering } from "./rendering.js";
|
|
12
|
+
import { Action, Beside, Command, CommandIds, Container, MarkExtent, RunOptions, blocksAt, coversText, isAtom, markExtent, marksInRange, moveTargets, run, runAction, takesTab, textBlockAt, unionChangeSet } from "./command.js";
|
|
13
|
+
import { InputContext, InputMatch, InputRule, applyInputRules, resolveInputRules } from "./input.js";
|
|
14
|
+
import { CommitOptions, History, Restore, canRedo, canUndo, commit, emptyHistory, groupFor, inspectHistory, redo, undo } from "./history.js";
|
|
15
|
+
import { Decoration, DecorationSet, DecorationSpan, RunPiece, decorationsIn, runPieces } from "./decoration.js";
|
|
16
|
+
import { CodeToken, CodeTokenizer, codeDecorations } from "./code.js";
|
|
17
|
+
import { safeUrl } from "./url.js";
|
|
18
|
+
import { Link, LinkAt, Strikethrough, imageBox, linkAt, standardMarks, standardNodes, standardRendering } from "./standard.js";
|
|
19
|
+
import { Migration, MigrationApplied, MigrationReport, migrate, migration, nodeProps, promoteUnknown } from "./migration.js";
|
|
20
|
+
import { Transform, TransformContext, TransformReport, defaultTransforms, mergeAdjacentRuns, unchanged } from "./transform.js";
|
|
21
|
+
import * as Replicated from "./replicated.js";
|
|
22
|
+
import { OutlineEntry, outlineOf, sectionAt } from "./outline.js";
|
|
23
|
+
import { Attachment, Audio, MediaFile, Video, fileSize, mediaBlock, mediaNodes, mediaRendering } from "./media.js";
|
|
24
|
+
export { ACTION_ATTRIBUTE, ADORNMENT_ATTRIBUTE, type Action, type Adornment, Attachment, Audio, Beside, Block, type BlockConstraint, type BlockList, type BlockPath, Bold, type ChangeSet, type ChildrenDeclaration, Code, type CodeToken, type CodeTokenizer, type CollapseStep, type Command, type CommandIds, type CommitOptions, Container, type ContentMode, type Decoration, type DecorationSet, type DecorationSpan, DefaultDocumentLimits, type Diagnostic, Document, DocumentLimits, Edit, EditorState, type ElementRendering, type Footnote, FootnoteDefinition, FootnoteRef, Heading, History, INDENT, type InputContext, type InputMatch, type InputRule, Italic, type Kit, Link, type LinkAt, type LocatedRun, MAX_NORMALIZATION_PASSES, Mark, type MarkDef, type MarkExpansion, type MarkExtent, type MarkRegistry, MarkValue, type MarksPolicy, type MediaFile, type Migration, type MigrationApplied, type MigrationReport, Node, NodeBlock, type NodeDefinition, type NodeDefinitionOf, NodeId, type NodeReference, type NodeRegistry, Operation, type OutlineEntry, Paragraph, Position, type PositionStep, type RelocateStep, type Rendering, type RenderingEntry, Replicated, type Restore, RetypeTarget, RunMark, type RunOptions, type RunPiece, type RunPlace, type RunRendering, SEARCH_CURRENT_DECORATION, SEARCH_DECORATION, type SearchMatch, type SearchOptions, Selection, Slice, type SplitStep, Strikethrough, TOGGLES_ATTRIBUTE, type TableAt, Text, TextBlock, type TextTarget, Transaction, type TransactionResult, type Transform, type TransformContext, type TransformReport, UnknownBlock, type UnknownMark, type UnknownNode, Video, adornmentAttributes, apply, applyInputRules, atom, block, blockAtPath, blockContent, blockKind, blocksAt, blocksOf, canRedo, canUndo, codeDecorations, commit, count, coversText, decodeDocument, decorationsIn, defaultTransforms, deserializeSlice, documentToHtml, documentToText, eachBlock, emptyHistory, emptySlice, fileSize, findMatches, findUnknownMarks, findUnknownNodes, footnoteAt, footnotesOf, groupFor, imageBox, inspect, inspectHistory, inspectKit, isAtom, isBlank, kit, linkAt, locateBlock, locateRun, mapPosition, mark, markExtent, markName, markProps, markRegistry, marksInRange, mediaBlock, mediaNodes, mediaRendering, mergeAdjacentRuns, migrate, migration, moveTargets, nextFootnoteLabel, noRendering, node, nodeProps, nodeRegistry, nodeRendering, outlineOf, plainTextOf, positionInBlock, promoteUnknown, rangeStart, redo, referenceLabel, rendering, renderingOver, resolveInputRules, resolveInsertion, run, runAction, runPieces, runRendering, safeUrl, sameElementRendering, sameMark, sameMarkSet, searchDecorations, sectionAt, selectionIsValid, serializeSlice, shippedMarks, shippedRegistry, siblingsOf, sliceFromText, sliceOf, standardMarks, standardNodes, standardRendering, tableAt, takesTab, textBefore, textBlockAt, textContent, textRangeBefore, toHtml, toText, unchanged, undo, unionChangeSet, validate, withFreshIds };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { documentToHtml, documentToText, toHtml, toText } from "./html.js";
|
|
2
|
+
import { Block, DefaultDocumentLimits, Document, DocumentLimits, EditorState, Heading, Mark, MarkValue, Node, NodeBlock, NodeId, Paragraph, Position, RunMark, Selection, Text, UnknownBlock, blockAtPath, blockContent, count, decodeDocument, eachBlock, findUnknownMarks, findUnknownNodes, inspect, isBlank, locateBlock, locateRun, markName, positionInBlock, rangeStart, selectionIsValid, siblingsOf, textBefore, textContent, textRangeBefore } from "./document.js";
|
|
3
|
+
import { Slice, deserializeSlice, emptySlice, plainTextOf, serializeSlice, sliceFromText, sliceOf, withFreshIds } from "./clipboard.js";
|
|
4
|
+
import { Bold, Code, Italic, mark, markProps, markRegistry, resolveInsertion, sameMark, sameMarkSet, shippedMarks, shippedRegistry } from "./marks.js";
|
|
5
|
+
import { atom, block, blockKind, blocksOf, inspectKit, kit, node, nodeRegistry, validate } from "./kit.js";
|
|
6
|
+
import { INDENT } from "./indent.js";
|
|
7
|
+
import { SEARCH_CURRENT_DECORATION, SEARCH_DECORATION, findMatches, searchDecorations } from "./search.js";
|
|
8
|
+
import { FootnoteDefinition, FootnoteRef, footnoteAt, footnotesOf, nextFootnoteLabel, referenceLabel } from "./footnote.js";
|
|
9
|
+
import { tableAt } from "./table.js";
|
|
10
|
+
import { Edit, MAX_NORMALIZATION_PASSES, Operation, RetypeTarget, TextBlock, Transaction, apply, mapPosition } from "./transaction.js";
|
|
11
|
+
import { ACTION_ATTRIBUTE, ADORNMENT_ATTRIBUTE, TOGGLES_ATTRIBUTE, adornmentAttributes, noRendering, nodeRendering, rendering, renderingOver, runRendering, sameElementRendering } from "./rendering.js";
|
|
12
|
+
import { Beside, Container, blocksAt, coversText, isAtom, markExtent, marksInRange, moveTargets, run, runAction, takesTab, textBlockAt, unionChangeSet } from "./command.js";
|
|
13
|
+
import { applyInputRules, resolveInputRules } from "./input.js";
|
|
14
|
+
import { History, canRedo, canUndo, commit, emptyHistory, groupFor, inspectHistory, redo, undo } from "./history.js";
|
|
15
|
+
import { decorationsIn, runPieces } from "./decoration.js";
|
|
16
|
+
import { codeDecorations } from "./code.js";
|
|
17
|
+
import { safeUrl } from "./url.js";
|
|
18
|
+
import { Link, Strikethrough, imageBox, linkAt, standardMarks, standardNodes, standardRendering } from "./standard.js";
|
|
19
|
+
import { migrate, migration, nodeProps, promoteUnknown } from "./migration.js";
|
|
20
|
+
import { defaultTransforms, mergeAdjacentRuns, unchanged } from "./transform.js";
|
|
21
|
+
import * as Replicated from "./replicated.js";
|
|
22
|
+
import { outlineOf, sectionAt } from "./outline.js";
|
|
23
|
+
import { Attachment, Audio, Video, fileSize, mediaBlock, mediaNodes, mediaRendering } from "./media.js";
|
|
24
|
+
export { ACTION_ATTRIBUTE, ADORNMENT_ATTRIBUTE, Attachment, Audio, Beside, Block, Bold, Code, Container, DefaultDocumentLimits, Document, DocumentLimits, Edit, EditorState, FootnoteDefinition, FootnoteRef, Heading, History, INDENT, Italic, Link, MAX_NORMALIZATION_PASSES, Mark, MarkValue, Node, NodeBlock, NodeId, Operation, Paragraph, Position, Replicated, RetypeTarget, RunMark, SEARCH_CURRENT_DECORATION, SEARCH_DECORATION, Selection, Slice, Strikethrough, TOGGLES_ATTRIBUTE, Text, TextBlock, Transaction, UnknownBlock, Video, adornmentAttributes, apply, applyInputRules, atom, block, blockAtPath, blockContent, blockKind, blocksAt, blocksOf, canRedo, canUndo, codeDecorations, commit, count, coversText, decodeDocument, decorationsIn, defaultTransforms, deserializeSlice, documentToHtml, documentToText, eachBlock, emptyHistory, emptySlice, fileSize, findMatches, findUnknownMarks, findUnknownNodes, footnoteAt, footnotesOf, groupFor, imageBox, inspect, inspectHistory, inspectKit, isAtom, isBlank, kit, linkAt, locateBlock, locateRun, mapPosition, mark, markExtent, markName, markProps, markRegistry, marksInRange, mediaBlock, mediaNodes, mediaRendering, mergeAdjacentRuns, migrate, migration, moveTargets, nextFootnoteLabel, noRendering, node, nodeProps, nodeRegistry, nodeRendering, outlineOf, plainTextOf, positionInBlock, promoteUnknown, rangeStart, redo, referenceLabel, rendering, renderingOver, resolveInputRules, resolveInsertion, run, runAction, runPieces, runRendering, safeUrl, sameElementRendering, sameMark, sameMarkSet, searchDecorations, sectionAt, selectionIsValid, serializeSlice, shippedMarks, shippedRegistry, siblingsOf, sliceFromText, sliceOf, standardMarks, standardNodes, standardRendering, tableAt, takesTab, textBefore, textBlockAt, textContent, textRangeBefore, toHtml, toText, unchanged, undo, unionChangeSet, validate, withFreshIds };
|
package/dist/input.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Action, Command } from "./command.js";
|
|
2
|
+
/** What a rule makes of the text before the caret, and what to do about it. */
|
|
3
|
+
export interface InputMatch {
|
|
4
|
+
/** How many characters of that text the rule consumed; the caller deletes them. */
|
|
5
|
+
readonly remove: number;
|
|
6
|
+
readonly commands: Action;
|
|
7
|
+
/**
|
|
8
|
+
* The marks the text typed next carries, when the rule decides it: `**bold**` ends its bold, so
|
|
9
|
+
* typing on is plain, though the bold run would otherwise continue. Absent, the editor's own
|
|
10
|
+
* rule applies.
|
|
11
|
+
*/
|
|
12
|
+
readonly storedMarks?: ReadonlyArray<string> | undefined;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A rule over the text before the caret. `match` is a pure read: it sees the text and the
|
|
16
|
+
* kinds of the node blocks around the caret's block, innermost first (`['ListItem',
|
|
17
|
+
* 'List']` inside a bulleted item), and nothing else, so a rule cannot depend on a
|
|
18
|
+
* selection, a clock, or the Model.
|
|
19
|
+
*/
|
|
20
|
+
export interface InputRule {
|
|
21
|
+
readonly name: string;
|
|
22
|
+
readonly match: (textBefore: string, within: ReadonlyArray<string>) => InputMatch | undefined;
|
|
23
|
+
}
|
|
24
|
+
/** One edit to read a rule against, named so two strings cannot be handed over swapped. */
|
|
25
|
+
export interface InputContext {
|
|
26
|
+
/** The caret's block text before the caret, before this insertion. */
|
|
27
|
+
readonly textBefore: string;
|
|
28
|
+
/** What was just typed. */
|
|
29
|
+
readonly text: string;
|
|
30
|
+
/** The insertion itself, which a matching rule then acts on. */
|
|
31
|
+
readonly insertion: Command;
|
|
32
|
+
/** The kinds of the node blocks around the caret's block, innermost first. Default none. */
|
|
33
|
+
readonly within?: ReadonlyArray<string>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The commands that insert the text and then let the first matching rule act on it: the
|
|
37
|
+
* insertion, one backward delete per character the rule consumed, and the rule's commands.
|
|
38
|
+
* Consumption is by deleting backwards rather than by a range, because a range would have
|
|
39
|
+
* to be computed against a state the insertion has not produced yet — the deletes resolve
|
|
40
|
+
* as each one runs. A rule that does not match yields the insertion alone.
|
|
41
|
+
*
|
|
42
|
+
* A rule that claims to consume more than it was shown throws: those deletes would run past
|
|
43
|
+
* the block's start and join it to the one before, which no rule means.
|
|
44
|
+
*/
|
|
45
|
+
export declare const applyInputRules: (rules: ReadonlyArray<InputRule>, input: InputContext) => Action;
|
|
46
|
+
/**
|
|
47
|
+
* `applyInputRules`, with the marks the matching rule says the next typed text carries, which an
|
|
48
|
+
* editor keeps as its stored marks.
|
|
49
|
+
*/
|
|
50
|
+
export declare const resolveInputRules: (rules: ReadonlyArray<InputRule>, input: InputContext) => {
|
|
51
|
+
readonly action: Action;
|
|
52
|
+
readonly storedMarks?: ReadonlyArray<string> | undefined;
|
|
53
|
+
};
|
|
54
|
+
//# sourceMappingURL=input.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input.d.ts","names":[],"sources":["../src/input.ts"],"mappings":";;iBAaiB;;WAEN;WACA,UAAU;;;;;;WAMV,cAAc;;;;;;;;iBASR;WACN;WACA,QAAQ,oBAAoB,QAAQ,0BAA0B;;;iBAIxD;;WAEN;;WAEA;;WAEA,WAAW;;WAEX,SAAS;;;;;;;;;;;;qBAaP,kBAAe,OAAW,cAAc,YAAU,OAAS,iBAAe;;;;;qBAO1E,oBAAiB,OACrB,cAAc,YAAU,OACxB;WACK,QAAQ;WAAiB,cAAc"}
|
package/dist/input.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/input.ts
|
|
2
|
+
/**
|
|
3
|
+
* The commands that insert the text and then let the first matching rule act on it: the
|
|
4
|
+
* insertion, one backward delete per character the rule consumed, and the rule's commands.
|
|
5
|
+
* Consumption is by deleting backwards rather than by a range, because a range would have
|
|
6
|
+
* to be computed against a state the insertion has not produced yet — the deletes resolve
|
|
7
|
+
* as each one runs. A rule that does not match yields the insertion alone.
|
|
8
|
+
*
|
|
9
|
+
* A rule that claims to consume more than it was shown throws: those deletes would run past
|
|
10
|
+
* the block's start and join it to the one before, which no rule means.
|
|
11
|
+
*/
|
|
12
|
+
const applyInputRules = (rules, input) => resolveInputRules(rules, input).action;
|
|
13
|
+
/**
|
|
14
|
+
* `applyInputRules`, with the marks the matching rule says the next typed text carries, which an
|
|
15
|
+
* editor keeps as its stored marks.
|
|
16
|
+
*/
|
|
17
|
+
const resolveInputRules = (rules, input) => {
|
|
18
|
+
const typed = `${input.textBefore}${input.text}`;
|
|
19
|
+
for (const rule of rules) {
|
|
20
|
+
const matched = rule.match(typed, input.within ?? []);
|
|
21
|
+
if (matched === void 0) continue;
|
|
22
|
+
if (!Number.isInteger(matched.remove) || matched.remove < 0 || matched.remove > typed.length) throw new RangeError(`Input rule "${rule.name}" removes ${matched.remove} characters of the ${typed.length} it was shown`);
|
|
23
|
+
const consumed = Array.from({ length: matched.remove }, () => ({ type: "DeleteBackward" }));
|
|
24
|
+
return {
|
|
25
|
+
action: [
|
|
26
|
+
input.insertion,
|
|
27
|
+
...consumed,
|
|
28
|
+
...matched.commands
|
|
29
|
+
],
|
|
30
|
+
storedMarks: matched.storedMarks
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return { action: [input.insertion] };
|
|
34
|
+
};
|
|
35
|
+
export { applyInputRules, resolveInputRules };
|
|
36
|
+
|
|
37
|
+
//# sourceMappingURL=input.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"input.js","names":[],"sources":["../src/input.ts"],"sourcesContent":["/**\n * Input rules (§124 §4): what someone just typed, read as an intent. A rule is not a\n * Transform (§23): a transform normalizes a document whenever it is applied, while a rule\n * is about the moment a particular thing was typed — `# ` at the start of a block means\n * \"make this a heading\" only at that moment, and never on a document loaded from storage.\n *\n * `applyInputRules` is the composition: the insertion, the deletes that consume what the\n * rule matched, and the rule's commands, as one action, so a transition and an undo step\n * cover typing the marker and the change it made.\n */\nimport type { Action, Command } from './command.js'\n\n/** What a rule makes of the text before the caret, and what to do about it. */\nexport interface InputMatch {\n /** How many characters of that text the rule consumed; the caller deletes them. */\n readonly remove: number\n readonly commands: Action\n /**\n * The marks the text typed next carries, when the rule decides it: `**bold**` ends its bold, so\n * typing on is plain, though the bold run would otherwise continue. Absent, the editor's own\n * rule applies.\n */\n readonly storedMarks?: ReadonlyArray<string> | undefined\n}\n\n/**\n * A rule over the text before the caret. `match` is a pure read: it sees the text and the\n * kinds of the node blocks around the caret's block, innermost first (`['ListItem',\n * 'List']` inside a bulleted item), and nothing else, so a rule cannot depend on a\n * selection, a clock, or the Model.\n */\nexport interface InputRule {\n readonly name: string\n readonly match: (textBefore: string, within: ReadonlyArray<string>) => InputMatch | undefined\n}\n\n/** One edit to read a rule against, named so two strings cannot be handed over swapped. */\nexport interface InputContext {\n /** The caret's block text before the caret, before this insertion. */\n readonly textBefore: string\n /** What was just typed. */\n readonly text: string\n /** The insertion itself, which a matching rule then acts on. */\n readonly insertion: Command\n /** The kinds of the node blocks around the caret's block, innermost first. Default none. */\n readonly within?: ReadonlyArray<string>\n}\n\n/**\n * The commands that insert the text and then let the first matching rule act on it: the\n * insertion, one backward delete per character the rule consumed, and the rule's commands.\n * Consumption is by deleting backwards rather than by a range, because a range would have\n * to be computed against a state the insertion has not produced yet — the deletes resolve\n * as each one runs. A rule that does not match yields the insertion alone.\n *\n * A rule that claims to consume more than it was shown throws: those deletes would run past\n * the block's start and join it to the one before, which no rule means.\n */\nexport const applyInputRules = (rules: ReadonlyArray<InputRule>, input: InputContext): Action =>\n resolveInputRules(rules, input).action\n\n/**\n * `applyInputRules`, with the marks the matching rule says the next typed text carries, which an\n * editor keeps as its stored marks.\n */\nexport const resolveInputRules = (\n rules: ReadonlyArray<InputRule>,\n input: InputContext,\n): { readonly action: Action; readonly storedMarks?: ReadonlyArray<string> | undefined } => {\n const typed = `${input.textBefore}${input.text}`\n for (const rule of rules) {\n const matched = rule.match(typed, input.within ?? [])\n if (matched === undefined) continue\n if (!Number.isInteger(matched.remove) || matched.remove < 0 || matched.remove > typed.length) {\n throw new RangeError(\n `Input rule \"${rule.name}\" removes ${matched.remove} characters of the ${typed.length} it was shown`,\n )\n }\n const consumed: ReadonlyArray<Command> = Array.from(\n { length: matched.remove },\n (): Command => ({ type: 'DeleteBackward' }),\n )\n return {\n action: [input.insertion, ...consumed, ...matched.commands],\n storedMarks: matched.storedMarks,\n }\n }\n return { action: [input.insertion] }\n}\n"],"mappings":";;;;;;;;;;;AA0DA,MAAa,mBAAmB,OAAiC,UAC/D,kBAAkB,OAAO,KAAK,CAAC,CAAC;;;;;AAMlC,MAAa,qBACX,OACA,UAC0F;CAC1F,MAAM,QAAQ,GAAG,MAAM,aAAa,MAAM;CAC1C,KAAK,MAAM,QAAQ,OAAO;EACxB,MAAM,UAAU,KAAK,MAAM,OAAO,MAAM,UAAU,CAAC,CAAC;EACpD,IAAI,YAAY,KAAA,GAAW;EAC3B,IAAI,CAAC,OAAO,UAAU,QAAQ,MAAM,KAAK,QAAQ,SAAS,KAAK,QAAQ,SAAS,MAAM,QACpF,MAAM,IAAI,WACR,eAAe,KAAK,KAAK,YAAY,QAAQ,OAAO,qBAAqB,MAAM,OAAO,cACxF;EAEF,MAAM,WAAmC,MAAM,KAC7C,EAAE,QAAQ,QAAQ,OAAO,UACT,EAAE,MAAM,iBAAiB,EAC3C;EACA,OAAO;GACL,QAAQ;IAAC,MAAM;IAAW,GAAG;IAAU,GAAG,QAAQ;GAAQ;GAC1D,aAAa,QAAQ;EACvB;CACF;CACA,OAAO,EAAE,QAAQ,CAAC,MAAM,SAAS,EAAE;AACrC"}
|
package/dist/kit.d.ts
ADDED
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { Block, ContentMode, Document, NodeBlock, NodeId, PropsSchema, blockContent, textContent } from "./document.js";
|
|
3
|
+
import { MarkDef } from "./marks.js";
|
|
4
|
+
/** Whether a kind's runs may carry formatting marks; `all` unless declared otherwise. */
|
|
5
|
+
export type MarksPolicy = 'all' | 'none';
|
|
6
|
+
/**
|
|
7
|
+
* Block content that accepts only the named kinds, so a `List` can declare that its
|
|
8
|
+
* children are `ListItem`s and `validate` can report one that is not (§13, §125).
|
|
9
|
+
*/
|
|
10
|
+
export interface BlockConstraint {
|
|
11
|
+
readonly mode: typeof blockContent;
|
|
12
|
+
readonly of: ReadonlyArray<string>;
|
|
13
|
+
}
|
|
14
|
+
/** Declares that a node holds nested blocks, only of the named kinds. */
|
|
15
|
+
export declare const blocksOf: (...kinds: ReadonlyArray<string>) => BlockConstraint;
|
|
16
|
+
/** A kind's declared content: a mode, or block content restricted to named kinds. */
|
|
17
|
+
export type ChildrenDeclaration = ContentMode | BlockConstraint;
|
|
18
|
+
/**
|
|
19
|
+
* The node shapes this version can declare: a `block` is one of the built-in
|
|
20
|
+
* text blocks, an `atom` is an addressable node holding nothing (with optional
|
|
21
|
+
* props), and an application `node` holds runs or nested blocks plus props its own
|
|
22
|
+
* schema validates (§13).
|
|
23
|
+
*/
|
|
24
|
+
export type NodeDefinition = {
|
|
25
|
+
readonly name: string;
|
|
26
|
+
readonly kind: 'block';
|
|
27
|
+
readonly children: 'text';
|
|
28
|
+
} | {
|
|
29
|
+
readonly name: string;
|
|
30
|
+
readonly kind: 'atom';
|
|
31
|
+
readonly children: 'none';
|
|
32
|
+
/** An atom's props decode at this boundary too, so an `Image` carries a source. */
|
|
33
|
+
readonly props?: PropsSchema | undefined;
|
|
34
|
+
} | {
|
|
35
|
+
readonly name: string;
|
|
36
|
+
readonly kind: 'node';
|
|
37
|
+
/** `textContent` holds runs; `blockContent` or `blocksOf(…)` holds nested blocks. */
|
|
38
|
+
readonly children: ChildrenDeclaration;
|
|
39
|
+
/** `none` forbids marks on this kind's runs, as a `CodeBlock` needs. */
|
|
40
|
+
readonly marks: MarksPolicy;
|
|
41
|
+
/** Validates a `Node` block's `props` at this boundary, not in the codec. */
|
|
42
|
+
readonly props?: PropsSchema | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* A boundary a lift never crosses: a table cell's content stays in the cell, where a
|
|
45
|
+
* quote's or a list item's would be lifted out.
|
|
46
|
+
*/
|
|
47
|
+
readonly isolating?: boolean | undefined;
|
|
48
|
+
/** Props a copy split off this node starts with, over the original's (§135). */
|
|
49
|
+
readonly splitProps?: NodeBlock['props'] | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* The kinds this one may stand in (§149); absent, any that holds it. A kind that names
|
|
52
|
+
* them never stands at the top level, as a `ListItem` stands only in a `List`.
|
|
53
|
+
*/
|
|
54
|
+
readonly within?: ReadonlyArray<string> | undefined;
|
|
55
|
+
/** Its text keeps line breaks: Enter inserts one rather than splitting, as in code. */
|
|
56
|
+
readonly lineBreaks?: boolean | undefined;
|
|
57
|
+
/** Tab indents its lines, and a new line starts as indented as the last, as in code. */
|
|
58
|
+
readonly indents?: boolean | undefined;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* A declared application node, with the caller's name, prop schema, and content
|
|
62
|
+
* mode kept in the type: the erasure to `any` happens only where heterogeneous
|
|
63
|
+
* definitions are collected, not at the authoring call.
|
|
64
|
+
*/
|
|
65
|
+
export interface NodeDefinitionOf<Name extends string, Props extends PropsSchema | undefined, Children extends ChildrenDeclaration = typeof textContent> {
|
|
66
|
+
readonly name: Name;
|
|
67
|
+
readonly kind: 'node';
|
|
68
|
+
readonly children: Children;
|
|
69
|
+
readonly marks: MarksPolicy;
|
|
70
|
+
readonly props: Props;
|
|
71
|
+
readonly isolating: boolean;
|
|
72
|
+
readonly splitProps: NodeBlock['props'] | undefined;
|
|
73
|
+
readonly within: ReadonlyArray<string> | undefined;
|
|
74
|
+
readonly lineBreaks: boolean;
|
|
75
|
+
readonly indents: boolean;
|
|
76
|
+
}
|
|
77
|
+
/** Declares a block node kind: a top-level node containing text runs. */
|
|
78
|
+
export declare const block: (name: string) => NodeDefinition;
|
|
79
|
+
/**
|
|
80
|
+
* Declares an atom: an addressable node with no text content. Its optional props
|
|
81
|
+
* decode at the same boundary as a `node`'s, so an `Image` is an atom carrying a
|
|
82
|
+
* source rather than a run holder.
|
|
83
|
+
*/
|
|
84
|
+
export declare const atom: (name: string, options?: {
|
|
85
|
+
readonly Props?: PropsSchema | undefined;
|
|
86
|
+
}) => NodeDefinition;
|
|
87
|
+
/**
|
|
88
|
+
* Declares an application node kind: a `Node` block whose props this schema
|
|
89
|
+
* validates and whose content the `children` declaration states — a mode, or block
|
|
90
|
+
* content restricted to the kinds `blocksOf` names. `marks: 'none'` forbids marks on
|
|
91
|
+
* its runs. The document codec keeps those props as JSON; the Kit is where an
|
|
92
|
+
* application's types meet them.
|
|
93
|
+
*/
|
|
94
|
+
export declare const node: <const Name extends string, Props extends PropsSchema | undefined = undefined, Children extends ChildrenDeclaration = typeof textContent>(name: Name, options?: {
|
|
95
|
+
readonly Props?: Props;
|
|
96
|
+
readonly children?: Children;
|
|
97
|
+
readonly marks?: MarksPolicy;
|
|
98
|
+
/** A boundary a lift never crosses, as a table cell is. */
|
|
99
|
+
readonly isolating?: boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Props a copy split off this node starts with, laid over the original's: Enter in a
|
|
102
|
+
* checked task item starts an unchecked one.
|
|
103
|
+
*/
|
|
104
|
+
readonly splitProps?: Props extends PropsSchema ? Partial<Schema.Schema.Type<Props>> : NodeBlock['props'];
|
|
105
|
+
/** The kinds this one may stand in; it then never stands at the top level (§149). */
|
|
106
|
+
readonly within?: ReadonlyArray<string>;
|
|
107
|
+
/**
|
|
108
|
+
* Its text keeps line breaks, as code does: Enter inserts a newline instead of splitting
|
|
109
|
+
* the block, and Enter on an empty last line leaves it for a paragraph after. A callout's
|
|
110
|
+
* text can keep them too, for a note of several lines.
|
|
111
|
+
*/
|
|
112
|
+
readonly lineBreaks?: boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Its lines are indented, as code's are: Tab and Shift+Tab indent and outdent them, and a
|
|
115
|
+
* line broken with Enter starts as indented as the one before.
|
|
116
|
+
*/
|
|
117
|
+
readonly indents?: boolean;
|
|
118
|
+
}) => NodeDefinitionOf<Name, Props, Children>;
|
|
119
|
+
/**
|
|
120
|
+
* The vocabulary one editor accepts: which node kinds and marks are available.
|
|
121
|
+
* A Kit is a definition, never document state, and it never holds renderers or
|
|
122
|
+
* executable code. Transforms and metadata arrive later.
|
|
123
|
+
*/
|
|
124
|
+
export interface Kit {
|
|
125
|
+
readonly nodes: ReadonlyArray<NodeDefinition>;
|
|
126
|
+
/** Mark definitions, not just names: a Kit carries each mark's boundary policy. */
|
|
127
|
+
readonly marks: ReadonlyArray<MarkDef>;
|
|
128
|
+
}
|
|
129
|
+
export declare const kit: (definition: {
|
|
130
|
+
readonly nodes: ReadonlyArray<NodeDefinition>;
|
|
131
|
+
readonly marks: ReadonlyArray<MarkDef>;
|
|
132
|
+
}) => Kit;
|
|
133
|
+
export interface Diagnostic {
|
|
134
|
+
readonly code: 'UnknownNode' | 'UnsupportedNode' | 'UnknownMark' | 'InvalidProps' | 'MismatchedDefinition' | 'UnexpectedChild' | 'MisplacedNode' | 'ForbiddenMark';
|
|
135
|
+
readonly message: string;
|
|
136
|
+
readonly node?: NodeId;
|
|
137
|
+
readonly detail?: string;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* A block's semantic kind: an application node's declared kind, a preserved node's
|
|
141
|
+
* original type, or a built-in block's own type. This is what a `blocksOf`
|
|
142
|
+
* constraint names, and what a `NodeRegistry` is looked up by.
|
|
143
|
+
*/
|
|
144
|
+
export declare const blockKind: (block: Block) => string;
|
|
145
|
+
/**
|
|
146
|
+
* Whether a kind may stand in a parent of `parentKind`, or at the top level when that is
|
|
147
|
+
* undefined: anywhere, unless its declaration names where (§149).
|
|
148
|
+
*/
|
|
149
|
+
export declare const standsWithin: (declared: NodeDefinition | undefined, parentKind: string | undefined) => boolean;
|
|
150
|
+
/** Structural summary of a Kit, for tooling and tests. */
|
|
151
|
+
export declare const inspectKit: (definition: Kit) => {
|
|
152
|
+
blocks: number;
|
|
153
|
+
atoms: number;
|
|
154
|
+
nodes: number;
|
|
155
|
+
marks: number;
|
|
156
|
+
};
|
|
157
|
+
/**
|
|
158
|
+
* The node vocabulary an edit may target, the way `MarkRegistry` is the mark
|
|
159
|
+
* vocabulary a mark edit consults. `run` reads a kind's declaration from it to refuse
|
|
160
|
+
* an edit a constraint forbids (§117, §125); `apply` still takes none, so a durable
|
|
161
|
+
* transaction never depends on a vocabulary that may have moved.
|
|
162
|
+
*/
|
|
163
|
+
export interface NodeRegistry {
|
|
164
|
+
/** The declaration for a block kind, or undefined when this vocabulary has none. */
|
|
165
|
+
readonly definitionFor: (kind: string) => NodeDefinition | undefined;
|
|
166
|
+
}
|
|
167
|
+
/** Builds the registry an edit consults. A later declaration for a name wins, as a Map does. */
|
|
168
|
+
export declare const nodeRegistry: (definitions: ReadonlyArray<NodeDefinition>) => NodeRegistry;
|
|
169
|
+
/**
|
|
170
|
+
* Checks a document against a Kit's vocabulary without changing it, walking
|
|
171
|
+
* nested blocks so a container's children are checked too. Reports preserved
|
|
172
|
+
* unknown blocks, known blocks the Kit does not declare, declarations that
|
|
173
|
+
* disagree with how the document holds a block, application nodes whose props
|
|
174
|
+
* their schema refuses, and marks outside the Kit or whose props their definition
|
|
175
|
+
* refuses. Content diagnostics never strip or repair the document; callers decide
|
|
176
|
+
* whether to block publishing or surface a placeholder.
|
|
177
|
+
*/
|
|
178
|
+
export declare const validate: (document: Document, definition: Kit) => ReadonlyArray<Diagnostic>;
|
|
179
|
+
//# sourceMappingURL=kit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"kit.d.ts","names":[],"sources":["../src/kit.ts"],"mappings":";;;;YAgBY;;;;;iBAMK;WACN,aAAa;WACb,IAAI;;;qBAIF,cAAQ,OAAc,0BAAwB;;YAI/C,sBAAsB,cAAc;;;;;;;YAQpC;WACG;WAAuB;WAAwB;;WAE/C;WACA;WACA;;WAEA,QAAQ;;WAGR;WACA;;WAEA,UAAU;;WAEV,OAAO;;WAEP,QAAQ;;;;;WAKR;;WAEA,aAAa;;;;;WAKb,SAAS;;WAET;;WAEA;;;;;;;iBAQE,iBACf,qBAC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