@astl/editor-tiptap 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/CHANGELOG.md +99 -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 +51 -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
package/src/surface.ts ADDED
@@ -0,0 +1,110 @@
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, EDITOR_NAMESPACE } from '@astl/editor';
8
+ import { Editor } from '@tiptap/core';
9
+ import { EditorState } from '@tiptap/pm/state';
10
+ import type { LitElement } from 'lit';
11
+ import { createTiptapAdapter } from './adapter.js';
12
+ import { BLOCK_CHANGE, type SurfaceChange } from './blocks.js';
13
+ import { astrolabeExtensions, PAGE_CHANGE, type PictureHooks } from './nodes.js';
14
+ import { adoptSurfaceStyles, SURFACE_CLASS } from './styles.js';
15
+
16
+ export interface Surface {
17
+ readonly adapter: EditorAdapter;
18
+ /** The authoring its labels come from. */
19
+ readonly authoring: Authoring;
20
+ /** Destroys the editor and removes its element. */
21
+ destroy(): void;
22
+ }
23
+
24
+ /**
25
+ * A TipTap editor on `source`, appended to `host` (the `<astl-editor>`) in the `surface` slot. `manifests` are every
26
+ * registered type: the schema keeps them all, so content of a type the authoring leaves out is never dropped. `changes`
27
+ * hears of each page added or joined and each block moved or deleted, before the change event of its edit.
28
+ */
29
+ export function createSurface(
30
+ host: LitElement,
31
+ authoring: Authoring,
32
+ manifests: readonly ComponentManifest[],
33
+ source: string,
34
+ pictures?: PictureHooks,
35
+ changes?: (change: SurfaceChange) => void,
36
+ ): Surface {
37
+ adoptSurfaceStyles(host);
38
+ const mount = document.createElement('div');
39
+ mount.slot = 'surface';
40
+ mount.className = SURFACE_CLASS;
41
+ host.append(mount);
42
+ let editor: Editor | null = null;
43
+ // A host may keep the toolbar in view (a sticky `::part(toolbar)`, the editor reference): what the editor scrolls
44
+ // into view (the caret, a picked page or element) then stops below it, not under it. Read when its size changes.
45
+ const covered = new ResizeObserver(([entry]) => {
46
+ const toolbar = entry?.target;
47
+ if (!(toolbar instanceof HTMLElement) || editor === null || editor.isDestroyed) return;
48
+ const style = getComputedStyle(toolbar);
49
+ const offset = Number.parseFloat(style.insetBlockStart);
50
+ const top = style.position === 'sticky' && !Number.isNaN(offset) ? offset + toolbar.offsetHeight : 0;
51
+ // ProseMirror's defaults on the other sides: no threshold, a 5px margin.
52
+ editor.view.setProps({
53
+ scrollThreshold: { top, right: 0, bottom: 0, left: 0 },
54
+ scrollMargin: { top: top + 5, right: 5, bottom: 5, left: 5 },
55
+ });
56
+ });
57
+ const destroy = () => {
58
+ covered.disconnect();
59
+ editor?.destroy();
60
+ mount.remove();
61
+ };
62
+ try {
63
+ editor = new Editor({
64
+ element: mount,
65
+ // No <style> element: the surface stylesheet is adopted (strict CSP allows it).
66
+ injectCSS: false,
67
+ extensions: astrolabeExtensions(authoring, manifests, pictures === undefined ? {} : { pictures }),
68
+ editorProps: {
69
+ attributes: (state) => ({
70
+ 'aria-label': authoring.t(EDITOR_NAMESPACE, 'surface.label'),
71
+ 'aria-multiline': 'true',
72
+ // The content language, for spell checking and screen readers.
73
+ lang: (state.doc.attrs.attributes as Record<string, string>).lang ?? '',
74
+ }),
75
+ handleDOMEvents: {
76
+ // Tab into the text leaves no caret (a page starts with its title field, not text), and ProseMirror only
77
+ // restores a caret the browser moved. Once the browser has placed its own: the editor's, where it was.
78
+ focus: (view) => {
79
+ setTimeout(() => {
80
+ const anchor = document.getSelection()?.anchorNode ?? null;
81
+ if (!view.isDestroyed && view.hasFocus() && (anchor === null || !view.dom.contains(anchor)))
82
+ view.focus();
83
+ });
84
+ return false;
85
+ },
86
+ },
87
+ },
88
+ });
89
+ const adapter = createTiptapAdapter(editor, manifests);
90
+ adapter.setSource(source);
91
+ // TipTap tells of a transaction before the update that its change listeners hear.
92
+ editor.on('transaction', ({ transaction }) => {
93
+ const change = (transaction.getMeta(PAGE_CHANGE) ?? transaction.getMeta(BLOCK_CHANGE)) as
94
+ | SurfaceChange
95
+ | undefined;
96
+ if (change !== undefined) changes?.(change);
97
+ });
98
+ // Loading is not an edit: a fresh state, so undo never goes back to an empty page.
99
+ editor.view.updateState(EditorState.create({ doc: editor.state.doc, plugins: editor.state.plugins }));
100
+ // The toolbar is there once the host has rendered: this may be its first update.
101
+ void host.updateComplete.then(() => {
102
+ const toolbar = host.renderRoot.querySelector('[part~="toolbar"]');
103
+ if (toolbar !== null && editor?.isDestroyed === false) covered.observe(toolbar);
104
+ });
105
+ return { adapter, authoring, destroy };
106
+ } catch (error) {
107
+ destroy();
108
+ throw error;
109
+ }
110
+ }
package/src/tiptap.ts 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 type { PictureHooks } from './nodes.js';
9
+ export { astrolabeExtensions } from './nodes.js';