@astl/editor-tiptap 0.0.0 → 0.1.1
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/CHANGELOG.md +111 -0
- package/LICENSE +202 -0
- package/README.md +49 -0
- package/dist/adapter.d.ts +13 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +421 -0
- package/dist/adapter.js.map +1 -0
- package/dist/blocks.d.ts +96 -0
- package/dist/blocks.d.ts.map +1 -0
- package/dist/blocks.js +460 -0
- package/dist/blocks.js.map +1 -0
- package/dist/define.d.ts +64 -0
- package/dist/define.d.ts.map +1 -0
- package/dist/define.js +20 -0
- package/dist/define.js.map +1 -0
- package/dist/element.d.ts +91 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +779 -0
- package/dist/element.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/layouts.d.ts +52 -0
- package/dist/layouts.d.ts.map +1 -0
- package/dist/layouts.js +198 -0
- package/dist/layouts.js.map +1 -0
- package/dist/nodes.d.ts +150 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +2426 -0
- package/dist/nodes.js.map +1 -0
- package/dist/source-view.d.ts +26 -0
- package/dist/source-view.d.ts.map +1 -0
- package/dist/source-view.js +114 -0
- package/dist/source-view.js.map +1 -0
- package/dist/source.d.ts +8 -0
- package/dist/source.d.ts.map +1 -0
- package/dist/source.js +24 -0
- package/dist/source.js.map +1 -0
- package/dist/style.d.ts +16 -0
- package/dist/style.d.ts.map +1 -0
- package/dist/style.js +568 -0
- package/dist/style.js.map +1 -0
- package/dist/styles.d.ts +5 -0
- package/dist/styles.d.ts.map +1 -0
- package/dist/styles.js +921 -0
- package/dist/styles.js.map +1 -0
- package/dist/surface.d.ts +24 -0
- package/dist/surface.d.ts.map +1 -0
- package/dist/surface.js +90 -0
- package/dist/surface.js.map +1 -0
- package/dist/tiptap.d.ts +10 -0
- package/dist/tiptap.d.ts.map +1 -0
- package/dist/tiptap.js +9 -0
- package/dist/tiptap.js.map +1 -0
- package/package.json +63 -4
- package/src/adapter.ts +421 -0
- package/src/blocks.ts +577 -0
- package/src/define.ts +84 -0
- package/src/element.ts +988 -0
- package/src/index.ts +10 -0
- package/src/layouts.ts +220 -0
- package/src/nodes.ts +2690 -0
- package/src/source-view.ts +138 -0
- package/src/source.ts +27 -0
- package/src/style.ts +638 -0
- package/src/styles.ts +931 -0
- package/src/surface.ts +110 -0
- package/src/tiptap.ts +9 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The source view of `<astl-editor>` (`part="source"`): the document's HTML (content format 1) laid out for reading
|
|
3
|
+
* (`formatHtml`) in a monospace text area, and under it the errors that keep the editor in this view. A module of its
|
|
4
|
+
* own, imported when the view first opens, so the editor's first load goes without it.
|
|
5
|
+
*/
|
|
6
|
+
import {
|
|
7
|
+
COLUMN_TAG,
|
|
8
|
+
COLUMNS_TAG,
|
|
9
|
+
DOC_TAG,
|
|
10
|
+
directionOf,
|
|
11
|
+
type ElementNode,
|
|
12
|
+
element,
|
|
13
|
+
type Node as HtmlNode,
|
|
14
|
+
type Issue,
|
|
15
|
+
isElement,
|
|
16
|
+
PAGE_TAG,
|
|
17
|
+
} from '@astl/core';
|
|
18
|
+
import { type Authoring, issueStyles, renderIssues } from '@astl/editor';
|
|
19
|
+
import { parseHtml, serializeHtml } from '@astl/engine';
|
|
20
|
+
import { css, html, type TemplateResult } from 'lit';
|
|
21
|
+
|
|
22
|
+
/** Where formatHtml puts each child element on a line of its own: elements that hold blocks, not text. */
|
|
23
|
+
const LAID_OUT: ReadonlySet<string> = new Set([
|
|
24
|
+
DOC_TAG,
|
|
25
|
+
PAGE_TAG,
|
|
26
|
+
COLUMNS_TAG,
|
|
27
|
+
COLUMN_TAG,
|
|
28
|
+
'ul',
|
|
29
|
+
'ol',
|
|
30
|
+
'blockquote',
|
|
31
|
+
]);
|
|
32
|
+
|
|
33
|
+
const INDENT = ' ';
|
|
34
|
+
|
|
35
|
+
/** HTML inter-element white space (ASCII only, as in the HTML standard). */
|
|
36
|
+
const WHITE_SPACE = /^[\t\n\f\r ]*$/;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* A source laid out for reading. In the document, a page, a layout, a column, a list and a quote (and at the top)
|
|
40
|
+
* that hold elements and only inter-element white space, each child element goes on a line of its own, indented two
|
|
41
|
+
* spaces per level, and the end tag on its own line: white space there means nothing to Astrolabe, to the editor or
|
|
42
|
+
* to a browser. Everything else (paragraphs, headings, list items, figures, rules, components) is written as
|
|
43
|
+
* `serializeHtml` writes it, on one line, so no white space is added where it would count: a component keeps its
|
|
44
|
+
* children as data, text included.
|
|
45
|
+
*
|
|
46
|
+
* Dropping the added white space gives the same tree back, and formatting twice gives the same text. A source the
|
|
47
|
+
* parser refuses comes back as it is.
|
|
48
|
+
*/
|
|
49
|
+
export function formatHtml(source: string): string {
|
|
50
|
+
let nodes: HtmlNode[];
|
|
51
|
+
try {
|
|
52
|
+
nodes = parseHtml(source);
|
|
53
|
+
} catch {
|
|
54
|
+
return source;
|
|
55
|
+
}
|
|
56
|
+
// `<plaintext>` takes the rest of the input as its text: no line may follow it.
|
|
57
|
+
return nodes.some(endsInput) ? serializeHtml(nodes) : (laidOut(nodes, 0) ?? serializeHtml(nodes));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const endsInput = (node: HtmlNode): boolean =>
|
|
61
|
+
isElement(node) && (node.tag === 'plaintext' || node.children.some(endsInput));
|
|
62
|
+
|
|
63
|
+
/** Each element of `nodes` on a line of its own, `depth` levels in; null when they hold text. */
|
|
64
|
+
function laidOut(nodes: readonly HtmlNode[], depth: number): string | null {
|
|
65
|
+
if (nodes.some((node) => node.type === 'text' && !WHITE_SPACE.test(node.value))) return null;
|
|
66
|
+
return nodes
|
|
67
|
+
.filter((node): node is ElementNode => isElement(node))
|
|
68
|
+
.map((node) => INDENT.repeat(depth) + formatElement(node, depth))
|
|
69
|
+
.join('\n');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function formatElement(node: ElementNode, depth: number): string {
|
|
73
|
+
const inner =
|
|
74
|
+
LAID_OUT.has(node.tag) && node.children.some((child) => isElement(child))
|
|
75
|
+
? laidOut(node.children, depth + 1)
|
|
76
|
+
: null;
|
|
77
|
+
if (inner === null) return serializeHtml([node]);
|
|
78
|
+
// The start tag as serializeHtml writes it: the element emptied, without its end tag.
|
|
79
|
+
const empty = serializeHtml([element(node.tag, node.attrs)]);
|
|
80
|
+
const end = `</${node.tag}>`;
|
|
81
|
+
return `${empty.slice(0, -end.length)}\n${inner}\n${INDENT.repeat(depth)}${end}`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// The text area grows with the laid-out source where the browser sizes fields by their content (`field-sizing`),
|
|
85
|
+
// so the page scrolls rather than a box in it; elsewhere it keeps its height and scrolls.
|
|
86
|
+
const sourceStyles = css`
|
|
87
|
+
.source {
|
|
88
|
+
display: grid;
|
|
89
|
+
gap: var(--astl-space-2);
|
|
90
|
+
}
|
|
91
|
+
.source textarea {
|
|
92
|
+
box-sizing: border-box;
|
|
93
|
+
inline-size: 100%;
|
|
94
|
+
min-block-size: 24rem;
|
|
95
|
+
field-sizing: content;
|
|
96
|
+
padding: var(--astl-space-3);
|
|
97
|
+
border: var(--astl-outline-width) solid var(--astl-outline);
|
|
98
|
+
border-radius: var(--astl-radius-l);
|
|
99
|
+
background: var(--astl-surface);
|
|
100
|
+
color: var(--astl-ink);
|
|
101
|
+
font: var(--astl-size-small) / 1.5 var(--astl-font-mono);
|
|
102
|
+
}
|
|
103
|
+
.source textarea:focus-visible {
|
|
104
|
+
outline: var(--astl-focus-width) solid var(--astl-focus);
|
|
105
|
+
outline-offset: var(--astl-focus-offset);
|
|
106
|
+
}
|
|
107
|
+
`;
|
|
108
|
+
|
|
109
|
+
/** Adopts the view's stylesheets into the shadow root holding `node`, once. */
|
|
110
|
+
function adopt(node: Node): void {
|
|
111
|
+
const root = node.getRootNode() as Partial<DocumentOrShadowRoot>;
|
|
112
|
+
const sheets = [issueStyles.styleSheet, sourceStyles.styleSheet];
|
|
113
|
+
if (root.adoptedStyleSheets === undefined || sheets.includes(undefined)) return;
|
|
114
|
+
const missing = sheets.filter((sheet) => sheet !== undefined && !root.adoptedStyleSheets?.includes(sheet));
|
|
115
|
+
if (missing.length > 0)
|
|
116
|
+
root.adoptedStyleSheets = [...root.adoptedStyleSheets, ...(missing as CSSStyleSheet[])];
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The view for `text` (what the author is editing) with `errors` listed under it (sentences of `describe`, nothing
|
|
121
|
+
* focusable); `onInput` gets each edit.
|
|
122
|
+
*/
|
|
123
|
+
export function renderSource(
|
|
124
|
+
root: Node,
|
|
125
|
+
authoring: Authoring,
|
|
126
|
+
text: string,
|
|
127
|
+
errors: readonly Issue[],
|
|
128
|
+
onInput: (text: string) => void,
|
|
129
|
+
): TemplateResult {
|
|
130
|
+
adopt(root);
|
|
131
|
+
const lang = authoring.locale;
|
|
132
|
+
return html`<div class="source" part="source" lang=${lang} dir=${directionOf(lang)}>
|
|
133
|
+
<label for="source">${authoring.t('astl.editor', 'view.sourceLabel')}</label>
|
|
134
|
+
<textarea id="source" dir="ltr" spellcheck="false" .value=${text} aria-invalid=${errors.length > 0}
|
|
135
|
+
@input=${(event: Event) => onInput((event.target as HTMLTextAreaElement).value)}></textarea>
|
|
136
|
+
${renderIssues(errors, authoring, null, text)}
|
|
137
|
+
</div>`;
|
|
138
|
+
}
|
package/src/source.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** The source of the TipTap document, as the adapter hands it to the host and the frames validate it. */
|
|
2
|
+
import { STYLE_TAG } from '@astl/core';
|
|
3
|
+
import { DOMSerializer, type Node as PMNode } from '@tiptap/pm/model';
|
|
4
|
+
|
|
5
|
+
/** A document without a window: nothing created or parsed in it is upgraded into a player element. */
|
|
6
|
+
export const inert: Document = document.implementation.createHTMLDocument('');
|
|
7
|
+
|
|
8
|
+
/** The HTML of nodes made in `inert`. */
|
|
9
|
+
export function toHtml(nodes: DocumentFragment | globalThis.Node): string {
|
|
10
|
+
const box = inert.createElement('div');
|
|
11
|
+
box.append(nodes);
|
|
12
|
+
return box.innerHTML;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The source of a document: `<astl-doc>` with its own attributes, around its rules and then its content. */
|
|
16
|
+
export function sourceOf(doc: PMNode): string {
|
|
17
|
+
const quote = (value: string) => value.replaceAll('&', '&').replaceAll('"', '"');
|
|
18
|
+
const attributes = (record: Record<string, string>) =>
|
|
19
|
+
Object.entries(record)
|
|
20
|
+
.map(([name, value]) => ` ${name}="${quote(value)}"`)
|
|
21
|
+
.join('');
|
|
22
|
+
const rules = (doc.attrs.rules as readonly Record<string, string>[])
|
|
23
|
+
.map((rule) => `<${STYLE_TAG}${attributes(rule)}></${STYLE_TAG}>`)
|
|
24
|
+
.join('');
|
|
25
|
+
const serializer = DOMSerializer.fromSchema(doc.type.schema);
|
|
26
|
+
return `<astl-doc${attributes(doc.attrs.attributes)}>${rules}${toHtml(serializer.serializeFragment(doc.content, { document: inert }))}</astl-doc>`;
|
|
27
|
+
}
|