@markput/react 0.4.0 → 0.6.0

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