@react-x11/components 0.7.1 → 0.9.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/README.md +295 -51
- package/dist/charts/node.d.ts +1 -1
- package/dist/charts/node.d.ts.map +1 -1
- package/dist/charts/node.js +6 -2
- package/dist/charts/node.js.map +1 -1
- package/dist/color-picker/ColorPicker.d.ts.map +1 -1
- package/dist/color-picker/ColorPicker.js +43 -33
- package/dist/color-picker/ColorPicker.js.map +1 -1
- package/dist/embed/client.d.ts +48 -3
- package/dist/embed/client.d.ts.map +1 -1
- package/dist/embed/client.js +79 -5
- package/dist/embed/client.js.map +1 -1
- package/dist/embed/index.d.ts +1 -1
- package/dist/embed/index.d.ts.map +1 -1
- package/dist/embed/index.js +1 -1
- package/dist/embed/index.js.map +1 -1
- package/dist/html/node.d.ts +3 -1
- package/dist/html/node.d.ts.map +1 -1
- package/dist/html/node.js +7 -5
- package/dist/html/node.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/{markdown → internal/markdown}/ast.d.ts +12 -0
- package/dist/internal/markdown/ast.d.ts.map +1 -0
- package/dist/internal/markdown/ast.js +22 -0
- package/dist/internal/markdown/ast.js.map +1 -0
- package/dist/internal/markdown/parse.d.ts.map +1 -0
- package/dist/{markdown → internal/markdown}/parse.js +112 -22
- package/dist/internal/markdown/parse.js.map +1 -0
- package/dist/internal/markdown/stringify.d.ts +4 -0
- package/dist/internal/markdown/stringify.d.ts.map +1 -0
- package/dist/internal/markdown/stringify.js +684 -0
- package/dist/internal/markdown/stringify.js.map +1 -0
- package/dist/internal/markdown/tags.d.ts.map +1 -0
- package/dist/internal/markdown/tags.js.map +1 -0
- package/dist/internal/scroll.d.ts +3 -2
- package/dist/internal/scroll.d.ts.map +1 -1
- package/dist/internal/scroll.js +33 -15
- package/dist/internal/scroll.js.map +1 -1
- package/dist/internal/units.d.ts +2 -0
- package/dist/internal/units.d.ts.map +1 -0
- package/dist/internal/units.js +35 -0
- package/dist/internal/units.js.map +1 -0
- package/dist/internal/window.d.ts.map +1 -1
- package/dist/internal/window.js +4 -2
- package/dist/internal/window.js.map +1 -1
- package/dist/maps/anchors.d.ts +98 -0
- package/dist/maps/anchors.d.ts.map +1 -0
- package/dist/maps/anchors.js +531 -0
- package/dist/maps/anchors.js.map +1 -0
- package/dist/maps/controller.d.ts +283 -0
- package/dist/maps/controller.d.ts.map +1 -0
- package/dist/maps/controller.js +743 -0
- package/dist/maps/controller.js.map +1 -0
- package/dist/maps/gl/buckets.d.ts +95 -0
- package/dist/maps/gl/buckets.d.ts.map +1 -0
- package/dist/maps/gl/buckets.js +362 -0
- package/dist/maps/gl/buckets.js.map +1 -0
- package/dist/maps/gl/build-worker.d.ts +13 -0
- package/dist/maps/gl/build-worker.d.ts.map +1 -0
- package/dist/maps/gl/build-worker.js +44 -0
- package/dist/maps/gl/build-worker.js.map +1 -0
- package/dist/maps/gl/color.d.ts +8 -0
- package/dist/maps/gl/color.d.ts.map +1 -0
- package/dist/maps/gl/color.js +57 -0
- package/dist/maps/gl/color.js.map +1 -0
- package/dist/maps/gl/cover.d.ts +51 -0
- package/dist/maps/gl/cover.d.ts.map +1 -0
- package/dist/maps/gl/cover.js +124 -0
- package/dist/maps/gl/cover.js.map +1 -0
- package/dist/maps/gl/index.d.ts +3 -0
- package/dist/maps/gl/index.d.ts.map +1 -0
- package/dist/maps/gl/index.js +20 -0
- package/dist/maps/gl/index.js.map +1 -0
- package/dist/maps/gl/markers.d.ts +28 -0
- package/dist/maps/gl/markers.d.ts.map +1 -0
- package/dist/maps/gl/markers.js +60 -0
- package/dist/maps/gl/markers.js.map +1 -0
- package/dist/maps/gl/overlays.d.ts +68 -0
- package/dist/maps/gl/overlays.d.ts.map +1 -0
- package/dist/maps/gl/overlays.js +300 -0
- package/dist/maps/gl/overlays.js.map +1 -0
- package/dist/maps/gl/pane.d.ts +12 -0
- package/dist/maps/gl/pane.d.ts.map +1 -0
- package/dist/maps/gl/pane.js +37 -0
- package/dist/maps/gl/pane.js.map +1 -0
- package/dist/maps/gl/placement.d.ts +111 -0
- package/dist/maps/gl/placement.d.ts.map +1 -0
- package/dist/maps/gl/placement.js +567 -0
- package/dist/maps/gl/placement.js.map +1 -0
- package/dist/maps/gl/renderer.d.ts +352 -0
- package/dist/maps/gl/renderer.d.ts.map +1 -0
- package/dist/maps/gl/renderer.js +1127 -0
- package/dist/maps/gl/renderer.js.map +1 -0
- package/dist/maps/gl/shaders.d.ts +120 -0
- package/dist/maps/gl/shaders.d.ts.map +1 -0
- package/dist/maps/gl/shaders.js +451 -0
- package/dist/maps/gl/shaders.js.map +1 -0
- package/dist/maps/gl/store.d.ts +115 -0
- package/dist/maps/gl/store.d.ts.map +1 -0
- package/dist/maps/gl/store.js +469 -0
- package/dist/maps/gl/store.js.map +1 -0
- package/dist/maps/gl/text.d.ts +154 -0
- package/dist/maps/gl/text.d.ts.map +1 -0
- package/dist/maps/gl/text.js +382 -0
- package/dist/maps/gl/text.js.map +1 -0
- package/dist/maps/gl/view.d.ts +54 -0
- package/dist/maps/gl/view.d.ts.map +1 -0
- package/dist/maps/gl/view.js +974 -0
- package/dist/maps/gl/view.js.map +1 -0
- package/dist/maps/index.d.ts +1 -1
- package/dist/maps/index.d.ts.map +1 -1
- package/dist/maps/index.js +216 -52
- package/dist/maps/index.js.map +1 -1
- package/dist/maps/labels.d.ts +37 -16
- package/dist/maps/labels.d.ts.map +1 -1
- package/dist/maps/labels.js +188 -119
- package/dist/maps/labels.js.map +1 -1
- package/dist/maps/node.d.ts +162 -56
- package/dist/maps/node.d.ts.map +1 -1
- package/dist/maps/node.js +607 -515
- package/dist/maps/node.js.map +1 -1
- package/dist/maps/overlay.d.ts +72 -9
- package/dist/maps/overlay.d.ts.map +1 -1
- package/dist/maps/overlay.js +108 -28
- package/dist/maps/overlay.js.map +1 -1
- package/dist/maps/paint.d.ts +3 -0
- package/dist/maps/paint.d.ts.map +1 -1
- package/dist/maps/paint.js.map +1 -1
- package/dist/maps/proj.d.ts +14 -0
- package/dist/maps/proj.d.ts.map +1 -1
- package/dist/maps/proj.js +28 -0
- package/dist/maps/proj.js.map +1 -1
- package/dist/maps/renderer.d.ts +69 -0
- package/dist/maps/renderer.d.ts.map +1 -0
- package/dist/maps/renderer.js +66 -0
- package/dist/maps/renderer.js.map +1 -0
- package/dist/maps/sources.d.ts +25 -3
- package/dist/maps/sources.d.ts.map +1 -1
- package/dist/maps/sources.js +17 -0
- package/dist/maps/sources.js.map +1 -1
- package/dist/maps/theme.d.ts +19 -0
- package/dist/maps/theme.d.ts.map +1 -0
- package/dist/maps/theme.js +44 -0
- package/dist/maps/theme.js.map +1 -0
- package/dist/maps/tiles.d.ts +117 -17
- package/dist/maps/tiles.d.ts.map +1 -1
- package/dist/maps/tiles.js +163 -30
- package/dist/maps/tiles.js.map +1 -1
- package/dist/maps/types.d.ts +188 -20
- package/dist/maps/types.d.ts.map +1 -1
- package/dist/markdown/expressions.d.ts +1 -1
- package/dist/markdown/expressions.d.ts.map +1 -1
- package/dist/markdown/expressions.js.map +1 -1
- package/dist/markdown/index.d.ts +7 -6
- package/dist/markdown/index.d.ts.map +1 -1
- package/dist/markdown/index.js +9 -7
- package/dist/markdown/index.js.map +1 -1
- package/dist/markdown/spans.d.ts +1 -1
- package/dist/markdown/spans.d.ts.map +1 -1
- package/dist/media-player/index.d.ts +11 -3
- package/dist/media-player/index.d.ts.map +1 -1
- package/dist/media-player/index.js +13 -2
- package/dist/media-player/index.js.map +1 -1
- package/dist/rich-text-editor/clipboard.d.ts +23 -0
- package/dist/rich-text-editor/clipboard.d.ts.map +1 -0
- package/dist/rich-text-editor/clipboard.js +307 -0
- package/dist/rich-text-editor/clipboard.js.map +1 -0
- package/dist/rich-text-editor/collab.d.ts +32 -0
- package/dist/rich-text-editor/collab.d.ts.map +1 -0
- package/dist/rich-text-editor/collab.js +55 -0
- package/dist/rich-text-editor/collab.js.map +1 -0
- package/dist/rich-text-editor/commands.d.ts +50 -0
- package/dist/rich-text-editor/commands.d.ts.map +1 -0
- package/dist/rich-text-editor/commands.js +376 -0
- package/dist/rich-text-editor/commands.js.map +1 -0
- package/dist/rich-text-editor/drag.d.ts +68 -0
- package/dist/rich-text-editor/drag.d.ts.map +1 -0
- package/dist/rich-text-editor/drag.js +148 -0
- package/dist/rich-text-editor/drag.js.map +1 -0
- package/dist/rich-text-editor/html.d.ts +145 -0
- package/dist/rich-text-editor/html.d.ts.map +1 -0
- package/dist/rich-text-editor/html.js +516 -0
- package/dist/rich-text-editor/html.js.map +1 -0
- package/dist/rich-text-editor/index.d.ts +263 -0
- package/dist/rich-text-editor/index.d.ts.map +1 -0
- package/dist/rich-text-editor/index.js +822 -0
- package/dist/rich-text-editor/index.js.map +1 -0
- package/dist/rich-text-editor/inline.d.ts +94 -0
- package/dist/rich-text-editor/inline.d.ts.map +1 -0
- package/dist/rich-text-editor/inline.js +252 -0
- package/dist/rich-text-editor/inline.js.map +1 -0
- package/dist/rich-text-editor/keymap.d.ts +71 -0
- package/dist/rich-text-editor/keymap.d.ts.map +1 -0
- package/dist/rich-text-editor/keymap.js +393 -0
- package/dist/rich-text-editor/keymap.js.map +1 -0
- package/dist/rich-text-editor/keys.d.ts +29 -0
- package/dist/rich-text-editor/keys.d.ts.map +1 -0
- package/dist/rich-text-editor/keys.js +93 -0
- package/dist/rich-text-editor/keys.js.map +1 -0
- package/dist/rich-text-editor/look.d.ts +71 -0
- package/dist/rich-text-editor/look.d.ts.map +1 -0
- package/dist/rich-text-editor/look.js +342 -0
- package/dist/rich-text-editor/look.js.map +1 -0
- package/dist/rich-text-editor/markdown.d.ts +18 -0
- package/dist/rich-text-editor/markdown.d.ts.map +1 -0
- package/dist/rich-text-editor/markdown.js +496 -0
- package/dist/rich-text-editor/markdown.js.map +1 -0
- package/dist/rich-text-editor/nodes.d.ts +135 -0
- package/dist/rich-text-editor/nodes.d.ts.map +1 -0
- package/dist/rich-text-editor/nodes.js +397 -0
- package/dist/rich-text-editor/nodes.js.map +1 -0
- package/dist/rich-text-editor/render.d.ts +68 -0
- package/dist/rich-text-editor/render.d.ts.map +1 -0
- package/dist/rich-text-editor/render.js +500 -0
- package/dist/rich-text-editor/render.js.map +1 -0
- package/dist/rich-text-editor/schema.d.ts +16 -0
- package/dist/rich-text-editor/schema.d.ts.map +1 -0
- package/dist/rich-text-editor/schema.js +386 -0
- package/dist/rich-text-editor/schema.js.map +1 -0
- package/dist/rich-text-editor/store.d.ts +11 -0
- package/dist/rich-text-editor/store.d.ts.map +1 -0
- package/dist/rich-text-editor/store.js +43 -0
- package/dist/rich-text-editor/store.js.map +1 -0
- package/dist/rich-text-editor/suggest.d.ts +127 -0
- package/dist/rich-text-editor/suggest.d.ts.map +1 -0
- package/dist/rich-text-editor/suggest.js +0 -0
- package/dist/rich-text-editor/suggest.js.map +1 -0
- package/dist/rich-text-editor/tables.d.ts +41 -0
- package/dist/rich-text-editor/tables.d.ts.map +1 -0
- package/dist/rich-text-editor/tables.js +204 -0
- package/dist/rich-text-editor/tables.js.map +1 -0
- package/dist/rich-text-editor/toolbar.d.ts +58 -0
- package/dist/rich-text-editor/toolbar.d.ts.map +1 -0
- package/dist/rich-text-editor/toolbar.js +548 -0
- package/dist/rich-text-editor/toolbar.js.map +1 -0
- package/dist/rich-text-editor/view.d.ts +368 -0
- package/dist/rich-text-editor/view.d.ts.map +1 -0
- package/dist/rich-text-editor/view.js +1968 -0
- package/dist/rich-text-editor/view.js.map +1 -0
- package/dist/rich-text-editor/virtual.d.ts +54 -0
- package/dist/rich-text-editor/virtual.d.ts.map +1 -0
- package/dist/rich-text-editor/virtual.js +262 -0
- package/dist/rich-text-editor/virtual.js.map +1 -0
- package/dist/richtext/node.d.ts +25 -2
- package/dist/richtext/node.d.ts.map +1 -1
- package/dist/richtext/node.js +46 -24
- package/dist/richtext/node.js.map +1 -1
- package/dist/table/index.d.ts.map +1 -1
- package/dist/table/index.js +14 -5
- package/dist/table/index.js.map +1 -1
- package/dist/terminal/backends.d.ts +4 -1
- package/dist/terminal/backends.d.ts.map +1 -1
- package/dist/terminal/backends.js.map +1 -1
- package/dist/terminal/index.d.ts +18 -7
- package/dist/terminal/index.d.ts.map +1 -1
- package/dist/terminal/index.js +28 -11
- package/dist/terminal/index.js.map +1 -1
- package/dist/terminal/vt/node.d.ts +56 -3
- package/dist/terminal/vt/node.d.ts.map +1 -1
- package/dist/terminal/vt/node.js +106 -26
- package/dist/terminal/vt/node.js.map +1 -1
- package/dist/terminal/vt/renderer.d.ts +4 -0
- package/dist/terminal/vt/renderer.d.ts.map +1 -1
- package/dist/terminal/vt/renderer.js +14 -5
- package/dist/terminal/vt/renderer.js.map +1 -1
- package/dist/tree/index.d.ts.map +1 -1
- package/dist/tree/index.js +6 -4
- package/dist/tree/index.js.map +1 -1
- package/package.json +23 -4
- package/src/charts/node.ts +6 -2
- package/src/color-picker/ColorPicker.ts +107 -81
- package/src/embed/client.ts +102 -8
- package/src/embed/index.ts +2 -0
- package/src/html/node.ts +7 -5
- package/src/index.ts +2 -0
- package/src/{markdown → internal/markdown}/ast.ts +24 -7
- package/src/{markdown → internal/markdown}/parse.ts +134 -24
- package/src/internal/markdown/stringify.ts +772 -0
- package/src/internal/scroll.ts +39 -18
- package/src/internal/units.ts +34 -0
- package/src/internal/window.ts +4 -2
- package/src/maps/anchors.ts +607 -0
- package/src/maps/controller.ts +999 -0
- package/src/maps/gl/buckets.ts +457 -0
- package/src/maps/gl/build-worker.ts +55 -0
- package/src/maps/gl/color.ts +62 -0
- package/src/maps/gl/cover.ts +170 -0
- package/src/maps/gl/index.ts +23 -0
- package/src/maps/gl/markers.ts +83 -0
- package/src/maps/gl/overlays.ts +379 -0
- package/src/maps/gl/pane.ts +42 -0
- package/src/maps/gl/placement.ts +816 -0
- package/src/maps/gl/renderer.ts +1576 -0
- package/src/maps/gl/shaders.ts +486 -0
- package/src/maps/gl/store.ts +566 -0
- package/src/maps/gl/text.ts +495 -0
- package/src/maps/gl/view.ts +1224 -0
- package/src/maps/index.ts +270 -81
- package/src/maps/labels.ts +214 -121
- package/src/maps/node.ts +763 -629
- package/src/maps/overlay.ts +148 -43
- package/src/maps/paint.ts +3 -0
- package/src/maps/proj.ts +31 -0
- package/src/maps/renderer.ts +155 -0
- package/src/maps/sources.ts +38 -3
- package/src/maps/theme.ts +51 -0
- package/src/maps/tiles.ts +191 -33
- package/src/maps/types.ts +188 -20
- package/src/markdown/expressions.ts +5 -1
- package/src/markdown/index.ts +14 -11
- package/src/markdown/spans.ts +1 -1
- package/src/media-player/index.ts +18 -4
- package/src/rich-text-editor/clipboard.ts +439 -0
- package/src/rich-text-editor/collab.ts +91 -0
- package/src/rich-text-editor/commands.ts +448 -0
- package/src/rich-text-editor/dom-globals.d.ts +23 -0
- package/src/rich-text-editor/drag.ts +211 -0
- package/src/rich-text-editor/html.ts +619 -0
- package/src/rich-text-editor/index.ts +1331 -0
- package/src/rich-text-editor/inline.ts +345 -0
- package/src/rich-text-editor/keymap.ts +510 -0
- package/src/rich-text-editor/keys.ts +119 -0
- package/src/rich-text-editor/look.ts +427 -0
- package/src/rich-text-editor/markdown.ts +653 -0
- package/src/rich-text-editor/nodes.ts +497 -0
- package/src/rich-text-editor/render.ts +800 -0
- package/src/rich-text-editor/schema.ts +431 -0
- package/src/rich-text-editor/store.ts +39 -0
- package/src/rich-text-editor/suggest.ts +597 -0
- package/src/rich-text-editor/tables.ts +272 -0
- package/src/rich-text-editor/toolbar.ts +724 -0
- package/src/rich-text-editor/view.ts +2287 -0
- package/src/rich-text-editor/virtual.ts +347 -0
- package/src/richtext/node.ts +59 -30
- package/src/table/index.ts +14 -5
- package/src/terminal/backends.ts +4 -1
- package/src/terminal/index.ts +43 -15
- package/src/terminal/vt/node.ts +128 -29
- package/src/terminal/vt/renderer.ts +27 -5
- package/src/tree/index.ts +6 -4
- package/dist/markdown/ast.d.ts.map +0 -1
- package/dist/markdown/ast.js +0 -17
- package/dist/markdown/ast.js.map +0 -1
- package/dist/markdown/parse.d.ts.map +0 -1
- package/dist/markdown/parse.js.map +0 -1
- package/dist/markdown/tags.d.ts.map +0 -1
- package/dist/markdown/tags.js.map +0 -1
- /package/dist/{markdown → internal/markdown}/parse.d.ts +0 -0
- /package/dist/{markdown → internal/markdown}/tags.d.ts +0 -0
- /package/dist/{markdown → internal/markdown}/tags.js +0 -0
- /package/src/{markdown → internal/markdown}/tags.ts +0 -0
|
@@ -0,0 +1,800 @@
|
|
|
1
|
+
// The document, rendered: one React element per block, keyed by the view's
|
|
2
|
+
// block keys, composed from `<box>` and the editor's own text element.
|
|
3
|
+
//
|
|
4
|
+
// **Composition, not a drawn document**, by the rule AGENTS.md sets for the
|
|
5
|
+
// question: the viewport is a scroll rather than a transform, and a
|
|
6
|
+
// document's block flow — paragraphs, lists, quotes, a table's rows — is
|
|
7
|
+
// what flexbox already lays out, which is how `<Markdown>` draws the same
|
|
8
|
+
// content. What composition buys an editor in particular is the extension
|
|
9
|
+
// seam: a node view is a React component in the flow, not a picture of one.
|
|
10
|
+
//
|
|
11
|
+
// **What re-renders, and why nothing else does.** A block is a memoized
|
|
12
|
+
// component keyed by its node's *identity* — ProseMirror shares every node
|
|
13
|
+
// an edit did not touch, so typing in one paragraph re-renders that
|
|
14
|
+
// paragraph and the chain of containers above it, and nothing else. A block
|
|
15
|
+
// never receives its position: positions shift on every keystroke before
|
|
16
|
+
// them, so a block asks the view for its position when it needs one
|
|
17
|
+
// (`getPos`). Decorations and a node selection reach a block through its own
|
|
18
|
+
// subscription (`./store.ts`), so neither re-renders the document.
|
|
19
|
+
import React, { useCallback, useMemo, useSyncExternalStore } from 'react';
|
|
20
|
+
import type { ComponentType, ReactElement, ReactNode } from 'react';
|
|
21
|
+
import { Icon } from 'react-x11';
|
|
22
|
+
import type { DrawnNode, MouseEvent as X11MouseEvent } from 'react-x11';
|
|
23
|
+
import type { Style } from 'react-x11/style';
|
|
24
|
+
import type { Node as PMNode } from 'prosemirror-model';
|
|
25
|
+
|
|
26
|
+
import { codeBlockStyle, codeTextStyle } from '../codeblock/index.js';
|
|
27
|
+
import { hx } from '../internal/hx.js';
|
|
28
|
+
import { setTaskChecked } from './commands.js';
|
|
29
|
+
import { buildInline } from './inline.js';
|
|
30
|
+
import type { InlineDecoration, InlineWidget } from './inline.js';
|
|
31
|
+
import { domTagOf } from './look.js';
|
|
32
|
+
import type { BlockContext, EditorLook } from './look.js';
|
|
33
|
+
import { TEXT_ELEMENT } from './nodes.js';
|
|
34
|
+
import type { KeyedStore } from './store.js';
|
|
35
|
+
import type { BlockDecorations, RichEditorView } from './view.js';
|
|
36
|
+
|
|
37
|
+
const h = React.createElement;
|
|
38
|
+
|
|
39
|
+
/** What an image standing alone in a paragraph is handed to render — the
|
|
40
|
+
* editor draws no remote bitmap, the same line `<Markdown>` holds. */
|
|
41
|
+
export interface ImageInfo {
|
|
42
|
+
src: string;
|
|
43
|
+
alt: string | null;
|
|
44
|
+
title: string | null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** What a node view is given. */
|
|
48
|
+
export interface NodeViewProps {
|
|
49
|
+
node: PMNode;
|
|
50
|
+
/** Where the node is now. A function, because a node's position moves
|
|
51
|
+
* with every edit before it and its view is not re-rendered for that. */
|
|
52
|
+
getPos(): number | undefined;
|
|
53
|
+
/** A node selection has selected this node. */
|
|
54
|
+
selected: boolean;
|
|
55
|
+
/** The node's content, rendered — editable text for a textblock, blocks
|
|
56
|
+
* for a container — for the view to place where it goes. Absent for a
|
|
57
|
+
* leaf, which has none. */
|
|
58
|
+
children?: ReactNode;
|
|
59
|
+
/** Merge attributes into the node — a checkbox's state, an embed's size. */
|
|
60
|
+
updateAttributes(attrs: Record<string, unknown>): void;
|
|
61
|
+
editable: boolean;
|
|
62
|
+
view: RichEditorView;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** What a table measures its cells with: the app's font manager, whose
|
|
66
|
+
* `layout` takes runs in the logical sizes styles are written in and
|
|
67
|
+
* answers in the same units. Null on the mock backend, which has none. */
|
|
68
|
+
export interface CellFonts {
|
|
69
|
+
layout(
|
|
70
|
+
runs: readonly object[],
|
|
71
|
+
style: { family?: string; size?: number },
|
|
72
|
+
): { width: number };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Everything a block renders with. One object per look and seam set, so a
|
|
76
|
+
* memoized block compares it by identity. */
|
|
77
|
+
export interface RenderContext {
|
|
78
|
+
view: RichEditorView;
|
|
79
|
+
look: EditorLook;
|
|
80
|
+
editable: boolean;
|
|
81
|
+
fonts?: CellFonts | null;
|
|
82
|
+
nodeViews?: Readonly<Record<string, ComponentType<NodeViewProps>>>;
|
|
83
|
+
renderImage?: (image: ImageInfo) => ReactNode;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const PLAIN: BlockContext = { quote: false, header: false };
|
|
87
|
+
const QUOTED: BlockContext = { quote: true, header: false };
|
|
88
|
+
const HEADER: BlockContext = { quote: false, header: true };
|
|
89
|
+
const QUOTED_HEADER: BlockContext = { quote: true, header: true };
|
|
90
|
+
|
|
91
|
+
const NO_DECORATIONS: readonly InlineDecoration[] = [];
|
|
92
|
+
const NO_WIDGETS: readonly InlineWidget[] = [];
|
|
93
|
+
|
|
94
|
+
function useKeyed<T>(store: KeyedStore<T>, key: string): T | undefined {
|
|
95
|
+
const subscribe = useCallback(
|
|
96
|
+
(fn: () => void) => store.subscribe(key, fn),
|
|
97
|
+
[store, key],
|
|
98
|
+
);
|
|
99
|
+
const get = useCallback(() => store.get(key), [store, key]);
|
|
100
|
+
return useSyncExternalStore(subscribe, get, get);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
type Role =
|
|
104
|
+
| 'textblock'
|
|
105
|
+
| 'quote'
|
|
106
|
+
| 'list'
|
|
107
|
+
| 'table'
|
|
108
|
+
| 'rule'
|
|
109
|
+
| 'image'
|
|
110
|
+
| 'atom'
|
|
111
|
+
| 'container';
|
|
112
|
+
|
|
113
|
+
/** What a block is, for drawing: by name (the reference schema's, then
|
|
114
|
+
* TipTap's), then by what its own `toDOM` says it is. */
|
|
115
|
+
function roleOf(node: PMNode): Role {
|
|
116
|
+
if (node.isTextblock) return 'textblock';
|
|
117
|
+
const name = node.type.name;
|
|
118
|
+
const table = node.type.spec.tableRole;
|
|
119
|
+
if (table === 'table') return 'table';
|
|
120
|
+
switch (name) {
|
|
121
|
+
case 'blockquote':
|
|
122
|
+
return 'quote';
|
|
123
|
+
case 'bullet_list':
|
|
124
|
+
case 'ordered_list':
|
|
125
|
+
case 'bulletList':
|
|
126
|
+
case 'orderedList':
|
|
127
|
+
case 'taskList':
|
|
128
|
+
return 'list';
|
|
129
|
+
case 'horizontal_rule':
|
|
130
|
+
case 'horizontalRule':
|
|
131
|
+
return 'rule';
|
|
132
|
+
case 'image':
|
|
133
|
+
return 'image';
|
|
134
|
+
}
|
|
135
|
+
const tag = domTagOf(node.type.spec, node)?.tag;
|
|
136
|
+
if (tag === 'blockquote') return 'quote';
|
|
137
|
+
if (tag === 'ul' || tag === 'ol') return 'list';
|
|
138
|
+
if (tag === 'hr') return 'rule';
|
|
139
|
+
if (tag === 'img') return 'image';
|
|
140
|
+
if (tag === 'table') return 'table';
|
|
141
|
+
return node.isLeaf ? 'atom' : 'container';
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function isOrdered(list: PMNode): boolean {
|
|
145
|
+
const name = list.type.name;
|
|
146
|
+
if (name === 'ordered_list' || name === 'orderedList') return true;
|
|
147
|
+
return domTagOf(list.type.spec, list)?.tag === 'ol';
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** The blocks inside `parent`, whose content starts at `contentStart`. */
|
|
151
|
+
export function renderBlocks(
|
|
152
|
+
parent: PMNode,
|
|
153
|
+
contentStart: number,
|
|
154
|
+
ctx: RenderContext,
|
|
155
|
+
bctx: BlockContext = PLAIN,
|
|
156
|
+
): ReactNode[] {
|
|
157
|
+
const out: ReactNode[] = [];
|
|
158
|
+
parent.forEach((child, offset) => {
|
|
159
|
+
const pos = contentStart + offset;
|
|
160
|
+
const key = ctx.view.keys.keyAt(pos) ?? `p${pos}`;
|
|
161
|
+
out.push(h(BlockView, { key, blockKey: key, node: child, ctx, bctx }));
|
|
162
|
+
});
|
|
163
|
+
return out;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
interface BlockProps {
|
|
167
|
+
blockKey: string;
|
|
168
|
+
node: PMNode;
|
|
169
|
+
ctx: RenderContext;
|
|
170
|
+
bctx: BlockContext;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function sameBlock(a: BlockProps, b: BlockProps): boolean {
|
|
174
|
+
return (
|
|
175
|
+
a.node === b.node &&
|
|
176
|
+
a.ctx === b.ctx &&
|
|
177
|
+
a.blockKey === b.blockKey &&
|
|
178
|
+
a.bctx === b.bctx
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** A block, of any kind. */
|
|
183
|
+
const BlockView = React.memo(function BlockView(
|
|
184
|
+
props: BlockProps,
|
|
185
|
+
): ReactElement | null {
|
|
186
|
+
const { node, blockKey, ctx, bctx } = props;
|
|
187
|
+
const selected = useKeyed(ctx.view.selectedBlocks, blockKey) === true;
|
|
188
|
+
const custom = ctx.nodeViews?.[node.type.name];
|
|
189
|
+
if (custom) return h(NodeViewHost, { ...props, selected, component: custom });
|
|
190
|
+
switch (roleOf(node)) {
|
|
191
|
+
case 'textblock':
|
|
192
|
+
return h(TextblockView, props);
|
|
193
|
+
case 'quote':
|
|
194
|
+
return hx(
|
|
195
|
+
'box',
|
|
196
|
+
{
|
|
197
|
+
style: {
|
|
198
|
+
flexDirection: 'column',
|
|
199
|
+
gap: ctx.look.blockGap,
|
|
200
|
+
borderLeftWidth: 3,
|
|
201
|
+
borderLeftColor: ctx.look.border,
|
|
202
|
+
paddingLeft: 10,
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
...renderBlocks(
|
|
206
|
+
node,
|
|
207
|
+
startOf(ctx, blockKey),
|
|
208
|
+
ctx,
|
|
209
|
+
bctx.header ? QUOTED_HEADER : QUOTED,
|
|
210
|
+
),
|
|
211
|
+
);
|
|
212
|
+
case 'list':
|
|
213
|
+
return h(ListView, props);
|
|
214
|
+
case 'table':
|
|
215
|
+
return h(TableView, props);
|
|
216
|
+
case 'rule':
|
|
217
|
+
return h(
|
|
218
|
+
AtomBox,
|
|
219
|
+
{ blockKey, ctx, selected, style: { paddingTop: 6, paddingBottom: 6 } },
|
|
220
|
+
hx('box', { style: { height: 1, backgroundColor: ctx.look.border } }),
|
|
221
|
+
);
|
|
222
|
+
case 'image':
|
|
223
|
+
return h(AtomBox, { blockKey, ctx, selected }, imageContent(node, ctx));
|
|
224
|
+
case 'atom':
|
|
225
|
+
return h(
|
|
226
|
+
AtomBox,
|
|
227
|
+
{ blockKey, ctx, selected },
|
|
228
|
+
hx(
|
|
229
|
+
'text',
|
|
230
|
+
{ style: { color: ctx.look.muted, fontSize: ctx.look.size } },
|
|
231
|
+
node.type.spec.leafText?.(node) || node.type.name,
|
|
232
|
+
),
|
|
233
|
+
);
|
|
234
|
+
default:
|
|
235
|
+
return hx(
|
|
236
|
+
'box',
|
|
237
|
+
{ style: { flexDirection: 'column', gap: ctx.look.blockGap } },
|
|
238
|
+
...renderBlocks(node, startOf(ctx, blockKey), ctx, bctx),
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
}, sameBlock);
|
|
242
|
+
|
|
243
|
+
/** Where a block's content starts, now. */
|
|
244
|
+
function startOf(ctx: RenderContext, key: string): number {
|
|
245
|
+
return (ctx.view.keys.posOf(key) ?? -1) + 1;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** A top-level block of a long document, in the box its height is measured
|
|
249
|
+
* by (./virtual.ts). */
|
|
250
|
+
interface RowProps extends BlockProps {
|
|
251
|
+
at: number;
|
|
252
|
+
register: (key: string, at: number, node: unknown) => void;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const ROW_STYLE = { flexDirection: 'column', flexShrink: 0 } as const;
|
|
256
|
+
|
|
257
|
+
const BlockRow = React.memo(
|
|
258
|
+
function BlockRow(props: RowProps): ReactElement {
|
|
259
|
+
const { blockKey, at, register } = props;
|
|
260
|
+
const ref = useCallback(
|
|
261
|
+
(node: unknown) => register(blockKey, at, node),
|
|
262
|
+
[register, blockKey, at],
|
|
263
|
+
);
|
|
264
|
+
return hx(
|
|
265
|
+
'box',
|
|
266
|
+
{ ref, style: ROW_STYLE },
|
|
267
|
+
h(BlockView, {
|
|
268
|
+
blockKey,
|
|
269
|
+
node: props.node,
|
|
270
|
+
ctx: props.ctx,
|
|
271
|
+
bctx: props.bctx,
|
|
272
|
+
}),
|
|
273
|
+
);
|
|
274
|
+
},
|
|
275
|
+
(a, b) => sameBlock(a, b) && a.at === b.at && a.register === b.register,
|
|
276
|
+
);
|
|
277
|
+
|
|
278
|
+
/** The top-level blocks `first` to `last` (exclusive) of a long document —
|
|
279
|
+
* the window ./virtual.ts keeps — each in a box of its own to measure. */
|
|
280
|
+
export function renderBlockRange(
|
|
281
|
+
doc: PMNode,
|
|
282
|
+
first: number,
|
|
283
|
+
last: number,
|
|
284
|
+
ctx: RenderContext,
|
|
285
|
+
register: RowProps['register'],
|
|
286
|
+
): ReactNode[] {
|
|
287
|
+
const out: ReactNode[] = [];
|
|
288
|
+
doc.forEach((child, offset, index) => {
|
|
289
|
+
if (index < first || index >= last) return;
|
|
290
|
+
const key = ctx.view.keys.keyAt(offset) ?? `p${offset}`;
|
|
291
|
+
out.push(
|
|
292
|
+
h(BlockRow, {
|
|
293
|
+
key,
|
|
294
|
+
blockKey: key,
|
|
295
|
+
node: child,
|
|
296
|
+
ctx,
|
|
297
|
+
bctx: PLAIN,
|
|
298
|
+
at: index,
|
|
299
|
+
register,
|
|
300
|
+
}),
|
|
301
|
+
);
|
|
302
|
+
});
|
|
303
|
+
return out;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function imageContent(node: PMNode, ctx: RenderContext): ReactNode {
|
|
307
|
+
const info: ImageInfo = {
|
|
308
|
+
src: String(node.attrs.src ?? ''),
|
|
309
|
+
alt: typeof node.attrs.alt === 'string' ? node.attrs.alt : null,
|
|
310
|
+
title: typeof node.attrs.title === 'string' ? node.attrs.title : null,
|
|
311
|
+
};
|
|
312
|
+
const drawn = ctx.renderImage?.(info);
|
|
313
|
+
if (drawn != null) return drawn;
|
|
314
|
+
return hx(
|
|
315
|
+
'box',
|
|
316
|
+
{
|
|
317
|
+
style: {
|
|
318
|
+
borderWidth: 1,
|
|
319
|
+
borderColor: ctx.look.border,
|
|
320
|
+
borderRadius: 4,
|
|
321
|
+
padding: 8,
|
|
322
|
+
alignSelf: 'flex-start',
|
|
323
|
+
},
|
|
324
|
+
},
|
|
325
|
+
hx(
|
|
326
|
+
'text',
|
|
327
|
+
{ style: { color: ctx.look.muted, fontSize: ctx.look.size } },
|
|
328
|
+
info.alt || info.src || 'image',
|
|
329
|
+
),
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
interface AtomBoxProps {
|
|
334
|
+
blockKey: string;
|
|
335
|
+
ctx: RenderContext;
|
|
336
|
+
selected: boolean;
|
|
337
|
+
style?: Style;
|
|
338
|
+
children?: ReactNode;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/** A block atom: registered with the view, so a press on it selects it, and
|
|
342
|
+
* outlined while it is selected. */
|
|
343
|
+
function AtomBox({
|
|
344
|
+
blockKey,
|
|
345
|
+
ctx,
|
|
346
|
+
selected,
|
|
347
|
+
style,
|
|
348
|
+
children,
|
|
349
|
+
}: AtomBoxProps): ReactElement {
|
|
350
|
+
const ref = useCallback(
|
|
351
|
+
(node: unknown) =>
|
|
352
|
+
ctx.view.attachBox(blockKey, (node as DrawnNode | null) ?? null),
|
|
353
|
+
[ctx.view, blockKey],
|
|
354
|
+
);
|
|
355
|
+
return hx(
|
|
356
|
+
'box',
|
|
357
|
+
{
|
|
358
|
+
ref,
|
|
359
|
+
style: {
|
|
360
|
+
flexDirection: 'column',
|
|
361
|
+
borderWidth: 2,
|
|
362
|
+
borderRadius: 4,
|
|
363
|
+
borderColor: selected ? ctx.look.accent : 'transparent',
|
|
364
|
+
...style,
|
|
365
|
+
},
|
|
366
|
+
},
|
|
367
|
+
children,
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** A textblock: the editor's text element, and what its kind wraps it in. */
|
|
372
|
+
function TextblockView({
|
|
373
|
+
node,
|
|
374
|
+
blockKey,
|
|
375
|
+
ctx,
|
|
376
|
+
bctx,
|
|
377
|
+
}: BlockProps): ReactElement {
|
|
378
|
+
const decorations = useKeyed(ctx.view.decorations, blockKey);
|
|
379
|
+
const look = ctx.look.inline(node, bctx);
|
|
380
|
+
const built = useMemo(
|
|
381
|
+
() =>
|
|
382
|
+
buildInline(
|
|
383
|
+
node,
|
|
384
|
+
look,
|
|
385
|
+
decorations?.inline ?? NO_DECORATIONS,
|
|
386
|
+
decorations?.widgets ?? NO_WIDGETS,
|
|
387
|
+
),
|
|
388
|
+
[node, look, decorations],
|
|
389
|
+
);
|
|
390
|
+
const code = !!look.code;
|
|
391
|
+
const align = node.attrs.align;
|
|
392
|
+
const text = h(TEXT_ELEMENT, {
|
|
393
|
+
runs: built.runs,
|
|
394
|
+
map: built.map,
|
|
395
|
+
node,
|
|
396
|
+
blockKey,
|
|
397
|
+
host: ctx.view,
|
|
398
|
+
style: {
|
|
399
|
+
...(code ? codeTextStyle(ctx.look.code) : null),
|
|
400
|
+
...(align === 'left' || align === 'center' || align === 'right'
|
|
401
|
+
? { textAlign: align }
|
|
402
|
+
: null),
|
|
403
|
+
flexShrink: 1,
|
|
404
|
+
},
|
|
405
|
+
});
|
|
406
|
+
const box = decorations?.box;
|
|
407
|
+
let out: ReactElement = text;
|
|
408
|
+
if (code) {
|
|
409
|
+
out = hx(
|
|
410
|
+
'box',
|
|
411
|
+
{ style: { ...codeBlockStyle(ctx.look.code), flexDirection: 'column' } },
|
|
412
|
+
text,
|
|
413
|
+
);
|
|
414
|
+
} else if (
|
|
415
|
+
node.type.name === 'heading' ||
|
|
416
|
+
/^h[1-6]$/.test(domTagOf(node.type.spec, node)?.tag ?? '')
|
|
417
|
+
) {
|
|
418
|
+
out = hx(
|
|
419
|
+
'box',
|
|
420
|
+
{
|
|
421
|
+
style: {
|
|
422
|
+
flexDirection: 'column',
|
|
423
|
+
paddingTop: Math.round(ctx.look.blockGap * 0.4),
|
|
424
|
+
},
|
|
425
|
+
},
|
|
426
|
+
text,
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
// an image alone in its paragraph is shown, when the app says how
|
|
430
|
+
const only = node.childCount === 1 ? node.firstChild : null;
|
|
431
|
+
if (only?.type.name === 'image' && ctx.renderImage) {
|
|
432
|
+
const drawn = ctx.renderImage({
|
|
433
|
+
src: String(only.attrs.src ?? ''),
|
|
434
|
+
alt: typeof only.attrs.alt === 'string' ? only.attrs.alt : null,
|
|
435
|
+
title: typeof only.attrs.title === 'string' ? only.attrs.title : null,
|
|
436
|
+
});
|
|
437
|
+
if (drawn != null) {
|
|
438
|
+
out = hx(
|
|
439
|
+
'box',
|
|
440
|
+
{ style: { flexDirection: 'column', gap: 4 } },
|
|
441
|
+
drawn,
|
|
442
|
+
out,
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
if (box?.background) {
|
|
447
|
+
out = hx(
|
|
448
|
+
'box',
|
|
449
|
+
{
|
|
450
|
+
style: {
|
|
451
|
+
flexDirection: 'column',
|
|
452
|
+
backgroundColor: box.background,
|
|
453
|
+
borderRadius: 3,
|
|
454
|
+
},
|
|
455
|
+
},
|
|
456
|
+
out,
|
|
457
|
+
);
|
|
458
|
+
}
|
|
459
|
+
return out;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/** A list: its items, each a marker and its blocks. The marker is chrome —
|
|
463
|
+
* a number, a bullet, or a task's box, which toggles. */
|
|
464
|
+
function ListView({ node, blockKey, ctx, bctx }: BlockProps): ReactElement {
|
|
465
|
+
const view = ctx.view;
|
|
466
|
+
const look = ctx.look;
|
|
467
|
+
const ordered = isOrdered(node);
|
|
468
|
+
const start = Number(node.attrs.order ?? node.attrs.start ?? 1);
|
|
469
|
+
const tight = node.attrs.tight !== false;
|
|
470
|
+
const contentStart = startOf(ctx, blockKey);
|
|
471
|
+
const last = start + node.childCount - 1;
|
|
472
|
+
const markerWidth = ordered
|
|
473
|
+
? Math.max(String(last).length, 1) * Math.ceil(look.size * 0.62) + 10
|
|
474
|
+
: Math.round(look.size * 1.15);
|
|
475
|
+
const items: ReactNode[] = [];
|
|
476
|
+
node.forEach((item, offset, index) => {
|
|
477
|
+
const pos = contentStart + offset;
|
|
478
|
+
const key = view.keys.keyAt(pos) ?? `i${pos}`;
|
|
479
|
+
items.push(
|
|
480
|
+
h(ItemView, {
|
|
481
|
+
key,
|
|
482
|
+
blockKey: key,
|
|
483
|
+
node: item,
|
|
484
|
+
ctx,
|
|
485
|
+
bctx,
|
|
486
|
+
marker: ordered ? `${start + index}.` : '•',
|
|
487
|
+
markerWidth,
|
|
488
|
+
}),
|
|
489
|
+
);
|
|
490
|
+
});
|
|
491
|
+
return hx(
|
|
492
|
+
'box',
|
|
493
|
+
{
|
|
494
|
+
style: {
|
|
495
|
+
flexDirection: 'column',
|
|
496
|
+
gap: tight ? Math.round(look.blockGap * 0.25) : look.blockGap,
|
|
497
|
+
},
|
|
498
|
+
},
|
|
499
|
+
...items,
|
|
500
|
+
);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
interface ItemProps extends BlockProps {
|
|
504
|
+
marker: string;
|
|
505
|
+
markerWidth: number;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
const ItemView = React.memo(
|
|
509
|
+
function ItemView({
|
|
510
|
+
node,
|
|
511
|
+
blockKey,
|
|
512
|
+
ctx,
|
|
513
|
+
bctx,
|
|
514
|
+
marker,
|
|
515
|
+
markerWidth,
|
|
516
|
+
}: ItemProps): ReactElement {
|
|
517
|
+
const look = ctx.look;
|
|
518
|
+
const checked = node.attrs.checked;
|
|
519
|
+
const toggle = useCallback(
|
|
520
|
+
(ev: X11MouseEvent) => {
|
|
521
|
+
// the box is the item's own control: the press is not a caret's
|
|
522
|
+
ev.preventDefault();
|
|
523
|
+
ev.stopPropagation();
|
|
524
|
+
if (!ctx.editable) return;
|
|
525
|
+
const pos = ctx.view.keys.posOf(blockKey);
|
|
526
|
+
const current = ctx.view.keys.nodeOf(blockKey);
|
|
527
|
+
if (pos !== undefined && current)
|
|
528
|
+
ctx.view.run(setTaskChecked(pos, !current.attrs.checked));
|
|
529
|
+
},
|
|
530
|
+
[ctx, blockKey],
|
|
531
|
+
);
|
|
532
|
+
const markerBox =
|
|
533
|
+
typeof checked === 'boolean'
|
|
534
|
+
? hx(
|
|
535
|
+
'box',
|
|
536
|
+
{
|
|
537
|
+
style: {
|
|
538
|
+
width: markerWidth,
|
|
539
|
+
flexShrink: 0,
|
|
540
|
+
flexDirection: 'row',
|
|
541
|
+
},
|
|
542
|
+
onMouseDown: toggle,
|
|
543
|
+
role: 'checkbox',
|
|
544
|
+
'aria-checked': checked,
|
|
545
|
+
},
|
|
546
|
+
hx(
|
|
547
|
+
'box',
|
|
548
|
+
{
|
|
549
|
+
style: {
|
|
550
|
+
width: 12,
|
|
551
|
+
height: 12,
|
|
552
|
+
borderWidth: 1,
|
|
553
|
+
borderColor: checked ? look.accent : look.border,
|
|
554
|
+
borderRadius: 3,
|
|
555
|
+
backgroundColor: checked ? look.accent : undefined,
|
|
556
|
+
alignItems: 'center',
|
|
557
|
+
justifyContent: 'center',
|
|
558
|
+
marginTop: Math.round(look.size * 0.25),
|
|
559
|
+
cursor: 'default',
|
|
560
|
+
},
|
|
561
|
+
},
|
|
562
|
+
checked
|
|
563
|
+
? h(Icon, { name: 'check', size: 10, color: look.accentText })
|
|
564
|
+
: null,
|
|
565
|
+
),
|
|
566
|
+
)
|
|
567
|
+
: hx(
|
|
568
|
+
'box',
|
|
569
|
+
{
|
|
570
|
+
style: {
|
|
571
|
+
width: markerWidth,
|
|
572
|
+
flexShrink: 0,
|
|
573
|
+
flexDirection: 'row',
|
|
574
|
+
},
|
|
575
|
+
},
|
|
576
|
+
hx(
|
|
577
|
+
'text',
|
|
578
|
+
{
|
|
579
|
+
style: {
|
|
580
|
+
color: look.muted,
|
|
581
|
+
fontSize: look.size,
|
|
582
|
+
fontFamily: look.family,
|
|
583
|
+
},
|
|
584
|
+
},
|
|
585
|
+
marker,
|
|
586
|
+
),
|
|
587
|
+
);
|
|
588
|
+
return hx(
|
|
589
|
+
'box',
|
|
590
|
+
{ style: { flexDirection: 'row' } },
|
|
591
|
+
markerBox,
|
|
592
|
+
hx(
|
|
593
|
+
'box',
|
|
594
|
+
{
|
|
595
|
+
style: {
|
|
596
|
+
flexDirection: 'column',
|
|
597
|
+
gap: Math.round(look.blockGap * 0.4),
|
|
598
|
+
flexGrow: 1,
|
|
599
|
+
flexShrink: 1,
|
|
600
|
+
},
|
|
601
|
+
},
|
|
602
|
+
...renderBlocks(node, startOf(ctx, blockKey), ctx, bctx),
|
|
603
|
+
),
|
|
604
|
+
);
|
|
605
|
+
},
|
|
606
|
+
(a, b) =>
|
|
607
|
+
sameBlock(a, b) && a.marker === b.marker && a.markerWidth === b.markerWidth,
|
|
608
|
+
);
|
|
609
|
+
|
|
610
|
+
/** What a cell's text measures, kept by the cell node and the look it was
|
|
611
|
+
* measured in. ProseMirror shares every node an edit did not touch, so
|
|
612
|
+
* typing in a cell measures that cell again and no other. */
|
|
613
|
+
const cellWidths = new WeakMap<PMNode, { key: string; width: number }>();
|
|
614
|
+
|
|
615
|
+
/** A textblock's text, laid out on one line, in logical pixels. */
|
|
616
|
+
function lineWidth(
|
|
617
|
+
block: PMNode,
|
|
618
|
+
ctx: RenderContext,
|
|
619
|
+
bctx: BlockContext,
|
|
620
|
+
): number {
|
|
621
|
+
if (!ctx.fonts) return block.textContent.length * ctx.look.size * 0.55;
|
|
622
|
+
const inline = ctx.look.inline(block, bctx);
|
|
623
|
+
const { runs } = buildInline(block, inline);
|
|
624
|
+
return ctx.fonts.layout(runs, {
|
|
625
|
+
family: inline.base.family,
|
|
626
|
+
size: inline.base.size,
|
|
627
|
+
}).width;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
function cellWidth(
|
|
631
|
+
cell: PMNode,
|
|
632
|
+
ctx: RenderContext,
|
|
633
|
+
bctx: BlockContext,
|
|
634
|
+
): number {
|
|
635
|
+
const key = `${ctx.look.key}|${bctx.header ? 'h' : ''}${bctx.quote ? 'q' : ''}|${ctx.fonts ? 'f' : ''}`;
|
|
636
|
+
const hit = cellWidths.get(cell);
|
|
637
|
+
if (hit && hit.key === key) return hit.width;
|
|
638
|
+
let width = 0;
|
|
639
|
+
if (cell.isTextblock) width = lineWidth(cell, ctx, bctx);
|
|
640
|
+
else {
|
|
641
|
+
// a schema whose cells hold blocks: the widest of them
|
|
642
|
+
cell.descendants((node) => {
|
|
643
|
+
if (!node.isTextblock) return true;
|
|
644
|
+
width = Math.max(width, lineWidth(node, ctx, bctx));
|
|
645
|
+
return false;
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
cellWidths.set(cell, { key, width });
|
|
649
|
+
return width;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
/**
|
|
653
|
+
* A table: rows of cells, each column as wide as its widest cell — capped,
|
|
654
|
+
* so one long cell cannot starve the rest; `<Markdown>`'s rule, so a table
|
|
655
|
+
* looks alike in both. When the table is wider than the document, every
|
|
656
|
+
* column gives up the same share and its text wraps. The rows are separate
|
|
657
|
+
* boxes and still line up, because every row's cells get the same basis and
|
|
658
|
+
* shrink alike.
|
|
659
|
+
*/
|
|
660
|
+
function TableView({ node, blockKey, ctx, bctx }: BlockProps): ReactElement {
|
|
661
|
+
const look = ctx.look;
|
|
662
|
+
const view = ctx.view;
|
|
663
|
+
const contentStart = startOf(ctx, blockKey);
|
|
664
|
+
const size = look.size;
|
|
665
|
+
const padX = Math.round(size * 0.6);
|
|
666
|
+
const least = Math.round(size * 2.5);
|
|
667
|
+
const headerCtx = bctx.quote ? QUOTED_HEADER : HEADER;
|
|
668
|
+
const isHeader = (cell: PMNode): boolean =>
|
|
669
|
+
cell.type.spec.tableRole === 'header_cell';
|
|
670
|
+
const widths: number[] = [];
|
|
671
|
+
node.forEach((row) => {
|
|
672
|
+
row.forEach((cell, _offset, col) => {
|
|
673
|
+
const w = cellWidth(cell, ctx, isHeader(cell) ? headerCtx : bctx);
|
|
674
|
+
widths[col] = Math.max(
|
|
675
|
+
widths[col] ?? 0,
|
|
676
|
+
Math.min(Math.ceil(w), size * 26),
|
|
677
|
+
);
|
|
678
|
+
});
|
|
679
|
+
});
|
|
680
|
+
const basis = widths.map((w) => Math.max(w + padX * 2 + 2, least));
|
|
681
|
+
const rows: ReactNode[] = [];
|
|
682
|
+
node.forEach((row, rowOffset, rowIndex) => {
|
|
683
|
+
const rowPos = contentStart + rowOffset;
|
|
684
|
+
const rowKey = view.keys.keyAt(rowPos) ?? `r${rowPos}`;
|
|
685
|
+
const cells: ReactNode[] = [];
|
|
686
|
+
row.forEach((cell, cellOffset, col) => {
|
|
687
|
+
const cellPos = rowPos + 1 + cellOffset;
|
|
688
|
+
const cellKey = view.keys.keyAt(cellPos) ?? `c${cellPos}`;
|
|
689
|
+
const header = isHeader(cell);
|
|
690
|
+
cells.push(
|
|
691
|
+
hx(
|
|
692
|
+
'box',
|
|
693
|
+
{
|
|
694
|
+
key: cellKey,
|
|
695
|
+
style: {
|
|
696
|
+
flexBasis: basis[col] ?? least,
|
|
697
|
+
flexGrow: 0,
|
|
698
|
+
flexShrink: 1,
|
|
699
|
+
minWidth: least,
|
|
700
|
+
paddingLeft: padX,
|
|
701
|
+
paddingRight: padX,
|
|
702
|
+
paddingTop: Math.round(size * 0.35),
|
|
703
|
+
paddingBottom: Math.round(size * 0.35),
|
|
704
|
+
borderLeftWidth: cellOffset === 0 ? 0 : 1,
|
|
705
|
+
borderLeftColor: look.border,
|
|
706
|
+
flexDirection: 'column',
|
|
707
|
+
backgroundColor: header ? look.headerBg : undefined,
|
|
708
|
+
},
|
|
709
|
+
},
|
|
710
|
+
cell.isTextblock
|
|
711
|
+
? h(BlockView, {
|
|
712
|
+
blockKey: cellKey,
|
|
713
|
+
node: cell,
|
|
714
|
+
ctx,
|
|
715
|
+
bctx: header ? headerCtx : bctx,
|
|
716
|
+
})
|
|
717
|
+
: renderBlocks(cell, cellPos + 1, ctx, header ? headerCtx : bctx),
|
|
718
|
+
),
|
|
719
|
+
);
|
|
720
|
+
});
|
|
721
|
+
rows.push(
|
|
722
|
+
hx(
|
|
723
|
+
'box',
|
|
724
|
+
{
|
|
725
|
+
key: rowKey,
|
|
726
|
+
style: {
|
|
727
|
+
flexDirection: 'row',
|
|
728
|
+
borderTopWidth: rowIndex === 0 ? 0 : 1,
|
|
729
|
+
borderTopColor: look.border,
|
|
730
|
+
},
|
|
731
|
+
},
|
|
732
|
+
...cells,
|
|
733
|
+
),
|
|
734
|
+
);
|
|
735
|
+
});
|
|
736
|
+
return hx(
|
|
737
|
+
'box',
|
|
738
|
+
{
|
|
739
|
+
style: {
|
|
740
|
+
flexDirection: 'column',
|
|
741
|
+
alignSelf: 'flex-start',
|
|
742
|
+
maxWidth: '100%',
|
|
743
|
+
borderWidth: 1,
|
|
744
|
+
borderColor: look.border,
|
|
745
|
+
borderRadius: 6,
|
|
746
|
+
},
|
|
747
|
+
},
|
|
748
|
+
...rows,
|
|
749
|
+
);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
interface NodeViewHostProps extends BlockProps {
|
|
753
|
+
selected: boolean;
|
|
754
|
+
component: ComponentType<NodeViewProps>;
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
/** An app's node view, handed what it needs and nothing positional. */
|
|
758
|
+
function NodeViewHost({
|
|
759
|
+
node,
|
|
760
|
+
blockKey,
|
|
761
|
+
ctx,
|
|
762
|
+
bctx,
|
|
763
|
+
selected,
|
|
764
|
+
component,
|
|
765
|
+
}: NodeViewHostProps): ReactElement {
|
|
766
|
+
const view = ctx.view;
|
|
767
|
+
const getPos = useCallback(() => view.keys.posOf(blockKey), [view, blockKey]);
|
|
768
|
+
const updateAttributes = useCallback(
|
|
769
|
+
(attrs: Record<string, unknown>) => {
|
|
770
|
+
const pos = view.keys.posOf(blockKey);
|
|
771
|
+
const current = view.keys.nodeOf(blockKey);
|
|
772
|
+
if (pos === undefined || !current) return;
|
|
773
|
+
view.dispatch(
|
|
774
|
+
view.state.tr.setNodeMarkup(pos, undefined, {
|
|
775
|
+
...current.attrs,
|
|
776
|
+
...attrs,
|
|
777
|
+
}),
|
|
778
|
+
);
|
|
779
|
+
},
|
|
780
|
+
[view, blockKey],
|
|
781
|
+
);
|
|
782
|
+
let children: ReactNode;
|
|
783
|
+
if (node.isTextblock)
|
|
784
|
+
children = h(TextblockView, { node, blockKey, ctx, bctx });
|
|
785
|
+
else if (!node.isLeaf)
|
|
786
|
+
children = renderBlocks(node, startOf(ctx, blockKey), ctx, bctx);
|
|
787
|
+
const inner = h(component, {
|
|
788
|
+
node,
|
|
789
|
+
getPos,
|
|
790
|
+
selected,
|
|
791
|
+
children,
|
|
792
|
+
updateAttributes,
|
|
793
|
+
editable: ctx.editable,
|
|
794
|
+
view,
|
|
795
|
+
});
|
|
796
|
+
// a leaf is something a press selects, so the view has to know its box
|
|
797
|
+
return node.isLeaf ? h(AtomBox, { blockKey, ctx, selected }, inner) : inner;
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
export type { BlockDecorations };
|