@aiquants/markdown-explorer 0.19.0 → 0.21.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 (32) hide show
  1. package/CHANGELOG.md +65 -1
  2. package/README.md +18 -9
  3. package/dist/EditSurface-Bwl4S1vQ.cjs +7 -0
  4. package/dist/EditSurface-rKoy_ndP.js +880 -0
  5. package/dist/{ExplorerSplit-LiI3IUZn.js → ExplorerSplit-3WMRDA7G.js} +1 -1
  6. package/dist/{ExplorerSplit-BDkBN8A0.cjs → ExplorerSplit-CdqEKz-I.cjs} +1 -1
  7. package/dist/{MultiLayout-BOaQK8fp.cjs → MultiLayout-BEg1X8Jx.cjs} +1 -1
  8. package/dist/{MultiLayout-KWacTPP8.js → MultiLayout-Gf1xT-Le.js} +2 -2
  9. package/dist/{NativeScrollBar-zzpty31a.js → NativeScrollBar-0cpp2OIu.js} +1 -1
  10. package/dist/{NativeScrollBar-BPgV3kp-.cjs → NativeScrollBar-idIQsvCv.cjs} +1 -1
  11. package/dist/{TreeLayout-S5TMf8_A.cjs → TreeLayout-DJY_WqzP.cjs} +1 -1
  12. package/dist/{TreeLayout-C8IP4cjY.js → TreeLayout-DzZCgMTt.js} +2 -2
  13. package/dist/core/labels.d.cts +26 -0
  14. package/dist/core/labels.d.ts +26 -0
  15. package/dist/{index-Dz01kBab.js → index-CJgHGeZs.js} +1323 -1238
  16. package/dist/index-bMaSoQTe.cjs +13 -0
  17. package/dist/index.cjs +1 -1
  18. package/dist/index.js +1 -1
  19. package/dist/{merge-ClVgYrkS.js → merge-BOcofJN5.js} +1 -1
  20. package/dist/{merge-B7Lkcdz0.cjs → merge-BVk6pvO_.cjs} +1 -1
  21. package/dist/sanitizePolicy-BWQtWhsL.js +98 -0
  22. package/dist/sanitizePolicy-gEXeavb-.cjs +1 -0
  23. package/dist/sanitizeWorker.cjs +1 -1
  24. package/dist/sanitizeWorker.js +1 -1
  25. package/docs/behavior/localization.md +5 -1
  26. package/docs/behavior/security.md +18 -8
  27. package/package.json +9 -7
  28. package/dist/EditSurface-4VX20isB.cjs +0 -2
  29. package/dist/EditSurface-Ckg-3iTA.js +0 -786
  30. package/dist/index-DrKHJNY4.cjs +0 -13
  31. package/dist/sanitizePolicy-DlrakwA4.js +0 -96
  32. package/dist/sanitizePolicy-DrIMuYCb.cjs +0 -1
@@ -0,0 +1,880 @@
1
+ import { jsx as I, jsxs as Q, Fragment as Ge } from "react/jsx-runtime";
2
+ import { useState as oe, useRef as P, useLayoutEffect as D, useEffectEvent as pe, useEffect as qe, useImperativeHandle as $e, useMemo as re, startTransition as Ke, useCallback as K, useId as je } from "react";
3
+ import { v as Z, g as ue, h as Xe, u as ie, i as Ye, l as Je, j as Qe, k as Re, m as Ze, n as et, o as tt, p as nt, E as rt, q as ot, s as it, t as he, w as ge, A as st, x as ve, y as lt, z as at, B as ct, G as ut } from "./index-CJgHGeZs.js";
4
+ import { history as dt, historyKeymap as ft, defaultKeymap as mt, historyField as pt, redo as ht, undo as gt, isolateHistory as vt } from "@codemirror/commands";
5
+ import { markdown as wt, markdownLanguage as Et } from "@codemirror/lang-markdown";
6
+ import { syntaxHighlighting as bt, HighlightStyle as St, ensureSyntaxTree as xt } from "@codemirror/language";
7
+ import { highlightSelectionMatches as yt, searchKeymap as Tt } from "@codemirror/search";
8
+ import { EditorState as _e, EditorSelection as ce, StateField as kt, RangeSetBuilder as At } from "@codemirror/state";
9
+ import { EditorView as U, lineNumbers as Mt, highlightActiveLineGutter as Lt, drawSelection as Ot, highlightActiveLine as It, keymap as Ct, Decoration as se, WidgetType as qt } from "@codemirror/view";
10
+ import { tags as R } from "@lezer/highlight";
11
+ import { MarkdownViewer as Rt } from "@aiquants/markdown";
12
+ import { SOURCE_LINES_ATTRIBUTE as Ne, parseSourceLines as _t } from "@aiquants/markdown/source-lines";
13
+ import { d as Nt, f as ee, E as Pt } from "./dataRoute-CObbjNxG.js";
14
+ import { H as Dt, c as Bt, M as Vt } from "./sanitizePolicy-BWQtWhsL.js";
15
+ import Ht from "isomorphic-dompurify";
16
+ import { PanelGroup as zt, Panel as we, PanelResizeHandle as Ft } from "@aiquants/resize-panels";
17
+ import { C as Wt, F as Ut, E as Gt, a as $t, b as te, d as Kt } from "./location-B-m9scLf.js";
18
+ import { u as jt, a as Xt } from "./useSplitPersistence-Cc4tDxB4.js";
19
+ const Pe = { history: pt }, Yt = St.define([
20
+ { tag: [R.heading1, R.heading2, R.heading3, R.heading4, R.heading5, R.heading6, R.heading], color: "var(--aqmx-syntax-heading)", fontWeight: "700" },
21
+ { tag: R.strong, fontWeight: "700" },
22
+ { tag: R.emphasis, fontStyle: "italic" },
23
+ { tag: R.strikethrough, textDecoration: "line-through" },
24
+ { tag: R.link, color: "var(--aqmx-syntax-link)", textDecoration: "underline" },
25
+ { tag: R.url, color: "var(--aqmx-syntax-url)" },
26
+ { tag: R.monospace, color: "var(--aqmx-syntax-code)" },
27
+ { tag: R.quote, color: "var(--aqmx-syntax-quote)" },
28
+ { tag: [R.processingInstruction, R.contentSeparator, R.labelName, R.meta], color: "var(--aqmx-syntax-mark)" }
29
+ ]), Jt = U.theme({
30
+ "&": { height: "100%", color: "var(--aqmx-text)", backgroundColor: "var(--aqmx-surface)", fontSize: "var(--aqmx-font-size-title)" },
31
+ "&.cm-focused": { outline: "none" },
32
+ ".cm-scroller": { fontFamily: 'ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace', lineHeight: "var(--aqmx-line-height-prose)" },
33
+ ".cm-content": { caretColor: "var(--aqmx-text)", paddingBlock: "var(--aqmx-space-6)" },
34
+ ".cm-line": { paddingInline: "var(--aqmx-space-6)" },
35
+ "&.cm-focused .cm-cursor": { borderLeftColor: "var(--aqmx-text)" },
36
+ "&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection": { backgroundColor: "var(--aqmx-editor-selection)" },
37
+ ".cm-activeLine": { backgroundColor: "var(--aqmx-editor-active-line)" },
38
+ ".cm-gutters": { backgroundColor: "var(--aqmx-surface-raised)", color: "var(--aqmx-editor-gutter)", borderInlineEnd: "var(--aqmx-space-1) solid var(--aqmx-border)" },
39
+ ".cm-activeLineGutter": { backgroundColor: "var(--aqmx-editor-active-line)" },
40
+ ".cm-searchMatch": { outline: "var(--aqmx-space-1) solid var(--aqmx-border-strong)" },
41
+ ".cm-panels": { backgroundColor: "var(--aqmx-surface-raised)", color: "var(--aqmx-text)" }
42
+ }), De = "data-aqmx-conflict-actions", Qt = se.line({ attributes: { "data-aqmx-conflict-line": "marker" } }), Zt = se.line({ attributes: { "data-aqmx-conflict-line": "ours" } }), en = se.line({ attributes: { "data-aqmx-conflict-line": "theirs" } });
43
+ class tn extends qt {
44
+ constructor(t, n) {
45
+ super(), this.index = t, this.labels = n;
46
+ }
47
+ index;
48
+ labels;
49
+ eq(t) {
50
+ return t.index === this.index && t.labels === this.labels;
51
+ }
52
+ toDOM(t) {
53
+ const n = document.createElement("div");
54
+ n.setAttribute(De, String(this.index));
55
+ for (const [r, a] of [
56
+ ["ours", this.labels.useOurs],
57
+ ["theirs", this.labels.useTheirs],
58
+ ["both", this.labels.keepBoth]
59
+ ]) {
60
+ const i = document.createElement("button");
61
+ i.type = "button", i.textContent = a, i.setAttribute("data-aqmx-conflict-choice", r), i.addEventListener("mousedown", (c) => c.preventDefault()), i.addEventListener("click", () => {
62
+ const c = J(t.state.doc)[this.index];
63
+ c !== void 0 && (Be(t, c, r), t.focus());
64
+ }), n.append(i);
65
+ }
66
+ return n;
67
+ }
68
+ ignoreEvent() {
69
+ return !0;
70
+ }
71
+ }
72
+ const J = (e) => Xe(e.iterLines()), Ee = (e, t) => {
73
+ const n = new At();
74
+ return J(e).forEach((r, a) => {
75
+ const i = e.line(r.start);
76
+ n.add(i.from, i.from, se.widget({ widget: new tn(a, t), block: !0, side: -1 }));
77
+ for (let c = r.start; c <= r.end; c += 1) {
78
+ const o = e.line(c).from, f = c === r.start || c === r.separator || c === r.end ? Qt : c < r.separator ? Zt : en;
79
+ n.add(o, o, f);
80
+ }
81
+ }), n.finish();
82
+ }, nn = (e) => kt.define({
83
+ create: (t) => Ee(t.doc, e),
84
+ update: (t, n) => n.docChanged ? Ee(n.state.doc, e) : t,
85
+ provide: (t) => U.decorations.from(t)
86
+ }), Be = (e, t, n) => {
87
+ const { doc: r } = e.state, a = (s, S) => {
88
+ const l = [];
89
+ for (let u = s; u <= S; u += 1) l.push(r.line(u).text);
90
+ return l;
91
+ }, i = a(t.start + 1, t.separator - 1), c = a(t.separator + 1, t.end - 1), o = n === "ours" ? i : n === "theirs" ? c : [...i, ...c];
92
+ let f = r.line(t.start).from, d = r.line(t.end).to;
93
+ o.length === 0 && (t.end < r.lines ? d += 1 : t.start > 1 && (f -= 1));
94
+ const m = o.join(`
95
+ `);
96
+ e.dispatch({ changes: { from: f, to: d, insert: m }, selection: ce.cursor(f), scrollIntoView: !0, userEvent: "input.resolve" });
97
+ }, be = "input.preview", rn = 500, on = /^([ >+*\-.)\d]*?)(?:`{3}|~{3})/u, sn = /^(?:[-+*]|\d{1,9}[.)])(?= )/u, Ve = /^[ \t]*$/u, ln = (e) => {
98
+ let t = "", n = 0;
99
+ for (; n < e.length; ) {
100
+ const r = e.slice(n);
101
+ if (r.startsWith(">"))
102
+ t += "> ", n += r.startsWith("> ") ? 2 : 1;
103
+ else if (r.startsWith(" "))
104
+ t += " ", n += 1;
105
+ else {
106
+ const a = sn.exec(r)?.[0];
107
+ if (a === void 0) return null;
108
+ t += " ".repeat(a.length), n += a.length;
109
+ }
110
+ }
111
+ return t;
112
+ }, an = (e, t, n) => {
113
+ if (e.startsWith(t)) {
114
+ const r = e.slice(t.length);
115
+ return n && Ve.test(r) ? "" : r;
116
+ }
117
+ return t.startsWith(e) && e.length >= t.trimEnd().length ? "" : null;
118
+ }, cn = (e, t, n) => e.split(`
119
+ `).map((r, a, i) => a === i.length - 1 && r === "" ? "" : (n ? Ve.test(r) : r === "") ? t.trimEnd() : `${t}${r}`).join(`
120
+ `), un = (e, t, n) => {
121
+ const { doc: r } = e;
122
+ if (!Number.isInteger(t) || t < 1 || t > r.lines) return null;
123
+ const a = r.line(t), i = on.exec(a.text)?.[1], c = i === void 0 ? null : ln(i);
124
+ if (i === void 0 || c === null) return null;
125
+ const o = Math.min(r.lines, t + n.split(`
126
+ `).length + 1), f = xt(e, r.line(o).to, rn);
127
+ if (f === null) return null;
128
+ const d = f.resolveInner(a.from + i.length, 1), m = d.parent;
129
+ if (d.name !== "CodeMark" || m === null || m.name !== "FencedCode" || m.from !== d.from) return null;
130
+ const [, ...s] = m.getChildren("CodeMark"), S = s.at(-1), l = S === void 0 ? r.lineAt(m.to).number : r.lineAt(S.from).number - 1;
131
+ let u = m.parent;
132
+ for (; u !== null && u.name !== "ListItem" && u.name !== "Blockquote"; ) u = u.parent;
133
+ const h = u?.name === "ListItem";
134
+ let w = "";
135
+ for (let E = t + 1; E <= l; E += 1) {
136
+ const L = an(r.line(E).text, c, h);
137
+ if (L === null) return null;
138
+ w += E < r.lines ? `${L}
139
+ ` : L;
140
+ }
141
+ if (w !== n) return null;
142
+ if (l > t) {
143
+ const E = l < r.lines ? r.line(l + 1).from : r.length;
144
+ return { from: r.line(t + 1).from, to: E, prefix: c, whiteSpaceLinesEmpty: h, lineBreak: !1 };
145
+ }
146
+ if (t === r.lines) return { from: r.length, to: r.length, prefix: c, whiteSpaceLinesEmpty: h, lineBreak: !0 };
147
+ const p = r.line(t + 1).from;
148
+ return { from: p, to: p, prefix: c, whiteSpaceLinesEmpty: h, lineBreak: !1 };
149
+ }, dn = ({ parent: e, scroll: t, text: n, saved: r, labels: a, onChange: i, onGeometry: c, onSave: o }) => {
150
+ let f = !1;
151
+ const d = [
152
+ U.scrollHandler.of((l, u, h) => (fn(l, u.head, h, t), !0)),
153
+ Mt(),
154
+ Lt(),
155
+ dt(),
156
+ Ot(),
157
+ It(),
158
+ yt(),
159
+ U.lineWrapping,
160
+ wt({ base: Et }),
161
+ bt(Yt),
162
+ nn(a),
163
+ Ct.of([
164
+ {
165
+ key: "Mod-s",
166
+ preventDefault: !0,
167
+ run: () => (o(), !0)
168
+ },
169
+ ...Tt,
170
+ ...ft,
171
+ ...mt
172
+ ]),
173
+ U.contentAttributes.of({ "aria-label": a.editor, "aria-multiline": "true" }),
174
+ U.updateListener.of((l) => {
175
+ l.docChanged && i(l.transactions.every((u) => !u.docChanged || u.isUserEvent(be))), (l.docChanged || l.heightChanged || l.geometryChanged) && c(l.docChanged && !f);
176
+ }),
177
+ Jt
178
+ ], m = hn(r, d) ?? _e.create({ doc: n, extensions: d }), s = new U({ state: m, parent: e }), S = (l) => {
179
+ f = !0;
180
+ try {
181
+ l();
182
+ } finally {
183
+ f = !1;
184
+ }
185
+ };
186
+ return {
187
+ text: () => s.state.doc.toString(),
188
+ replaceText: (l) => {
189
+ const u = s.state.doc.toString();
190
+ if (u === l) return;
191
+ let h = 0;
192
+ const w = Math.min(u.length, l.length);
193
+ for (; h < w && u.charCodeAt(h) === l.charCodeAt(h); ) h += 1;
194
+ let p = 0;
195
+ for (; p < w - h && u.charCodeAt(u.length - 1 - p) === l.charCodeAt(l.length - 1 - p); ) p += 1;
196
+ s.dispatch({ changes: { from: h, to: u.length - p, insert: l.slice(h, l.length - p) }, userEvent: "input.replace" });
197
+ },
198
+ replaceBody: (l, u, h) => {
199
+ const w = un(s.state, l, u);
200
+ if (w === null) return !1;
201
+ const p = cn(h, w.prefix, w.whiteSpaceLinesEmpty), E = w.lineBreak ? `
202
+ ${p}` : p;
203
+ return S(() => s.dispatch({ changes: { from: w.from, to: w.to, insert: E }, userEvent: be, annotations: vt.of("full") })), !0;
204
+ },
205
+ undo: () => S(() => gt(s)),
206
+ redo: () => S(() => ht(s)),
207
+ reveal: ({ line: l, column: u }, h) => {
208
+ const { doc: w } = s.state, p = w.line(Math.min(Math.max(l, 1), w.lines)), E = Math.min(p.from + u, p.to);
209
+ s.dispatch({ selection: ce.cursor(E), effects: Se(s, E, h, t) });
210
+ },
211
+ showLine: (l) => {
212
+ const { doc: u } = s.state, h = u.line(Math.min(Math.max(l, 1), u.lines)).from;
213
+ s.dispatch({ effects: Se(s, h, null, t) });
214
+ },
215
+ topLine: () => {
216
+ const l = t.scrollerOf(s.scrollDOM), u = Z(l, s.contentDOM), h = ue(l) + l.clientHeight, { documentTop: w } = s, { doc: p } = s.state;
217
+ let E = s.lineBlockAtHeight(Math.max(u - w, 0));
218
+ for (; p.lineAt(E.from).text.trim() === "" && E.to < p.length; ) {
219
+ const _ = s.lineBlockAt(E.to + 1);
220
+ if (w + _.top >= h) break;
221
+ E = _;
222
+ }
223
+ const L = s.coordsAtPos(E.from), C = L === null ? w + E.top : L.top;
224
+ return { line: s.state.doc.lineAt(E.from).number, clientTop: C, offset: C - u };
225
+ },
226
+ lineSpans: (l) => {
227
+ const u = s.scrollDOM, h = u.scrollTop + s.documentTop - Z(u, s.contentDOM), { doc: w } = s.state, p = (E) => w.line(Math.min(Math.max(E, 1), w.lines)).from;
228
+ return l.map(({ first: E, last: L }) => ({ top: h + s.lineBlockAt(p(E)).top, bottom: h + s.lineBlockAt(p(L)).bottom }));
229
+ },
230
+ scrollElement: s.scrollDOM,
231
+ focus: () => s.focus(),
232
+ conflicts: () => J(s.state.doc),
233
+ resolveAtCursor: (l) => {
234
+ const u = J(s.state.doc), h = s.state.doc.lineAt(s.state.selection.main.head).number, w = u.find((p) => p.end >= h);
235
+ return w === void 0 ? !1 : (Be(s, w, l), !0);
236
+ },
237
+ nextConflict: () => {
238
+ const l = J(s.state.doc);
239
+ if (l[0] === void 0) return !1;
240
+ const h = s.state.doc.lineAt(s.state.selection.main.head).number, w = l.findIndex((C) => C.start > h), p = w < 0 ? 0 : w, E = s.state.doc.line(l[p].start).from;
241
+ s.dispatch({ selection: ce.cursor(E), effects: U.scrollIntoView(E, { y: "center" }) });
242
+ const L = s.contentDOM.querySelector(`[${De}="${String(p)}"] button`);
243
+ return L instanceof HTMLElement ? L.focus({ preventScroll: !0 }) : s.focus(), !0;
244
+ },
245
+ save: () => s.state.toJSON(Pe),
246
+ destroy: () => s.destroy()
247
+ };
248
+ }, Se = (e, t, n, r) => {
249
+ const a = r.scrollerOf(e.scrollDOM), i = (n === null ? Z(a, e.contentDOM) : n) - ue(a);
250
+ return U.scrollIntoView(t, { y: "start", yMargin: i });
251
+ }, fn = (e, t, n, r) => {
252
+ const a = mn(e, t), i = r.scrollerOf(e.scrollDOM), c = ue(i), o = Z(i, e.contentDOM), f = c + i.clientHeight;
253
+ let d = 0;
254
+ switch (n.y) {
255
+ case "start":
256
+ d = a.top - (c + n.yMargin);
257
+ break;
258
+ case "center":
259
+ d = (a.top + a.bottom) / 2 - (o + f) / 2;
260
+ break;
261
+ case "end":
262
+ d = a.bottom - (f - n.yMargin);
263
+ break;
264
+ default:
265
+ a.top < o + n.yMargin ? d = a.top - (o + n.yMargin) : a.bottom > f - n.yMargin && (d = Math.min(a.bottom - (f - n.yMargin), a.top - (o + n.yMargin)));
266
+ }
267
+ if (d !== 0 && (i.scrollTop += d), n.y === "start") return;
268
+ const m = pn(e, t);
269
+ m !== null && r.reveal(m);
270
+ }, mn = (e, t) => {
271
+ const { node: n, offset: r } = e.domAtPos(t);
272
+ if (n.nodeType === Node.TEXT_NODE) {
273
+ const c = n.ownerDocument?.createRange();
274
+ if (c !== void 0) {
275
+ c.setStart(n, r), c.setEnd(n, r);
276
+ const [o] = c.getClientRects();
277
+ if (o !== void 0 && o.height > 0) return { top: o.top, bottom: o.bottom };
278
+ }
279
+ }
280
+ const a = e.lineBlockAt(t), i = e.documentTop + a.top;
281
+ return { top: i, bottom: i + a.height };
282
+ }, pn = (e, t) => {
283
+ const { node: n } = e.domAtPos(t);
284
+ for (let r = n instanceof Element ? n : n.parentElement; r !== null; r = r.parentElement)
285
+ if (r.parentElement === e.contentDOM) return r;
286
+ return null;
287
+ }, hn = (e, t) => {
288
+ if (e == null) return null;
289
+ try {
290
+ return _e.fromJSON(e, { extensions: t }, Pe);
291
+ } catch {
292
+ return null;
293
+ }
294
+ }, gn = 610, vn = 987, xe = Object.freeze({ kind: "unavailable" }), wn = "true", En = "false", bn = (e) => e === wn ? !0 : e === En ? !1 : null, Sn = (e) => {
295
+ const t = new DOMParser().parseFromString(e, "text/html"), n = [];
296
+ for (const r of t.querySelectorAll("img[data-asset-src], a[data-doc-path]")) {
297
+ const a = r.localName === "img", i = r.getAttribute(a ? "data-asset-src" : "data-doc-path");
298
+ i !== null && n.push({ target: { kind: a ? "asset" : "doc", path: i }, exists: bn(r.getAttribute(a ? "data-asset-exists" : "data-doc-exists")) });
299
+ }
300
+ return n;
301
+ }, xn = ({ path: e, instant: t, preview: n, initial: r, onView: a }) => {
302
+ const i = new AbortController(), c = { asset: /* @__PURE__ */ new Map(), doc: /* @__PURE__ */ new Map() };
303
+ let o = r, f = null, d = !1, m = !1, s = null, S = null, l = [], u, h = !1, w, p = null;
304
+ const E = (x) => {
305
+ x.parsed === o.parsed && x.failure === o.failure && x.busy === o.busy && x.tier === o.tier || (o = x, a(x));
306
+ }, L = (x, y) => {
307
+ d || (d = !0, console.error(`[markdown-explorer] ${x}`, ...y === void 0 ? [] : [y]));
308
+ }, C = (x) => {
309
+ if (t !== null)
310
+ for (const { target: y, exists: A } of t.marksOf(x)) c[y.kind].set(y.path, A);
311
+ }, _ = () => {
312
+ clearTimeout(w), w = void 0, p?.abort(), p = null;
313
+ }, H = () => {
314
+ S = null, l = [], clearTimeout(u), u = void 0;
315
+ }, B = (x, y) => {
316
+ E({ parsed: o.parsed, failure: x, busy: !1, tier: y });
317
+ }, W = () => {
318
+ clearTimeout(u), u = l.length === 0 ? void 0 : setTimeout(T, vn);
319
+ }, b = () => {
320
+ if (t === null || S === null) return;
321
+ const x = S, y = [], A = { asset: /* @__PURE__ */ new Set(), doc: /* @__PURE__ */ new Set() };
322
+ let v;
323
+ try {
324
+ v = t.sanitize(x.html, (k) => {
325
+ const V = c[k.kind].get(k.path);
326
+ return V === void 0 && !A[k.kind].has(k.path) && (A[k.kind].add(k.path), y.push(k)), V ?? void 0;
327
+ });
328
+ } catch (k) {
329
+ H(), k instanceof Dt ? B(ee("too-large"), "instant") : (L("the draft sanitizer failed", k), B(ee("failed"), "instant"));
330
+ return;
331
+ }
332
+ l = y;
333
+ const M = o.parsed !== null && o.parsed.htmlContent === v && o.parsed.headings === x.headings ? o.parsed : { htmlContent: v, headings: x.headings };
334
+ E({ parsed: M, failure: null, busy: !1, tier: "instant" }), W();
335
+ }, T = () => {
336
+ if (u = void 0, h || S === null || l.length === 0) return;
337
+ h = !0;
338
+ const x = l;
339
+ n(e, S.text, i.signal).then((y) => {
340
+ if (h = !1, !(i.signal.aborted || !("kind" in y))) {
341
+ y.kind === "preview" && C(y.htmlContent);
342
+ for (const A of x) c[A.kind].has(A.path) || c[A.kind].set(A.path, null);
343
+ b();
344
+ }
345
+ });
346
+ }, O = (x) => {
347
+ _(), H(), E({ ...o, busy: !0, tier: "server" });
348
+ const y = new AbortController();
349
+ p = y, w = setTimeout(() => {
350
+ w = void 0, n(e, x, y.signal).then((A) => {
351
+ if (!(y.signal.aborted || !("kind" in A))) {
352
+ if (p = null, A.kind === "failure") {
353
+ B(A.failure, "server");
354
+ return;
355
+ }
356
+ C(A.htmlContent), E({ parsed: { htmlContent: A.htmlContent, headings: A.headings }, failure: null, busy: !1, tier: "server" });
357
+ }
358
+ });
359
+ }, gn);
360
+ }, g = async (x, y) => {
361
+ let A;
362
+ try {
363
+ A = await x.convert({ path: e, text: y }, i.signal);
364
+ } catch (M) {
365
+ return i.signal.aborted || L("the draft converter rejected a conversion, which its contract allows only once the signal aborted", M), xe;
366
+ }
367
+ const v = Nt(A);
368
+ return v === null ? A : (L(`the draft converter answered outside its contract: ${v}`), xe);
369
+ }, N = (x, y) => {
370
+ switch (y.kind) {
371
+ case "preview":
372
+ if (_(), y.headings.length > Pt) {
373
+ H(), B(ee("too-large"), "instant");
374
+ return;
375
+ }
376
+ S = { text: x, html: y.htmlContent, headings: y.headings.map(({ text: A, depth: v, id: M }) => ({ text: A, depth: v, id: M })) }, b();
377
+ return;
378
+ case "failure":
379
+ _(), H(), B(ee(y.failure.reason, y.failure.code), "instant");
380
+ return;
381
+ case "unavailable":
382
+ O(x);
383
+ return;
384
+ }
385
+ }, z = async (x) => {
386
+ m = !0;
387
+ try {
388
+ for (; s !== null && !i.signal.aborted; ) {
389
+ const y = s;
390
+ s = null;
391
+ const A = await g(x, y);
392
+ if (i.signal.aborted) return;
393
+ N(y, A);
394
+ }
395
+ } finally {
396
+ m = !1;
397
+ }
398
+ };
399
+ return t !== null && t.seed !== null && C(t.seed), {
400
+ update: (x) => {
401
+ if (!(i.signal.aborted || x === f)) {
402
+ if (f = x, clearTimeout(u), u = void 0, t === null) {
403
+ O(x);
404
+ return;
405
+ }
406
+ s = x, m || z(t.converter);
407
+ }
408
+ },
409
+ dispose: () => {
410
+ i.abort(), s = null, clearTimeout(u), u = void 0, _();
411
+ }
412
+ };
413
+ }, yn = (e, t) => Bt(Ht(window), e, t, Vt), Tn = (e, t) => e.flatMap((n, r) => {
414
+ const a = t[r];
415
+ return a === void 0 ? [] : [
416
+ { editor: a.top, preview: n.top },
417
+ { editor: a.bottom, preview: n.bottom }
418
+ ];
419
+ }), $ = (e, t) => {
420
+ const n = e[t];
421
+ if (n === void 0) throw new RangeError(`[markdown-explorer] a scroll map index must be in 0..${String(e.length - 1)}; got ${String(t)}`);
422
+ return n;
423
+ }, kn = (e) => {
424
+ const t = [];
425
+ for (const r of e) {
426
+ let a = 0, i = t.length;
427
+ for (; a < i; ) {
428
+ const c = a + i >> 1;
429
+ $(t, c).pair.preview <= r.preview ? a = c + 1 : i = c;
430
+ }
431
+ t[a] = { pair: r, before: a === 0 ? void 0 : $(t, a - 1) };
432
+ }
433
+ const n = [];
434
+ for (let r = t.at(-1); r !== void 0; r = r.before) n.push(r.pair);
435
+ return n.reverse();
436
+ }, An = (e, t) => {
437
+ const n = Math.max(t.editorMax, 0), r = Math.max(t.previewMax, 0), a = n - t.editorViewport, i = r - t.previewViewport, c = e.filter((d) => d.editor > 0 && d.editor < a && d.preview > 0 && d.preview < i).sort((d, m) => d.editor - m.editor || d.preview - m.preview);
438
+ let o = { editor: 0, preview: 0 };
439
+ const f = [o];
440
+ for (const d of kn(c))
441
+ d.editor > o.editor && d.preview > o.preview && (f.push(d), o = d);
442
+ return f.push({ editor: n, preview: r }), {
443
+ toPreview: (d) => ye(f, "editor", "preview", d),
444
+ toEditor: (d) => ye(f, "preview", "editor", d)
445
+ };
446
+ }, ye = (e, t, n, r) => {
447
+ const a = $(e, 0), i = $(e, e.length - 1);
448
+ if (Number.isNaN(r) || r <= a[t]) return a[n];
449
+ if (r >= i[t]) return i[n];
450
+ let c = 0, o = e.length - 1;
451
+ for (; o - c > 1; ) {
452
+ const m = c + o >> 1;
453
+ $(e, m)[t] <= r ? c = m : o = m;
454
+ }
455
+ const f = $(e, c), d = $(e, o);
456
+ return f[n] + (d[n] - f[n]) * (r - f[t]) / (d[t] - f[t]);
457
+ }, Mn = 610, Te = 89, ke = 0.5, Ln = ({ editor: e, preview: t, previewBlocks: n, editorSpans: r, requestFrame: a, cancelFrame: i, now: c }) => {
458
+ let o = "editor", f = 0, d = null, m = null, s = !1, S = null, l = null;
459
+ const u = { editor: 0, preview: 0 }, h = { editor: null, preview: null };
460
+ let w = null, p = () => {
461
+ };
462
+ const E = (g) => {
463
+ d !== null && g !== d || (o = g, f = c() + Mn);
464
+ }, L = (g, N) => {
465
+ g !== o && d === null && c() >= f && !C(g, N) && (o = g), b();
466
+ }, C = (g, N) => {
467
+ const z = h[g];
468
+ return c() < u[g] || z !== null && Math.abs(N.position() - z) < ke;
469
+ }, _ = e.onScroll(() => L("editor", e)), H = e.onIntent(() => E("editor")), B = () => {
470
+ const g = t();
471
+ return g !== w && (p(), w = g, p = g === null ? () => {
472
+ } : W(g)), g;
473
+ }, W = (g) => {
474
+ const N = g.onScroll(() => L("preview", g)), z = g.onIntent(() => E("preview"));
475
+ return () => {
476
+ N(), z();
477
+ };
478
+ }, b = () => {
479
+ !s && m === null && (m = a(T));
480
+ }, T = () => {
481
+ m = null;
482
+ const g = B();
483
+ g !== null && (S ??= n(), l ??= An(Tn(S, r(S)), { editorMax: e.max(), previewMax: g.max(), editorViewport: e.viewport(), previewViewport: g.viewport() }), o === "editor" ? O(g, "preview", l.toPreview(e.position())) : O(e, "editor", l.toEditor(g.position())));
484
+ }, O = (g, N, z) => {
485
+ if (Math.abs(g.position() - z) < ke) {
486
+ h[N] = g.position();
487
+ return;
488
+ }
489
+ u[N] = c() + Te, h[N] = g.scrollTo(z);
490
+ };
491
+ return b(), {
492
+ editorChanged: (g) => {
493
+ l = null, g && E("editor"), b();
494
+ },
495
+ previewChanged: () => {
496
+ S = null, l = null, u.preview = c() + Te, b();
497
+ },
498
+ showAlone: (g) => {
499
+ d = g, !(g === null || g === o) && (o = g, f = 0, b());
500
+ },
501
+ dispose: () => {
502
+ s = !0, m !== null && i(m), m = null, _(), H(), p();
503
+ }
504
+ };
505
+ }, Ae = Object.freeze(["wheel", "pointerdown", "keydown", "focusin"]), He = (e) => ({
506
+ position: () => e.scrollTop,
507
+ max: () => Math.max(e.scrollHeight - e.clientHeight, 0),
508
+ viewport: () => e.clientHeight,
509
+ scrollTo: (t) => (e.scrollTo({ top: t, behavior: "instant" }), e.scrollTop),
510
+ onScroll: (t) => (e.addEventListener("scroll", t, { passive: !0 }), () => e.removeEventListener("scroll", t)),
511
+ onIntent: (t) => {
512
+ for (const n of Ae) e.addEventListener(n, t, { capture: !0, passive: !0 });
513
+ return () => {
514
+ for (const n of Ae) e.removeEventListener(n, t, { capture: !0 });
515
+ };
516
+ }
517
+ }), Y = "[data-aqmd-scroll-container]", On = "[data-aqmd-content]", In = `[${Ne}]`, Cn = Object.freeze(["p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8"]), ze = (e, t) => {
518
+ const n = Ze(e.peek(t));
519
+ return n !== null && n.document.kind === "markdown" ? { htmlContent: n.document.htmlContent, headings: n.document.headings } : null;
520
+ }, qn = (e, t, n, r, a) => {
521
+ if (n === void 0) return null;
522
+ let i;
523
+ try {
524
+ i = yn(e, t);
525
+ } catch (c) {
526
+ return console.error("[markdown-explorer] the draft sanitizer cannot run in this browser, so the server renders the preview", c), null;
527
+ }
528
+ return { converter: n, sanitize: i, marksOf: Sn, seed: ze(r, a)?.htmlContent ?? null };
529
+ }, Rn = (e) => {
530
+ const t = [];
531
+ let n = null;
532
+ for (const r of e.querySelectorAll(In)) {
533
+ const a = _t(r.getAttribute(Ne));
534
+ if (a === null || r.getClientRects().length === 0) continue;
535
+ n ??= e.scrollTop - Z(e, r);
536
+ const { top: i, bottom: c } = r.getBoundingClientRect();
537
+ t.push({ first: a.first, last: a.last, top: n + i, bottom: n + c });
538
+ }
539
+ return t;
540
+ }, _n = ({ path: e, readDraft: t, viewer: n, spreadsheetEditor: r, concealed: a, onEditAt: i, onLayout: c, handleRef: o }) => {
541
+ const { config: f, data: d, documents: m, draftConverter: s, labels: S } = ie(), [l, u] = oe(() => ({ parsed: ze(m, e), failure: null, busy: !0, tier: s === void 0 ? "server" : "instant" })), h = P(null), w = P(null), p = P(l);
542
+ D(() => {
543
+ p.current = l;
544
+ });
545
+ const E = pe(() => t());
546
+ qe(() => {
547
+ const b = qn(f.iframeHosts, f.dataPath, s, m, e), T = xn({ path: e, instant: b, preview: d.preview, initial: p.current, onView: (O) => Ke(() => u(O)) });
548
+ return w.current = T, T.update(E()), () => {
549
+ T.dispose(), w.current = null;
550
+ };
551
+ }, [f, d, m, s, e]);
552
+ const L = pe(() => c());
553
+ D(() => {
554
+ l.parsed !== null && L();
555
+ }, [l.parsed]);
556
+ const C = l.parsed !== null;
557
+ D(() => {
558
+ const b = C ? h.current?.querySelector(Y) : null;
559
+ if (!(b instanceof HTMLElement)) return;
560
+ b.tabIndex = 0;
561
+ const T = new ResizeObserver(() => L());
562
+ T.observe(b);
563
+ const O = b.querySelector(On);
564
+ return O !== null && T.observe(O), () => T.disconnect();
565
+ }, [C]);
566
+ const _ = P(null);
567
+ $e(
568
+ o,
569
+ () => ({
570
+ draft: (b) => {
571
+ w.current?.update(b);
572
+ },
573
+ scrollPort: () => {
574
+ const b = h.current?.querySelector(Y);
575
+ return b instanceof HTMLElement ? (_.current?.scroller !== b && (_.current = { scroller: b, port: He(b) }), _.current.port) : null;
576
+ },
577
+ blocks: () => {
578
+ const b = h.current?.querySelector(Y);
579
+ return b instanceof HTMLElement ? Rn(b) : [];
580
+ },
581
+ focus: () => {
582
+ const b = h.current?.querySelector(Y);
583
+ b instanceof HTMLElement && b.focus({ preventScroll: !0 });
584
+ },
585
+ anchor: (b) => {
586
+ const T = h.current, O = T?.querySelector(Y);
587
+ return T === null || !(O instanceof HTMLElement) ? null : Qe(T, b(O));
588
+ },
589
+ place: ({ line: b, clientTop: T }, O) => {
590
+ const g = h.current, N = g === null ? null : Ye(g, b);
591
+ N !== null && (O(N).scrollTop += Je(N, b) - T);
592
+ }
593
+ }),
594
+ []
595
+ );
596
+ const H = (b) => {
597
+ const T = h.current, { target: O } = b;
598
+ if (i === void 0 || T === null || !(O instanceof Element) || !et(O)) return;
599
+ const g = tt(O, T, T.ownerDocument.getSelection());
600
+ g !== null && i(g);
601
+ }, B = re(() => ({ ...n, showToc: !1, showStyle: !1, showAlign: !1, renderScrollBar: void 0, spreadsheetEditor: r }), [n, r]), W = re(
602
+ () => l.parsed === null ? null : /* @__PURE__ */ I("div", { className: "aqmx-document-viewer", children: /* @__PURE__ */ I(Rt, { ...B, parseResult: l.parsed, content: null }) }),
603
+ [B, l.parsed]
604
+ );
605
+ return /* @__PURE__ */ Q(
606
+ "section",
607
+ {
608
+ ref: h,
609
+ className: "aqmx-edit-preview",
610
+ "aria-label": S.previewRegion,
611
+ "aria-busy": l.busy,
612
+ inert: a !== null,
613
+ hidden: a === "removed",
614
+ "data-aqmx-preview-tier": l.tier,
615
+ "data-testid": "markdown-explorer-edit-preview",
616
+ onDoubleClick: i === void 0 ? void 0 : H,
617
+ children: [
618
+ l.failure === null ? null : /* @__PURE__ */ I("p", { className: "aqmx-callout aqmx-document-notice", "data-aqmx-tone": "warning", "data-testid": "markdown-explorer-edit-preview-failure", children: S.previewFailed(Re(S, l.failure.reason)) }),
619
+ W ?? (l.failure === null ? /* @__PURE__ */ I("div", { className: "aqmx-skeleton", "aria-hidden": "true", "data-testid": "markdown-explorer-edit-preview-skeleton", children: Cn.map((b) => /* @__PURE__ */ I("span", {}, b)) }) : null),
620
+ l.busy ? /* @__PURE__ */ I("p", { className: "aqmx-sr-only", children: S.previewUpdating }) : null
621
+ ]
622
+ }
623
+ );
624
+ }, Me = "data-aqmx-edit-split", ae = "--aqmx-edit-share", Le = "--aqmx-edit-gutter", Nn = "data-aqmx-edit-split-probe", Pn = "aqmx-edit-split", Dn = "aqmx-edit-editor", Bn = "aqmx-edit-preview", Oe = { value: $t, unit: "percentage" }, Ie = { value: Gt, unit: "percentage" }, Ce = (e) => {
625
+ if (e.length !== 2) return null;
626
+ const [t = 0, n = 0] = e, r = t + n;
627
+ return r > 0 ? t / r * 100 : null;
628
+ }, ne = (e) => {
629
+ const t = e.current?.parentElement;
630
+ if (t == null) throw new Error("[markdown-explorer] the edit split is not mounted in its surface");
631
+ return t;
632
+ }, Vn = (e, t, n) => {
633
+ const [r, a] = oe(null);
634
+ return D(() => {
635
+ const i = e.current, c = t.current;
636
+ if (i === null || c === null) throw new Error("[markdown-explorer] the edit split is not mounted");
637
+ const o = { frame: i.getBoundingClientRect().width, measure: c.getBoundingClientRect().width }, f = () => {
638
+ o.frame > 0 && o.measure > 0 && a(Kt(o.frame, o.measure, n));
639
+ };
640
+ f();
641
+ const d = new ResizeObserver((m) => {
642
+ for (const s of m) {
643
+ const [S] = s.borderBoxSize;
644
+ S !== void 0 && (s.target === i && (o.frame = S.inlineSize), s.target === c && (o.measure = S.inlineSize));
645
+ }
646
+ f();
647
+ });
648
+ return d.observe(i), d.observe(c), () => d.disconnect();
649
+ }, [e, t, n]), r;
650
+ }, Hn = ({ editorId: e }) => {
651
+ const { edits: t, labels: n } = ie(), r = P(null), a = P(null), i = jt() ? Wt : Ut, c = Vn(r, a, i), [o] = oe(() => t.editorShare());
652
+ D(() => {
653
+ const m = ne(r);
654
+ return m.style.setProperty(Le, `${String(i)}px`), () => {
655
+ m.style.removeProperty(Le);
656
+ };
657
+ }, [i]), D(() => {
658
+ const m = ne(r);
659
+ return m.style.setProperty(ae, `${String(o)}%`), () => {
660
+ m.style.removeProperty(ae);
661
+ };
662
+ }, [o]), D(() => {
663
+ if (c === null) return;
664
+ const m = ne(r);
665
+ return m.setAttribute(Me, c), () => m.removeAttribute(Me);
666
+ }, [c]);
667
+ const f = Xt(
668
+ K(
669
+ (m) => {
670
+ const s = Ce(m);
671
+ s !== null && t.setEditorShare(s);
672
+ },
673
+ [t]
674
+ )
675
+ ), d = K(
676
+ (m, s) => {
677
+ const S = Ce(m);
678
+ S !== null && ne(r).style.setProperty(ae, `${String(S)}%`), f.layoutChanged(m, s);
679
+ },
680
+ [f]
681
+ );
682
+ return /* @__PURE__ */ Q("div", { ref: r, className: "aqmx-edit-split", "data-testid": "markdown-explorer-edit-split", children: [
683
+ /* @__PURE__ */ I("span", { ref: a, className: "aqmx-edit-split-probe", "aria-hidden": "true", [Nn]: "" }),
684
+ c === null ? null : /* @__PURE__ */ Q(zt, { id: Pn, direction: c === "beside" ? "horizontal" : "vertical", onLayout: d, className: "aqmx-edit-split-group", children: [
685
+ /* @__PURE__ */ I(we, { id: Dn, defaultSize: { value: o, unit: "percentage" }, minSize: Oe, maxSize: Ie, contentOverflow: "hidden" }),
686
+ /* @__PURE__ */ I(
687
+ Ft,
688
+ {
689
+ thickness: i,
690
+ indicator: { visible: !0, thickness: te.gripThickness, length: te.gripLength, barThickness: te.barThickness, barLength: te.barLength },
691
+ title: n.editViewSplitResize,
692
+ "aria-label": n.editViewSplitResize,
693
+ "aria-controls": e,
694
+ onDragging: f.draggingChanged,
695
+ className: "aqmx-split-handle aqmx-edit-split-handle"
696
+ }
697
+ ),
698
+ /* @__PURE__ */ I(we, { id: Bn, defaultSize: { value: 100 - o, unit: "percentage" }, minSize: Oe, maxSize: Ie, contentOverflow: "hidden" })
699
+ ] })
700
+ ] });
701
+ }, zn = 144, Fn = "[data-aqmd-spreadsheet-fullscreen]", Wn = Object.freeze(["e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8"]), Un = (e) => {
702
+ if (typeof e != "object" || e === null) return !1;
703
+ const { generation: t, topLine: n } = e;
704
+ return "state" in e && typeof t == "number" && typeof n == "number";
705
+ }, Gn = (e) => {
706
+ e.stopPropagation();
707
+ }, dr = (e) => {
708
+ const { path: t, name: n, session: r } = e, { edits: a, labels: i, announcer: c } = ie(), { phase: o } = r, f = K(() => a.retry(t), [a, t]);
709
+ switch (qe(() => {
710
+ o.kind === "failed" && c.announce(i.documentFailed(n, Re(i, o.failure.reason)));
711
+ }, [o, c, i, n]), o.kind) {
712
+ case "opening":
713
+ return /* @__PURE__ */ Q(Ge, { children: [
714
+ /* @__PURE__ */ I("div", { className: "aqmx-skeleton", "aria-hidden": "true", "data-testid": "markdown-explorer-edit-skeleton", children: Wn.map((d) => /* @__PURE__ */ I("span", {}, d)) }),
715
+ /* @__PURE__ */ I("p", { className: "aqmx-sr-only", children: i.editLoading })
716
+ ] });
717
+ case "grant":
718
+ return /* @__PURE__ */ I("div", { className: "aqmx-edit-message", children: /* @__PURE__ */ I(rt, { href: o.href, onRetry: f, testId: "markdown-explorer-edit-grant" }) });
719
+ case "failed":
720
+ return /* @__PURE__ */ I("div", { className: "aqmx-edit-message", children: /* @__PURE__ */ I(nt, { failure: o.failure, alert: !1, onRetry: f, testId: "markdown-explorer-edit-failure" }) });
721
+ case "editing":
722
+ return /* @__PURE__ */ I($n, { ...e });
723
+ }
724
+ }, $n = ({ path: e, name: t, session: n, layout: r, viewer: a, region: i, paneScroll: c }) => {
725
+ const { edits: o, labels: f } = ie(), d = P(null), m = je(), s = P(null), [S] = oe(() => /* @__PURE__ */ new Set()), l = P(null), u = P(null), h = r === "fill", w = ot(), p = it(n, w, h), E = p !== "editor" || n.previewPrepared, L = E && h, C = P({ layout: r, region: i, paneScroll: c });
726
+ D(() => {
727
+ C.current = { layout: r, region: i, paneScroll: c };
728
+ });
729
+ const _ = P(n.generation);
730
+ D(() => {
731
+ _.current = n.generation;
732
+ });
733
+ const H = P({ view: p, previewMounted: E });
734
+ D(() => {
735
+ H.current = { view: p, previewMounted: E };
736
+ });
737
+ const B = P(null), W = P(null), b = P(!1), T = re(() => ({ editor: f.editorLabel(t), useOurs: f.conflictUseOurs, useTheirs: f.conflictUseTheirs, keepBoth: f.conflictKeepBoth }), [f, t]), O = K((v) => {
738
+ const { region: M, paneScroll: k } = C.current, V = M();
739
+ return V === null ? v : k.scrollerOf(V, v);
740
+ }, []);
741
+ D(() => {
742
+ const v = d.current, M = o.draft(e);
743
+ if (v === null || M === null) return;
744
+ const k = o.editorState.get(e), V = Un(k) && k.generation === _.current ? k : null;
745
+ let G = null, j = null;
746
+ const le = () => {
747
+ G !== null && clearTimeout(G), G = null, j !== null && o.setDraft(e, j.text());
748
+ }, F = dn({
749
+ parent: v,
750
+ scroll: {
751
+ scrollerOf: (q) => {
752
+ const { layout: X, region: We, paneScroll: Ue } = C.current, me = We();
753
+ return X === "fill" || me === null ? q : Ue.scrollerOf(me, q);
754
+ },
755
+ reveal: (q) => C.current.paneScroll.reveal(q)
756
+ },
757
+ text: M,
758
+ saved: V === null ? void 0 : V.state,
759
+ labels: T,
760
+ onGeometry: (q) => u.current?.editorChanged(q),
761
+ onChange: (q) => {
762
+ if (G ??= setTimeout(le, zn), !q)
763
+ for (const X of [...S]) X();
764
+ H.current.previewMounted && j !== null && l.current?.draft(j.text());
765
+ },
766
+ onSave: () => {
767
+ o.save(e);
768
+ }
769
+ });
770
+ j = F, s.current = F;
771
+ const de = () => {
772
+ const q = F.topLine();
773
+ return { line: q.line, clientTop: q.clientTop };
774
+ }, fe = () => l.current?.anchor(O) ?? null, Fe = o.bindEditor(e, {
775
+ flush: le,
776
+ adopt: (q) => F.replaceText(q),
777
+ returnPoint: () => {
778
+ const q = H.current.view === "preview" && C.current.layout === "content" ? fe() : null;
779
+ return q === null ? de() : { line: q.lines.first, clientTop: q.clientTop };
780
+ },
781
+ nextConflict: () => H.current.view !== "preview" ? F.nextConflict() : F.conflicts().length === 0 ? !1 : (b.current = !0, o.setView(e, "editor"), !0),
782
+ beforeView: (q) => {
783
+ if (C.current.layout === "fill") return;
784
+ if (q === "preview") {
785
+ B.current = { to: "preview", point: de() };
786
+ return;
787
+ }
788
+ const X = fe();
789
+ B.current = X === null ? null : { to: "editor", anchor: X };
790
+ }
791
+ });
792
+ return V !== null && F.showLine(V.topLine), () => {
793
+ le(), o.editorState.set(e, { state: F.save(), generation: _.current, topLine: F.topLine().line }), Fe(), F.destroy(), j = null, s.current = null;
794
+ };
795
+ }, [o, e, T, O, S]);
796
+ const { request: g } = n;
797
+ D(() => {
798
+ const v = s.current, M = o.draft(e);
799
+ v === null || M === null || g === null || (g.anchor !== null && v.reveal(he(M, g.anchor), g.anchor.clientTop), v.focus(), o.consumeRequest(e));
800
+ }, [o, e, g, T]), D(() => {
801
+ p !== "editor" && !n.previewPrepared && o.preparePreview(e);
802
+ }, [o, e, p, n.previewPrepared]), D(() => {
803
+ const v = B.current, M = W.current, k = b.current;
804
+ B.current = null, W.current = null, b.current = !1;
805
+ const V = s.current;
806
+ if (v?.to === "preview") l.current?.place(v.point, O);
807
+ else if (v?.to === "editor" && V !== null) {
808
+ const G = o.draft(e);
809
+ G !== null && V.reveal(he(G, v.anchor), v.anchor.clientTop);
810
+ }
811
+ k ? V?.nextConflict() : M === "editor" ? V?.focus() : M === "preview" && l.current?.focus();
812
+ }, [o, e, p, O]);
813
+ const N = re(
814
+ () => ({
815
+ write: ({ fenceLine: v, expected: M, text: k }) => s.current?.replaceBody(v, M, k) === !0,
816
+ undo: () => s.current?.undo(),
817
+ redo: () => s.current?.redo(),
818
+ subscribe: (v) => (S.add(v), () => {
819
+ S.delete(v);
820
+ })
821
+ }),
822
+ [S]
823
+ ), z = K(() => {
824
+ const v = s.current;
825
+ if (v === null) throw new Error("[markdown-explorer] the preview read the draft before the editor was mounted");
826
+ return v.text();
827
+ }, []);
828
+ D(() => {
829
+ const v = s.current;
830
+ if (!L || v === null) return;
831
+ const M = Ln({
832
+ editor: He(v.scrollElement),
833
+ preview: () => l.current?.scrollPort() ?? null,
834
+ previewBlocks: () => l.current?.blocks() ?? [],
835
+ editorSpans: (k) => v.lineSpans(k),
836
+ requestFrame: (k) => requestAnimationFrame(k),
837
+ cancelFrame: (k) => cancelAnimationFrame(k),
838
+ now: () => performance.now()
839
+ });
840
+ return M.showAlone(ge(H.current.view)), u.current = M, () => {
841
+ M.dispose(), u.current = null;
842
+ };
843
+ }, [L, o, e, T]), D(() => {
844
+ u.current?.showAlone(ge(p));
845
+ }, [p]);
846
+ const x = K(() => u.current?.previewChanged(), []), y = K((v) => o.start(e, v), [o, e]);
847
+ return (
848
+ /* @__PURE__ */ Q("div", { className: "aqmx-edit", "data-aqmx-edit-layout": r, "data-aqmx-edit-view": p, "data-testid": "markdown-explorer-edit", onKeyDown: (v) => {
849
+ if (v.target instanceof Element && v.target.closest(Fn) !== null) return;
850
+ if (v.key === "Escape") {
851
+ ve(v);
852
+ return;
853
+ }
854
+ if (!lt(v.nativeEvent, at(navigator.platform))) return;
855
+ ve(v);
856
+ const M = ut(p);
857
+ W.current = M, ct(o, e, M, h);
858
+ }, onDoubleClick: Gn, children: [
859
+ /* @__PURE__ */ I("div", { ref: d, id: m, className: "aqmx-edit-editor", "data-testid": "markdown-explorer-editor", inert: p === "preview", hidden: p === "preview" && !h, ...st }),
860
+ p === "split" ? /* @__PURE__ */ I(Hn, { editorId: m }) : null,
861
+ E ? /* @__PURE__ */ I(
862
+ _n,
863
+ {
864
+ path: e,
865
+ readDraft: z,
866
+ viewer: a,
867
+ spreadsheetEditor: N,
868
+ concealed: p !== "editor" ? null : h ? "overlay" : "removed",
869
+ onEditAt: p === "preview" ? y : void 0,
870
+ onLayout: x,
871
+ handleRef: l
872
+ }
873
+ ) : null
874
+ ] })
875
+ );
876
+ };
877
+ export {
878
+ zn as DRAFT_WRITE_INTERVAL_MS,
879
+ dr as default
880
+ };