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