@open-artifact/editor-kit 0.0.0-stage → 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 (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 +20 -0
  10. package/src/agent.js +40 -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 +116 -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 +147 -0
  45. package/src/collab/comments.js +287 -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 +33 -0
  75. package/src/roles.js +26 -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,507 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * Markdown in comment bodies: the small subset its comment cards
4
+ * render when the boot flag commentMarkdownEnabled is on. Fenced code, block quotes, ## to ###### headings (bold,
5
+ * not bigger), - / * / 1. lists, inline code, *em*, **strong**, ~~del~~. Links and bare URLs are kept out of emphasis but
6
+ * stay text. Block mode is a thread's card; flat mode (one run of text: quotes in italics, list items as "• " / "1. ")
7
+ * is a row in the "All comments" panel. Same limits, flanking rules and class strings.
8
+ *
9
+ * Left out: @mentions (local comments carry no <mention:…> tokens, so no "@agent" chip and no reply that starts
10
+ * with one), and the screen-reader text form (it labels the drawer's hidden "go to comment" button, which the local
11
+ * panel does not have).
12
+ */
13
+ import { Fragment } from 'react';
14
+ export const COMMENT_MARKDOWN = true;
15
+ /** Only text with one of these goes through the parser; the rest renders as it is. */
16
+ const DETECT = /[\u0060*_~]|^[ \t]*(?:>|#{2,6}[ \t]|-[ \t]|\d{1,3}[.)][ \t])/m;
17
+ /** The backtick, written so (here and in the regexes) to keep build-css.mjs's template-literal scan off this file. */
18
+ const TICK = '\u0060';
19
+ const MAX_LENGTH = 4096;
20
+ const MAX_DEPTH = 6;
21
+ const MAX_DELIMITERS = 400;
22
+ const MAX_BLOCKS = 200;
23
+ const RUN = 'rtl:[unicode-bidi:plaintext]';
24
+ const CODE = 'rounded bg-alpha-1 px-xs font-mono text-code [&&]:[word-break:normal]';
25
+ const PRE = 'm-0 rounded bg-neutral px-sm py-xs text-start font-mono text-code break-words whitespace-pre-wrap';
26
+ const QUOTE = 'm-0 border-s-4 ps-md italic';
27
+ const UL = 'm-0 list-disc ps-xl';
28
+ const OL = 'm-0 list-decimal ps-[2.5em]';
29
+ /** A comment's text as React children: Markdown when it has any, else the text itself. flat: a panel row. */
30
+ export function commentMarkdown(text, { flat = false } = {}) {
31
+ if (!COMMENT_MARKDOWN || text.length > MAX_LENGTH || !DETECT.test(text))
32
+ return text;
33
+ const { blocks, endsWithNewline } = parse(text);
34
+ const out = renderBlocks(blocks, flat, 'b', 0);
35
+ const last = blocks[blocks.length - 1];
36
+ if (endsWithNewline && (last?.t === 'p' || last?.t === 'h'))
37
+ out.push('\n');
38
+ return out;
39
+ }
40
+ // ---------- blocks ----------
41
+ const cache = new Map();
42
+ const CACHE_SIZE = 1200;
43
+ function parse(text) {
44
+ const hit = cache.get(text);
45
+ if (hit) {
46
+ cache.delete(text);
47
+ cache.set(text, hit);
48
+ return hit;
49
+ }
50
+ const lines = text.replaceAll(/\r\n?/g, '\n').split('\n');
51
+ const endsWithNewline = lines.length > 1 && lines[lines.length - 1] === '';
52
+ if (endsWithNewline)
53
+ lines.pop();
54
+ const parsed = { blocks: parseBlocks(lines, 0, false, { blocks: MAX_BLOCKS }), endsWithNewline };
55
+ if (cache.size >= CACHE_SIZE)
56
+ cache.delete(cache.keys().next().value ?? '');
57
+ cache.set(text, parsed);
58
+ return parsed;
59
+ }
60
+ const FENCE_OPEN = /^ {0,3}(\u0060{3,}|~{3,})(.*)$/;
61
+ const FENCE_CLOSE = /^ {0,3}(\u0060+|~+)[ \t]*$/;
62
+ const QUOTE_LINE = /^ {0,3}>(?: |$)/;
63
+ const HEADING = /^ {0,3}#{2,6}[ \t]/;
64
+ const RULE = /^ {0,3}([-*])(?:[ \t]*\1){2,}[ \t]*$/;
65
+ const ITEM = /^( *)([-*]|(\d{1,3})([.)]))( +)(\S.*)$/;
66
+ const isBlank = (line) => /^[ \t]*$/.test(line);
67
+ const indentOf = (line) => /^ */.exec(line)?.[0].length ?? 0;
68
+ const expandTabs = (line) => line.replace(/^[ \t]+/, lead => lead.replaceAll('\t', ' '));
69
+ /** Spaces and tabs off both ends (not other white space). */
70
+ function trim(text) {
71
+ let start = 0;
72
+ let end = text.length;
73
+ while (start < end && (text[start] === ' ' || text[start] === '\t'))
74
+ start++;
75
+ while (end > start && (text[end - 1] === ' ' || text[end - 1] === '\t'))
76
+ end--;
77
+ return text.slice(start, end);
78
+ }
79
+ function fenceOpen(line) {
80
+ const m = FENCE_OPEN.exec(line);
81
+ if (!m)
82
+ return undefined;
83
+ const [, fence = '', rest = ''] = m;
84
+ if (fence[0] === TICK && rest.includes(TICK))
85
+ return undefined;
86
+ return { ch: fence[0] ?? TICK, n: fence.length, info: trim(rest) };
87
+ }
88
+ /** The line that closes the fence opened at line at, or -1. An unclosed fence shuts out shorter ones after it. */
89
+ function fenceEnd(lines, at, unclosed) {
90
+ const open = fenceOpen(lines[at] ?? '');
91
+ if (!open || open.n >= (unclosed[open.ch] ?? Infinity))
92
+ return -1;
93
+ for (let i = at + 1; i < lines.length; i++) {
94
+ const fence = FENCE_CLOSE.exec(lines[i] ?? '')?.[1];
95
+ if (fence && fence[0] === open.ch && fence.length >= open.n)
96
+ return i;
97
+ }
98
+ unclosed[open.ch] = open.n;
99
+ return -1;
100
+ }
101
+ function heading(line) {
102
+ const m = HEADING.exec(line);
103
+ if (!m)
104
+ return undefined;
105
+ const rest = trim(line.slice(m[0].length));
106
+ let end = rest.length;
107
+ while (end > 0 && rest[end - 1] === '#')
108
+ end--;
109
+ const text = end < rest.length && (end === 0 || /[ \t]/.test(rest[end - 1] ?? '')) ? trim(rest.slice(0, end)) : rest;
110
+ return text === '' ? undefined : text;
111
+ }
112
+ /** A list item's first line. After a paragraph only "1." (or a bullet) starts a list. */
113
+ function listItem(line, afterParagraph) {
114
+ if (RULE.test(line))
115
+ return undefined;
116
+ const m = ITEM.exec(line);
117
+ if (!m)
118
+ return undefined;
119
+ const [, lead = '', marker = '', number, delimiter = '', gap = '', rest = ''] = m;
120
+ const ordered = number !== undefined;
121
+ const n = ordered ? Number(number) : 1;
122
+ if (ordered && afterParagraph && n !== 1)
123
+ return undefined;
124
+ const pad = gap.length > 4 ? 1 : gap.length;
125
+ return {
126
+ indent: lead.length,
127
+ content: lead.length + marker.length + pad,
128
+ ordered,
129
+ kind: ordered ? delimiter : marker,
130
+ n,
131
+ rest: ' '.repeat(gap.length - pad) + rest,
132
+ };
133
+ }
134
+ const startsBlock = (lines, at, unclosed) => {
135
+ const line = lines[at] ?? '';
136
+ return fenceEnd(lines, at, unclosed) >= 0 || QUOTE_LINE.test(line) || heading(line) !== undefined || listItem(line, true) !== undefined;
137
+ };
138
+ function parseBlocks(raw, depth, inItem, budget) {
139
+ if (depth >= MAX_DEPTH) {
140
+ budget.blocks--;
141
+ return [{ t: 'p', c: inline(raw.join('\n')) }];
142
+ }
143
+ const lines = raw.map(expandTabs);
144
+ const unclosed = {};
145
+ const out = [];
146
+ let at = 0;
147
+ while (at < lines.length) {
148
+ if (budget.blocks <= 0) {
149
+ out.push({ t: 'p', c: [{ t: 'raw', raw: raw.slice(at).join('\n') }] });
150
+ break;
151
+ }
152
+ budget.blocks--;
153
+ const line = lines[at] ?? '';
154
+ if (isBlank(line)) {
155
+ out.push({ t: 'blank' });
156
+ at++;
157
+ continue;
158
+ }
159
+ const end = fenceEnd(lines, at, unclosed);
160
+ if (end >= 0) {
161
+ const indent = indentOf(line);
162
+ const body = indent === 0 ? raw.slice(at + 1, end) : lines.slice(at + 1, end).map(l => l.slice(Math.min(indent, indentOf(l))));
163
+ out.push({ t: 'code', text: body.join('\n'), info: fenceOpen(line)?.info ?? '' });
164
+ at = end + 1;
165
+ continue;
166
+ }
167
+ // The first line of a list item is the item's own text, never a quote, heading or nested list.
168
+ const blockStart = !(inItem && at === 0);
169
+ if (blockStart && QUOTE_LINE.test(line)) {
170
+ let stop = at;
171
+ while (stop < lines.length && QUOTE_LINE.test(lines[stop] ?? ''))
172
+ stop++;
173
+ const inner = lines.slice(at, stop).map(l => l.replace(/^ {0,3}> ?/, ''));
174
+ out.push(inner.every(isBlank) ? { t: 'p', c: inline(raw.slice(at, stop).join('\n')) } : { t: 'quote', c: parseBlocks(inner, depth + 1, false, budget) });
175
+ at = stop;
176
+ continue;
177
+ }
178
+ const title = blockStart ? heading(line) : undefined;
179
+ if (title !== undefined) {
180
+ out.push({ t: 'h', c: inline(title) });
181
+ at++;
182
+ continue;
183
+ }
184
+ const first = blockStart ? listItem(line, out[out.length - 1]?.t === 'p') : undefined;
185
+ if (first) {
186
+ const items = [];
187
+ let item = first;
188
+ while (item !== undefined && (items.length === 0 || budget.blocks > 0)) {
189
+ const current = item;
190
+ const continuation = Math.min(current.content, current.indent + 2);
191
+ const body = [current.rest];
192
+ let next;
193
+ for (at++; at < lines.length;) {
194
+ let ahead = at;
195
+ while (ahead < lines.length && isBlank(lines[ahead] ?? ''))
196
+ ahead++;
197
+ if (ahead === lines.length)
198
+ break;
199
+ if (indentOf(lines[ahead] ?? '') >= continuation) {
200
+ for (; at <= ahead; at++) {
201
+ const l = lines[at] ?? '';
202
+ body.push(l.slice(Math.min(current.content, indentOf(l))));
203
+ }
204
+ continue;
205
+ }
206
+ const sibling = listItem(lines[ahead] ?? '', false);
207
+ if (sibling !== undefined && sibling.ordered === first.ordered && sibling.kind === current.kind && sibling.indent >= current.indent) {
208
+ for (; at < ahead; at++)
209
+ body.push('');
210
+ next = sibling;
211
+ }
212
+ break;
213
+ }
214
+ items.push({ n: current.n, c: parseBlocks(body, depth + 1, true, budget) });
215
+ item = next;
216
+ }
217
+ out.push({ t: 'list', ordered: first.ordered, items });
218
+ continue;
219
+ }
220
+ const paragraph = [raw[at] ?? ''];
221
+ for (at++; at < lines.length && !isBlank(lines[at] ?? '') && !startsBlock(lines, at, unclosed); at++)
222
+ paragraph.push(raw[at] ?? '');
223
+ out.push({ t: 'p', c: inline(paragraph.join('\n')) });
224
+ }
225
+ return out;
226
+ }
227
+ // ---------- inline ----------
228
+ const WORD = /[\p{L}\p{N}]/u;
229
+ const isSpace = (c) => c === undefined || /\s/u.test(c);
230
+ const CJK = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Thai}\p{Script=Lao}\p{Script=Khmer}\p{Script=Myanmar} -〿＀-￯]/u;
231
+ const QUOTE_MARK = /["']/;
232
+ /** Punctuation, symbols and emoji (with their variation selector and keycap). */
233
+ const isSymbol = (c) => c !== undefined && c > '' && (!/[\p{L}\p{M}\p{N}]/u.test(c) || c === '️' || c === '⃣');
234
+ const opensAfter = (c) => isSpace(c) || /[([{"'*_~]/.test(c ?? '') || isSymbol(c);
235
+ const closesBefore = (c) => isSpace(c) || /[)\]}"'.,;:!?%*_~]/.test(c ?? '') || isSymbol(c);
236
+ /** Whether a run of n ch between before and after can open or close emphasis. */
237
+ function flanks(ch, n, before, after, afterNext) {
238
+ const letter = /[\p{L}\p{M}\p{N}]/u;
239
+ if (ch === '_' && letter.test(before ?? ' ') && letter.test(after ?? ' '))
240
+ return { open: false, close: false };
241
+ const cjk = CJK.test(before ?? ' ') || CJK.test(after ?? ' ');
242
+ const quoted = n === 1 && QUOTE_MARK.test(before ?? ' ') && QUOTE_MARK.test(after ?? ' ');
243
+ const dotted = after === '.' && WORD.test(afterNext ?? ' ');
244
+ const beforeCloser = /[)\]}]/.test(after ?? ' ') || (n === 1 && after === '/');
245
+ const afterOpener = /[([{]/.test(before ?? ' ') || (n === 1 && before === '/');
246
+ return {
247
+ open: !isSpace(after) && !quoted && !beforeCloser && !(n === 1 && dotted) && (cjk || opensAfter(before)),
248
+ close: !isSpace(before) && !quoted && !afterOpener && !dotted && (cjk || closesBefore(after)),
249
+ };
250
+ }
251
+ const URL_TEXT = /(?:[a-z][a-z0-9+.-]{0,31}:\/\/|www\.)[^\s<]+/giu;
252
+ const LINK = /!?\[[^\]\n]{0,300}\]\((?:<[^>\n]{0,1000}>|[^\s()]{1,1000}(?:\([^\s()]{0,200}\)[^\s()]{0,200}){0,8})(?:[ \t]+"[^"\n]{0,300}")?\)/g;
253
+ /** A bare URL without the punctuation that ends the sentence around it (and unmatched closing parentheses). */
254
+ function trimUrl(url) {
255
+ let unbalanced = url.split(')').length - url.split('(').length;
256
+ let end = url.length;
257
+ for (;;) {
258
+ const c = url[end - 1];
259
+ if (end > 0 && /[?!.,:;*_~'"]/.test(c ?? ''))
260
+ end--;
261
+ else if (c === ')' && unbalanced > 0) {
262
+ end--;
263
+ unbalanced--;
264
+ }
265
+ else
266
+ return url.slice(0, end);
267
+ }
268
+ }
269
+ /** Start → end of every Markdown link and bare URL: no emphasis or code starts inside them. */
270
+ function linkRanges(text) {
271
+ const ranges = new Map();
272
+ for (const m of text.matchAll(LINK))
273
+ ranges.set(m.index, m.index + m[0].length);
274
+ for (const m of text.matchAll(URL_TEXT)) {
275
+ if (m.index !== 0 && /[A-Za-z0-9]/.test(text[m.index - 1] ?? ''))
276
+ continue;
277
+ ranges.set(m.index, Math.max(m.index + trimUrl(m[0]).length, ranges.get(m.index) ?? 0));
278
+ }
279
+ return ranges;
280
+ }
281
+ function inline(text) {
282
+ const links = linkRanges(text);
283
+ const out = [];
284
+ const escaped = { '*': 0, _: 0, '~': 0, [TICK]: 0 };
285
+ let guarded = 0;
286
+ let delimiters = 0;
287
+ let run = '';
288
+ const flush = () => {
289
+ if (run !== '')
290
+ out.push(run);
291
+ run = '';
292
+ };
293
+ const runLength = (at) => {
294
+ let n = 1;
295
+ while (text[at + n] === text[at])
296
+ n++;
297
+ return n;
298
+ };
299
+ const closingTicks = (from, n) => {
300
+ for (let i = from + n; i < text.length; i++) {
301
+ if (text[i] !== TICK)
302
+ continue;
303
+ const length = runLength(i);
304
+ if (length === n)
305
+ return i;
306
+ i += length;
307
+ }
308
+ return -1;
309
+ };
310
+ const delimiter = (ch, n, before, after) => {
311
+ if ((ch === '_' && n !== 1) || (ch === '~' && n !== 2))
312
+ return undefined;
313
+ const f = flanks(ch, n, before, text[after], text[after + 1]);
314
+ return f.open || f.close ? f : undefined;
315
+ };
316
+ let at = 0;
317
+ while (at < text.length) {
318
+ guarded = Math.max(guarded, links.get(at) ?? 0);
319
+ const ch = text[at] ?? '';
320
+ if (at < guarded) {
321
+ run += ch;
322
+ at++;
323
+ continue;
324
+ }
325
+ const next = text[at + 1];
326
+ if (ch === '\\' && (next === TICK || next === '*' || next === '_' || next === '~')) {
327
+ // An escaped run loses its backslashes only where it would otherwise have opened (or closed) emphasis.
328
+ let end = at;
329
+ while (text[end] === '\\' && text[end + 1] === next)
330
+ end += 2;
331
+ const n = next === TICK ? runLength(at + 1) : (end - at) / 2;
332
+ const stop = next === TICK ? at + 1 + n : end;
333
+ const f = next === TICK ? { open: closingTicks(at + 1, n) >= 0, close: true } : delimiter(next, n, text[at - 1], end);
334
+ const opens = f?.open === true && WORD.test(text[stop] ?? ' ');
335
+ if (opens || (f?.close === true && (escaped[next] ?? 0) > 0)) {
336
+ escaped[next] = (escaped[next] ?? 0) + (opens ? 1 : -1);
337
+ run += next.repeat(n);
338
+ }
339
+ else
340
+ run += text.slice(at, stop);
341
+ at = stop;
342
+ continue;
343
+ }
344
+ if (ch === TICK) {
345
+ const n = runLength(at);
346
+ const close = closingTicks(at, n);
347
+ if (close < 0) {
348
+ run += text.slice(at, at + n);
349
+ at += n;
350
+ continue;
351
+ }
352
+ let code = text.slice(at + n, close);
353
+ if (code.startsWith(' ') && code.endsWith(' ') && code.trim() !== '')
354
+ code = code.slice(1, -1);
355
+ flush();
356
+ out.push({ t: 'code', text: code });
357
+ at = close + n;
358
+ continue;
359
+ }
360
+ if (ch === '*' || ch === '_' || ch === '~') {
361
+ const n = runLength(at);
362
+ const f = delimiters < MAX_DELIMITERS ? delimiter(ch, n, text[at - 1], at + n) : undefined;
363
+ flush();
364
+ if (f) {
365
+ out.push({ t: 'delim', ch, n, orig: n, ...f });
366
+ delimiters++;
367
+ }
368
+ else if (ch === '~')
369
+ run += text.slice(at, at + n);
370
+ else
371
+ out.push({ t: 'refused', ch, text: text.slice(at, at + n) });
372
+ at += n;
373
+ continue;
374
+ }
375
+ run += ch;
376
+ at++;
377
+ }
378
+ flush();
379
+ return resolve(out);
380
+ }
381
+ const isDelimiter = (t) => typeof t === 'object' && t.t === 'delim';
382
+ const literal = (t) => (isDelimiter(t) ? t.ch.repeat(t.n) : typeof t === 'object' && t.t === 'refused' ? t.text : t);
383
+ const depthOf = (nodes) => Math.max(0, ...nodes.map(t => (typeof t === 'object' && 'depth' in t ? t.depth : 0)));
384
+ const hasText = (nodes) => nodes.some(t => typeof t !== 'string' || /[\p{L}\p{N}\u0080-\u{10FFFF}]/u.test(t));
385
+ /** Pairs delimiter runs into em / strong / del, CommonMark's way (the rule of three included); the rest is text. */
386
+ function resolve(tokens) {
387
+ let list = tokens;
388
+ let at = 0;
389
+ while (at < list.length) {
390
+ const closer = list[at];
391
+ if (closer === undefined || !isDelimiter(closer) || !closer.close) {
392
+ at++;
393
+ continue;
394
+ }
395
+ let opener = -1;
396
+ for (let i = at - 1; i >= 0; i--) {
397
+ const t = list[i];
398
+ if (t === undefined)
399
+ continue;
400
+ if (typeof t === 'object' && t.t === 'refused' && t.ch === closer.ch)
401
+ break;
402
+ if (isDelimiter(t) &&
403
+ t.ch === closer.ch &&
404
+ t.open &&
405
+ !(closer.ch !== '~' && (t.close || closer.open) && (t.orig + closer.orig) % 3 === 0 && (t.orig % 3 !== 0 || closer.orig % 3 !== 0))) {
406
+ opener = i;
407
+ break;
408
+ }
409
+ }
410
+ const inner = opener < 0 ? [] : list.slice(opener + 1, at).map(literal);
411
+ const depth = depthOf(inner) + 1;
412
+ const openToken = opener < 0 ? undefined : list[opener];
413
+ if (openToken !== undefined && !hasText(inner))
414
+ list[opener] = literal(openToken);
415
+ if (openToken === undefined || !isDelimiter(openToken) || depth > MAX_DEPTH || !hasText(inner)) {
416
+ if (!closer.open || opener >= 0)
417
+ list[at] = literal(closer);
418
+ at++;
419
+ continue;
420
+ }
421
+ const used = closer.ch === '~' || (openToken.n >= 2 && closer.n >= 2) ? 2 : 1;
422
+ const node = { t: closer.ch === '~' ? 'del' : used === 2 ? 'strong' : 'em', c: inner, depth };
423
+ openToken.n -= used;
424
+ closer.n -= used;
425
+ const head = list.slice(0, openToken.n > 0 ? opener + 1 : opener);
426
+ list = [...head, node, ...list.slice(closer.n > 0 ? at : at + 1)];
427
+ at = head.length + 1;
428
+ }
429
+ return list.map(literal);
430
+ }
431
+ // ---------- rendering ----------
432
+ /**
433
+ * Text blocks run together as text (the card keeps white space), each run after a block element in its own
434
+ * plaintext-bidi span; flat mode makes every block a run.
435
+ */
436
+ function renderBlocks(blocks, flat, key, level) {
437
+ const out = [];
438
+ const isRun = (b) => b !== undefined && (flat || b.t === 'p' || b.t === 'h' || b.t === 'blank');
439
+ let run = out;
440
+ const endRun = (k) => {
441
+ if (run === out)
442
+ return;
443
+ out.push(_jsx("span", { className: RUN, children: run }, k));
444
+ run = out;
445
+ };
446
+ blocks.forEach((block, i) => {
447
+ const k = key + '.' + i;
448
+ const newline = isRun(blocks.at(i + 1)) ? '\n' : '';
449
+ if (!isRun(block))
450
+ endRun(k + 'r');
451
+ else if (!flat && i > 0 && !isRun(blocks.at(i - 1)))
452
+ run = [];
453
+ switch (block.t) {
454
+ case 'blank':
455
+ run.push('\n');
456
+ break;
457
+ case 'p':
458
+ run.push(...renderInline(block.c, k), newline);
459
+ break;
460
+ case 'h':
461
+ run.push(_jsx("strong", { className: "font-semibold", children: renderInline(block.c, k) }, k), newline);
462
+ break;
463
+ case 'code': {
464
+ const info = block.info === '' ? null : _jsxs("span", { className: "block text-caption text-muted", children: [block.info, '\n'] });
465
+ run.push(flat ? (_jsxs(Fragment, { children: [info && _jsxs("span", { className: "text-muted", children: [block.info, " "] }), _jsx("code", { dir: "ltr", className: CODE, children: block.text })] }, k)) : (_jsxs("pre", { dir: "ltr", className: PRE, children: [info, _jsx("code", { className: "[&&]:[word-break:normal]", children: block.text })] }, k)), flat ? newline : '');
466
+ break;
467
+ }
468
+ case 'quote':
469
+ run.push(flat ? (_jsx("span", { className: "italic", children: renderBlocks(block.c, flat, k, level) }, k)) : (_jsx("blockquote", { className: QUOTE, children: _jsx("span", { className: RUN, children: renderBlocks(block.c, flat, k, level) }) }, k)), flat ? newline : '');
470
+ break;
471
+ case 'list': {
472
+ if (flat) {
473
+ block.items.forEach((item, j) => {
474
+ run.push(' '.repeat(level) + (block.ordered ? item.n + '. ' : '• '), ...renderBlocks(item.c, flat, k + '.' + j, level + 1));
475
+ if (item.c.at(-1)?.t !== 'blank')
476
+ run.push(j < block.items.length - 1 ? '\n' : newline);
477
+ });
478
+ break;
479
+ }
480
+ const items = block.items.map((item, j) => (_jsx("li", { value: block.ordered ? item.n : undefined, children: _jsx("span", { className: RUN, children: renderBlocks(item.c, flat, k + '.' + j, level + 1) }) }, j)));
481
+ run.push(block.ordered ? (_jsx("ol", { className: OL, children: items }, k)) : (_jsx("ul", { className: UL, children: items }, k)));
482
+ break;
483
+ }
484
+ }
485
+ });
486
+ endRun(key + '.end');
487
+ return out;
488
+ }
489
+ function renderInline(nodes, key) {
490
+ return nodes.map((node, i) => {
491
+ if (typeof node === 'string')
492
+ return node;
493
+ const k = key + ':' + i;
494
+ switch (node.t) {
495
+ case 'raw':
496
+ return _jsx(Fragment, { children: node.raw }, k);
497
+ case 'code':
498
+ return (_jsx("code", { dir: "ltr", className: CODE, children: node.text }, k));
499
+ case 'em':
500
+ return _jsx("em", { children: renderInline(node.c, k) }, k);
501
+ case 'strong':
502
+ return (_jsx("strong", { className: "font-semibold", children: renderInline(node.c, k) }, k));
503
+ case 'del':
504
+ return _jsx("del", { children: renderInline(node.c, k) }, k);
505
+ }
506
+ });
507
+ }
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Keeps the comments layer's events away from the editor runtime, which listens on window and document (its
3
+ * shortcuts, outside-click and wheel handlers) and would act on a key typed in a comment.
4
+ *
5
+ * A window capture listener, installed before the runtime starts so it runs first, takes each event that happens
6
+ * inside the layer: it stops the original there, then dispatches a copy at the same target that is not `composed`, so
7
+ * the copy stays inside the shadow root, where React (and Base UI) handle it. If a handler cancels the copy, the
8
+ * original is cancelled too, so default actions (typing, focus on mousedown) follow what the handlers decided.
9
+ *
10
+ * Events the runtime does not watch for shortcuts (click, input, focus…) pass as they are and stop at the layer.
11
+ *
12
+ * `mouseup` goes through untouched, out to the document: Base UI's menu trigger arms its press-drag-release on
13
+ * mousedown and disarms it from a one-time mouseup listener on the document; without that mouseup every menu item
14
+ * click would fire twice. The runtime's own mouseup handlers only finish drags it started, and the mousedown that
15
+ * would start one is shielded.
16
+ *
17
+ * Open Artifact: one shield per window, installed when the editor kit's views load (before an editor's own listeners);
18
+ * every comments layer's shadow host joins it (`addHost`, layer.tsx).
19
+ */
20
+ export interface ShieldOptions {
21
+ /** Called for every pointerdown, with whether it happened inside the layer (before the copy is dispatched). */
22
+ onPointerDown?: (inside: boolean, event: PointerEvent) => void;
23
+ }
24
+ export interface Shield {
25
+ /** Shield another shadow host as part of the layer (a seat in the page's toolbar); the returned function stops. */
26
+ addHost(host: Element): () => void;
27
+ }
28
+ export declare function installShield(win: Window & typeof globalThis, options?: ShieldOptions): Shield;
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Keeps the comments layer's events away from the editor runtime, which listens on window and document (its
3
+ * shortcuts, outside-click and wheel handlers) and would act on a key typed in a comment.
4
+ *
5
+ * A window capture listener, installed before the runtime starts so it runs first, takes each event that happens
6
+ * inside the layer: it stops the original there, then dispatches a copy at the same target that is not `composed`, so
7
+ * the copy stays inside the shadow root, where React (and Base UI) handle it. If a handler cancels the copy, the
8
+ * original is cancelled too, so default actions (typing, focus on mousedown) follow what the handlers decided.
9
+ *
10
+ * Events the runtime does not watch for shortcuts (click, input, focus…) pass as they are and stop at the layer.
11
+ *
12
+ * `mouseup` goes through untouched, out to the document: Base UI's menu trigger arms its press-drag-release on
13
+ * mousedown and disarms it from a one-time mouseup listener on the document; without that mouseup every menu item
14
+ * click would fire twice. The runtime's own mouseup handlers only finish drags it started, and the mousedown that
15
+ * would start one is shielded.
16
+ *
17
+ * Open Artifact: one shield per window, installed when the editor kit's views load (before an editor's own listeners);
18
+ * every comments layer's shadow host joins it (`addHost`, layer.tsx).
19
+ */
20
+ const SHIELDED = [
21
+ 'keydown',
22
+ 'keyup',
23
+ 'keypress',
24
+ 'pointerdown',
25
+ 'pointerup',
26
+ 'pointermove',
27
+ 'mousedown',
28
+ 'mousemove',
29
+ 'touchstart',
30
+ 'touchend',
31
+ 'touchmove',
32
+ 'wheel',
33
+ 'paste',
34
+ 'copy',
35
+ 'cut',
36
+ 'contextmenu',
37
+ 'dblclick',
38
+ 'compositionstart',
39
+ 'compositionupdate',
40
+ 'compositionend',
41
+ ];
42
+ const STOPPED_AT_LAYER = ['click', 'auxclick', 'input', 'change', 'beforeinput', 'focusin', 'focusout'];
43
+ const MOUSE_KEYS = ['clientX', 'clientY', 'screenX', 'screenY', 'button', 'buttons', 'ctrlKey', 'shiftKey', 'altKey', 'metaKey', 'movementX', 'movementY', 'detail'];
44
+ const POINTER_KEYS = ['pointerId', 'pointerType', 'isPrimary', 'width', 'height', 'pressure', 'tangentialPressure', 'tiltX', 'tiltY', 'twist'];
45
+ const KEY_KEYS = ['key', 'code', 'location', 'repeat', 'isComposing', 'ctrlKey', 'shiftKey', 'altKey', 'metaKey', 'keyCode', 'charCode', 'which'];
46
+ const WHEEL_KEYS = ['deltaX', 'deltaY', 'deltaZ', 'deltaMode'];
47
+ const TOUCH_KEYS = ['ctrlKey', 'shiftKey', 'altKey', 'metaKey'];
48
+ function pick(event, keys) {
49
+ const out = {};
50
+ for (const key of keys)
51
+ out[key] = event[key];
52
+ return out;
53
+ }
54
+ /** The same event, not composed: it stays in the shadow root it is dispatched in. */
55
+ function copyOf(event, win) {
56
+ const base = { bubbles: true, cancelable: event.cancelable, composed: false, view: win };
57
+ if (win.PointerEvent && event instanceof win.PointerEvent) {
58
+ return new win.PointerEvent(event.type, { ...base, ...pick(event, MOUSE_KEYS), ...pick(event, POINTER_KEYS) });
59
+ }
60
+ if (win.WheelEvent && event instanceof win.WheelEvent)
61
+ return new win.WheelEvent(event.type, { ...base, ...pick(event, MOUSE_KEYS), ...pick(event, WHEEL_KEYS) });
62
+ if (event instanceof win.MouseEvent)
63
+ return new win.MouseEvent(event.type, { ...base, ...pick(event, MOUSE_KEYS) });
64
+ if (event instanceof win.KeyboardEvent)
65
+ return new win.KeyboardEvent(event.type, { ...base, ...pick(event, KEY_KEYS) });
66
+ if (win.TouchEvent && event instanceof win.TouchEvent) {
67
+ return new win.TouchEvent(event.type, {
68
+ ...base,
69
+ ...pick(event, TOUCH_KEYS),
70
+ touches: [...event.touches],
71
+ targetTouches: [...event.targetTouches],
72
+ changedTouches: [...event.changedTouches],
73
+ });
74
+ }
75
+ if (win.ClipboardEvent && event instanceof win.ClipboardEvent)
76
+ return new win.ClipboardEvent(event.type, { ...base, clipboardData: event.clipboardData });
77
+ if (event instanceof win.CompositionEvent)
78
+ return new win.CompositionEvent(event.type, { ...base, data: event.data });
79
+ return null;
80
+ }
81
+ export function installShield(win, options = {}) {
82
+ const hosts = new Set();
83
+ const stop = (event) => event.stopPropagation();
84
+ for (const type of SHIELDED) {
85
+ win.addEventListener(type, event => {
86
+ const path = event.composedPath();
87
+ const inside = path.some(node => hosts.has(node));
88
+ if (type === 'pointerdown')
89
+ options.onPointerDown?.(inside, event);
90
+ if (!inside)
91
+ return;
92
+ event.stopImmediatePropagation();
93
+ const target = path[0];
94
+ const copy = copyOf(event, win);
95
+ if (!copy || !target)
96
+ return;
97
+ target.dispatchEvent(copy);
98
+ if (copy.defaultPrevented && event.cancelable)
99
+ event.preventDefault();
100
+ }, { capture: true, passive: false });
101
+ }
102
+ return {
103
+ addHost(extra) {
104
+ hosts.add(extra);
105
+ for (const type of STOPPED_AT_LAYER)
106
+ extra.addEventListener(type, stop);
107
+ return () => {
108
+ hosts.delete(extra);
109
+ for (const type of STOPPED_AT_LAYER)
110
+ extra.removeEventListener(type, stop);
111
+ };
112
+ },
113
+ };
114
+ }
@@ -0,0 +1,2 @@
1
+ export declare function commentTime(iso: string, now?: Date): string;
2
+ export declare function commentAgo(iso: string, now?: Date): string;