@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.
Files changed (69) hide show
  1. package/CHANGELOG.md +111 -0
  2. package/LICENSE +202 -0
  3. package/README.md +49 -0
  4. package/dist/adapter.d.ts +13 -0
  5. package/dist/adapter.d.ts.map +1 -0
  6. package/dist/adapter.js +421 -0
  7. package/dist/adapter.js.map +1 -0
  8. package/dist/blocks.d.ts +96 -0
  9. package/dist/blocks.d.ts.map +1 -0
  10. package/dist/blocks.js +460 -0
  11. package/dist/blocks.js.map +1 -0
  12. package/dist/define.d.ts +64 -0
  13. package/dist/define.d.ts.map +1 -0
  14. package/dist/define.js +20 -0
  15. package/dist/define.js.map +1 -0
  16. package/dist/element.d.ts +91 -0
  17. package/dist/element.d.ts.map +1 -0
  18. package/dist/element.js +779 -0
  19. package/dist/element.js.map +1 -0
  20. package/dist/index.d.ts +11 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +3 -0
  23. package/dist/index.js.map +1 -0
  24. package/dist/layouts.d.ts +52 -0
  25. package/dist/layouts.d.ts.map +1 -0
  26. package/dist/layouts.js +198 -0
  27. package/dist/layouts.js.map +1 -0
  28. package/dist/nodes.d.ts +150 -0
  29. package/dist/nodes.d.ts.map +1 -0
  30. package/dist/nodes.js +2426 -0
  31. package/dist/nodes.js.map +1 -0
  32. package/dist/source-view.d.ts +26 -0
  33. package/dist/source-view.d.ts.map +1 -0
  34. package/dist/source-view.js +114 -0
  35. package/dist/source-view.js.map +1 -0
  36. package/dist/source.d.ts +8 -0
  37. package/dist/source.d.ts.map +1 -0
  38. package/dist/source.js +24 -0
  39. package/dist/source.js.map +1 -0
  40. package/dist/style.d.ts +16 -0
  41. package/dist/style.d.ts.map +1 -0
  42. package/dist/style.js +568 -0
  43. package/dist/style.js.map +1 -0
  44. package/dist/styles.d.ts +5 -0
  45. package/dist/styles.d.ts.map +1 -0
  46. package/dist/styles.js +921 -0
  47. package/dist/styles.js.map +1 -0
  48. package/dist/surface.d.ts +24 -0
  49. package/dist/surface.d.ts.map +1 -0
  50. package/dist/surface.js +90 -0
  51. package/dist/surface.js.map +1 -0
  52. package/dist/tiptap.d.ts +10 -0
  53. package/dist/tiptap.d.ts.map +1 -0
  54. package/dist/tiptap.js +9 -0
  55. package/dist/tiptap.js.map +1 -0
  56. package/package.json +63 -4
  57. package/src/adapter.ts +421 -0
  58. package/src/blocks.ts +577 -0
  59. package/src/define.ts +84 -0
  60. package/src/element.ts +988 -0
  61. package/src/index.ts +10 -0
  62. package/src/layouts.ts +220 -0
  63. package/src/nodes.ts +2690 -0
  64. package/src/source-view.ts +138 -0
  65. package/src/source.ts +27 -0
  66. package/src/style.ts +638 -0
  67. package/src/styles.ts +931 -0
  68. package/src/surface.ts +110 -0
  69. package/src/tiptap.ts +9 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"styles.js","sourceRoot":"","sources":["../src/styles.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAkB,GAAG,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAErD,wGAAwG;AACxG,MAAM,CAAC,MAAM,aAAa,GAAG,cAAc,CAAC;AAE5C;;;;;GAKG;AACH,MAAM,iBAAiB,GAAc,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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FAA2F;AAC3F,MAAM,UAAU,kBAAkB,CAAC,IAAU;IAC3C,MAAM,IAAI,GAAG,IAAI,CAAC,WAAW,EAAmC,CAAC;IACjE,MAAM,KAAK,GAAG,aAAa,CAAC,UAAU,CAAC;IACvC,IAAI,KAAK,KAAK,SAAS,IAAI,IAAI,CAAC,kBAAkB,KAAK,SAAS;QAAE,OAAO;IACzE,IAAI,CAAC,IAAI,CAAC,kBAAkB,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,IAAI,CAAC,kBAAkB,GAAG,CAAC,GAAG,IAAI,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;AAC9G,CAAC"}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * The built-in editing surface of `<astl-editor>`: a TipTap editor in the element's light DOM (ProseMirror selection
3
+ * is unreliable inside shadow roots), slotted into its `surface` slot. Only `element.ts` loads this module, by dynamic
4
+ * import, and only when no other editor is plugged in.
5
+ */
6
+ import type { ComponentManifest, EditorAdapter } from '@astl/core';
7
+ import { type Authoring } from '@astl/editor';
8
+ import type { LitElement } from 'lit';
9
+ import { type SurfaceChange } from './blocks.js';
10
+ import { type PictureHooks } from './nodes.js';
11
+ export interface Surface {
12
+ readonly adapter: EditorAdapter;
13
+ /** The authoring its labels come from. */
14
+ readonly authoring: Authoring;
15
+ /** Destroys the editor and removes its element. */
16
+ destroy(): void;
17
+ }
18
+ /**
19
+ * A TipTap editor on `source`, appended to `host` (the `<astl-editor>`) in the `surface` slot. `manifests` are every
20
+ * registered type: the schema keeps them all, so content of a type the authoring leaves out is never dropped. `changes`
21
+ * hears of each page added or joined and each block moved or deleted, before the change event of its edit.
22
+ */
23
+ export declare function createSurface(host: LitElement, authoring: Authoring, manifests: readonly ComponentManifest[], source: string, pictures?: PictureHooks, changes?: (change: SurfaceChange) => void): Surface;
24
+ //# sourceMappingURL=surface.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"surface.d.ts","sourceRoot":"","sources":["../src/surface.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,KAAK,EAAE,iBAAiB,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AACnE,OAAO,EAAE,KAAK,SAAS,EAAoB,MAAM,cAAc,CAAC;AAGhE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,KAAK,CAAC;AAEtC,OAAO,EAAgB,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAoC,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAGjF,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC;IAChC,0CAA0C;IAC1C,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;IAC9B,mDAAmD;IACnD,OAAO,IAAI,IAAI,CAAC;CACjB;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,UAAU,EAChB,SAAS,EAAE,SAAS,EACpB,SAAS,EAAE,SAAS,iBAAiB,EAAE,EACvC,MAAM,EAAE,MAAM,EACd,QAAQ,CAAC,EAAE,YAAY,EACvB,OAAO,CAAC,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,IAAI,GACxC,OAAO,CA0ET"}
@@ -0,0 +1,90 @@
1
+ import { EDITOR_NAMESPACE } from '@astl/editor';
2
+ import { Editor } from '@tiptap/core';
3
+ import { EditorState } from '@tiptap/pm/state';
4
+ import { createTiptapAdapter } from './adapter.js';
5
+ import { BLOCK_CHANGE } from './blocks.js';
6
+ import { astrolabeExtensions, PAGE_CHANGE } from './nodes.js';
7
+ import { adoptSurfaceStyles, SURFACE_CLASS } from './styles.js';
8
+ /**
9
+ * A TipTap editor on `source`, appended to `host` (the `<astl-editor>`) in the `surface` slot. `manifests` are every
10
+ * registered type: the schema keeps them all, so content of a type the authoring leaves out is never dropped. `changes`
11
+ * hears of each page added or joined and each block moved or deleted, before the change event of its edit.
12
+ */
13
+ export function createSurface(host, authoring, manifests, source, pictures, changes) {
14
+ adoptSurfaceStyles(host);
15
+ const mount = document.createElement('div');
16
+ mount.slot = 'surface';
17
+ mount.className = SURFACE_CLASS;
18
+ host.append(mount);
19
+ let editor = null;
20
+ // A host may keep the toolbar in view (a sticky `::part(toolbar)`, the editor reference): what the editor scrolls
21
+ // into view (the caret, a picked page or element) then stops below it, not under it. Read when its size changes.
22
+ const covered = new ResizeObserver(([entry]) => {
23
+ const toolbar = entry?.target;
24
+ if (!(toolbar instanceof HTMLElement) || editor === null || editor.isDestroyed)
25
+ return;
26
+ const style = getComputedStyle(toolbar);
27
+ const offset = Number.parseFloat(style.insetBlockStart);
28
+ const top = style.position === 'sticky' && !Number.isNaN(offset) ? offset + toolbar.offsetHeight : 0;
29
+ // ProseMirror's defaults on the other sides: no threshold, a 5px margin.
30
+ editor.view.setProps({
31
+ scrollThreshold: { top, right: 0, bottom: 0, left: 0 },
32
+ scrollMargin: { top: top + 5, right: 5, bottom: 5, left: 5 },
33
+ });
34
+ });
35
+ const destroy = () => {
36
+ covered.disconnect();
37
+ editor?.destroy();
38
+ mount.remove();
39
+ };
40
+ try {
41
+ editor = new Editor({
42
+ element: mount,
43
+ // No <style> element: the surface stylesheet is adopted (strict CSP allows it).
44
+ injectCSS: false,
45
+ extensions: astrolabeExtensions(authoring, manifests, pictures === undefined ? {} : { pictures }),
46
+ editorProps: {
47
+ attributes: (state) => ({
48
+ 'aria-label': authoring.t(EDITOR_NAMESPACE, 'surface.label'),
49
+ 'aria-multiline': 'true',
50
+ // The content language, for spell checking and screen readers.
51
+ lang: state.doc.attrs.attributes.lang ?? '',
52
+ }),
53
+ handleDOMEvents: {
54
+ // Tab into the text leaves no caret (a page starts with its title field, not text), and ProseMirror only
55
+ // restores a caret the browser moved. Once the browser has placed its own: the editor's, where it was.
56
+ focus: (view) => {
57
+ setTimeout(() => {
58
+ const anchor = document.getSelection()?.anchorNode ?? null;
59
+ if (!view.isDestroyed && view.hasFocus() && (anchor === null || !view.dom.contains(anchor)))
60
+ view.focus();
61
+ });
62
+ return false;
63
+ },
64
+ },
65
+ },
66
+ });
67
+ const adapter = createTiptapAdapter(editor, manifests);
68
+ adapter.setSource(source);
69
+ // TipTap tells of a transaction before the update that its change listeners hear.
70
+ editor.on('transaction', ({ transaction }) => {
71
+ const change = (transaction.getMeta(PAGE_CHANGE) ?? transaction.getMeta(BLOCK_CHANGE));
72
+ if (change !== undefined)
73
+ changes?.(change);
74
+ });
75
+ // Loading is not an edit: a fresh state, so undo never goes back to an empty page.
76
+ editor.view.updateState(EditorState.create({ doc: editor.state.doc, plugins: editor.state.plugins }));
77
+ // The toolbar is there once the host has rendered: this may be its first update.
78
+ void host.updateComplete.then(() => {
79
+ const toolbar = host.renderRoot.querySelector('[part~="toolbar"]');
80
+ if (toolbar !== null && editor?.isDestroyed === false)
81
+ covered.observe(toolbar);
82
+ });
83
+ return { adapter, authoring, destroy };
84
+ }
85
+ catch (error) {
86
+ destroy();
87
+ throw error;
88
+ }
89
+ }
90
+ //# sourceMappingURL=surface.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"surface.js","sourceRoot":"","sources":["../src/surface.ts"],"names":[],"mappings":"AAMA,OAAO,EAAkB,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EAAE,YAAY,EAAsB,MAAM,aAAa,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,WAAW,EAAqB,MAAM,YAAY,CAAC;AACjF,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAUhE;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAC3B,IAAgB,EAChB,SAAoB,EACpB,SAAuC,EACvC,MAAc,EACd,QAAuB,EACvB,OAAyC;IAEzC,kBAAkB,CAAC,IAAI,CAAC,CAAC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M,CAAC,CAAC;QAC1B,kFAAkF;QAClF,MAAM,CAAC,EAAE,CAAC,aAAa,EAAE,CAAC,EAAE,WAAW,EAAE,EAAE,EAAE;YAC3C,MAAM,MAAM,GAAG,CAAC,WAAW,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,WAAW,CAAC,OAAO,CAAC,YAAY,CAAC,CAExE,CAAC;YACd,IAAI,MAAM,KAAK,SAAS;gBAAE,OAAO,EAAE,CAAC,MAAM,CAAC,CAAC;QAC9C,CAAC,CAAC,CAAC;QACH,mFAAmF;QACnF,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;QACtG,iFAAiF;QACjF,KAAK,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,GAAG,EAAE;YACjC,MAAM,OAAO,GAAG,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,mBAAmB,CAAC,CAAC;YACnE,IAAI,OAAO,KAAK,IAAI,IAAI,MAAM,EAAE,WAAW,KAAK,KAAK;gBAAE,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;QAClF,CAAC,CAAC,CAAC;QACH,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC;IACzC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,EAAE,CAAC;QACV,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * `@astl/editor-tiptap/tiptap`: for hosts that build their own TipTap editor around the authoring kit. A separate
3
+ * entry, because it imports TipTap: the main entry never does, so `<astl-editor>` with another editor never loads it.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ export { createTiptapAdapter } from './adapter.js';
8
+ export type { PictureHooks } from './nodes.js';
9
+ export { astrolabeExtensions } from './nodes.js';
10
+ //# sourceMappingURL=tiptap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tiptap.d.ts","sourceRoot":"","sources":["../src/tiptap.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,YAAY,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC"}
package/dist/tiptap.js ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * `@astl/editor-tiptap/tiptap`: for hosts that build their own TipTap editor around the authoring kit. A separate
3
+ * entry, because it imports TipTap: the main entry never does, so `<astl-editor>` with another editor never loads it.
4
+ *
5
+ * @packageDocumentation
6
+ */
7
+ export { createTiptapAdapter } from './adapter.js';
8
+ export { astrolabeExtensions } from './nodes.js';
9
+ //# sourceMappingURL=tiptap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tiptap.js","sourceRoot":"","sources":["../src/tiptap.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAEnD,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,66 @@
1
1
  {
2
2
  "name": "@astl/editor-tiptap",
3
- "version": "0.0.0",
4
- "description": "Placeholder for the trusted publishing setup. Use 0.1.0 or later.",
3
+ "version": "0.1.1",
4
+ "description": "<astl-editor>: the authoring kit around a TipTap surface, loaded only when no other editor is plugged in through an adapter.",
5
+ "keywords": [
6
+ "astrolabe",
7
+ "education",
8
+ "edtech",
9
+ "e-learning",
10
+ "interactive-exercises",
11
+ "quiz",
12
+ "assessment",
13
+ "tiptap",
14
+ "wysiwyg",
15
+ "authoring"
16
+ ],
5
17
  "license": "Apache-2.0",
6
- "repository": { "type": "git", "url": "git+https://github.com/starship1337/astrolabe.git" }
7
- }
18
+ "author": "The Astrolabe Authors",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git+https://github.com/starship1337/astrolabe.git",
22
+ "directory": "packages/editor-tiptap"
23
+ },
24
+ "homepage": "https://astrolabe.starship.education",
25
+ "bugs": "https://github.com/starship1337/astrolabe/issues",
26
+ "type": "module",
27
+ "sideEffects": false,
28
+ "types": "./dist/index.d.ts",
29
+ "exports": {
30
+ ".": {
31
+ "@astl/source": "./src/index.ts",
32
+ "types": "./dist/index.d.ts",
33
+ "default": "./dist/index.js"
34
+ },
35
+ "./tiptap": {
36
+ "@astl/source": "./src/tiptap.ts",
37
+ "types": "./dist/tiptap.d.ts",
38
+ "default": "./dist/tiptap.js"
39
+ },
40
+ "./package.json": "./package.json"
41
+ },
42
+ "files": [
43
+ "dist",
44
+ "src",
45
+ "CHANGELOG.md",
46
+ "!src/**/*.test.ts",
47
+ "!src/**/testing"
48
+ ],
49
+ "engines": {
50
+ "node": ">=22.12"
51
+ },
52
+ "publishConfig": {
53
+ "access": "public"
54
+ },
55
+ "dependencies": {
56
+ "@astl/core": "0.1.1",
57
+ "@astl/editor": "0.1.1",
58
+ "@astl/engine": "0.1.1",
59
+ "@astl/player": "0.1.1",
60
+ "@astl/tokens": "0.1.1",
61
+ "@tiptap/core": "3.32.0",
62
+ "@tiptap/pm": "3.32.0",
63
+ "@tiptap/starter-kit": "3.32.0",
64
+ "lit": "3.3.3"
65
+ }
66
+ }
package/src/adapter.ts ADDED
@@ -0,0 +1,421 @@
1
+ /**
2
+ * EditorAdapter over a TipTap editor. Everything crosses the boundary as source HTML in the content format, so the
3
+ * authoring kit never sees a ProseMirror node, and this file is the whole integration.
4
+ */
5
+ import {
6
+ type ComponentManifest,
7
+ type EditorAdapter,
8
+ type EditorFormat,
9
+ type EditorFormatState,
10
+ type EditorSelection,
11
+ inertStyles,
12
+ STYLE_TAG,
13
+ } from '@astl/core';
14
+ import { scanElements } from '@astl/editor';
15
+ import type { ChainedCommands, Editor } from '@tiptap/core';
16
+ import { closeHistory } from '@tiptap/pm/history';
17
+ import { DOMSerializer, type Node as PMNode, DOMParser as SchemaParser } from '@tiptap/pm/model';
18
+ import { NodeSelection, type Selection, TextSelection, type Transaction } from '@tiptap/pm/state';
19
+ import { cursorIn, isBlockOf, LAYOUT, LAYOUT_COLUMN, moveBlockIn, removeBlockIn } from './blocks.js';
20
+ import { addPageIn, attributesOf, mergePageIn, nodeName, removePageIn, splitPageIn } from './nodes.js';
21
+ import { inert, sourceOf, toHtml } from './source.js';
22
+ import { createStyles } from './style.js';
23
+
24
+ const MARKS: readonly EditorFormat[] = ['bold', 'italic', 'underline', 'link'];
25
+ const FORMATS: readonly EditorFormat[] = [
26
+ ...MARKS,
27
+ 'paragraph',
28
+ 'heading',
29
+ 'subheading',
30
+ 'bulleted',
31
+ 'numbered',
32
+ ];
33
+
34
+ /** The command of a format on a chain; `active` says whether the format is on at the cursor. */
35
+ function formatCommand(
36
+ chain: ChainedCommands,
37
+ format: EditorFormat,
38
+ active: boolean,
39
+ href: string | undefined,
40
+ ): ChainedCommands {
41
+ switch (format) {
42
+ case 'bold':
43
+ return chain.toggleBold();
44
+ case 'italic':
45
+ return chain.toggleItalic();
46
+ case 'underline':
47
+ return chain.toggleUnderline();
48
+ case 'link':
49
+ return href === undefined || href === ''
50
+ ? chain.extendMarkRange('link').unsetLink()
51
+ : chain.extendMarkRange('link').setLink({ href });
52
+ case 'paragraph':
53
+ return chain.setParagraph();
54
+ case 'heading':
55
+ return active ? chain.setParagraph() : chain.setHeading({ level: 3 });
56
+ case 'subheading':
57
+ return active ? chain.setParagraph() : chain.setHeading({ level: 4 });
58
+ case 'bulleted':
59
+ return chain.toggleBulletList();
60
+ case 'numbered':
61
+ return chain.toggleOrderedList();
62
+ }
63
+ }
64
+
65
+ /** Attribute changes are applied in place (so the selection survives); other changes replace the content. */
66
+ function patch(tr: Transaction, pos: number, from: PMNode, to: PMNode): void {
67
+ if (!from.sameMarkup(to)) tr.setNodeMarkup(pos, undefined, to.attrs, from.marks);
68
+ if (from.isLeaf || from.content.eq(to.content)) return;
69
+ const sameShape =
70
+ from.childCount === to.childCount &&
71
+ from.content.content.every(
72
+ (child, i) => child.type === to.child(i).type && (!child.isText || child.eq(to.child(i))),
73
+ );
74
+ if (!sameShape) {
75
+ tr.replaceWith(pos + 1, pos + from.nodeSize - 1, to.content);
76
+ return;
77
+ }
78
+ // Last child first, so earlier positions stay valid.
79
+ const offsets: number[] = [];
80
+ from.forEach((_, offset) => {
81
+ offsets.push(offset);
82
+ });
83
+ for (let i = from.childCount - 1; i >= 0; i--)
84
+ patch(tr, pos + 1 + (offsets[i] ?? 0), from.child(i), to.child(i));
85
+ }
86
+
87
+ /**
88
+ * The `EditorAdapter` over a TipTap editor made with `astrolabeExtensions`: the source in and out, one element by id,
89
+ * the selection and change events, for the kit elements or `<astl-editor>`'s `adapter`. `manifests` are the types of
90
+ * its schema, as given to `astrolabeExtensions`.
91
+ */
92
+ export function createTiptapAdapter(editor: Editor, manifests: readonly ComponentManifest[]): EditorAdapter {
93
+ const tags = new Map(manifests.map((m) => [nodeName(m.tag), m.tag]));
94
+ const types = new Map(manifests.map((m) => [nodeName(m.tag), m]));
95
+ const childTags = new Map(
96
+ manifests.flatMap((m) => (m.children ?? []).map((child) => [nodeName(child.tag), child.tag] as const)),
97
+ );
98
+ const isBlock = isBlockOf((node) => types.get(node.type.name) ?? null);
99
+ const serializer = DOMSerializer.fromSchema(editor.schema);
100
+ const parser = SchemaParser.fromSchema(editor.schema);
101
+ const isComponent = (node: PMNode): boolean => tags.has(node.type.name);
102
+ const toSelection = (node: PMNode): EditorSelection | null => {
103
+ const id = node.attrs.attributes.id;
104
+ return typeof id === 'string' && id !== '' ? { id, tag: tags.get(node.type.name) ?? '' } : null;
105
+ };
106
+
107
+ /** A page or a component by source id, with its position. */
108
+ const find = (id: string): { node: PMNode; pos: number } | null => {
109
+ let found: { node: PMNode; pos: number } | null = null;
110
+ editor.state.doc.descendants((node, pos) => {
111
+ if (found !== null) return false;
112
+ if ((isComponent(node) || node.type.name === 'astlPage') && node.attrs.attributes.id === id) {
113
+ found = { node, pos };
114
+ }
115
+ return found === null;
116
+ });
117
+ return found;
118
+ };
119
+
120
+ /** The component the cursor is on: a selected atom, a block around the cursor, or an atom next to it. */
121
+ const selection = (): EditorSelection | null => {
122
+ const current = editor.state.selection;
123
+ if (current instanceof NodeSelection && isComponent(current.node)) return toSelection(current.node);
124
+ const { $from } = current;
125
+ for (let depth = $from.depth; depth > 0; depth--) {
126
+ if (isComponent($from.node(depth))) return toSelection($from.node(depth));
127
+ }
128
+ const near = [$from.nodeAfter, $from.nodeBefore].find((node) => node !== null && isComponent(node));
129
+ return near ? toSelection(near) : null;
130
+ };
131
+
132
+ /** Where `select` puts the cursor: on an atom, or at the start of a block or a page. */
133
+ const placeAt = (id: string): Selection | null => {
134
+ const hit = find(id);
135
+ if (hit === null) return null;
136
+ const { doc } = editor.state;
137
+ return hit.node.isAtom
138
+ ? NodeSelection.create(doc, hit.pos)
139
+ : TextSelection.near(doc.resolve(hit.pos + 1));
140
+ };
141
+
142
+ /** The selected text when the selection is text in one text block outside components, else ''. */
143
+ const selectedText = (): string => {
144
+ const { doc, selection: current } = editor.state;
145
+ if (!(current instanceof TextSelection) || current.empty || !current.$from.sameParent(current.$to))
146
+ return '';
147
+ for (let depth = current.$from.depth; depth > 0; depth--)
148
+ if (isComponent(current.$from.node(depth))) return '';
149
+ let plain = true;
150
+ doc.nodesBetween(current.from, current.to, (node) => {
151
+ if (node.isInline && !node.isText) plain = false;
152
+ return plain;
153
+ });
154
+ return plain ? doc.textBetween(current.from, current.to).trim() : '';
155
+ };
156
+
157
+ /** Where formats apply: nowhere on a selected component, marks only in a component's children. */
158
+ const allowed = (): readonly EditorFormat[] => {
159
+ const current = editor.state.selection;
160
+ if (current instanceof NodeSelection && isComponent(current.node)) return [];
161
+ for (let depth = current.$from.depth; depth > 0; depth--) {
162
+ const node = current.$from.node(depth);
163
+ if (isComponent(node)) return types.get(node.type.name)?.content === 'flow' ? FORMATS : MARKS;
164
+ }
165
+ return FORMATS;
166
+ };
167
+
168
+ const formatState = (): EditorFormatState => {
169
+ const { selection: current } = editor.state;
170
+ const active: EditorFormat[] = [];
171
+ const parent = current.$from.parent;
172
+ const component = current instanceof NodeSelection && isComponent(current.node);
173
+ for (const mark of ['bold', 'italic', 'underline', 'link'] as const)
174
+ if (editor.isActive(mark)) active.push(mark);
175
+ if (!component && parent.type.name === 'paragraph') active.push('paragraph');
176
+ if (!component && parent.type.name === 'heading')
177
+ active.push(parent.attrs.level === 4 ? 'subheading' : 'heading');
178
+ if (editor.isActive('bulletList')) active.push('bulleted');
179
+ if (editor.isActive('orderedList')) active.push('numbered');
180
+ const can = editor.can();
181
+ const possible: Record<EditorFormat, () => boolean> = {
182
+ bold: () => can.toggleBold(),
183
+ italic: () => can.toggleItalic(),
184
+ underline: () => can.toggleUnderline(),
185
+ link: () => (!current.empty || editor.isActive('link')) && can.setLink({ href: 'https://example.org' }),
186
+ paragraph: () => can.setParagraph() || active.includes('paragraph'),
187
+ heading: () => can.setHeading({ level: 3 }) || active.includes('heading'),
188
+ subheading: () => can.setHeading({ level: 4 }) || active.includes('subheading'),
189
+ bulleted: () => can.toggleBulletList(),
190
+ numbered: () => can.toggleOrderedList(),
191
+ };
192
+ const href = editor.getAttributes('link').href;
193
+ return {
194
+ active,
195
+ enabled: allowed().filter((format) => possible[format]()),
196
+ href: typeof href === 'string' ? href : null,
197
+ undo: can.undo(),
198
+ redo: can.redo(),
199
+ };
200
+ };
201
+
202
+ /** Runs a command chain on the editor, focused, as an undo step of its own: whether the document changed. */
203
+ const step = (build: (chain: ChainedCommands) => ChainedCommands): boolean => {
204
+ const before = editor.state.doc;
205
+ build(
206
+ editor
207
+ .chain()
208
+ .focus()
209
+ .command(({ tr }) => {
210
+ closeHistory(tr);
211
+ return true;
212
+ }),
213
+ ).run();
214
+ return editor.state.doc !== before;
215
+ };
216
+
217
+ const adapter: EditorAdapter = {
218
+ getSource: () => sourceOf(editor.state.doc),
219
+ setSource(source) {
220
+ const body = new DOMParser().parseFromString(inertStyles(source), 'text/html').body;
221
+ const doc = body.querySelector('astl-doc');
222
+ // The document's rules are kept on its node, wherever among its children they are: they print first.
223
+ const rules = doc === null ? [] : [...doc.children].filter((el) => el.localName === STYLE_TAG);
224
+ for (const rule of rules) rule.remove();
225
+ const { content } = parser.parse(doc ?? body);
226
+ const tr = editor.state.tr;
227
+ // A change of the document's own attributes (its look, from the inspector) keeps the content and the cursor.
228
+ if (!content.eq(editor.state.doc.content)) tr.replaceWith(0, editor.state.doc.content.size, content);
229
+ const kept = rules.map(attributesOf);
230
+ if (JSON.stringify(kept) !== JSON.stringify(editor.state.doc.attrs.rules))
231
+ tr.setDocAttribute('rules', kept);
232
+ editor.view.dispatch(tr.setDocAttribute('attributes', doc === null ? {} : attributesOf(doc)));
233
+ },
234
+ insert(html, options = {}) {
235
+ const { doc, selection: current } = editor.state;
236
+ const parsed = inertStyles(html);
237
+ const box = inert.createElement('div');
238
+ box.innerHTML = parsed;
239
+ const first = parser.parseSlice(box).content.firstChild;
240
+ const inline = first?.isInline ?? true;
241
+ const layout = first?.type.name === LAYOUT;
242
+ // Never replace or nest into the component at the cursor: insert after it. A selected component in text takes
243
+ // inline snippets after it; a block goes after its block, below.
244
+ const selected = current instanceof NodeSelection && isComponent(current.node) ? current.node : null;
245
+ let at: number | { from: number; to: number } | null =
246
+ selected !== null && (inline || !selected.isInline) ? current.to : null;
247
+ for (let depth = current.$from.depth; at === null && depth > 0; depth--) {
248
+ if (isComponent(current.$from.node(depth))) at = current.$from.after(depth);
249
+ }
250
+ // A layout never nests: from inside one, a new one goes after it.
251
+ for (let depth = current.$from.depth; layout && depth > 0; depth--) {
252
+ if (current.$from.node(depth).type.name === LAYOUT) at = current.$from.after(depth);
253
+ }
254
+ const replace = options.replaceSelection === true && at === null && selectedText() !== '';
255
+ if (at === null && replace) {
256
+ // The selected text without the spaces around it: "word " becomes the snippet and a space.
257
+ const text = doc.textBetween(current.from, current.to);
258
+ at = {
259
+ from: current.from + (text.length - text.trimStart().length),
260
+ to: current.to - (text.length - text.trimEnd().length),
261
+ };
262
+ } else if (at === null && !inline) {
263
+ // Blocks go after the element the cursor is in on its page or in its column, or in the place of an empty
264
+ // paragraph.
265
+ const { $from } = current;
266
+ let top = doc.firstChild?.type.name === 'astlPage' ? 1 : 0;
267
+ for (let depth = $from.depth; depth > top; depth--) {
268
+ if ($from.node(depth).type.name === LAYOUT_COLUMN) top = depth;
269
+ }
270
+ if ($from.depth > top) {
271
+ const node = $from.node(top + 1);
272
+ at =
273
+ node.type.name === 'paragraph' && node.content.size === 0
274
+ ? { from: $from.before(top + 1), to: $from.after(top + 1) }
275
+ : $from.after(top + 1);
276
+ } else at = current.to;
277
+ }
278
+ editor
279
+ .chain()
280
+ .insertContentAt(at ?? current.to, parsed)
281
+ .run();
282
+ // The new element becomes the selection, so the inspector shows it (focus stays where it is); a word that
283
+ // became an inline element keeps the cursor after it, for the author to write on.
284
+ const id = scanElements(html)[0]?.id;
285
+ const placed = id ? placeAt(id) : null;
286
+ if (placed instanceof NodeSelection && replace && placed.node.isInline) {
287
+ editor.view.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, placed.to)));
288
+ } else if (placed !== null) editor.view.dispatch(editor.state.tr.setSelection(placed));
289
+ },
290
+ getElement(id) {
291
+ const hit = find(id);
292
+ return hit !== null && isComponent(hit.node)
293
+ ? toHtml(serializer.serializeNode(hit.node, { document: inert }))
294
+ : null;
295
+ },
296
+ replaceElement(id, html) {
297
+ const hit = find(id);
298
+ if (hit === null || !isComponent(hit.node)) return;
299
+ const box = inert.createElement('div');
300
+ box.innerHTML = inertStyles(html);
301
+ let next: PMNode | null = null;
302
+ parser.parseSlice(box).content.descendants((node) => {
303
+ if (next === null && node.type === hit.node.type) next = node;
304
+ return next === null;
305
+ });
306
+ if (next === null) return;
307
+ const selected =
308
+ editor.state.selection instanceof NodeSelection && editor.state.selection.from === hit.pos;
309
+ const tr = editor.state.tr;
310
+ patch(tr, hit.pos, hit.node, next);
311
+ if (!tr.docChanged) return;
312
+ if (selected) tr.setSelection(NodeSelection.create(tr.doc, hit.pos));
313
+ editor.view.dispatch(tr);
314
+ },
315
+ getSelection: selection,
316
+ select(id) {
317
+ const placed = placeAt(id);
318
+ if (placed === null) return;
319
+ editor.view.dispatch(editor.state.tr.setSelection(placed).scrollIntoView());
320
+ editor.view.focus();
321
+ },
322
+ onChange(listener) {
323
+ const run = () => listener();
324
+ editor.on('update', run);
325
+ return () => editor.off('update', run);
326
+ },
327
+ wrap(tag) {
328
+ const changed = editor.commands.astlWrap(tag);
329
+ editor.view.focus();
330
+ return changed;
331
+ },
332
+ splitPage() {
333
+ const tr = editor.state.tr;
334
+ const id = splitPageIn(tr);
335
+ if (id !== null) editor.view.dispatch(tr);
336
+ return id;
337
+ },
338
+ addPage(after) {
339
+ const tr = editor.state.tr;
340
+ const id = addPageIn(tr, after);
341
+ editor.view.dispatch(tr);
342
+ return id;
343
+ },
344
+ mergePage(id) {
345
+ const tr = editor.state.tr;
346
+ const joined = mergePageIn(tr, id);
347
+ if (joined) editor.view.dispatch(tr);
348
+ return joined;
349
+ },
350
+ removePage(id) {
351
+ const tr = editor.state.tr;
352
+ const removed = removePageIn(tr, id);
353
+ if (removed) editor.view.dispatch(tr);
354
+ return removed;
355
+ },
356
+ format(format, href) {
357
+ if (!allowed().includes(format)) return false;
358
+ const active = formatState().active.includes(format);
359
+ return step((chain) => formatCommand(chain, format, active, href));
360
+ },
361
+ formatState,
362
+ undo() {
363
+ const before = editor.state.doc;
364
+ editor.chain().focus().undo().run();
365
+ return editor.state.doc !== before;
366
+ },
367
+ redo() {
368
+ const before = editor.state.doc;
369
+ editor.chain().focus().redo().run();
370
+ return editor.state.doc !== before;
371
+ },
372
+ moveBlock(block, direction) {
373
+ const tr = editor.state.tr;
374
+ if (moveBlockIn(tr, block, direction, isBlock) === null) return false;
375
+ editor.view.dispatch(tr);
376
+ return true;
377
+ },
378
+ removeBlock(block) {
379
+ const tr = editor.state.tr;
380
+ if (!removeBlockIn(tr, block, isBlock)) return false;
381
+ editor.view.dispatch(tr);
382
+ return true;
383
+ },
384
+ getCursor: () => cursorIn(editor.state, isBlock),
385
+ onCursorChange(listener) {
386
+ // Focus and blur are transactions too: they change nothing a toolbar shows.
387
+ const run = ({ transaction }: { transaction: Transaction }) => {
388
+ if (transaction.docChanged || transaction.selectionSet || transaction.storedMarksSet) listener();
389
+ };
390
+ editor.on('transaction', run);
391
+ return () => editor.off('transaction', run);
392
+ },
393
+ selectedText,
394
+ focus() {
395
+ // A surface that has the focus keeps it: focusing again writes the last known cursor over a click's new one.
396
+ if (!editor.view.hasFocus()) editor.view.focus();
397
+ },
398
+ onSelectionChange(listener) {
399
+ let last = selection();
400
+ const run = () => {
401
+ const now = selection();
402
+ if (now?.id === last?.id && now?.tag === last?.tag) return;
403
+ last = now;
404
+ listener(now);
405
+ };
406
+ editor.on('selectionUpdate', run);
407
+ editor.on('update', run);
408
+ return () => {
409
+ editor.off('selectionUpdate', run);
410
+ editor.off('update', run);
411
+ };
412
+ },
413
+ style: createStyles(
414
+ editor,
415
+ isComponent,
416
+ (node) => tags.get(node.type.name) ?? childTags.get(node.type.name) ?? node.type.name,
417
+ (html) => adapter.insert(html),
418
+ ),
419
+ };
420
+ return adapter;
421
+ }