@aiquants/markdown-explorer 0.12.1 → 0.14.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 (44) hide show
  1. package/CHANGELOG.md +30 -1
  2. package/README.md +21 -12
  3. package/dist/EditSurface-BLlu5zEt.cjs +2 -0
  4. package/dist/EditSurface-CF2io496.js +705 -0
  5. package/dist/{ExplorerSplit-BjjqNRtj.js → ExplorerSplit-BPLECNpv.js} +109 -109
  6. package/dist/{ExplorerSplit-CqmbUSZ7.cjs → ExplorerSplit-Cjvy0JHP.cjs} +1 -1
  7. package/dist/{MultiLayout-BZbEl73E.cjs → MultiLayout-BjA0k-5n.cjs} +1 -1
  8. package/dist/{MultiLayout-BYXoD1aS.js → MultiLayout-D8F0BfxA.js} +25 -25
  9. package/dist/{NativeScrollBar-RP37nwmY.js → NativeScrollBar-BFF12oEd.js} +1 -1
  10. package/dist/{NativeScrollBar-B7jTbQmn.cjs → NativeScrollBar-BUWXPvTV.cjs} +1 -1
  11. package/dist/{TreeLayout-CEp5oKly.cjs → TreeLayout-CQUpgsxz.cjs} +1 -1
  12. package/dist/{TreeLayout-Uo6RG7lq.js → TreeLayout-rhyVTfrI.js} +3 -3
  13. package/dist/core/contracts.d.cts +7 -1
  14. package/dist/core/contracts.d.ts +7 -1
  15. package/dist/core/labels.d.cts +14 -1
  16. package/dist/core/labels.d.ts +14 -1
  17. package/dist/core/revalidation.d.cts +1 -2
  18. package/dist/core/revalidation.d.ts +1 -2
  19. package/dist/{dataRoute-CH3yOKR0.cjs → dataRoute-BONacTPi.cjs} +1 -1
  20. package/dist/{dataRoute-Pm_0ue_4.js → dataRoute-CObbjNxG.js} +14 -12
  21. package/dist/index-Dn7cYy-4.cjs +13 -0
  22. package/dist/index-v997iKxe.js +3464 -0
  23. package/dist/index.cjs +1 -1
  24. package/dist/index.js +21 -21
  25. package/dist/{location-BUNWJ4eB.cjs → location-D6RE3wAN.cjs} +1 -1
  26. package/dist/{location-DWd6SQhN.js → location-IRD9h42r.js} +1 -1
  27. package/dist/{merge-DRGBbLuR.cjs → merge-BFk7nT59.cjs} +1 -1
  28. package/dist/{merge-q-5i8qsn.js → merge-Bi2VedIr.js} +1 -1
  29. package/dist/{sanitizePolicy-djLC3fRN.js → sanitizePolicy-DlrakwA4.js} +1 -1
  30. package/dist/{sanitizePolicy-BfUlXI5E.cjs → sanitizePolicy-DrIMuYCb.cjs} +1 -1
  31. package/dist/sanitizeWorker.cjs +1 -1
  32. package/dist/sanitizeWorker.js +1 -1
  33. package/dist/server/ports.d.cts +3 -0
  34. package/dist/server/ports.d.ts +3 -0
  35. package/dist/server.cjs +1 -1
  36. package/dist/server.js +93 -83
  37. package/dist/styles/markdown-explorer.css +1 -1
  38. package/docs/behavior/dom-hooks.md +6 -5
  39. package/docs/behavior/layout.md +10 -3
  40. package/package.json +6 -5
  41. package/dist/EditSurface-By6NPcD7.js +0 -633
  42. package/dist/EditSurface-BztcpQPW.cjs +0 -2
  43. package/dist/index-CGBl3fYT.cjs +0 -13
  44. package/dist/index-CjCr8czx.js +0 -3356
@@ -0,0 +1,705 @@
1
+ import { jsx as O, jsxs as te, Fragment as qe } from "react/jsx-runtime";
2
+ import { useState as Oe, useRef as N, useLayoutEffect as B, useEffectEvent as se, useEffect as we, useImperativeHandle as Ie, useMemo as Z, startTransition as Ne, useCallback as U } from "react";
3
+ import { v as X, f as re, g as Re, u as ne, h as De, l as _e, i as Pe, j as be, k as Ve, m as He, n as Be, F as Fe, E as ze, o as Ke, p as Ge, q as ce, r as ae, A as We, s as ue, t as je, w as Ue, x as $e, y as Xe } from "./index-v997iKxe.js";
4
+ import { history as Ye, historyKeymap as Je, defaultKeymap as Qe, historyField as Ze } from "@codemirror/commands";
5
+ import { markdown as et, markdownLanguage as tt } from "@codemirror/lang-markdown";
6
+ import { syntaxHighlighting as rt, HighlightStyle as nt } from "@codemirror/language";
7
+ import { highlightSelectionMatches as ot, searchKeymap as it } from "@codemirror/search";
8
+ import { EditorState as Ee, EditorSelection as ee, StateField as lt, RangeSetBuilder as st } from "@codemirror/state";
9
+ import { EditorView as K, lineNumbers as ct, highlightActiveLineGutter as at, drawSelection as ut, highlightActiveLine as dt, keymap as ft, Decoration as Q, WidgetType as mt } from "@codemirror/view";
10
+ import { tags as A } from "@lezer/highlight";
11
+ import { MarkdownViewer as pt } from "@aiquants/markdown";
12
+ import { SOURCE_LINES_ATTRIBUTE as xe, parseSourceLines as ht } from "@aiquants/markdown/source-lines";
13
+ import { d as vt, f as J, E as gt } from "./dataRoute-CObbjNxG.js";
14
+ import { H as wt, c as bt, M as Et } from "./sanitizePolicy-DlrakwA4.js";
15
+ import xt from "isomorphic-dompurify";
16
+ const Se = { history: Ze }, St = nt.define([
17
+ { tag: [A.heading1, A.heading2, A.heading3, A.heading4, A.heading5, A.heading6, A.heading], color: "var(--aqmx-syntax-heading)", fontWeight: "700" },
18
+ { tag: A.strong, fontWeight: "700" },
19
+ { tag: A.emphasis, fontStyle: "italic" },
20
+ { tag: A.strikethrough, textDecoration: "line-through" },
21
+ { tag: A.link, color: "var(--aqmx-syntax-link)", textDecoration: "underline" },
22
+ { tag: A.url, color: "var(--aqmx-syntax-url)" },
23
+ { tag: A.monospace, color: "var(--aqmx-syntax-code)" },
24
+ { tag: A.quote, color: "var(--aqmx-syntax-quote)" },
25
+ { tag: [A.processingInstruction, A.contentSeparator, A.labelName, A.meta], color: "var(--aqmx-syntax-mark)" }
26
+ ]), yt = K.theme({
27
+ "&": { height: "100%", color: "var(--aqmx-text)", backgroundColor: "var(--aqmx-surface)", fontSize: "var(--aqmx-font-size-title)" },
28
+ "&.cm-focused": { outline: "none" },
29
+ ".cm-scroller": { fontFamily: 'ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace', lineHeight: "var(--aqmx-line-height-prose)" },
30
+ ".cm-content": { caretColor: "var(--aqmx-text)", paddingBlock: "var(--aqmx-space-6)" },
31
+ ".cm-line": { paddingInline: "var(--aqmx-space-6)" },
32
+ "&.cm-focused .cm-cursor": { borderLeftColor: "var(--aqmx-text)" },
33
+ "&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection": { backgroundColor: "var(--aqmx-editor-selection)" },
34
+ ".cm-activeLine": { backgroundColor: "var(--aqmx-editor-active-line)" },
35
+ ".cm-gutters": { backgroundColor: "var(--aqmx-surface-raised)", color: "var(--aqmx-editor-gutter)", borderInlineEnd: "var(--aqmx-space-1) solid var(--aqmx-border)" },
36
+ ".cm-activeLineGutter": { backgroundColor: "var(--aqmx-editor-active-line)" },
37
+ ".cm-searchMatch": { outline: "var(--aqmx-space-1) solid var(--aqmx-border-strong)" },
38
+ ".cm-panels": { backgroundColor: "var(--aqmx-surface-raised)", color: "var(--aqmx-text)" }
39
+ }), ye = "data-aqmx-conflict-actions", kt = Q.line({ attributes: { "data-aqmx-conflict-line": "marker" } }), Tt = Q.line({ attributes: { "data-aqmx-conflict-line": "ours" } }), Mt = Q.line({ attributes: { "data-aqmx-conflict-line": "theirs" } });
40
+ class At extends mt {
41
+ constructor(t, r) {
42
+ super(), this.index = t, this.labels = r;
43
+ }
44
+ index;
45
+ labels;
46
+ eq(t) {
47
+ return t.index === this.index && t.labels === this.labels;
48
+ }
49
+ toDOM(t) {
50
+ const r = document.createElement("div");
51
+ r.setAttribute(ye, String(this.index));
52
+ for (const [o, l] of [
53
+ ["ours", this.labels.useOurs],
54
+ ["theirs", this.labels.useTheirs],
55
+ ["both", this.labels.keepBoth]
56
+ ]) {
57
+ const s = document.createElement("button");
58
+ s.type = "button", s.textContent = l, s.setAttribute("data-aqmx-conflict-choice", o), s.addEventListener("mousedown", (a) => a.preventDefault()), s.addEventListener("click", () => {
59
+ const a = $(t.state.doc)[this.index];
60
+ a !== void 0 && (ke(t, a, o), t.focus());
61
+ }), r.append(s);
62
+ }
63
+ return r;
64
+ }
65
+ ignoreEvent() {
66
+ return !0;
67
+ }
68
+ }
69
+ const $ = (e) => Re(e.iterLines()), de = (e, t) => {
70
+ const r = new st();
71
+ return $(e).forEach((o, l) => {
72
+ const s = e.line(o.start);
73
+ r.add(s.from, s.from, Q.widget({ widget: new At(l, t), block: !0, side: -1 }));
74
+ for (let a = o.start; a <= o.end; a += 1) {
75
+ const n = e.line(a).from, w = a === o.start || a === o.separator || a === o.end ? kt : a < o.separator ? Tt : Mt;
76
+ r.add(n, n, w);
77
+ }
78
+ }), r.finish();
79
+ }, Lt = (e) => lt.define({
80
+ create: (t) => de(t.doc, e),
81
+ update: (t, r) => r.docChanged ? de(r.state.doc, e) : t,
82
+ provide: (t) => K.decorations.from(t)
83
+ }), ke = (e, t, r) => {
84
+ const { doc: o } = e.state, l = (d, u) => {
85
+ const v = [];
86
+ for (let p = d; p <= u; p += 1) v.push(o.line(p).text);
87
+ return v;
88
+ }, s = l(t.start + 1, t.separator - 1), a = l(t.separator + 1, t.end - 1), n = r === "ours" ? s : r === "theirs" ? a : [...s, ...a];
89
+ let w = o.line(t.start).from, f = o.line(t.end).to;
90
+ n.length === 0 && (t.end < o.lines ? f += 1 : t.start > 1 && (w -= 1));
91
+ const i = n.join(`
92
+ `);
93
+ e.dispatch({ changes: { from: w, to: f, insert: i }, selection: ee.cursor(w), scrollIntoView: !0, userEvent: "input.resolve" });
94
+ }, Ct = ({ parent: e, scroll: t, text: r, saved: o, labels: l, onChange: s, onGeometry: a, onSave: n }) => {
95
+ const w = [
96
+ K.scrollHandler.of((d, u, v) => (qt(d, u.head, v, t), !0)),
97
+ ct(),
98
+ at(),
99
+ Ye(),
100
+ ut(),
101
+ dt(),
102
+ ot(),
103
+ K.lineWrapping,
104
+ et({ base: tt }),
105
+ rt(St),
106
+ Lt(l),
107
+ ft.of([
108
+ {
109
+ key: "Mod-s",
110
+ preventDefault: !0,
111
+ run: () => (n(), !0)
112
+ },
113
+ ...it,
114
+ ...Je,
115
+ ...Qe
116
+ ]),
117
+ K.contentAttributes.of({ "aria-label": l.editor, "aria-multiline": "true" }),
118
+ K.updateListener.of((d) => {
119
+ d.docChanged && s(), (d.docChanged || d.heightChanged || d.geometryChanged) && a(d.docChanged);
120
+ }),
121
+ yt
122
+ ], f = Nt(o, w) ?? Ee.create({ doc: r, extensions: w }), i = new K({ state: f, parent: e });
123
+ return {
124
+ text: () => i.state.doc.toString(),
125
+ replaceText: (d) => {
126
+ const u = i.state.doc.toString();
127
+ if (u === d) return;
128
+ let v = 0;
129
+ const p = Math.min(u.length, d.length);
130
+ for (; v < p && u.charCodeAt(v) === d.charCodeAt(v); ) v += 1;
131
+ let h = 0;
132
+ for (; h < p - v && u.charCodeAt(u.length - 1 - h) === d.charCodeAt(d.length - 1 - h); ) h += 1;
133
+ i.dispatch({ changes: { from: v, to: u.length - h, insert: d.slice(v, d.length - h) }, userEvent: "input.replace" });
134
+ },
135
+ reveal: ({ line: d, column: u }, v) => {
136
+ const { doc: p } = i.state, h = p.line(Math.min(Math.max(d, 1), p.lines)), x = Math.min(h.from + u, h.to);
137
+ i.dispatch({ selection: ee.cursor(x), effects: fe(i, x, v, t) });
138
+ },
139
+ showLine: (d) => {
140
+ const { doc: u } = i.state, v = u.line(Math.min(Math.max(d, 1), u.lines)).from;
141
+ i.dispatch({ effects: fe(i, v, null, t) });
142
+ },
143
+ topLine: () => {
144
+ const d = t.scrollerOf(i.scrollDOM), u = X(d, i.contentDOM), v = re(d) + d.clientHeight, { documentTop: p } = i, { doc: h } = i.state;
145
+ let x = i.lineBlockAtHeight(Math.max(u - p, 0));
146
+ for (; h.lineAt(x.from).text.trim() === "" && x.to < h.length; ) {
147
+ const q = i.lineBlockAt(x.to + 1);
148
+ if (p + q.top >= v) break;
149
+ x = q;
150
+ }
151
+ const T = i.coordsAtPos(x.from), y = T === null ? p + x.top : T.top;
152
+ return { line: i.state.doc.lineAt(x.from).number, clientTop: y, offset: y - u };
153
+ },
154
+ lineSpans: (d) => {
155
+ const u = i.scrollDOM, v = u.scrollTop + i.documentTop - X(u, i.contentDOM), { doc: p } = i.state, h = (x) => p.line(Math.min(Math.max(x, 1), p.lines)).from;
156
+ return d.map(({ first: x, last: T }) => ({ top: v + i.lineBlockAt(h(x)).top, bottom: v + i.lineBlockAt(h(T)).bottom }));
157
+ },
158
+ scrollElement: i.scrollDOM,
159
+ focus: () => i.focus(),
160
+ conflicts: () => $(i.state.doc),
161
+ resolveAtCursor: (d) => {
162
+ const u = $(i.state.doc), v = i.state.doc.lineAt(i.state.selection.main.head).number, p = u.find((h) => h.end >= v);
163
+ return p === void 0 ? !1 : (ke(i, p, d), !0);
164
+ },
165
+ nextConflict: () => {
166
+ const d = $(i.state.doc);
167
+ if (d[0] === void 0) return !1;
168
+ const v = i.state.doc.lineAt(i.state.selection.main.head).number, p = d.findIndex((y) => y.start > v), h = p < 0 ? 0 : p, x = i.state.doc.line(d[h].start).from;
169
+ i.dispatch({ selection: ee.cursor(x), effects: K.scrollIntoView(x, { y: "center" }) });
170
+ const T = i.contentDOM.querySelector(`[${ye}="${String(h)}"] button`);
171
+ return T instanceof HTMLElement ? T.focus({ preventScroll: !0 }) : i.focus(), !0;
172
+ },
173
+ save: () => i.state.toJSON(Se),
174
+ destroy: () => i.destroy()
175
+ };
176
+ }, fe = (e, t, r, o) => {
177
+ const l = o.scrollerOf(e.scrollDOM), s = (r === null ? X(l, e.contentDOM) : r) - re(l);
178
+ return K.scrollIntoView(t, { y: "start", yMargin: s });
179
+ }, qt = (e, t, r, o) => {
180
+ const l = Ot(e, t), s = o.scrollerOf(e.scrollDOM), a = re(s), n = X(s, e.contentDOM), w = a + s.clientHeight;
181
+ let f = 0;
182
+ switch (r.y) {
183
+ case "start":
184
+ f = l.top - (a + r.yMargin);
185
+ break;
186
+ case "center":
187
+ f = (l.top + l.bottom) / 2 - (n + w) / 2;
188
+ break;
189
+ case "end":
190
+ f = l.bottom - (w - r.yMargin);
191
+ break;
192
+ default:
193
+ l.top < n + r.yMargin ? f = l.top - (n + r.yMargin) : l.bottom > w - r.yMargin && (f = Math.min(l.bottom - (w - r.yMargin), l.top - (n + r.yMargin)));
194
+ }
195
+ if (f !== 0 && (s.scrollTop += f), r.y === "start") return;
196
+ const i = It(e, t);
197
+ i !== null && o.reveal(i);
198
+ }, Ot = (e, t) => {
199
+ const { node: r, offset: o } = e.domAtPos(t);
200
+ if (r.nodeType === Node.TEXT_NODE) {
201
+ const a = r.ownerDocument?.createRange();
202
+ if (a !== void 0) {
203
+ a.setStart(r, o), a.setEnd(r, o);
204
+ const [n] = a.getClientRects();
205
+ if (n !== void 0 && n.height > 0) return { top: n.top, bottom: n.bottom };
206
+ }
207
+ }
208
+ const l = e.lineBlockAt(t), s = e.documentTop + l.top;
209
+ return { top: s, bottom: s + l.height };
210
+ }, It = (e, t) => {
211
+ const { node: r } = e.domAtPos(t);
212
+ for (let o = r instanceof Element ? r : r.parentElement; o !== null; o = o.parentElement)
213
+ if (o.parentElement === e.contentDOM) return o;
214
+ return null;
215
+ }, Nt = (e, t) => {
216
+ if (e == null) return null;
217
+ try {
218
+ return Ee.fromJSON(e, { extensions: t }, Se);
219
+ } catch {
220
+ return null;
221
+ }
222
+ }, Rt = 610, Dt = 987, me = Object.freeze({ kind: "unavailable" }), _t = "true", Pt = "false", Vt = (e) => e === _t ? !0 : e === Pt ? !1 : null, Ht = (e) => {
223
+ const t = new DOMParser().parseFromString(e, "text/html"), r = [];
224
+ for (const o of t.querySelectorAll("img[data-asset-src], a[data-doc-path]")) {
225
+ const l = o.localName === "img", s = o.getAttribute(l ? "data-asset-src" : "data-doc-path");
226
+ s !== null && r.push({ target: { kind: l ? "asset" : "doc", path: s }, exists: Vt(o.getAttribute(l ? "data-asset-exists" : "data-doc-exists")) });
227
+ }
228
+ return r;
229
+ }, Bt = ({ path: e, instant: t, preview: r, initial: o, onView: l }) => {
230
+ const s = new AbortController(), a = { asset: /* @__PURE__ */ new Map(), doc: /* @__PURE__ */ new Map() };
231
+ let n = o, w = null, f = !1, i = !1, d = null, u = null, v = [], p, h = !1, x, T = null;
232
+ const y = (c) => {
233
+ c.parsed === n.parsed && c.failure === n.failure && c.busy === n.busy && c.tier === n.tier || (n = c, l(c));
234
+ }, q = (c, m) => {
235
+ f || (f = !0, console.error(`[markdown-explorer] ${c}`, ...m === void 0 ? [] : [m]));
236
+ }, I = (c) => {
237
+ if (t !== null)
238
+ for (const { target: m, exists: b } of t.marksOf(c)) a[m.kind].set(m.path, b);
239
+ }, R = () => {
240
+ clearTimeout(x), x = void 0, T?.abort(), T = null;
241
+ }, D = () => {
242
+ u = null, v = [], clearTimeout(p), p = void 0;
243
+ }, _ = (c, m) => {
244
+ y({ parsed: n.parsed, failure: c, busy: !1, tier: m });
245
+ }, E = () => {
246
+ clearTimeout(p), p = v.length === 0 ? void 0 : setTimeout(k, Dt);
247
+ }, S = () => {
248
+ if (t === null || u === null) return;
249
+ const c = u, m = [], b = { asset: /* @__PURE__ */ new Set(), doc: /* @__PURE__ */ new Set() };
250
+ let M;
251
+ try {
252
+ M = t.sanitize(c.html, (L) => {
253
+ const W = a[L.kind].get(L.path);
254
+ return W === void 0 && !b[L.kind].has(L.path) && (b[L.kind].add(L.path), m.push(L)), W ?? void 0;
255
+ });
256
+ } catch (L) {
257
+ D(), L instanceof wt ? _(J("too-large"), "instant") : (q("the draft sanitizer failed", L), _(J("failed"), "instant"));
258
+ return;
259
+ }
260
+ v = m;
261
+ const H = n.parsed !== null && n.parsed.htmlContent === M && n.parsed.headings === c.headings ? n.parsed : { htmlContent: M, headings: c.headings };
262
+ y({ parsed: H, failure: null, busy: !1, tier: "instant" }), E();
263
+ }, k = () => {
264
+ if (p = void 0, h || u === null || v.length === 0) return;
265
+ h = !0;
266
+ const c = v;
267
+ r(e, u.text, s.signal).then((m) => {
268
+ if (h = !1, !(s.signal.aborted || !("kind" in m))) {
269
+ m.kind === "preview" && I(m.htmlContent);
270
+ for (const b of c) a[b.kind].has(b.path) || a[b.kind].set(b.path, null);
271
+ S();
272
+ }
273
+ });
274
+ }, P = (c) => {
275
+ R(), D(), y({ ...n, busy: !0, tier: "server" });
276
+ const m = new AbortController();
277
+ T = m, x = setTimeout(() => {
278
+ x = void 0, r(e, c, m.signal).then((b) => {
279
+ if (!(m.signal.aborted || !("kind" in b))) {
280
+ if (T = null, b.kind === "failure") {
281
+ _(b.failure, "server");
282
+ return;
283
+ }
284
+ I(b.htmlContent), y({ parsed: { htmlContent: b.htmlContent, headings: b.headings }, failure: null, busy: !1, tier: "server" });
285
+ }
286
+ });
287
+ }, Rt);
288
+ }, g = async (c, m) => {
289
+ let b;
290
+ try {
291
+ b = await c.convert({ path: e, text: m }, s.signal);
292
+ } catch (H) {
293
+ return s.signal.aborted || q("the draft converter rejected a conversion, which its contract allows only once the signal aborted", H), me;
294
+ }
295
+ const M = vt(b);
296
+ return M === null ? b : (q(`the draft converter answered outside its contract: ${M}`), me);
297
+ }, V = (c, m) => {
298
+ switch (m.kind) {
299
+ case "preview":
300
+ if (R(), m.headings.length > gt) {
301
+ D(), _(J("too-large"), "instant");
302
+ return;
303
+ }
304
+ u = { text: c, html: m.htmlContent, headings: m.headings.map(({ text: b, depth: M, id: H }) => ({ text: b, depth: M, id: H })) }, S();
305
+ return;
306
+ case "failure":
307
+ R(), D(), _(J(m.failure.reason, m.failure.code), "instant");
308
+ return;
309
+ case "unavailable":
310
+ P(c);
311
+ return;
312
+ }
313
+ }, F = async (c) => {
314
+ i = !0;
315
+ try {
316
+ for (; d !== null && !s.signal.aborted; ) {
317
+ const m = d;
318
+ d = null;
319
+ const b = await g(c, m);
320
+ if (s.signal.aborted) return;
321
+ V(m, b);
322
+ }
323
+ } finally {
324
+ i = !1;
325
+ }
326
+ };
327
+ return t !== null && t.seed !== null && I(t.seed), {
328
+ update: (c) => {
329
+ if (!(s.signal.aborted || c === w)) {
330
+ if (w = c, clearTimeout(p), p = void 0, t === null) {
331
+ P(c);
332
+ return;
333
+ }
334
+ d = c, i || F(t.converter);
335
+ }
336
+ },
337
+ dispose: () => {
338
+ s.abort(), d = null, clearTimeout(p), p = void 0, R();
339
+ }
340
+ };
341
+ }, Ft = (e, t) => bt(xt(window), e, t, Et), zt = (e, t) => e.flatMap((r, o) => {
342
+ const l = t[o];
343
+ return l === void 0 ? [] : [
344
+ { editor: l.top, preview: r.top },
345
+ { editor: l.bottom, preview: r.bottom }
346
+ ];
347
+ }), G = (e, t) => {
348
+ const r = e[t];
349
+ if (r === void 0) throw new RangeError(`[markdown-explorer] a scroll map index must be in 0..${String(e.length - 1)}; got ${String(t)}`);
350
+ return r;
351
+ }, Kt = (e) => {
352
+ const t = [];
353
+ for (const o of e) {
354
+ let l = 0, s = t.length;
355
+ for (; l < s; ) {
356
+ const a = l + s >> 1;
357
+ G(t, a).pair.preview <= o.preview ? l = a + 1 : s = a;
358
+ }
359
+ t[l] = { pair: o, before: l === 0 ? void 0 : G(t, l - 1) };
360
+ }
361
+ const r = [];
362
+ for (let o = t.at(-1); o !== void 0; o = o.before) r.push(o.pair);
363
+ return r.reverse();
364
+ }, Gt = (e, t) => {
365
+ const r = Math.max(t.editorMax, 0), o = Math.max(t.previewMax, 0), l = r - t.editorViewport, s = o - t.previewViewport, a = e.filter((f) => f.editor > 0 && f.editor < l && f.preview > 0 && f.preview < s).sort((f, i) => f.editor - i.editor || f.preview - i.preview);
366
+ let n = { editor: 0, preview: 0 };
367
+ const w = [n];
368
+ for (const f of Kt(a))
369
+ f.editor > n.editor && f.preview > n.preview && (w.push(f), n = f);
370
+ return w.push({ editor: r, preview: o }), {
371
+ toPreview: (f) => pe(w, "editor", "preview", f),
372
+ toEditor: (f) => pe(w, "preview", "editor", f)
373
+ };
374
+ }, pe = (e, t, r, o) => {
375
+ const l = G(e, 0), s = G(e, e.length - 1);
376
+ if (Number.isNaN(o) || o <= l[t]) return l[r];
377
+ if (o >= s[t]) return s[r];
378
+ let a = 0, n = e.length - 1;
379
+ for (; n - a > 1; ) {
380
+ const i = a + n >> 1;
381
+ G(e, i)[t] <= o ? a = i : n = i;
382
+ }
383
+ const w = G(e, a), f = G(e, n);
384
+ return w[r] + (f[r] - w[r]) * (o - w[t]) / (f[t] - w[t]);
385
+ }, Wt = 610, he = 89, ve = 0.5, jt = ({ editor: e, preview: t, previewBlocks: r, editorSpans: o, requestFrame: l, cancelFrame: s, now: a }) => {
386
+ let n = "editor", w = 0, f = null, i = null, d = !1, u = null, v = null;
387
+ const p = { editor: 0, preview: 0 }, h = { editor: null, preview: null };
388
+ let x = null, T = () => {
389
+ };
390
+ const y = (g) => {
391
+ f !== null && g !== f || (n = g, w = a() + Wt);
392
+ }, q = (g, V) => {
393
+ g !== n && f === null && a() >= w && !I(g, V) && (n = g), S();
394
+ }, I = (g, V) => {
395
+ const F = h[g];
396
+ return a() < p[g] || F !== null && Math.abs(V.position() - F) < ve;
397
+ }, R = e.onScroll(() => q("editor", e)), D = e.onIntent(() => y("editor")), _ = () => {
398
+ const g = t();
399
+ return g !== x && (T(), x = g, T = g === null ? () => {
400
+ } : E(g)), g;
401
+ }, E = (g) => {
402
+ const V = g.onScroll(() => q("preview", g)), F = g.onIntent(() => y("preview"));
403
+ return () => {
404
+ V(), F();
405
+ };
406
+ }, S = () => {
407
+ !d && i === null && (i = l(k));
408
+ }, k = () => {
409
+ i = null;
410
+ const g = _();
411
+ g !== null && (u ??= r(), v ??= Gt(zt(u, o(u)), { editorMax: e.max(), previewMax: g.max(), editorViewport: e.viewport(), previewViewport: g.viewport() }), n === "editor" ? P(g, "preview", v.toPreview(e.position())) : P(e, "editor", v.toEditor(g.position())));
412
+ }, P = (g, V, F) => {
413
+ if (Math.abs(g.position() - F) < ve) {
414
+ h[V] = g.position();
415
+ return;
416
+ }
417
+ p[V] = a() + he, h[V] = g.scrollTo(F);
418
+ };
419
+ return S(), {
420
+ editorChanged: (g) => {
421
+ v = null, g && y("editor"), S();
422
+ },
423
+ previewChanged: () => {
424
+ u = null, v = null, p.preview = a() + he, S();
425
+ },
426
+ showAlone: (g) => {
427
+ f = g, !(g === null || g === n) && (n = g, w = 0, S());
428
+ },
429
+ dispose: () => {
430
+ d = !0, i !== null && s(i), i = null, R(), D(), T();
431
+ }
432
+ };
433
+ }, ge = Object.freeze(["wheel", "pointerdown", "keydown", "focusin"]), Te = (e) => ({
434
+ position: () => e.scrollTop,
435
+ max: () => Math.max(e.scrollHeight - e.clientHeight, 0),
436
+ viewport: () => e.clientHeight,
437
+ scrollTo: (t) => (e.scrollTo({ top: t, behavior: "instant" }), e.scrollTop),
438
+ onScroll: (t) => (e.addEventListener("scroll", t, { passive: !0 }), () => e.removeEventListener("scroll", t)),
439
+ onIntent: (t) => {
440
+ for (const r of ge) e.addEventListener(r, t, { capture: !0, passive: !0 });
441
+ return () => {
442
+ for (const r of ge) e.removeEventListener(r, t, { capture: !0 });
443
+ };
444
+ }
445
+ }), j = "[data-aqmd-scroll-container]", Ut = "[data-aqmd-content]", $t = `[${xe}]`, Xt = Object.freeze(["p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8"]), Me = (e, t) => {
446
+ const r = Ve(e.peek(t));
447
+ return r !== null && r.document.kind === "markdown" ? { htmlContent: r.document.htmlContent, headings: r.document.headings } : null;
448
+ }, Yt = (e, t, r, o, l) => {
449
+ if (r === void 0) return null;
450
+ let s;
451
+ try {
452
+ s = Ft(e, t);
453
+ } catch (a) {
454
+ return console.error("[markdown-explorer] the draft sanitizer cannot run in this browser, so the server renders the preview", a), null;
455
+ }
456
+ return { converter: r, sanitize: s, marksOf: Ht, seed: Me(o, l)?.htmlContent ?? null };
457
+ }, Jt = (e) => {
458
+ const t = [];
459
+ let r = null;
460
+ for (const o of e.querySelectorAll($t)) {
461
+ const l = ht(o.getAttribute(xe));
462
+ if (l === null || o.getClientRects().length === 0) continue;
463
+ r ??= e.scrollTop - X(e, o);
464
+ const { top: s, bottom: a } = o.getBoundingClientRect();
465
+ t.push({ first: l.first, last: l.last, top: r + s, bottom: r + a });
466
+ }
467
+ return t;
468
+ }, Qt = ({ path: e, readDraft: t, viewer: r, concealed: o, onEditAt: l, onLayout: s, handleRef: a }) => {
469
+ const { config: n, data: w, documents: f, draftConverter: i, labels: d } = ne(), [u, v] = Oe(() => ({ parsed: Me(f, e), failure: null, busy: !0, tier: i === void 0 ? "server" : "instant" })), p = N(null), h = N(null), x = N(u);
470
+ B(() => {
471
+ x.current = u;
472
+ });
473
+ const T = se(() => t());
474
+ we(() => {
475
+ const E = Yt(n.iframeHosts, n.dataPath, i, f, e), S = Bt({ path: e, instant: E, preview: w.preview, initial: x.current, onView: (k) => Ne(() => v(k)) });
476
+ return h.current = S, S.update(T()), () => {
477
+ S.dispose(), h.current = null;
478
+ };
479
+ }, [n, w, f, i, e]);
480
+ const y = se(() => s());
481
+ B(() => {
482
+ u.parsed !== null && y();
483
+ }, [u.parsed]);
484
+ const q = u.parsed !== null;
485
+ B(() => {
486
+ const E = q ? p.current?.querySelector(j) : null;
487
+ if (!(E instanceof HTMLElement)) return;
488
+ E.tabIndex = 0;
489
+ const S = new ResizeObserver(() => y());
490
+ S.observe(E);
491
+ const k = E.querySelector(Ut);
492
+ return k !== null && S.observe(k), () => S.disconnect();
493
+ }, [q]);
494
+ const I = N(null);
495
+ Ie(
496
+ a,
497
+ () => ({
498
+ draft: (E) => {
499
+ h.current?.update(E);
500
+ },
501
+ scrollPort: () => {
502
+ const E = p.current?.querySelector(j);
503
+ return E instanceof HTMLElement ? (I.current?.scroller !== E && (I.current = { scroller: E, port: Te(E) }), I.current.port) : null;
504
+ },
505
+ blocks: () => {
506
+ const E = p.current?.querySelector(j);
507
+ return E instanceof HTMLElement ? Jt(E) : [];
508
+ },
509
+ focus: () => {
510
+ const E = p.current?.querySelector(j);
511
+ E instanceof HTMLElement && E.focus({ preventScroll: !0 });
512
+ },
513
+ anchor: (E) => {
514
+ const S = p.current, k = S?.querySelector(j);
515
+ return S === null || !(k instanceof HTMLElement) ? null : Pe(S, E(k));
516
+ },
517
+ place: ({ line: E, clientTop: S }, k) => {
518
+ const P = p.current, g = P === null ? null : De(P, E);
519
+ g !== null && (k(g).scrollTop += _e(g, E) - S);
520
+ }
521
+ }),
522
+ []
523
+ );
524
+ const R = (E) => {
525
+ const S = p.current, { target: k } = E;
526
+ if (l === void 0 || S === null || !(k instanceof Element) || !He(k)) return;
527
+ const P = Be(k, S, S.ownerDocument.getSelection());
528
+ P !== null && l(P);
529
+ }, D = Z(() => ({ ...r, showToc: !1, showStyle: !1, showAlign: !1, renderScrollBar: void 0 }), [r]), _ = Z(
530
+ () => u.parsed === null ? null : /* @__PURE__ */ O("div", { className: "aqmx-document-viewer", children: /* @__PURE__ */ O(pt, { ...D, parseResult: u.parsed, content: null }) }),
531
+ [D, u.parsed]
532
+ );
533
+ return /* @__PURE__ */ te(
534
+ "section",
535
+ {
536
+ ref: p,
537
+ className: "aqmx-edit-preview",
538
+ "aria-label": d.previewRegion,
539
+ "aria-busy": u.busy,
540
+ inert: o !== null,
541
+ hidden: o === "removed",
542
+ "data-aqmx-preview-tier": u.tier,
543
+ "data-testid": "markdown-explorer-edit-preview",
544
+ onDoubleClick: l === void 0 ? void 0 : R,
545
+ children: [
546
+ u.failure === null ? null : /* @__PURE__ */ O("p", { className: "aqmx-callout aqmx-document-notice", "data-aqmx-tone": "warning", "data-testid": "markdown-explorer-edit-preview-failure", children: d.previewFailed(be(d, u.failure.reason)) }),
547
+ _ ?? (u.failure === null ? /* @__PURE__ */ O("div", { className: "aqmx-skeleton", "aria-hidden": "true", "data-testid": "markdown-explorer-edit-preview-skeleton", children: Xt.map((E) => /* @__PURE__ */ O("span", {}, E)) }) : null),
548
+ u.busy ? /* @__PURE__ */ O("p", { className: "aqmx-sr-only", children: d.previewUpdating }) : null
549
+ ]
550
+ }
551
+ );
552
+ }, Zt = 144, er = Object.freeze(["e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8"]), tr = (e) => {
553
+ if (typeof e != "object" || e === null) return !1;
554
+ const { generation: t, topLine: r } = e;
555
+ return "state" in e && typeof t == "number" && typeof r == "number";
556
+ }, rr = (e) => {
557
+ e.stopPropagation();
558
+ }, br = (e) => {
559
+ const { path: t, name: r, session: o } = e, { edits: l, labels: s, announcer: a } = ne(), { phase: n } = o, w = U(() => l.retry(t), [l, t]);
560
+ switch (we(() => {
561
+ n.kind === "failed" && a.announce(s.documentFailed(r, be(s, n.failure.reason)));
562
+ }, [n, a, s, r]), n.kind) {
563
+ case "opening":
564
+ return /* @__PURE__ */ te(qe, { children: [
565
+ /* @__PURE__ */ O("div", { className: "aqmx-skeleton", "aria-hidden": "true", "data-testid": "markdown-explorer-edit-skeleton", children: er.map((f) => /* @__PURE__ */ O("span", {}, f)) }),
566
+ /* @__PURE__ */ O("p", { className: "aqmx-sr-only", children: s.editLoading })
567
+ ] });
568
+ case "grant":
569
+ return /* @__PURE__ */ O("div", { className: "aqmx-edit-message", children: /* @__PURE__ */ O(ze, { href: n.href, onRetry: w, testId: "markdown-explorer-edit-grant" }) });
570
+ case "failed":
571
+ return /* @__PURE__ */ O("div", { className: "aqmx-edit-message", children: /* @__PURE__ */ O(Fe, { failure: n.failure, alert: !1, onRetry: w, testId: "markdown-explorer-edit-failure" }) });
572
+ case "editing":
573
+ return /* @__PURE__ */ O(nr, { ...e });
574
+ }
575
+ }, nr = ({ path: e, name: t, session: r, layout: o, viewer: l, region: s, paneScroll: a }) => {
576
+ const { edits: n, labels: w } = ne(), f = N(null), i = N(null), d = N(null), u = N(null), v = o === "fill", p = Ke(), h = Ge(r, p, v), x = h !== "editor" || r.previewPrepared, T = x && v, y = N({ layout: o, region: s, paneScroll: a });
577
+ B(() => {
578
+ y.current = { layout: o, region: s, paneScroll: a };
579
+ });
580
+ const q = N(r.generation);
581
+ B(() => {
582
+ q.current = r.generation;
583
+ });
584
+ const I = N({ view: h, previewMounted: x });
585
+ B(() => {
586
+ I.current = { view: h, previewMounted: x };
587
+ });
588
+ const R = N(null), D = N(null), _ = N(!1), E = Z(() => ({ editor: w.editorLabel(t), useOurs: w.conflictUseOurs, useTheirs: w.conflictUseTheirs, keepBoth: w.conflictKeepBoth }), [w, t]), S = U((c) => {
589
+ const { region: m, paneScroll: b } = y.current, M = m();
590
+ return M === null ? c : b.scrollerOf(M, c);
591
+ }, []);
592
+ B(() => {
593
+ const c = f.current, m = n.draft(e);
594
+ if (c === null || m === null) return;
595
+ const b = n.editorState.get(e), M = tr(b) && b.generation === q.current ? b : null;
596
+ let H = null, L = null;
597
+ const W = () => {
598
+ H !== null && clearTimeout(H), H = null, L !== null && n.setDraft(e, L.text());
599
+ }, z = Ct({
600
+ parent: c,
601
+ scroll: {
602
+ scrollerOf: (C) => {
603
+ const { layout: Y, region: Le, paneScroll: Ce } = y.current, le = Le();
604
+ return Y === "fill" || le === null ? C : Ce.scrollerOf(le, C);
605
+ },
606
+ reveal: (C) => y.current.paneScroll.reveal(C)
607
+ },
608
+ text: m,
609
+ saved: M === null ? void 0 : M.state,
610
+ labels: E,
611
+ onGeometry: (C) => u.current?.editorChanged(C),
612
+ onChange: () => {
613
+ H ??= setTimeout(W, Zt), I.current.previewMounted && L !== null && d.current?.draft(L.text());
614
+ },
615
+ onSave: () => {
616
+ n.save(e);
617
+ }
618
+ });
619
+ L = z, i.current = z;
620
+ const oe = () => {
621
+ const C = z.topLine();
622
+ return { line: C.line, clientTop: C.clientTop };
623
+ }, ie = () => d.current?.anchor(S) ?? null, Ae = n.bindEditor(e, {
624
+ flush: W,
625
+ adopt: (C) => z.replaceText(C),
626
+ returnPoint: () => {
627
+ const C = I.current.view === "preview" && y.current.layout === "content" ? ie() : null;
628
+ return C === null ? oe() : { line: C.lines.first, clientTop: C.clientTop };
629
+ },
630
+ nextConflict: () => I.current.view !== "preview" ? z.nextConflict() : z.conflicts().length === 0 ? !1 : (_.current = !0, n.setView(e, "editor"), !0),
631
+ beforeView: (C) => {
632
+ if (y.current.layout === "fill") return;
633
+ if (C === "preview") {
634
+ R.current = { to: "preview", point: oe() };
635
+ return;
636
+ }
637
+ const Y = ie();
638
+ R.current = Y === null ? null : { to: "editor", anchor: Y };
639
+ }
640
+ });
641
+ return M !== null && z.showLine(M.topLine), () => {
642
+ W(), n.editorState.set(e, { state: z.save(), generation: q.current, topLine: z.topLine().line }), Ae(), z.destroy(), L = null, i.current = null;
643
+ };
644
+ }, [n, e, E, S]);
645
+ const { request: k } = r;
646
+ B(() => {
647
+ const c = i.current, m = n.draft(e);
648
+ c === null || m === null || k === null || (k.anchor !== null && c.reveal(ce(m, k.anchor), k.anchor.clientTop), c.focus(), n.consumeRequest(e));
649
+ }, [n, e, k, E]), B(() => {
650
+ h !== "editor" && !r.previewPrepared && n.preparePreview(e);
651
+ }, [n, e, h, r.previewPrepared]), B(() => {
652
+ const c = R.current, m = D.current, b = _.current;
653
+ R.current = null, D.current = null, _.current = !1;
654
+ const M = i.current;
655
+ if (c?.to === "preview") d.current?.place(c.point, S);
656
+ else if (c?.to === "editor" && M !== null) {
657
+ const H = n.draft(e);
658
+ H !== null && M.reveal(ce(H, c.anchor), c.anchor.clientTop);
659
+ }
660
+ b ? M?.nextConflict() : m === "editor" ? M?.focus() : m === "preview" && d.current?.focus();
661
+ }, [n, e, h, S]);
662
+ const P = U(() => {
663
+ const c = i.current;
664
+ if (c === null) throw new Error("[markdown-explorer] the preview read the draft before the editor was mounted");
665
+ return c.text();
666
+ }, []);
667
+ B(() => {
668
+ const c = i.current;
669
+ if (!T || c === null) return;
670
+ const m = jt({
671
+ editor: Te(c.scrollElement),
672
+ preview: () => d.current?.scrollPort() ?? null,
673
+ previewBlocks: () => d.current?.blocks() ?? [],
674
+ editorSpans: (b) => c.lineSpans(b),
675
+ requestFrame: (b) => requestAnimationFrame(b),
676
+ cancelFrame: (b) => cancelAnimationFrame(b),
677
+ now: () => performance.now()
678
+ });
679
+ return m.showAlone(ae(I.current.view)), u.current = m, () => {
680
+ m.dispose(), u.current = null;
681
+ };
682
+ }, [T, n, e, E]), B(() => {
683
+ u.current?.showAlone(ae(h));
684
+ }, [h]);
685
+ const g = U(() => u.current?.previewChanged(), []), V = U((c) => n.start(e, c), [n, e]);
686
+ return (
687
+ /* @__PURE__ */ te("div", { className: "aqmx-edit", "data-aqmx-edit-layout": o, "data-aqmx-edit-view": h, "data-testid": "markdown-explorer-edit", onKeyDown: (c) => {
688
+ if (c.key === "Escape") {
689
+ ue(c);
690
+ return;
691
+ }
692
+ if (!je(c.nativeEvent, Ue(navigator.platform))) return;
693
+ ue(c);
694
+ const m = Xe(h);
695
+ D.current = m, $e(n, e, m, v);
696
+ }, onDoubleClick: rr, children: [
697
+ /* @__PURE__ */ O("div", { ref: f, className: "aqmx-edit-editor", "data-testid": "markdown-explorer-editor", inert: h === "preview", hidden: h === "preview" && !v, ...We }),
698
+ x ? /* @__PURE__ */ O(Qt, { path: e, readDraft: P, viewer: l, concealed: h !== "editor" ? null : v ? "overlay" : "removed", onEditAt: h === "preview" ? V : void 0, onLayout: g, handleRef: d }) : null
699
+ ] })
700
+ );
701
+ };
702
+ export {
703
+ Zt as DRAFT_WRITE_INTERVAL_MS,
704
+ br as default
705
+ };