@markput/react 0.5.0 → 0.6.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 (4) hide show
  1. package/index.css +2 -1
  2. package/index.d.ts +7 -8
  3. package/index.js +2479 -2183
  4. package/package.json +1 -1
package/index.js CHANGED
@@ -1,2194 +1,2490 @@
1
1
  import "./index.css";
2
- import { jsxs as J, jsx as h, Fragment as wt } from "react/jsx-runtime";
3
- import { useState as M, useEffect as P, useImperativeHandle as St, createContext as at, useContext as ct, memo as O, useRef as H, useCallback as k, useMemo as S, useLayoutEffect as Ct } from "react";
4
- function bt(s) {
5
- return s ? Object.fromEntries(
6
- Object.entries(s).map(([t, e]) => t.startsWith("data") && t.length > 4 && t[4] === t[4].toUpperCase() ? [`data-${t.slice(4).replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase()}`, e] : [t, e])
7
- ) : {};
8
- }
9
- function Et(...s) {
10
- return s.filter(Boolean).join(" ") || void 0;
11
- }
12
- function Tt(...s) {
13
- const t = Object.assign({}, ...s.filter(Boolean));
14
- return Object.keys(t).length > 0 ? t : void 0;
15
- }
16
- function Nt(s, t) {
17
- return s !== void 0 ? typeof s == "function" ? s(t) : s : t ?? {};
18
- }
19
- const It = {
20
- container: "div",
21
- span: "span"
2
+ import { Children as e, createContext as t, memo as n, useCallback as r, useContext as i, useEffect as a, useImperativeHandle as o, useLayoutEffect as s, useMemo as c, useRef as l, useState as u } from "react";
3
+ import { Fragment as d, jsx as f, jsxs as p } from "react/jsx-runtime";
4
+ //#region ../../common/core/src/shared/utils/dataAttributes.ts
5
+ function m(e) {
6
+ return e ? Object.fromEntries(Object.entries(e).map(([e, t]) => e.startsWith("data") && e.length > 4 && e[4] === e[4].toUpperCase() ? [`data-${e.slice(4).replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase()}`, t] : [e, t])) : {};
7
+ }
8
+ //#endregion
9
+ //#region ../../common/core/src/shared/utils/cx.ts
10
+ function h(...e) {
11
+ return e.filter(Boolean).join(" ") || void 0;
12
+ }
13
+ //#endregion
14
+ //#region ../../common/core/src/shared/utils/merge.ts
15
+ function g(...e) {
16
+ let t = Object.assign({}, ...e.filter(Boolean));
17
+ return Object.keys(t).length > 0 ? t : void 0;
18
+ }
19
+ //#endregion
20
+ //#region ../../common/core/src/shared/utils/resolveOptionSlot.ts
21
+ function _(e, t) {
22
+ return e === void 0 ? t : typeof e == "function" ? e(t) : e;
23
+ }
24
+ //#endregion
25
+ //#region ../../common/core/src/shared/utils/resolveSlot.ts
26
+ var v = {
27
+ container: "div",
28
+ span: "span"
22
29
  };
23
- function Q(s, t) {
24
- return t?.[s] ?? It[s];
25
- }
26
- function K(s, t) {
27
- const e = t?.[s];
28
- return e ? bt(e) : void 0;
29
- }
30
- var v = /* @__PURE__ */ ((s) => (s.UP = "ArrowUp", s.DOWN = "ArrowDown", s.LEFT = "ArrowLeft", s.RIGHT = "ArrowRight", s.END = "End", s.HOME = "Home", s.PAGE_DOWN = "PageDown", s.PAGE_UP = "PageUp", s.ENTER = "Enter", s.TAB = "Tab", s.SPACE = " ", s.BACKSPACE = "Backspace", s.DELETE = "Delete", s.COMMA = ",", s.ESC = "Escape", s))(v || {});
31
- const Mt = "@", Pt = "@[__value__](__meta__)", Lt = [
32
- {
33
- markup: Pt,
34
- overlay: {
35
- trigger: Mt,
36
- data: []
37
- }
38
- }
39
- ], x = {
40
- Value: "__value__",
41
- Meta: "__meta__",
42
- Nested: "__nested__"
43
- }, T = {
44
- Value: "value",
45
- Meta: "meta",
46
- Nested: "nested"
30
+ function y(e, t) {
31
+ return t?.[e] ?? v[e];
32
+ }
33
+ function b(e, t) {
34
+ let n = t?.[e];
35
+ return n ? m(n) : void 0;
36
+ }
37
+ //#endregion
38
+ //#region ../../common/core/src/shared/constants.ts
39
+ var x = /* @__PURE__ */ function(e) {
40
+ return e.UP = "ArrowUp", e.DOWN = "ArrowDown", e.LEFT = "ArrowLeft", e.RIGHT = "ArrowRight", e.END = "End", e.HOME = "Home", e.PAGE_DOWN = "PageDown", e.PAGE_UP = "PageUp", e.ENTER = "Enter", e.TAB = "Tab", e.SPACE = " ", e.BACKSPACE = "Backspace", e.DELETE = "Delete", e.COMMA = ",", e.ESC = "Escape", e;
41
+ }({}), S = [{
42
+ markup: "@[__value__](__meta__)",
43
+ overlay: {
44
+ trigger: "@",
45
+ data: []
46
+ }
47
+ }], C = {
48
+ Value: "__value__",
49
+ Meta: "__meta__",
50
+ Nested: "__nested__"
51
+ }, w = {
52
+ Value: "value",
53
+ Meta: "meta",
54
+ Nested: "nested"
47
55
  };
48
- function At(s, t) {
49
- const { segments: e, gapTypes: n, counts: r, valueGapIndices: o } = Ot(s);
50
- Dt(r, s);
51
- const i = r.value === 2, { segments: a, gapTypes: c } = i ? Rt(e, n, o) : { segments: e, gapTypes: n };
52
- return {
53
- markup: s,
54
- index: t,
55
- segments: a,
56
- gapTypes: c,
57
- hasNested: r.nested === 1,
58
- hasTwoValues: i,
59
- segmentGlobalIndices: Array.from({ length: a.length })
60
- // Will be populated by MarkupRegistry
61
- };
62
- }
63
- function Ot(s) {
64
- const t = [], e = [], n = [], r = {
65
- value: 0,
66
- meta: 0,
67
- nested: 0
68
- }, o = [], i = [T.Value, T.Meta, T.Nested];
69
- for (const l of i) {
70
- const d = st[l];
71
- let f = s.indexOf(d);
72
- for (; f !== -1; )
73
- o.push({ type: l, position: f }), f = s.indexOf(d, f + d.length);
74
- }
75
- o.sort((l, d) => l.position - d.position);
76
- let a = 0;
77
- for (const l of o) {
78
- const d = s.substring(a, l.position);
79
- d.length > 0 && t.push(d), e.push(l.type), r[l.type]++, l.type === T.Value && n.push(e.length - 1), a = l.position + st[l.type].length;
80
- }
81
- const c = s.substring(a);
82
- return c.length > 0 && t.push(c), {
83
- segments: t,
84
- gapTypes: e,
85
- counts: r,
86
- valueGapIndices: n
87
- };
88
- }
89
- function Dt(s, t) {
90
- const e = [
91
- { count: s.value, max: 2, name: x.Value },
92
- { count: s.meta, max: 1, name: x.Meta },
93
- { count: s.nested, max: 1, name: x.Nested }
94
- ];
95
- for (const { count: n, max: r, name: o } of e)
96
- if (n > r)
97
- throw new Error(`Invalid markup: "${t}". Max ${r} "${o}" placeholders, got ${n}`);
98
- if (s.value === 0 && s.nested === 0)
99
- throw new Error(
100
- `Invalid markup: "${t}". Need at least one "${x.Value}" or "${x.Nested}"`
101
- );
102
- }
103
- const st = {
104
- [T.Value]: x.Value,
105
- [T.Meta]: x.Meta,
106
- [T.Nested]: x.Nested
56
+ //#endregion
57
+ //#region ../../common/core/src/features/parsing/ParserV2/core/MarkupDescriptor.ts
58
+ function T(e, t) {
59
+ let { segments: n, gapTypes: r, counts: i, valueGapIndices: a } = E(e);
60
+ D(i, e);
61
+ let o = i.value === 2, { segments: s, gapTypes: c } = o ? k(n, r, a) : {
62
+ segments: n,
63
+ gapTypes: r
64
+ };
65
+ return {
66
+ markup: e,
67
+ index: t,
68
+ segments: s,
69
+ gapTypes: c,
70
+ hasNested: i.nested === 1,
71
+ hasTwoValues: o,
72
+ segmentGlobalIndices: Array.from({ length: s.length })
73
+ };
74
+ }
75
+ function E(e) {
76
+ let t = [], n = [], r = [], i = {
77
+ value: 0,
78
+ meta: 0,
79
+ nested: 0
80
+ }, a = [], o = [
81
+ w.Value,
82
+ w.Meta,
83
+ w.Nested
84
+ ];
85
+ for (let t of o) {
86
+ let n = O[t], r = e.indexOf(n);
87
+ for (; r !== -1;) a.push({
88
+ type: t,
89
+ position: r
90
+ }), r = e.indexOf(n, r + n.length);
91
+ }
92
+ a.sort((e, t) => e.position - t.position);
93
+ let s = 0;
94
+ for (let o of a) {
95
+ let a = e.substring(s, o.position);
96
+ a.length > 0 && t.push(a), n.push(o.type), i[o.type]++, o.type === w.Value && r.push(n.length - 1), s = o.position + O[o.type].length;
97
+ }
98
+ let c = e.substring(s);
99
+ return c.length > 0 && t.push(c), {
100
+ segments: t,
101
+ gapTypes: n,
102
+ counts: i,
103
+ valueGapIndices: r
104
+ };
105
+ }
106
+ function D(e, t) {
107
+ let n = [
108
+ {
109
+ count: e.value,
110
+ max: 2,
111
+ name: C.Value
112
+ },
113
+ {
114
+ count: e.meta,
115
+ max: 1,
116
+ name: C.Meta
117
+ },
118
+ {
119
+ count: e.nested,
120
+ max: 1,
121
+ name: C.Nested
122
+ }
123
+ ];
124
+ for (let { count: e, max: r, name: i } of n) if (e > r) throw Error(`Invalid markup: "${t}". Max ${r} "${i}" placeholders, got ${e}`);
125
+ if (e.value === 0 && e.nested === 0) throw Error(`Invalid markup: "${t}". Need at least one "${C.Value}" or "${C.Nested}"`);
126
+ }
127
+ var O = {
128
+ [w.Value]: C.Value,
129
+ [w.Meta]: C.Meta,
130
+ [w.Nested]: C.Nested
107
131
  };
108
- function Rt(s, t, e) {
109
- if (e.length !== 2)
110
- return { segments: s, gapTypes: t };
111
- const [n, r] = e, o = [], i = s[n], a = s[n + 1];
112
- i && a && o.push(f(i, a, s[n + 2]));
113
- for (let u = n + 2; u < r; u++)
114
- o.push(s[u]);
115
- const c = s[r], l = s[r + 1];
116
- c && l && o.push(f(c, l, s[r + 2]));
117
- const d = t.filter((u) => u !== T.Value);
118
- return { segments: o, gapTypes: d };
119
- function f(u, g, m) {
120
- if (!m) return [u, g, ""];
121
- const C = m.charAt(0), y = C && !g.includes(C) && !m.startsWith(u) ? C : "";
122
- return [u, g, y];
123
- }
124
- }
125
- class _t {
126
- markups;
127
- descriptors;
128
- /** Deduplicated list of unique segment definitions (static strings or dynamic patterns) */
129
- segments = [];
130
- /** Map from first segment index to descriptors that start with this segment (for O(1) lookup) */
131
- firstSegmentIndexMap = /* @__PURE__ */ new Map();
132
- constructor(t) {
133
- this.markups = t;
134
- const e = /* @__PURE__ */ new Map();
135
- this.descriptors = t.map((n, r) => {
136
- if (n === void 0)
137
- return null;
138
- const o = At(n, r);
139
- return o.segments.forEach((i, a) => {
140
- this.processSegment(o, i, a, e);
141
- }), this.addToFirstSegmentIndexMap(o), o;
142
- }).filter((n) => n !== null);
143
- }
144
- /**
145
- * Adds a descriptor to the firstSegmentIndexMap using its first segment's global index
146
- */
147
- addToFirstSegmentIndexMap(t) {
148
- const e = t.segmentGlobalIndices[0];
149
- if (e === void 0) return;
150
- const n = this.firstSegmentIndexMap.get(e);
151
- n ? n.push(t) : this.firstSegmentIndexMap.set(e, [t]);
152
- }
153
- processSegment(t, e, n, r) {
154
- const o = this.getSegmentKey(e);
155
- if (!o) return;
156
- this.registerSegment(e, o, r);
157
- const i = r.get(o);
158
- if (t.segmentGlobalIndices[n] = i, typeof e != "string") {
159
- const [a, c] = e;
160
- a && this.registerSegment(a, a, r), c && this.registerSegment(c, c, r);
161
- }
162
- }
163
- registerSegment(t, e, n) {
164
- if (!n.has(e)) {
165
- const r = this.segments.length;
166
- this.segments.push(t), n.set(e, r);
167
- }
168
- }
169
- /**
170
- * Gets a unique key for a segment definition
171
- * For static segments (strings), returns the string itself if non-empty
172
- * For dynamic segments (arrays), returns before|after|exclusions if before or after is non-empty
173
- * Returns empty string for segments that should be ignored
174
- */
175
- getSegmentKey(t) {
176
- if (typeof t == "string")
177
- return t;
178
- const [e, n, r] = t;
179
- return e || n ? `${e}|${n}|${r}` : "";
180
- }
181
- }
182
- function lt(s, t) {
183
- if (!t)
184
- return s;
185
- let e = 5381;
186
- for (let n = 0; n < t.length; n++)
187
- e = e * 33 ^ t.charCodeAt(n);
188
- return e = e >>> 0, s * 1e6 + (e & 1048575);
189
- }
190
- class Bt {
191
- constructor(t, e) {
192
- if (this.descriptor = t, this.expectedSegmentIndex = 1, this.start = e.start, this.end = e.end, t.segments.length === 1 && (this.expectedSegmentIndex = NaN, this.gaps.value = { start: this.start, end: this.end }), t.hasTwoValues && e.captured) {
193
- this.captured = e.captured;
194
- const n = e.start + e.value.indexOf(e.captured), r = n + e.captured.length;
195
- this.gaps.value = { start: n, end: r };
196
- }
197
- }
198
- gaps = {};
199
- /** Captured value from first dynamic segment (for hasTwoValues patterns) */
200
- captured;
201
- /**
202
- * Index of expected next segment:
203
- * - >= 0: active, waiting for segment at this index
204
- * - NaN: completed successfully
205
- * - -1: invalid, should be discarded
206
- */
207
- expectedSegmentIndex;
208
- start;
209
- end;
210
- /**
211
- * Check if the match is invalid and should be discarded
212
- */
213
- get isInvalid() {
214
- return this.expectedSegmentIndex === -1;
215
- }
216
- /**
217
- * Check if the pattern is completed (computed property)
218
- */
219
- get isCompleted() {
220
- return isNaN(this.expectedSegmentIndex);
221
- }
222
- /**
223
- * Check if the match is waiting for the last segment (high priority)
224
- */
225
- get isAwaitingLastSegment() {
226
- return this.expectedSegmentIndex === this.descriptor.segments.length - 1;
227
- }
228
- /**
229
- * Get the next expected segment index
230
- */
231
- get nextSegment() {
232
- if (this.isCompleted || this.isInvalid)
233
- return;
234
- const t = this.descriptor.segmentGlobalIndices[this.expectedSegmentIndex], e = this.descriptor.segments[this.expectedSegmentIndex];
235
- if (typeof e == "object" && this.descriptor.hasTwoValues && this.captured && this.isAwaitingLastSegment) {
236
- const [n, r] = e, o = n + this.captured + r;
237
- return lt(t, o);
238
- }
239
- return t;
240
- }
241
- /**
242
- * Update state with new segment by setting gap positions
243
- */
244
- processNext(t) {
245
- const e = this.end, n = t.start, r = this.descriptor.gapTypes[this.expectedSegmentIndex - 1];
246
- if (n < e) {
247
- this.expectedSegmentIndex = -1;
248
- return;
249
- }
250
- this.gaps[r] = { start: e, end: n }, this.end = t.end, this.expectedSegmentIndex++, this.expectedSegmentIndex >= this.descriptor.segments.length && (this.expectedSegmentIndex = NaN);
251
- }
252
- /**
253
- * Checks if this match conflicts with another match (overlaps and cannot nest properly)
254
- */
255
- conflictsWith(t) {
256
- return !t || this.start >= t.end ? !1 : !t.descriptor.hasNested || t.gaps.nested === void 0 ? !0 : !(this.start >= t.gaps.nested.start && this.end <= t.gaps.nested.end);
257
- }
258
- }
259
- class Vt {
260
- constructor(t) {
261
- this.registry = t;
262
- }
263
- pendingStates = /* @__PURE__ */ new Map();
264
- completingStates = /* @__PURE__ */ new Map();
265
- completedStates = [];
266
- /** Main method that converts found segments into structured matches */
267
- process(t) {
268
- this.pendingStates.clear(), this.completingStates.clear(), this.completedStates.length = 0;
269
- for (const e of t)
270
- this.processWaitingStates(e), this.tryStartNewStates(e);
271
- return this.completedStates.map((e) => e.match);
272
- }
273
- /**
274
- * Process states waiting for this segment
275
- * Try states by priority until one is valid, keeping rejected states for later attempts
276
- * Process completing states first (higher priority), then pending states
277
- * Process only one state per call
278
- */
279
- processWaitingStates(t) {
280
- const e = this.dequeueWaitingMatch(t);
281
- if (e && (e.processNext(t), !e.isInvalid)) {
282
- if (e.isCompleted) return this.addToCompleted(e);
283
- this.addToWaiting(e);
284
- }
285
- }
286
- tryStartNewStates(t) {
287
- this.registry.firstSegmentIndexMap.get(t.index)?.forEach((e) => {
288
- const n = new Bt(e, t);
289
- if (!n.isInvalid) {
290
- if (n.isCompleted) return this.addToCompleted(n);
291
- this.addToWaiting(n);
292
- }
293
- });
294
- }
295
- /**
296
- * Gets the next waiting match for the given segment
297
- * Uses value-specific index for dynamic segments, base index for static segments
298
- */
299
- dequeueWaitingMatch(t) {
300
- const e = t.captured ? lt(t.index, t.value) : t.index, n = this.completingStates.get(e);
301
- if (n?.length) return n.pop();
302
- const r = this.pendingStates.get(e);
303
- if (r?.length) return r.pop();
304
- }
305
- /**
306
- * Adds a state to the waiting list for the next expected segment
307
- */
308
- addToWaiting(t) {
309
- const e = t.nextSegment;
310
- if (t.isAwaitingLastSegment) {
311
- const n = this.completingStates.get(e) || [];
312
- n.length === 0 && this.completingStates.set(e, n), n.push(t);
313
- } else {
314
- const n = this.pendingStates.get(e) || [];
315
- n.length === 0 && this.pendingStates.set(e, n), n.push(t);
316
- }
317
- }
318
- /**
319
- * Add match to position-indexed array, replacing any existing match at the same position
320
- * Uses binary search to find insertion point and maintains sorted order
321
- * Relies on processing order to determine which match to keep
322
- */
323
- addToCompleted(t) {
324
- const e = t.start;
325
- let n = 0, r = this.completedStates.length;
326
- for (; n < r; ) {
327
- const o = Math.floor((n + r) / 2);
328
- this.completedStates[o].position < e ? n = o + 1 : r = o;
329
- }
330
- n < this.completedStates.length && this.completedStates[n].position === e ? this.completedStates[n].match = t : this.completedStates.splice(n, 0, { position: e, match: t });
331
- }
332
- }
333
- const A = (s) => s.replace(/[.*+?^${}()|[\]\\\\]/g, "\\$&");
334
- function $t(s, t, e) {
335
- const n = A(s), r = A(t), o = A(t + e);
336
- return `${n}([^${o}]+?)${r}`;
337
- }
338
- class Ht {
339
- staticRegex;
340
- staticToIndex;
341
- dynamicRegex;
342
- dynamicEntries;
343
- dynamicIndices;
344
- constructor(t) {
345
- this.initializeDual(t);
346
- }
347
- initializeDual(t) {
348
- const e = [], n = [], r = /* @__PURE__ */ new Map();
349
- if (t.forEach((o, i) => {
350
- typeof o == "string" ? (e.push(o), r.set(o, i)) : n.push(o);
351
- }), e.length > 0) {
352
- const i = [...e].sort((a, c) => c.length - a.length).map(A);
353
- this.staticRegex = new RegExp(`(?:${i.join("|")})`, "gu"), this.staticToIndex = r;
354
- }
355
- if (n.length > 0) {
356
- const o = /* @__PURE__ */ new Set(), i = [];
357
- n.forEach((a) => {
358
- const c = t.indexOf(a);
359
- if (typeof a == "string")
360
- i.push({ index: c, pattern: A(a), definition: a });
361
- else {
362
- const [l, d, f] = a;
363
- o.add(c);
364
- const g = $t(l, d, f).replace("(", `(?<content${c}>`);
365
- i.push({ index: c, pattern: g, definition: a });
366
- }
367
- }), i.sort((a, c) => {
368
- const l = typeof a.definition == "string" ? a.definition.length : a.pattern.length;
369
- return (typeof c.definition == "string" ? c.definition.length : c.pattern.length) - l;
370
- }), this.dynamicEntries = i, this.dynamicIndices = o, this.dynamicRegex = new RegExp(i.map((a, c) => `(?<seg${c}>${a.pattern})`).join("|"), "gu");
371
- }
372
- }
373
- search(t) {
374
- const e = [], n = [];
375
- if (this.staticRegex && this.staticToIndex)
376
- for (const o of t.matchAll(this.staticRegex)) {
377
- const i = this.staticToIndex.get(o[0]);
378
- i !== void 0 && e.push({
379
- index: i,
380
- start: o.index,
381
- end: o.index + o[0].length,
382
- value: o[0]
383
- });
384
- }
385
- if (this.dynamicRegex && this.dynamicEntries && this.dynamicIndices)
386
- for (const o of t.matchAll(this.dynamicRegex)) {
387
- const i = o[0], a = o.index;
388
- let c, l;
389
- if (o.groups) {
390
- for (let d = 0; d < this.dynamicEntries.length; d++)
391
- if (o.groups[`seg${d}`] !== void 0) {
392
- c = this.dynamicEntries[d].index, this.dynamicIndices.has(c) && (l = o.groups[`content${c}`]);
393
- break;
394
- }
395
- }
396
- c !== void 0 && n.push({
397
- index: c,
398
- start: a,
399
- end: a + i.length,
400
- value: i,
401
- captured: l
402
- });
403
- }
404
- const r = [...n];
405
- for (const o of e)
406
- n.some(
407
- (a) => o.start < a.end && o.end > a.start
408
- ) || r.push(o);
409
- return r.sort((o, i) => o.start - i.start), r;
410
- }
411
- }
412
- const dt = (s, t = 0, e = s.length) => ({
413
- type: "text",
414
- content: s.substring(t, e),
415
- position: { start: t, end: e }
416
- });
417
- class Ft {
418
- // Instance fields - only what's needed for single pass
419
- input;
420
- // ===== PUBLIC API =====
421
- /**
422
- * Builds nested token tree from pre-processed matches in a single pass
423
- *
424
- * Algorithm:
425
- * 1. Iterate through matches in order
426
- * 2. For each match:
427
- * - Close any parents whose content ends before this match
428
- * - Skip matches that conflict with the last accepted match
429
- * - Add text token before this match
430
- * - Create mark token and push to appropriate parent
431
- * - If match has nested content, push to active parents stack
432
- * 3. After all matches, close remaining parents and add final text
433
- *
434
- * Complexity: O(M) where M is number of matches
435
- * Memory: O(D) for active parents stack where D is nesting depth
436
- */
437
- build(t, e) {
438
- return this.input = e, t.length === 0 ? [this.createTextToken(0, e.length)] : this.buildSinglePass(t);
439
- }
440
- // ===== SINGLE-PASS ALGORITHM =====
441
- /**
442
- * Builds token tree in a single pass through matches
443
- *
444
- * This is the core algorithm that processes matches sequentially,
445
- * maintaining a stack of active parents and creating tokens directly.
446
- */
447
- buildSinglePass(t) {
448
- const e = [], n = [];
449
- let r = null, o = 0;
450
- for (const i of t) {
451
- if (r && i.conflictsWith(r))
452
- continue;
453
- for (r = i; n.length > 0; ) {
454
- const l = n[n.length - 1], d = this.getContentBounds(l.match);
455
- if (d.end <= i.start)
456
- this.finalizeParent(l, d.end), n.pop(), n.length > 0 ? n[n.length - 1].token.children.push(l.token) : e.push(l.token);
457
- else
458
- break;
459
- }
460
- const a = n.length > 0 ? n[n.length - 1] : null;
461
- if (a) {
462
- const l = this.createTextToken(a.textPos, i.start);
463
- a.token.children.push(l), a.textPos = i.end;
464
- } else {
465
- const l = this.createTextToken(o, i.start);
466
- e.push(l), o = i.end;
467
- }
468
- const c = this.createMarkToken(i);
469
- if (this.hasNestedContent(i)) {
470
- const l = this.getContentBounds(i);
471
- n.push({
472
- match: i,
473
- token: c,
474
- textPos: l.start
475
- });
476
- } else
477
- a ? a.token.children.push(c) : e.push(c);
478
- }
479
- for (; n.length > 0; ) {
480
- const i = n.pop(), a = this.getContentBounds(i.match);
481
- this.finalizeParent(i, a.end), n.length > 0 ? n[n.length - 1].token.children.push(i.token) : e.push(i.token);
482
- }
483
- return e.push(this.createTextToken(o, this.input.length)), e;
484
- }
485
- /**
486
- * Finalizes a parent token by adding final text token if needed
487
- */
488
- finalizeParent(t, e) {
489
- const n = this.createTextToken(t.textPos, e);
490
- t.token.children.push(n), t.token.children.some((o) => o.type === "mark") || (t.token.children = []);
491
- }
492
- /**
493
- * Creates a mark token from a match (without children - those are added later)
494
- */
495
- createMarkToken(t) {
496
- const e = this.extractSubstring(t.gaps.value?.start, t.gaps.value?.end), n = this.extractSubstring(t.gaps.nested?.start, t.gaps.nested?.end), r = this.extractSubstring(t.gaps.meta?.start, t.gaps.meta?.end), o = n || void 0, i = t.gaps.meta !== void 0 ? r : void 0, a = e || o || "";
497
- return {
498
- type: "mark",
499
- content: this.input.substring(t.start, t.end),
500
- children: [],
501
- // Will be populated if match has nested content
502
- descriptor: t.descriptor,
503
- value: a,
504
- meta: i,
505
- position: { start: t.start, end: t.end },
506
- nested: this.createNestedInfo(t, o)
507
- };
508
- }
509
- // ===== UTILITY METHODS =====
510
- /**
511
- * Gets the content boundaries for a match
512
- * Priority: nested content if present, otherwise value content
513
- */
514
- getContentBounds(t) {
515
- return t.gaps.nested ? t.gaps.nested : t.gaps.value ? t.gaps.value : {
516
- start: t.start,
517
- end: t.start
518
- };
519
- }
520
- /**
521
- * Checks if a match has nested content capability
522
- */
523
- hasNestedContent(t) {
524
- return t.gaps.nested !== void 0;
525
- }
526
- /**
527
- * Extracts substring safely, returns empty string if positions are undefined
528
- */
529
- extractSubstring(t, e) {
530
- return t !== void 0 && e !== void 0 ? this.input.substring(t, e) : "";
531
- }
532
- /**
533
- * Creates a text token for a range in the input
534
- */
535
- createTextToken(t, e) {
536
- return dt(this.input, t, e);
537
- }
538
- /**
539
- * Creates nested info object if nested content exists
540
- */
541
- createNestedInfo(t, e) {
542
- if (!(!e || t.gaps.nested === void 0))
543
- return {
544
- content: e,
545
- start: t.gaps.nested.start,
546
- end: t.gaps.nested.end
547
- };
548
- }
549
- }
550
- function Y(s, t) {
551
- let e = "";
552
- for (const n of s)
553
- if (n.type === "text")
554
- e += n.content;
555
- else if (n.children.length > 0) {
556
- const r = Y(n.children, t), o = {
557
- ...n,
558
- value: r
559
- };
560
- e += t(o);
561
- } else
562
- e += t(n);
563
- return e;
564
- }
565
- function q(s, t) {
566
- let e = s;
567
- return t.value !== void 0 && (e = e.replaceAll(x.Value, t.value)), t.meta !== void 0 && (e = e.replaceAll(x.Meta, t.meta)), t.nested !== void 0 && (e = e.replaceAll(x.Nested, t.nested)), e;
568
- }
569
- function N(s) {
570
- let t = "";
571
- for (const e of s) {
572
- if (e.type === "text") {
573
- t += e.content;
574
- continue;
575
- }
576
- const n = e.descriptor.markup, r = n.includes(x.Nested) ? e.children.length > 0 ? N(e.children) : e.nested?.content : void 0;
577
- t += q(n, {
578
- value: e.value,
579
- meta: e.meta,
580
- nested: r
581
- });
582
- }
583
- return t;
584
- }
585
- class F {
586
- registry;
587
- segmentMatcher;
588
- patternMatcher;
589
- treeBuilder;
590
- /**
591
- * Creates a new Parser instance with the specified markup patterns
592
- *
593
- * @param markups - Array of markup pattern strings with placeholders (can include undefined values):
594
- * - `__value__` - main content (plain text, no nesting)
595
- * - `__meta__` - metadata (plain text, no nesting)
596
- * - `__nested__` - content supporting nested structures
597
- * - `undefined` - skipped, but original array indices are preserved for descriptor matching
598
- *
599
- * @example
600
- * ```typescript
601
- * const parser = new Parser([
602
- * '@[__value__](__meta__)', // @[label](value) - descriptor.index = 0
603
- * undefined, // skipped
604
- * '#[__nested__]', // #[nested content] - descriptor.index = 2
605
- * '**__nested__**' // **bold text** - descriptor.index = 3
606
- * ])
607
- * ```
608
- */
609
- constructor(t) {
610
- this.registry = new _t(t), this.segmentMatcher = new Ht(this.registry.segments), this.patternMatcher = new Vt(this.registry), this.treeBuilder = new Ft();
611
- }
612
- /**
613
- * Parses text into tokens (static convenience method)
614
- *
615
- * @param value - Text to parse
616
- * @param options - Options with markup patterns
617
- * @returns Array of tokens (TextToken and MarkToken)
618
- *
619
- * @example
620
- * ```typescript
621
- * const tokens = Parser.parse('Hello @[world]', {
622
- * markup: ['@[__value__]']
623
- * })
624
- * ```
625
- */
626
- static parse(t, e) {
627
- const n = e?.markup;
628
- return !n || n.length === 0 ? [dt(t)] : new F(n).parse(t);
629
- }
630
- /**
631
- * Converts tokens back to text (static convenience method)
632
- *
633
- * @param tokens - Array of tokens to convert
634
- * @returns Reconstructed text string
635
- *
636
- * @example
637
- * ```typescript
638
- * const text = Parser.stringify(tokens)
639
- * ```
640
- */
641
- static stringify(t) {
642
- return N(t);
643
- }
644
- /**
645
- * Parses text into a nested token tree
646
- *
647
- * This is the main parsing method. It processes the input text through
648
- * three stages:
649
- * 1. Segment matching - finds all markup segments (O(N + M))
650
- * 2. Pattern matching - builds complete patterns from segments (O(M))
651
- * 3. Tree building - constructs nested token tree (O(M·D))
652
- *
653
- * @param value - Text to parse
654
- * @returns Array of tokens representing the parsed structure
655
- *
656
- * @example
657
- * ```typescript
658
- * const parser = new Parser(['@[__value__](__meta__)'])
659
- * const tokens = parser.parse('Hello @[world](test)')
660
- * // Returns: [
661
- * // TextToken('Hello '),
662
- * // MarkToken('@[world](test)', value='world', meta='test'),
663
- * // TextToken('')
664
- * // ]
665
- * ```
666
- */
667
- parse(t) {
668
- const e = this.segmentMatcher.search(t), n = this.patternMatcher.process(e);
669
- return this.treeBuilder.build(n, t);
670
- }
671
- /**
672
- * Converts tokens back to the original text
673
- *
674
- * This is the inverse operation of parse(). It reconstructs the original
675
- * text from a token tree, preserving all markup and structure.
676
- *
677
- * @param tokens - Array of tokens to convert
678
- * @returns Reconstructed text string
679
- *
680
- * @example
681
- * ```typescript
682
- * const text = 'Hello @[world](test)'
683
- * const tokens = parser.parse(text)
684
- * const reconstructed = parser.stringify(tokens)
685
- * console.log(reconstructed === text) // true
686
- * ```
687
- */
688
- stringify(t) {
689
- return N(t);
690
- }
691
- /**
692
- * Transforms annotated text by processing all mark tokens with a callback
693
- *
694
- * This method parses the text, recursively processes all MarkTokens
695
- * (including nested ones) with the provided callback, and returns
696
- * the transformed text.
697
- *
698
- * @param value - Annotated text to process
699
- * @param callback - Function to transform each MarkToken
700
- * @returns Transformed text
701
- *
702
- * @example
703
- * ```typescript
704
- * // Extract all values
705
- * const text = '@[Hello](world) and #[tag]'
706
- * const result = parser.transform(text, mark => mark.value)
707
- * // Returns: 'Hello and tag'
708
- *
709
- * // Custom transformation
710
- * const result = parser.transform(text, mark =>
711
- * mark.meta ? `${mark.value}:${mark.meta}` : mark.value
712
- * )
713
- * // Returns: 'Hello:world and tag'
714
- * ```
715
- */
716
- transform(t, e) {
717
- const n = this.parse(t);
718
- return Y(n, e);
719
- }
720
- /**
721
- * Escapes markup segments in the given text using backslash
722
- *
723
- * This method uses the registry's unique segments and escapes them by adding
724
- * a backslash before each character of each segment, preventing them from being
725
- * parsed as markup when the text is processed again.
726
- *
727
- * @param text - Text to escape segments in
728
- * @returns Text with escaped segments
729
- *
730
- * @example
731
- * ```typescript
732
- * const parser = new Parser(['**__nested__**', '@[__value__]'])
733
- * const escaped = parser.escape('Hello **world** and @[user]')
734
- * // Returns: 'Hello \*\*world\*\* and \@[user]'
735
- * ```
736
- */
737
- escape(t) {
738
- return this.registry.segments.filter((e) => typeof e == "string").sort((e, n) => n.length - e.length).reduce((e, n) => e.replaceAll(n, n.replace(/(.)/g, "\\$1")), t);
739
- }
740
- /**
741
- * Unescapes markup patterns in the given text
742
- *
743
- * This method removes escape characters from segments that were previously
744
- * escaped using escape(), allowing the patterns to be parsed normally.
745
- *
746
- * @param text - Text to unescape patterns in
747
- * @returns Text with unescaped patterns
748
- *
749
- * @example
750
- * ```typescript
751
- * const parser = new Parser(['**__nested__**', '@[__value__]'])
752
- * const unescaped = parser.unescape('Hello \*\*world\*\* and \@[user]')
753
- * // Returns: 'Hello **world** and @[user]'
754
- * ```
755
- */
756
- unescape(t) {
757
- return t.replaceAll(/\\(.)/g, "$1");
758
- }
759
- }
760
- function Ge(s, t, e) {
761
- if (!e.length) return s;
762
- const n = new F(e).parse(s);
763
- return Y(n, t);
764
- }
765
- function z(s, t, e = 0, n) {
766
- for (const r of s) {
767
- if (r === t) return { depth: e, parent: n };
768
- if (r.type === "mark") {
769
- const o = z(r.children, t, e + 1, r);
770
- if (o) return o;
771
- }
772
- }
773
- }
774
- function Wt(s = "", t = "") {
775
- if (s === t) return {};
776
- let e;
777
- for (let r = 0; r < s.length; r++)
778
- if (s[r] !== t[r]) {
779
- e = r;
780
- break;
781
- }
782
- let n;
783
- for (let r = 1; r <= s.length; r++)
784
- if (s.at(-r) !== t.at(-r)) {
785
- n = s.length - r + 1;
786
- break;
787
- }
788
- return { left: e, right: n };
789
- }
790
- function rt(s, t) {
791
- let e = -1, n = s.length;
792
- for (; n - e > 1; ) {
793
- const r = Math.round((e + n) / 2);
794
- s[r] <= t ? e = r : n = r;
795
- }
796
- return s[e] == t && (n = e), [e, n].filter((r) => s[r] !== void 0);
797
- }
798
- function Gt(s) {
799
- const { focus: t } = s.nodes, e = s.state.parser.get(), n = s.state.tokens.get();
800
- if (!e)
801
- return n;
802
- const r = e.parse(t.content);
803
- return r.length === 1 ? n : n.toSpliced(t.index, 1, ...r);
804
- }
805
- function Ut(s) {
806
- const t = s.state.value.get(), e = jt(s), n = Wt(s.state.previousValue.get(), t);
807
- if (!n.left && !n.right)
808
- return s.state.previousValue.set(t), s.state.tokens.get();
809
- s.state.previousValue.set(t);
810
- const r = s.state.tokens.get();
811
- switch (!0) {
812
- case (n.left !== void 0 && e.includes(n.left) && n.right !== void 0 && Math.abs(n.left - n.right) > 1): {
813
- const o = e.indexOf(n.left), i = U(s, o - 1, o);
814
- return r.toSpliced(o - 1, 2, ...i);
815
- }
816
- case n.left !== void 0: {
817
- const [o] = rt(e, n.left), i = U(s, o);
818
- return i.length === 1 ? r : r.toSpliced(o, 1, ...i);
819
- }
820
- case n.right !== void 0: {
821
- const [o] = rt(e, n.right), i = U(s, o);
822
- return i.length === 1 ? r : r.toSpliced(o, 1, ...i);
823
- }
824
- default:
825
- return _(s, t ?? "");
826
- }
827
- }
828
- function U(s, ...t) {
829
- let e = "";
830
- const n = s.state.tokens.get();
831
- for (const r of t) {
832
- const o = n[r];
833
- e += o.content;
834
- }
835
- return _(s, e);
836
- }
837
- function jt(s) {
838
- let t = 0;
839
- return s.state.tokens.get().map((n) => {
840
- const r = n.content.length;
841
- return t += r, t - r;
842
- }) ?? [];
843
- }
844
- function _(s, t) {
845
- const e = s.state.parser.get();
846
- return e ? e.parse(t) : [
847
- {
848
- type: "text",
849
- content: t,
850
- position: { start: 0, end: t.length }
851
- }
852
- ];
853
- }
854
- const qt = (s, t) => s === t;
855
- class B {
856
- #t;
857
- #e = /* @__PURE__ */ new Set();
858
- #n;
859
- constructor(t, e) {
860
- this.#t = t, this.#n = e?.equals ?? qt;
861
- }
862
- static event() {
863
- return new B(void 0, { equals: !1 });
864
- }
865
- get value() {
866
- return this.#t;
867
- }
868
- set value(t) {
869
- (this.#n === !1 || !this.#n(this.#t, t)) && (this.#t = t, this.#e.forEach((e) => e(t)));
870
- }
871
- on(t) {
872
- return this.#e.add(t), () => this.#e.delete(t);
873
- }
874
- emit(t) {
875
- arguments.length > 0 && (this.#t = t), this.#e.forEach((e) => e(this.#t));
876
- }
877
- get() {
878
- return this.#t;
879
- }
880
- set(t) {
881
- this.value = t;
882
- }
883
- }
884
- function zt(s, t) {
885
- const e = {};
886
- return e.get = () => s.get(), e.set = (n) => s.set(n), e.on = (n) => s.on(n), e.use = t(e), e;
887
- }
888
- function Xt(s, t) {
889
- const e = /* @__PURE__ */ new Map();
890
- for (const n in s)
891
- e.set(n, new B(s[n]));
892
- return new Proxy(s, {
893
- get(n, r) {
894
- if (r === "set")
895
- return (i) => {
896
- for (const a in i)
897
- e.get(a)?.set(i[a]);
898
- };
899
- const o = e.get(r);
900
- if (o)
901
- return zt(o, t);
902
- }
903
- });
904
- }
905
- function Zt(s) {
906
- const t = function(e) {
907
- s.emit(e);
908
- };
909
- return t.on = (e) => s.on(e), t;
910
- }
911
- function Jt(s) {
912
- const t = [], e = /* @__PURE__ */ new Map();
913
- for (const n of t)
914
- e.set(n, B.event());
915
- return new Proxy({}, {
916
- get(n, r) {
917
- let o = e.get(r);
918
- return o || (o = B.event(), e.set(r, o)), Zt(o);
919
- }
920
- });
921
- }
922
- class Qt {
923
- #t = 1;
924
- #e = /* @__PURE__ */ new WeakMap();
925
- get(t) {
926
- return this.#e.has(t) ? this.#e.get(t) : (this.#e.set(t, this.#t), this.#t++);
927
- }
928
- }
929
- class w {
930
- static get isSelectedPosition() {
931
- const t = window.getSelection();
932
- if (t)
933
- return t.isCollapsed;
934
- }
935
- static getCurrentPosition() {
936
- return window.getSelection()?.anchorOffset ?? 0;
937
- }
938
- //TODO get span from state?
939
- static getFocusedSpan() {
940
- return window.getSelection()?.anchorNode?.textContent ?? "";
941
- }
942
- static getSelectedNode() {
943
- const t = window.getSelection()?.anchorNode;
944
- if (t) return t;
945
- throw new Error("Anchor node of selection is not exists!");
946
- }
947
- //TODO add the returned type: "{left: CSSProperties["left"], top: CSSProperties["top"]}"?
948
- static getAbsolutePosition() {
949
- const t = window.getSelection()?.getRangeAt(0).getBoundingClientRect?.();
950
- return t ? { left: t.left, top: t.top + t.height + 1 } : { left: 0, top: 0 };
951
- }
952
- static trySetIndex(t, e) {
953
- try {
954
- this.setIndex(t, e);
955
- } catch (n) {
956
- console.error(n);
957
- }
958
- }
959
- static setIndex(t, e) {
960
- const n = window.getSelection();
961
- if (!n?.anchorNode || !n.rangeCount) return;
962
- const r = n.getRangeAt(0);
963
- r?.setStart(t.firstChild || t, e), r?.setEnd(t.firstChild || t, e);
964
- }
965
- static getCaretIndex(t) {
966
- let e = 0;
967
- const n = window.getSelection();
968
- if (!n?.rangeCount) return e;
969
- const r = n.getRangeAt(0), o = r.cloneRange();
970
- return o.selectNodeContents(t), o.setEnd(r.endContainer, r.endOffset), e = o.toString().length, e;
971
- }
972
- static setCaretToEnd(t) {
973
- if (!t) return;
974
- window.getSelection()?.setPosition(t, 1);
975
- }
976
- static getIndex() {
977
- return window.getSelection()?.anchorOffset ?? NaN;
978
- }
979
- static setIndex1(t) {
980
- const e = window.getSelection();
981
- if (!e?.anchorNode || !e.rangeCount) return;
982
- const n = e.getRangeAt(0);
983
- n?.setStart(n.startContainer.firstChild || n.startContainer, t), n?.setEnd(n.startContainer.firstChild || n.startContainer, t);
984
- }
985
- setCaretRightTo(t, e) {
986
- const r = window.getSelection()?.getRangeAt(0);
987
- r?.setStart(r.endContainer, e), r?.setEnd(r.endContainer, e);
988
- }
989
- }
990
- const Kt = new RegExp(/^\w*/);
991
- class tt {
992
- span;
993
- node;
994
- dividedText;
995
- constructor() {
996
- const t = w.getCurrentPosition();
997
- this.node = w.getSelectedNode(), this.span = w.getFocusedSpan(), this.dividedText = this.getDividedTextBy(t);
998
- }
999
- /**
1000
- * Find overlay match in text using provided options and trigger extractor.
1001
- * @template T - Type of option objects
1002
- * @param options - Array of options to search through
1003
- * @param getTrigger - Function that extracts trigger from each option
1004
- * @returns OverlayMatch with correct option type or undefined
1005
- *
1006
- * @example
1007
- * // React usage
1008
- * TriggerFinder.find(options, (opt) => opt.slotProps?.overlay?.trigger ?? '@')
1009
- *
1010
- * @example
1011
- * // Other framework usage
1012
- * TriggerFinder.find(vueOptions, (opt) => opt.overlay?.trigger ?? '@')
1013
- */
1014
- static find(t, e) {
1015
- if (t && w.isSelectedPosition)
1016
- return new tt().find(t, e);
1017
- }
1018
- getDividedTextBy(t) {
1019
- return { left: this.span.slice(0, t), right: this.span.slice(t) };
1020
- }
1021
- /**
1022
- * Find overlay match in provided options.
1023
- * @template T - Type of option objects
1024
- * @param options - Array of options
1025
- * @param getTrigger - Function to extract trigger from each option
1026
- */
1027
- find(t, e) {
1028
- for (let n = 0; n < t.length; n++) {
1029
- const r = t[n], o = e(r, n);
1030
- if (!o) continue;
1031
- const i = this.matchInTextVia(o);
1032
- if (i)
1033
- return {
1034
- value: i.word,
1035
- source: i.annotation,
1036
- index: i.index,
1037
- span: this.span,
1038
- node: this.node,
1039
- option: r
1040
- };
1041
- }
1042
- }
1043
- matchInTextVia(t = "@") {
1044
- const e = this.matchRightPart(), n = this.matchLeftPart(t);
1045
- if (n)
1046
- return {
1047
- word: n.word + e.word,
1048
- annotation: n.annotation + e.word,
1049
- index: n.index
1050
- };
1051
- }
1052
- matchRightPart() {
1053
- const { right: t } = this.dividedText;
1054
- return { word: t.match(Kt)?.[0] };
1055
- }
1056
- matchLeftPart(t) {
1057
- const e = this.makeTriggerRegex(t), { left: n } = this.dividedText, r = n.match(e);
1058
- if (!r) return;
1059
- const [o, i] = r;
1060
- return { word: i, annotation: o, index: r.index ?? 0 };
1061
- }
1062
- //TODO new overlayMatch option if (isSpaceBeforeRequired) append space check for not first words '\\s'
1063
- makeTriggerRegex(t) {
1064
- const e = A(t) + "(\\w*)$";
1065
- return new RegExp(e);
1066
- }
1067
- }
1068
- class V {
1069
- #t;
1070
- #e;
1071
- get target() {
1072
- return this.#t;
1073
- }
1074
- set target(t) {
1075
- this.#t = t;
1076
- }
1077
- get next() {
1078
- return new V(this.target?.nextSibling, this.#e);
1079
- }
1080
- get prev() {
1081
- return new V(this.target?.previousSibling, this.#e);
1082
- }
1083
- get isSpan() {
1084
- return this.index % 2 === 0;
1085
- }
1086
- get isMark() {
1087
- return !this.isSpan;
1088
- }
1089
- get isEditable() {
1090
- return this.target?.isContentEditable ?? !1;
1091
- }
1092
- get isCaretAtBeginning() {
1093
- return this.target ? w.getCaretIndex(this.target) === 0 : void 0;
1094
- }
1095
- get isCaretAtEnd() {
1096
- return this.target ? w.getCaretIndex(this.target) === this.target.textContent?.length : void 0;
1097
- }
1098
- get index() {
1099
- return this.target?.parentElement ? [...this.target.parentElement.children].indexOf(this.target) : -1;
1100
- }
1101
- get caret() {
1102
- return this.target ? w.getCaretIndex(this.target) : -1;
1103
- }
1104
- set caret(t) {
1105
- this.target && w.trySetIndex(this.target, t);
1106
- }
1107
- get length() {
1108
- return this.target?.textContent?.length ?? -1;
1109
- }
1110
- get content() {
1111
- return this.target?.textContent ?? "";
1112
- }
1113
- set content(t) {
1114
- this.target && (this.target.textContent = t ?? "");
1115
- }
1116
- get head() {
1117
- return this.#e.refs.container?.firstChild;
1118
- }
1119
- get tail() {
1120
- return this.#e.refs.container?.lastChild;
1121
- }
1122
- get isFocused() {
1123
- return this.target === document.activeElement;
1124
- }
1125
- constructor(t, e) {
1126
- this.target = t, this.#e = e;
1127
- }
1128
- setCaretToEnd() {
1129
- w.setCaretToEnd(this.target);
1130
- }
1131
- focus() {
1132
- this.target?.focus();
1133
- }
1134
- clear() {
1135
- this.target = void 0;
1136
- }
1137
- }
1138
- function Yt(s, t, e, n) {
1139
- return s.slice(0, e) + t + s.slice(e + n.length);
1140
- }
1141
- function j(s, t) {
1142
- const n = {
1143
- prev: 2,
1144
- self: 1,
1145
- next: 0
1146
- }[s], { focus: r } = t.nodes, o = t.state.tokens.get(), [i, , a] = o.splice(r.index - n, 3), c = (i.type === "text", i.content), l = (a.type === "text", a.content);
1147
- t.state.tokens.set(
1148
- o.toSpliced(r.index - n, 0, {
1149
- type: "text",
1150
- content: c + l,
1151
- position: {
1152
- start: i.position.start,
1153
- end: a.position.end
1154
- }
1155
- })
1156
- );
1157
- let d = r;
1158
- for (let u = 0; u < n; u++)
1159
- d = d.prev;
1160
- const f = d.length;
1161
- t.state.recovery.set({ anchor: d.prev, caret: f }), t.state.onChange.get()?.(N(t.state.tokens.get()));
1162
- }
1163
- class te {
1164
- constructor(t) {
1165
- this.store = t;
1166
- }
1167
- #t;
1168
- enable() {
1169
- this.#t || (this.#t = this.store.state.readOnly.on(() => this.sync()), this.sync());
1170
- }
1171
- disable() {
1172
- this.#t?.(), this.#t = void 0;
1173
- }
1174
- sync() {
1175
- const t = this.store.refs.container;
1176
- if (!t) return;
1177
- const n = this.store.state.readOnly.get() ? "false" : "true", r = t.children;
1178
- for (let o = 0; o < r.length; o += 2)
1179
- r[o].contentEditable = n;
1180
- }
1181
- }
1182
- class ee {
1183
- constructor(t) {
1184
- this.store = t;
1185
- }
1186
- #t;
1187
- #e;
1188
- #n;
1189
- enable() {
1190
- this.#t || (this.#t = this.store.events.change.on(() => {
1191
- const t = this.store.state.onChange.get();
1192
- if (!this.store.nodes.focus.target) return;
1193
- const e = this.store.state.tokens.get(), n = e[this.store.nodes.focus.index];
1194
- n.type === "text" ? n.content = this.store.nodes.focus.content : n.type === "mark" && (n.value = this.store.nodes.focus.content), t?.(N(e)), this.store.events.parse();
1195
- }), this.#e = this.store.events.delete.on((t) => {
1196
- if (!t) return;
1197
- const { token: e } = t, n = this.store.state.onChange.get(), r = this.store.state.tokens.get(), o = r.indexOf(e);
1198
- this.store.state.tokens.set(r.toSpliced(o, 1)), n?.(N(this.store.state.tokens.get()));
1199
- }), this.#n = this.store.events.select.on((t) => {
1200
- if (!t) return;
1201
- const e = this.store.state.Mark.get(), n = this.store.state.onChange.get(), {
1202
- mark: r,
1203
- match: { option: o, span: i, index: a, source: c }
1204
- } = t, l = r.type === "mark" ? q(o.markup, {
1205
- value: r.value,
1206
- meta: r.meta
1207
- }) : q(o.markup, {
1208
- value: r.content
1209
- }), d = Yt(i, l, a, c);
1210
- if (this.store.state.recovery.set(
1211
- e ? {
1212
- caret: 0,
1213
- anchor: this.store.nodes.input.next,
1214
- isNext: !0,
1215
- childIndex: this.store.nodes.input.index
1216
- } : { caret: a + l.length, anchor: this.store.nodes.input }
1217
- ), this.store.nodes.input.target) {
1218
- this.store.nodes.input.content = d;
1219
- const f = this.store.state.tokens.get(), u = f[this.store.nodes.input.index];
1220
- u.type === "text" && (u.content = d), this.store.nodes.focus.target = this.store.nodes.input.target, this.store.nodes.input.clear(), n?.(N(f)), this.store.events.parse();
1221
- }
1222
- }));
1223
- }
1224
- disable() {
1225
- this.#t?.(), this.#e?.(), this.#n?.(), this.#t = void 0, this.#e = void 0, this.#n = void 0;
1226
- }
1227
- }
1228
- class ne {
1229
- constructor(t) {
1230
- this.store = t;
1231
- }
1232
- #t;
1233
- #e;
1234
- #n;
1235
- enable() {
1236
- if (this.#t) return;
1237
- const t = this.store.refs.container;
1238
- t && (this.#t = (e) => {
1239
- this.store.nodes.focus.target = e.target;
1240
- }, this.#e = () => {
1241
- this.store.nodes.focus.target = void 0;
1242
- }, this.#n = () => {
1243
- const e = this.store.state.tokens.get();
1244
- e.length === 1 && e[0].type === "text" && e[0].content === "" && this.store.refs.container?.firstElementChild?.focus();
1245
- }, t.addEventListener("focusin", this.#t), t.addEventListener("focusout", this.#e), t.addEventListener("click", this.#n));
1246
- }
1247
- disable() {
1248
- const t = this.store.refs.container;
1249
- !t || !this.#t || (t.removeEventListener("focusin", this.#t), t.removeEventListener("focusout", this.#e), t.removeEventListener("click", this.#n), this.#t = void 0, this.#e = void 0, this.#n = void 0);
1250
- }
1251
- recover() {
1252
- const t = this.store.state.recovery.get();
1253
- if (!t) return;
1254
- const { anchor: e, caret: n, isNext: r } = t, o = !e.target || !e.target.isConnected;
1255
- switch (!0) {
1256
- case (r && o): {
1257
- const i = this.store.refs.container, a = t.childIndex != null ? i?.children[t.childIndex + 2] : void 0;
1258
- a ? a.focus() : this.store.nodes.focus.tail.focus();
1259
- break;
1260
- }
1261
- case r:
1262
- e.prev.focus();
1263
- break;
1264
- case o:
1265
- this.store.nodes.focus.head.focus();
1266
- break;
1267
- default:
1268
- e.next.focus();
1269
- }
1270
- this.store.nodes.focus.caret = n, this.store.state.recovery.set(void 0);
1271
- }
1272
- }
1273
- function se(s, t) {
1274
- const { focus: e } = s.nodes;
1275
- if (e.isMark && !e.isEditable || e.isCaretAtBeginning) {
1276
- const n = e.prev;
1277
- return n.focus(), n.isFocused || (n.prev.focus(), t.preventDefault()), e.setCaretToEnd(), !0;
1278
- }
1279
- return !1;
1280
- }
1281
- function re(s, t) {
1282
- const { focus: e } = s.nodes;
1283
- if (e.isMark && !e.isEditable || e.isCaretAtEnd) {
1284
- const n = e.next;
1285
- return n.focus(), n.isFocused || (n.next.focus(), t.preventDefault()), !0;
1286
- }
1287
- return !1;
1288
- }
1289
- class oe {
1290
- constructor(t) {
1291
- this.store = t;
1292
- }
1293
- #t;
1294
- #e;
1295
- #n;
1296
- #s;
1297
- #r = null;
1298
- #o = !1;
1299
- enable() {
1300
- this.#t || (this.#t = (t) => {
1301
- this.#r = t.target, this.#o = !0;
1302
- }, this.#e = (t) => {
1303
- const e = this.#o, n = !this.store.refs.container?.contains(this.#r) || this.#r !== t.target, r = window.getSelection()?.containsNode(this.store.refs.container, !0);
1304
- e && n && r && this.store.state.selecting.set("drag");
1305
- }, this.#n = () => {
1306
- this.#o = !1, this.#r = null, this.store.state.selecting.set(void 0);
1307
- }, this.#s = () => {
1308
- if (this.store.state.selecting.get() !== "drag") return;
1309
- const t = [...this.store.refs.container.children], e = t.map((n) => n.contentEditable);
1310
- t.forEach((n) => n.contentEditable = "false"), t.forEach((n, r) => n.contentEditable = e[r]);
1311
- }, document.addEventListener("mousedown", this.#t), document.addEventListener("mousemove", this.#e), document.addEventListener("mouseup", this.#n), document.addEventListener("selectionchange", this.#s));
1312
- }
1313
- disable() {
1314
- this.#t && (document.removeEventListener("mousedown", this.#t), document.removeEventListener("mousemove", this.#e), document.removeEventListener("mouseup", this.#n), document.removeEventListener("selectionchange", this.#s), this.#t = void 0, this.#e = void 0, this.#n = void 0, this.#s = void 0), this.#r = null, this.#o = !1;
1315
- }
1316
- }
1317
- function ie(s, t) {
1318
- if ((t.ctrlKey || t.metaKey) && t.code === "KeyA") {
1319
- t.preventDefault();
1320
- const e = window.getSelection(), n = s.refs.container?.firstChild, r = s.refs.container?.lastChild;
1321
- if (!e || !n || !r) return;
1322
- e.setBaseAndExtent(n, 0, r, 1), s.state.selecting.set("all");
1323
- }
1324
- }
1325
- class ae {
1326
- constructor(t) {
1327
- this.store = t;
1328
- }
1329
- #t;
1330
- #e;
1331
- #n;
1332
- enable() {
1333
- if (this.#t) return;
1334
- const t = this.store.refs.container;
1335
- t && (this.#t = (e) => {
1336
- e.key === v.LEFT ? se(this.store, e) : e.key === v.RIGHT && re(this.store, e), this.#s(e), ie(this.store, e);
1337
- }, this.#e = (e) => {
1338
- de(this.store, e);
1339
- }, this.#n = (e) => {
1340
- ce(this.store, e);
1341
- }, t.addEventListener("keydown", this.#t), t.addEventListener("paste", this.#e), t.addEventListener("beforeinput", this.#n, !0));
1342
- }
1343
- disable() {
1344
- const t = this.store.refs.container;
1345
- !t || !this.#t || (t.removeEventListener("keydown", this.#t), t.removeEventListener("paste", this.#e), t.removeEventListener("beforeinput", this.#n, !0), this.#t = void 0, this.#e = void 0, this.#n = void 0);
1346
- }
1347
- #s(t) {
1348
- const { focus: e } = this.store.nodes;
1349
- if (t.key === v.DELETE || t.key === v.BACKSPACE) {
1350
- if (e.isMark) {
1351
- if (e.isEditable && (t.key === v.BACKSPACE && !e.isCaretAtBeginning || t.key === v.DELETE && !e.isCaretAtEnd))
1352
- return;
1353
- t.preventDefault(), j("self", this.store);
1354
- return;
1355
- }
1356
- t.key === v.BACKSPACE && e.isSpan && e.isCaretAtBeginning && e.prev.target && (t.preventDefault(), j("prev", this.store)), t.key === v.DELETE && e.isSpan && e.isCaretAtEnd && e.next.target && (t.preventDefault(), j("next", this.store));
1357
- }
1358
- }
1359
- }
1360
- function ce(s, t) {
1361
- const e = s.state.selecting.get();
1362
- if (e === "all" && ut(s)) {
1363
- if (t.inputType === "insertFromPaste") {
1364
- t.preventDefault();
1365
- return;
1366
- }
1367
- t.preventDefault();
1368
- const r = t.inputType.startsWith("delete") ? "" : t.data ?? "";
1369
- ht(s, r);
1370
- return;
1371
- }
1372
- e === "all" && s.state.selecting.set(void 0);
1373
- const { focus: n } = s.nodes;
1374
- !n.target || !n.isEditable || le(n, t) && s.events.change();
1375
- }
1376
- function le(s, t) {
1377
- const e = s.caret, n = s.content;
1378
- let r, o;
1379
- switch (t.inputType) {
1380
- case "insertText": {
1381
- t.preventDefault();
1382
- const i = t.data ?? "";
1383
- r = n.slice(0, e) + i + n.slice(e), o = e + i.length;
1384
- break;
1385
- }
1386
- case "deleteContentBackward":
1387
- case "deleteContentForward":
1388
- case "deleteWordBackward":
1389
- case "deleteWordForward":
1390
- case "deleteSoftLineBackward":
1391
- case "deleteSoftLineForward": {
1392
- const i = t.getTargetRanges();
1393
- if (!i.length) return !1;
1394
- const { startOffset: a, endOffset: c } = i[0];
1395
- if (a === c) return !1;
1396
- t.preventDefault(), r = n.slice(0, a) + n.slice(c), o = a;
1397
- break;
1398
- }
1399
- case "insertFromPaste":
1400
- case "insertReplacementText": {
1401
- const i = t.dataTransfer?.getData("text/plain") ?? "", a = t.getTargetRanges(), c = a[0]?.startOffset ?? e, l = a[0]?.endOffset ?? e;
1402
- t.preventDefault(), r = n.slice(0, c) + i + n.slice(l), o = c + i.length;
1403
- break;
1404
- }
1405
- default:
1406
- return !1;
1407
- }
1408
- return s.content = r, s.caret = o, !0;
1409
- }
1410
- function de(s, t) {
1411
- const e = s.state.selecting.get();
1412
- if (e !== "all" || !ut(s)) {
1413
- e === "all" && s.state.selecting.set(void 0);
1414
- return;
1415
- }
1416
- t.preventDefault();
1417
- const n = t.clipboardData?.getData("text/plain") ?? "";
1418
- ht(s, n);
1419
- }
1420
- function ut(s) {
1421
- const t = window.getSelection(), e = s.refs.container;
1422
- if (!t?.rangeCount || !e?.firstChild || !e?.lastChild) return !1;
1423
- try {
1424
- const n = t.getRangeAt(0);
1425
- return e.contains(n.startContainer) && e.contains(n.endContainer) && n.toString().length > 0;
1426
- } catch {
1427
- return !1;
1428
- }
1429
- }
1430
- function ht(s, t) {
1431
- s.nodes.focus.target = null, s.state.selecting.set(void 0), s.state.onChange.get()?.(t), s.state.value.get() === void 0 && s.state.tokens.set(
1432
- s.state.parser.get()?.parse(t) ?? [
1433
- {
1434
- type: "text",
1435
- content: t,
1436
- position: { start: 0, end: t.length }
1437
- }
1438
- ]
1439
- ), queueMicrotask(() => {
1440
- const e = s.refs.container?.firstChild;
1441
- e && (s.state.recovery.set({
1442
- anchor: s.nodes.focus,
1443
- caret: t.length
1444
- }), e.focus());
1445
- });
1446
- }
1447
- class ue {
1448
- #t = [];
1449
- #e = !1;
1450
- register(t) {
1451
- return this.#t.push(t), this;
1452
- }
1453
- enableAll() {
1454
- this.#e || (this.#e = !0, this.#t.forEach((t) => t.enable()));
1455
- }
1456
- disableAll() {
1457
- this.#e && (this.#e = !1, this.#t.forEach((t) => t.disable()));
1458
- }
1459
- }
1460
- const R = (s, t) => ({
1461
- name: s,
1462
- enable: () => t.enable(),
1463
- disable: () => t.disable()
1464
- }), he = (s) => {
1465
- const t = new ue();
1466
- return t.register(R("keydown", s.controllers.keydown)).register(R("system", s.controllers.system)).register(R("focus", s.controllers.focus)).register(R("textSelection", s.controllers.textSelection)).register(R("contentEditable", s.controllers.contentEditable)), t;
132
+ function k(e, t, n) {
133
+ if (n.length !== 2) return {
134
+ segments: e,
135
+ gapTypes: t
136
+ };
137
+ let [r, i] = n, a = [], o = e[r], s = e[r + 1];
138
+ o && s && a.push(A(o, s, e[r + 2]));
139
+ for (let t = r + 2; t < i; t++) a.push(e[t]);
140
+ let c = e[i], l = e[i + 1];
141
+ return c && l && a.push(A(c, l, e[i + 2])), {
142
+ segments: a,
143
+ gapTypes: t.filter((e) => e !== w.Value)
144
+ };
145
+ }
146
+ function A(e, t, n) {
147
+ if (!n) return [
148
+ e,
149
+ t,
150
+ ""
151
+ ];
152
+ let r = n.charAt(0);
153
+ return [
154
+ e,
155
+ t,
156
+ r && !t.includes(r) && !n.startsWith(e) ? r : ""
157
+ ];
158
+ }
159
+ //#endregion
160
+ //#region ../../common/core/src/features/parsing/ParserV2/core/MarkupRegistry.ts
161
+ var j = class {
162
+ markups;
163
+ descriptors;
164
+ segments = [];
165
+ firstSegmentIndexMap = /* @__PURE__ */ new Map();
166
+ constructor(e) {
167
+ this.markups = e;
168
+ let t = /* @__PURE__ */ new Map();
169
+ this.descriptors = e.map((e, n) => {
170
+ if (e === void 0) return null;
171
+ let r = T(e, n);
172
+ return r.segments.forEach((e, n) => {
173
+ this.processSegment(r, e, n, t);
174
+ }), this.addToFirstSegmentIndexMap(r), r;
175
+ }).filter((e) => e !== null);
176
+ }
177
+ addToFirstSegmentIndexMap(e) {
178
+ let t = e.segmentGlobalIndices[0];
179
+ if (t === void 0) return;
180
+ let n = this.firstSegmentIndexMap.get(t);
181
+ n ? n.push(e) : this.firstSegmentIndexMap.set(t, [e]);
182
+ }
183
+ processSegment(e, t, n, r) {
184
+ let i = this.getSegmentKey(t);
185
+ if (!i) return;
186
+ this.registerSegment(t, i, r);
187
+ let a = r.get(i);
188
+ if (e.segmentGlobalIndices[n] = a, typeof t != "string") {
189
+ let [e, n] = t;
190
+ e && this.registerSegment(e, e, r), n && this.registerSegment(n, n, r);
191
+ }
192
+ }
193
+ registerSegment(e, t, n) {
194
+ if (!n.has(t)) {
195
+ let r = this.segments.length;
196
+ this.segments.push(e), n.set(t, r);
197
+ }
198
+ }
199
+ getSegmentKey(e) {
200
+ if (typeof e == "string") return e;
201
+ let [t, n, r] = e;
202
+ return t || n ? `${t}|${n}|${r}` : "";
203
+ }
204
+ };
205
+ //#endregion
206
+ //#region ../../common/core/src/features/parsing/ParserV2/utils/getSegmentIndex.ts
207
+ function M(e, t) {
208
+ if (!t) return e;
209
+ let n = 5381;
210
+ for (let e = 0; e < t.length; e++) n = n * 33 ^ t.charCodeAt(e);
211
+ return n >>>= 0, e * 1e6 + (n & 1048575);
212
+ }
213
+ //#endregion
214
+ //#region ../../common/core/src/features/parsing/ParserV2/core/Match.ts
215
+ var ee = class {
216
+ gaps = {};
217
+ captured;
218
+ expectedSegmentIndex;
219
+ start;
220
+ end;
221
+ constructor(e, t) {
222
+ if (this.descriptor = e, this.expectedSegmentIndex = 1, this.start = t.start, this.end = t.end, e.segments.length === 1 && (this.expectedSegmentIndex = NaN, this.gaps.value = {
223
+ start: this.start,
224
+ end: this.end
225
+ }), e.hasTwoValues && t.captured) {
226
+ this.captured = t.captured;
227
+ let e = t.start + t.value.indexOf(t.captured), n = e + t.captured.length;
228
+ this.gaps.value = {
229
+ start: e,
230
+ end: n
231
+ };
232
+ }
233
+ }
234
+ get isInvalid() {
235
+ return this.expectedSegmentIndex === -1;
236
+ }
237
+ get isCompleted() {
238
+ return isNaN(this.expectedSegmentIndex);
239
+ }
240
+ get isAwaitingLastSegment() {
241
+ return this.expectedSegmentIndex === this.descriptor.segments.length - 1;
242
+ }
243
+ get nextSegment() {
244
+ if (this.isCompleted || this.isInvalid) return;
245
+ let e = this.descriptor.segmentGlobalIndices[this.expectedSegmentIndex], t = this.descriptor.segments[this.expectedSegmentIndex];
246
+ if (typeof t == "object" && this.descriptor.hasTwoValues && this.captured && this.isAwaitingLastSegment) {
247
+ let [n, r] = t;
248
+ return M(e, n + this.captured + r);
249
+ }
250
+ return e;
251
+ }
252
+ processNext(e) {
253
+ let t = this.end, n = e.start, r = this.descriptor.gapTypes[this.expectedSegmentIndex - 1];
254
+ if (n < t) {
255
+ this.expectedSegmentIndex = -1;
256
+ return;
257
+ }
258
+ this.gaps[r] = {
259
+ start: t,
260
+ end: n
261
+ }, this.end = e.end, this.expectedSegmentIndex++, this.expectedSegmentIndex >= this.descriptor.segments.length && (this.expectedSegmentIndex = NaN);
262
+ }
263
+ conflictsWith(e) {
264
+ return !e || this.start >= e.end ? !1 : !e.descriptor.hasNested || e.gaps.nested === void 0 ? !0 : !(this.start >= e.gaps.nested.start && this.end <= e.gaps.nested.end);
265
+ }
266
+ }, te = class {
267
+ pendingStates = /* @__PURE__ */ new Map();
268
+ completingStates = /* @__PURE__ */ new Map();
269
+ completedStates = [];
270
+ constructor(e) {
271
+ this.registry = e;
272
+ }
273
+ process(e) {
274
+ this.pendingStates.clear(), this.completingStates.clear(), this.completedStates.length = 0;
275
+ for (let t of e) this.processWaitingStates(t), this.tryStartNewStates(t);
276
+ return this.completedStates.map((e) => e.match);
277
+ }
278
+ processWaitingStates(e) {
279
+ let t = this.dequeueWaitingMatch(e);
280
+ if (t && (t.processNext(e), !t.isInvalid)) {
281
+ if (t.isCompleted) return this.addToCompleted(t);
282
+ this.addToWaiting(t);
283
+ }
284
+ }
285
+ tryStartNewStates(e) {
286
+ this.registry.firstSegmentIndexMap.get(e.index)?.forEach((t) => {
287
+ let n = new ee(t, e);
288
+ if (!n.isInvalid) {
289
+ if (n.isCompleted) return this.addToCompleted(n);
290
+ this.addToWaiting(n);
291
+ }
292
+ });
293
+ }
294
+ dequeueWaitingMatch(e) {
295
+ let t = e.captured ? M(e.index, e.value) : e.index, n = this.completingStates.get(t);
296
+ if (n?.length) return n.pop();
297
+ let r = this.pendingStates.get(t);
298
+ if (r?.length) return r.pop();
299
+ }
300
+ addToWaiting(e) {
301
+ let t = e.nextSegment;
302
+ if (e.isAwaitingLastSegment) {
303
+ let n = this.completingStates.get(t) || [];
304
+ n.length === 0 && this.completingStates.set(t, n), n.push(e);
305
+ } else {
306
+ let n = this.pendingStates.get(t) || [];
307
+ n.length === 0 && this.pendingStates.set(t, n), n.push(e);
308
+ }
309
+ }
310
+ addToCompleted(e) {
311
+ let t = e.start, n = 0, r = this.completedStates.length;
312
+ for (; n < r;) {
313
+ let e = Math.floor((n + r) / 2);
314
+ this.completedStates[e].position < t ? n = e + 1 : r = e;
315
+ }
316
+ n < this.completedStates.length && this.completedStates[n].position === t ? this.completedStates[n].match = e : this.completedStates.splice(n, 0, {
317
+ position: t,
318
+ match: e
319
+ });
320
+ }
321
+ }, N = (e) => e.replace(/[.*+?^${}()|[\]\\\\]/g, "\\$&");
322
+ //#endregion
323
+ //#region ../../common/core/src/features/parsing/ParserV2/core/SegmentMatcher.ts
324
+ function ne(e, t, n) {
325
+ let r = N(e), i = N(t);
326
+ return `${r}([^${N(t + n)}]+?)${i}`;
327
+ }
328
+ var re = class {
329
+ staticRegex;
330
+ staticToIndex;
331
+ dynamicRegex;
332
+ dynamicEntries;
333
+ dynamicIndices;
334
+ constructor(e) {
335
+ this.initializeDual(e);
336
+ }
337
+ initializeDual(e) {
338
+ let t = [], n = [], r = /* @__PURE__ */ new Map();
339
+ if (e.forEach((e, i) => {
340
+ typeof e == "string" ? (t.push(e), r.set(e, i)) : n.push(e);
341
+ }), t.length > 0) {
342
+ let e = [...t].sort((e, t) => t.length - e.length).map(N);
343
+ this.staticRegex = RegExp(`(?:${e.join("|")})`, "gu"), this.staticToIndex = r;
344
+ }
345
+ if (n.length > 0) {
346
+ let t = /* @__PURE__ */ new Set(), r = [];
347
+ n.forEach((n) => {
348
+ let i = e.indexOf(n);
349
+ if (typeof n == "string") r.push({
350
+ index: i,
351
+ pattern: N(n),
352
+ definition: n
353
+ });
354
+ else {
355
+ let [e, a, o] = n;
356
+ t.add(i);
357
+ let s = ne(e, a, o).replace("(", `(?<content${i}>`);
358
+ r.push({
359
+ index: i,
360
+ pattern: s,
361
+ definition: n
362
+ });
363
+ }
364
+ }), r.sort((e, t) => {
365
+ let n = typeof e.definition == "string" ? e.definition.length : e.pattern.length;
366
+ return (typeof t.definition == "string" ? t.definition.length : t.pattern.length) - n;
367
+ }), this.dynamicEntries = r, this.dynamicIndices = t, this.dynamicRegex = new RegExp(r.map((e, t) => `(?<seg${t}>${e.pattern})`).join("|"), "gu");
368
+ }
369
+ }
370
+ search(e) {
371
+ let t = [], n = [];
372
+ if (this.staticRegex && this.staticToIndex) for (let n of e.matchAll(this.staticRegex)) {
373
+ let e = this.staticToIndex.get(n[0]);
374
+ e !== void 0 && t.push({
375
+ index: e,
376
+ start: n.index,
377
+ end: n.index + n[0].length,
378
+ value: n[0]
379
+ });
380
+ }
381
+ if (this.dynamicRegex && this.dynamicEntries && this.dynamicIndices) for (let t of e.matchAll(this.dynamicRegex)) {
382
+ let e = t[0], r = t.index, i, a;
383
+ if (t.groups) {
384
+ for (let e = 0; e < this.dynamicEntries.length; e++) if (t.groups[`seg${e}`] !== void 0) {
385
+ i = this.dynamicEntries[e].index, this.dynamicIndices.has(i) && (a = t.groups[`content${i}`]);
386
+ break;
387
+ }
388
+ }
389
+ i !== void 0 && n.push({
390
+ index: i,
391
+ start: r,
392
+ end: r + e.length,
393
+ value: e,
394
+ captured: a
395
+ });
396
+ }
397
+ let r = [...n];
398
+ for (let e of t) n.some((t) => e.start < t.end && e.end > t.start) || r.push(e);
399
+ return r.sort((e, t) => e.start - t.start), r;
400
+ }
401
+ }, ie = (e, t = 0, n = e.length) => ({
402
+ type: "text",
403
+ content: e.substring(t, n),
404
+ position: {
405
+ start: t,
406
+ end: n
407
+ }
408
+ }), ae = class {
409
+ input;
410
+ build(e, t) {
411
+ return this.input = t, e.length === 0 ? [this.createTextToken(0, t.length)] : this.buildSinglePass(e);
412
+ }
413
+ buildSinglePass(e) {
414
+ let t = [], n = [], r = null, i = 0;
415
+ for (let a of e) {
416
+ if (r && a.conflictsWith(r)) continue;
417
+ for (r = a; n.length > 0;) {
418
+ let e = n[n.length - 1], r = this.getContentBounds(e.match);
419
+ if (r.end <= a.start) this.finalizeParent(e, r.end), n.pop(), n.length > 0 ? n[n.length - 1].token.children.push(e.token) : t.push(e.token);
420
+ else break;
421
+ }
422
+ let e = n.length > 0 ? n[n.length - 1] : null;
423
+ if (e) {
424
+ let t = this.createTextToken(e.textPos, a.start);
425
+ e.token.children.push(t), e.textPos = a.end;
426
+ } else {
427
+ let e = this.createTextToken(i, a.start);
428
+ t.push(e), i = a.end;
429
+ }
430
+ let o = this.createMarkToken(a);
431
+ if (this.hasNestedContent(a)) {
432
+ let e = this.getContentBounds(a);
433
+ n.push({
434
+ match: a,
435
+ token: o,
436
+ textPos: e.start
437
+ });
438
+ } else e ? e.token.children.push(o) : t.push(o);
439
+ }
440
+ for (; n.length > 0;) {
441
+ let e = n.pop(), r = this.getContentBounds(e.match);
442
+ this.finalizeParent(e, r.end), n.length > 0 ? n[n.length - 1].token.children.push(e.token) : t.push(e.token);
443
+ }
444
+ return t.push(this.createTextToken(i, this.input.length)), t;
445
+ }
446
+ finalizeParent(e, t) {
447
+ let n = this.createTextToken(e.textPos, t);
448
+ e.token.children.push(n), e.token.children.some((e) => e.type === "mark") || (e.token.children = []);
449
+ }
450
+ createMarkToken(e) {
451
+ let t = this.extractSubstring(e.gaps.value?.start, e.gaps.value?.end), n = this.extractSubstring(e.gaps.nested?.start, e.gaps.nested?.end), r = this.extractSubstring(e.gaps.meta?.start, e.gaps.meta?.end), i = n || void 0, a = e.gaps.meta === void 0 ? void 0 : r, o = t || i || "";
452
+ return {
453
+ type: "mark",
454
+ content: this.input.substring(e.start, e.end),
455
+ children: [],
456
+ descriptor: e.descriptor,
457
+ value: o,
458
+ meta: a,
459
+ position: {
460
+ start: e.start,
461
+ end: e.end
462
+ },
463
+ nested: this.createNestedInfo(e, i)
464
+ };
465
+ }
466
+ getContentBounds(e) {
467
+ return e.gaps.nested ? e.gaps.nested : e.gaps.value ? e.gaps.value : {
468
+ start: e.start,
469
+ end: e.start
470
+ };
471
+ }
472
+ hasNestedContent(e) {
473
+ return e.gaps.nested !== void 0;
474
+ }
475
+ extractSubstring(e, t) {
476
+ return e !== void 0 && t !== void 0 ? this.input.substring(e, t) : "";
477
+ }
478
+ createTextToken(e, t) {
479
+ return ie(this.input, e, t);
480
+ }
481
+ createNestedInfo(e, t) {
482
+ if (!(!t || e.gaps.nested === void 0)) return {
483
+ content: t,
484
+ start: e.gaps.nested.start,
485
+ end: e.gaps.nested.end
486
+ };
487
+ }
488
+ };
489
+ //#endregion
490
+ //#region ../../common/core/src/features/parsing/ParserV2/utils/processTokens.ts
491
+ function oe(e, t) {
492
+ let n = "";
493
+ for (let r of e) if (r.type === "text") n += r.content;
494
+ else if (r.children.length > 0) {
495
+ let e = oe(r.children, t), i = {
496
+ ...r,
497
+ value: e
498
+ };
499
+ n += t(i);
500
+ } else n += t(r);
501
+ return n;
502
+ }
503
+ //#endregion
504
+ //#region ../../common/core/src/features/parsing/ParserV2/utils/annotate.ts
505
+ function P(e, t) {
506
+ let n = e;
507
+ return t.value !== void 0 && (n = n.replaceAll(C.Value, t.value)), t.meta !== void 0 && (n = n.replaceAll(C.Meta, t.meta)), t.nested !== void 0 && (n = n.replaceAll(C.Nested, t.nested)), n;
508
+ }
509
+ //#endregion
510
+ //#region ../../common/core/src/features/parsing/ParserV2/utils/toString.ts
511
+ function F(e) {
512
+ let t = "";
513
+ for (let n of e) {
514
+ if (n.type === "text") {
515
+ t += n.content;
516
+ continue;
517
+ }
518
+ let e = n.descriptor.markup, r = e.includes(C.Nested) ? n.children.length > 0 ? F(n.children) : n.nested?.content : void 0;
519
+ t += P(e, {
520
+ value: n.value,
521
+ meta: n.meta,
522
+ nested: r
523
+ });
524
+ }
525
+ return t;
526
+ }
527
+ //#endregion
528
+ //#region ../../common/core/src/features/parsing/ParserV2/Parser.ts
529
+ var se = class e {
530
+ registry;
531
+ segmentMatcher;
532
+ patternMatcher;
533
+ treeBuilder;
534
+ constructor(e) {
535
+ this.registry = new j(e), this.segmentMatcher = new re(this.registry.segments), this.patternMatcher = new te(this.registry), this.treeBuilder = new ae();
536
+ }
537
+ static parse(t, n) {
538
+ let r = n?.markup;
539
+ return !r || r.length === 0 ? [ie(t)] : new e(r).parse(t);
540
+ }
541
+ static stringify(e) {
542
+ return F(e);
543
+ }
544
+ parse(e) {
545
+ let t = this.segmentMatcher.search(e), n = this.patternMatcher.process(t);
546
+ return this.treeBuilder.build(n, e);
547
+ }
548
+ stringify(e) {
549
+ return F(e);
550
+ }
551
+ transform(e, t) {
552
+ return oe(this.parse(e), t);
553
+ }
554
+ escape(e) {
555
+ return this.registry.segments.filter((e) => typeof e == "string").sort((e, t) => t.length - e.length).reduce((e, t) => e.replaceAll(t, t.replace(/(.)/g, "\\$1")), e);
556
+ }
557
+ unescape(e) {
558
+ return e.replaceAll(/\\(.)/g, "$1");
559
+ }
560
+ };
561
+ //#endregion
562
+ //#region ../../common/core/src/features/parsing/ParserV2/utils/denote.ts
563
+ function ce(e, t, n) {
564
+ return n.length ? oe(new se(n).parse(e), t) : e;
565
+ }
566
+ //#endregion
567
+ //#region ../../common/core/src/features/parsing/utils/findToken.ts
568
+ function I(e, t, n = 0, r) {
569
+ for (let i of e) {
570
+ if (i === t) return {
571
+ depth: n,
572
+ parent: r
573
+ };
574
+ if (i.type === "mark") {
575
+ let e = I(i.children, t, n + 1, i);
576
+ if (e) return e;
577
+ }
578
+ }
579
+ }
580
+ //#endregion
581
+ //#region ../../common/core/src/features/preparsing/utils/findGap.ts
582
+ function le(e = "", t = "") {
583
+ if (e === t) return {};
584
+ let n;
585
+ for (let r = 0; r < e.length; r++) if (e[r] !== t[r]) {
586
+ n = r;
587
+ break;
588
+ }
589
+ let r;
590
+ for (let n = 1; n <= e.length; n++) if (e.at(-n) !== t.at(-n)) {
591
+ r = e.length - n + 1;
592
+ break;
593
+ }
594
+ return {
595
+ left: n,
596
+ right: r
597
+ };
598
+ }
599
+ //#endregion
600
+ //#region ../../common/core/src/features/preparsing/utils/getClosestIndexes.ts
601
+ function ue(e, t) {
602
+ let n = -1, r = e.length;
603
+ for (; r - n > 1;) {
604
+ let i = Math.round((n + r) / 2);
605
+ e[i] <= t ? n = i : r = i;
606
+ }
607
+ return e[n] == t && (r = n), [n, r].filter((t) => e[t] !== void 0);
608
+ }
609
+ //#endregion
610
+ //#region ../../common/core/src/features/parsing/utils/valueParser.ts
611
+ function de(e) {
612
+ let { focus: t } = e.nodes, n = e.state.parser.get(), r = e.state.tokens.get();
613
+ if (!n) return r;
614
+ let i = n.parse(t.content);
615
+ return i.length === 1 ? r : r.toSpliced(t.index, 1, ...i);
616
+ }
617
+ function fe(e) {
618
+ let t = e.state.value.get(), n = pe(e), r = le(e.state.previousValue.get(), t);
619
+ if (!r.left && !r.right) return e.state.previousValue.set(t), e.state.tokens.get();
620
+ e.state.previousValue.set(t);
621
+ let i = e.state.tokens.get();
622
+ switch (!0) {
623
+ case r.left !== void 0 && n.includes(r.left) && r.right !== void 0 && Math.abs(r.left - r.right) > 1: {
624
+ let t = n.indexOf(r.left), a = L(e, t - 1, t);
625
+ return i.toSpliced(t - 1, 2, ...a);
626
+ }
627
+ case r.left !== void 0: {
628
+ let [t] = ue(n, r.left), a = L(e, t);
629
+ return a.length === 1 ? i : i.toSpliced(t, 1, ...a);
630
+ }
631
+ case r.right !== void 0: {
632
+ let [t] = ue(n, r.right), a = L(e, t);
633
+ return a.length === 1 ? i : i.toSpliced(t, 1, ...a);
634
+ }
635
+ default: return R(e, t ?? "");
636
+ }
637
+ }
638
+ function L(e, ...t) {
639
+ let n = "", r = e.state.tokens.get();
640
+ for (let e of t) {
641
+ let t = r[e];
642
+ n += t.content;
643
+ }
644
+ return R(e, n);
645
+ }
646
+ function pe(e) {
647
+ let t = 0;
648
+ return e.state.tokens.get().map((e) => {
649
+ let n = e.content.length;
650
+ return t += n, t - n;
651
+ }) ?? [];
652
+ }
653
+ function R(e, t) {
654
+ let n = e.state.parser.get();
655
+ return n ? n.parse(t) : [{
656
+ type: "text",
657
+ content: t,
658
+ position: {
659
+ start: 0,
660
+ end: t.length
661
+ }
662
+ }];
663
+ }
664
+ //#endregion
665
+ //#region ../../common/core/src/shared/classes/Reactive.ts
666
+ var me = (e, t) => e === t, z = class e {
667
+ #e;
668
+ #t = /* @__PURE__ */ new Set();
669
+ #n;
670
+ constructor(e, t) {
671
+ this.#e = e, this.#n = t?.equals ?? me;
672
+ }
673
+ static event() {
674
+ return new e(void 0, { equals: !1 });
675
+ }
676
+ get value() {
677
+ return this.#e;
678
+ }
679
+ set value(e) {
680
+ (this.#n === !1 || !this.#n(this.#e, e)) && (this.#e = e, this.#t.forEach((t) => t(e)));
681
+ }
682
+ on(e) {
683
+ return this.#t.add(e), () => this.#t.delete(e);
684
+ }
685
+ emit(e) {
686
+ arguments.length > 0 && (this.#e = e), this.#t.forEach((e) => e(this.#e));
687
+ }
688
+ get() {
689
+ return this.#e;
690
+ }
691
+ set(e) {
692
+ this.value = e;
693
+ }
694
+ };
695
+ //#endregion
696
+ //#region ../../common/core/src/shared/classes/defineState.ts
697
+ function he(e, t) {
698
+ let n = {};
699
+ return n.get = () => e.get(), n.set = (t) => e.set(t), n.on = (t) => e.on(t), n.use = t(n), n;
700
+ }
701
+ function ge(e, t) {
702
+ let n = /* @__PURE__ */ new Map();
703
+ for (let t in e) n.set(t, new z(e[t]));
704
+ return new Proxy(e, { get(e, r) {
705
+ if (r === "set") return (e) => {
706
+ for (let t in e) n.get(t)?.set(e[t]);
707
+ };
708
+ let i = n.get(r);
709
+ if (i) return he(i, t);
710
+ } });
711
+ }
712
+ //#endregion
713
+ //#region ../../common/core/src/shared/classes/defineEvents.ts
714
+ function _e(e) {
715
+ let t = function(t) {
716
+ e.emit(t);
717
+ };
718
+ return t.on = (t) => e.on(t), t;
719
+ }
720
+ function ve(e) {
721
+ let t = e ? Object.keys(e) : [], n = /* @__PURE__ */ new Map();
722
+ for (let e of t) n.set(e, z.event());
723
+ return new Proxy({}, { get(e, t) {
724
+ let r = n.get(t);
725
+ return r || (r = z.event(), n.set(t, r)), _e(r);
726
+ } });
727
+ }
728
+ //#endregion
729
+ //#region ../../common/core/src/shared/classes/KeyGenerator.ts
730
+ var ye = class {
731
+ #e = 1;
732
+ #t = /* @__PURE__ */ new WeakMap();
733
+ get(e) {
734
+ return this.#t.has(e) ? this.#t.get(e) : (this.#t.set(e, this.#e), this.#e++);
735
+ }
736
+ }, B = class {
737
+ static get isSelectedPosition() {
738
+ let e = window.getSelection();
739
+ if (e) return e.isCollapsed;
740
+ }
741
+ static getCurrentPosition() {
742
+ return window.getSelection()?.anchorOffset ?? 0;
743
+ }
744
+ static getFocusedSpan() {
745
+ return window.getSelection()?.anchorNode?.textContent ?? "";
746
+ }
747
+ static getSelectedNode() {
748
+ let e = window.getSelection()?.anchorNode;
749
+ if (e) return e;
750
+ throw Error("Anchor node of selection is not exists!");
751
+ }
752
+ static getAbsolutePosition() {
753
+ let e = window.getSelection()?.getRangeAt(0).getBoundingClientRect?.();
754
+ return e ? {
755
+ left: e.left,
756
+ top: e.top + e.height + 1
757
+ } : {
758
+ left: 0,
759
+ top: 0
760
+ };
761
+ }
762
+ static getCaretRect() {
763
+ try {
764
+ return (window.getSelection()?.getRangeAt(0))?.getBoundingClientRect() ?? null;
765
+ } catch {
766
+ return null;
767
+ }
768
+ }
769
+ static isCaretOnFirstLine(e) {
770
+ let t = this.getCaretRect();
771
+ if (!t || t.height === 0) return !0;
772
+ let n = e.getBoundingClientRect();
773
+ return t.top < n.top + t.height + 2;
774
+ }
775
+ static isCaretOnLastLine(e) {
776
+ let t = this.getCaretRect();
777
+ if (!t || t.height === 0) return !0;
778
+ let n = e.getBoundingClientRect();
779
+ return t.bottom > n.bottom - t.height - 2;
780
+ }
781
+ static setAtX(e, t, n) {
782
+ let r = e.getBoundingClientRect(), i = n ?? r.top + r.height / 2, a = document.caretRangeFromPoint?.(t, i) ?? document.caretPositionFromPoint?.(t, i);
783
+ if (!a) return;
784
+ let o = window.getSelection();
785
+ if (!o) return;
786
+ let s;
787
+ if (a instanceof Range) s = a;
788
+ else if (a && typeof a == "object" && "offsetNode" in a) s = document.createRange(), s.setStart(a.offsetNode, a.offset), s.collapse(!0);
789
+ else return;
790
+ if (!e.contains(s.startContainer)) {
791
+ this.setIndex(e, Infinity);
792
+ return;
793
+ }
794
+ o.removeAllRanges(), o.addRange(s);
795
+ }
796
+ static trySetIndex(e, t) {
797
+ try {
798
+ this.setIndex(e, t);
799
+ } catch (e) {
800
+ console.error(e);
801
+ }
802
+ }
803
+ static setIndex(e, t) {
804
+ let n = window.getSelection();
805
+ if (!n) return;
806
+ let r = document.createTreeWalker(e, 4), i = r.nextNode();
807
+ if (!i) return;
808
+ let a = isFinite(t) ? Math.max(0, t) : Infinity;
809
+ for (; i;) {
810
+ let e = r.nextNode();
811
+ if (!e || a <= i.length) {
812
+ let e = isFinite(a) ? Math.min(a, i.length) : i.length, t = document.createRange();
813
+ t.setStart(i, e), t.collapse(!0), n.removeAllRanges(), n.addRange(t);
814
+ return;
815
+ }
816
+ a -= i.length, i = e;
817
+ }
818
+ }
819
+ static getCaretIndex(e) {
820
+ let t = 0, n = window.getSelection();
821
+ if (!n?.rangeCount) return t;
822
+ let r = n.getRangeAt(0), i = r.cloneRange();
823
+ return i.selectNodeContents(e), i.setEnd(r.endContainer, r.endOffset), t = i.toString().length, t;
824
+ }
825
+ static setCaretToEnd(e) {
826
+ e && this.setIndex(e, Infinity);
827
+ }
828
+ static getIndex() {
829
+ return window.getSelection()?.anchorOffset ?? NaN;
830
+ }
831
+ static setIndex1(e) {
832
+ let t = window.getSelection();
833
+ if (!t?.anchorNode || !t.rangeCount) return;
834
+ let n = t.getRangeAt(0);
835
+ n?.setStart(n.startContainer.firstChild || n.startContainer, e), n?.setEnd(n.startContainer.firstChild || n.startContainer, e);
836
+ }
837
+ setCaretRightTo(e, t) {
838
+ let n = window.getSelection()?.getRangeAt(0);
839
+ n?.setStart(n.endContainer, t), n?.setEnd(n.endContainer, t);
840
+ }
841
+ }, be = /* @__PURE__ */ new RegExp(/^\w*/), xe = class e {
842
+ span;
843
+ node;
844
+ dividedText;
845
+ constructor() {
846
+ let e = B.getCurrentPosition();
847
+ this.node = B.getSelectedNode(), this.span = B.getFocusedSpan(), this.dividedText = this.getDividedTextBy(e);
848
+ }
849
+ static find(t, n) {
850
+ if (t && B.isSelectedPosition) return new e().find(t, n);
851
+ }
852
+ getDividedTextBy(e) {
853
+ return {
854
+ left: this.span.slice(0, e),
855
+ right: this.span.slice(e)
856
+ };
857
+ }
858
+ find(e, t) {
859
+ for (let n = 0; n < e.length; n++) {
860
+ let r = e[n], i = t(r, n);
861
+ if (!i) continue;
862
+ let a = this.matchInTextVia(i);
863
+ if (a) return {
864
+ value: a.word,
865
+ source: a.annotation,
866
+ index: a.index,
867
+ span: this.span,
868
+ node: this.node,
869
+ option: r
870
+ };
871
+ }
872
+ }
873
+ matchInTextVia(e = "@") {
874
+ let t = this.matchRightPart(), n = this.matchLeftPart(e);
875
+ if (n) return {
876
+ word: n.word + t.word,
877
+ annotation: n.annotation + t.word,
878
+ index: n.index
879
+ };
880
+ }
881
+ matchRightPart() {
882
+ let { right: e } = this.dividedText;
883
+ return { word: e.match(be)?.[0] };
884
+ }
885
+ matchLeftPart(e) {
886
+ let t = this.makeTriggerRegex(e), { left: n } = this.dividedText, r = n.match(t);
887
+ if (!r) return;
888
+ let [i, a] = r;
889
+ return {
890
+ word: a,
891
+ annotation: i,
892
+ index: r.index ?? 0
893
+ };
894
+ }
895
+ makeTriggerRegex(e) {
896
+ let t = N(e) + "(\\w*)$";
897
+ return new RegExp(t);
898
+ }
899
+ }, Se = class e {
900
+ #e;
901
+ #t;
902
+ get target() {
903
+ return this.#e;
904
+ }
905
+ set target(e) {
906
+ this.#e = e;
907
+ }
908
+ get next() {
909
+ return new e(this.target?.nextSibling, this.#t);
910
+ }
911
+ get prev() {
912
+ return new e(this.target?.previousSibling, this.#t);
913
+ }
914
+ get isSpan() {
915
+ return this.index % 2 == 0;
916
+ }
917
+ get isMark() {
918
+ return !this.isSpan;
919
+ }
920
+ get isEditable() {
921
+ return this.target?.isContentEditable ?? !1;
922
+ }
923
+ get isCaretAtBeginning() {
924
+ if (this.target) return B.getCaretIndex(this.target) === 0;
925
+ }
926
+ get isCaretAtEnd() {
927
+ if (this.target) return B.getCaretIndex(this.target) === this.target.textContent?.length;
928
+ }
929
+ get index() {
930
+ return this.target?.parentElement ? [...this.target.parentElement.children].indexOf(this.target) : -1;
931
+ }
932
+ get caret() {
933
+ return this.target ? B.getCaretIndex(this.target) : -1;
934
+ }
935
+ set caret(e) {
936
+ this.target && B.trySetIndex(this.target, e);
937
+ }
938
+ get length() {
939
+ return this.target?.textContent?.length ?? -1;
940
+ }
941
+ get content() {
942
+ return this.target?.textContent ?? "";
943
+ }
944
+ set content(e) {
945
+ this.target && (this.target.textContent = e ?? "");
946
+ }
947
+ get head() {
948
+ return this.#t.refs.container?.firstChild;
949
+ }
950
+ get tail() {
951
+ return this.#t.refs.container?.lastChild;
952
+ }
953
+ get isFocused() {
954
+ return this.target === document.activeElement;
955
+ }
956
+ constructor(e, t) {
957
+ this.target = e, this.#t = t;
958
+ }
959
+ setCaretToEnd() {
960
+ B.setCaretToEnd(this.target);
961
+ }
962
+ focus() {
963
+ this.target?.focus();
964
+ }
965
+ clear() {
966
+ this.target = void 0;
967
+ }
968
+ };
969
+ //#endregion
970
+ //#region ../../common/core/src/features/text-manipulation/utils/createNewSpan.ts
971
+ function Ce(e, t, n, r) {
972
+ return e.slice(0, n) + t + e.slice(n + r.length);
973
+ }
974
+ //#endregion
975
+ //#region ../../common/core/src/features/text-manipulation/utils/deleteMark.ts
976
+ function V(e, t) {
977
+ let n = {
978
+ prev: 2,
979
+ self: 1,
980
+ next: 0
981
+ }[e], { focus: r } = t.nodes, i = t.state.tokens.get(), [a, , o] = i.splice(r.index - n, 3), s = (a.type, a.content), c = (o.type, o.content);
982
+ t.state.tokens.set(i.toSpliced(r.index - n, 0, {
983
+ type: "text",
984
+ content: s + c,
985
+ position: {
986
+ start: a.position.start,
987
+ end: o.position.end
988
+ }
989
+ }));
990
+ let l = r;
991
+ for (let e = 0; e < n; e++) l = l.prev;
992
+ let u = l.length;
993
+ t.state.recovery.set({
994
+ anchor: l.prev,
995
+ caret: u
996
+ }), t.state.onChange.get()?.(F(t.state.tokens.get()));
997
+ }
998
+ //#endregion
999
+ //#region ../../common/core/src/features/editable/ContentEditableController.ts
1000
+ var we = class {
1001
+ #e;
1002
+ constructor(e) {
1003
+ this.store = e;
1004
+ }
1005
+ enable() {
1006
+ this.#e || (this.#e = this.store.state.readOnly.on(() => this.sync()), this.sync());
1007
+ }
1008
+ disable() {
1009
+ this.#e?.(), this.#e = void 0;
1010
+ }
1011
+ sync() {
1012
+ let e = this.store.refs.container;
1013
+ if (!e) return;
1014
+ let t = this.store.state.readOnly.get() ? "false" : "true", n = e.children, r = this.store.state.drag.get() ? 1 : 2;
1015
+ for (let e = 0; e < n.length; e += r) n[e].contentEditable = t;
1016
+ }
1017
+ }, Te = class {
1018
+ #e;
1019
+ #t;
1020
+ #n;
1021
+ constructor(e) {
1022
+ this.store = e;
1023
+ }
1024
+ enable() {
1025
+ this.#e || (this.#e = this.store.events.change.on(() => {
1026
+ let e = this.store.state.onChange.get();
1027
+ if (!this.store.nodes.focus.target) return;
1028
+ let t = this.store.state.tokens.get(), n = t[this.store.nodes.focus.index];
1029
+ n.type === "text" ? n.content = this.store.nodes.focus.content : n.type === "mark" && (n.value = this.store.nodes.focus.content), e?.(F(t)), this.store.events.parse();
1030
+ }), this.#t = this.store.events.delete.on((e) => {
1031
+ if (!e) return;
1032
+ let { token: t } = e, n = this.store.state.onChange.get(), r = this.store.state.tokens.get(), i = r.indexOf(t);
1033
+ this.store.state.tokens.set(r.toSpliced(i, 1)), n?.(F(this.store.state.tokens.get()));
1034
+ }), this.#n = this.store.events.select.on((e) => {
1035
+ if (!e) return;
1036
+ let t = this.store.state.Mark.get(), n = this.store.state.onChange.get(), { mark: r, match: { option: i, span: a, index: o, source: s } } = e, c = r.type === "mark" ? P(i.markup, {
1037
+ value: r.value,
1038
+ meta: r.meta
1039
+ }) : P(i.markup, { value: r.content }), l = Ce(a, c, o, s);
1040
+ if (this.store.state.recovery.set(t ? {
1041
+ caret: 0,
1042
+ anchor: this.store.nodes.input.next,
1043
+ isNext: !0,
1044
+ childIndex: this.store.nodes.input.index
1045
+ } : {
1046
+ caret: o + c.length,
1047
+ anchor: this.store.nodes.input
1048
+ }), this.store.nodes.input.target) {
1049
+ this.store.nodes.input.content = l;
1050
+ let e = this.store.state.tokens.get(), t = e[this.store.nodes.input.index];
1051
+ t.type === "text" && (t.content = l), this.store.nodes.focus.target = this.store.nodes.input.target, this.store.nodes.input.clear(), n?.(F(e)), this.store.events.parse();
1052
+ }
1053
+ }));
1054
+ }
1055
+ disable() {
1056
+ this.#e?.(), this.#t?.(), this.#n?.(), this.#e = void 0, this.#t = void 0, this.#n = void 0;
1057
+ }
1058
+ }, Ee = class {
1059
+ #e;
1060
+ #t;
1061
+ #n;
1062
+ constructor(e) {
1063
+ this.store = e;
1064
+ }
1065
+ enable() {
1066
+ if (this.#e) return;
1067
+ let e = this.store.refs.container;
1068
+ e && (this.#e = (e) => {
1069
+ this.store.nodes.focus.target = e.target;
1070
+ }, this.#t = () => {
1071
+ this.store.nodes.focus.target = void 0;
1072
+ }, this.#n = () => {
1073
+ let e = this.store.state.tokens.get();
1074
+ e.length === 1 && e[0].type === "text" && e[0].content === "" && (this.store.refs.container?.firstElementChild)?.focus();
1075
+ }, e.addEventListener("focusin", this.#e), e.addEventListener("focusout", this.#t), e.addEventListener("click", this.#n));
1076
+ }
1077
+ disable() {
1078
+ let e = this.store.refs.container;
1079
+ !e || !this.#e || (e.removeEventListener("focusin", this.#e), e.removeEventListener("focusout", this.#t), e.removeEventListener("click", this.#n), this.#e = void 0, this.#t = void 0, this.#n = void 0);
1080
+ }
1081
+ recover() {
1082
+ let e = this.store.state.recovery.get();
1083
+ if (!e) return;
1084
+ let { anchor: t, caret: n, isNext: r } = e, i = !t.target || !t.target.isConnected;
1085
+ switch (!0) {
1086
+ case r && i: {
1087
+ let t = this.store.refs.container, n = e.childIndex == null ? void 0 : t?.children[e.childIndex + 2];
1088
+ n ? n.focus() : this.store.nodes.focus.tail.focus();
1089
+ break;
1090
+ }
1091
+ case r:
1092
+ t.prev.focus();
1093
+ break;
1094
+ case i:
1095
+ this.store.nodes.focus.head.focus();
1096
+ break;
1097
+ default: t.next.focus();
1098
+ }
1099
+ this.store.nodes.focus.caret = n, this.store.state.recovery.set(void 0);
1100
+ }
1101
+ };
1102
+ //#endregion
1103
+ //#region ../../common/core/src/features/blocks/config.ts
1104
+ function De(e) {
1105
+ return typeof e == "object" && !!e.alwaysShowHandle;
1106
+ }
1107
+ //#endregion
1108
+ //#region ../../common/core/src/features/blocks/dragOperations.ts
1109
+ function H(e) {
1110
+ return e.tokens.length === 0 || e.tokens[0].type === "text";
1111
+ }
1112
+ function Oe(e, t) {
1113
+ return H(e) && H(t) ? "\n\n" : "";
1114
+ }
1115
+ function ke(e, t, n) {
1116
+ if (t.length === 0) return e + "\n\n";
1117
+ if (n >= t.length - 1) return e === "" ? "\n\n\n\n" : e + "\n\n";
1118
+ let r = t[n], i = t[n + 1];
1119
+ return i.startPos - r.endPos === 0 ? e.slice(0, r.endPos) + "\n\n\n\n" + e.slice(i.startPos) : e.slice(0, i.startPos) + "\n\n" + e.slice(i.startPos);
1120
+ }
1121
+ function Ae(e, t, n) {
1122
+ return t.length <= 1 ? "" : n >= t.length - 1 ? e.slice(0, t[n - 1].endPos) : e.slice(0, t[n].startPos) + e.slice(t[n + 1].startPos);
1123
+ }
1124
+ function je(e, t, n) {
1125
+ let r = t[n], i = e.substring(r.startPos, r.endPos);
1126
+ if (n >= t.length - 1) return e + (H(r), "\n\n") + i;
1127
+ let a = t[n + 1], o = a.startPos - r.endPos === 0 ? "" : "\n\n";
1128
+ return e.slice(0, a.startPos) + i + o + e.slice(a.startPos);
1129
+ }
1130
+ function Me(e, t) {
1131
+ return t <= 0 || t >= e.length ? 0 : e[t - 1].endPos;
1132
+ }
1133
+ function U(e, t, n) {
1134
+ if (n <= 0 || n >= t.length) return e;
1135
+ let r = t[n - 1], i = t[n];
1136
+ return e.slice(0, r.endPos) + e.slice(i.startPos);
1137
+ }
1138
+ function Ne(e, t, n, r) {
1139
+ if (n === r || n === r - 1 || t.length < 2 || n < 0 || n >= t.length || r < 0 || r > t.length) return e;
1140
+ let i = t.map((t) => e.substring(t.startPos, t.endPos)), a = [...t], [o] = a.splice(n, 1), [s] = i.splice(n, 1), c = r > n ? r - 1 : r;
1141
+ a.splice(c, 0, o), i.splice(c, 0, s);
1142
+ let l = [];
1143
+ for (let e = 0; e < i.length; e++) l.push(i[e]), e < i.length - 1 && l.push(Oe(a[e], a[e + 1]));
1144
+ return l.join("");
1145
+ }
1146
+ //#endregion
1147
+ //#region ../../common/core/src/features/blocks/splitTokensIntoDragRows.ts
1148
+ function Pe(e) {
1149
+ let t = [], { content: n, position: r } = e, i = r.start, a = [], o = () => {
1150
+ if (a.length > 0) {
1151
+ let e = a.join("");
1152
+ t.push({
1153
+ content: e,
1154
+ position: {
1155
+ start: i,
1156
+ end: i + e.length
1157
+ },
1158
+ isBlockSeparator: !1
1159
+ }), i += e.length, a.length = 0;
1160
+ }
1161
+ };
1162
+ for (let e = 0; e < n.length; e++) {
1163
+ let r = n[e];
1164
+ r === "\n" ? e + 1 < n.length && n[e + 1] === "\n" ? (o(), t.push({
1165
+ content: "\n\n",
1166
+ position: {
1167
+ start: i,
1168
+ end: i + 2
1169
+ },
1170
+ isBlockSeparator: !0
1171
+ }), i += 2, e++) : (a.push(r), i++) : r === "\r" ? e + 1 < n.length && n[e + 1] === "\n" ? e + 2 < n.length && n[e + 2] === "\r" && e + 3 < n.length && n[e + 3] === "\n" ? (o(), t.push({
1172
+ content: "\n\n",
1173
+ position: {
1174
+ start: i,
1175
+ end: i + 4
1176
+ },
1177
+ isBlockSeparator: !0
1178
+ }), i += 4, e += 3) : (a.push("\n"), i += 2, e++) : (a.push("\n"), i++) : a.push(r);
1179
+ }
1180
+ return o(), t;
1181
+ }
1182
+ var Fe = 0;
1183
+ function W(e) {
1184
+ return `drag-${Fe++}-${e}`;
1185
+ }
1186
+ function Ie() {
1187
+ Fe = 0;
1188
+ }
1189
+ function G(e) {
1190
+ if (e.length === 0) return [];
1191
+ Ie();
1192
+ let t = [], n = null;
1193
+ for (let r of e) if (r.type === "mark") n = null, t.push({
1194
+ id: W(r.position.start),
1195
+ tokens: [r],
1196
+ startPos: r.position.start,
1197
+ endPos: r.position.end
1198
+ });
1199
+ else if (r.type === "text") {
1200
+ let e = Pe(r);
1201
+ for (let r of e) r.isBlockSeparator ? (n !== null && t.push({
1202
+ id: W(n),
1203
+ tokens: [],
1204
+ startPos: n,
1205
+ endPos: n
1206
+ }), n = r.position.end) : r.content.trim().length > 0 && (n = null, t.push({
1207
+ id: W(r.position.start),
1208
+ tokens: [{
1209
+ type: "text",
1210
+ content: r.content,
1211
+ position: r.position
1212
+ }],
1213
+ startPos: r.position.start,
1214
+ endPos: r.position.end
1215
+ }));
1216
+ }
1217
+ return n !== null && t.push({
1218
+ id: W(n),
1219
+ tokens: [],
1220
+ startPos: n,
1221
+ endPos: n
1222
+ }), t;
1223
+ }
1224
+ //#endregion
1225
+ //#region ../../common/core/src/features/navigation/index.ts
1226
+ function Le(e, t) {
1227
+ let { focus: n } = e.nodes;
1228
+ if (n.isMark && !n.isEditable || n.isCaretAtBeginning) {
1229
+ let e = n.prev;
1230
+ for (; e.target && e.isMark && !e.isEditable;) e = e.prev;
1231
+ return e.target ? (t.preventDefault(), e.target.focus(), B.setCaretToEnd(e.target), !0) : !1;
1232
+ }
1233
+ return !1;
1234
+ }
1235
+ function Re(e, t) {
1236
+ let { focus: n } = e.nodes;
1237
+ if (n.isMark && !n.isEditable || n.isCaretAtEnd) {
1238
+ let e = n.next;
1239
+ for (; e.target && e.isMark && !e.isEditable;) e = e.next;
1240
+ return e.target ? (t.preventDefault(), e.target.focus(), B.trySetIndex(e.target, 0), !0) : !1;
1241
+ }
1242
+ return !1;
1243
+ }
1244
+ //#endregion
1245
+ //#region ../../common/core/src/features/selection/TextSelectionController.ts
1246
+ var ze = class {
1247
+ #e;
1248
+ #t;
1249
+ #n;
1250
+ #r;
1251
+ #i = null;
1252
+ #a = !1;
1253
+ constructor(e) {
1254
+ this.store = e;
1255
+ }
1256
+ enable() {
1257
+ this.#e || (this.#e = (e) => {
1258
+ this.#i = e.target, this.#a = !0;
1259
+ }, this.#t = (e) => {
1260
+ let t = this.#a, n = !this.store.refs.container?.contains(this.#i) || this.#i !== e.target, r = window.getSelection()?.containsNode(this.store.refs.container, !0);
1261
+ t && n && r && this.store.state.selecting.set("drag");
1262
+ }, this.#n = () => {
1263
+ this.#a = !1, this.#i = null, this.store.state.selecting.set(void 0);
1264
+ }, this.#r = () => {
1265
+ if (this.store.state.selecting.get() !== "drag") return;
1266
+ let e = [...this.store.refs.container.children], t = e.map((e) => e.contentEditable);
1267
+ e.forEach((e) => e.contentEditable = "false"), e.forEach((e, n) => e.contentEditable = t[n]);
1268
+ }, document.addEventListener("mousedown", this.#e), document.addEventListener("mousemove", this.#t), document.addEventListener("mouseup", this.#n), document.addEventListener("selectionchange", this.#r));
1269
+ }
1270
+ disable() {
1271
+ this.#e && (document.removeEventListener("mousedown", this.#e), document.removeEventListener("mousemove", this.#t), document.removeEventListener("mouseup", this.#n), document.removeEventListener("selectionchange", this.#r), this.#e = void 0, this.#t = void 0, this.#n = void 0, this.#r = void 0), this.#i = null, this.#a = !1;
1272
+ }
1467
1273
  };
1468
- class fe {
1469
- constructor(t) {
1470
- this.store = t;
1471
- }
1472
- #t = [];
1473
- #e = !1;
1474
- enable(t) {
1475
- const { store: e } = this, n = he(e);
1476
- n.enableAll(), this.#t.push(() => n.disableAll()), this.#n(), t?.getTrigger && this.#s(t.getTrigger);
1477
- }
1478
- disable() {
1479
- for (const t of this.#t)
1480
- t();
1481
- this.#t = [], this.#e = !1;
1482
- }
1483
- /**
1484
- * Synchronizes the parser with current options and handles parsing.
1485
- * Must be called by the framework layer when value or options change,
1486
- * since these are props set synchronously during render.
1487
- */
1488
- syncParser(t, e) {
1489
- const { store: n } = this, r = e?.map((i) => i.markup);
1490
- if (r && r.some(Boolean) ? n.state.parser.set(new F(r)) : n.state.parser.set(void 0), this.#e) {
1491
- n.state.recovery.get() || n.events.parse();
1492
- return;
1493
- }
1494
- const o = t ?? n.state.defaultValue.get() ?? "";
1495
- n.state.tokens.set(_(n, o)), this.#e = !0;
1496
- }
1497
- /**
1498
- * Recovers focus after tokens change.
1499
- * Must be called by the framework layer after DOM updates,
1500
- * since focus recovery requires the new DOM to be committed.
1501
- */
1502
- recoverFocus() {
1503
- this.store.controllers.contentEditable.sync(), this.store.state.Mark.get() && this.store.controllers.focus.recover();
1504
- }
1505
- #n() {
1506
- const { store: t } = this;
1507
- this.#t.push(
1508
- t.events.parse.on(() => {
1509
- if (t.state.recovery.get()) {
1510
- const e = N(t.state.tokens.get());
1511
- t.state.tokens.set(_(t, e)), t.state.previousValue.set(e);
1512
- return;
1513
- }
1514
- t.state.tokens.set(t.nodes.focus.target ? Gt(t) : Ut(t));
1515
- })
1516
- );
1517
- }
1518
- #s(t) {
1519
- const { store: e } = this;
1520
- e.controllers.overlay.enableTrigger(t, (n) => e.state.overlayMatch.set(n)), this.#t.push(() => e.controllers.overlay.disable()), this.#t.push(
1521
- e.state.overlayMatch.on((n) => {
1522
- n ? (e.nodes.input.target = e.nodes.focus.target, e.controllers.overlay.enableClose()) : e.controllers.overlay.disableClose();
1523
- })
1524
- );
1525
- }
1526
- }
1527
- function ge(s, t) {
1528
- const e = t.toLowerCase();
1529
- return s.filter((n) => n.toLowerCase().indexOf(e) > -1);
1530
- }
1531
- function pe(s, t, e) {
1532
- return {
1533
- type: "mark",
1534
- value: t,
1535
- meta: e,
1536
- content: "",
1537
- position: {
1538
- start: s.index,
1539
- end: s.index + s.span.length
1540
- },
1541
- descriptor: {
1542
- markup: s.option.markup,
1543
- index: 0,
1544
- segments: [],
1545
- gapTypes: [],
1546
- hasNested: !1,
1547
- hasTwoValues: !1,
1548
- segmentGlobalIndices: []
1549
- },
1550
- children: [],
1551
- nested: void 0
1552
- };
1553
- }
1554
- class ve {
1555
- constructor(t) {
1556
- this.store = t;
1557
- }
1558
- #t;
1559
- #e;
1560
- #n;
1561
- #s;
1562
- #r;
1563
- #o;
1564
- #i;
1565
- #a;
1566
- enableTrigger(t, e) {
1567
- if (this.#t) return;
1568
- this.#t = this.store.events.clearOverlay.on(() => {
1569
- e(void 0);
1570
- }), this.#e = this.store.events.checkOverlay.on(() => {
1571
- const r = tt.find(this.store.state.options.get(), t);
1572
- e(r);
1573
- }), this.#n = this.store.events.change.on(() => {
1574
- const r = this.store.state.showOverlayOn.get(), o = "change";
1575
- (r === o || Array.isArray(r) && r.includes(o)) && this.store.events.checkOverlay();
1576
- }), this.#s = () => {
1577
- const r = this.store.state.showOverlayOn.get(), o = "selectionChange";
1578
- (r === o || Array.isArray(r) && r.includes(o)) && this.store.events.checkOverlay();
1579
- }, this.#r = () => {
1580
- document.addEventListener("selectionchange", this.#s);
1581
- }, this.#o = () => {
1582
- document.removeEventListener("selectionchange", this.#s);
1583
- };
1584
- const n = this.store.refs.container;
1585
- n && (n.addEventListener("focusin", this.#r), n.addEventListener("focusout", this.#o));
1586
- }
1587
- enableClose() {
1588
- this.#i || (this.#i = (t) => {
1589
- t.key === v.ESC && this.store.events.clearOverlay();
1590
- }, this.#a = (t) => {
1591
- const e = t.target;
1592
- this.store.refs.overlay?.contains(e) || this.store.refs.container?.contains(e) || this.store.events.clearOverlay();
1593
- }, window.addEventListener("keydown", this.#i), document.addEventListener("click", this.#a, !0));
1594
- }
1595
- disableClose() {
1596
- this.#i && (window.removeEventListener("keydown", this.#i), document.removeEventListener("click", this.#a, !0), this.#i = void 0, this.#a = void 0);
1597
- }
1598
- disable() {
1599
- const t = this.store.refs.container;
1600
- t && this.#r && (t.removeEventListener("focusin", this.#r), t.removeEventListener("focusout", this.#o)), this.#s && document.removeEventListener("selectionchange", this.#s), this.disableClose(), this.#t?.(), this.#e?.(), this.#n?.(), this.#t = void 0, this.#e = void 0, this.#n = void 0, this.#s = void 0, this.#r = void 0, this.#o = void 0;
1601
- }
1602
- }
1603
- function me(s, t, e) {
1604
- if (e === 0) return { action: "none", index: t };
1605
- const n = !isNaN(t);
1606
- switch (s) {
1607
- case v.UP:
1608
- return { action: "up", index: n ? (e + (t - 1) % e) % e : 0 };
1609
- case v.DOWN:
1610
- return { action: "down", index: n ? (t + 1) % e : 0 };
1611
- case v.ENTER:
1612
- return n ? { action: "select", index: t } : { action: "none", index: t };
1613
- default:
1614
- return { action: "none", index: t };
1615
- }
1616
- }
1617
- class ye {
1618
- key = new Qt();
1619
- nodes = {
1620
- focus: new V(void 0, this),
1621
- input: new V(void 0, this)
1622
- };
1623
- state;
1624
- events = Jt();
1625
- refs = {
1626
- container: null,
1627
- overlay: null
1628
- };
1629
- controllers = {
1630
- overlay: new ve(this),
1631
- focus: new ne(this),
1632
- keydown: new ae(this),
1633
- system: new ee(this),
1634
- textSelection: new oe(this),
1635
- contentEditable: new te(this)
1636
- };
1637
- lifecycle = new fe(this);
1638
- constructor(t) {
1639
- this.state = Xt(
1640
- {
1641
- tokens: [],
1642
- parser: void 0,
1643
- previousValue: void 0,
1644
- recovery: void 0,
1645
- selecting: void 0,
1646
- overlayMatch: void 0,
1647
- value: void 0,
1648
- defaultValue: void 0,
1649
- onChange: void 0,
1650
- readOnly: !1,
1651
- options: void 0,
1652
- showOverlayOn: void 0,
1653
- Mark: void 0,
1654
- Overlay: void 0,
1655
- className: void 0,
1656
- style: void 0,
1657
- slots: void 0,
1658
- slotProps: void 0
1659
- },
1660
- t.createUseHook
1661
- );
1662
- }
1663
- createHandler() {
1664
- const t = this;
1665
- return {
1666
- get container() {
1667
- return t.refs.container;
1668
- },
1669
- get overlay() {
1670
- return t.refs.overlay;
1671
- },
1672
- focus() {
1673
- t.nodes.focus.head?.focus();
1674
- }
1675
- };
1676
- }
1677
- }
1678
- class xe {
1679
- ref;
1680
- #t;
1681
- #e;
1682
- #n;
1683
- constructor(t) {
1684
- this.ref = t.ref, this.#t = t.store, this.#e = t.token;
1685
- }
1686
- get readOnly() {
1687
- return this.#n;
1688
- }
1689
- set readOnly(t) {
1690
- this.#n = t;
1691
- }
1692
- get content() {
1693
- return this.#e.content;
1694
- }
1695
- set content(t) {
1696
- this.#e.content = t, this.#s();
1697
- }
1698
- get value() {
1699
- return this.#e.value;
1700
- }
1701
- set value(t) {
1702
- this.#e.value = t ?? "", this.#s();
1703
- }
1704
- get meta() {
1705
- return this.#e.meta;
1706
- }
1707
- set meta(t) {
1708
- this.#e.meta = t, this.#s();
1709
- }
1710
- get depth() {
1711
- return z(this.#t.state.tokens.get(), this.#e).depth;
1712
- }
1713
- get hasChildren() {
1714
- return this.#e.children.length > 0;
1715
- }
1716
- get parent() {
1717
- return z(this.#t.state.tokens.get(), this.#e)?.parent;
1718
- }
1719
- get tokens() {
1720
- return this.#e.children;
1721
- }
1722
- change = (t) => {
1723
- this.#e.content = t.content, this.#e.value = t.value ?? "", t.meta !== void 0 && (this.#e.meta = t.meta), this.#s();
1724
- };
1725
- remove = () => this.#t.events.delete({ token: this.#e });
1726
- #s() {
1727
- this.#t.events.change();
1728
- }
1729
- }
1730
- let ft = 0;
1731
- function ot(s) {
1732
- return `block-${ft++}-${s}`;
1733
- }
1734
- function ke() {
1735
- ft = 0;
1736
- }
1737
- function we(s) {
1738
- if (s.length === 0) return [];
1739
- ke();
1740
- const t = [];
1741
- let e = [], n = -1;
1742
- const r = (o) => {
1743
- e.length !== 0 && (t.push({
1744
- id: ot(n),
1745
- tokens: [...e],
1746
- startPos: n,
1747
- endPos: o
1748
- }), e = [], n = -1);
1749
- };
1750
- for (const o of s) {
1751
- if (o.type === "mark") {
1752
- o.content.endsWith(`
1753
- `) ? (r(o.position.start), t.push({
1754
- id: ot(o.position.start),
1755
- tokens: [o],
1756
- startPos: o.position.start,
1757
- endPos: o.position.end
1758
- })) : (n === -1 && (n = o.position.start), e.push(o));
1759
- continue;
1760
- }
1761
- if (o.type !== "text") continue;
1762
- const a = Se(o);
1763
- for (let c = 0; c < a.length; c++) {
1764
- const l = a[c];
1765
- if (l.isNewline) {
1766
- r(l.position.start);
1767
- continue;
1768
- }
1769
- l.content.length !== 0 && (n === -1 && (n = l.position.start), e.push({
1770
- type: "text",
1771
- content: l.content,
1772
- position: l.position
1773
- }));
1774
- }
1775
- }
1776
- if (e.length > 0) {
1777
- const o = e[e.length - 1];
1778
- r(o.position.end);
1779
- }
1780
- return t;
1781
- }
1782
- function Se(s) {
1783
- const t = [], { content: e, position: n } = s;
1784
- let r = n.start;
1785
- const o = [], i = () => {
1786
- if (o.length > 0) {
1787
- const a = o.join("");
1788
- t.push({
1789
- content: a,
1790
- position: { start: r, end: r + a.length },
1791
- isNewline: !1
1792
- }), r += a.length, o.length = 0;
1793
- }
1794
- };
1795
- for (let a = 0; a < e.length; a++) {
1796
- const c = e[a];
1797
- c === `
1798
- ` ? (i(), t.push({
1799
- content: `
1800
- `,
1801
- position: { start: r, end: r + 1 },
1802
- isNewline: !0
1803
- }), r += 1) : c === "\r" ? a + 1 < e.length && e[a + 1] === `
1804
- ` ? (i(), t.push({
1805
- content: `
1806
- `,
1807
- position: { start: r, end: r + 2 },
1808
- isNewline: !0
1809
- }), r += 2, a++) : (i(), t.push({
1810
- content: `
1811
- `,
1812
- position: { start: r, end: r + 1 },
1813
- isNewline: !0
1814
- }), r += 1) : o.push(c);
1815
- }
1816
- return i(), t;
1817
- }
1818
- function Ce(s, t, e, n) {
1819
- if (e === n || e === n - 1 || t.length < 2 || e < 0 || e >= t.length || n < 0 || n > t.length) return s;
1820
- const r = be(s, t), o = Te(r, e, n);
1821
- return Ee(o);
1822
- }
1823
- function be(s, t) {
1824
- return t.map((e, n) => {
1825
- const r = s.substring(e.startPos, e.endPos);
1826
- let o = "";
1827
- if (n < t.length - 1) {
1828
- const i = t[n + 1];
1829
- o = s.substring(e.endPos, i.startPos);
1830
- }
1831
- return {
1832
- index: n,
1833
- text: r,
1834
- separatorAfter: o
1835
- };
1836
- });
1837
- }
1838
- function Ee(s) {
1839
- const t = [];
1840
- for (let e = 0; e < s.length; e++) {
1841
- const n = s[e], r = e === s.length - 1;
1842
- let o = n.text;
1843
- o.endsWith(`
1844
- `) && (o = o.slice(0, -1)), t.push(o), r || t.push(n.separatorAfter || `
1845
- `);
1846
- }
1847
- return t.join("");
1848
- }
1849
- function Te(s, t, e) {
1850
- const n = [...s], [r] = n.splice(t, 1), o = e > t ? e - 1 : e;
1851
- return n.splice(o, 0, r), Ne(n, s), n;
1852
- }
1853
- function Ne(s, t) {
1854
- for (let e = 0; e < s.length - 1; e++) {
1855
- const n = s[e].index, r = s[e + 1].index;
1856
- if (Math.abs(n - r) === 1) {
1857
- const o = Math.min(n, r), i = t[o].separatorAfter;
1858
- s[e].separatorAfter = i.length > 0 ? i : `
1859
- `;
1860
- } else
1861
- s[e].separatorAfter = `
1862
- `;
1863
- }
1864
- }
1865
- const Ie = (s) => () => {
1866
- const [t, e] = M(() => s.get());
1867
- return P(() => s.on(e), [s]), t;
1274
+ //#endregion
1275
+ //#region ../../common/core/src/features/selection/index.ts
1276
+ function Be(e, t) {
1277
+ if ((t.ctrlKey || t.metaKey) && t.code === "KeyA") {
1278
+ if (e.state.drag.get()) return;
1279
+ t.preventDefault();
1280
+ let n = window.getSelection(), r = e.refs.container?.firstChild, i = e.refs.container?.lastChild;
1281
+ if (!n || !r || !i) return;
1282
+ n.setBaseAndExtent(r, 0, i, 1), e.state.selecting.set("all");
1283
+ }
1284
+ }
1285
+ //#endregion
1286
+ //#region ../../common/core/src/features/input/KeyDownController.ts
1287
+ var Ve = class {
1288
+ #e;
1289
+ #t;
1290
+ #n;
1291
+ constructor(e) {
1292
+ this.store = e;
1293
+ }
1294
+ enable() {
1295
+ if (this.#e) return;
1296
+ let e = this.store.refs.container;
1297
+ e && (this.#e = (e) => {
1298
+ e.key === x.LEFT ? Le(this.store, e) : e.key === x.RIGHT ? Re(this.store, e) : (e.key === x.UP || e.key === x.DOWN) && this.#a(e), this.#r(e), this.#i(e), Be(this.store, e);
1299
+ }, this.#t = (e) => {
1300
+ We(this.store, e);
1301
+ }, this.#n = (e) => {
1302
+ He(this.store, e);
1303
+ }, e.addEventListener("keydown", this.#e), e.addEventListener("paste", this.#t), e.addEventListener("beforeinput", this.#n, !0));
1304
+ }
1305
+ disable() {
1306
+ let e = this.store.refs.container;
1307
+ !e || !this.#e || (e.removeEventListener("keydown", this.#e), e.removeEventListener("paste", this.#t), e.removeEventListener("beforeinput", this.#n, !0), this.#e = void 0, this.#t = void 0, this.#n = void 0);
1308
+ }
1309
+ #r(e) {
1310
+ let { focus: t } = this.store.nodes, n = !!this.store.state.drag.get();
1311
+ if (!n && (e.key === x.DELETE || e.key === x.BACKSPACE)) {
1312
+ if (t.isMark) {
1313
+ if (t.isEditable && (e.key === x.BACKSPACE && !t.isCaretAtBeginning || e.key === x.DELETE && !t.isCaretAtEnd)) return;
1314
+ e.preventDefault(), V("self", this.store);
1315
+ return;
1316
+ }
1317
+ if (e.key === x.BACKSPACE && t.isSpan && t.isCaretAtBeginning && t.prev.target) {
1318
+ e.preventDefault(), V("prev", this.store);
1319
+ return;
1320
+ }
1321
+ if (e.key === x.DELETE && t.isSpan && t.isCaretAtEnd && t.next.target) {
1322
+ e.preventDefault(), V("next", this.store);
1323
+ return;
1324
+ }
1325
+ }
1326
+ if (!n) return;
1327
+ let r = this.store.refs.container;
1328
+ if (!r) return;
1329
+ let i = Array.from(r.children), a = i.findIndex((e) => e === document.activeElement || e.contains(document.activeElement));
1330
+ if (a === -1) return;
1331
+ let o = G(this.store.state.tokens.get());
1332
+ if (a >= o.length) return;
1333
+ let s = o[a], c = this.store.state.value.get() ?? this.store.state.previousValue.get() ?? "";
1334
+ if (this.store.state.onChange.get()) {
1335
+ if (e.key === x.BACKSPACE) {
1336
+ let t = i[a], n = B.getCaretIndex(t) === 0;
1337
+ if (s.tokens.map((e) => "content" in e ? e.content : "").join("") === "") {
1338
+ e.preventDefault();
1339
+ let t = o.length <= 1 ? "" : a >= o.length - 1 ? c.slice(0, o[a - 1].endPos) : c.slice(0, o[a].startPos) + c.slice(o[a + 1].startPos);
1340
+ this.store.applyValue(t), queueMicrotask(() => {
1341
+ let e = r.children[Math.max(0, a - 1)];
1342
+ e && (e.focus(), B.setCaretToEnd(e));
1343
+ });
1344
+ return;
1345
+ }
1346
+ if (n && a > 0) {
1347
+ let t = o[a - 1], n = o[a], s = n.startPos - t.endPos;
1348
+ if (H(t) && H(n) && s === 2) {
1349
+ e.preventDefault();
1350
+ let t = Me(o, a), n = U(c, o, a);
1351
+ this.store.applyValue(n), queueMicrotask(() => {
1352
+ let e = r.children[a - 1];
1353
+ if (e) {
1354
+ e.focus();
1355
+ let n = G(this.store.state.tokens.get())[a - 1];
1356
+ n && q(e, n, t);
1357
+ }
1358
+ });
1359
+ return;
1360
+ }
1361
+ e.preventDefault(), queueMicrotask(() => {
1362
+ let e = i[a - 1];
1363
+ e && (e.focus(), B.setCaretToEnd(e));
1364
+ });
1365
+ return;
1366
+ }
1367
+ }
1368
+ if (e.key === x.DELETE) {
1369
+ let t = i[a], n = B.getCaretIndex(t), s = n === t.textContent?.length;
1370
+ if (n === 0 && a > 0) {
1371
+ let t = o[a - 1], n = o[a], s = n.startPos - t.endPos;
1372
+ if (H(t) && H(n) && s === 2) {
1373
+ e.preventDefault();
1374
+ let t = Me(o, a), n = U(c, o, a);
1375
+ this.store.applyValue(n), queueMicrotask(() => {
1376
+ let e = r.children[a - 1];
1377
+ if (e) {
1378
+ e.focus();
1379
+ let n = G(this.store.state.tokens.get())[a - 1];
1380
+ n && q(e, n, t);
1381
+ }
1382
+ });
1383
+ return;
1384
+ }
1385
+ e.preventDefault(), queueMicrotask(() => {
1386
+ let e = i[a - 1];
1387
+ e && (e.focus(), B.setCaretToEnd(e));
1388
+ });
1389
+ return;
1390
+ }
1391
+ if (s && a < o.length - 1) {
1392
+ let t = o[a], n = o[a + 1], s = n.startPos - t.endPos;
1393
+ if (H(t) && H(n) && s === 2) {
1394
+ e.preventDefault();
1395
+ let n = t.endPos, i = U(c, o, a + 1);
1396
+ this.store.applyValue(i), queueMicrotask(() => {
1397
+ let e = r.children[a];
1398
+ if (e) {
1399
+ e.focus();
1400
+ let t = G(this.store.state.tokens.get())[a];
1401
+ t && q(e, t, n);
1402
+ }
1403
+ });
1404
+ return;
1405
+ }
1406
+ e.preventDefault(), queueMicrotask(() => {
1407
+ let e = i[a + 1];
1408
+ e && (e.focus(), B.trySetIndex(e, 0));
1409
+ });
1410
+ return;
1411
+ }
1412
+ }
1413
+ }
1414
+ }
1415
+ #i(e) {
1416
+ if (!this.store.state.drag.get() || e.key !== x.ENTER || e.shiftKey) return;
1417
+ let t = this.store.refs.container;
1418
+ if (!t) return;
1419
+ let n = document.activeElement;
1420
+ if (!n || !t.contains(n)) return;
1421
+ e.preventDefault();
1422
+ let r = t.children, i = -1;
1423
+ for (let e = 0; e < r.length; e++) if (r[e] === n || r[e].contains(n)) {
1424
+ i = e;
1425
+ break;
1426
+ }
1427
+ if (i === -1) return;
1428
+ let a = G(this.store.state.tokens.get()), o = a[i];
1429
+ if (!o) return;
1430
+ let s = r[i], c = this.store.state.value.get() ?? "";
1431
+ if (!this.store.state.onChange.get()) return;
1432
+ if (!H(o)) {
1433
+ let e = ke(c, a, i);
1434
+ this.store.applyValue(e), queueMicrotask(() => {
1435
+ let e = t.children, n = i + 1;
1436
+ if (n < e.length) {
1437
+ let t = e[n];
1438
+ t.focus(), B.trySetIndex(t, 0);
1439
+ }
1440
+ });
1441
+ return;
1442
+ }
1443
+ let l = K(s, o), u = l === o.startPos ? "\n\n\n\n" : "\n\n", d = c.slice(0, l) + u + c.slice(l);
1444
+ this.store.applyValue(d), queueMicrotask(() => {
1445
+ let e = t.children, n = i + 1;
1446
+ if (n < e.length) {
1447
+ let t = e[n];
1448
+ t.focus(), B.trySetIndex(t, 0);
1449
+ }
1450
+ });
1451
+ }
1452
+ #a(e) {
1453
+ if (!this.store.state.drag.get()) return;
1454
+ let t = this.store.refs.container;
1455
+ if (!t) return;
1456
+ let n = document.activeElement;
1457
+ if (!n || !t.contains(n)) return;
1458
+ let r = Array.from(t.children), i = r.findIndex((e) => e === n || e.contains(n));
1459
+ if (i === -1) return;
1460
+ let a = r[i];
1461
+ if (e.key === x.UP) {
1462
+ if (!B.isCaretOnFirstLine(a) || i === 0) return;
1463
+ e.preventDefault();
1464
+ let t = B.getCaretRect()?.left ?? a.getBoundingClientRect().left, n = r[i - 1];
1465
+ n.focus();
1466
+ let o = n.getBoundingClientRect();
1467
+ B.setAtX(n, t, o.bottom - 4);
1468
+ } else if (e.key === x.DOWN) {
1469
+ if (!B.isCaretOnLastLine(a) || i >= r.length - 1) return;
1470
+ e.preventDefault();
1471
+ let t = B.getCaretRect()?.left ?? a.getBoundingClientRect().left, n = r[i + 1];
1472
+ n.focus();
1473
+ let o = n.getBoundingClientRect();
1474
+ B.setAtX(n, t, o.top + 4);
1475
+ }
1476
+ }
1868
1477
  };
1869
- function Me(s, t) {
1870
- St(t, () => s.createHandler(), [s]), P(() => (s.lifecycle.enable({
1871
- getTrigger: (a) => a.overlay?.trigger
1872
- }), () => s.lifecycle.disable()), []);
1873
- const e = s.state.value.use(), n = s.state.Mark.use(), r = s.state.options.use(), o = n ? r : void 0, i = s.state.tokens.use();
1874
- P(() => {
1875
- s.lifecycle.syncParser(e, o);
1876
- }, [e, o]), P(() => {
1877
- s.lifecycle.recoverFocus();
1878
- }, [i]);
1879
- }
1880
- const et = at(void 0);
1881
- et.displayName = "StoreContext";
1882
- function E() {
1883
- const s = ct(et);
1884
- if (s === void 0)
1885
- throw new Error("Store not found. Make sure to wrap component in StoreContext.");
1886
- return s;
1887
- }
1888
- const X = {
1889
- position: "absolute",
1890
- left: -28,
1891
- top: 2,
1892
- width: 20,
1893
- height: 20,
1894
- display: "flex",
1895
- alignItems: "center",
1896
- justifyContent: "center",
1897
- cursor: "grab",
1898
- borderRadius: 4,
1899
- opacity: 0,
1900
- transition: "opacity 0.15s ease",
1901
- userSelect: "none",
1902
- color: "#9ca3af",
1903
- flexShrink: 0,
1904
- background: "none",
1905
- border: "none",
1906
- padding: 0,
1907
- margin: 0,
1908
- font: "inherit",
1909
- lineHeight: 1
1910
- }, gt = {
1911
- ...X,
1912
- opacity: 1
1913
- }, Pe = {
1914
- ...gt,
1915
- cursor: "grabbing"
1916
- }, Le = {
1917
- position: "relative",
1918
- paddingLeft: 4,
1919
- transition: "opacity 0.2s ease"
1920
- }, it = {
1921
- position: "absolute",
1922
- left: 0,
1923
- right: 0,
1924
- height: 2,
1925
- backgroundColor: "#3b82f6",
1926
- borderRadius: 1,
1927
- pointerEvents: "none",
1928
- zIndex: 10
1929
- }, pt = O(() => /* @__PURE__ */ J("svg", { width: "14", height: "14", viewBox: "0 0 16 16", fill: "currentColor", children: [
1930
- /* @__PURE__ */ h("circle", { cx: "5", cy: "3", r: "1.5" }),
1931
- /* @__PURE__ */ h("circle", { cx: "11", cy: "3", r: "1.5" }),
1932
- /* @__PURE__ */ h("circle", { cx: "5", cy: "8", r: "1.5" }),
1933
- /* @__PURE__ */ h("circle", { cx: "11", cy: "8", r: "1.5" }),
1934
- /* @__PURE__ */ h("circle", { cx: "5", cy: "13", r: "1.5" }),
1935
- /* @__PURE__ */ h("circle", { cx: "11", cy: "13", r: "1.5" })
1936
- ] }));
1937
- pt.displayName = "GripIcon";
1938
- const vt = O(({ blockIndex: s, children: t, readOnly: e, onReorder: n }) => {
1939
- const [r, o] = M(!1), [i, a] = M(!1), [c, l] = M(null), d = H(null), f = k(() => o(!0), []), u = k(() => o(!1), []), g = k(
1940
- (p) => {
1941
- p.dataTransfer.effectAllowed = "move", p.dataTransfer.setData("text/plain", String(s)), a(!0), d.current && p.dataTransfer.setDragImage(d.current, 0, 0);
1942
- },
1943
- [s]
1944
- ), m = k(() => {
1945
- a(!1), l(null);
1946
- }, []), C = k((p) => {
1947
- if (p.preventDefault(), p.dataTransfer.dropEffect = "move", !d.current) return;
1948
- const D = d.current.getBoundingClientRect(), G = D.top + D.height / 2;
1949
- l(p.clientY < G ? "before" : "after");
1950
- }, []), y = k((p) => {
1951
- p.currentTarget.contains(p.relatedTarget) || l(null);
1952
- }, []), b = k(
1953
- (p) => {
1954
- p.preventDefault();
1955
- const D = parseInt(p.dataTransfer.getData("text/plain"), 10);
1956
- if (isNaN(D)) return;
1957
- const G = c === "before" ? s : s + 1;
1958
- l(null), n(D, G);
1959
- },
1960
- [s, c, n]
1961
- ), I = {
1962
- ...Le,
1963
- opacity: i ? 0.4 : 1
1964
- }, L = e ? { ...X, display: "none" } : i ? Pe : r ? gt : X;
1965
- return /* @__PURE__ */ J(
1966
- "div",
1967
- {
1968
- ref: d,
1969
- style: I,
1970
- onMouseEnter: f,
1971
- onMouseLeave: u,
1972
- onDragOver: C,
1973
- onDragLeave: y,
1974
- onDrop: b,
1975
- children: [
1976
- c === "before" && /* @__PURE__ */ h("div", { style: { ...it, top: -1 } }),
1977
- /* @__PURE__ */ h(
1978
- "button",
1979
- {
1980
- type: "button",
1981
- draggable: !e,
1982
- onDragStart: g,
1983
- onDragEnd: m,
1984
- style: L,
1985
- "aria-label": "Drag to reorder",
1986
- children: /* @__PURE__ */ h(pt, {})
1987
- }
1988
- ),
1989
- t,
1990
- c === "after" && /* @__PURE__ */ h("div", { style: { ...it, bottom: -1 } })
1991
- ]
1992
- }
1993
- );
1478
+ function K(e, t) {
1479
+ let n = window.getSelection();
1480
+ if (!n?.rangeCount) return t.endPos;
1481
+ let { focusNode: r, focusOffset: i } = n;
1482
+ return r ? J(r, i, e, t) : t.endPos;
1483
+ }
1484
+ function He(e, t) {
1485
+ let n = e.state.selecting.get();
1486
+ if (n === "all" && Ge(e)) {
1487
+ if (t.inputType === "insertFromPaste") {
1488
+ t.preventDefault();
1489
+ return;
1490
+ }
1491
+ t.preventDefault(), Ke(e, t.inputType.startsWith("delete") ? "" : t.data ?? "");
1492
+ return;
1493
+ }
1494
+ if (n === "all" && e.state.selecting.set(void 0), e.state.drag.get()) {
1495
+ qe(e, t);
1496
+ return;
1497
+ }
1498
+ let { focus: r } = e.nodes;
1499
+ !r.target || !r.isEditable || Ue(r, t) && e.events.change();
1500
+ }
1501
+ function Ue(e, t) {
1502
+ let n = e.caret, r = e.content, i, a;
1503
+ switch (t.inputType) {
1504
+ case "insertText": {
1505
+ t.preventDefault();
1506
+ let e = t.data ?? "";
1507
+ i = r.slice(0, n) + e + r.slice(n), a = n + e.length;
1508
+ break;
1509
+ }
1510
+ case "deleteContentBackward":
1511
+ case "deleteContentForward":
1512
+ case "deleteWordBackward":
1513
+ case "deleteWordForward":
1514
+ case "deleteSoftLineBackward":
1515
+ case "deleteSoftLineForward": {
1516
+ let e = t.getTargetRanges();
1517
+ if (!e.length) return !1;
1518
+ let { startOffset: n, endOffset: o } = e[0];
1519
+ if (n === o) return !1;
1520
+ t.preventDefault(), i = r.slice(0, n) + r.slice(o), a = n;
1521
+ break;
1522
+ }
1523
+ case "insertFromPaste":
1524
+ case "insertReplacementText": {
1525
+ let e = t.dataTransfer?.getData("text/plain") ?? "", o = t.getTargetRanges(), s = o[0]?.startOffset ?? n, c = o[0]?.endOffset ?? n;
1526
+ t.preventDefault(), i = r.slice(0, s) + e + r.slice(c), a = s + e.length;
1527
+ break;
1528
+ }
1529
+ default: return !1;
1530
+ }
1531
+ return e.content = i, e.caret = a, !0;
1532
+ }
1533
+ function We(e, t) {
1534
+ let n = e.state.selecting.get();
1535
+ if (n !== "all" || !Ge(e)) {
1536
+ n === "all" && e.state.selecting.set(void 0);
1537
+ return;
1538
+ }
1539
+ t.preventDefault(), Ke(e, t.clipboardData?.getData("text/plain") ?? "");
1540
+ }
1541
+ function Ge(e) {
1542
+ let t = window.getSelection(), n = e.refs.container;
1543
+ if (!t?.rangeCount || !n?.firstChild || !n?.lastChild) return !1;
1544
+ try {
1545
+ let e = t.getRangeAt(0);
1546
+ return n.contains(e.startContainer) && n.contains(e.endContainer) && e.toString().length > 0;
1547
+ } catch {
1548
+ return !1;
1549
+ }
1550
+ }
1551
+ function Ke(e, t) {
1552
+ e.nodes.focus.target = null, e.state.selecting.set(void 0), e.state.onChange.get()?.(t), e.state.value.get() === void 0 && e.state.tokens.set(e.state.parser.get()?.parse(t) ?? [{
1553
+ type: "text",
1554
+ content: t,
1555
+ position: {
1556
+ start: 0,
1557
+ end: t.length
1558
+ }
1559
+ }]), queueMicrotask(() => {
1560
+ let n = e.refs.container?.firstChild;
1561
+ n && (e.state.recovery.set({
1562
+ anchor: e.nodes.focus,
1563
+ caret: t.length
1564
+ }), n.focus());
1565
+ });
1566
+ }
1567
+ function qe(e, t) {
1568
+ let n = e.refs.container;
1569
+ if (!n) return;
1570
+ let r = document.activeElement;
1571
+ if (!r || !n.contains(r)) return;
1572
+ let i = Array.from(n.children), a = i.findIndex((e) => e === r || e.contains(r));
1573
+ if (a === -1) return;
1574
+ let o = i[a], s = G(e.state.tokens.get());
1575
+ if (a >= s.length) return;
1576
+ let c = s[a], l = e.state.value.get() ?? e.state.previousValue.get() ?? "", u = (t) => {
1577
+ queueMicrotask(() => {
1578
+ let r = n.children[a];
1579
+ if (!r) return;
1580
+ r.focus();
1581
+ let i = G(e.state.tokens.get())[a];
1582
+ i && q(r, i, t);
1583
+ });
1584
+ };
1585
+ switch (t.inputType) {
1586
+ case "insertText": {
1587
+ t.preventDefault();
1588
+ let n = t.data ?? "", r = t.getTargetRanges(), i, a;
1589
+ if (r.length > 0) {
1590
+ let e = J(r[0].startContainer, r[0].startOffset, o, c), t = J(r[0].endContainer, r[0].endOffset, o, c);
1591
+ [i, a] = e <= t ? [e, t] : [t, e];
1592
+ } else i = a = K(o, c);
1593
+ e.applyValue(l.slice(0, i) + n + l.slice(a)), u(i + n.length);
1594
+ break;
1595
+ }
1596
+ case "insertFromPaste":
1597
+ case "insertReplacementText": {
1598
+ t.preventDefault();
1599
+ let n = t.dataTransfer?.getData("text/plain") ?? "", r = t.getTargetRanges(), i, a;
1600
+ if (r.length > 0) {
1601
+ let e = J(r[0].startContainer, r[0].startOffset, o, c), t = J(r[0].endContainer, r[0].endOffset, o, c);
1602
+ [i, a] = e <= t ? [e, t] : [t, e];
1603
+ } else i = a = K(o, c);
1604
+ e.applyValue(l.slice(0, i) + n + l.slice(a)), u(i + n.length);
1605
+ break;
1606
+ }
1607
+ case "deleteContentBackward":
1608
+ case "deleteContentForward":
1609
+ case "deleteWordBackward":
1610
+ case "deleteWordForward":
1611
+ case "deleteSoftLineBackward":
1612
+ case "deleteSoftLineForward": {
1613
+ let n = t.getTargetRanges();
1614
+ if (!n.length) return;
1615
+ let r = J(n[0].startContainer, n[0].startOffset, o, c), i = J(n[0].endContainer, n[0].endOffset, o, c), [a, s] = r <= i ? [r, i] : [i, r];
1616
+ if (a === s) return;
1617
+ t.preventDefault(), e.applyValue(l.slice(0, a) + l.slice(s)), u(a);
1618
+ break;
1619
+ }
1620
+ }
1621
+ }
1622
+ function Je(e, t, n) {
1623
+ let r = window.getSelection();
1624
+ if (!r) return !1;
1625
+ if (!t.children || t.children.length === 0) {
1626
+ let i = t.nested?.start ?? t.position.start, a = t.nested?.end ?? t.position.end, o = Math.max(0, Math.min(n - i, a - i)), s = document.createTreeWalker(e, 4).nextNode();
1627
+ if (!s) return !1;
1628
+ let c = document.createRange();
1629
+ return c.setStart(s, Math.min(o, s.length)), c.collapse(!0), r.removeAllRanges(), r.addRange(c), !0;
1630
+ }
1631
+ let i = 0;
1632
+ for (let a of Array.from(e.childNodes)) {
1633
+ if (i >= t.children.length) break;
1634
+ let e = t.children[i];
1635
+ if (a.nodeType === Node.TEXT_NODE && e.type === "text") {
1636
+ if (n >= e.position.start && n <= e.position.end) {
1637
+ let t = Math.min(n - e.position.start, a.length), i = document.createRange();
1638
+ return i.setStart(a, t), i.collapse(!0), r.removeAllRanges(), r.addRange(i), !0;
1639
+ }
1640
+ i++;
1641
+ } else if (a.nodeType === Node.ELEMENT_NODE && e.type === "mark") {
1642
+ let r = i + 1 < t.children.length ? t.children[i + 1] : null;
1643
+ if (!(n === e.position.end && r?.position.start === n) && n >= e.position.start && n <= e.position.end) return Je(a, e, n);
1644
+ i++;
1645
+ }
1646
+ }
1647
+ return !1;
1648
+ }
1649
+ function q(e, t, n) {
1650
+ let r = window.getSelection();
1651
+ if (!r) return;
1652
+ let i = Array.from(e.children);
1653
+ for (let e = 0; e < t.tokens.length; e++) {
1654
+ let a = t.tokens[e], o = i[e + 1];
1655
+ if (!o) continue;
1656
+ let s = t.tokens[e + 1];
1657
+ if (!(s && n === a.position.end && n === s.position.start) && n >= a.position.start && n <= a.position.end) {
1658
+ if (a.type === "text") {
1659
+ let e = n - a.position.start, t = document.createTreeWalker(o, 4).nextNode();
1660
+ if (t) {
1661
+ let n = Math.min(e, t.length), i = document.createRange();
1662
+ i.setStart(t, n), i.collapse(!0), r.removeAllRanges(), r.addRange(i);
1663
+ return;
1664
+ }
1665
+ }
1666
+ if (Je(o, a, n)) return;
1667
+ break;
1668
+ }
1669
+ }
1670
+ B.setCaretToEnd(e);
1671
+ }
1672
+ function J(e, t, n, r) {
1673
+ if (e === n) {
1674
+ let e = window.getSelection();
1675
+ return e?.focusNode && e.focusNode !== n ? J(e.focusNode, e.focusOffset, n, r) : r.endPos;
1676
+ }
1677
+ let i = e.nodeType === Node.ELEMENT_NODE ? e : e.parentElement;
1678
+ for (; i && i.parentElement !== n;) i = i.parentElement;
1679
+ if (!i) return r.endPos;
1680
+ let a = Array.from(n.children).indexOf(i);
1681
+ if (a < 0) return r.endPos;
1682
+ let o = a - 1;
1683
+ if (o < 0) return r.startPos;
1684
+ if (o >= r.tokens.length) return r.endPos;
1685
+ let s = r.tokens[o];
1686
+ return s.type === "text" ? s.position.start + Math.min(t, s.content.length) : Ye(e, t, i, s);
1687
+ }
1688
+ function Ye(e, t, n, r) {
1689
+ if (!r.children || r.children.length === 0) {
1690
+ if (t === 0) return r.position.start;
1691
+ let e = r.nested?.content.length ?? r.value.length;
1692
+ return e > 0 && t >= e ? r.content.endsWith("\n\n") && r.nested ? r.nested.end : r.position.end : (r.nested?.start ?? r.position.start) + Math.min(t, e);
1693
+ }
1694
+ let i = 0;
1695
+ for (let a of Array.from(n.childNodes)) {
1696
+ if (i >= r.children.length) break;
1697
+ let n = r.children[i];
1698
+ if (a.nodeType === Node.TEXT_NODE && n.type === "text") {
1699
+ if (e === a) return n.position.start + Math.min(t, n.content.length);
1700
+ i++;
1701
+ } else if (a.nodeType === Node.ELEMENT_NODE && n.type === "mark") {
1702
+ if (a === e || a.contains(e)) return Ye(e, t, a, n);
1703
+ i++;
1704
+ }
1705
+ }
1706
+ return r.nested?.end ?? r.position.end;
1707
+ }
1708
+ //#endregion
1709
+ //#region ../../common/core/src/features/FeatureManager.ts
1710
+ var Xe = class {
1711
+ #e = [];
1712
+ #t = !1;
1713
+ register(e) {
1714
+ return this.#e.push(e), this;
1715
+ }
1716
+ enableAll() {
1717
+ this.#t || (this.#t = !0, this.#e.forEach((e) => e.enable()));
1718
+ }
1719
+ disableAll() {
1720
+ this.#t && (this.#t = !1, this.#e.forEach((e) => e.disable()));
1721
+ }
1722
+ }, Y = (e, t) => ({
1723
+ name: e,
1724
+ enable: () => t.enable(),
1725
+ disable: () => t.disable()
1726
+ }), Ze = (e) => {
1727
+ let t = new Xe();
1728
+ return t.register(Y("keydown", e.controllers.keydown)).register(Y("system", e.controllers.system)).register(Y("focus", e.controllers.focus)).register(Y("textSelection", e.controllers.textSelection)).register(Y("contentEditable", e.controllers.contentEditable)), t;
1729
+ }, Qe = class {
1730
+ #e = [];
1731
+ #t = !1;
1732
+ constructor(e) {
1733
+ this.store = e;
1734
+ }
1735
+ enable(e) {
1736
+ let { store: t } = this, n = Ze(t);
1737
+ n.enableAll(), this.#e.push(() => n.disableAll()), this.#n(), e?.getTrigger && this.#r(e.getTrigger);
1738
+ }
1739
+ disable() {
1740
+ for (let e of this.#e) e();
1741
+ this.#e = [], this.#t = !1;
1742
+ }
1743
+ syncParser(e, t) {
1744
+ let { store: n } = this, r = t?.map((e) => e.markup);
1745
+ if (r && r.some(Boolean) ? n.state.parser.set(new se(r)) : n.state.parser.set(void 0), this.#t) {
1746
+ n.state.recovery.get() || n.events.parse();
1747
+ return;
1748
+ }
1749
+ let i = e ?? n.state.defaultValue.get() ?? "";
1750
+ n.state.tokens.set(R(n, i)), this.#t = !0;
1751
+ }
1752
+ recoverFocus() {
1753
+ this.store.controllers.contentEditable.sync(), this.store.state.Mark.get() && this.store.controllers.focus.recover();
1754
+ }
1755
+ #n() {
1756
+ let { store: e } = this;
1757
+ this.#e.push(e.events.parse.on(() => {
1758
+ if (e.state.recovery.get()) {
1759
+ let t = F(e.state.tokens.get());
1760
+ e.state.tokens.set(R(e, t)), e.state.previousValue.set(t);
1761
+ return;
1762
+ }
1763
+ e.state.tokens.set(e.nodes.focus.target ? de(e) : fe(e));
1764
+ }));
1765
+ }
1766
+ #r(e) {
1767
+ let { store: t } = this;
1768
+ t.controllers.overlay.enableTrigger(e, (e) => t.state.overlayMatch.set(e)), this.#e.push(() => t.controllers.overlay.disable()), this.#e.push(t.state.overlayMatch.on((e) => {
1769
+ e ? (t.nodes.input.target = t.nodes.focus.target, t.controllers.overlay.enableClose()) : t.controllers.overlay.disableClose();
1770
+ }));
1771
+ }
1772
+ };
1773
+ //#endregion
1774
+ //#region ../../common/core/src/features/overlay/filterSuggestions.ts
1775
+ function $e(e, t) {
1776
+ let n = t.toLowerCase();
1777
+ return e.filter((e) => e.toLowerCase().includes(n));
1778
+ }
1779
+ //#endregion
1780
+ //#region ../../common/core/src/features/overlay/createMarkFromOverlay.ts
1781
+ function et(e, t, n) {
1782
+ let r = e.option.markup;
1783
+ if (!r) throw Error("createMarkFromOverlay: option.markup is required");
1784
+ return {
1785
+ type: "mark",
1786
+ value: t,
1787
+ meta: n,
1788
+ content: "",
1789
+ position: {
1790
+ start: e.index,
1791
+ end: e.index + e.span.length
1792
+ },
1793
+ descriptor: {
1794
+ markup: r,
1795
+ index: 0,
1796
+ segments: [],
1797
+ gapTypes: [],
1798
+ hasNested: !1,
1799
+ hasTwoValues: !1,
1800
+ segmentGlobalIndices: []
1801
+ },
1802
+ children: [],
1803
+ nested: void 0
1804
+ };
1805
+ }
1806
+ //#endregion
1807
+ //#region ../../common/core/src/features/overlay/OverlayController.ts
1808
+ var tt = class {
1809
+ #e;
1810
+ #t;
1811
+ #n;
1812
+ #r;
1813
+ #i;
1814
+ #a;
1815
+ #o;
1816
+ #s;
1817
+ constructor(e) {
1818
+ this.store = e;
1819
+ }
1820
+ enableTrigger(e, t) {
1821
+ if (this.#e) return;
1822
+ this.#e = this.store.events.clearOverlay.on(() => {
1823
+ t(void 0);
1824
+ }), this.#t = this.store.events.checkOverlay.on(() => {
1825
+ t(xe.find(this.store.state.options.get(), e));
1826
+ }), this.#n = this.store.events.change.on(() => {
1827
+ let e = this.store.state.showOverlayOn.get(), t = "change";
1828
+ (e === t || Array.isArray(e) && e.includes(t)) && this.store.events.checkOverlay();
1829
+ }), this.#r = () => {
1830
+ let e = this.store.state.showOverlayOn.get(), t = "selectionChange";
1831
+ (e === t || Array.isArray(e) && e.includes(t)) && this.store.events.checkOverlay();
1832
+ }, this.#i = () => {
1833
+ document.addEventListener("selectionchange", this.#r);
1834
+ }, this.#a = () => {
1835
+ document.removeEventListener("selectionchange", this.#r);
1836
+ };
1837
+ let n = this.store.refs.container;
1838
+ n && (n.addEventListener("focusin", this.#i), n.addEventListener("focusout", this.#a));
1839
+ }
1840
+ enableClose() {
1841
+ this.#o || (this.#o = (e) => {
1842
+ e.key === x.ESC && this.store.events.clearOverlay();
1843
+ }, this.#s = (e) => {
1844
+ let t = e.target;
1845
+ this.store.refs.overlay?.contains(t) || this.store.refs.container?.contains(t) || this.store.events.clearOverlay();
1846
+ }, window.addEventListener("keydown", this.#o), document.addEventListener("click", this.#s, !0));
1847
+ }
1848
+ disableClose() {
1849
+ this.#o && (window.removeEventListener("keydown", this.#o), document.removeEventListener("click", this.#s, !0), this.#o = void 0, this.#s = void 0);
1850
+ }
1851
+ disable() {
1852
+ let e = this.store.refs.container;
1853
+ e && this.#i && (e.removeEventListener("focusin", this.#i), e.removeEventListener("focusout", this.#a)), this.#r && document.removeEventListener("selectionchange", this.#r), this.disableClose(), this.#e?.(), this.#t?.(), this.#n?.(), this.#e = void 0, this.#t = void 0, this.#n = void 0, this.#r = void 0, this.#i = void 0, this.#a = void 0;
1854
+ }
1855
+ };
1856
+ //#endregion
1857
+ //#region ../../common/core/src/features/overlay/suggestionNavigation.ts
1858
+ function nt(e, t, n) {
1859
+ if (n === 0) return {
1860
+ action: "none",
1861
+ index: t
1862
+ };
1863
+ let r = !isNaN(t);
1864
+ switch (e) {
1865
+ case x.UP: return {
1866
+ action: "up",
1867
+ index: r ? (n + (t - 1) % n) % n : 0
1868
+ };
1869
+ case x.DOWN: return {
1870
+ action: "down",
1871
+ index: r ? (t + 1) % n : 0
1872
+ };
1873
+ case x.ENTER: return r ? {
1874
+ action: "select",
1875
+ index: t
1876
+ } : {
1877
+ action: "none",
1878
+ index: t
1879
+ };
1880
+ default: return {
1881
+ action: "none",
1882
+ index: t
1883
+ };
1884
+ }
1885
+ }
1886
+ //#endregion
1887
+ //#region ../../common/core/src/features/store/Store.ts
1888
+ var rt = class {
1889
+ key = new ye();
1890
+ nodes = {
1891
+ focus: new Se(void 0, this),
1892
+ input: new Se(void 0, this)
1893
+ };
1894
+ state;
1895
+ events = ve();
1896
+ refs = {
1897
+ container: null,
1898
+ overlay: null
1899
+ };
1900
+ controllers = {
1901
+ overlay: new tt(this),
1902
+ focus: new Ee(this),
1903
+ keydown: new Ve(this),
1904
+ system: new Te(this),
1905
+ textSelection: new ze(this),
1906
+ contentEditable: new we(this)
1907
+ };
1908
+ lifecycle = new Qe(this);
1909
+ constructor(e) {
1910
+ this.state = ge({
1911
+ tokens: [],
1912
+ parser: void 0,
1913
+ previousValue: void 0,
1914
+ recovery: void 0,
1915
+ selecting: void 0,
1916
+ overlayMatch: void 0,
1917
+ value: void 0,
1918
+ defaultValue: void 0,
1919
+ onChange: void 0,
1920
+ readOnly: !1,
1921
+ options: void 0,
1922
+ showOverlayOn: void 0,
1923
+ Mark: void 0,
1924
+ Overlay: void 0,
1925
+ className: void 0,
1926
+ style: void 0,
1927
+ slots: void 0,
1928
+ slotProps: void 0,
1929
+ drag: !1
1930
+ }, e.createUseHook);
1931
+ }
1932
+ applyValue(e) {
1933
+ let t = this.state.onChange.get();
1934
+ if (!t) return;
1935
+ let n = R(this, e);
1936
+ this.state.tokens.set(n), this.state.previousValue.set(e), t(e);
1937
+ }
1938
+ createHandler() {
1939
+ let { refs: e, nodes: t } = this;
1940
+ return {
1941
+ get container() {
1942
+ return e.container;
1943
+ },
1944
+ get overlay() {
1945
+ return e.overlay;
1946
+ },
1947
+ focus() {
1948
+ t.focus.head?.focus();
1949
+ }
1950
+ };
1951
+ }
1952
+ }, it = class {
1953
+ ref;
1954
+ #e;
1955
+ #t;
1956
+ #n;
1957
+ constructor(e) {
1958
+ this.ref = e.ref, this.#e = e.store, this.#t = e.token;
1959
+ }
1960
+ get readOnly() {
1961
+ return this.#n;
1962
+ }
1963
+ set readOnly(e) {
1964
+ this.#n = e;
1965
+ }
1966
+ get content() {
1967
+ return this.#t.content;
1968
+ }
1969
+ set content(e) {
1970
+ this.#t.content = e, this.#r();
1971
+ }
1972
+ get value() {
1973
+ return this.#t.value;
1974
+ }
1975
+ set value(e) {
1976
+ this.#t.value = e ?? "", this.#r();
1977
+ }
1978
+ get meta() {
1979
+ return this.#t.meta;
1980
+ }
1981
+ set meta(e) {
1982
+ this.#t.meta = e, this.#r();
1983
+ }
1984
+ get depth() {
1985
+ return I(this.#e.state.tokens.get(), this.#t).depth;
1986
+ }
1987
+ get hasChildren() {
1988
+ return this.#t.children.length > 0;
1989
+ }
1990
+ get parent() {
1991
+ return I(this.#e.state.tokens.get(), this.#t)?.parent;
1992
+ }
1993
+ get tokens() {
1994
+ return this.#t.children;
1995
+ }
1996
+ change = (e) => {
1997
+ this.#t.content = e.content, this.#t.value = e.value ?? "", e.meta !== void 0 && (this.#t.meta = e.meta), this.#r();
1998
+ };
1999
+ remove = () => this.#e.events.delete({ token: this.#t });
2000
+ #r() {
2001
+ this.#e.events.change();
2002
+ }
2003
+ }, at = (e) => () => {
2004
+ let [t, n] = u(() => e.get());
2005
+ return a(() => e.on(n), [e]), t;
2006
+ };
2007
+ //#endregion
2008
+ //#region src/lib/hooks/useCoreFeatures.ts
2009
+ function ot(e, t) {
2010
+ o(t, () => e.createHandler(), [e]), a(() => (e.lifecycle.enable({ getTrigger: (e) => e.overlay?.trigger }), () => e.lifecycle.disable()), []);
2011
+ let n = e.state.value.use(), r = e.state.Mark.use(), i = e.state.options.use(), s = r ? i : void 0, c = e.state.tokens.use();
2012
+ a(() => {
2013
+ e.lifecycle.syncParser(n, s);
2014
+ }, [n, s]), a(() => {
2015
+ e.lifecycle.recoverFocus();
2016
+ }, [c]);
2017
+ }
2018
+ //#endregion
2019
+ //#region src/lib/providers/StoreContext.ts
2020
+ var st = t(void 0);
2021
+ st.displayName = "StoreContext";
2022
+ function X() {
2023
+ let e = i(st);
2024
+ if (e === void 0) throw Error("Store not found. Make sure to wrap component in StoreContext.");
2025
+ return e;
2026
+ }
2027
+ var Z = {
2028
+ Container: "_Container_1526r_1",
2029
+ Icon: "_Icon_1526r_5",
2030
+ IconGrip: "_IconGrip_1526r_16",
2031
+ IconAdd: "_IconAdd_1526r_20",
2032
+ IconDuplicate: "_IconDuplicate_1526r_21",
2033
+ IconTrash: "_IconTrash_1526r_22",
2034
+ Suggestions: "_Suggestions_1526r_43",
2035
+ suggestionActive: "_suggestionActive_1526r_71"
2036
+ }, ct = `${Z.Icon} ${Z.IconGrip}`, lt = n(({ blockIndex: t, children: n, readOnly: i, alwaysShowHandle: a, onReorder: o, onRequestMenu: s }) => {
2037
+ let [c, d] = u(!1), [m, h] = u(!1), [g, _] = u(null), v = l(null), y = l(null), b = r(() => d(!0), []), x = r(() => d(!1), []), S = r((e) => {
2038
+ e.dataTransfer.effectAllowed = "move", e.dataTransfer.setData("text/plain", String(t)), h(!0), v.current && e.dataTransfer.setDragImage(v.current, 0, 0);
2039
+ }, [t]), C = r(() => {
2040
+ h(!1), _(null);
2041
+ }, []), w = r((e) => {
2042
+ if (e.preventDefault(), e.dataTransfer.dropEffect = "move", !v.current) return;
2043
+ let t = v.current.getBoundingClientRect(), n = t.top + t.height / 2;
2044
+ _(e.clientY < n ? "before" : "after");
2045
+ }, []), T = r((e) => {
2046
+ e.currentTarget.contains(e.relatedTarget) || _(null);
2047
+ }, []), E = r((e) => {
2048
+ e.preventDefault();
2049
+ let n = parseInt(e.dataTransfer.getData("text/plain"), 10);
2050
+ if (isNaN(n)) return;
2051
+ let r = g === "before" ? t : t + 1;
2052
+ _(null), o(n, r);
2053
+ }, [
2054
+ t,
2055
+ g,
2056
+ o
2057
+ ]), D = r((e) => {
2058
+ e.preventDefault(), y.current && s?.(t, y.current.getBoundingClientRect());
2059
+ }, [t, s]), O = {
2060
+ position: "relative",
2061
+ marginLeft: 0,
2062
+ paddingLeft: i ? 0 : 32,
2063
+ transition: "opacity 0.2s ease",
2064
+ opacity: m ? .4 : 1,
2065
+ background: "transparent",
2066
+ borderRadius: 3,
2067
+ minHeight: "1.2em",
2068
+ userSelect: "none",
2069
+ outline: "none"
2070
+ }, k = {
2071
+ position: "absolute",
2072
+ left: 4,
2073
+ top: 0,
2074
+ bottom: 0,
2075
+ width: 24,
2076
+ display: "flex",
2077
+ alignItems: "center",
2078
+ opacity: a || c && !m ? 1 : 0,
2079
+ transition: a ? void 0 : "opacity 0.15s ease",
2080
+ pointerEvents: a || c ? "auto" : "none"
2081
+ }, A = {
2082
+ width: 24,
2083
+ height: 24,
2084
+ display: "flex",
2085
+ alignItems: "center",
2086
+ justifyContent: "center",
2087
+ cursor: "pointer",
2088
+ borderRadius: 4,
2089
+ color: "#9ca3af",
2090
+ background: "none",
2091
+ border: "none",
2092
+ padding: 0,
2093
+ margin: 0,
2094
+ font: "inherit",
2095
+ lineHeight: 1,
2096
+ flexShrink: 0,
2097
+ userSelect: "none"
2098
+ }, j = {
2099
+ position: "absolute",
2100
+ left: i ? 0 : 32,
2101
+ right: 0,
2102
+ height: 2,
2103
+ backgroundColor: "#3b82f6",
2104
+ borderRadius: 1,
2105
+ pointerEvents: "none",
2106
+ zIndex: 10
2107
+ };
2108
+ return /* @__PURE__ */ p("div", {
2109
+ ref: v,
2110
+ "data-testid": "block",
2111
+ style: O,
2112
+ onMouseEnter: b,
2113
+ onMouseLeave: x,
2114
+ onDragOver: w,
2115
+ onDragLeave: T,
2116
+ onDrop: E,
2117
+ children: [
2118
+ g === "before" && /* @__PURE__ */ f("div", { style: {
2119
+ ...j,
2120
+ top: -1
2121
+ } }),
2122
+ !i && /* @__PURE__ */ f("div", {
2123
+ style: k,
2124
+ children: /* @__PURE__ */ f("button", {
2125
+ ref: y,
2126
+ type: "button",
2127
+ draggable: !0,
2128
+ onDragStart: S,
2129
+ onDragEnd: C,
2130
+ onClick: D,
2131
+ style: {
2132
+ ...A,
2133
+ cursor: m ? "grabbing" : "grab"
2134
+ },
2135
+ "aria-label": "Drag to reorder or click for options",
2136
+ children: /* @__PURE__ */ f("span", { className: ct })
2137
+ })
2138
+ }),
2139
+ e.count(n) === 0 ? /* @__PURE__ */ f("br", {}) : n,
2140
+ g === "after" && /* @__PURE__ */ f("div", { style: {
2141
+ ...j,
2142
+ bottom: -1
2143
+ } })
2144
+ ]
2145
+ });
1994
2146
  });
1995
- vt.displayName = "DraggableBlock";
1996
- const $ = at(void 0);
1997
- $.displayName = "TokenProvider";
1998
- function nt() {
1999
- const s = ct($);
2000
- if (s === void 0)
2001
- throw new Error("Token not found. Make sure to wrap component in TokenContext.Provider.");
2002
- return s;
2003
- }
2004
- function mt(s, t, e, n) {
2005
- const r = E(), o = r.state.Mark.use(), i = r.state.Overlay.use(), a = s === "mark" ? o : i, c = s === "mark" ? t?.mark : t?.overlay, l = Nt(c, e ?? {}), d = l.slot || a || n;
2006
- if (!d)
2007
- throw new Error(
2008
- `No ${s} component found. Provide either option.${s}.slot, global ${s === "mark" ? "Mark" : "Overlay"}, or a defaultComponent.`
2009
- );
2010
- return [d, l];
2011
- }
2012
- function Ae() {
2013
- const s = nt(), t = E(), e = t.state.options.use(), n = t.key, r = e?.[s.descriptor.index], o = s.children.map((l) => /* @__PURE__ */ h(W, { mark: l, isNested: !0 }, n.get(l))), i = {
2014
- value: s.value,
2015
- meta: s.meta,
2016
- nested: s.nested?.content,
2017
- children: s.children.length > 0 ? o : void 0
2018
- }, [a, c] = mt("mark", r, i);
2019
- return /* @__PURE__ */ h(a, { ...c });
2020
- }
2021
- const Oe = () => {
2022
- const s = nt(), t = E(), e = H(null), n = t.state.slots.use(), r = t.state.slotProps.use(), o = S(() => Q("span", n), [n]), i = S(() => K("span", r), [r]);
2023
- if (s.type !== "text")
2024
- throw new Error("TextSpan component expects a TextToken");
2025
- return Ct(() => {
2026
- e.current && e.current.textContent !== s.content && (e.current.textContent = s.content);
2027
- }, [s.content]), /* @__PURE__ */ h(o, { ...i, ref: e });
2028
- }, W = O(({ mark: s, isNested: t = !1 }) => s.type === "mark" ? /* @__PURE__ */ h($, { value: s, children: /* @__PURE__ */ h(Ae, {}) }) : t ? /* @__PURE__ */ h(wt, { children: s.content }) : /* @__PURE__ */ h($, { value: s, children: /* @__PURE__ */ h(Oe, {}) }));
2029
- W.displayName = "Token";
2030
- const yt = O(() => {
2031
- const s = E(), t = s.state.tokens.use(), e = s.state.slots.use(), n = s.state.slotProps.use(), r = s.state.className.use(), o = s.state.style.use(), i = s.state.readOnly.use(), a = s.state.value.use(), c = s.state.onChange.use(), l = s.key, d = s.refs, f = S(() => Q("container", e), [e]), u = S(() => K("container", n), [n]), g = S(() => we(t), [t]), m = H(g);
2032
- m.current = g;
2033
- const C = k(
2034
- (y, b) => {
2035
- if (!a || !c) return;
2036
- const I = m.current, L = Ce(a, I, y, b);
2037
- if (L !== a) {
2038
- const p = _(s, L);
2039
- s.state.tokens.set(p), s.state.previousValue.set(L), c(L);
2040
- }
2041
- },
2042
- [s, a, c]
2043
- );
2044
- return /* @__PURE__ */ h(
2045
- f,
2046
- {
2047
- ref: (y) => d.container = y,
2048
- ...u,
2049
- className: r,
2050
- style: o,
2051
- children: g.map((y, b) => /* @__PURE__ */ h(vt, { blockIndex: b, readOnly: i, onReorder: C, children: y.tokens.map((I) => /* @__PURE__ */ h(W, { mark: I }, l.get(I))) }, y.id))
2052
- }
2053
- );
2147
+ lt.displayName = "DraggableBlock";
2148
+ //#endregion
2149
+ //#region src/lib/providers/TokenContext.ts
2150
+ var Q = t(void 0);
2151
+ Q.displayName = "TokenProvider";
2152
+ function ut() {
2153
+ let e = i(Q);
2154
+ if (e === void 0) throw Error("Token not found. Make sure to wrap component in TokenContext.Provider.");
2155
+ return e;
2156
+ }
2157
+ //#endregion
2158
+ //#region src/lib/hooks/useSlot.ts
2159
+ function dt(e, t, n, r) {
2160
+ let i = X(), a = i.state.Mark.use(), o = i.state.Overlay.use(), s = e === "mark" ? a : o, c = _(e === "mark" ? t?.mark : t?.overlay, n ?? {}), l = c.slot || s || r;
2161
+ if (!l) throw Error(`No ${e} component found. Provide either option.${e}.slot, global ${e === "mark" ? "Mark" : "Overlay"}, or a defaultComponent.`);
2162
+ return [l, c];
2163
+ }
2164
+ //#endregion
2165
+ //#region src/components/MarkRenderer.tsx
2166
+ function ft() {
2167
+ let e = ut(), t = X(), n = t.state.options.use(), r = t.key, i = n?.[e.descriptor.index], a = e.children.map((e) => /* @__PURE__ */ f($, {
2168
+ mark: e,
2169
+ isNested: !0
2170
+ }, r.get(e))), [o, s] = dt("mark", i, {
2171
+ value: e.value,
2172
+ meta: e.meta,
2173
+ nested: e.nested?.content,
2174
+ children: e.children.length > 0 ? a : void 0
2175
+ });
2176
+ return /* @__PURE__ */ f(o, { ...s });
2177
+ }
2178
+ //#endregion
2179
+ //#region src/components/TextSpan.tsx
2180
+ var pt = () => {
2181
+ let e = ut(), t = X(), n = l(null), r = t.state.slots.use(), i = t.state.slotProps.use(), a = c(() => y("span", r), [r]), o = c(() => b("span", i), [i]);
2182
+ if (e.type !== "text") throw Error("TextSpan component expects a TextToken");
2183
+ return s(() => {
2184
+ n.current && n.current.textContent !== e.content && (n.current.textContent = e.content);
2185
+ }, [e.content]), /* @__PURE__ */ f(a, {
2186
+ ...o,
2187
+ ref: n
2188
+ });
2189
+ }, $ = n(({ mark: e, isNested: t = !1 }) => e.type === "mark" ? /* @__PURE__ */ f(Q, {
2190
+ value: e,
2191
+ children: /* @__PURE__ */ f(ft, {})
2192
+ }) : t ? /* @__PURE__ */ f(d, { children: e.content }) : /* @__PURE__ */ f(Q, {
2193
+ value: e,
2194
+ children: /* @__PURE__ */ f(pt, {})
2195
+ }));
2196
+ $.displayName = "Token";
2197
+ //#endregion
2198
+ //#region src/components/BlockContainer.tsx
2199
+ var mt = {
2200
+ id: "block-empty",
2201
+ tokens: [],
2202
+ startPos: 0,
2203
+ endPos: 0
2204
+ }, ht = {
2205
+ height: 1,
2206
+ background: "rgba(55, 53, 47, 0.09)",
2207
+ margin: "4px 0"
2208
+ }, gt = n(({ position: e, onAdd: t, onDelete: n, onDuplicate: r, onClose: i }) => {
2209
+ let o = l(null), [s, c] = u(null), d = l(i);
2210
+ d.current = i, a(() => {
2211
+ let e = (e) => {
2212
+ o.current && !o.current.contains(e.target) && d.current();
2213
+ }, t = (e) => {
2214
+ e.key === "Escape" && d.current();
2215
+ };
2216
+ return document.addEventListener("mousedown", e), document.addEventListener("keydown", t), () => {
2217
+ document.removeEventListener("mousedown", e), document.removeEventListener("keydown", t);
2218
+ };
2219
+ }, []);
2220
+ let m = {
2221
+ position: "fixed",
2222
+ top: e.top,
2223
+ left: e.left,
2224
+ background: "white",
2225
+ border: "1px solid rgba(55, 53, 47, 0.16)",
2226
+ borderRadius: 6,
2227
+ boxShadow: "0 4px 16px rgba(15, 15, 15, 0.12)",
2228
+ padding: 4,
2229
+ zIndex: 9999,
2230
+ minWidth: 160,
2231
+ fontSize: 14
2232
+ }, g = (e) => ({
2233
+ display: "flex",
2234
+ alignItems: "center",
2235
+ gap: 8,
2236
+ padding: "6px 10px",
2237
+ borderRadius: 4,
2238
+ cursor: "pointer",
2239
+ color: e === "delete" ? "#eb5757" : "rgba(55, 53, 47, 0.85)",
2240
+ background: s === e ? e === "delete" ? "rgba(235, 87, 87, 0.06)" : "rgba(55, 53, 47, 0.06)" : "transparent",
2241
+ transition: "background 0.1s ease",
2242
+ userSelect: "none",
2243
+ lineHeight: 1
2244
+ });
2245
+ return /* @__PURE__ */ p("div", {
2246
+ ref: o,
2247
+ style: m,
2248
+ children: [
2249
+ /* @__PURE__ */ p("div", {
2250
+ style: g("add"),
2251
+ onMouseEnter: () => c("add"),
2252
+ onMouseLeave: () => c(null),
2253
+ onMouseDown: (e) => {
2254
+ e.preventDefault(), t(), i();
2255
+ },
2256
+ children: [/* @__PURE__ */ f("span", { className: h(Z.Icon, Z.IconAdd) }), /* @__PURE__ */ f("span", { children: "Add below" })]
2257
+ }),
2258
+ /* @__PURE__ */ p("div", {
2259
+ style: g("duplicate"),
2260
+ onMouseEnter: () => c("duplicate"),
2261
+ onMouseLeave: () => c(null),
2262
+ onMouseDown: (e) => {
2263
+ e.preventDefault(), r(), i();
2264
+ },
2265
+ children: [/* @__PURE__ */ f("span", { className: h(Z.Icon, Z.IconDuplicate) }), /* @__PURE__ */ f("span", { children: "Duplicate" })]
2266
+ }),
2267
+ /* @__PURE__ */ f("div", { style: ht }),
2268
+ /* @__PURE__ */ p("div", {
2269
+ style: g("delete"),
2270
+ onMouseEnter: () => c("delete"),
2271
+ onMouseLeave: () => c(null),
2272
+ onMouseDown: (e) => {
2273
+ e.preventDefault(), n(), i();
2274
+ },
2275
+ children: [/* @__PURE__ */ f("span", { className: h(Z.Icon, Z.IconTrash) }), /* @__PURE__ */ f("span", { children: "Delete" })]
2276
+ })
2277
+ ]
2278
+ });
2054
2279
  });
2055
- yt.displayName = "BlockContainer";
2056
- const xt = O(() => {
2057
- const s = E(), t = s.state.tokens.use(), e = s.state.slots.use(), n = s.state.slotProps.use(), r = s.state.className.use(), o = s.state.style.use(), i = s.key, a = s.refs, c = S(() => Q("container", e), [e]), l = S(() => K("container", n), [n]);
2058
- return /* @__PURE__ */ h(
2059
- c,
2060
- {
2061
- ref: (d) => a.container = d,
2062
- ...l,
2063
- className: r,
2064
- style: o,
2065
- children: t.map((d) => /* @__PURE__ */ h(W, { mark: d }, i.get(d)))
2066
- }
2067
- );
2280
+ gt.displayName = "BlockMenu";
2281
+ var _t = n(() => {
2282
+ let e = X(), t = e.state.tokens.use(), n = e.state.slots.use(), i = e.state.slotProps.use(), a = e.state.className.use(), o = e.state.style.use(), s = e.state.readOnly.use(), m = De(e.state.drag.use()), h = e.state.value.use(), g = e.state.onChange.use(), _ = e.key, v = e.refs, [x, S] = u(null), C = c(() => y("container", n), [n]), w = c(() => b("container", i), [i]), T = c(() => {
2283
+ let e = G(t);
2284
+ return e.length > 0 ? e : [mt];
2285
+ }, [t]), E = l(T);
2286
+ E.current = T;
2287
+ let D = r((t, n) => {
2288
+ if (h == null || !g) return;
2289
+ let r = Ne(h, E.current, t, n);
2290
+ r !== h && e.applyValue(r);
2291
+ }, [
2292
+ e,
2293
+ h,
2294
+ g
2295
+ ]), O = r((t) => {
2296
+ h == null || !g || (e.applyValue(ke(h, E.current, t)), queueMicrotask(() => {
2297
+ let n = e.refs.container;
2298
+ if (!n) return;
2299
+ let r = t + 1;
2300
+ n.children[r]?.focus();
2301
+ }));
2302
+ }, [
2303
+ e,
2304
+ h,
2305
+ g
2306
+ ]), k = r((t) => {
2307
+ h == null || !g || e.applyValue(Ae(h, E.current, t));
2308
+ }, [
2309
+ e,
2310
+ h,
2311
+ g
2312
+ ]), A = r((t) => {
2313
+ h == null || !g || e.applyValue(je(h, E.current, t));
2314
+ }, [
2315
+ e,
2316
+ h,
2317
+ g
2318
+ ]), j = r((e, t) => {
2319
+ S({
2320
+ index: e,
2321
+ position: {
2322
+ top: t.bottom + 4,
2323
+ left: t.left
2324
+ }
2325
+ });
2326
+ }, []), M = r(() => S(null), []);
2327
+ return /* @__PURE__ */ p(d, { children: [/* @__PURE__ */ f(C, {
2328
+ ref: (e) => v.container = e,
2329
+ ...w,
2330
+ className: a,
2331
+ style: o,
2332
+ children: T.map((e, t) => /* @__PURE__ */ f(lt, {
2333
+ blockIndex: t,
2334
+ readOnly: s,
2335
+ alwaysShowHandle: m,
2336
+ onReorder: D,
2337
+ onRequestMenu: j,
2338
+ children: e.tokens.map((e) => /* @__PURE__ */ f($, { mark: e }, _.get(e)))
2339
+ }, e.id))
2340
+ }), x && /* @__PURE__ */ f(gt, {
2341
+ position: x.position,
2342
+ onAdd: () => {
2343
+ O(x.index), M();
2344
+ },
2345
+ onDelete: () => {
2346
+ k(x.index), M();
2347
+ },
2348
+ onDuplicate: () => {
2349
+ A(x.index), M();
2350
+ },
2351
+ onClose: M
2352
+ })] });
2068
2353
  });
2069
- xt.displayName = "Container";
2070
- function De() {
2071
- const s = E(), t = s.state.overlayMatch.use(), e = w.getAbsolutePosition(), n = k(() => s.events.clearOverlay(), []), r = k(
2072
- (i) => {
2073
- const a = pe(t, i.value, i.meta);
2074
- s.events.select({ mark: a, match: t }), s.events.clearOverlay();
2075
- },
2076
- [t]
2077
- ), o = S(
2078
- () => ({
2079
- get current() {
2080
- return s.refs.overlay;
2081
- },
2082
- set current(i) {
2083
- s.refs.overlay = i;
2084
- }
2085
- }),
2086
- []
2087
- );
2088
- return { match: t, style: e, select: r, close: n, ref: o };
2089
- }
2090
- const Re = "_Container_1lmfr_1", _e = "_Suggestions_1lmfr_10", Be = "_suggestionActive_1lmfr_38", Z = {
2091
- Container: Re,
2092
- Suggestions: _e,
2093
- suggestionActive: Be
2094
- }, Ve = () => {
2095
- const s = E(), { match: t, select: e, style: n, ref: r } = De(), [o, i] = M(NaN), a = t.option.overlay?.data || [], c = S(() => ge(a, t.value), [t.value, a]), l = c.length;
2096
- return P(() => {
2097
- const d = s.refs.container;
2098
- if (!d) return;
2099
- const f = (u) => {
2100
- const g = me(u.key, o, l);
2101
- switch (g.action) {
2102
- case "up":
2103
- case "down":
2104
- u.preventDefault(), i(g.index);
2105
- break;
2106
- case "select":
2107
- u.preventDefault();
2108
- const m = c[g.index];
2109
- e({ value: m, meta: g.index.toString() });
2110
- break;
2111
- }
2112
- };
2113
- return d.addEventListener("keydown", f), () => d.removeEventListener("keydown", f);
2114
- }, [l, c, o]), c.length ? /* @__PURE__ */ h("ul", { ref: r, className: Z.Suggestions, style: n, children: c.map((d, f) => {
2115
- const u = f === o ? Z.suggestionActive : void 0;
2116
- return /* @__PURE__ */ h(
2117
- "li",
2118
- {
2119
- ref: (g) => {
2120
- u && g && g.scrollIntoView(!1);
2121
- },
2122
- className: u,
2123
- onClick: (g) => e({ value: d, meta: f.toString() }),
2124
- children: d
2125
- },
2126
- d
2127
- );
2128
- }) }) : null;
2129
- }, kt = O(() => {
2130
- const s = E(), t = s.state.overlayMatch.use(), e = S(() => t ? s.key.get(t.option) : void 0, [t]), [n, r] = mt("overlay", t?.option, void 0, Ve);
2131
- if (e) return /* @__PURE__ */ h(n, { ...r ?? {} }, e);
2354
+ _t.displayName = "BlockContainer";
2355
+ //#endregion
2356
+ //#region src/components/Container.tsx
2357
+ var vt = n(() => {
2358
+ let e = X(), t = e.state.tokens.use(), n = e.state.slots.use(), r = e.state.slotProps.use(), i = e.state.className.use(), a = e.state.style.use(), o = e.key, s = e.refs;
2359
+ return /* @__PURE__ */ f(c(() => y("container", n), [n]), {
2360
+ ref: (e) => s.container = e,
2361
+ ...c(() => b("container", r), [r]),
2362
+ className: i,
2363
+ style: a,
2364
+ children: t.map((e) => /* @__PURE__ */ f($, { mark: e }, o.get(e)))
2365
+ });
2132
2366
  });
2133
- kt.displayName = "OverlayRenderer";
2134
- function Ue(s) {
2135
- const {
2136
- ref: t,
2137
- value: e,
2138
- defaultValue: n,
2139
- onChange: r,
2140
- readOnly: o = !1,
2141
- block: i = !1,
2142
- Mark: a,
2143
- Overlay: c,
2144
- slots: l,
2145
- slotProps: d,
2146
- options: f = Lt,
2147
- showOverlayOn: u = "change",
2148
- className: g,
2149
- style: m
2150
- } = s, C = Et(Z.Container, g, d?.container?.className), y = Tt(m, d?.container?.style), [b] = M(() => new ye({ createUseHook: Ie }));
2151
- return b.state.set({
2152
- value: e,
2153
- defaultValue: n,
2154
- onChange: r,
2155
- readOnly: o,
2156
- options: f,
2157
- showOverlayOn: u,
2158
- Mark: a,
2159
- Overlay: c,
2160
- className: C,
2161
- style: y,
2162
- slots: l,
2163
- slotProps: d
2164
- }), Me(b, t), /* @__PURE__ */ J(et, { value: b, children: [
2165
- /* @__PURE__ */ h(i ? yt : xt, {}),
2166
- /* @__PURE__ */ h(kt, {})
2167
- ] });
2168
- }
2169
- const je = (s = {}) => {
2170
- const t = E(), e = nt(), n = H(null);
2171
- if (e.type !== "mark")
2172
- throw new Error("useMark can only be used with mark tokens");
2173
- const [r] = M(() => new xe({ ref: n, store: t, token: e }));
2174
- $e(n, s, e);
2175
- const o = t.state.readOnly.use();
2176
- return P(() => {
2177
- r.readOnly = o;
2178
- }, [o]), r;
2179
- };
2180
- function $e(s, t, e) {
2181
- P(() => {
2182
- s.current && !t.controlled && (s.current.textContent = e.content);
2183
- }, []);
2184
- }
2185
- export {
2186
- xe as MarkHandler,
2187
- Ue as MarkedInput,
2188
- q as annotate,
2189
- Ge as denote,
2190
- Ce as reorderBlocks,
2191
- we as splitTokensIntoBlocks,
2192
- je as useMark,
2193
- De as useOverlay
2367
+ vt.displayName = "Container";
2368
+ //#endregion
2369
+ //#region src/lib/hooks/useOverlay.tsx
2370
+ function yt() {
2371
+ let e = X(), t = e.state.overlayMatch.use(), n = B.getAbsolutePosition(), i = r(() => e.events.clearOverlay(), []);
2372
+ return {
2373
+ match: t,
2374
+ style: n,
2375
+ select: r((n) => {
2376
+ let r = et(t, n.value, n.meta);
2377
+ e.events.select({
2378
+ mark: r,
2379
+ match: t
2380
+ }), e.events.clearOverlay();
2381
+ }, [t]),
2382
+ close: i,
2383
+ ref: c(() => ({
2384
+ get current() {
2385
+ return e.refs.overlay;
2386
+ },
2387
+ set current(t) {
2388
+ e.refs.overlay = t;
2389
+ }
2390
+ }), [])
2391
+ };
2392
+ }
2393
+ //#endregion
2394
+ //#region src/components/Suggestions/Suggestions.tsx
2395
+ var bt = () => {
2396
+ let e = X(), { match: t, select: n, style: r, ref: i } = yt(), [o, s] = u(NaN), d = t.option.overlay?.data || [], p = c(() => $e(d, t.value), [t.value, d]), m = p.length, h = l(o);
2397
+ h.current = o;
2398
+ let g = l(p);
2399
+ return g.current = p, a(() => {
2400
+ let t = e.refs.container;
2401
+ if (!t) return;
2402
+ let r = (e) => {
2403
+ let t = nt(e.key, h.current, m);
2404
+ switch (t.action) {
2405
+ case "up":
2406
+ case "down":
2407
+ e.preventDefault(), s(t.index);
2408
+ break;
2409
+ case "select": {
2410
+ e.preventDefault();
2411
+ let r = g.current[t.index];
2412
+ n({
2413
+ value: r,
2414
+ meta: t.index.toString()
2415
+ });
2416
+ break;
2417
+ }
2418
+ }
2419
+ };
2420
+ return t.addEventListener("keydown", r), () => t.removeEventListener("keydown", r);
2421
+ }, [m, n]), p.length ? /* @__PURE__ */ f("ul", {
2422
+ ref: i,
2423
+ className: Z.Suggestions,
2424
+ style: r,
2425
+ children: p.map((e, t) => {
2426
+ let r = t === o ? Z.suggestionActive : void 0;
2427
+ return /* @__PURE__ */ f("li", {
2428
+ ref: (e) => {
2429
+ r && e && e.scrollIntoView(!1);
2430
+ },
2431
+ className: r,
2432
+ onClick: (r) => n({
2433
+ value: e,
2434
+ meta: t.toString()
2435
+ }),
2436
+ children: e
2437
+ }, e);
2438
+ })
2439
+ }) : null;
2440
+ }, xt = n(() => {
2441
+ let e = X(), t = e.state.overlayMatch.use(), n = c(() => t ? e.key.get(t.option) : void 0, [t]), [r, i] = dt("overlay", t?.option, void 0, bt);
2442
+ if (n) return /* @__PURE__ */ f(r, { ...i ?? {} }, n);
2443
+ });
2444
+ xt.displayName = "OverlayRenderer";
2445
+ //#endregion
2446
+ //#region src/components/MarkedInput.tsx
2447
+ function St(e) {
2448
+ let { ref: t, value: n, defaultValue: r, onChange: i, readOnly: a = !1, drag: o = !1, Mark: s, Overlay: c, slots: l, slotProps: d, options: m = S, showOverlayOn: _ = "change", className: v, style: y } = e, b = h(Z.Container, v, d?.container?.className), x = g(y, d?.container?.style), [C] = u(() => new rt({ createUseHook: at }));
2449
+ return C.state.set({
2450
+ value: n,
2451
+ defaultValue: r,
2452
+ onChange: i,
2453
+ readOnly: a,
2454
+ drag: o,
2455
+ options: m,
2456
+ showOverlayOn: _,
2457
+ Mark: s,
2458
+ Overlay: c,
2459
+ className: b,
2460
+ style: x,
2461
+ slots: l,
2462
+ slotProps: d
2463
+ }), ot(C, t), /* @__PURE__ */ p(st, {
2464
+ value: C,
2465
+ children: [/* @__PURE__ */ f(o ? _t : vt, {}), /* @__PURE__ */ f(xt, {})]
2466
+ });
2467
+ }
2468
+ //#endregion
2469
+ //#region src/lib/hooks/useMark.tsx
2470
+ var Ct = (e = {}) => {
2471
+ let t = X(), n = ut(), r = l(null);
2472
+ if (n.type !== "mark") throw Error("useMark can only be used with mark tokens");
2473
+ let [i] = u(() => new it({
2474
+ ref: r,
2475
+ store: t,
2476
+ token: n
2477
+ }));
2478
+ wt(r, e, n);
2479
+ let o = t.state.readOnly.use();
2480
+ return a(() => {
2481
+ i.readOnly = o;
2482
+ }, [o]), i;
2194
2483
  };
2484
+ function wt(e, t, n) {
2485
+ a(() => {
2486
+ e.current && !t.controlled && (e.current.textContent = n.content);
2487
+ }, []);
2488
+ }
2489
+ //#endregion
2490
+ export { it as MarkHandler, St as MarkedInput, P as annotate, ce as denote, Ct as useMark, yt as useOverlay };