@andares/pdeditor 0.4.0 → 0.5.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.
@@ -0,0 +1,867 @@
1
+ var Me = Object.defineProperty;
2
+ var He = (r, i, s) => i in r ? Me(r, i, { enumerable: !0, configurable: !0, writable: !0, value: s }) : r[i] = s;
3
+ var A = (r, i, s) => He(r, typeof i != "symbol" ? i + "" : i, s);
4
+ import { hasLiteralColon as me, matchKeyValue as ve, findKeyValueSeparator as ye, splitInlineCode as Ie } from "@andares/pdfoundation";
5
+ import { format as ut, jsonToPdText as dt, pdToJsonText as ht } from "@andares/pdfoundation";
6
+ function Pe(r, i, { signal: s, edges: t } = {}) {
7
+ let a, e = null;
8
+ const l = t != null && t.includes("leading"), u = t == null || t.includes("trailing"), d = () => {
9
+ e !== null && (r.apply(a, e), a = void 0, e = null);
10
+ }, S = () => {
11
+ u && d(), $();
12
+ };
13
+ let v = null;
14
+ const _ = () => {
15
+ v != null && clearTimeout(v), v = setTimeout(() => {
16
+ v = null, S();
17
+ }, i);
18
+ }, L = () => {
19
+ v !== null && (clearTimeout(v), v = null);
20
+ }, $ = () => {
21
+ L(), a = void 0, e = null;
22
+ }, y = () => {
23
+ d();
24
+ }, x = function(...p) {
25
+ if (s != null && s.aborted) return;
26
+ a = this, e = p;
27
+ const g = v == null;
28
+ _(), l && g && d();
29
+ };
30
+ return x.schedule = _, x.cancel = $, x.flush = y, s == null || s.addEventListener("abort", $, { once: !0 }), x;
31
+ }
32
+ function Z(r, i) {
33
+ const s = r, t = s.key.toLowerCase(), a = i.split("+"), e = a.slice(0, -1), l = a[a.length - 1];
34
+ return !(t !== l || e.includes("shift") !== s.shiftKey || e.includes("ctrl") !== s.ctrlKey || e.includes("alt") !== s.altKey || e.includes("meta") !== s.metaKey);
35
+ }
36
+ function xe(r, i) {
37
+ var a;
38
+ const t = r.slice(0, i).split(`
39
+ `);
40
+ return { line: t.length - 1, col: ((a = t[t.length - 1]) == null ? void 0 : a.length) ?? 0 };
41
+ }
42
+ function P(r, i, s) {
43
+ var a;
44
+ let t = 0;
45
+ for (let e = 0; e < i; e++) t += (((a = r[e]) == null ? void 0 : a.length) ?? 0) + 1;
46
+ return t + s;
47
+ }
48
+ function ke() {
49
+ return (r, i) => {
50
+ var S;
51
+ if (i.type !== "keydown" || !Z(i, "enter")) return;
52
+ const { value: s, selectionStart: t, selectionEnd: a } = r, e = s.slice(0, t).split(`
53
+ `).pop() ?? "", l = ((S = /^\s*/.exec(e)) == null ? void 0 : S[0]) ?? "";
54
+ i.preventDefault();
55
+ const u = /^(\s*)-(?=[ ]|$)/.test(e), d = `
56
+ ${l}${u ? "- " : ""}`;
57
+ return {
58
+ value: s.slice(0, t) + d + s.slice(a),
59
+ selectionStart: t + d.length,
60
+ selectionEnd: t + d.length
61
+ };
62
+ };
63
+ }
64
+ function Ke(r) {
65
+ return /^(\s*)-(?=[ ]|$)/.test(r);
66
+ }
67
+ function _e(r, i) {
68
+ return r.startsWith(i) ? i.length : r.startsWith(" ") ? 1 : 0;
69
+ }
70
+ function Se(r) {
71
+ return (i, s) => {
72
+ if (s.type !== "keydown") return;
73
+ const { value: t, selectionStart: a, selectionEnd: e } = i, l = t.split(`
74
+ `), u = t.slice(0, a).split(`
75
+ `).length - 1, d = t.slice(0, e).split(`
76
+ `).length - 1, S = xe(t, a), v = xe(t, e);
77
+ if (Z(s, "shift+tab")) {
78
+ s.preventDefault();
79
+ const _ = l.map(
80
+ (p, g) => g >= u && g <= d ? p.slice(_e(p, r)) : p
81
+ ), L = _.join(`
82
+ `);
83
+ if (L === t) return;
84
+ const $ = (p) => {
85
+ const g = _e(l[p.line] ?? "", r);
86
+ return { line: p.line, col: Math.max(0, p.col - g) };
87
+ }, y = $(S), x = $(v);
88
+ return {
89
+ value: L,
90
+ selectionStart: P(_, y.line, y.col),
91
+ selectionEnd: P(_, x.line, x.col)
92
+ };
93
+ }
94
+ if (Z(s, "tab")) {
95
+ s.preventDefault();
96
+ const _ = l.map((y, x) => ({ l: y, i: x })).filter(({ i: y }) => y >= u && y <= d);
97
+ if (_.length > 0 && _.every(({ l: y }) => Ke(y)) || a !== e) {
98
+ const y = l.map(
99
+ (k, b) => b >= u && b <= d && k !== "" ? r + k : k
100
+ ), x = y.join(`
101
+ `), p = (k) => {
102
+ const b = l[k.line] ?? "", Q = k.line >= u && k.line <= d;
103
+ return {
104
+ line: k.line,
105
+ col: Q && b !== "" ? k.col + r.length : k.col
106
+ };
107
+ }, g = p(S), o = p(v);
108
+ return {
109
+ value: x,
110
+ selectionStart: P(y, g.line, g.col),
111
+ selectionEnd: P(y, o.line, o.col)
112
+ };
113
+ }
114
+ const $ = a + r.length;
115
+ return {
116
+ value: t.slice(0, a) + r + t.slice(e),
117
+ selectionStart: $,
118
+ selectionEnd: $
119
+ };
120
+ }
121
+ };
122
+ }
123
+ function Be(r) {
124
+ return r.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
125
+ }
126
+ function O(r) {
127
+ return r.replace(/\r\n?/g, `
128
+ `);
129
+ }
130
+ function je(r, i) {
131
+ const s = document.createRange();
132
+ s.selectNodeContents(r), r.replaceChildren(s.createContextualFragment(i));
133
+ }
134
+ function We(r, i) {
135
+ const s = Math.min(r.length, i.length);
136
+ let t = 0;
137
+ for (; t < s && r.charCodeAt(t) === i.charCodeAt(t); ) t++;
138
+ return t;
139
+ }
140
+ const qe = /* @__PURE__ */ new Set([
141
+ "padding",
142
+ "paddingTop",
143
+ "paddingRight",
144
+ "paddingBottom",
145
+ "paddingLeft"
146
+ ]);
147
+ function Le(r) {
148
+ var t;
149
+ const i = {
150
+ top: "0px",
151
+ right: "0px",
152
+ bottom: "0px",
153
+ left: "0px"
154
+ }, s = (t = r.padding) == null ? void 0 : t.trim();
155
+ if (s) {
156
+ const a = s.split(/\s+/);
157
+ if (a.length === 1) {
158
+ const e = a[0] ?? "0px";
159
+ i.top = i.right = i.bottom = i.left = e;
160
+ } else a.length === 2 ? (i.top = i.bottom = a[0] ?? "0px", i.right = i.left = a[1] ?? "0px") : a.length === 3 ? (i.top = a[0] ?? "0px", i.right = i.left = a[1] ?? "0px", i.bottom = a[2] ?? "0px") : (i.top = a[0] ?? "0px", i.right = a[1] ?? "0px", i.bottom = a[2] ?? "0px", i.left = a[3] ?? "0px");
161
+ }
162
+ return r.paddingTop && (i.top = r.paddingTop), r.paddingRight && (i.right = r.paddingRight), r.paddingBottom && (i.bottom = r.paddingBottom), r.paddingLeft && (i.left = r.paddingLeft), i;
163
+ }
164
+ function Ue(r) {
165
+ return r.startsWith("--") ? r : r.replace(/[A-Z]/g, (i) => `-${i.toLowerCase()}`);
166
+ }
167
+ const $e = "position:relative;display:block;box-sizing:border-box;width:100%;height:auto;max-height:inherit;overflow:hidden;text-align:start;margin:0;border:0;font-family:var(--pd-font-family,inherit);font-size:var(--pd-font-size,inherit);line-height:var(--pd-line-height,1.5);letter-spacing:var(--pd-letter-spacing,inherit);color:var(--pd-color,inherit);background:var(--pd-background,transparent);tab-size:4;font-variant-ligatures:none;font-kerning:normal;", Ce = "position:absolute;left:0;top:0;width:100%;height:100%;box-sizing:border-box;margin:0;border:0;outline:none;resize:none;background:transparent;-webkit-text-fill-color:transparent;caret-color:var(--pd-caret-color,currentColor);overflow:auto;scrollbar-gutter:stable;white-space:pre-wrap;overflow-wrap:break-word;word-break:normal;font:inherit;line-height:inherit;letter-spacing:inherit;tab-size:inherit;z-index:2;", Ee = "position:relative;left:auto;top:auto;width:100%;height:auto;box-sizing:border-box;margin:0;border:0;overflow:hidden;scrollbar-gutter:stable;pointer-events:none;user-select:none;white-space:pre-wrap;overflow-wrap:break-word;word-break:normal;font:inherit;line-height:inherit;letter-spacing:inherit;tab-size:inherit;color:inherit;z-index:1;", Ge = "position:absolute;left:0;top:0;width:100%;height:100%;box-sizing:border-box;margin:0;border:0;overflow:hidden;scrollbar-gutter:stable;pointer-events:none;user-select:none;white-space:pre-wrap;overflow-wrap:break-word;word-break:normal;font:inherit;line-height:inherit;letter-spacing:inherit;tab-size:inherit;color:inherit;z-index:0;", Ae = "position:absolute;left:0;top:0;width:100%;height:100%;box-sizing:border-box;margin:0;border:0;overflow:hidden;scrollbar-gutter:stable;pointer-events:none;user-select:none;white-space:pre-wrap;overflow-wrap:break-word;word-break:normal;font:inherit;line-height:inherit;letter-spacing:inherit;tab-size:inherit;color:inherit;opacity:var(--pd-placeholder-opacity,0.5);z-index:1;";
168
+ function K(r, i, s, t, a = {}) {
169
+ const e = document.createElement(r);
170
+ e.className = i, e.setAttribute("part", s), e.style.cssText = t;
171
+ for (const [l, u] of Object.entries(a)) e.setAttribute(l, u);
172
+ return e;
173
+ }
174
+ function Je(r, i, s) {
175
+ let t = { ...i }, a = t.language ?? "pd", e = O(t.value ?? ""), l = e, u = !1, d = !1, S = !1, v = t.lineNumbers ?? !1, _ = U(), L = "", $ = !1, y = 0, x = null;
176
+ const p = K("div", "pd-root", "root", $e), g = K("pre", "pd-highlight", "highlight", Ee, {
177
+ "aria-hidden": "true"
178
+ }), o = K("textarea", "pd-input", "input", Ce), k = K("div", "pd-placeholder", "placeholder", Ae, {
179
+ "aria-hidden": "true"
180
+ });
181
+ let b = null;
182
+ o.spellcheck = t.spellcheck ?? !1, o.setAttribute("autocapitalize", "off"), o.setAttribute("autocorrect", "off"), o.setAttribute("autocomplete", "off"), o.setAttribute("wrap", "soft"), o.value = e, o.readOnly = t.readOnly ?? !1, o.disabled = t.disabled ?? !1, ((n) => {
183
+ n !== void 0 && n >= 0 ? o.maxLength = n : o.removeAttribute("maxlength");
184
+ })(t.maxLength), t.dir && (o.dir = t.dir), H(), p.appendChild(g), p.appendChild(k), p.appendChild(o), r.appendChild(p), M(), G(), T(), W();
185
+ function B() {
186
+ u || (g.scrollTop = o.scrollTop, g.scrollLeft = o.scrollLeft, b && (b.scrollTop = o.scrollTop, b.scrollLeft = o.scrollLeft));
187
+ }
188
+ function Oe() {
189
+ if (typeof getComputedStyle != "function") return 0;
190
+ const n = getComputedStyle(r), c = Number.parseFloat(n.paddingTop) || 0, h = Number.parseFloat(n.paddingBottom) || 0;
191
+ return Math.max(0, r.clientHeight - c - h);
192
+ }
193
+ function j() {
194
+ $ ? (g.style.position = "absolute", g.style.left = "0", g.style.top = "0", g.style.height = "100%") : (g.style.position = "relative", g.style.left = "auto", g.style.top = "auto", g.style.height = "auto");
195
+ }
196
+ function W() {
197
+ if (u) return;
198
+ const n = Oe(), c = g.scrollHeight;
199
+ if (!$) {
200
+ n > 0 && c > n + 1 && ($ = !0, j(), p.style.height = `${n}px`, B());
201
+ return;
202
+ }
203
+ if (n <= 0 || c <= n + 1) {
204
+ $ = !1, j(), p.style.height = "", o.scrollTop = 0;
205
+ return;
206
+ }
207
+ const h = Number.parseFloat(p.style.height) || 0;
208
+ Math.abs(h - n) > 0.5 && (p.style.height = `${n}px`), B();
209
+ }
210
+ function ee() {
211
+ if (!y) {
212
+ if (typeof requestAnimationFrame != "function") {
213
+ W();
214
+ return;
215
+ }
216
+ y = requestAnimationFrame(() => {
217
+ y = 0, W();
218
+ });
219
+ }
220
+ }
221
+ typeof ResizeObserver < "u" && (x = new ResizeObserver(() => ee()), x.observe(r), x.observe(p));
222
+ const te = () => B(), Re = (n, c, h) => {
223
+ var f;
224
+ e = n, l = n, q({ value: n, selStart: c, selEnd: h }), (f = t.onValueChange) == null || f.call(t, n);
225
+ }, ne = (n) => {
226
+ if (u) return;
227
+ const c = O(o.value);
228
+ if (d || n != null && n.isComposing) {
229
+ e = c, T();
230
+ return;
231
+ }
232
+ c !== e && (e = c, T()), c !== l && Re(c, o.selectionStart, o.selectionEnd);
233
+ }, ie = () => {
234
+ d = !0;
235
+ }, re = () => {
236
+ d = !1;
237
+ const n = O(o.value);
238
+ n !== e && (e = n, T());
239
+ }, ze = Math.max(0, t.historyLimit ?? 5), oe = Math.max(0, t.historyDebounce ?? 1e3), E = [
240
+ { value: e, selStart: -1, selEnd: -1 }
241
+ ], N = [], le = () => {
242
+ E.length > ze + 1 && E.splice(1, 1);
243
+ }, se = (n) => {
244
+ const c = E[0];
245
+ if (c !== void 0 && c.selStart < 0 && c.value !== n.value) {
246
+ const f = We(c.value, n.value);
247
+ c.selStart = f, c.selEnd = f;
248
+ }
249
+ const h = E.at(-1);
250
+ h !== void 0 && h.value === n.value || (N.length = 0, E.push(n), le());
251
+ }, R = Pe(se, oe), q = (n) => {
252
+ oe === 0 ? se(n) : R(n);
253
+ }, ae = (n) => {
254
+ var f;
255
+ const c = n.selStart >= 0 ? n.selStart : Math.min(n.value.length, o.selectionStart), h = n.selEnd >= 0 ? n.selEnd : c;
256
+ e = n.value, l = n.value, o.value = n.value, o.setSelectionRange(
257
+ Math.max(0, Math.min(c, n.value.length)),
258
+ Math.max(0, Math.min(h, n.value.length))
259
+ ), T(), B(), (f = t.onValueChange) == null || f.call(t, n.value);
260
+ }, ce = () => {
261
+ if (R.flush(), E.length <= 1) return !1;
262
+ const n = E.pop();
263
+ n !== void 0 && N.push(n);
264
+ const c = E.at(-1);
265
+ return c !== void 0 && ae(c), !0;
266
+ }, ue = () => {
267
+ if (R.flush(), N.length === 0) return !1;
268
+ const n = N.pop();
269
+ return n === void 0 ? !1 : (E.push(n), le(), ae(n), !0);
270
+ }, de = (n) => {
271
+ var w;
272
+ if (u || n.isComposing || d || o.readOnly || o.disabled) return;
273
+ const c = n.ctrlKey || n.metaKey, h = n.key.toLowerCase();
274
+ if (c && (h === "z" || h === "y")) {
275
+ n.preventDefault(), h === "y" || h === "z" && n.shiftKey ? ue() : ce();
276
+ return;
277
+ }
278
+ if (h === "escape") {
279
+ (t.tabBehavior ?? "indent") === "indent" && (S = !0);
280
+ return;
281
+ }
282
+ if (h === "tab" && (t.tabBehavior ?? "indent") === "focus") return;
283
+ if (h === "tab" && S) {
284
+ S = !1;
285
+ return;
286
+ }
287
+ const f = {
288
+ value: e,
289
+ selectionStart: o.selectionStart,
290
+ selectionEnd: o.selectionEnd
291
+ };
292
+ let m = f;
293
+ for (const D of X) {
294
+ const V = D(m, n);
295
+ V && (m = { ...m, ...V });
296
+ }
297
+ (m.value !== f.value || m.selectionStart !== f.selectionStart || m.selectionEnd !== f.selectionEnd) && (n.preventDefault(), e = m.value, o.value = m.value, o.setSelectionRange(m.selectionStart, m.selectionEnd), l = m.value, T(), q({
298
+ value: m.value,
299
+ selStart: m.selectionStart,
300
+ selEnd: m.selectionEnd
301
+ }), (w = t.onValueChange) == null || w.call(t, m.value), B());
302
+ }, he = () => p.classList.add("pd-focused"), pe = () => p.classList.remove("pd-focused");
303
+ o.addEventListener("input", ne), o.addEventListener("compositionstart", ie), o.addEventListener("compositionend", re), o.addEventListener("scroll", te, { passive: !0 }), o.addEventListener("keydown", de), o.addEventListener("focus", he), o.addEventListener("blur", pe);
304
+ function U() {
305
+ return t.highlight ? (n) => {
306
+ var c;
307
+ return ((c = t.highlight) == null ? void 0 : c.call(t, n, a)) ?? "";
308
+ } : s(a);
309
+ }
310
+ function T() {
311
+ if (u) return;
312
+ const n = t.highlightThreshold ?? 1e5, c = e.length > n ? Be(e) : _(e);
313
+ je(g, `${c}<br/>`), v && fe(), G(), x || ee();
314
+ }
315
+ function fe() {
316
+ if (!v) return;
317
+ b || (b = document.createElement("pre"), b.className = "pd-lines", b.setAttribute("part", "gutter"), b.setAttribute("aria-hidden", "true"), p.insertBefore(b, k));
318
+ const n = e.split(`
319
+ `), h = `${String(n.length).length + 1}ch`, f = Le(t.styles ?? {}), m = `position:absolute;inset-inline-start:${f.left};width:${h};text-align:end;opacity:var(--pd-gutter-opacity,0.35);color:var(--pd-gutter-color,inherit)`, w = document.createDocumentFragment();
320
+ n.forEach((D, V) => {
321
+ const I = document.createElement("span");
322
+ I.className = "pd-ln", I.style.cssText = m, I.textContent = String(V + 1);
323
+ const Y = document.createElement("span");
324
+ Y.style.color = "transparent", Y.textContent = D, w.append(I, Y), V < n.length - 1 && w.append(document.createTextNode(`
325
+ `));
326
+ }), w.append(document.createElement("br")), b.replaceChildren(w), h !== L && (L = h, M(), b.style.cssText = be(f));
327
+ }
328
+ function G() {
329
+ const n = t.placeholder ?? "";
330
+ k.textContent = n, k.style.display = n && e.length === 0 ? "block" : "none", n ? o.setAttribute("aria-placeholder", n) : o.removeAttribute("aria-placeholder");
331
+ }
332
+ function ge(n) {
333
+ const c = L ? `calc(${n.left} + ${L})` : n.left;
334
+ return `padding:${n.top} ${n.right} ${n.bottom} ${c};`;
335
+ }
336
+ function J(n, c) {
337
+ return c + ge(n);
338
+ }
339
+ function be(n) {
340
+ return Ge + ge(n);
341
+ }
342
+ function M() {
343
+ const n = t.styles ?? {}, c = Le(n), h = Object.entries(n).filter(([f]) => !qe.has(f)).map(([f, m]) => `${Ue(f)}:${m};`).join("");
344
+ p.style.cssText = $e + h, g.style.cssText = J(c, Ee), o.style.cssText = J(c, Ce), k.style.cssText = J(c, Ae), b && (b.style.cssText = be(c)), j(), t.dir && (p.dir = t.dir, o.dir = t.dir, g.dir = t.dir, k.dir = t.dir, b && (b.dir = t.dir));
345
+ }
346
+ function H() {
347
+ t.ariaLabel ? o.setAttribute("aria-label", t.ariaLabel) : o.removeAttribute("aria-label"), t.ariaLabelledby ? o.setAttribute("aria-labelledby", t.ariaLabelledby) : o.removeAttribute("aria-labelledby"), t.ariaDescribedby ? o.setAttribute("aria-describedby", t.ariaDescribedby) : o.removeAttribute("aria-describedby"), t.readOnly ? o.setAttribute("aria-readonly", "true") : o.removeAttribute("aria-readonly"), t.disabled ? o.setAttribute("aria-disabled", "true") : o.removeAttribute("aria-disabled");
348
+ }
349
+ v && fe();
350
+ const X = [ke(), Se(t.indentUnit ?? " ")];
351
+ return {
352
+ setValue(n, c) {
353
+ if (u || d) return;
354
+ let h = O(n);
355
+ t.maxLength !== void 0 && t.maxLength >= 0 && (h = h.slice(0, t.maxLength)), R.cancel(), e = h, l = h, o.value = h, o.setSelectionRange(h.length, h.length), ((c == null ? void 0 : c.resetHistory) ?? !0) && (E.length = 0, E.push({ value: h, selStart: -1, selEnd: -1 }), N.length = 0), T(), B();
356
+ },
357
+ getValue() {
358
+ return e;
359
+ },
360
+ setLanguage(n) {
361
+ a = n, t = { ...t, language: n }, _ = U(), T();
362
+ },
363
+ setPlaceholder(n) {
364
+ t = { ...t, placeholder: n }, G();
365
+ },
366
+ setReadOnly(n) {
367
+ t = { ...t, readOnly: n }, o.readOnly = n, H();
368
+ },
369
+ setDisabled(n) {
370
+ t = { ...t, disabled: n }, o.disabled = n, H();
371
+ },
372
+ setLineNumbers(n) {
373
+ v !== n && (v = n, !n && b && (b.remove(), b = null, L = ""), M(), T());
374
+ },
375
+ update(n) {
376
+ u || (t = { ...t, ...n }, n.indentUnit !== void 0 && X.splice(0, X.length, ke(), Se(n.indentUnit)), n.language !== void 0 && (a = n.language), ("highlight" in n || n.language !== void 0) && (_ = U()), n.lineNumbers !== void 0 && (v = n.lineNumbers, !v && b && (b.remove(), b = null, L = "")), n.value !== void 0 && (e = O(n.value), l = e, o.value = e, E.length = 0, E.push({ value: e, selStart: -1, selEnd: -1 }), N.length = 0), n.readOnly !== void 0 && (o.readOnly = n.readOnly), n.disabled !== void 0 && (o.disabled = n.disabled), n.maxLength !== void 0 && (n.maxLength >= 0 ? o.maxLength = n.maxLength : o.removeAttribute("maxlength")), n.spellcheck !== void 0 && (o.spellcheck = n.spellcheck), n.dir !== void 0 && (o.dir = n.dir), H(), M(), T(), B());
377
+ },
378
+ focus() {
379
+ o.focus();
380
+ },
381
+ blur() {
382
+ o.blur();
383
+ },
384
+ undo() {
385
+ ce();
386
+ },
387
+ redo() {
388
+ ue();
389
+ },
390
+ insertText(n) {
391
+ var D;
392
+ if (u || o.readOnly || o.disabled) return;
393
+ const c = o.selectionStart, h = o.selectionEnd;
394
+ let f = O(n);
395
+ if (t.maxLength !== void 0 && t.maxLength >= 0) {
396
+ const V = t.maxLength - (e.length - (h - c));
397
+ if (V <= 0) return;
398
+ f.length > V && (f = f.slice(0, V));
399
+ }
400
+ const m = e.slice(0, c) + f + e.slice(h);
401
+ e = m, l = m, o.value = m;
402
+ const w = c + f.length;
403
+ o.setSelectionRange(w, w), T(), q({ value: m, selStart: w, selEnd: w }), (D = t.onValueChange) == null || D.call(t, m), B();
404
+ },
405
+ getSelection() {
406
+ return { start: o.selectionStart, end: o.selectionEnd };
407
+ },
408
+ setSelection(n, c = n) {
409
+ const h = (f) => Math.max(0, Math.min(e.length, Number.isFinite(f) ? f : 0));
410
+ o.setSelectionRange(h(n), h(c));
411
+ },
412
+ destroy() {
413
+ u || (u = !0, R.cancel(), x == null || x.disconnect(), y && typeof cancelAnimationFrame == "function" && cancelAnimationFrame(y), o.removeEventListener("input", ne), o.removeEventListener("compositionstart", ie), o.removeEventListener("compositionend", re), o.removeEventListener("scroll", te), o.removeEventListener("keydown", de), o.removeEventListener("focus", he), o.removeEventListener("blur", pe), p.remove());
414
+ },
415
+ textarea: o,
416
+ root: p,
417
+ pre: g
418
+ };
419
+ }
420
+ function C(r) {
421
+ return r.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
422
+ }
423
+ const F = {
424
+ key: "pd-key",
425
+ keyPunct: "pd-key-punct",
426
+ item: "pd-item",
427
+ section: "pd-section",
428
+ sep: "pd-sep",
429
+ ref: "pd-ref",
430
+ inlineCode: "pd-inline-code",
431
+ fence: "pd-fence"
432
+ };
433
+ function Xe(r) {
434
+ return `<span class="${F.inlineCode}">${C(r)}</span>`;
435
+ }
436
+ const we = /(?:^|\s):([^\s-][^\s]*)(?=\s|$)/g;
437
+ function Ye(r) {
438
+ let i = "", s = 0;
439
+ we.lastIndex = 0;
440
+ let t;
441
+ for (; (t = we.exec(r)) !== null; ) {
442
+ const a = /^\s/.test(t[0]) ? 1 : 0, e = t.index + a;
443
+ i += C(r.slice(s, e)), i += `<span class="${F.ref}">${C(r.slice(e, t.index + t[0].length))}</span>`, s = t.index + t[0].length;
444
+ }
445
+ return i += C(r.slice(s)), i;
446
+ }
447
+ function z(r) {
448
+ let i = "";
449
+ for (const { code: s, seg: t } of Ie(r))
450
+ i += s ? Xe(t) : Ye(t);
451
+ return i;
452
+ }
453
+ function Te(r, i, s, t) {
454
+ const a = `<span class="${F.key}">${z(s)}</span>`, e = `<span class="${F.keyPunct}">:</span>`;
455
+ return `${r}${i}${a}${e}${z(t)}`;
456
+ }
457
+ function Ze(r) {
458
+ const i = r.trim();
459
+ if (/^\/\/!pd(?:\s+[^\s]+)?$/.test(i)) {
460
+ const t = /^(\s*)(\/\/!pd)(\s*.*)$/.exec(r), a = (t == null ? void 0 : t[1]) ?? "", e = (t == null ? void 0 : t[2]) ?? "//!pd", l = (t == null ? void 0 : t[3]) ?? "";
461
+ return `${C(a)}<span class="${F.section}">${C(e)}</span>${z(l)}`;
462
+ }
463
+ if (i === "---")
464
+ return `<span class="${F.sep}">${C(r)}</span>`;
465
+ const s = /^(\s*)(-)(?=[ ]|$)/.exec(r);
466
+ if (s) {
467
+ const t = C(s[1] ?? ""), a = (s[1] ?? "").length + 1, e = r.slice(a), l = e.trimStart(), u = e.slice(0, e.length - l.length), d = `<span class="${F.item}">-</span>`;
468
+ if (!me(l) && ve(l)) {
469
+ const v = ye(l), _ = l.slice(0, v), L = l.slice(v + 1);
470
+ return `${t}${d}${C(u)}${Te("", "", _, L)}`;
471
+ }
472
+ return `${t}${d}${C(u)}${z(l)}`;
473
+ }
474
+ if (!me(i) && ve(i)) {
475
+ const a = r.length - r.trimStart().length, e = r.slice(a), l = ye(e), u = e.slice(0, l).trim(), d = e.slice(l + 1);
476
+ return Te(C(r.slice(0, a)), "", u, d);
477
+ }
478
+ return z(r);
479
+ }
480
+ function Qe(r) {
481
+ const i = r.split(`
482
+ `);
483
+ let s = !1;
484
+ const t = [];
485
+ for (const a of i) {
486
+ const e = a.trimStart();
487
+ if (s) {
488
+ e.startsWith("```") ? (s = !1, t.push(`<span class="${F.fence}">${C(a)}</span>`)) : t.push(C(a));
489
+ continue;
490
+ }
491
+ if (e.startsWith("```")) {
492
+ s = !0, t.push(`<span class="${F.fence}">${C(a)}</span>`);
493
+ continue;
494
+ }
495
+ t.push(Ze(a));
496
+ }
497
+ return t.join(`
498
+ `);
499
+ }
500
+ function et(r) {
501
+ return (i) => r === "pd" ? Qe(i) : Be(i);
502
+ }
503
+ function De(r) {
504
+ const i = r;
505
+ return `
506
+ ${i}.pd-key{color:var(--pd-token-key,color-mix(in oklab,currentColor 66%,transparent))}
507
+ ${i}.pd-key-punct{color:var(--pd-token-punct,color-mix(in oklab,currentColor 45%,transparent))}
508
+ ${i}.pd-item{color:var(--pd-token-item,color-mix(in oklab,currentColor 78%,transparent))}
509
+ ${i}.pd-section{color:var(--pd-token-section,color-mix(in oklab,currentColor 62%,transparent))}
510
+ ${i}.pd-sep{color:var(--pd-token-sep,color-mix(in oklab,currentColor 45%,transparent))}
511
+ ${i}.pd-ref{color:var(--pd-token-ref,color-mix(in oklab,currentColor 74%,transparent))}
512
+ ${i}.pd-inline-code{color:var(--pd-token-code,color-mix(in oklab,currentColor 84%,transparent))}
513
+ ${i}.pd-fence{color:var(--pd-token-fence,color-mix(in oklab,currentColor 55%,transparent))}
514
+ ${i}.token.comment,${i}.token.prolog,${i}.token.doctype,${i}.token.cdata{color:var(--pd-token-comment,color-mix(in oklab,currentColor 52%,transparent))}
515
+ ${i}.token.string,${i}.token.attr-value{color:var(--pd-token-string,color-mix(in oklab,currentColor 78%,transparent))}
516
+ ${i}.token.keyword,${i}.token.tag,${i}.token.operator{color:var(--pd-token-keyword,color-mix(in oklab,currentColor 64%,transparent))}
517
+ ${i}.token.number,${i}.token.boolean{color:var(--pd-token-number,color-mix(in oklab,currentColor 72%,transparent))}
518
+ ${i}.token.property,${i}.token.function{color:var(--pd-token-property,color-mix(in oklab,currentColor 68%,transparent))}
519
+ ${i}.token.punctuation{color:var(--pd-token-punctuation,color-mix(in oklab,currentColor 50%,transparent))}
520
+ `;
521
+ }
522
+ function Ne(r) {
523
+ const i = r;
524
+ return `
525
+ @supports not (color: color-mix(in oklab, red, blue)){
526
+ ${i}.pd-key{opacity:.72}${i}.pd-key-punct{opacity:.5}${i}.pd-item{opacity:.8}
527
+ ${i}.pd-section{opacity:.65}${i}.pd-sep{opacity:.5}${i}.pd-ref{opacity:.78}
528
+ ${i}.pd-inline-code{opacity:.88}${i}.pd-fence{opacity:.6}
529
+ }`;
530
+ }
531
+ const Ve = `
532
+ :host{display:block;min-width:0;max-width:100%;height:auto;max-height:inherit;font:inherit;color:inherit;background:transparent}
533
+ :host([disabled]){opacity:var(--pd-disabled-opacity,.6)}
534
+ :host(:focus-within){outline:var(--pd-focus-ring,2px solid color-mix(in oklab,currentColor 32%,transparent));outline-offset:var(--pd-focus-ring-offset,2px);border-radius:var(--pd-focus-radius,2px)}
535
+ ${De("")}
536
+ ${Ne("")}
537
+ `;
538
+ function tt() {
539
+ const r = "pd-editor ";
540
+ return `
541
+ pd-editor{display:block;min-width:0;max-width:100%;height:auto;max-height:inherit;font:inherit;color:inherit;background:transparent}
542
+ pd-editor[disabled]{opacity:var(--pd-disabled-opacity,.6)}
543
+ pd-editor:focus-within{outline:var(--pd-focus-ring,2px solid color-mix(in oklab,currentColor 32%,transparent));outline-offset:var(--pd-focus-ring-offset,2px);border-radius:var(--pd-focus-radius,2px)}
544
+ ${De(r)}
545
+ ${Ne(r)}
546
+ `;
547
+ }
548
+ const Fe = /* @__PURE__ */ new WeakSet();
549
+ function nt(r) {
550
+ if (Fe.has(r)) return;
551
+ Fe.add(r);
552
+ const i = r.createElement("style");
553
+ i.setAttribute("data-pd-editor", ""), i.textContent = tt(), (r.head ?? r.documentElement).appendChild(i);
554
+ }
555
+ function it(r) {
556
+ if (r.querySelector("style[data-pd-editor]")) return;
557
+ try {
558
+ if (typeof CSSStyleSheet < "u" && "replaceSync" in CSSStyleSheet.prototype) {
559
+ const s = new CSSStyleSheet();
560
+ s.replaceSync(Ve), r.adoptedStyleSheets = [...r.adoptedStyleSheets, s];
561
+ return;
562
+ }
563
+ } catch {
564
+ }
565
+ const i = document.createElement("style");
566
+ i.setAttribute("data-pd-editor", ""), i.textContent = Ve, r.appendChild(i);
567
+ }
568
+ const rt = [
569
+ "value",
570
+ "language",
571
+ "placeholder",
572
+ "indent-unit",
573
+ "line-numbers",
574
+ "tab-behavior",
575
+ "readonly",
576
+ "disabled",
577
+ "maxlength",
578
+ "name",
579
+ "spellcheck",
580
+ "inputmode",
581
+ "enterkeyhint",
582
+ "dir",
583
+ "aria-label",
584
+ "aria-labelledby",
585
+ "aria-describedby",
586
+ "shadow"
587
+ ];
588
+ function st(r = "pd-editor") {
589
+ if (typeof HTMLElement > "u" || typeof customElements > "u")
590
+ return null;
591
+ const i = customElements.get(r);
592
+ if (i) return i;
593
+ class s extends HTMLElement {
594
+ constructor() {
595
+ super();
596
+ A(this, "_editor", null);
597
+ A(this, "_internals", null);
598
+ A(this, "_container", null);
599
+ A(this, "_shadow", null);
600
+ A(this, "_value", "");
601
+ A(this, "_defaultValue", null);
602
+ A(this, "_highlight", null);
603
+ A(this, "_formDisabled", !1);
604
+ A(this, "_focusValue", "");
605
+ A(this, "_onFocus", () => {
606
+ this._focusValue = this._value, this._emit("pd-focus", { value: this._value });
607
+ });
608
+ A(this, "_onBlur", () => {
609
+ this._value !== this._focusValue && this._emit("pd-change", { value: this._value }), this._emit("pd-blur", { value: this._value });
610
+ });
611
+ try {
612
+ this._internals = typeof this.attachInternals == "function" ? this.attachInternals() : null;
613
+ } catch {
614
+ this._internals = null;
615
+ }
616
+ }
617
+ static get observedAttributes() {
618
+ return rt;
619
+ }
620
+ // ---- 属性 / 属性反射 ----
621
+ get value() {
622
+ return this._editor ? this._editor.getValue() : this._value;
623
+ }
624
+ set value(e) {
625
+ var u;
626
+ const l = e == null ? "" : String(e);
627
+ this._value = l, (u = this._editor) == null || u.setValue(l), this._syncFormValue();
628
+ }
629
+ get language() {
630
+ return this.getAttribute("language") ?? "pd";
631
+ }
632
+ set language(e) {
633
+ this.setAttribute("language", String(e));
634
+ }
635
+ get placeholder() {
636
+ return this.getAttribute("placeholder") ?? "";
637
+ }
638
+ set placeholder(e) {
639
+ this.setAttribute("placeholder", String(e));
640
+ }
641
+ get readOnly() {
642
+ return this.hasAttribute("readonly");
643
+ }
644
+ set readOnly(e) {
645
+ this.toggleAttribute("readonly", !!e);
646
+ }
647
+ get disabled() {
648
+ return this.hasAttribute("disabled");
649
+ }
650
+ set disabled(e) {
651
+ this.toggleAttribute("disabled", !!e);
652
+ }
653
+ get lineNumbers() {
654
+ return this.hasAttribute("line-numbers");
655
+ }
656
+ set lineNumbers(e) {
657
+ this.toggleAttribute("line-numbers", !!e);
658
+ }
659
+ get indentUnit() {
660
+ return this.getAttribute("indent-unit") ?? " ";
661
+ }
662
+ set indentUnit(e) {
663
+ this.setAttribute("indent-unit", String(e));
664
+ }
665
+ get tabBehavior() {
666
+ return this.getAttribute("tab-behavior") === "focus" ? "focus" : "indent";
667
+ }
668
+ set tabBehavior(e) {
669
+ this.setAttribute("tab-behavior", e === "focus" ? "focus" : "indent");
670
+ }
671
+ get maxLength() {
672
+ const e = this.getAttribute("maxlength");
673
+ if (e === null || e === "") return -1;
674
+ const l = Number.parseInt(e, 10);
675
+ return Number.isFinite(l) ? l : -1;
676
+ }
677
+ set maxLength(e) {
678
+ !Number.isFinite(e) || e < 0 ? this.removeAttribute("maxlength") : this.setAttribute("maxlength", String(Math.floor(e)));
679
+ }
680
+ get name() {
681
+ return this.getAttribute("name") ?? "";
682
+ }
683
+ set name(e) {
684
+ this.setAttribute("name", String(e));
685
+ }
686
+ get highlight() {
687
+ return this._highlight;
688
+ }
689
+ set highlight(e) {
690
+ this._highlight = typeof e == "function" ? e : null, this._syncOptions();
691
+ }
692
+ get editor() {
693
+ return this._editor;
694
+ }
695
+ get textarea() {
696
+ var e;
697
+ return ((e = this._editor) == null ? void 0 : e.textarea) ?? null;
698
+ }
699
+ // ---- 生命周期 ----
700
+ connectedCallback() {
701
+ this._upgradeProperties();
702
+ const e = this.getAttribute("value");
703
+ if (e !== null && this._value === "" && (this._value = e), this._defaultValue === null && (this._defaultValue = this._value), this._editor) return;
704
+ const l = this.getAttribute("shadow") === "false";
705
+ l ? nt(this.ownerDocument) : (this._shadow ?? (this._shadow = this.attachShadow({
706
+ mode: "open",
707
+ delegatesFocus: !0
708
+ })), it(this._shadow));
709
+ const u = l ? this : this._shadow, d = document.createElement("div");
710
+ d.className = "pd-container", d.setAttribute("part", "container"), d.style.cssText = "display:block;height:100%;max-height:inherit;min-width:0", u.appendChild(d), this._container = d;
711
+ const S = {
712
+ value: this._value,
713
+ language: this.language,
714
+ placeholder: this.placeholder,
715
+ readOnly: this.readOnly,
716
+ disabled: this.disabled || this._formDisabled,
717
+ lineNumbers: this.lineNumbers,
718
+ indentUnit: this.indentUnit,
719
+ tabBehavior: this.tabBehavior,
720
+ maxLength: this.maxLength,
721
+ spellcheck: this.getAttribute("spellcheck") === "true",
722
+ dir: this.getAttribute("dir") ?? "",
723
+ ariaLabel: this.getAttribute("aria-label") ?? void 0,
724
+ ariaLabelledby: this.getAttribute("aria-labelledby") ?? void 0,
725
+ ariaDescribedby: this.getAttribute("aria-describedby") ?? void 0,
726
+ highlight: this._highlight ?? void 0,
727
+ onValueChange: (v) => this._handleValueChange(v)
728
+ };
729
+ this._editor = Je(d, S, et), this._editor.textarea.addEventListener("focus", this._onFocus), this._editor.textarea.addEventListener("blur", this._onBlur), this._focusValue = this._value, this._applyInputHints(), this._syncFormValue(), queueMicrotask(() => {
730
+ this._editor && this._emit("pd-ready", { editor: this._editor });
731
+ });
732
+ }
733
+ disconnectedCallback() {
734
+ var e, l, u, d;
735
+ this._editor && (this._value = this._editor.getValue()), (e = this._editor) == null || e.textarea.removeEventListener("focus", this._onFocus), (l = this._editor) == null || l.textarea.removeEventListener("blur", this._onBlur), (u = this._editor) == null || u.destroy(), this._editor = null, (d = this._container) == null || d.remove(), this._container = null;
736
+ }
737
+ attributeChangedCallback(e, l, u) {
738
+ var d;
739
+ if (l !== u) {
740
+ if (e === "value") {
741
+ this._value = u ?? "", (d = this._editor) == null || d.setValue(this._value), this._syncFormValue();
742
+ return;
743
+ }
744
+ e !== "shadow" && this._editor && (this._syncOptions(), e === "language" && this._emit("pd-language-change", { value: this.language }));
745
+ }
746
+ }
747
+ // ---- 表单关联 ----
748
+ formResetCallback() {
749
+ this.value = this._defaultValue ?? "";
750
+ }
751
+ formDisabledCallback(e) {
752
+ var l;
753
+ this._formDisabled = e, (l = this._editor) == null || l.setDisabled(e || this.disabled);
754
+ }
755
+ formStateRestoreCallback(e) {
756
+ typeof e == "string" && (this.value = e);
757
+ }
758
+ // ---- 公共方法 ----
759
+ setValue(e) {
760
+ this.value = e;
761
+ }
762
+ getValue() {
763
+ return this.value;
764
+ }
765
+ setLanguage(e) {
766
+ this.language = e;
767
+ }
768
+ insertText(e) {
769
+ var l;
770
+ (l = this._editor) == null || l.insertText(e);
771
+ }
772
+ getSelection() {
773
+ var e;
774
+ return ((e = this._editor) == null ? void 0 : e.getSelection()) ?? null;
775
+ }
776
+ setSelection(e, l) {
777
+ var u;
778
+ (u = this._editor) == null || u.setSelection(e, l);
779
+ }
780
+ undo() {
781
+ var e;
782
+ (e = this._editor) == null || e.undo();
783
+ }
784
+ redo() {
785
+ var e;
786
+ (e = this._editor) == null || e.redo();
787
+ }
788
+ focus() {
789
+ var e;
790
+ (e = this._editor) == null || e.focus();
791
+ }
792
+ blur() {
793
+ var e;
794
+ (e = this._editor) == null || e.blur();
795
+ }
796
+ // ---- 内部 ----
797
+ _upgradeProperties() {
798
+ const e = [
799
+ "value",
800
+ "language",
801
+ "placeholder",
802
+ "readOnly",
803
+ "disabled",
804
+ "lineNumbers",
805
+ "indentUnit",
806
+ "tabBehavior",
807
+ "maxLength",
808
+ "name",
809
+ "highlight"
810
+ ];
811
+ for (const l of e)
812
+ if (Object.prototype.hasOwnProperty.call(this, l)) {
813
+ const u = this, d = u[l];
814
+ delete u[l], u[l] = d;
815
+ }
816
+ }
817
+ _syncOptions() {
818
+ var e;
819
+ (e = this._editor) == null || e.update({
820
+ language: this.language,
821
+ placeholder: this.placeholder,
822
+ readOnly: this.readOnly,
823
+ disabled: this.disabled || this._formDisabled,
824
+ lineNumbers: this.lineNumbers,
825
+ indentUnit: this.indentUnit,
826
+ tabBehavior: this.tabBehavior,
827
+ maxLength: this.maxLength,
828
+ spellcheck: this.getAttribute("spellcheck") === "true",
829
+ dir: this.getAttribute("dir") ?? "",
830
+ ariaLabel: this.getAttribute("aria-label") ?? void 0,
831
+ ariaLabelledby: this.getAttribute("aria-labelledby") ?? void 0,
832
+ ariaDescribedby: this.getAttribute("aria-describedby") ?? void 0,
833
+ highlight: this._highlight ?? void 0
834
+ }), this._applyInputHints();
835
+ }
836
+ /** 移动端输入提示:inputmode / enterkeyhint 原样转发到内部 textarea */
837
+ _applyInputHints() {
838
+ var l;
839
+ const e = (l = this._editor) == null ? void 0 : l.textarea;
840
+ if (e)
841
+ for (const u of ["inputmode", "enterkeyhint"]) {
842
+ const d = this.getAttribute(u);
843
+ d ? e.setAttribute(u, d) : e.removeAttribute(u);
844
+ }
845
+ }
846
+ _handleValueChange(e) {
847
+ this._value = e, this._syncFormValue(), this._emit("pd-input", { value: e });
848
+ }
849
+ _syncFormValue() {
850
+ var e;
851
+ typeof ((e = this._internals) == null ? void 0 : e.setFormValue) == "function" && this._internals.setFormValue(this._value);
852
+ }
853
+ _emit(e, l) {
854
+ this.dispatchEvent(
855
+ new CustomEvent(e, { detail: l, bubbles: !0, composed: !0 })
856
+ );
857
+ }
858
+ }
859
+ return A(s, "formAssociated", !0), customElements.define(r, s), s;
860
+ }
861
+ export {
862
+ st as definePdEditor,
863
+ ut as format,
864
+ Qe as highlightPd,
865
+ dt as jsonToPdText,
866
+ ht as pdToJsonText
867
+ };