@open-artifact/editor-kit 0.0.0-stage → 0.2.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 (82) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +128 -2
  4. package/package.json +58 -4
  5. package/src/Announcer.d.ts +2 -0
  6. package/src/Announcer.js +8 -0
  7. package/src/FindBar.d.ts +31 -0
  8. package/src/FindBar.js +34 -0
  9. package/src/agent.d.ts +22 -0
  10. package/src/agent.js +46 -0
  11. package/src/announce.d.ts +18 -0
  12. package/src/announce.js +21 -0
  13. package/src/chrome.d.ts +180 -0
  14. package/src/chrome.js +108 -0
  15. package/src/collab/CommentList.d.ts +23 -0
  16. package/src/collab/CommentList.js +117 -0
  17. package/src/collab/CommentViews.d.ts +118 -0
  18. package/src/collab/CommentViews.js +531 -0
  19. package/src/collab/HistoryViews.d.ts +38 -0
  20. package/src/collab/HistoryViews.js +103 -0
  21. package/src/collab/PresenceViews.d.ts +52 -0
  22. package/src/collab/PresenceViews.js +46 -0
  23. package/src/collab/cds/cds.d.ts +148 -0
  24. package/src/collab/cds/cds.js +230 -0
  25. package/src/collab/cds/comment-card.d.ts +93 -0
  26. package/src/collab/cds/comment-card.js +211 -0
  27. package/src/collab/cds/comment-composer.d.ts +28 -0
  28. package/src/collab/cds/comment-composer.js +101 -0
  29. package/src/collab/cds/comments-css.d.ts +2 -0
  30. package/src/collab/cds/comments-css.js +3 -0
  31. package/src/collab/cds/comments.css +497 -0
  32. package/src/collab/cds/edge-piles.d.ts +69 -0
  33. package/src/collab/cds/edge-piles.js +178 -0
  34. package/src/collab/cds/layer.d.ts +41 -0
  35. package/src/collab/cds/layer.js +143 -0
  36. package/src/collab/cds/markdown.d.ts +17 -0
  37. package/src/collab/cds/markdown.js +507 -0
  38. package/src/collab/cds/shield.d.ts +28 -0
  39. package/src/collab/cds/shield.js +114 -0
  40. package/src/collab/cds/time.d.ts +2 -0
  41. package/src/collab/cds/time.js +41 -0
  42. package/src/collab/collab.d.ts +100 -0
  43. package/src/collab/collab.js +119 -0
  44. package/src/collab/comments.d.ts +159 -0
  45. package/src/collab/comments.js +297 -0
  46. package/src/collab/history-wiring.d.ts +32 -0
  47. package/src/collab/history-wiring.js +53 -0
  48. package/src/collab/history.d.ts +53 -0
  49. package/src/collab/history.js +94 -0
  50. package/src/collab/peer-colors.d.ts +123 -0
  51. package/src/collab/peer-colors.js +273 -0
  52. package/src/collab/pins.d.ts +70 -0
  53. package/src/collab/pins.js +44 -0
  54. package/src/collab/presence.d.ts +92 -0
  55. package/src/collab/presence.js +158 -0
  56. package/src/design-systems.d.ts +46 -0
  57. package/src/design-systems.js +61 -0
  58. package/src/downloads.d.ts +24 -0
  59. package/src/downloads.js +36 -0
  60. package/src/find.d.ts +64 -0
  61. package/src/find.js +147 -0
  62. package/src/index.d.ts +25 -0
  63. package/src/index.js +25 -0
  64. package/src/keys.d.ts +13 -0
  65. package/src/keys.js +20 -0
  66. package/src/merge.d.ts +23 -0
  67. package/src/merge.js +116 -0
  68. package/src/page-link.d.ts +33 -0
  69. package/src/page-link.js +75 -0
  70. package/src/project-sync.d.ts +118 -0
  71. package/src/project-sync.js +174 -0
  72. package/src/react.d.ts +16 -0
  73. package/src/react.js +16 -0
  74. package/src/roles.d.ts +36 -0
  75. package/src/roles.js +27 -0
  76. package/src/session.d.ts +42 -0
  77. package/src/session.js +37 -0
  78. package/src/shell.d.ts +56 -0
  79. package/src/shell.js +96 -0
  80. package/src/stash.d.ts +26 -0
  81. package/src/stash.js +66 -0
  82. package/src/styles.css +17 -0
@@ -0,0 +1,69 @@
1
+ /**
2
+ * Edge piles: a thread whose pin is out of view (the page scrolled past it, the canvas panned it off, it is under the
3
+ * toolbar) gets no pin. Instead a tab hangs at that edge of the window, "More comments" and how many; a click shows the
4
+ * nearest one (controller.showThread: the runtime brings it into view and opens its card). A side whose edge has no room
5
+ * for a tab keeps its pins (out of view, as before).
6
+ *
7
+ * Left out: right-to-left layout (the reserve and the tab order flip), the lift for the comment sidebar's tab
8
+ * and comment regions (`commentRegionsEnabled`: the layer has no regions).
9
+ *
10
+ * Pin stack: pins that would overlap in a row (two comments on the same spot) are laid out side by side, 6px over each
11
+ * other, leftwards when the row would run past the window's right edge.
12
+ */
13
+ import type { CSSProperties } from 'react';
14
+ export type PileSide = 'above' | 'below' | 'left' | 'right';
15
+ type Side = PileSide | 'in';
16
+ /** A box in viewport px. */
17
+ export interface Box {
18
+ left: number;
19
+ top: number;
20
+ width: number;
21
+ height: number;
22
+ }
23
+ /** Which edge a point (viewport px) is past, if any. */
24
+ export declare function sideOf(box: Box, point: {
25
+ x: number;
26
+ y: number;
27
+ }, top: number): Side;
28
+ export interface PileInput {
29
+ /** The threads that have pins: id and point (viewport px). */
30
+ points: readonly {
31
+ id: string;
32
+ x: number;
33
+ y: number;
34
+ }[];
35
+ /** The thread whose card is open: it stays a pin wherever it is. */
36
+ openId: string | null;
37
+ box: Box;
38
+ /** The bar over the page (header), px from the box's top: what is under it is out of view. */
39
+ top: number;
40
+ /** The page's toolbar at the top: the top tab hangs from its bottom. */
41
+ floor: number;
42
+ /** The page's toolbar at the bottom, px from the box's bottom: the bottom tab sits that far up, all rounded. */
43
+ foot: number;
44
+ /** Room the top tab keeps clear at the right (the open "All comments" panel). */
45
+ reserveRight: number;
46
+ }
47
+ export interface Piles {
48
+ /** Per side: the threads past that edge, nearest first, and where its tab goes (null: no room, they keep pins). */
49
+ sides: Record<PileSide, {
50
+ ids: string[];
51
+ style: CSSProperties | null;
52
+ }>;
53
+ /** The bottom tab is lifted over a toolbar. */
54
+ floating: boolean;
55
+ /** Threads drawn as a tab, not a pin. */
56
+ piled: ReadonlySet<string>;
57
+ }
58
+ export declare function edgePiles({ points, openId, box, top, floor, foot, reserveRight }: PileInput): Piles;
59
+ export declare function EdgePiles({ piles, onOpen }: {
60
+ piles: Piles;
61
+ onOpen: (id: string) => void;
62
+ }): (import("react/jsx-runtime").JSX.Element | null)[];
63
+ export declare function stackPins(pins: readonly {
64
+ id: string;
65
+ x: number;
66
+ y: number;
67
+ w: number;
68
+ }[], right: number, size: number): Map<string, number>;
69
+ export {};
@@ -0,0 +1,178 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { cn, Icon } from "./cds.js";
3
+ /** A tab's widest (above, below) or tallest (left, right). */
4
+ const PILE_MAX = 148;
5
+ /** A tab's thickness, and the room a tab at the bottom keeps over the toolbar. */
6
+ const PILE_GAP = 24;
7
+ /** The window needs this width for any tab. */
8
+ const MIN_WIDTH = 164;
9
+ /** …and this much height between its top inset and bottom for the top or bottom tab; more when both show. */
10
+ const MIN_HEIGHT = 64;
11
+ const MIN_HEIGHT_BOTH = 96;
12
+ /** …and this much for a side tab; more when the top or bottom tab comes near the window's corners. */
13
+ const SIDE_MIN_HEIGHT = 180;
14
+ const SIDE_MIN_HEIGHT_TIGHT = 228;
15
+ /** The most a toolbar at the bottom lifts the bottom tab. */
16
+ const FOOT_MAX = 120;
17
+ const clamp = (value, min, max) => Math.min(Math.max(value, min), max);
18
+ /** The part of the box below the top inset. */
19
+ function band(box, top) {
20
+ return { top: Math.max(box.top, top, 0), bottom: box.top + box.height };
21
+ }
22
+ /** Which edge a point (viewport px) is past, if any. */
23
+ export function sideOf(box, point, top) {
24
+ const { top: t, bottom } = band(box, top);
25
+ if (point.y < t)
26
+ return 'above';
27
+ if (point.y > bottom)
28
+ return 'below';
29
+ if (point.x < box.left)
30
+ return 'left';
31
+ if (point.x > box.left + box.width)
32
+ return 'right';
33
+ return 'in';
34
+ }
35
+ /** Where a side's tab goes (its anchor point and transform), or null when the window has no room for it. */
36
+ function place(box, side, minHeight, { top: inset, floor = 0, gutter = 0, reserveRight = 0 }) {
37
+ const { top, bottom } = band(box, Math.max(floor, inset));
38
+ const reserve = side === 'above' ? reserveRight : 0;
39
+ if (bottom - top < minHeight || box.width < MIN_WIDTH + reserve)
40
+ return null;
41
+ const middle = (top + bottom) / 2;
42
+ if (side === 'left')
43
+ return { left: box.left + gutter, top: middle, transform: 'translateY(-50%)' };
44
+ if (side === 'right')
45
+ return { left: box.left + box.width - gutter, top: middle, transform: 'translate(-100%, -50%)' };
46
+ const half = PILE_MAX / 2 + 8;
47
+ return {
48
+ left: clamp(box.left + box.width / 2, box.left + half, box.left + box.width - half - reserve),
49
+ top: side === 'above' ? top : bottom - gutter,
50
+ transform: side === 'above' ? 'translateX(-50%)' : 'translate(-50%, -100%)',
51
+ };
52
+ }
53
+ export function edgePiles({ points, openId, box, top, floor, foot, reserveRight }) {
54
+ const past = { above: [], below: [], left: [], right: [] };
55
+ const sideById = new Map();
56
+ for (const point of points) {
57
+ if (point.id === openId)
58
+ continue;
59
+ const side = sideOf(box, point, top);
60
+ if (side === 'in')
61
+ continue;
62
+ past[side].push(point);
63
+ sideById.set(point.id, side);
64
+ }
65
+ // Nearest the edge first: a click shows the first.
66
+ past.above.sort((a, b) => b.y - a.y);
67
+ past.below.sort((a, b) => a.y - b.y);
68
+ past.left.sort((a, b) => b.x - a.x);
69
+ past.right.sort((a, b) => a.x - b.x);
70
+ const minHeight = past.above.length > 0 && past.below.length > 0 ? MIN_HEIGHT_BOTH : MIN_HEIGHT;
71
+ const above = past.above.length ? place(box, 'above', minHeight, { top, floor, reserveRight }) : null;
72
+ const visible = band(box, Math.max(floor, top));
73
+ const lift = foot > 0 ? Math.min(foot, FOOT_MAX, Math.max(0, visible.bottom - visible.top - (past.above.length > 0 ? PILE_GAP : 0) - PILE_GAP)) : 0;
74
+ const below = past.below.length ? place(box, 'below', minHeight, { top, gutter: lift }) : null;
75
+ // The side tabs need more height when the top or bottom tab comes within a tab's thickness of the corners.
76
+ const room = [above, below].flatMap(style => style && typeof style.left === 'number' ? [style.left - PILE_MAX / 2 - box.left, box.left + box.width - (style.left + PILE_MAX / 2)] : []);
77
+ const near = room.length > 0 ? Math.min(...room) : null;
78
+ const sideMin = near !== null && near < PILE_GAP + 8 ? SIDE_MIN_HEIGHT_TIGHT : SIDE_MIN_HEIGHT;
79
+ const left = past.left.length ? place(box, 'left', sideMin, { top }) : null;
80
+ const right = past.right.length ? place(box, 'right', sideMin, { top }) : null;
81
+ const styles = { above, below, left, right };
82
+ const piled = new Set();
83
+ for (const [id, side] of sideById)
84
+ if (styles[side])
85
+ piled.add(id);
86
+ const sides = Object.fromEntries(Object.keys(past).map(side => [side, { ids: past[side].map(p => p.id), style: styles[side] }]));
87
+ return { sides, floating: lift > 0, piled };
88
+ }
89
+ // ---------- the tabs ----------
90
+ const PILE_CLASS = 'pointer-events-auto relative flex items-center gap-xs border bg-surface-popover text-secondary shadow-sm cursor-[var(--cds-cursor-interactive)] outline-none focus-visible:outline-hidden focus-visible:shadow-focus';
91
+ const SHAPE_CLASS = {
92
+ above: 'h-6 ps-xs pe-sm',
93
+ below: 'h-6 border-b-0 ps-xs pe-sm',
94
+ left: 'w-6 flex-col py-sm',
95
+ right: 'w-6 flex-col py-sm',
96
+ floating: 'h-6 rounded-[10px] ps-xs pe-sm',
97
+ };
98
+ const SHAPE_STYLE = {
99
+ above: { maxWidth: PILE_MAX, borderTopWidth: 0, borderBottomLeftRadius: 10, borderBottomRightRadius: 10 },
100
+ below: { maxWidth: PILE_MAX, borderTopLeftRadius: 10, borderTopRightRadius: 10 },
101
+ left: { maxHeight: PILE_MAX, borderLeftWidth: 0, borderTopRightRadius: 10, borderBottomRightRadius: 10 },
102
+ right: { maxHeight: PILE_MAX, borderRightWidth: 0, borderTopLeftRadius: 10, borderBottomLeftRadius: 10 },
103
+ floating: { maxWidth: PILE_MAX },
104
+ };
105
+ /** The caret points at the edge. */
106
+ const CARET_TURN = { above: 'rotate(180deg)', below: undefined, left: 'rotate(90deg)', right: 'rotate(-90deg)' };
107
+ const WHERE = { above: 'above', below: 'below', left: 'to the left', right: 'to the right' };
108
+ function Pile({ side, ids, style, floating, onOpen }) {
109
+ const count = ids.length;
110
+ const vertical = side === 'left' || side === 'right';
111
+ const shape = side === 'below' && floating ? 'floating' : side;
112
+ const caret = (_jsx("span", { "aria-hidden": true, className: "flex", style: { transform: CARET_TURN[side] }, children: _jsx(Icon, { name: "CaretDown", size: "xs", className: "opacity-60" }) }, "caret"));
113
+ const label = (_jsxs("span", { "aria-hidden": true, className: "flex min-h-0 min-w-0 items-baseline gap-1 overflow-hidden whitespace-nowrap text-caption leading-none", style: vertical ? { writingMode: 'vertical-rl', transform: side === 'left' ? 'rotate(180deg)' : undefined } : undefined, children: ["More comments", _jsx("span", { dir: "ltr", className: "text-muted tabular-nums", children: count > 99 ? '99+' : count })] }, "label"));
114
+ return (_jsx("div", { style: { position: 'fixed', ...style, pointerEvents: 'none' }, children: _jsx("button", { type: "button", "data-comment-edge-pile": side, "data-comment-edge-pile-threads": ids.join(' '), "aria-label": `More comments: jump to ${count === 1 ? '1 comment' : `${count} comments`} ${WHERE[side]}`, className: cn(PILE_CLASS, SHAPE_CLASS[shape]), style: SHAPE_STYLE[shape], onClick: () => ids[0] && onOpen(ids[0]), children: side === 'left' ? [label, caret] : [caret, label] }) }));
115
+ }
116
+ export function EdgePiles({ piles, onOpen }) {
117
+ return ['above', 'below', 'left', 'right'].map(side => {
118
+ const { ids, style } = piles.sides[side];
119
+ return style && ids.length > 0 ? _jsx(Pile, { side: side, ids: ids, style: style, floating: piles.floating, onOpen: onOpen }, side) : null;
120
+ });
121
+ }
122
+ // ---------- the pin stack ----------
123
+ export function stackPins(pins, right, size) {
124
+ const near = size * 0.75;
125
+ const overlaps = (a, ax, b, bx) => ax - bx < b.w - size + near && bx - ax < a.w - size + near && Math.abs(b.y - a.y) < near;
126
+ const sorted = [...pins].sort((a, b) => a.x - b.x || a.y - b.y);
127
+ const groups = [];
128
+ for (const pin of sorted) {
129
+ const group = groups.find(g => g.some(other => overlaps(pin, pin.x, other, other.x)));
130
+ if (group)
131
+ group.push(pin);
132
+ else
133
+ groups.push([pin]);
134
+ }
135
+ const layout = (group) => {
136
+ if (group.length < 2)
137
+ return group.map(p => p.x);
138
+ const start = group[0].x;
139
+ if (start + group.reduce((sum, p) => sum + p.w - 6, 6) > right) {
140
+ const xs = [];
141
+ let edge = start + group[0].w;
142
+ for (const p of group) {
143
+ xs.push(edge - p.w);
144
+ edge -= p.w - 6;
145
+ }
146
+ return xs;
147
+ }
148
+ let x = start;
149
+ return group.map(p => {
150
+ const at = x;
151
+ x += p.w - 6;
152
+ return at;
153
+ });
154
+ };
155
+ let merged = true;
156
+ while (merged && groups.length > 1) {
157
+ merged = false;
158
+ const xs = groups.map(layout);
159
+ outer: for (let i = 0; i < groups.length; i++) {
160
+ for (let j = i + 1; j < groups.length; j++) {
161
+ if (groups[i].some((a, ai) => groups[j].some((b, bi) => overlaps(a, xs[i][ai], b, xs[j][bi])))) {
162
+ groups[i] = [...groups[i], ...groups[j]].sort((a, b) => a.x - b.x || a.y - b.y);
163
+ groups.splice(j, 1);
164
+ merged = true;
165
+ break outer;
166
+ }
167
+ }
168
+ }
169
+ }
170
+ const shift = new Map();
171
+ for (const group of groups) {
172
+ if (group.length < 2)
173
+ continue;
174
+ const xs = layout(group);
175
+ group.forEach((p, i) => shift.set(p.id, xs[i] - p.x));
176
+ }
177
+ return shift;
178
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * Where the comments UI draws: a shadow root carrying its own stylesheet (comments-css.ts, compiled from
3
+ * comments.css), so its class strings mean what they mean there and never meet the editor's own Tailwind.
4
+ *
5
+ * {@link CommentsLayer} is a shadow host in the editor's tree (a full-surface layer, a panel, a toolbar seat); its
6
+ * children are portalled into the shadow root under a `.cds-root` (density, light or dark from the page, rem scale).
7
+ * Views that may be drawn on their own (a thread card in a side panel) wrap themselves in one; inside a layer they
8
+ * draw straight into it ({@link useInLayer}).
9
+ *
10
+ * Events: every host joins one shield (shield.ts), installed when this module loads, so a key typed in a comment
11
+ * never reaches the editor's window and document listeners (its shortcuts), and presses in the layer never start its
12
+ * gestures. React still bubbles the layer's events up the editor's component tree (a portal's events do), so the
13
+ * layer stops them at its root (all but mouseup, which must reach the document).
14
+ */
15
+ import { type CSSProperties, type ReactNode } from 'react';
16
+ export declare const useInLayer: () => boolean;
17
+ /** Dark when the page says so (data-theme, a dark/light class), else when the system is; and the root font scale. */
18
+ export declare function usePageTheme(win?: Window): {
19
+ dark: boolean;
20
+ remScale: number;
21
+ };
22
+ export interface CommentsLayerProps {
23
+ /** Classes of the host element (in the editor's tree, styled by the editor's CSS). */
24
+ className?: string;
25
+ style?: CSSProperties;
26
+ /** Laid out as `display: contents` inside the shadow root (the default), or as a flex row (a toolbar seat). */
27
+ inline?: boolean;
28
+ /** An Escape no view in the layer took (default: nothing). */
29
+ onEscape?(): boolean | void;
30
+ children: ReactNode;
31
+ /** A data attribute for the host, so an editor can find it. */
32
+ mark?: string;
33
+ }
34
+ /** A shadow host whose children draw with the comments styles (see the module comment). */
35
+ export declare function CommentsLayer({ className, style, inline, onEscape, children, mark }: CommentsLayerProps): ReactNode;
36
+ /** Draws `children` in a layer of their own unless already inside one. */
37
+ export declare function InLayer({ children, className, style }: {
38
+ children: ReactNode;
39
+ className?: string;
40
+ style?: CSSProperties;
41
+ }): ReactNode;
@@ -0,0 +1,143 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * Where the comments UI draws: a shadow root carrying its own stylesheet (comments-css.ts, compiled from
4
+ * comments.css), so its class strings mean what they mean there and never meet the editor's own Tailwind.
5
+ *
6
+ * {@link CommentsLayer} is a shadow host in the editor's tree (a full-surface layer, a panel, a toolbar seat); its
7
+ * children are portalled into the shadow root under a `.cds-root` (density, light or dark from the page, rem scale).
8
+ * Views that may be drawn on their own (a thread card in a side panel) wrap themselves in one; inside a layer they
9
+ * draw straight into it ({@link useInLayer}).
10
+ *
11
+ * Events: every host joins one shield (shield.ts), installed when this module loads, so a key typed in a comment
12
+ * never reaches the editor's window and document listeners (its shortcuts), and presses in the layer never start its
13
+ * gestures. React still bubbles the layer's events up the editor's component tree (a portal's events do), so the
14
+ * layer stops them at its root (all but mouseup, which must reach the document).
15
+ */
16
+ import { Tooltip as BaseTooltip } from '@base-ui/react/tooltip';
17
+ import { createContext, useContext, useEffect, useLayoutEffect, useState } from 'react';
18
+ import { createPortal } from 'react-dom';
19
+ import { COMMENTS_CSS } from "./comments-css.js";
20
+ import { CdsRootContext } from "./cds.js";
21
+ import { closeMenus } from "./comment-card.js";
22
+ import { installShield } from "./shield.js";
23
+ /** Set inside a layer: views draw straight into it. */
24
+ const InLayerContext = createContext(false);
25
+ export const useInLayer = () => useContext(InLayerContext);
26
+ let shield = null;
27
+ let sheet = null;
28
+ /** The window's shield, installed once (before an editor's own listeners, when this module loads in a browser). */
29
+ function shieldOf(win) {
30
+ shield ??= installShield(win, {
31
+ onPointerDown(inside, event) {
32
+ if (!inside) {
33
+ closeMenus();
34
+ return;
35
+ }
36
+ const target = event.composedPath()[0];
37
+ if (!(target instanceof win.Element) || !target.closest('[data-cds=Menu], [data-popup-open]'))
38
+ closeMenus();
39
+ },
40
+ });
41
+ return shield;
42
+ }
43
+ // The layer's font (Inter) is declared on the page by editor-kit's styles.css: @font-face is ignored in a shadow root.
44
+ if (typeof window !== 'undefined' && typeof document !== 'undefined')
45
+ shieldOf(window);
46
+ /** Puts the comments sheet in a shadow root: one constructed sheet shared by every layer, else a <style>. */
47
+ function adoptSheet(shadow) {
48
+ const win = shadow.ownerDocument.defaultView;
49
+ if (win && 'adoptedStyleSheets' in shadow && typeof win.CSSStyleSheet === 'function') {
50
+ try {
51
+ if (!sheet) {
52
+ sheet = new win.CSSStyleSheet();
53
+ sheet.replaceSync(COMMENTS_CSS);
54
+ }
55
+ shadow.adoptedStyleSheets = [sheet];
56
+ return;
57
+ }
58
+ catch {
59
+ // A sheet from another realm, or no constructable sheets: a <style> instead.
60
+ }
61
+ }
62
+ const style = shadow.ownerDocument.createElement('style');
63
+ style.textContent = COMMENTS_CSS;
64
+ shadow.append(style);
65
+ }
66
+ /** Dark when the page says so (data-theme, a dark/light class), else when the system is; and the root font scale. */
67
+ export function usePageTheme(win = window) {
68
+ const read = () => {
69
+ const doc = win.document;
70
+ const html = doc.documentElement;
71
+ const attr = html.getAttribute('data-theme') ?? doc.body?.getAttribute('data-theme');
72
+ const system = win.matchMedia?.('(prefers-color-scheme: dark)').matches ?? false;
73
+ const dark = attr === 'dark' || html.classList.contains('dark') || (attr !== 'light' && !html.classList.contains('light') && system);
74
+ const rootSize = Number.parseFloat(win.getComputedStyle(html).fontSize) || 16;
75
+ return { dark, remScale: 16 / rootSize };
76
+ };
77
+ const [theme, setTheme] = useState(read);
78
+ useEffect(() => {
79
+ const update = () => setTheme((prev) => {
80
+ const next = read();
81
+ return next.dark === prev.dark && next.remScale === prev.remScale ? prev : next;
82
+ });
83
+ const media = win.matchMedia?.('(prefers-color-scheme: dark)');
84
+ media?.addEventListener?.('change', update);
85
+ const observer = new MutationObserver(update);
86
+ observer.observe(win.document.documentElement, { attributes: true, attributeFilter: ['data-theme', 'class', 'style'] });
87
+ if (win.document.body)
88
+ observer.observe(win.document.body, { attributes: true, attributeFilter: ['data-theme'] });
89
+ return () => {
90
+ media?.removeEventListener?.('change', update);
91
+ observer.disconnect();
92
+ };
93
+ }, [win]);
94
+ return theme;
95
+ }
96
+ const stop = (event) => event.stopPropagation();
97
+ /** A shadow host whose children draw with the comments styles (see the module comment). */
98
+ export function CommentsLayer({ className, style, inline = false, onEscape, children, mark = 'data-comments-layer' }) {
99
+ const [host, setHost] = useState(null);
100
+ const [container, setContainer] = useState(null);
101
+ const [portal, setPortal] = useState(null);
102
+ const { dark, remScale } = usePageTheme();
103
+ useLayoutEffect(() => {
104
+ if (!host)
105
+ return;
106
+ const win = host.ownerDocument.defaultView;
107
+ const shadow = host.shadowRoot ?? host.attachShadow({ mode: 'open' });
108
+ if (!shadow.querySelector('[data-cds-container]')) {
109
+ adoptSheet(shadow);
110
+ const div = host.ownerDocument.createElement('div');
111
+ div.setAttribute('data-cds-container', '');
112
+ div.style.display = 'contents';
113
+ shadow.append(div);
114
+ }
115
+ setContainer(shadow.querySelector('[data-cds-container]'));
116
+ const release = win ? shieldOf(win).addHost(host) : undefined;
117
+ return () => release?.();
118
+ }, [host]);
119
+ const root = (_jsxs("div", { className: "cds-root", "data-density": "comfortable", "data-platform": "web", "data-mode": dark ? 'dark' : 'light', style: { '--cds-rem-scale': remScale, ...(inline ? { display: 'flex', alignItems: 'center' } : { display: 'contents' }) },
120
+ // The layer's events stop here: past it, React would hand them to the editor's components.
121
+ onPointerDown: stop, onPointerUp: stop, onPointerMove: stop, onMouseDown: stop,
122
+ // Not mouseup: React's stop stops the native event too, and Base UI's menus disarm their press-drag-release from
123
+ // a mouseup on the document (without it a menu item's click is taken twice). See shield.ts.
124
+ onClick: stop, onDoubleClick: stop, onContextMenu: stop, onWheel: stop, onKeyUp: stop, onKeyDown: (event) => {
125
+ event.stopPropagation();
126
+ if (event.key !== 'Escape')
127
+ return;
128
+ if (closeMenus()) {
129
+ event.preventDefault();
130
+ return;
131
+ }
132
+ if (!event.defaultPrevented && !event.nativeEvent.isComposing && onEscape?.())
133
+ event.preventDefault();
134
+ }, children: [_jsx(CdsRootContext.Provider, { value: { container: portal, dark }, children: _jsx(InLayerContext.Provider, { value: true, children: _jsx(BaseTooltip.Provider, { children: children }) }) }), _jsx("div", { ref: setPortal, style: { position: 'relative', zIndex: 60 } })] }));
135
+ const attrs = { [mark]: '' };
136
+ return (_jsx("div", { ref: setHost, className: className, style: style, ...attrs, children: container ? createPortal(root, container) : null }));
137
+ }
138
+ /** Draws `children` in a layer of their own unless already inside one. */
139
+ export function InLayer({ children, className, style }) {
140
+ if (useInLayer())
141
+ return children;
142
+ return (_jsx(CommentsLayer, { className: className, style: style, children: children }));
143
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Markdown in comment bodies: the small subset its comment cards
3
+ * render when the boot flag commentMarkdownEnabled is on. Fenced code, block quotes, ## to ###### headings (bold,
4
+ * not bigger), - / * / 1. lists, inline code, *em*, **strong**, ~~del~~. Links and bare URLs are kept out of emphasis but
5
+ * stay text. Block mode is a thread's card; flat mode (one run of text: quotes in italics, list items as "• " / "1. ")
6
+ * is a row in the "All comments" panel. Same limits, flanking rules and class strings.
7
+ *
8
+ * Left out: @mentions (local comments carry no <mention:…> tokens, so no "@agent" chip and no reply that starts
9
+ * with one), and the screen-reader text form (it labels the drawer's hidden "go to comment" button, which the local
10
+ * panel does not have).
11
+ */
12
+ import { type ReactNode } from 'react';
13
+ export declare const COMMENT_MARKDOWN = true;
14
+ /** A comment's text as React children: Markdown when it has any, else the text itself. flat: a panel row. */
15
+ export declare function commentMarkdown(text: string, { flat }?: {
16
+ flat?: boolean;
17
+ }): ReactNode;