@markput/react 0.1.0 → 0.3.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/README.md +13 -4
  2. package/index.d.ts +32 -84
  3. package/index.js +1455 -1073
  4. package/package.json +2 -2
package/index.js CHANGED
@@ -1,49 +1,38 @@
1
1
  import "./index.css";
2
- import { jsx as m, Fragment as oe, jsxs as ie } from "react/jsx-runtime";
3
- import ae, { createContext as ce, useContext as N, useEffect as x, useState as A, useRef as I, memo as W, useCallback as R, useImperativeHandle as le, useMemo as ue, forwardRef as de } from "react";
4
- function he(t) {
5
- return t ? Object.fromEntries(
6
- Object.entries(t).map(([e, n]) => 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()}`, n] : [e, n])
7
- ) : {};
2
+ import { jsx as f, Fragment as wt, jsxs as Z } from "react/jsx-runtime";
3
+ import { createContext as at, useContext as ct, useRef as H, useMemo as S, useLayoutEffect as St, memo as O, useState as P, useCallback as k, useEffect as M, useImperativeHandle as Ct } from "react";
4
+ function bt(s) {
5
+ if (s == null) throw new Error("Value must be a non nullable!");
8
6
  }
9
- const fe = {
10
- container: "div",
11
- span: "span"
12
- };
13
- function Z(t, e) {
14
- return e.props.slots?.[t] ? e.props.slots[t] : fe[t];
15
- }
16
- function J(t, e) {
17
- const n = e.props.slotProps?.[t];
18
- return n ? he(n) : void 0;
19
- }
20
- function ge(...t) {
21
- return t.filter(Boolean).join(" ") || void 0;
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
+ ) : {};
22
11
  }
23
- function pe(...t) {
24
- const e = t.reduce((n, s) => (s && Object.assign(n, s), n), {});
25
- return Object.keys(e).length > 0 ? e : void 0;
12
+ function Tt(...s) {
13
+ return s.filter(Boolean).join(" ") || void 0;
26
14
  }
27
- function Q(t) {
28
- if (t == null) throw new Error("Value must be a non nullable!");
15
+ function Nt(...s) {
16
+ const t = Object.assign({}, ...s.filter(Boolean));
17
+ return Object.keys(t).length > 0 ? t : void 0;
29
18
  }
30
- var k = /* @__PURE__ */ ((t) => (t.UP = "ArrowUp", t.DOWN = "ArrowDown", t.LEFT = "ArrowLeft", t.RIGHT = "ArrowRight", t.END = "End", t.HOME = "Home", t.PAGE_DOWN = "PageDown", t.PAGE_UP = "PageUp", t.ENTER = "Enter", t.TAB = "Tab", t.SPACE = " ", t.BACKSPACE = "Backspace", t.DELETE = "Delete", t.COMMA = ",", t.ESC = "Escape", t))(k || {});
31
- const me = "@", ve = "@[__value__](__meta__)", y = {
19
+ var x = /* @__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))(x || {});
20
+ const It = "@", Pt = "@[__value__](__meta__)", y = {
32
21
  Value: "__value__",
33
22
  Meta: "__meta__",
34
23
  Nested: "__nested__"
35
- }, w = {
24
+ }, N = {
36
25
  Value: "value",
37
26
  Meta: "meta",
38
27
  Nested: "nested"
39
28
  };
40
- function xe(t, e) {
41
- const { segments: n, gapTypes: s, counts: r, valueGapIndices: o } = ye(t);
42
- ke(r, t);
43
- const i = r.value === 2, { segments: a, gapTypes: c } = i ? Se(n, s, o) : { segments: n, gapTypes: s };
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 };
44
33
  return {
45
- markup: t,
46
- index: e,
34
+ markup: s,
35
+ index: t,
47
36
  segments: a,
48
37
  gapTypes: c,
49
38
  hasNested: r.nested === 1,
@@ -52,110 +41,110 @@ function xe(t, e) {
52
41
  // Will be populated by MarkupRegistry
53
42
  };
54
43
  }
55
- function ye(t) {
56
- const e = [], n = [], s = [], r = {
44
+ function Lt(s) {
45
+ const t = [], e = [], n = [], r = {
57
46
  value: 0,
58
47
  meta: 0,
59
48
  nested: 0
60
- }, o = [], i = [w.Value, w.Meta, w.Nested];
49
+ }, o = [], i = [N.Value, N.Meta, N.Nested];
61
50
  for (const l of i) {
62
- const h = j[l];
63
- let u = t.indexOf(h);
64
- for (; u !== -1; )
65
- o.push({ type: l, position: u }), u = t.indexOf(h, u + h.length);
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);
66
55
  }
67
- o.sort((l, h) => l.position - h.position);
56
+ o.sort((l, d) => l.position - d.position);
68
57
  let a = 0;
69
58
  for (const l of o) {
70
- const h = t.substring(a, l.position);
71
- h.length > 0 && e.push(h), n.push(l.type), r[l.type]++, l.type === w.Value && s.push(n.length - 1), a = l.position + j[l.type].length;
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;
72
61
  }
73
- const c = t.substring(a);
74
- return c.length > 0 && e.push(c), {
75
- segments: e,
76
- gapTypes: n,
62
+ const c = s.substring(a);
63
+ return c.length > 0 && t.push(c), {
64
+ segments: t,
65
+ gapTypes: e,
77
66
  counts: r,
78
- valueGapIndices: s
67
+ valueGapIndices: n
79
68
  };
80
69
  }
81
- function ke(t, e) {
82
- const n = [
83
- { count: t.value, max: 2, name: y.Value },
84
- { count: t.meta, max: 1, name: y.Meta },
85
- { count: t.nested, max: 1, name: y.Nested }
70
+ function At(s, t) {
71
+ const e = [
72
+ { count: s.value, max: 2, name: y.Value },
73
+ { count: s.meta, max: 1, name: y.Meta },
74
+ { count: s.nested, max: 1, name: y.Nested }
86
75
  ];
87
- for (const { count: s, max: r, name: o } of n)
88
- if (s > r)
89
- throw new Error(`Invalid markup: "${e}". Max ${r} "${o}" placeholders, got ${s}`);
90
- if (t.value === 0 && t.nested === 0)
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)
91
80
  throw new Error(
92
- `Invalid markup: "${e}". Need at least one "${y.Value}" or "${y.Nested}"`
81
+ `Invalid markup: "${t}". Need at least one "${y.Value}" or "${y.Nested}"`
93
82
  );
94
83
  }
95
- const j = {
96
- [w.Value]: y.Value,
97
- [w.Meta]: y.Meta,
98
- [w.Nested]: y.Nested
84
+ const nt = {
85
+ [N.Value]: y.Value,
86
+ [N.Meta]: y.Meta,
87
+ [N.Nested]: y.Nested
99
88
  };
100
- function Se(t, e, n) {
101
- if (n.length !== 2)
102
- return { segments: t, gapTypes: e };
103
- const [s, r] = n, o = [], i = t[s], a = t[s + 1];
104
- i && a && o.push(u(i, a, t[s + 2]));
105
- for (let d = s + 2; d < r; d++)
106
- o.push(t[d]);
107
- const c = t[r], l = t[r + 1];
108
- c && l && o.push(u(c, l, t[r + 2]));
109
- const h = e.filter((d) => d !== w.Value);
110
- return { segments: o, gapTypes: h };
111
- function u(d, g, E) {
112
- if (!E) return [d, g, ""];
113
- const L = E.charAt(0), re = L && !g.includes(L) && !E.startsWith(d) ? L : "";
114
- return [d, g, re];
115
- }
116
- }
117
- class Ce {
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, v) {
101
+ if (!v) return [u, h, ""];
102
+ const C = v.charAt(0), m = C && !h.includes(C) && !v.startsWith(u) ? C : "";
103
+ return [u, h, m];
104
+ }
105
+ }
106
+ class Dt {
118
107
  markups;
119
108
  descriptors;
120
109
  /** Deduplicated list of unique segment definitions (static strings or dynamic patterns) */
121
110
  segments = [];
122
111
  /** Map from first segment index to descriptors that start with this segment (for O(1) lookup) */
123
112
  firstSegmentIndexMap = /* @__PURE__ */ new Map();
124
- constructor(e) {
125
- this.markups = e;
126
- const n = /* @__PURE__ */ new Map();
127
- this.descriptors = e.map((s, r) => {
128
- if (s === void 0)
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)
129
118
  return null;
130
- const o = xe(s, r);
119
+ const o = Mt(n, r);
131
120
  return o.segments.forEach((i, a) => {
132
- this.processSegment(o, i, a, n);
121
+ this.processSegment(o, i, a, e);
133
122
  }), this.addToFirstSegmentIndexMap(o), o;
134
- }).filter((s) => s !== null);
123
+ }).filter((n) => n !== null);
135
124
  }
136
125
  /**
137
126
  * Adds a descriptor to the firstSegmentIndexMap using its first segment's global index
138
127
  */
139
- addToFirstSegmentIndexMap(e) {
140
- const n = e.segmentGlobalIndices[0];
141
- if (n === void 0) return;
142
- const s = this.firstSegmentIndexMap.get(n);
143
- s ? s.push(e) : this.firstSegmentIndexMap.set(n, [e]);
144
- }
145
- processSegment(e, n, s, r) {
146
- const o = this.getSegmentKey(n);
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);
147
136
  if (!o) return;
148
- this.registerSegment(n, o, r);
137
+ this.registerSegment(e, o, r);
149
138
  const i = r.get(o);
150
- if (e.segmentGlobalIndices[s] = i, typeof n != "string") {
151
- const [a, c] = n;
139
+ if (t.segmentGlobalIndices[n] = i, typeof e != "string") {
140
+ const [a, c] = e;
152
141
  a && this.registerSegment(a, a, r), c && this.registerSegment(c, c, r);
153
142
  }
154
143
  }
155
- registerSegment(e, n, s) {
156
- if (!s.has(n)) {
144
+ registerSegment(t, e, n) {
145
+ if (!n.has(e)) {
157
146
  const r = this.segments.length;
158
- this.segments.push(e), s.set(n, r);
147
+ this.segments.push(t), n.set(e, r);
159
148
  }
160
149
  }
161
150
  /**
@@ -164,27 +153,27 @@ class Ce {
164
153
  * For dynamic segments (arrays), returns before|after|exclusions if before or after is non-empty
165
154
  * Returns empty string for segments that should be ignored
166
155
  */
167
- getSegmentKey(e) {
168
- if (typeof e == "string")
169
- return e;
170
- const [n, s, r] = e;
171
- return n || s ? `${n}|${s}|${r}` : "";
156
+ getSegmentKey(t) {
157
+ if (typeof t == "string")
158
+ return t;
159
+ const [e, n, r] = t;
160
+ return e || n ? `${e}|${n}|${r}` : "";
172
161
  }
173
162
  }
174
- function K(t, e) {
175
- if (!e)
176
- return t;
177
- let n = 5381;
178
- for (let s = 0; s < e.length; s++)
179
- n = n * 33 ^ e.charCodeAt(s);
180
- return n = n >>> 0, t * 1e6 + (n & 1048575);
181
- }
182
- class we {
183
- constructor(e, n) {
184
- if (this.descriptor = e, this.expectedSegmentIndex = 1, this.start = n.start, this.end = n.end, e.segments.length === 1 && (this.expectedSegmentIndex = NaN, this.gaps.value = { start: this.start, end: this.end }), e.hasTwoValues && n.captured) {
185
- this.captured = n.captured;
186
- const s = n.start + n.value.indexOf(n.captured), r = s + n.captured.length;
187
- this.gaps.value = { start: s, end: r };
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 _t {
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 };
188
177
  }
189
178
  }
190
179
  gaps = {};
@@ -223,44 +212,44 @@ class we {
223
212
  get nextSegment() {
224
213
  if (this.isCompleted || this.isInvalid)
225
214
  return;
226
- const e = this.descriptor.segmentGlobalIndices[this.expectedSegmentIndex], n = this.descriptor.segments[this.expectedSegmentIndex];
227
- if (typeof n == "object" && this.descriptor.hasTwoValues && this.captured && this.isAwaitingLastSegment) {
228
- const [s, r] = n, o = s + this.captured + r;
229
- return K(e, o);
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);
230
219
  }
231
- return e;
220
+ return t;
232
221
  }
233
222
  /**
234
223
  * Update state with new segment by setting gap positions
235
224
  */
236
- processNext(e) {
237
- const n = this.end, s = e.start, r = this.descriptor.gapTypes[this.expectedSegmentIndex - 1];
238
- if (s < n) {
225
+ processNext(t) {
226
+ const e = this.end, n = t.start, r = this.descriptor.gapTypes[this.expectedSegmentIndex - 1];
227
+ if (n < e) {
239
228
  this.expectedSegmentIndex = -1;
240
229
  return;
241
230
  }
242
- this.gaps[r] = { start: n, end: s }, this.end = e.end, this.expectedSegmentIndex++, this.expectedSegmentIndex >= this.descriptor.segments.length && (this.expectedSegmentIndex = NaN);
231
+ this.gaps[r] = { start: e, end: n }, this.end = t.end, this.expectedSegmentIndex++, this.expectedSegmentIndex >= this.descriptor.segments.length && (this.expectedSegmentIndex = NaN);
243
232
  }
244
233
  /**
245
234
  * Checks if this match conflicts with another match (overlaps and cannot nest properly)
246
235
  */
247
- conflictsWith(e) {
248
- 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);
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);
249
238
  }
250
239
  }
251
- class Te {
252
- constructor(e) {
253
- this.registry = e;
240
+ class Rt {
241
+ constructor(t) {
242
+ this.registry = t;
254
243
  }
255
244
  pendingStates = /* @__PURE__ */ new Map();
256
245
  completingStates = /* @__PURE__ */ new Map();
257
246
  completedStates = [];
258
247
  /** Main method that converts found segments into structured matches */
259
- process(e) {
248
+ process(t) {
260
249
  this.pendingStates.clear(), this.completingStates.clear(), this.completedStates.length = 0;
261
- for (const n of e)
262
- this.processWaitingStates(n), this.tryStartNewStates(n);
263
- return this.completedStates.map((n) => n.match);
250
+ for (const e of t)
251
+ this.processWaitingStates(e), this.tryStartNewStates(e);
252
+ return this.completedStates.map((e) => e.match);
264
253
  }
265
254
  /**
266
255
  * Process states waiting for this segment
@@ -268,19 +257,19 @@ class Te {
268
257
  * Process completing states first (higher priority), then pending states
269
258
  * Process only one state per call
270
259
  */
271
- processWaitingStates(e) {
272
- const n = this.dequeueWaitingMatch(e);
273
- if (n && (n.processNext(e), !n.isInvalid)) {
274
- if (n.isCompleted) return this.addToCompleted(n);
275
- this.addToWaiting(n);
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);
276
265
  }
277
266
  }
278
- tryStartNewStates(e) {
279
- this.registry.firstSegmentIndexMap.get(e.index)?.forEach((n) => {
280
- const s = new we(n, e);
281
- if (!s.isInvalid) {
282
- if (s.isCompleted) return this.addToCompleted(s);
283
- this.addToWaiting(s);
267
+ tryStartNewStates(t) {
268
+ this.registry.firstSegmentIndexMap.get(t.index)?.forEach((e) => {
269
+ const n = new _t(e, t);
270
+ if (!n.isInvalid) {
271
+ if (n.isCompleted) return this.addToCompleted(n);
272
+ this.addToWaiting(n);
284
273
  }
285
274
  });
286
275
  }
@@ -288,23 +277,23 @@ class Te {
288
277
  * Gets the next waiting match for the given segment
289
278
  * Uses value-specific index for dynamic segments, base index for static segments
290
279
  */
291
- dequeueWaitingMatch(e) {
292
- const n = e.captured ? K(e.index, e.value) : e.index, s = this.completingStates.get(n);
293
- if (s?.length) return s.pop();
294
- const r = this.pendingStates.get(n);
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);
295
284
  if (r?.length) return r.pop();
296
285
  }
297
286
  /**
298
287
  * Adds a state to the waiting list for the next expected segment
299
288
  */
300
- addToWaiting(e) {
301
- const n = e.nextSegment;
302
- if (e.isAwaitingLastSegment) {
303
- const s = this.completingStates.get(n) || [];
304
- s.length === 0 && this.completingStates.set(n, s), s.push(e);
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);
305
294
  } else {
306
- const s = this.pendingStates.get(n) || [];
307
- s.length === 0 && this.pendingStates.set(n, s), s.push(e);
295
+ const n = this.pendingStates.get(e) || [];
296
+ n.length === 0 && this.pendingStates.set(e, n), n.push(t);
308
297
  }
309
298
  }
310
299
  /**
@@ -312,49 +301,49 @@ class Te {
312
301
  * Uses binary search to find insertion point and maintains sorted order
313
302
  * Relies on processing order to determine which match to keep
314
303
  */
315
- addToCompleted(e) {
316
- const n = e.start;
317
- let s = 0, r = this.completedStates.length;
318
- for (; s < r; ) {
319
- const o = Math.floor((s + r) / 2);
320
- this.completedStates[o].position < n ? s = o + 1 : r = o;
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;
321
310
  }
322
- s < this.completedStates.length && this.completedStates[s].position === n ? this.completedStates[s].match = e : this.completedStates.splice(s, 0, { position: n, match: e });
311
+ n < this.completedStates.length && this.completedStates[n].position === e ? this.completedStates[n].match = t : this.completedStates.splice(n, 0, { position: e, match: t });
323
312
  }
324
313
  }
325
- const b = (t) => t.replace(/[.*+?^${}()|[\]\\\\]/g, "\\$&");
326
- function Ee(t, e, n) {
327
- const s = b(t), r = b(e), o = b(e + n);
328
- return `${s}([^${o}]+?)${r}`;
314
+ const A = (s) => s.replace(/[.*+?^${}()|[\]\\\\]/g, "\\$&");
315
+ function Vt(s, t, e) {
316
+ const n = A(s), r = A(t), o = A(t + e);
317
+ return `${n}([^${o}]+?)${r}`;
329
318
  }
330
- class be {
319
+ class $t {
331
320
  staticRegex;
332
321
  staticToIndex;
333
322
  dynamicRegex;
334
323
  dynamicEntries;
335
324
  dynamicIndices;
336
- constructor(e) {
337
- this.initializeDual(e);
338
- }
339
- initializeDual(e) {
340
- const n = [], s = [], r = /* @__PURE__ */ new Map();
341
- if (e.forEach((o, i) => {
342
- typeof o == "string" ? (n.push(o), r.set(o, i)) : s.push(o);
343
- }), n.length > 0) {
344
- const i = [...n].sort((a, c) => c.length - a.length).map(b);
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);
345
334
  this.staticRegex = new RegExp(`(?:${i.join("|")})`, "gu"), this.staticToIndex = r;
346
335
  }
347
- if (s.length > 0) {
336
+ if (n.length > 0) {
348
337
  const o = /* @__PURE__ */ new Set(), i = [];
349
- s.forEach((a) => {
350
- const c = e.indexOf(a);
338
+ n.forEach((a) => {
339
+ const c = t.indexOf(a);
351
340
  if (typeof a == "string")
352
- i.push({ index: c, pattern: b(a), definition: a });
341
+ i.push({ index: c, pattern: A(a), definition: a });
353
342
  else {
354
- const [l, h, u] = a;
343
+ const [l, d, g] = a;
355
344
  o.add(c);
356
- const g = Ee(l, h, u).replace("(", `(?<content${c}>`);
357
- i.push({ index: c, pattern: g, definition: a });
345
+ const h = Vt(l, d, g).replace("(", `(?<content${c}>`);
346
+ i.push({ index: c, pattern: h, definition: a });
358
347
  }
359
348
  }), i.sort((a, c) => {
360
349
  const l = typeof a.definition == "string" ? a.definition.length : a.pattern.length;
@@ -362,12 +351,12 @@ class be {
362
351
  }), this.dynamicEntries = i, this.dynamicIndices = o, this.dynamicRegex = new RegExp(i.map((a, c) => `(?<seg${c}>${a.pattern})`).join("|"), "gu");
363
352
  }
364
353
  }
365
- search(e) {
366
- const n = [], s = [];
354
+ search(t) {
355
+ const e = [], n = [];
367
356
  if (this.staticRegex && this.staticToIndex)
368
- for (const o of e.matchAll(this.staticRegex)) {
357
+ for (const o of t.matchAll(this.staticRegex)) {
369
358
  const i = this.staticToIndex.get(o[0]);
370
- i !== void 0 && n.push({
359
+ i !== void 0 && e.push({
371
360
  index: i,
372
361
  start: o.index,
373
362
  end: o.index + o[0].length,
@@ -375,17 +364,17 @@ class be {
375
364
  });
376
365
  }
377
366
  if (this.dynamicRegex && this.dynamicEntries && this.dynamicIndices)
378
- for (const o of e.matchAll(this.dynamicRegex)) {
367
+ for (const o of t.matchAll(this.dynamicRegex)) {
379
368
  const i = o[0], a = o.index;
380
369
  let c, l;
381
370
  if (o.groups) {
382
- for (let h = 0; h < this.dynamicEntries.length; h++)
383
- if (o.groups[`seg${h}`] !== void 0) {
384
- c = this.dynamicEntries[h].index, this.dynamicIndices.has(c) && (l = o.groups[`content${c}`]);
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}`]);
385
374
  break;
386
375
  }
387
376
  }
388
- c !== void 0 && s.push({
377
+ c !== void 0 && n.push({
389
378
  index: c,
390
379
  start: a,
391
380
  end: a + i.length,
@@ -393,20 +382,20 @@ class be {
393
382
  captured: l
394
383
  });
395
384
  }
396
- const r = [...s];
397
- for (const o of n)
398
- s.some(
385
+ const r = [...n];
386
+ for (const o of e)
387
+ n.some(
399
388
  (a) => o.start < a.end && o.end > a.start
400
389
  ) || r.push(o);
401
390
  return r.sort((o, i) => o.start - i.start), r;
402
391
  }
403
392
  }
404
- const Y = (t, e = 0, n = t.length) => ({
393
+ const dt = (s, t = 0, e = s.length) => ({
405
394
  type: "text",
406
- content: t.substring(e, n),
407
- position: { start: e, end: n }
395
+ content: s.substring(t, e),
396
+ position: { start: t, end: e }
408
397
  });
409
- class Ie {
398
+ class Ht {
410
399
  // Instance fields - only what's needed for single pass
411
400
  input;
412
401
  // ===== PUBLIC API =====
@@ -426,8 +415,8 @@ class Ie {
426
415
  * Complexity: O(M) where M is number of matches
427
416
  * Memory: O(D) for active parents stack where D is nesting depth
428
417
  */
429
- build(e, n) {
430
- return this.input = n, e.length === 0 ? [this.createTextToken(0, n.length)] : this.buildSinglePass(e);
418
+ build(t, e) {
419
+ return this.input = e, t.length === 0 ? [this.createTextToken(0, e.length)] : this.buildSinglePass(t);
431
420
  }
432
421
  // ===== SINGLE-PASS ALGORITHM =====
433
422
  /**
@@ -436,66 +425,66 @@ class Ie {
436
425
  * This is the core algorithm that processes matches sequentially,
437
426
  * maintaining a stack of active parents and creating tokens directly.
438
427
  */
439
- buildSinglePass(e) {
440
- const n = [], s = [];
428
+ buildSinglePass(t) {
429
+ const e = [], n = [];
441
430
  let r = null, o = 0;
442
- for (const i of e) {
431
+ for (const i of t) {
443
432
  if (r && i.conflictsWith(r))
444
433
  continue;
445
- for (r = i; s.length > 0; ) {
446
- const l = s[s.length - 1], h = this.getContentBounds(l.match);
447
- if (h.end <= i.start)
448
- this.finalizeParent(l, h.end), s.pop(), s.length > 0 ? s[s.length - 1].token.children.push(l.token) : n.push(l.token);
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);
449
438
  else
450
439
  break;
451
440
  }
452
- const a = s.length > 0 ? s[s.length - 1] : null;
441
+ const a = n.length > 0 ? n[n.length - 1] : null;
453
442
  if (a) {
454
443
  const l = this.createTextToken(a.textPos, i.start);
455
444
  a.token.children.push(l), a.textPos = i.end;
456
445
  } else {
457
446
  const l = this.createTextToken(o, i.start);
458
- n.push(l), o = i.end;
447
+ e.push(l), o = i.end;
459
448
  }
460
449
  const c = this.createMarkToken(i);
461
450
  if (this.hasNestedContent(i)) {
462
451
  const l = this.getContentBounds(i);
463
- s.push({
452
+ n.push({
464
453
  match: i,
465
454
  token: c,
466
455
  textPos: l.start
467
456
  });
468
457
  } else
469
- a ? a.token.children.push(c) : n.push(c);
458
+ a ? a.token.children.push(c) : e.push(c);
470
459
  }
471
- for (; s.length > 0; ) {
472
- const i = s.pop(), a = this.getContentBounds(i.match);
473
- this.finalizeParent(i, a.end), s.length > 0 ? s[s.length - 1].token.children.push(i.token) : n.push(i.token);
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);
474
463
  }
475
- return n.push(this.createTextToken(o, this.input.length)), n;
464
+ return e.push(this.createTextToken(o, this.input.length)), e;
476
465
  }
477
466
  /**
478
467
  * Finalizes a parent token by adding final text token if needed
479
468
  */
480
- finalizeParent(e, n) {
481
- const s = this.createTextToken(e.textPos, n);
482
- e.token.children.push(s), e.token.children.some((o) => o.type === "mark") || (e.token.children = []);
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 = []);
483
472
  }
484
473
  /**
485
474
  * Creates a mark token from a match (without children - those are added later)
486
475
  */
487
- createMarkToken(e) {
488
- const n = this.extractSubstring(e.gaps.value?.start, e.gaps.value?.end), s = this.extractSubstring(e.gaps.nested?.start, e.gaps.nested?.end), r = this.extractSubstring(e.gaps.meta?.start, e.gaps.meta?.end), o = s || void 0, i = e.gaps.meta !== void 0 ? r : void 0, a = n || o || "";
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 || "";
489
478
  return {
490
479
  type: "mark",
491
- content: this.input.substring(e.start, e.end),
480
+ content: this.input.substring(t.start, t.end),
492
481
  children: [],
493
482
  // Will be populated if match has nested content
494
- descriptor: e.descriptor,
483
+ descriptor: t.descriptor,
495
484
  value: a,
496
485
  meta: i,
497
- position: { start: e.start, end: e.end },
498
- nested: this.createNestedInfo(e, o)
486
+ position: { start: t.start, end: t.end },
487
+ nested: this.createNestedInfo(t, o)
499
488
  };
500
489
  }
501
490
  // ===== UTILITY METHODS =====
@@ -503,78 +492,78 @@ class Ie {
503
492
  * Gets the content boundaries for a match
504
493
  * Priority: nested content if present, otherwise value content
505
494
  */
506
- getContentBounds(e) {
507
- return e.gaps.nested ? e.gaps.nested : e.gaps.value ? e.gaps.value : {
508
- start: e.start,
509
- end: e.start
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
510
499
  };
511
500
  }
512
501
  /**
513
502
  * Checks if a match has nested content capability
514
503
  */
515
- hasNestedContent(e) {
516
- return e.gaps.nested !== void 0;
504
+ hasNestedContent(t) {
505
+ return t.gaps.nested !== void 0;
517
506
  }
518
507
  /**
519
508
  * Extracts substring safely, returns empty string if positions are undefined
520
509
  */
521
- extractSubstring(e, n) {
522
- return e !== void 0 && n !== void 0 ? this.input.substring(e, n) : "";
510
+ extractSubstring(t, e) {
511
+ return t !== void 0 && e !== void 0 ? this.input.substring(t, e) : "";
523
512
  }
524
513
  /**
525
514
  * Creates a text token for a range in the input
526
515
  */
527
- createTextToken(e, n) {
528
- return Y(this.input, e, n);
516
+ createTextToken(t, e) {
517
+ return dt(this.input, t, e);
529
518
  }
530
519
  /**
531
520
  * Creates nested info object if nested content exists
532
521
  */
533
- createNestedInfo(e, n) {
534
- if (!(!n || e.gaps.nested === void 0))
522
+ createNestedInfo(t, e) {
523
+ if (!(!e || t.gaps.nested === void 0))
535
524
  return {
536
- content: n,
537
- start: e.gaps.nested.start,
538
- end: e.gaps.nested.end
525
+ content: e,
526
+ start: t.gaps.nested.start,
527
+ end: t.gaps.nested.end
539
528
  };
540
529
  }
541
530
  }
542
- function $(t, e) {
543
- let n = t;
544
- return e.value !== void 0 && (n = n.replaceAll(y.Value, e.value)), e.meta !== void 0 && (n = n.replaceAll(y.Meta, e.meta)), e.nested !== void 0 && (n = n.replaceAll(y.Nested, e.nested)), n;
531
+ function j(s, t) {
532
+ let e = s;
533
+ return t.value !== void 0 && (e = e.replaceAll(y.Value, t.value)), t.meta !== void 0 && (e = e.replaceAll(y.Meta, t.meta)), t.nested !== void 0 && (e = e.replaceAll(y.Nested, t.nested)), e;
545
534
  }
546
- function T(t) {
547
- let e = "";
548
- for (const n of t) {
549
- if (n.type === "text") {
550
- e += n.content;
535
+ function I(s) {
536
+ let t = "";
537
+ for (const e of s) {
538
+ if (e.type === "text") {
539
+ t += e.content;
551
540
  continue;
552
541
  }
553
- const s = n.descriptor.markup, r = s.includes(y.Nested) ? n.children.length > 0 ? T(n.children) : n.nested?.content : void 0;
554
- e += $(s, {
555
- value: n.value,
556
- meta: n.meta,
542
+ const n = e.descriptor.markup, r = n.includes(y.Nested) ? e.children.length > 0 ? I(e.children) : e.nested?.content : void 0;
543
+ t += j(n, {
544
+ value: e.value,
545
+ meta: e.meta,
557
546
  nested: r
558
547
  });
559
548
  }
560
- return e;
549
+ return t;
561
550
  }
562
- function G(t, e) {
563
- let n = "";
564
- for (const s of t)
565
- if (s.type === "text")
566
- n += s.content;
567
- else if (s.children.length > 0) {
568
- const r = G(s.children, e), o = {
569
- ...s,
551
+ function J(s, t) {
552
+ let e = "";
553
+ for (const n of s)
554
+ if (n.type === "text")
555
+ e += n.content;
556
+ else if (n.children.length > 0) {
557
+ const r = J(n.children, t), o = {
558
+ ...n,
570
559
  value: r
571
560
  };
572
- n += e(o);
561
+ e += t(o);
573
562
  } else
574
- n += e(s);
575
- return n;
563
+ e += t(n);
564
+ return e;
576
565
  }
577
- class O {
566
+ class B {
578
567
  registry;
579
568
  segmentMatcher;
580
569
  patternMatcher;
@@ -598,8 +587,8 @@ class O {
598
587
  * ])
599
588
  * ```
600
589
  */
601
- constructor(e) {
602
- this.registry = new Ce(e), this.segmentMatcher = new be(this.registry.segments), this.patternMatcher = new Te(this.registry), this.treeBuilder = new Ie();
590
+ constructor(t) {
591
+ this.registry = new Dt(t), this.segmentMatcher = new $t(this.registry.segments), this.patternMatcher = new Rt(this.registry), this.treeBuilder = new Ht();
603
592
  }
604
593
  /**
605
594
  * Parses text into tokens (static convenience method)
@@ -615,9 +604,9 @@ class O {
615
604
  * })
616
605
  * ```
617
606
  */
618
- static parse(e, n) {
619
- const s = n?.markup;
620
- return !s || s.length === 0 ? [Y(e)] : new O(s).parse(e);
607
+ static parse(t, e) {
608
+ const n = e?.markup;
609
+ return !n || n.length === 0 ? [dt(t)] : new B(n).parse(t);
621
610
  }
622
611
  /**
623
612
  * Converts tokens back to text (static convenience method)
@@ -630,8 +619,8 @@ class O {
630
619
  * const text = Parser.stringify(tokens)
631
620
  * ```
632
621
  */
633
- static stringify(e) {
634
- return T(e);
622
+ static stringify(t) {
623
+ return I(t);
635
624
  }
636
625
  /**
637
626
  * Parses text into a nested token tree
@@ -656,9 +645,9 @@ class O {
656
645
  * // ]
657
646
  * ```
658
647
  */
659
- parse(e) {
660
- const n = this.segmentMatcher.search(e), s = this.patternMatcher.process(n);
661
- return this.treeBuilder.build(s, e);
648
+ parse(t) {
649
+ const e = this.segmentMatcher.search(t), n = this.patternMatcher.process(e);
650
+ return this.treeBuilder.build(n, t);
662
651
  }
663
652
  /**
664
653
  * Converts tokens back to the original text
@@ -677,8 +666,8 @@ class O {
677
666
  * console.log(reconstructed === text) // true
678
667
  * ```
679
668
  */
680
- stringify(e) {
681
- return T(e);
669
+ stringify(t) {
670
+ return I(t);
682
671
  }
683
672
  /**
684
673
  * Transforms annotated text by processing all mark tokens with a callback
@@ -705,9 +694,9 @@ class O {
705
694
  * // Returns: 'Hello:world and tag'
706
695
  * ```
707
696
  */
708
- transform(e, n) {
709
- const s = this.parse(e);
710
- return G(s, n);
697
+ transform(t, e) {
698
+ const n = this.parse(t);
699
+ return J(n, e);
711
700
  }
712
701
  /**
713
702
  * Escapes markup segments in the given text using backslash
@@ -726,8 +715,8 @@ class O {
726
715
  * // Returns: 'Hello \*\*world\*\* and \@[user]'
727
716
  * ```
728
717
  */
729
- escape(e) {
730
- return this.registry.segments.filter((n) => typeof n == "string").sort((n, s) => s.length - n.length).reduce((n, s) => n.replaceAll(s, s.replace(/(.)/g, "\\$1")), e);
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);
731
720
  }
732
721
  /**
733
722
  * Unescapes markup patterns in the given text
@@ -745,101 +734,184 @@ class O {
745
734
  * // Returns: 'Hello **world** and @[user]'
746
735
  * ```
747
736
  */
748
- unescape(e) {
749
- return e.replaceAll(/\\(.)/g, "$1");
737
+ unescape(t) {
738
+ return t.replaceAll(/\\(.)/g, "$1");
750
739
  }
751
740
  }
752
- function kt(t, e, n) {
753
- if (!n.length) return t;
754
- const s = new O(n).parse(t);
755
- return G(s, e);
741
+ function Be(s, t, e) {
742
+ if (!e.length) return s;
743
+ const n = new B(e).parse(s);
744
+ return J(n, t);
756
745
  }
757
- function Me(t = "", e = "") {
758
- if (t === e) return {};
759
- let n;
760
- for (let r = 0; r < t.length; r++)
761
- if (t[r] !== e[r]) {
762
- n = r;
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 Bt(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;
763
761
  break;
764
762
  }
765
- let s;
766
- for (let r = 1; r <= t.length; r++)
767
- if (t.at(-r) !== e.at(-r)) {
768
- s = t.length - r + 1;
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;
769
767
  break;
770
768
  }
771
- return { left: n, right: s };
769
+ return { left: e, right: n };
772
770
  }
773
- function q(t, e) {
774
- let n = -1, s = t.length;
775
- for (; s - n > 1; ) {
776
- const r = Math.round((n + s) / 2);
777
- t[r] <= e ? n = r : s = r;
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;
778
776
  }
779
- return t[n] == e && (s = n), [n, s].filter((r) => t[r] !== void 0);
777
+ return s[e] == t && (n = e), [e, n].filter((r) => s[r] !== void 0);
780
778
  }
781
- function Pe(t, e) {
782
- if (Object.is(t, e))
783
- return !0;
784
- if (typeof t != "object" || t === null || typeof e != "object" || e === null)
785
- return !1;
786
- const n = Object.keys(t);
787
- if (n.length !== Object.keys(e).length)
788
- return !1;
789
- for (let s = 0; s < n.length; s++)
790
- if (!Object.prototype.hasOwnProperty.call(e, n[s]) || !Object.is(t[n[s]], e[n[s]]))
791
- return !1;
792
- return !0;
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);
793
785
  }
794
- function Ne(t, e, n, s) {
795
- return t.slice(0, n) + e + t.slice(n + s.length);
786
+ function Wt(s) {
787
+ const t = s.state.value.get(), e = Gt(s), n = Bt(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 _(s, t ?? "");
807
+ }
796
808
  }
797
- function _(t, e) {
798
- const s = {
799
- prev: 2,
800
- self: 1,
801
- next: 0
802
- }[t], { focus: r } = e.nodes, [o, , i] = e.tokens.splice(r.index - s, 3), a = (o.type === "text", o.content), c = (i.type === "text", i.content);
803
- e.tokens = e.tokens.toSpliced(r.index - s, 0, {
804
- type: "text",
805
- content: a + c,
806
- position: {
807
- start: o.position.start,
808
- end: i.position.end
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 _(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 _(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 R {
837
+ #t;
838
+ #e = /* @__PURE__ */ new Set();
839
+ #n;
840
+ constructor(t, e) {
841
+ this.#t = t, this.#n = e?.equals ?? Ut;
842
+ }
843
+ static event() {
844
+ return new R(void 0, { equals: !1 });
845
+ }
846
+ get value() {
847
+ return this.#t;
848
+ }
849
+ set value(t) {
850
+ (this.#n === !1 || !this.#n(this.#t, t)) && (this.#t = t, this.#e.forEach((e) => e(t)));
851
+ }
852
+ on(t) {
853
+ return this.#e.add(t), () => this.#e.delete(t);
854
+ }
855
+ emit(t) {
856
+ arguments.length > 0 && (this.#t = t), this.#e.forEach((e) => e(this.#t));
857
+ }
858
+ get() {
859
+ return this.#t;
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 R(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);
809
883
  }
810
884
  });
811
- let l = r;
812
- for (let u = 0; u < s; u++)
813
- l = l.prev;
814
- const h = l.length;
815
- e.recovery = { anchor: l.prev, caret: h }, e.props.onChange?.(T(e.tokens));
816
- }
817
- class Ae {
818
- #t = /* @__PURE__ */ new Map();
819
- send(e, n) {
820
- this.#e(e).forEach((s) => s(n));
821
- }
822
- on(e, n) {
823
- return this.#e(e).add(n), () => this.#e(e).delete(n);
824
- }
825
- #e(e) {
826
- return this.#t.has(e) || this.#t.set(e, /* @__PURE__ */ new Set()), this.#t.get(e);
827
- }
828
- }
829
- const p = {
830
- STORE_UPDATED: Symbol(),
831
- ClearTrigger: Symbol(),
832
- CheckTrigger: Symbol(),
833
- Change: Symbol(),
834
- Parse: Symbol(),
835
- Delete: Symbol(),
836
- Select: Symbol()
837
- };
838
- class S {
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, R.event());
896
+ return new Proxy({}, {
897
+ get(n, r) {
898
+ let o = e.get(r);
899
+ return o || (o = R.event(), e.set(r, o)), qt(o);
900
+ }
901
+ });
902
+ }
903
+ class Zt {
904
+ #t = 1;
905
+ #e = /* @__PURE__ */ new WeakMap();
906
+ get(t) {
907
+ return this.#e.has(t) ? this.#e.get(t) : (this.#e.set(t, this.#t), this.#t++);
908
+ }
909
+ }
910
+ class w {
839
911
  static get isSelectedPosition() {
840
- const e = window.getSelection();
841
- if (e)
842
- return e.isCollapsed;
912
+ const t = window.getSelection();
913
+ if (t)
914
+ return t.isCollapsed;
843
915
  }
844
916
  static getCurrentPosition() {
845
917
  return window.getSelection()?.anchorOffset ?? 0;
@@ -849,61 +921,61 @@ class S {
849
921
  return window.getSelection()?.anchorNode?.textContent ?? "";
850
922
  }
851
923
  static getSelectedNode() {
852
- const e = window.getSelection()?.anchorNode;
853
- if (e) return e;
924
+ const t = window.getSelection()?.anchorNode;
925
+ if (t) return t;
854
926
  throw new Error("Anchor node of selection is not exists!");
855
927
  }
856
928
  //TODO add the returned type: "{left: CSSProperties["left"], top: CSSProperties["top"]}"?
857
929
  static getAbsolutePosition() {
858
- const e = window.getSelection()?.getRangeAt(0).getBoundingClientRect?.();
859
- return e ? { left: e.left, top: e.top + e.height + 1 } : { left: 0, top: 0 };
930
+ const t = window.getSelection()?.getRangeAt(0).getBoundingClientRect?.();
931
+ return t ? { left: t.left, top: t.top + t.height + 1 } : { left: 0, top: 0 };
860
932
  }
861
- static trySetIndex(e, n) {
933
+ static trySetIndex(t, e) {
862
934
  try {
863
- this.setIndex(e, n);
864
- } catch (s) {
865
- console.error(s);
935
+ this.setIndex(t, e);
936
+ } catch (n) {
937
+ console.error(n);
866
938
  }
867
939
  }
868
- static setIndex(e, n) {
869
- const s = window.getSelection();
870
- if (!s?.anchorNode || !s.rangeCount) return;
871
- const r = s.getRangeAt(0);
872
- r?.setStart(e.firstChild || e, n), r?.setEnd(e.firstChild || e, n);
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);
873
945
  }
874
- static getCaretIndex(e) {
875
- let n = 0;
876
- const s = window.getSelection();
877
- if (!s?.rangeCount) return n;
878
- const r = s.getRangeAt(0), o = r.cloneRange();
879
- return o.selectNodeContents(e), o.setEnd(r.endContainer, r.endOffset), n = o.toString().length, n;
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;
880
952
  }
881
- static setCaretToEnd(e) {
882
- if (!e) return;
883
- window.getSelection()?.setPosition(e, 1);
953
+ static setCaretToEnd(t) {
954
+ if (!t) return;
955
+ window.getSelection()?.setPosition(t, 1);
884
956
  }
885
957
  static getIndex() {
886
958
  return window.getSelection()?.anchorOffset ?? NaN;
887
959
  }
888
- static setIndex1(e) {
889
- const n = window.getSelection();
890
- if (!n?.anchorNode || !n.rangeCount) return;
891
- const s = n.getRangeAt(0);
892
- s?.setStart(s.startContainer.firstChild || s.startContainer, e), s?.setEnd(s.startContainer.firstChild || s.startContainer, e);
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);
893
965
  }
894
- setCaretRightTo(e, n) {
966
+ setCaretRightTo(t, e) {
895
967
  const r = window.getSelection()?.getRangeAt(0);
896
- r?.setStart(r.endContainer, n), r?.setEnd(r.endContainer, n);
968
+ r?.setStart(r.endContainer, e), r?.setEnd(r.endContainer, e);
897
969
  }
898
970
  }
899
- const Oe = new RegExp(/^\w*/);
900
- class U {
971
+ const Jt = new RegExp(/^\w*/);
972
+ class Q {
901
973
  span;
902
974
  node;
903
975
  dividedText;
904
976
  constructor() {
905
- const e = S.getCurrentPosition();
906
- this.node = S.getSelectedNode(), this.span = S.getFocusedSpan(), this.dividedText = this.getDividedTextBy(e);
977
+ const t = w.getCurrentPosition();
978
+ this.node = w.getSelectedNode(), this.span = w.getFocusedSpan(), this.dividedText = this.getDividedTextBy(t);
907
979
  }
908
980
  /**
909
981
  * Find overlay match in text using provided options and trigger extractor.
@@ -920,12 +992,12 @@ class U {
920
992
  * // Other framework usage
921
993
  * TriggerFinder.find(vueOptions, (opt) => opt.overlay?.trigger ?? '@')
922
994
  */
923
- static find(e, n) {
924
- if (e && S.isSelectedPosition)
925
- return new U().find(e, n);
995
+ static find(t, e) {
996
+ if (t && w.isSelectedPosition)
997
+ return new Q().find(t, e);
926
998
  }
927
- getDividedTextBy(e) {
928
- return { left: this.span.slice(0, e), right: this.span.slice(e) };
999
+ getDividedTextBy(t) {
1000
+ return { left: this.span.slice(0, t), right: this.span.slice(t) };
929
1001
  }
930
1002
  /**
931
1003
  * Find overlay match in provided options.
@@ -933,9 +1005,9 @@ class U {
933
1005
  * @param options - Array of options
934
1006
  * @param getTrigger - Function to extract trigger from each option
935
1007
  */
936
- find(e, n) {
937
- for (let s = 0; s < e.length; s++) {
938
- const r = e[s], o = n(r, s);
1008
+ find(t, e) {
1009
+ for (let n = 0; n < t.length; n++) {
1010
+ const r = t[n], o = e(r, n);
939
1011
  if (!o) continue;
940
1012
  const i = this.matchInTextVia(o);
941
1013
  if (i)
@@ -949,45 +1021,45 @@ class U {
949
1021
  };
950
1022
  }
951
1023
  }
952
- matchInTextVia(e = "@") {
953
- const n = this.matchRightPart(), s = this.matchLeftPart(e);
954
- if (s)
1024
+ matchInTextVia(t = "@") {
1025
+ const e = this.matchRightPart(), n = this.matchLeftPart(t);
1026
+ if (n)
955
1027
  return {
956
- word: s.word + n.word,
957
- annotation: s.annotation + n.word,
958
- index: s.index
1028
+ word: n.word + e.word,
1029
+ annotation: n.annotation + e.word,
1030
+ index: n.index
959
1031
  };
960
1032
  }
961
1033
  matchRightPart() {
962
- const { right: e } = this.dividedText;
963
- return { word: e.match(Oe)?.[0] };
1034
+ const { right: t } = this.dividedText;
1035
+ return { word: t.match(Jt)?.[0] };
964
1036
  }
965
- matchLeftPart(e) {
966
- const n = this.makeTriggerRegex(e), { left: s } = this.dividedText, r = s.match(n);
1037
+ matchLeftPart(t) {
1038
+ const e = this.makeTriggerRegex(t), { left: n } = this.dividedText, r = n.match(e);
967
1039
  if (!r) return;
968
1040
  const [o, i] = r;
969
1041
  return { word: i, annotation: o, index: r.index ?? 0 };
970
1042
  }
971
1043
  //TODO new overlayMatch option if (isSpaceBeforeRequired) append space check for not first words '\\s'
972
- makeTriggerRegex(e) {
973
- const n = b(e) + "(\\w*)$";
974
- return new RegExp(n);
1044
+ makeTriggerRegex(t) {
1045
+ const e = A(t) + "(\\w*)$";
1046
+ return new RegExp(e);
975
1047
  }
976
1048
  }
977
- class M {
1049
+ class V {
978
1050
  #t;
979
1051
  #e;
980
1052
  get target() {
981
1053
  return this.#t;
982
1054
  }
983
- set target(e) {
984
- this.#t = e;
1055
+ set target(t) {
1056
+ this.#t = t;
985
1057
  }
986
1058
  get next() {
987
- return new M(this.target?.nextSibling, this.#e);
1059
+ return new V(this.target?.nextSibling, this.#e);
988
1060
  }
989
1061
  get prev() {
990
- return new M(this.target?.previousSibling, this.#e);
1062
+ return new V(this.target?.previousSibling, this.#e);
991
1063
  }
992
1064
  get isSpan() {
993
1065
  return this.index % 2 === 0;
@@ -999,19 +1071,19 @@ class M {
999
1071
  return this.target?.isContentEditable ?? !1;
1000
1072
  }
1001
1073
  get isCaretAtBeginning() {
1002
- return this.target ? S.getCaretIndex(this.target) === 0 : void 0;
1074
+ return this.target ? w.getCaretIndex(this.target) === 0 : void 0;
1003
1075
  }
1004
1076
  get isCaretAtEnd() {
1005
- return this.target ? S.getCaretIndex(this.target) === this.target.textContent?.length : void 0;
1077
+ return this.target ? w.getCaretIndex(this.target) === this.target.textContent?.length : void 0;
1006
1078
  }
1007
1079
  get index() {
1008
1080
  return this.target?.parentElement ? [...this.target.parentElement.children].indexOf(this.target) : -1;
1009
1081
  }
1010
1082
  get caret() {
1011
- return this.target ? S.getCaretIndex(this.target) : -1;
1083
+ return this.target ? w.getCaretIndex(this.target) : -1;
1012
1084
  }
1013
- set caret(e) {
1014
- this.target && S.trySetIndex(this.target, e);
1085
+ set caret(t) {
1086
+ this.target && w.trySetIndex(this.target, t);
1015
1087
  }
1016
1088
  get length() {
1017
1089
  return this.target?.textContent?.length ?? -1;
@@ -1019,8 +1091,8 @@ class M {
1019
1091
  get content() {
1020
1092
  return this.target?.textContent ?? "";
1021
1093
  }
1022
- set content(e) {
1023
- this.target && (this.target.textContent = e ?? "");
1094
+ set content(t) {
1095
+ this.target && (this.target.textContent = t ?? "");
1024
1096
  }
1025
1097
  get head() {
1026
1098
  return this.#e.refs.container?.firstChild;
@@ -1031,11 +1103,11 @@ class M {
1031
1103
  get isFocused() {
1032
1104
  return this.target === document.activeElement;
1033
1105
  }
1034
- constructor(e, n) {
1035
- this.target = e, this.#e = n;
1106
+ constructor(t, e) {
1107
+ this.target = t, this.#e = e;
1036
1108
  }
1037
1109
  setCaretToEnd() {
1038
- S.setCaretToEnd(this.target);
1110
+ w.setCaretToEnd(this.target);
1039
1111
  }
1040
1112
  focus() {
1041
1113
  this.target?.focus();
@@ -1044,712 +1116,1022 @@ class M {
1044
1116
  this.target = void 0;
1045
1117
  }
1046
1118
  }
1047
- class Le {
1048
- #t = 1;
1049
- #e = /* @__PURE__ */ new WeakMap();
1050
- get(e) {
1051
- return this.#e.has(e) ? this.#e.get(e) : (this.#e.set(e, this.#t), this.#t++);
1052
- }
1119
+ function Qt(s, t, e, n) {
1120
+ return s.slice(0, e) + t + s.slice(e + n.length);
1053
1121
  }
1054
- class B {
1055
- // Utils domain
1056
- bus = new Ae();
1057
- key = new Le();
1058
- // Config domain
1059
- props;
1060
- // Document domain
1061
- tokens = [];
1062
- parser;
1063
- previousValue;
1064
- // Navigation domain
1065
- nodes = {
1066
- focus: new M(void 0, this),
1067
- input: new M(void 0, this)
1068
- };
1069
- recovery;
1070
- // UI domain
1071
- refs = {
1072
- container: null,
1073
- overlay: null,
1074
- setContainer: (e) => {
1075
- this.refs.container = e;
1076
- },
1077
- setOverlay: (e) => {
1078
- this.refs.overlay = e;
1079
- }
1080
- };
1081
- selecting;
1082
- // Overlay domain
1083
- overlayMatch;
1084
- constructor(e) {
1085
- this.props = e;
1086
- }
1087
- static create = (e) => new Proxy(new B(e), { set: De });
1088
- }
1089
- const _e = /* @__PURE__ */ new Set(["bus", "refs", "nodes", "key"]);
1090
- function De(t, e, n, s) {
1091
- return _e.has(String(e)) ? !1 : (t[e] === n || (t[e] = n, t.bus.send(p.STORE_UPDATED, s)), !0);
1092
- }
1093
- const P = ce(void 0);
1094
- P.displayName = "StoreContext";
1095
- function v(t, e, n) {
1096
- typeof t == "string" ? $e(t, e, n) : Re(t, e, n);
1097
- }
1098
- function Re(t, e, n) {
1099
- const s = N(P);
1100
- Q(s), x(() => s.bus.on(t, e), n);
1101
- }
1102
- function $e(t, e, n) {
1103
- const s = N(P);
1104
- x(() => (s?.refs.container?.addEventListener(t, e), () => s?.refs.container?.removeEventListener(t, e)), n);
1105
- }
1106
- function f(t, e) {
1107
- const n = N(P);
1108
- Q(n);
1109
- const [s, r] = A(() => t?.(n));
1110
- return v(
1111
- p.STORE_UPDATED,
1112
- () => {
1113
- r((o) => {
1114
- const i = t?.(n);
1115
- return e && Pe(o, i) ? o : i;
1116
- });
1117
- },
1118
- []
1119
- ), t ? s : n;
1120
- }
1121
- const Ve = (t) => {
1122
- const e = ae.createContext(void 0);
1123
- e.displayName = t;
1124
- const n = o(e), s = e.Provider;
1125
- return [n, s, e];
1126
- function o(i) {
1127
- return () => {
1128
- const a = N(i);
1129
- if (a) return a;
1130
- throw new Error(`The context ${i.displayName} didn't found!`);
1131
- };
1132
- }
1133
- }, [H, X] = Ve("NodeProvider");
1134
- function ee(t, e, n, s) {
1135
- const r = f((c) => t === "mark" ? c.props.Mark : c.props.Overlay), o = t === "mark" ? e?.mark : e?.overlay;
1136
- let i;
1137
- o !== void 0 ? typeof o == "function" ? i = o(n) : i = o : i = n ?? {};
1138
- const a = i.slot || r || s;
1139
- if (!a)
1140
- throw new Error(
1141
- `No ${t} component found. Provide either option.${t}.slot, global ${t === "mark" ? "Mark" : "Overlay"}, or a defaultComponent.`
1142
- );
1143
- return [a, i];
1144
- }
1145
- function Fe() {
1146
- const t = H(), { options: e, key: n } = f((c) => ({ options: c.props.options, key: c.key }), !0), s = e?.[t.descriptor.index], r = t.children.map((c) => /* @__PURE__ */ m(z, { mark: c, isNested: !0 }, n.get(c))), o = {
1147
- value: t.value,
1148
- meta: t.meta,
1149
- nested: t.nested?.content,
1150
- children: t.children.length > 0 ? r : void 0
1151
- }, [i, a] = ee("mark", s, o);
1152
- return /* @__PURE__ */ m(i, { ...a });
1153
- }
1154
- const We = () => {
1155
- const t = H(), e = I(null), { readOnly: n, SpanComponent: s, spanProps: r } = f(
1156
- (o) => ({
1157
- readOnly: o.props.readOnly,
1158
- SpanComponent: Z("span", o),
1159
- spanProps: J("span", o)
1160
- }),
1161
- !0
1162
- );
1163
- if (t.type !== "text")
1164
- throw new Error("TextSpan component expects a TextToken");
1165
- return /* @__PURE__ */ m(
1166
- s,
1167
- {
1168
- ...r,
1169
- ref: e,
1170
- contentEditable: !n,
1171
- onPaste: Ge,
1172
- suppressContentEditableWarning: !0,
1173
- children: t.content
1174
- }
1175
- );
1176
- };
1177
- function Ge(t) {
1178
- t.preventDefault();
1179
- const e = t.clipboardData.getData("text");
1180
- document.execCommand("insertText", !1, e);
1181
- }
1182
- const z = W(({ mark: t, isNested: e = !1 }) => t.type === "mark" ? /* @__PURE__ */ m(X, { value: t, children: /* @__PURE__ */ m(Fe, {}) }) : e ? /* @__PURE__ */ m(oe, { children: t.content }) : /* @__PURE__ */ m(X, { value: t, children: /* @__PURE__ */ m(We, {}) }));
1183
- z.displayName = "Token";
1184
- const te = W(() => {
1185
- const { className: t, style: e, tokens: n, bus: s, key: r, ContainerComponent: o, containerProps: i, refs: a } = f(
1186
- (c) => ({
1187
- className: c.props.className,
1188
- style: c.props.style,
1189
- tokens: c.tokens,
1190
- bus: c.bus,
1191
- key: c.key,
1192
- refs: c.refs,
1193
- ContainerComponent: Z("container", c),
1194
- containerProps: J("container", c)
1195
- }),
1196
- !0
1197
- );
1198
- return v("input", () => s.send(p.Change), []), /* @__PURE__ */ m(o, { ref: a.setContainer, ...i, className: t, style: e, children: n.map((c) => /* @__PURE__ */ m(z, { mark: c }, r.get(c))) });
1199
- });
1200
- te.displayName = "Container";
1201
- function Ue() {
1202
- const { match: t, bus: e } = f(
1203
- (n) => ({
1204
- match: n.overlayMatch,
1205
- bus: n.bus
1206
- }),
1207
- !0
1208
- );
1209
- x(() => {
1210
- if (!t) return;
1211
- const n = (s) => {
1212
- s.key === k.ESC && e.send(p.ClearTrigger);
1213
- };
1214
- return window.addEventListener("keydown", n), () => window.removeEventListener("keydown", n);
1215
- }, [t, e]);
1216
- }
1217
- function Be() {
1218
- const t = f(), e = f((n) => n.overlayMatch);
1219
- x(() => {
1220
- if (!e) return;
1221
- const n = (s) => {
1222
- const r = s.target;
1223
- t.refs.overlay?.contains(r) || t.refs.container?.contains(r) || t.bus.send(p.ClearTrigger);
1224
- };
1225
- return document.addEventListener("click", n), () => document.removeEventListener("click", n);
1226
- }, [e]);
1227
- }
1228
- function C(t, e, n = []) {
1229
- v(
1230
- "keydown",
1231
- (s) => {
1232
- s.key === t && e(s);
1233
- },
1234
- n
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
+ })
1235
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()));
1236
1143
  }
1237
- function He() {
1238
- const t = f();
1239
- C(k.LEFT, e), C(k.RIGHT, n), C(k.DELETE, s), C(k.BACKSPACE, s), C(k.DELETE, o), C(k.BACKSPACE, r), v("keydown", i, []), v("paste", l, []), x(() => {
1240
- const u = t.refs.container;
1241
- if (!u) return;
1242
- const d = (g) => {
1243
- c(g);
1244
- };
1245
- return u.addEventListener("beforeinput", d, !0), () => u.removeEventListener("beforeinput", d, !0);
1246
- }, []);
1247
- function e(u) {
1248
- const { focus: d } = t.nodes;
1249
- if (d.isMark && !d.isEditable || d.isCaretAtBeginning) {
1250
- const g = d.prev;
1251
- g.focus(), g.isFocused || (g.prev.focus(), u.preventDefault()), d.setCaretToEnd();
1252
- }
1144
+ class Kt {
1145
+ constructor(t) {
1146
+ this.store = t;
1253
1147
  }
1254
- function n(u) {
1255
- const { focus: d } = t.nodes;
1256
- if (d.isMark && !d.isEditable || d.isCaretAtEnd) {
1257
- const g = d.next;
1258
- g.focus(), g.isFocused || (g.next.focus(), u.preventDefault());
1259
- }
1260
- }
1261
- function s() {
1262
- t.nodes.focus.isMark && _("self", t);
1148
+ #t;
1149
+ #e;
1150
+ #n;
1151
+ enable() {
1152
+ this.#t || (this.#t = 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.#e = 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
+ }));
1263
1185
  }
1264
- function r(u) {
1265
- t.nodes.focus.isSpan && t.nodes.focus.isCaretAtBeginning && t.nodes.focus.prev.target && (u.preventDefault(), _("prev", t));
1186
+ disable() {
1187
+ this.#t?.(), this.#e?.(), this.#n?.(), this.#t = void 0, this.#e = void 0, this.#n = void 0;
1266
1188
  }
1267
- function o(u) {
1268
- t.nodes.focus.isSpan && t.nodes.focus.isCaretAtEnd && t.nodes.focus.next.target && (u.preventDefault(), _("next", t));
1189
+ }
1190
+ class Yt {
1191
+ constructor(t) {
1192
+ this.store = t;
1269
1193
  }
1270
- function i(u) {
1271
- if ((u.ctrlKey || u.metaKey) && u.code === "KeyA") {
1272
- u.preventDefault();
1273
- const d = window.getSelection(), g = t.refs.container?.firstChild, E = t.refs.container?.lastChild;
1274
- if (!d || !g || !E) return;
1275
- d.setBaseAndExtent(g, 0, E, 1), t.selecting = "all";
1276
- }
1194
+ #t;
1195
+ #e;
1196
+ #n;
1197
+ #s;
1198
+ #r;
1199
+ #o;
1200
+ #i;
1201
+ #a;
1202
+ enableTrigger(t, e) {
1203
+ if (this.#t) return;
1204
+ this.#t = this.store.events.clearOverlay.on(() => {
1205
+ e(void 0);
1206
+ }), this.#e = this.store.events.checkOverlay.on(() => {
1207
+ const r = Q.find(this.store.state.options.get(), t);
1208
+ e(r);
1209
+ }), this.#n = this.store.events.change.on(() => {
1210
+ const r = this.store.state.showOverlayOn.get(), o = "change";
1211
+ (r === o || Array.isArray(r) && r.includes(o)) && this.store.events.checkOverlay();
1212
+ }), this.#s = () => {
1213
+ const r = this.store.state.showOverlayOn.get(), o = "selectionChange";
1214
+ (r === o || Array.isArray(r) && r.includes(o)) && this.store.events.checkOverlay();
1215
+ }, this.#r = () => {
1216
+ document.addEventListener("selectionchange", this.#s);
1217
+ }, this.#o = () => {
1218
+ document.removeEventListener("selectionchange", this.#s);
1219
+ };
1220
+ const n = this.store.refs.container;
1221
+ n && (n.addEventListener("focusin", this.#r), n.addEventListener("focusout", this.#o));
1277
1222
  }
1278
- function a() {
1279
- const u = window.getSelection(), d = t.refs.container;
1280
- if (!u?.rangeCount || !d?.firstChild || !d?.lastChild) return !1;
1281
- try {
1282
- const g = u.getRangeAt(0);
1283
- return d.contains(g.startContainer) && d.contains(g.endContainer) && g.toString().length > 0;
1284
- } catch {
1285
- return !1;
1286
- }
1223
+ enableClose() {
1224
+ this.#i || (this.#i = (t) => {
1225
+ t.key === x.ESC && this.store.events.clearOverlay();
1226
+ }, this.#a = (t) => {
1227
+ const e = t.target;
1228
+ this.store.refs.overlay?.contains(e) || this.store.refs.container?.contains(e) || this.store.events.clearOverlay();
1229
+ }, window.addEventListener("keydown", this.#i), document.addEventListener("click", this.#a, !0));
1287
1230
  }
1288
- function c(u) {
1289
- if (t.selecting !== "all" || !a()) {
1290
- t.selecting === "all" && (t.selecting = void 0);
1291
- return;
1292
- }
1293
- if (u.inputType === "insertFromPaste") return;
1294
- u.preventDefault();
1295
- const d = u.inputType.startsWith("delete") ? "" : u.data ?? "";
1296
- h(d);
1297
- }
1298
- function l(u) {
1299
- if (t.selecting !== "all" || !a()) {
1300
- t.selecting === "all" && (t.selecting = void 0);
1301
- return;
1302
- }
1303
- u.preventDefault();
1304
- const d = u.clipboardData?.getData("text/plain") ?? "";
1305
- h(d);
1231
+ disableClose() {
1232
+ this.#i && (window.removeEventListener("keydown", this.#i), document.removeEventListener("click", this.#a, !0), this.#i = void 0, this.#a = void 0);
1306
1233
  }
1307
- function h(u) {
1308
- t.nodes.focus.target = null, t.selecting = void 0, t.props.onChange?.(u), t.props.value === void 0 && (t.tokens = t.parser?.parse(u) ?? [
1309
- {
1310
- type: "text",
1311
- content: u,
1312
- position: { start: 0, end: u.length }
1313
- }
1314
- ]), queueMicrotask(() => {
1315
- const d = t.refs.container?.firstChild;
1316
- d && (t.recovery = {
1317
- anchor: t.nodes.focus,
1318
- caret: u.length
1319
- }, d.focus());
1320
- });
1234
+ disable() {
1235
+ const t = this.store.refs.container;
1236
+ t && this.#r && (t.removeEventListener("focusin", this.#r), t.removeEventListener("focusout", this.#o)), this.#s && document.removeEventListener("selectionchange", this.#s), this.disableClose(), this.#t?.(), this.#e?.(), this.#n?.(), this.#t = void 0, this.#e = void 0, this.#n = void 0, this.#s = void 0, this.#r = void 0, this.#o = void 0;
1321
1237
  }
1322
1238
  }
1323
- function ze() {
1324
- const t = f();
1325
- v(
1326
- p.Change,
1327
- () => {
1328
- const { onChange: e } = t.props;
1329
- if (!t.nodes.focus.target) return;
1330
- const n = t.tokens[t.nodes.focus.index];
1331
- n.type === "text" ? n.content = t.nodes.focus.content : n.type === "mark" && (n.value = t.nodes.focus.content), e?.(T(t.tokens)), t.bus.send(p.Parse);
1332
- },
1333
- []
1334
- ), v(
1335
- p.Delete,
1336
- ({ token: e }) => {
1337
- const { onChange: n } = t.props;
1338
- t.tokens.splice(t.tokens.indexOf(e), 1), n?.(T(t.tokens));
1339
- },
1340
- []
1341
- ), v(
1342
- p.Select,
1343
- (e) => {
1344
- const { Mark: n, onChange: s } = t.props, {
1345
- mark: r,
1346
- match: { option: o, span: i, index: a, source: c }
1347
- } = e, l = r.type === "mark" ? $(o.markup, {
1348
- value: r.value,
1349
- meta: r.meta
1350
- }) : $(o.markup, {
1351
- value: r.content
1352
- }), h = Ne(i, l, a, c);
1353
- if (t.recovery = n ? { caret: 0, anchor: t.nodes.input.next, isNext: !0 } : { caret: a + l.length, anchor: t.nodes.input }, t.nodes.input.target) {
1354
- t.nodes.input.content = h;
1355
- const u = t.tokens[t.nodes.input.index];
1356
- u.type === "text" && (u.content = h), t.nodes.focus.target = t.nodes.input.target, t.nodes.input.clear(), s?.(T(t.tokens)), t.bus.send(p.Parse);
1357
- }
1358
- },
1359
- []
1360
- );
1361
- }
1362
- const je = () => {
1363
- const t = f();
1364
- v("focusin", (e) => t.nodes.focus.target = e.target, []), v("focusout", (e) => t.nodes.focus.target = void 0, []);
1365
- }, qe = () => {
1366
- const t = f(), e = f((n) => n.tokens);
1367
- v(
1368
- "click",
1369
- () => {
1370
- e.length === 1 && e[0].type === "text" && e[0].content === "" && t.refs.container?.firstElementChild?.focus();
1371
- },
1372
- [e]
1373
- );
1374
- }, Xe = () => {
1375
- const t = f(), e = f((s) => s.tokens), n = t.props.Mark ? [e] : void 0;
1376
- x(() => {
1377
- if (!t.recovery) return;
1378
- const { anchor: s, caret: r, isNext: o } = t.recovery;
1239
+ class te {
1240
+ constructor(t) {
1241
+ this.store = t;
1242
+ }
1243
+ #t;
1244
+ #e;
1245
+ #n;
1246
+ enable() {
1247
+ if (this.#t) return;
1248
+ const t = this.store.refs.container;
1249
+ t && (this.#t = (e) => {
1250
+ this.store.nodes.focus.target = e.target;
1251
+ }, this.#e = () => {
1252
+ this.store.nodes.focus.target = void 0;
1253
+ }, this.#n = () => {
1254
+ const e = this.store.state.tokens.get();
1255
+ e.length === 1 && e[0].type === "text" && e[0].content === "" && this.store.refs.container?.firstElementChild?.focus();
1256
+ }, t.addEventListener("focusin", this.#t), t.addEventListener("focusout", this.#e), t.addEventListener("click", this.#n));
1257
+ }
1258
+ disable() {
1259
+ const t = this.store.refs.container;
1260
+ !t || !this.#t || (t.removeEventListener("focusin", this.#t), t.removeEventListener("focusout", this.#e), t.removeEventListener("click", this.#n), this.#t = void 0, this.#e = void 0, this.#n = void 0);
1261
+ }
1262
+ recover() {
1263
+ const t = this.store.state.recovery.get();
1264
+ if (!t) return;
1265
+ const { anchor: e, caret: n, isNext: r } = t, o = !e.target || !e.target.isConnected;
1379
1266
  switch (!0) {
1380
- case (o && !s.target):
1381
- t.nodes.focus.tail.focus();
1267
+ case (r && o): {
1268
+ const i = this.store.refs.container, a = t.childIndex != null ? i?.children[t.childIndex + 2] : void 0;
1269
+ a ? a.focus() : this.store.nodes.focus.tail.focus();
1382
1270
  break;
1383
- case o:
1384
- s.prev.focus();
1271
+ }
1272
+ case r:
1273
+ e.prev.focus();
1385
1274
  break;
1386
- case !s.target:
1387
- t.nodes.focus.head.focus();
1275
+ case o:
1276
+ this.store.nodes.focus.head.focus();
1388
1277
  break;
1389
1278
  default:
1390
- s.next.focus();
1279
+ e.next.focus();
1391
1280
  }
1392
- t.nodes.focus.caret = r, t.recovery = void 0;
1393
- }, n);
1394
- };
1395
- function Ze() {
1396
- const t = f(), e = I(!1), n = I(null);
1397
- x(() => {
1398
- const s = (r) => {
1399
- n.current = r.target, e.current = !0;
1400
- };
1401
- return document.addEventListener("mousedown", s), () => document.removeEventListener("mousedown", s);
1402
- }, []), x(() => {
1403
- const s = (r) => {
1404
- const o = e.current, i = !t.refs.container?.contains(n.current) || n.current !== r.target, a = window.getSelection()?.containsNode(t.refs.container, !0);
1405
- o && i && a && (t.selecting = "drag");
1406
- };
1407
- return document.addEventListener("mousemove", s), () => document.removeEventListener("mousemove", s);
1408
- }, []), x(() => {
1409
- const s = () => {
1410
- e.current = !1, n.current = null, t.selecting = void 0;
1411
- };
1412
- return document.addEventListener("mouseup", s), () => document.removeEventListener("mouseup", s);
1413
- }, []), Je();
1414
- }
1415
- function Je() {
1416
- const t = f(), e = f((n) => n.selecting);
1417
- x(() => {
1418
- if (e !== "drag") return;
1419
- const n = [...t.refs.container.children], s = n.map((r) => r.contentEditable);
1420
- return n.forEach((r) => r.contentEditable = "false"), () => n.forEach((r, o) => r.contentEditable = s[o]);
1421
- }, [e]);
1422
- }
1423
- function Qe() {
1424
- const t = f(), e = R(() => {
1425
- const n = t.props.showOverlayOn, s = "selectionChange";
1426
- (n === s || n.includes(s)) && t.bus.send(p.CheckTrigger);
1427
- }, []);
1428
- v(
1429
- "focusin",
1430
- () => {
1431
- document.addEventListener("selectionchange", e);
1432
- },
1433
- []
1434
- ), v(
1435
- "focusout",
1436
- () => {
1437
- document.removeEventListener("selectionchange", e);
1438
- },
1439
- []
1440
- ), v(
1441
- p.Change,
1442
- () => {
1443
- const n = t.props.showOverlayOn, s = "change";
1444
- (n === s || n.includes(s)) && t.bus.send(p.CheckTrigger);
1445
- },
1446
- []
1447
- );
1281
+ this.store.nodes.focus.caret = n, this.store.state.recovery.set(void 0);
1282
+ }
1448
1283
  }
1449
- const Ke = () => {
1450
- const t = f();
1451
- v(p.ClearTrigger, (e) => t.overlayMatch = void 0, []), v(
1452
- p.CheckTrigger,
1453
- (e) => t.overlayMatch = U.find(t.props.options, (n) => n.overlay?.trigger),
1454
- []
1455
- );
1456
- }, Ye = () => {
1457
- const t = f(), e = I(!1), { value: n, options: s } = f(
1458
- (r) => ({
1459
- value: r.props.value,
1460
- options: r.props.Mark ? r.props.options : void 0
1461
- }),
1462
- !0
1463
- );
1464
- x(() => {
1465
- const r = s?.map((i) => i.markup);
1466
- if (r && r.some(Boolean) ? t.parser = new O(r) : t.parser = void 0, e.current) {
1467
- t.bus.send(p.Parse);
1468
- return;
1469
- }
1470
- const o = n ?? t.props.defaultValue ?? "";
1471
- t.parser ? t.tokens = t.parser.parse(o) : t.tokens = [
1472
- {
1473
- type: "text",
1474
- content: o,
1475
- position: { start: 0, end: o.length }
1284
+ class ee {
1285
+ constructor(t) {
1286
+ this.store = t;
1287
+ }
1288
+ #t;
1289
+ #e;
1290
+ #n;
1291
+ #s;
1292
+ #r = null;
1293
+ #o = !1;
1294
+ enable() {
1295
+ this.#t || (this.#t = (t) => {
1296
+ this.#r = t.target, this.#o = !0;
1297
+ }, this.#e = (t) => {
1298
+ 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);
1299
+ e && n && r && this.store.state.selecting.set("drag");
1300
+ }, this.#n = () => {
1301
+ this.#o = !1, this.#r = null, this.store.state.selecting.set(void 0);
1302
+ }, this.#s = () => {
1303
+ if (this.store.state.selecting.get() !== "drag") return;
1304
+ const t = [...this.store.refs.container.children], e = t.map((n) => n.contentEditable);
1305
+ t.forEach((n) => n.contentEditable = "false"), t.forEach((n, r) => n.contentEditable = e[r]);
1306
+ }, document.addEventListener("mousedown", this.#t), document.addEventListener("mousemove", this.#e), document.addEventListener("mouseup", this.#n), document.addEventListener("selectionchange", this.#s));
1307
+ }
1308
+ disable() {
1309
+ this.#t && (document.removeEventListener("mousedown", this.#t), document.removeEventListener("mousemove", this.#e), document.removeEventListener("mouseup", this.#n), document.removeEventListener("selectionchange", this.#s), this.#t = void 0, this.#e = void 0, this.#n = void 0, this.#s = void 0), this.#r = null, this.#o = !1;
1310
+ }
1311
+ }
1312
+ function ne(s, t) {
1313
+ if ((t.ctrlKey || t.metaKey) && t.code === "KeyA") {
1314
+ t.preventDefault();
1315
+ const e = window.getSelection(), n = s.refs.container?.firstChild, r = s.refs.container?.lastChild;
1316
+ if (!e || !n || !r) return;
1317
+ e.setBaseAndExtent(n, 0, r, 1), s.state.selecting.set("all");
1318
+ }
1319
+ }
1320
+ function se(s, t) {
1321
+ const { focus: e } = s.nodes;
1322
+ if (e.isMark && !e.isEditable || e.isCaretAtBeginning) {
1323
+ const n = e.prev;
1324
+ return n.focus(), n.isFocused || (n.prev.focus(), t.preventDefault()), e.setCaretToEnd(), !0;
1325
+ }
1326
+ return !1;
1327
+ }
1328
+ function re(s, t) {
1329
+ const { focus: e } = s.nodes;
1330
+ if (e.isMark && !e.isEditable || e.isCaretAtEnd) {
1331
+ const n = e.next;
1332
+ return n.focus(), n.isFocused || (n.next.focus(), t.preventDefault()), !0;
1333
+ }
1334
+ return !1;
1335
+ }
1336
+ class oe {
1337
+ constructor(t) {
1338
+ this.store = t;
1339
+ }
1340
+ #t;
1341
+ #e;
1342
+ #n;
1343
+ enable() {
1344
+ if (this.#t) return;
1345
+ const t = this.store.refs.container;
1346
+ t && (this.#t = (e) => {
1347
+ e.key === x.LEFT ? se(this.store, e) : e.key === x.RIGHT && re(this.store, e), this.#s(e), ne(this.store, e);
1348
+ }, this.#e = (e) => {
1349
+ ae(this.store, e);
1350
+ }, this.#n = (e) => {
1351
+ ie(this.store, e);
1352
+ }, t.addEventListener("keydown", this.#t), t.addEventListener("paste", this.#e), t.addEventListener("beforeinput", this.#n, !0));
1353
+ }
1354
+ disable() {
1355
+ const t = this.store.refs.container;
1356
+ !t || !this.#t || (t.removeEventListener("keydown", this.#t), t.removeEventListener("paste", this.#e), t.removeEventListener("beforeinput", this.#n, !0), this.#t = void 0, this.#e = void 0, this.#n = void 0);
1357
+ }
1358
+ #s(t) {
1359
+ const { focus: e } = this.store.nodes;
1360
+ if (t.key === x.DELETE || t.key === x.BACKSPACE) {
1361
+ if (e.isMark) {
1362
+ t.preventDefault(), U("self", this.store);
1363
+ return;
1476
1364
  }
1477
- ], e.current = !0;
1478
- }, [n, s]), v(
1479
- p.Parse,
1480
- () => {
1481
- t.tokens = t.nodes.focus.target ? et(t) : tt(t);
1482
- },
1483
- []
1484
- );
1485
- };
1486
- function et(t) {
1487
- const { focus: e } = t.nodes;
1488
- if (!t.parser)
1489
- return t.tokens;
1490
- const n = t.parser.parse(e.content);
1491
- return n.length === 1 ? t.tokens : t.tokens.toSpliced(e.index, 1, ...n);
1492
- }
1493
- function tt(t) {
1494
- const {
1495
- props: { value: e }
1496
- } = t, n = nt(t), s = Me(t.previousValue, e);
1497
- switch (t.previousValue = e, !0) {
1498
- //Mark removing happen
1499
- case (s.left && n.includes(s.left) && s.right && Math.abs(s.left - s.right) > 1): {
1500
- const r = n.indexOf(s.left), o = D(t, r - 1, r);
1501
- return t.tokens.toSpliced(r - 1, 2, ...o);
1502
- }
1503
- //Changing in label
1504
- case s.left !== void 0: {
1505
- const [r] = q(n, s.left), o = D(t, r);
1506
- return o.length === 1 ? t.tokens : t.tokens.toSpliced(r, 1, ...o);
1365
+ t.key === x.BACKSPACE && e.isSpan && e.isCaretAtBeginning && e.prev.target && (t.preventDefault(), U("prev", this.store)), t.key === x.DELETE && e.isSpan && e.isCaretAtEnd && e.next.target && (t.preventDefault(), U("next", this.store));
1507
1366
  }
1508
- case s.right !== void 0: {
1509
- const [r] = q(n, s.right), o = D(t, r);
1510
- return o.length === 1 ? t.tokens : t.tokens.toSpliced(r, 1, ...o);
1511
- }
1512
- default:
1513
- return ne(t, e ?? "");
1514
1367
  }
1515
1368
  }
1516
- function D(t, ...e) {
1517
- let n = "";
1518
- for (const s of e) {
1519
- const r = t.tokens[s];
1520
- n += r.content;
1369
+ function ie(s, t) {
1370
+ const e = s.state.selecting.get();
1371
+ if (e !== "all" || !ut(s)) {
1372
+ e === "all" && s.state.selecting.set(void 0);
1373
+ return;
1521
1374
  }
1522
- return ne(t, n);
1375
+ if (t.inputType === "insertFromPaste") return;
1376
+ t.preventDefault();
1377
+ const n = t.inputType.startsWith("delete") ? "" : t.data ?? "";
1378
+ ht(s, n);
1523
1379
  }
1524
- function nt(t) {
1525
- let e = 0;
1526
- return t.tokens.map((n) => {
1527
- const s = n.content.length;
1528
- return e += s, e - s;
1529
- }) ?? [];
1380
+ function ae(s, t) {
1381
+ const e = s.state.selecting.get();
1382
+ if (e !== "all" || !ut(s)) {
1383
+ e === "all" && s.state.selecting.set(void 0);
1384
+ return;
1385
+ }
1386
+ t.preventDefault();
1387
+ const n = t.clipboardData?.getData("text/plain") ?? "";
1388
+ ht(s, n);
1530
1389
  }
1531
- function ne(t, e) {
1532
- return t.parser ? t.parser.parse(e) : [
1533
- {
1534
- type: "text",
1535
- content: e,
1536
- position: { start: 0, end: e.length }
1537
- }
1538
- ];
1390
+ function ut(s) {
1391
+ const t = window.getSelection(), e = s.refs.container;
1392
+ if (!t?.rangeCount || !e?.firstChild || !e?.lastChild) return !1;
1393
+ try {
1394
+ const n = t.getRangeAt(0);
1395
+ return e.contains(n.startContainer) && e.contains(n.endContainer) && n.toString().length > 0;
1396
+ } catch {
1397
+ return !1;
1398
+ }
1539
1399
  }
1540
- const st = (t) => ({
1541
- get container() {
1542
- return t.refs.container;
1543
- },
1544
- get overlay() {
1545
- return t.refs.overlay;
1546
- },
1547
- focus() {
1548
- t.nodes.focus.head?.focus();
1400
+ function ht(s, t) {
1401
+ 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(
1402
+ s.state.parser.get()?.parse(t) ?? [
1403
+ {
1404
+ type: "text",
1405
+ content: t,
1406
+ position: { start: 0, end: t.length }
1407
+ }
1408
+ ]
1409
+ ), queueMicrotask(() => {
1410
+ const e = s.refs.container?.firstChild;
1411
+ e && (s.state.recovery.set({
1412
+ anchor: s.nodes.focus,
1413
+ caret: t.length
1414
+ }), e.focus());
1415
+ });
1416
+ }
1417
+ class ce {
1418
+ #t = [];
1419
+ #e = !1;
1420
+ register(t) {
1421
+ return this.#t.push(t), this;
1549
1422
  }
1550
- });
1551
- function rt(t) {
1552
- const e = f();
1553
- le(t, () => st(e), [e]);
1554
- }
1555
- const ot = ({ inRef: t }) => (rt(t), ze(), Ye(), je(), He(), qe(), Xe(), Ke(), Qe(), Ue(), Be(), Ze(), null), it = "_Container_1lmfr_1", at = "_Suggestions_1lmfr_10", ct = "_suggestionActive_1lmfr_38", V = {
1556
- Container: it,
1557
- Suggestions: at,
1558
- suggestionActive: ct
1559
- }, lt = [
1560
- {
1561
- markup: ve,
1562
- overlay: {
1563
- trigger: me,
1564
- data: []
1565
- }
1423
+ enableAll() {
1424
+ this.#e || (this.#e = !0, this.#t.forEach((t) => t.enable()));
1425
+ }
1426
+ disableAll() {
1427
+ this.#e && (this.#e = !1, this.#t.forEach((t) => t.disable()));
1566
1428
  }
1567
- ], ut = ({ props: t, children: e }) => {
1568
- const n = dt(t), [s] = A(() => B.create(n));
1569
- return x(() => {
1570
- s.props = n;
1571
- }), /* @__PURE__ */ m(P.Provider, { value: s, children: e });
1429
+ }
1430
+ const $ = (s, t) => ({
1431
+ name: s,
1432
+ enable: () => t.enable(),
1433
+ disable: () => t.disable()
1434
+ }), le = (s) => {
1435
+ const t = new ce();
1436
+ return t.register($("keydown", s.controllers.keydown)).register($("system", s.controllers.system)).register($("focus", s.controllers.focus)).register($("textSelection", s.controllers.textSelection)), t;
1572
1437
  };
1573
- function dt(t) {
1574
- const e = ge(V.Container, t.className, t.slotProps?.container?.className), n = pe(t.style, t.slotProps?.container?.style);
1575
- return {
1576
- value: t.value,
1577
- defaultValue: t.defaultValue,
1578
- onChange: t.onChange,
1579
- readOnly: t.readOnly,
1580
- options: t.options ?? lt,
1581
- showOverlayOn: t.showOverlayOn ?? "change",
1582
- className: e,
1583
- style: n,
1584
- Mark: t.Mark,
1585
- Overlay: t.Overlay,
1586
- slots: t.slots,
1587
- slotProps: t.slotProps
1588
- };
1438
+ class de {
1439
+ constructor(t) {
1440
+ this.store = t;
1441
+ }
1442
+ #t = [];
1443
+ #e = !1;
1444
+ enable(t) {
1445
+ const { store: e } = this, n = le(e);
1446
+ n.enableAll(), this.#t.push(() => n.disableAll()), this.#s(), this.#n(), t?.getTrigger && this.#r(t.getTrigger);
1447
+ }
1448
+ disable() {
1449
+ for (const t of this.#t)
1450
+ t();
1451
+ this.#t = [], this.#e = !1;
1452
+ }
1453
+ /**
1454
+ * Synchronizes the parser with current options and handles parsing.
1455
+ * Must be called by the framework layer when value or options change,
1456
+ * since these are props set synchronously during render.
1457
+ */
1458
+ syncParser(t, e) {
1459
+ const { store: n } = this, r = e?.map((i) => i.markup);
1460
+ if (r && r.some(Boolean) ? n.state.parser.set(new B(r)) : n.state.parser.set(void 0), this.#e) {
1461
+ n.state.recovery.get() || n.events.parse();
1462
+ return;
1463
+ }
1464
+ const o = t ?? n.state.defaultValue.get() ?? "";
1465
+ n.state.tokens.set(_(n, o)), this.#e = !0;
1466
+ }
1467
+ /**
1468
+ * Recovers focus after tokens change.
1469
+ * Must be called by the framework layer after DOM updates,
1470
+ * since focus recovery requires the new DOM to be committed.
1471
+ */
1472
+ recoverFocus() {
1473
+ this.store.state.Mark.get() && this.store.controllers.focus.recover();
1474
+ }
1475
+ #n() {
1476
+ const t = this.store.refs.container;
1477
+ if (!t) return;
1478
+ const e = () => this.store.events.change();
1479
+ t.addEventListener("input", e), this.#t.push(() => t.removeEventListener("input", e));
1480
+ }
1481
+ #s() {
1482
+ const { store: t } = this;
1483
+ this.#t.push(
1484
+ t.events.parse.on(() => {
1485
+ if (t.state.recovery.get()) {
1486
+ const e = I(t.state.tokens.get());
1487
+ t.state.tokens.set(_(t, e)), t.state.previousValue.set(e);
1488
+ return;
1489
+ }
1490
+ t.state.tokens.set(t.nodes.focus.target ? Ft(t) : Wt(t));
1491
+ })
1492
+ );
1493
+ }
1494
+ #r(t) {
1495
+ const { store: e } = this;
1496
+ e.controllers.overlay.enableTrigger(t, (n) => e.state.overlayMatch.set(n)), this.#t.push(() => e.controllers.overlay.disable()), this.#t.push(
1497
+ e.state.overlayMatch.on((n) => {
1498
+ n ? (e.nodes.input.target = e.nodes.focus.target, e.controllers.overlay.enableClose()) : e.controllers.overlay.disableClose();
1499
+ })
1500
+ );
1501
+ }
1589
1502
  }
1590
- function ht() {
1591
- const t = f(), e = f((o) => o.overlayMatch), n = S.getAbsolutePosition(), s = R(() => t.bus.send(p.ClearTrigger), []), r = R(
1592
- (o) => {
1593
- const i = {
1594
- type: "mark",
1595
- value: o.value,
1596
- meta: o.meta,
1597
- content: "",
1598
- position: { start: e.index, end: e.index + e.span.length },
1599
- descriptor: {
1600
- index: 0,
1601
- // TODO: get correct index
1602
- markup: e.option.markup
1603
- },
1604
- // TODO: fix typing
1605
- children: [],
1606
- nested: void 0
1607
- };
1608
- t.bus.send(p.Select, { mark: i, match: e }), t.bus.send(p.ClearTrigger);
1609
- },
1610
- [e]
1611
- );
1612
- return { match: e, style: n, select: r, close: s, ref: { current: t.refs.overlay } };
1613
- }
1614
- const ft = () => {
1615
- const { match: t, select: e, style: n, ref: s } = ht(), [r, o] = A(NaN), i = t.option.overlay?.data || [], a = ue(
1616
- () => i.filter((l) => l.toLowerCase().indexOf(t.value.toLowerCase()) > -1),
1617
- [t.value, i]
1618
- ), c = a.length;
1619
- return C(
1620
- k.UP,
1621
- (l) => {
1622
- l.preventDefault(), o((h) => isNaN(h) ? 0 : (c + (h - 1) % c) % c);
1623
- },
1624
- [c]
1625
- ), C(
1626
- k.DOWN,
1627
- (l) => {
1628
- l.preventDefault(), o((h) => isNaN(h) ? 0 : (h + 1) % c);
1629
- },
1630
- [c]
1631
- ), C(
1632
- k.ENTER,
1633
- (l) => {
1634
- l.preventDefault();
1635
- const h = a[r];
1636
- e({ value: h, meta: r.toString() });
1637
- },
1638
- [a, r]
1639
- ), a.length ? /* @__PURE__ */ m("ul", { ref: s, className: V.Suggestions, style: n, children: a.map((l, h) => {
1640
- const u = h === r ? V.suggestionActive : void 0;
1641
- return /* @__PURE__ */ m(
1642
- "li",
1503
+ class ue {
1504
+ key = new Zt();
1505
+ nodes = {
1506
+ focus: new V(void 0, this),
1507
+ input: new V(void 0, this)
1508
+ };
1509
+ state;
1510
+ events = Xt();
1511
+ refs = {
1512
+ container: null,
1513
+ overlay: null
1514
+ };
1515
+ controllers = {
1516
+ overlay: new Yt(this),
1517
+ focus: new te(this),
1518
+ keydown: new oe(this),
1519
+ system: new Kt(this),
1520
+ textSelection: new ee(this)
1521
+ };
1522
+ lifecycle = new de(this);
1523
+ constructor(t) {
1524
+ this.state = zt(
1643
1525
  {
1644
- ref: (d) => u && d?.scrollIntoView(!1),
1645
- className: u,
1646
- onClick: (d) => e({ value: l, meta: h.toString() }),
1647
- children: l
1526
+ tokens: [],
1527
+ parser: void 0,
1528
+ previousValue: void 0,
1529
+ recovery: void 0,
1530
+ selecting: void 0,
1531
+ overlayMatch: void 0,
1532
+ value: void 0,
1533
+ defaultValue: void 0,
1534
+ onChange: void 0,
1535
+ readOnly: !1,
1536
+ options: void 0,
1537
+ showOverlayOn: void 0,
1538
+ Mark: void 0,
1539
+ Overlay: void 0,
1540
+ className: void 0,
1541
+ style: void 0,
1542
+ slots: void 0,
1543
+ slotProps: void 0
1648
1544
  },
1649
- l
1545
+ t.createUseHook
1650
1546
  );
1651
- }) }) : null;
1652
- }, se = W(() => {
1653
- const t = f(), e = f((o) => o.overlayMatch), n = f((o) => o.overlayMatch ? o.key.get(o.overlayMatch.option) : void 0), [s, r] = ee("overlay", e?.option, void 0, ft);
1654
- if (x(() => {
1655
- t.nodes.input.target = t.nodes.focus.target;
1656
- }, [n]), n) return /* @__PURE__ */ m(s, { ...r ?? {} }, n);
1657
- });
1658
- se.displayName = "Whisper";
1659
- const gt = (t, e) => /* @__PURE__ */ ie(ut, { props: t, children: [
1660
- /* @__PURE__ */ m(te, {}),
1661
- /* @__PURE__ */ m(se, {}),
1662
- /* @__PURE__ */ m(ot, { inRef: e })
1663
- ] }), St = de(gt);
1664
- function F(t, e, n = 0, s) {
1665
- for (const r of t) {
1666
- if (r === e) return { depth: n, parent: s };
1667
- if (r.type === "mark") {
1668
- const o = F(r.children, e, n + 1, r);
1669
- if (o) return o;
1670
- }
1547
+ }
1548
+ createHandler() {
1549
+ const t = this;
1550
+ return {
1551
+ get container() {
1552
+ return t.refs.container;
1553
+ },
1554
+ get overlay() {
1555
+ return t.refs.overlay;
1556
+ },
1557
+ focus() {
1558
+ t.nodes.focus.head?.focus();
1559
+ }
1560
+ };
1671
1561
  }
1672
1562
  }
1673
- class pt {
1563
+ class he {
1674
1564
  ref;
1675
1565
  #t;
1676
1566
  #e;
1677
- readOnly;
1678
- constructor(e) {
1679
- this.ref = e.ref, this.#t = e.store, this.#e = e.token;
1567
+ #n;
1568
+ constructor(t) {
1569
+ this.ref = t.ref, this.#t = t.store, this.#e = t.token;
1570
+ }
1571
+ get readOnly() {
1572
+ return this.#n;
1573
+ }
1574
+ set readOnly(t) {
1575
+ this.#n = t;
1680
1576
  }
1681
- // ─── Data Properties ─────────────────────────────────────────────────────────
1682
- /** Displayed text of the mark */
1683
1577
  get content() {
1684
1578
  return this.#e.content;
1685
1579
  }
1686
- set content(e) {
1687
- this.#e.content = e, this.#n();
1580
+ set content(t) {
1581
+ this.#e.content = t, this.#s();
1688
1582
  }
1689
- /** Data value associated with the mark */
1690
1583
  get value() {
1691
1584
  return this.#e.value;
1692
1585
  }
1693
- set value(e) {
1694
- this.#e.value = e ?? "", this.#n();
1586
+ set value(t) {
1587
+ this.#e.value = t ?? "", this.#s();
1695
1588
  }
1696
- /** Optional metadata for the mark */
1697
1589
  get meta() {
1698
1590
  return this.#e.meta;
1699
1591
  }
1700
- set meta(e) {
1701
- this.#e.meta = e, this.#n();
1592
+ set meta(t) {
1593
+ this.#e.meta = t, this.#s();
1702
1594
  }
1703
- // ─── Navigation Properties ───────────────────────────────────────────────────
1704
- /** Nesting depth (0 for root-level marks) */
1705
1595
  get depth() {
1706
- return F(this.#t.tokens, this.#e).depth;
1596
+ return z(this.#t.state.tokens.get(), this.#e).depth;
1707
1597
  }
1708
- /** Whether this mark has nested children */
1709
1598
  get hasChildren() {
1710
1599
  return this.#e.children.length > 0;
1711
1600
  }
1712
- /** Parent mark token (undefined for root-level marks) */
1713
1601
  get parent() {
1714
- return F(this.#t.tokens, this.#e)?.parent;
1602
+ return z(this.#t.state.tokens.get(), this.#e)?.parent;
1715
1603
  }
1716
- /** Child tokens of this mark */
1717
1604
  get tokens() {
1718
1605
  return this.#e.children;
1719
1606
  }
1720
- // ─── Mutation Methods ────────────────────────────────────────────────────────
1721
- /** Update multiple properties in a single operation */
1722
- change = (e) => {
1723
- this.#e.content = e.content, this.#e.value = e.value ?? "", e.meta !== void 0 && (this.#e.meta = e.meta), this.#n();
1607
+ change = (t) => {
1608
+ this.#e.content = t.content, this.#e.value = t.value ?? "", t.meta !== void 0 && (this.#e.meta = t.meta), this.#s();
1724
1609
  };
1725
- /** Delete this mark from the editor */
1726
- remove = () => this.#t.bus.send(p.Delete, { token: this.#e });
1727
- // ─── Private ─────────────────────────────────────────────────────────────────
1728
- #n() {
1729
- this.#t.bus.send(p.Change, { node: this.#e });
1610
+ remove = () => this.#t.events.delete({ token: this.#e });
1611
+ #s() {
1612
+ this.#t.events.change();
1613
+ }
1614
+ }
1615
+ let ft = 0;
1616
+ function rt(s) {
1617
+ return `block-${ft++}-${s}`;
1618
+ }
1619
+ function fe() {
1620
+ ft = 0;
1621
+ }
1622
+ function ge(s) {
1623
+ if (s.length === 0) return [];
1624
+ fe();
1625
+ const t = [];
1626
+ let e = [], n = -1;
1627
+ const r = (o) => {
1628
+ e.length !== 0 && (t.push({
1629
+ id: rt(n),
1630
+ tokens: [...e],
1631
+ startPos: n,
1632
+ endPos: o
1633
+ }), e = [], n = -1);
1634
+ };
1635
+ for (const o of s) {
1636
+ if (o.type === "mark") {
1637
+ o.content.endsWith(`
1638
+ `) ? (r(o.position.start), t.push({
1639
+ id: rt(o.position.start),
1640
+ tokens: [o],
1641
+ startPos: o.position.start,
1642
+ endPos: o.position.end
1643
+ })) : (n === -1 && (n = o.position.start), e.push(o));
1644
+ continue;
1645
+ }
1646
+ if (o.type !== "text") continue;
1647
+ const a = pe(o);
1648
+ for (let c = 0; c < a.length; c++) {
1649
+ const l = a[c];
1650
+ if (l.isNewline) {
1651
+ r(l.position.start);
1652
+ continue;
1653
+ }
1654
+ l.content.length !== 0 && (n === -1 && (n = l.position.start), e.push({
1655
+ type: "text",
1656
+ content: l.content,
1657
+ position: l.position
1658
+ }));
1659
+ }
1660
+ }
1661
+ if (e.length > 0) {
1662
+ const o = e[e.length - 1];
1663
+ r(o.position.end);
1664
+ }
1665
+ return t;
1666
+ }
1667
+ function pe(s) {
1668
+ const t = [], { content: e, position: n } = s;
1669
+ let r = n.start;
1670
+ const o = [], i = () => {
1671
+ if (o.length > 0) {
1672
+ const a = o.join("");
1673
+ t.push({
1674
+ content: a,
1675
+ position: { start: r, end: r + a.length },
1676
+ isNewline: !1
1677
+ }), r += a.length, o.length = 0;
1678
+ }
1679
+ };
1680
+ for (let a = 0; a < e.length; a++) {
1681
+ const c = e[a];
1682
+ c === `
1683
+ ` ? (i(), t.push({
1684
+ content: `
1685
+ `,
1686
+ position: { start: r, end: r + 1 },
1687
+ isNewline: !0
1688
+ }), r += 1) : c === "\r" ? a + 1 < e.length && e[a + 1] === `
1689
+ ` ? (i(), t.push({
1690
+ content: `
1691
+ `,
1692
+ position: { start: r, end: r + 2 },
1693
+ isNewline: !0
1694
+ }), r += 2, a++) : (i(), t.push({
1695
+ content: `
1696
+ `,
1697
+ position: { start: r, end: r + 1 },
1698
+ isNewline: !0
1699
+ }), r += 1) : o.push(c);
1700
+ }
1701
+ return i(), t;
1702
+ }
1703
+ function ve(s, t, e, n) {
1704
+ if (e === n || e === n - 1 || t.length < 2 || e < 0 || e >= t.length || n < 0 || n > t.length) return s;
1705
+ const r = me(s, t), o = xe(r, e, n);
1706
+ return ye(o);
1707
+ }
1708
+ function me(s, t) {
1709
+ return t.map((e, n) => {
1710
+ const r = s.substring(e.startPos, e.endPos);
1711
+ let o = "";
1712
+ if (n < t.length - 1) {
1713
+ const i = t[n + 1];
1714
+ o = s.substring(e.endPos, i.startPos);
1715
+ }
1716
+ return {
1717
+ index: n,
1718
+ text: r,
1719
+ separatorAfter: o
1720
+ };
1721
+ });
1722
+ }
1723
+ function ye(s) {
1724
+ const t = [];
1725
+ for (let e = 0; e < s.length; e++) {
1726
+ const n = s[e], r = e === s.length - 1;
1727
+ let o = n.text;
1728
+ o.endsWith(`
1729
+ `) && (o = o.slice(0, -1)), t.push(o), r || t.push(n.separatorAfter || `
1730
+ `);
1731
+ }
1732
+ return t.join("");
1733
+ }
1734
+ function xe(s, t, e) {
1735
+ const n = [...s], [r] = n.splice(t, 1), o = e > t ? e - 1 : e;
1736
+ return n.splice(o, 0, r), ke(n, s), n;
1737
+ }
1738
+ function ke(s, t) {
1739
+ for (let e = 0; e < s.length - 1; e++) {
1740
+ const n = s[e].index, r = s[e + 1].index;
1741
+ if (Math.abs(n - r) === 1) {
1742
+ const o = Math.min(n, r), i = t[o].separatorAfter;
1743
+ s[e].separatorAfter = i.length > 0 ? i : `
1744
+ `;
1745
+ } else
1746
+ s[e].separatorAfter = `
1747
+ `;
1748
+ }
1749
+ }
1750
+ const we = {
1751
+ container: "div",
1752
+ span: "span"
1753
+ };
1754
+ function K(s, t) {
1755
+ return t?.[s] ? t[s] : we[s];
1756
+ }
1757
+ function Y(s, t) {
1758
+ const e = t?.[s];
1759
+ return e ? Et(e) : void 0;
1760
+ }
1761
+ const tt = at(void 0);
1762
+ tt.displayName = "StoreContext";
1763
+ function E() {
1764
+ const s = ct(tt);
1765
+ return bt(s), s;
1766
+ }
1767
+ const Se = (s) => {
1768
+ const t = at(void 0);
1769
+ return t.displayName = s, [() => {
1770
+ const n = ct(t);
1771
+ if (n === void 0)
1772
+ throw new Error(`Context "${s}" not found. Make sure to wrap component in its Provider.`);
1773
+ return n;
1774
+ }, t.Provider, t];
1775
+ }, [et, ot] = Se("NodeProvider");
1776
+ function gt(s, t, e, n) {
1777
+ 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;
1778
+ let l;
1779
+ c !== void 0 ? typeof c == "function" ? l = c(e) : l = c : l = e ?? {};
1780
+ const d = l.slot || a || n;
1781
+ if (!d)
1782
+ throw new Error(
1783
+ `No ${s} component found. Provide either option.${s}.slot, global ${s === "mark" ? "Mark" : "Overlay"}, or a defaultComponent.`
1784
+ );
1785
+ return [d, l];
1786
+ }
1787
+ function Ce() {
1788
+ 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 = {
1789
+ value: s.value,
1790
+ meta: s.meta,
1791
+ nested: s.nested?.content,
1792
+ children: s.children.length > 0 ? o : void 0
1793
+ }, [a, c] = gt("mark", r, i);
1794
+ return /* @__PURE__ */ f(a, { ...c });
1795
+ }
1796
+ const be = () => {
1797
+ const s = et(), t = E(), e = H(null), n = t.state.readOnly.use(), r = t.state.slots.use(), o = t.state.slotProps.use(), i = S(() => K("span", r), [r]), a = S(() => Y("span", o), [o]);
1798
+ if (s.type !== "text")
1799
+ throw new Error("TextSpan component expects a TextToken");
1800
+ return St(() => {
1801
+ e.current && e.current.textContent !== s.content && (e.current.textContent = s.content);
1802
+ }, [s.content]), /* @__PURE__ */ f(
1803
+ i,
1804
+ {
1805
+ ...a,
1806
+ ref: e,
1807
+ contentEditable: !n,
1808
+ onPaste: Ee,
1809
+ suppressContentEditableWarning: !0
1810
+ }
1811
+ );
1812
+ };
1813
+ function Ee(s) {
1814
+ s.preventDefault();
1815
+ const t = s.clipboardData.getData("text");
1816
+ document.execCommand("insertText", !1, t);
1817
+ }
1818
+ const F = O(({ mark: s, isNested: t = !1 }) => s.type === "mark" ? /* @__PURE__ */ f(ot, { value: s, children: /* @__PURE__ */ f(Ce, {}) }) : t ? /* @__PURE__ */ f(wt, { children: s.content }) : /* @__PURE__ */ f(ot, { value: s, children: /* @__PURE__ */ f(be, {}) }));
1819
+ F.displayName = "Token";
1820
+ const pt = O(() => {
1821
+ 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(() => K("container", e), [e]), l = S(() => Y("container", n), [n]);
1822
+ return /* @__PURE__ */ f(
1823
+ c,
1824
+ {
1825
+ ref: (d) => a.container = d,
1826
+ ...l,
1827
+ className: r,
1828
+ style: o,
1829
+ children: t.map((d) => /* @__PURE__ */ f(F, { mark: d }, i.get(d)))
1830
+ }
1831
+ );
1832
+ });
1833
+ pt.displayName = "Container";
1834
+ const q = {
1835
+ position: "absolute",
1836
+ left: -28,
1837
+ top: 2,
1838
+ width: 20,
1839
+ height: 20,
1840
+ display: "flex",
1841
+ alignItems: "center",
1842
+ justifyContent: "center",
1843
+ cursor: "grab",
1844
+ borderRadius: 4,
1845
+ opacity: 0,
1846
+ transition: "opacity 0.15s ease",
1847
+ userSelect: "none",
1848
+ color: "#9ca3af",
1849
+ flexShrink: 0,
1850
+ background: "none",
1851
+ border: "none",
1852
+ padding: 0,
1853
+ margin: 0,
1854
+ font: "inherit",
1855
+ lineHeight: 1
1856
+ }, vt = {
1857
+ ...q,
1858
+ opacity: 1
1859
+ }, Te = {
1860
+ ...vt,
1861
+ cursor: "grabbing"
1862
+ }, Ne = {
1863
+ position: "relative",
1864
+ paddingLeft: 4,
1865
+ transition: "opacity 0.2s ease"
1866
+ }, it = {
1867
+ position: "absolute",
1868
+ left: 0,
1869
+ right: 0,
1870
+ height: 2,
1871
+ backgroundColor: "#3b82f6",
1872
+ borderRadius: 1,
1873
+ pointerEvents: "none",
1874
+ zIndex: 10
1875
+ }, mt = O(() => /* @__PURE__ */ Z("svg", { width: "14", height: "14", viewBox: "0 0 16 16", fill: "currentColor", children: [
1876
+ /* @__PURE__ */ f("circle", { cx: "5", cy: "3", r: "1.5" }),
1877
+ /* @__PURE__ */ f("circle", { cx: "11", cy: "3", r: "1.5" }),
1878
+ /* @__PURE__ */ f("circle", { cx: "5", cy: "8", r: "1.5" }),
1879
+ /* @__PURE__ */ f("circle", { cx: "11", cy: "8", r: "1.5" }),
1880
+ /* @__PURE__ */ f("circle", { cx: "5", cy: "13", r: "1.5" }),
1881
+ /* @__PURE__ */ f("circle", { cx: "11", cy: "13", r: "1.5" })
1882
+ ] }));
1883
+ mt.displayName = "GripIcon";
1884
+ const yt = O(({ blockIndex: s, children: t, readOnly: e, onReorder: n }) => {
1885
+ const [r, o] = P(!1), [i, a] = P(!1), [c, l] = P(null), d = H(null), g = k(() => o(!0), []), u = k(() => o(!1), []), h = k(
1886
+ (p) => {
1887
+ p.dataTransfer.effectAllowed = "move", p.dataTransfer.setData("text/plain", String(s)), a(!0), d.current && p.dataTransfer.setDragImage(d.current, 0, 0);
1888
+ },
1889
+ [s]
1890
+ ), v = k(() => {
1891
+ a(!1), l(null);
1892
+ }, []), C = k((p) => {
1893
+ if (p.preventDefault(), p.dataTransfer.dropEffect = "move", !d.current) return;
1894
+ const D = d.current.getBoundingClientRect(), W = D.top + D.height / 2;
1895
+ l(p.clientY < W ? "before" : "after");
1896
+ }, []), m = k((p) => {
1897
+ p.currentTarget.contains(p.relatedTarget) || l(null);
1898
+ }, []), b = k(
1899
+ (p) => {
1900
+ p.preventDefault();
1901
+ const D = parseInt(p.dataTransfer.getData("text/plain"), 10);
1902
+ if (isNaN(D)) return;
1903
+ const W = c === "before" ? s : s + 1;
1904
+ l(null), n(D, W);
1905
+ },
1906
+ [s, c, n]
1907
+ ), T = {
1908
+ ...Ne,
1909
+ opacity: i ? 0.4 : 1
1910
+ }, L = e ? { ...q, display: "none" } : i ? Te : r ? vt : q;
1911
+ return /* @__PURE__ */ Z(
1912
+ "div",
1913
+ {
1914
+ ref: d,
1915
+ style: T,
1916
+ onMouseEnter: g,
1917
+ onMouseLeave: u,
1918
+ onDragOver: C,
1919
+ onDragLeave: m,
1920
+ onDrop: b,
1921
+ children: [
1922
+ c === "before" && /* @__PURE__ */ f("div", { style: { ...it, top: -1 } }),
1923
+ /* @__PURE__ */ f(
1924
+ "button",
1925
+ {
1926
+ type: "button",
1927
+ draggable: !e,
1928
+ onDragStart: h,
1929
+ onDragEnd: v,
1930
+ style: L,
1931
+ "aria-label": "Drag to reorder",
1932
+ children: /* @__PURE__ */ f(mt, {})
1933
+ }
1934
+ ),
1935
+ t,
1936
+ c === "after" && /* @__PURE__ */ f("div", { style: { ...it, bottom: -1 } })
1937
+ ]
1938
+ }
1939
+ );
1940
+ });
1941
+ yt.displayName = "DraggableBlock";
1942
+ const xt = O(() => {
1943
+ 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(() => K("container", e), [e]), u = S(() => Y("container", n), [n]), h = S(() => ge(t), [t]), v = H(h);
1944
+ v.current = h;
1945
+ const C = k(
1946
+ (m, b) => {
1947
+ if (!a || !c) return;
1948
+ const T = v.current, L = ve(a, T, m, b);
1949
+ if (L !== a) {
1950
+ const p = _(s, L);
1951
+ s.state.tokens.set(p), s.state.previousValue.set(L), c(L);
1952
+ }
1953
+ },
1954
+ [s, a, c]
1955
+ );
1956
+ return /* @__PURE__ */ f(
1957
+ g,
1958
+ {
1959
+ ref: (m) => d.container = m,
1960
+ ...u,
1961
+ className: r,
1962
+ style: o,
1963
+ children: h.map((m, b) => /* @__PURE__ */ f(yt, { blockIndex: b, readOnly: i, onReorder: C, children: m.tokens.map((T) => /* @__PURE__ */ f(F, { mark: T }, l.get(T))) }, m.id))
1964
+ }
1965
+ );
1966
+ });
1967
+ xt.displayName = "BlockContainer";
1968
+ function Ie() {
1969
+ const s = E(), t = s.state.overlayMatch.use(), e = w.getAbsolutePosition(), n = k(() => s.events.clearOverlay(), []), r = k(
1970
+ (i) => {
1971
+ const a = {
1972
+ type: "mark",
1973
+ value: i.value,
1974
+ meta: i.meta,
1975
+ content: "",
1976
+ position: { start: t.index, end: t.index + t.span.length },
1977
+ descriptor: {
1978
+ index: 0,
1979
+ markup: t.option.markup
1980
+ },
1981
+ children: [],
1982
+ nested: void 0
1983
+ };
1984
+ s.events.select({ mark: a, match: t }), s.events.clearOverlay();
1985
+ },
1986
+ [t]
1987
+ ), o = S(
1988
+ () => ({
1989
+ get current() {
1990
+ return s.refs.overlay;
1991
+ },
1992
+ set current(i) {
1993
+ s.refs.overlay = i;
1994
+ }
1995
+ }),
1996
+ []
1997
+ );
1998
+ return { match: t, style: e, select: r, close: n, ref: o };
1999
+ }
2000
+ const Pe = "_Container_1lmfr_1", Me = "_Suggestions_1lmfr_10", Le = "_suggestionActive_1lmfr_38", X = {
2001
+ Container: Pe,
2002
+ Suggestions: Me,
2003
+ suggestionActive: Le
2004
+ }, Ae = () => {
2005
+ const s = E(), { match: t, select: e, style: n, ref: r } = Ie(), [o, i] = P(NaN), a = t.option.overlay?.data || [], c = S(
2006
+ () => a.filter((d) => d.toLowerCase().indexOf(t.value.toLowerCase()) > -1),
2007
+ [t.value, a]
2008
+ ), l = c.length;
2009
+ return M(() => {
2010
+ const d = s.refs.container;
2011
+ if (!d) return;
2012
+ const g = (u) => {
2013
+ switch (u.key) {
2014
+ case x.UP:
2015
+ u.preventDefault(), i((h) => isNaN(h) ? 0 : (l + (h - 1) % l) % l);
2016
+ break;
2017
+ case x.DOWN:
2018
+ u.preventDefault(), i((h) => isNaN(h) ? 0 : (h + 1) % l);
2019
+ break;
2020
+ case x.ENTER:
2021
+ u.preventDefault(), i((h) => {
2022
+ if (isNaN(h)) return h;
2023
+ const v = c[h];
2024
+ return e({ value: v, meta: h.toString() }), h;
2025
+ });
2026
+ break;
2027
+ }
2028
+ };
2029
+ return d.addEventListener("keydown", g), () => d.removeEventListener("keydown", g);
2030
+ }, [l, c]), c.length ? /* @__PURE__ */ f("ul", { ref: r, className: X.Suggestions, style: n, children: c.map((d, g) => {
2031
+ const u = g === o ? X.suggestionActive : void 0;
2032
+ return /* @__PURE__ */ f(
2033
+ "li",
2034
+ {
2035
+ ref: (h) => {
2036
+ u && h && h.scrollIntoView(!1);
2037
+ },
2038
+ className: u,
2039
+ onClick: (h) => e({ value: d, meta: g.toString() }),
2040
+ children: d
2041
+ },
2042
+ d
2043
+ );
2044
+ }) }) : null;
2045
+ }, kt = O(() => {
2046
+ const s = E(), t = s.state.overlayMatch.use(), e = S(() => t ? s.key.get(t.option) : void 0, [t]), [n, r] = gt("overlay", t?.option, void 0, Ae);
2047
+ if (e) return /* @__PURE__ */ f(n, { ...r ?? {} }, e);
2048
+ });
2049
+ kt.displayName = "OverlayRenderer";
2050
+ function Oe(s, t) {
2051
+ Ct(t, () => s.createHandler(), [s]), M(() => (s.lifecycle.enable({
2052
+ getTrigger: (a) => a.overlay?.trigger
2053
+ }), () => s.lifecycle.disable()), []);
2054
+ const e = s.state.value.use(), n = s.state.Mark.use(), r = s.state.options.use(), o = n ? r : void 0;
2055
+ M(() => {
2056
+ s.lifecycle.syncParser(e, o);
2057
+ }, [e, o]);
2058
+ const i = s.state.tokens.use();
2059
+ M(() => {
2060
+ s.lifecycle.recoverFocus();
2061
+ }, [i]);
2062
+ }
2063
+ const De = (s) => () => {
2064
+ const [t, e] = P(() => s.get());
2065
+ return M(() => s.on(e), [s]), t;
2066
+ }, _e = [
2067
+ {
2068
+ markup: Pt,
2069
+ overlay: {
2070
+ trigger: It,
2071
+ data: []
2072
+ }
1730
2073
  }
2074
+ ];
2075
+ function Fe(s) {
2076
+ const {
2077
+ ref: t,
2078
+ value: e,
2079
+ defaultValue: n,
2080
+ onChange: r,
2081
+ readOnly: o = !1,
2082
+ block: i = !1,
2083
+ Mark: a,
2084
+ Overlay: c,
2085
+ slots: l,
2086
+ slotProps: d,
2087
+ options: g = _e,
2088
+ showOverlayOn: u = "change",
2089
+ className: h,
2090
+ style: v
2091
+ } = s, C = Tt(X.Container, h, d?.container?.className), m = Nt(v, d?.container?.style), [b] = P(() => new ue({ createUseHook: De }));
2092
+ b.state.set({
2093
+ value: e,
2094
+ defaultValue: n,
2095
+ onChange: r,
2096
+ readOnly: o,
2097
+ options: g,
2098
+ showOverlayOn: u,
2099
+ Mark: a,
2100
+ Overlay: c,
2101
+ className: C,
2102
+ style: m,
2103
+ slots: l,
2104
+ slotProps: d
2105
+ }), Oe(b, t);
2106
+ const T = i ? xt : pt;
2107
+ return /* @__PURE__ */ Z(tt.Provider, { value: b, children: [
2108
+ /* @__PURE__ */ f(T, {}),
2109
+ /* @__PURE__ */ f(kt, {})
2110
+ ] });
1731
2111
  }
1732
- const Ct = (t = {}) => {
1733
- const e = f(), n = H(), s = I();
1734
- if (n.type !== "mark")
2112
+ const We = (s = {}) => {
2113
+ const t = E(), e = et(), n = H(null);
2114
+ if (e.type !== "mark")
1735
2115
  throw new Error("useMark can only be used with mark tokens");
1736
- const [r] = A(() => new pt({ ref: s, store: e, token: n }));
1737
- mt(s, t, n);
1738
- const o = f((i) => i.props.readOnly);
1739
- return x(() => {
2116
+ const [r] = P(() => new he({ ref: n, store: t, token: e }));
2117
+ Re(n, s, e);
2118
+ const o = t.state.readOnly.use();
2119
+ return M(() => {
1740
2120
  r.readOnly = o;
1741
2121
  }, [o]), r;
1742
2122
  };
1743
- function mt(t, e, n) {
1744
- x(() => {
1745
- t.current && !e.controlled && (t.current.textContent = n.content);
2123
+ function Re(s, t, e) {
2124
+ M(() => {
2125
+ s.current && !t.controlled && (s.current.textContent = e.content);
1746
2126
  }, []);
1747
2127
  }
1748
2128
  export {
1749
- St as MarkedInput,
1750
- $ as annotate,
1751
- kt as denote,
1752
- v as useListener,
1753
- Ct as useMark,
1754
- ht as useOverlay
2129
+ he as MarkHandler,
2130
+ Fe as MarkedInput,
2131
+ j as annotate,
2132
+ Be as denote,
2133
+ ve as reorderBlocks,
2134
+ ge as splitTokensIntoBlocks,
2135
+ We as useMark,
2136
+ Ie as useOverlay
1755
2137
  };