@vector-im/matrix-wysiwyg 2.39.0 → 2.42.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.
@@ -1,994 +1,818 @@
1
- import { useState as E, useRef as b, useEffect as h, useMemo as C, useCallback as A } from "react";
2
- import { HtmlSource as H, initAsync as ae, new_composer_model_from_html as G, new_composer_model as w } from "@vector-im/matrix-wysiwyg-wasm";
3
- function y(e) {
4
- return "inputType" in e;
5
- }
6
- function V(e) {
7
- return "clipboardData" in e;
8
- }
9
- function ce(e) {
10
- return y(e) && e.inputType === "insertSuggestion";
11
- }
12
- function ue(e) {
13
- return y(e) && e.inputType === "insertAtRoomSuggestion";
14
- }
15
- function le(e) {
16
- return y(e) && e.inputType == "insertLink";
17
- }
18
- function X(e, t, n, s) {
19
- return s ? s(e, t, n) : e;
20
- }
21
- function fe(e, t, n, s, r, i, c, l) {
22
- const a = X(
23
- e,
24
- {
25
- actions: s,
26
- content: () => t.get_content_as_html(),
27
- messageContent: () => t.get_content_as_message_html()
28
- },
29
- r,
30
- c
31
- );
32
- if (!a)
33
- return;
34
- if (V(a)) {
35
- const o = a.clipboardData, f = o?.getData("text/html"), m = o?.getData("text/plain") ?? "";
36
- if (f && f !== m) {
37
- const d = o?.types.includes(
38
- "application/x-vnd.google-docs-document-slice-clip+wrapped"
39
- ) ? H.GoogleDoc : H.UnknownExternal;
40
- return n(
41
- t.replace_html(f, d),
42
- "replace_html_paste"
43
- );
44
- }
45
- return n(
46
- t.replace_text(m),
47
- "replace_text_paste"
48
- );
49
- }
50
- switch (a.inputType) {
51
- case "insertAtRoomSuggestion": {
52
- if (i && ue(a)) {
53
- const { attributes: o } = a.data;
54
- return o.has("data-mention-type") && o.delete("data-mention-type"), n(
55
- t.insert_at_room_mention_at_suggestion(
56
- i,
57
- o
58
- ),
59
- "insert_at_room_mention_at_suggestion"
60
- );
61
- }
62
- break;
63
- }
64
- case "insertSuggestion": {
65
- if (i && ce(a)) {
66
- const { text: o, url: f, attributes: m } = a.data;
67
- return m.has("data-mention-type") && m.delete("data-mention-type"), n(
68
- t.insert_mention_at_suggestion(
69
- f,
70
- o,
71
- i,
72
- m
73
- ),
74
- "insert_mention_at_suggestion"
75
- );
76
- }
77
- break;
78
- }
79
- case "insertCommand": {
80
- if (i && a.data)
81
- return n(
82
- t.replace_text_suggestion(
83
- a.data,
84
- i,
85
- !0
86
- ),
87
- "replace_text_suggestion"
88
- );
89
- break;
90
- }
91
- case "clear":
92
- return n(t.clear(), "clear");
93
- case "deleteContentBackward":
94
- return n(t.backspace(), "backspace");
95
- case "deleteWordBackward":
96
- return n(t.backspace_word(), "backspace_word");
97
- case "deleteSoftLineBackward": {
98
- const o = document.getSelection();
99
- return o && (o.modify("extend", "backward", "lineboundary"), document.dispatchEvent(new CustomEvent("selectionchange"))), n(t.delete(), "backspace_line");
100
- }
101
- case "deleteContentForward":
102
- return n(t.delete(), "delete");
103
- case "deleteWordForward":
104
- return n(t.delete_word(), "delete_word");
105
- case "deleteByCut":
106
- return n(t.delete(), "delete");
107
- case "formatBold":
108
- return n(t.bold(), "bold");
109
- case "formatItalic":
110
- return n(t.italic(), "italic");
111
- case "formatStrikeThrough":
112
- return n(t.strike_through(), "strike_through");
113
- case "formatUnderline":
114
- return n(t.underline(), "underline");
115
- case "formatInlineCode":
116
- return n(t.inline_code(), "inline_code");
117
- case "historyRedo":
118
- return n(t.redo(), "redo");
119
- case "historyUndo":
120
- return n(t.undo(), "undo");
121
- case "insertCodeBlock":
122
- return n(t.code_block(), "code_block");
123
- case "insertQuote":
124
- return n(t.quote(), "quote");
125
- case "insertFromPaste":
126
- return;
127
- case "insertOrderedList":
128
- return n(t.ordered_list(), "ordered_list");
129
- case "insertLineBreak":
130
- case "insertParagraph":
131
- return u(
132
- t,
133
- i,
134
- l
135
- ), n(t.enter(), "enter");
136
- case "insertReplacementText": {
137
- const o = r.innerHTML.slice(
138
- 0,
139
- r.innerHTML.length - 4
140
- );
141
- return n(
142
- t.set_content_from_html(o),
143
- "set_content_from_html",
144
- o
145
- );
146
- }
147
- case "insertCompositionText":
148
- case "insertFromComposition":
149
- case "insertText":
150
- if (a.data)
151
- return a.data == " " && u(
152
- t,
153
- i,
154
- l
155
- ), n(
156
- t.replace_text(a.data),
157
- "replace_text",
158
- a.data
159
- );
160
- break;
161
- case "insertUnorderedList":
162
- return n(t.unordered_list(), "unordered_list");
163
- case "insertLink":
164
- if (le(a)) {
165
- const { text: o, url: f } = a.data;
166
- return n(
167
- o ? t.set_link_with_text(f, o, /* @__PURE__ */ new Map()) : t.set_link(f, /* @__PURE__ */ new Map()),
168
- "insertLink"
169
- );
170
- }
171
- break;
172
- case "removeLinks":
173
- return n(t.remove_links(), "remove_links");
174
- case "formatIndent":
175
- return n(t.indent(), "indent");
176
- case "formatOutdent":
177
- return n(t.unindent(), "unindent");
178
- case "sendMessage":
179
- return null;
180
- default:
181
- return console.error(`Unknown input type: ${a.inputType}`), console.error(e), null;
182
- }
183
- function u(o, f, m) {
184
- if (m && f && f.key.key_type == 3 && f.key.custom_key_value) {
185
- const d = m.get(f.key.custom_key_value);
186
- d && o.replace_text_suggestion(d, f, !1);
187
- }
188
- }
189
- }
190
- function de(e, t) {
191
- e.innerHTML = "";
192
- const n = t.document();
193
- let s = 0;
194
- function r(c, l, a, u) {
195
- const o = document.createElement(l);
196
- if (a && (o.innerText = a.replace("​", "~")), u)
197
- for (const [f, m] of u.entries()) {
198
- const d = document.createAttribute(f);
199
- m !== null && (d.value = m), o.setAttributeNode(d);
200
- }
201
- return c.appendChild(o), o;
202
- }
203
- function i(c, l) {
204
- const a = r(l, "ul");
205
- a.className = `group_${s % 10}`;
206
- const u = c.children(t);
207
- let o;
208
- for (; o = u.next_child(); ) {
209
- const f = o.node_type(t);
210
- if (f === "container") {
211
- const d = `dom_${s}`;
212
- s++;
213
- const g = r(a, "li");
214
- r(
215
- g,
216
- "input",
217
- null,
218
- /* @__PURE__ */ new Map([
219
- ["type", "checkbox"],
220
- ["id", d],
221
- ["checked", null]
222
- ])
223
- ), r(
224
- g,
225
- "label",
226
- o.tag(t),
227
- /* @__PURE__ */ new Map([["for", d]])
228
- ), i(o, g);
229
- } else if (f === "line_break")
230
- r(a, "li", "br");
231
- else if (f === "text") {
232
- const m = r(a, "li");
233
- r(m, "span", '"', /* @__PURE__ */ new Map([["class", "quote"]])), r(m, "span", `${o.text(t)}`), r(m, "span", '"', /* @__PURE__ */ new Map([["class", "quote"]]));
234
- } else if (f === "mention") {
235
- const m = r(a, "li");
236
- r(
237
- m,
238
- "span",
239
- '"Mention - ',
240
- /* @__PURE__ */ new Map([["class", "quote"]])
241
- ), r(m, "span", `${o.text(t)}`), r(m, "span", '"', /* @__PURE__ */ new Map([["class", "quote"]]));
242
- } else
243
- console.error(`Unknown node type: ${f}`);
244
- }
245
- }
246
- i(n, e);
247
- }
248
- function me(e, t, n) {
249
- const s = document.createRange();
250
- let r = I(e, t), i = I(e, n);
251
- if (r.node && i.node) {
252
- const l = r.node.compareDocumentPosition(i.node) & Node.DOCUMENT_POSITION_PRECEDING, a = r.node === i.node && i.offset < r.offset;
253
- if ((l || a) && ([r, i] = [i, r], !r.node || !i.node))
254
- throw new Error();
255
- s.setStart(r.node, r.offset), s.setEnd(i.node, i.offset);
256
- } else
257
- s.selectNodeContents(e), s.collapse();
258
- const c = document.getSelection();
259
- c && (c.removeAllRanges(), c.addRange(s));
260
- }
261
- function z(e, t, n, s) {
262
- e.innerHTML = t + "<br />", me(e, n, s);
263
- }
264
- function I(e, t, n) {
265
- const s = e.nodeName === "LI" && !e.hasChildNodes(), r = e.nodeType === Node.TEXT_NODE;
266
- if (r && e.parentElement?.hasAttribute("data-mention-type")) {
267
- const l = L(e) ? 1 : 0, a = t - l;
268
- return a <= 1 ? a === 0 ? e.previousSibling ? {
269
- node: e.previousSibling,
270
- offset: T(
271
- e.previousSibling,
272
- 1 / 0
273
- )
274
- } : {
275
- node: e.parentNode?.parentNode ?? null,
276
- offset: 0
277
- } : { node: null, offset: 0 } : { node: null, offset: t - l - 1 };
278
- } else if (r) {
279
- const l = L(e) ? 1 : 0;
280
- return Z(e) ? t === 0 ? { node: e, offset: t } : { node: null, offset: t - l } : t <= (e.textContent?.length || 0) ? { node: e, offset: t } : {
281
- node: null,
282
- offset: t - (e.textContent?.length || 0) - l
283
- };
284
- } else {
285
- if (s)
286
- return t <= (e.textContent?.length || 0) ? { node: e, offset: t } : {
287
- node: null,
288
- offset: t - (e.textContent?.length || 0)
289
- };
290
- if (e.nodeName === "BR")
291
- return t === 0 ? { node: e, offset: 0 } : {
292
- node: null,
293
- offset: t - 1
294
- };
295
- if (ge(e) && t === 0)
296
- return { node: e, offset: t };
297
- for (const c of e.childNodes) {
298
- const l = I(
299
- c,
300
- t
301
- );
302
- if (l.node)
303
- return { node: l.node, offset: l.offset };
304
- t = l.offset;
305
- }
306
- return { node: null, offset: t };
307
- }
308
- }
309
- function Q(e, t) {
310
- const n = e.childNodes.length === 1 && e.firstChild?.nodeName === "BR";
311
- if (!t || n)
312
- return [0, 0];
313
- const s = t.anchorNode && F(
314
- e,
315
- t.anchorNode,
316
- t.anchorOffset
317
- ) || 0, r = t.focusNode && F(
318
- e,
319
- t.focusNode,
320
- t.focusOffset
321
- ) || 0;
322
- return [s, r];
323
- }
324
- function T(e, t) {
325
- if (e.nodeType === Node.TEXT_NODE) {
326
- const s = L(e) ? 1 : 0;
327
- return (e.textContent?.length ?? 0) + s;
328
- } else {
329
- if (e.nodeName === "BR")
330
- return t === 0 ? 0 : 1;
331
- {
332
- let n = 0, s = 0;
333
- for (const r of e.childNodes) {
334
- if (s === t)
335
- break;
336
- n += T(r, -1), s++;
337
- }
338
- return n;
339
- }
340
- }
341
- }
342
- function Y(e, t, n) {
343
- const s = e.nodeType === Node.TEXT_NODE, r = e.parentElement?.hasAttribute("data-mention-type");
344
- if (e === t)
345
- return s ? n > (e.textContent?.length ?? 0) ? { found: !1, offset: 0 } : r ? { found: !0, offset: n === 0 ? 0 : 1 } : { found: !0, offset: n } : { found: !0, offset: T(e, n) };
346
- if (s) {
347
- const c = L(e) ? 1 : 0;
348
- return Z(e) ? { found: !1, offset: c } : r ? { found: !1, offset: 1 + c } : {
349
- found: !1,
350
- offset: (e.textContent?.length ?? 0) + c
351
- };
352
- } else {
353
- if (e.nodeName === "BR")
354
- return { found: !1, offset: 1 };
355
- {
356
- let i = 0;
357
- for (const c of e.childNodes) {
358
- const l = Y(c, t, n);
359
- if (l.found)
360
- return { found: !0, offset: i + l.offset };
361
- i += l.offset;
362
- }
363
- return { found: !1, offset: i };
364
- }
365
- }
366
- }
367
- function F(e, t, n) {
368
- if (t === e && n === e.childNodes.length - 1)
369
- return T(e, -1) - 1;
370
- const s = new Range();
371
- s.setStart(e, 0), s.setEnd(e, e.childNodes.length);
372
- const r = s.comparePoint(t, 0);
373
- if (r === -1)
374
- return 0;
375
- if (r === 1)
376
- return T(e, -1) - 1;
377
- const i = Y(e, t, n);
378
- return i.found ? i.offset : -1;
379
- }
380
- function L(e) {
381
- if (e === null) return !1;
382
- let t = e;
383
- const n = P(t.parentNode);
384
- for (; t; ) {
385
- const s = t.nextSibling;
386
- if (s && P(s) || n && s && !K(s) || s && s.nodeName === "BR")
387
- break;
388
- if (K(t))
389
- return !0;
390
- t = t.parentNode;
391
- }
392
- return !1;
393
- }
394
- const J = ["EM", "U", "STRONG", "DEL", "CODE", "A"], _e = ["OL", "UL", "LI", "PRE", "BLOCKQUOTE", "P"];
395
- function P(e) {
396
- return e === null ? !1 : J.includes(e.nodeName || "");
1
+ import { useCallback as e, useEffect as t, useMemo as n, useRef as r, useState as i } from "react";
2
+ import { HtmlSource as a, initAsync as o, new_composer_model as s, new_composer_model_from_html as c } from "@vector-im/matrix-wysiwyg-wasm";
3
+ //#region lib/useListeners/assert.ts
4
+ function l(e) {
5
+ return "inputType" in e;
6
+ }
7
+ function u(e) {
8
+ return "clipboardData" in e;
9
+ }
10
+ function d(e) {
11
+ return l(e) && e.inputType === "insertSuggestion";
12
+ }
13
+ function f(e) {
14
+ return l(e) && e.inputType === "insertAtRoomSuggestion";
15
+ }
16
+ function p(e) {
17
+ return l(e) && e.inputType == "insertLink";
18
+ }
19
+ //#endregion
20
+ //#region lib/composer.ts
21
+ function m(e, t, n, r) {
22
+ return r ? r(e, t, n) : e;
23
+ }
24
+ function h(e, t, n, r, i, o, s, c) {
25
+ let l = m(e, {
26
+ actions: r,
27
+ content: () => t.get_content_as_html(),
28
+ messageContent: () => t.get_content_as_message_html()
29
+ }, i, s);
30
+ if (!l) return;
31
+ if (u(l)) {
32
+ let e = l.clipboardData, r = e?.getData("text/html"), i = e?.getData("text/plain") ?? "";
33
+ if (r && r !== i) {
34
+ let i = e?.types.includes("application/x-vnd.google-docs-document-slice-clip+wrapped") ? a.GoogleDoc : a.UnknownExternal;
35
+ return n(t.replace_html(r, i), "replace_html_paste");
36
+ }
37
+ return n(t.replace_text(i), "replace_text_paste");
38
+ }
39
+ switch (l.inputType) {
40
+ case "insertAtRoomSuggestion":
41
+ if (o && f(l)) {
42
+ let { attributes: e } = l.data;
43
+ return e.has("data-mention-type") && e.delete("data-mention-type"), n(t.insert_at_room_mention_at_suggestion(o, e), "insert_at_room_mention_at_suggestion");
44
+ }
45
+ break;
46
+ case "insertSuggestion":
47
+ if (o && d(l)) {
48
+ let { text: e, url: r, attributes: i } = l.data;
49
+ return i.has("data-mention-type") && i.delete("data-mention-type"), n(t.insert_mention_at_suggestion(r, e, o, i), "insert_mention_at_suggestion");
50
+ }
51
+ break;
52
+ case "insertCommand":
53
+ case "insertEmoji":
54
+ if (o && l.data) {
55
+ let e = l.inputType === "insertCommand";
56
+ return n(t.replace_text_suggestion(l.data, o, e), "replace_text_suggestion");
57
+ }
58
+ break;
59
+ case "clear": return n(t.clear(), "clear");
60
+ case "deleteContentBackward": return n(t.backspace(), "backspace");
61
+ case "deleteWordBackward": return n(t.backspace_word(), "backspace_word");
62
+ case "deleteSoftLineBackward": {
63
+ let e = document.getSelection();
64
+ return e && (e.modify("extend", "backward", "lineboundary"), document.dispatchEvent(new CustomEvent("selectionchange"))), n(t.delete(), "backspace_line");
65
+ }
66
+ case "deleteContentForward": return n(t.delete(), "delete");
67
+ case "deleteWordForward": return n(t.delete_word(), "delete_word");
68
+ case "deleteByCut": return n(t.delete(), "delete");
69
+ case "formatBold": return n(t.bold(), "bold");
70
+ case "formatItalic": return n(t.italic(), "italic");
71
+ case "formatStrikeThrough": return n(t.strike_through(), "strike_through");
72
+ case "formatUnderline": return n(t.underline(), "underline");
73
+ case "formatInlineCode": return n(t.inline_code(), "inline_code");
74
+ case "historyRedo": return n(t.redo(), "redo");
75
+ case "historyUndo": return n(t.undo(), "undo");
76
+ case "insertCodeBlock": return n(t.code_block(), "code_block");
77
+ case "insertQuote": return n(t.quote(), "quote");
78
+ case "insertFromPaste": return;
79
+ case "insertOrderedList": return n(t.ordered_list(), "ordered_list");
80
+ case "insertLineBreak":
81
+ case "insertParagraph": return h(t, o, c), n(t.enter(), "enter");
82
+ case "insertReplacementText": {
83
+ let e = i.innerHTML.slice(0, i.innerHTML.length - 4);
84
+ return n(t.set_content_from_html(e), "set_content_from_html", e);
85
+ }
86
+ case "insertCompositionText":
87
+ case "insertFromComposition":
88
+ case "insertText":
89
+ if (l.data) return l.data == " " && h(t, o, c), n(t.replace_text(l.data), "replace_text", l.data);
90
+ break;
91
+ case "insertUnorderedList": return n(t.unordered_list(), "unordered_list");
92
+ case "insertLink":
93
+ if (p(l)) {
94
+ let { text: e, url: r } = l.data;
95
+ return n(e ? t.set_link_with_text(r, e, /* @__PURE__ */ new Map()) : t.set_link(r, /* @__PURE__ */ new Map()), "insertLink");
96
+ }
97
+ break;
98
+ case "removeLinks": return n(t.remove_links(), "remove_links");
99
+ case "formatIndent": return n(t.indent(), "indent");
100
+ case "formatOutdent": return n(t.unindent(), "unindent");
101
+ case "sendMessage": return null;
102
+ default: return console.error(`Unknown input type: ${l.inputType}`), console.error(e), null;
103
+ }
104
+ function h(e, t, n) {
105
+ if (n && t && t.key.key_type == 3 && t.key.custom_key_value) {
106
+ let r = n.get(t.key.custom_key_value);
107
+ r && e.replace_text_suggestion(r, t, !1);
108
+ }
109
+ }
110
+ }
111
+ //#endregion
112
+ //#region lib/dom.ts
113
+ function g(e, t) {
114
+ e.innerHTML = "";
115
+ let n = t.document(), r = 0;
116
+ function i(e, t, n, r) {
117
+ let i = document.createElement(t);
118
+ if (n && (i.innerText = n.replace("​", "~")), r) for (let [e, t] of r.entries()) {
119
+ let n = document.createAttribute(e);
120
+ t !== null && (n.value = t), i.setAttributeNode(n);
121
+ }
122
+ return e.appendChild(i), i;
123
+ }
124
+ function a(e, n) {
125
+ let o = i(n, "ul");
126
+ o.className = `group_${r % 10}`;
127
+ let s = e.children(t), c;
128
+ for (; c = s.next_child();) {
129
+ let e = c.node_type(t);
130
+ if (e === "container") {
131
+ let e = `dom_${r}`;
132
+ r++;
133
+ let n = i(o, "li");
134
+ i(n, "input", null, new Map([
135
+ ["type", "checkbox"],
136
+ ["id", e],
137
+ ["checked", null]
138
+ ])), i(n, "label", c.tag(t), new Map([["for", e]])), a(c, n);
139
+ } else if (e === "line_break") i(o, "li", "br");
140
+ else if (e === "text") {
141
+ let e = i(o, "li");
142
+ i(e, "span", "\"", new Map([["class", "quote"]])), i(e, "span", `${c.text(t)}`), i(e, "span", "\"", new Map([["class", "quote"]]));
143
+ } else if (e === "mention") {
144
+ let e = i(o, "li");
145
+ i(e, "span", "\"Mention - ", new Map([["class", "quote"]])), i(e, "span", `${c.text(t)}`), i(e, "span", "\"", new Map([["class", "quote"]]));
146
+ } else console.error(`Unknown node type: ${e}`);
147
+ }
148
+ }
149
+ a(n, e);
150
+ }
151
+ function _(e, t, n) {
152
+ let r = document.createRange(), i = y(e, t), a = y(e, n);
153
+ if (i.node && a.node) {
154
+ let e = i.node.compareDocumentPosition(a.node) & Node.DOCUMENT_POSITION_PRECEDING, t = i.node === a.node && a.offset < i.offset;
155
+ if ((e || t) && ([i, a] = [a, i], !i.node || !a.node)) throw Error();
156
+ r.setStart(i.node, i.offset), r.setEnd(a.node, a.offset);
157
+ } else r.selectNodeContents(e), r.collapse();
158
+ let o = document.getSelection();
159
+ o && (o.removeAllRanges(), o.addRange(r));
160
+ }
161
+ function v(e, t, n, r) {
162
+ e.innerHTML = t + "<br />", _(e, n, r);
163
+ }
164
+ function y(e, t, n) {
165
+ let r = e.nodeName === "LI" && !e.hasChildNodes(), i = e.nodeType === Node.TEXT_NODE;
166
+ if (i && e.parentElement?.hasAttribute("data-mention-type")) {
167
+ let n = +!!w(e), r = t - n;
168
+ return r <= 1 ? r === 0 ? e.previousSibling ? {
169
+ node: e.previousSibling,
170
+ offset: x(e.previousSibling, Infinity)
171
+ } : {
172
+ node: e.parentNode?.parentNode ?? null,
173
+ offset: 0
174
+ } : {
175
+ node: null,
176
+ offset: 0
177
+ } : {
178
+ node: null,
179
+ offset: t - n - 1
180
+ };
181
+ } else if (i) {
182
+ let n = +!!w(e);
183
+ return k(e) ? t === 0 ? {
184
+ node: e,
185
+ offset: t
186
+ } : {
187
+ node: null,
188
+ offset: t - n
189
+ } : t <= (e.textContent?.length || 0) ? {
190
+ node: e,
191
+ offset: t
192
+ } : {
193
+ node: null,
194
+ offset: t - (e.textContent?.length || 0) - n
195
+ };
196
+ } else if (r) return t <= (e.textContent?.length || 0) ? {
197
+ node: e,
198
+ offset: t
199
+ } : {
200
+ node: null,
201
+ offset: t - (e.textContent?.length || 0)
202
+ };
203
+ else if (e.nodeName === "BR") return t === 0 ? {
204
+ node: e,
205
+ offset: 0
206
+ } : {
207
+ node: null,
208
+ offset: t - 1
209
+ };
210
+ else {
211
+ if (O(e) && t === 0) return {
212
+ node: e,
213
+ offset: t
214
+ };
215
+ for (let r of e.childNodes) {
216
+ let i = y(r, t, n || e);
217
+ if (i.node) return {
218
+ node: i.node,
219
+ offset: i.offset
220
+ };
221
+ t = i.offset;
222
+ }
223
+ return {
224
+ node: null,
225
+ offset: t
226
+ };
227
+ }
228
+ }
229
+ function b(e, t) {
230
+ let n = e.childNodes.length === 1 && e.firstChild?.nodeName === "BR";
231
+ return !t || n ? [0, 0] : [t.anchorNode && C(e, t.anchorNode, t.anchorOffset) || 0, t.focusNode && C(e, t.focusNode, t.focusOffset) || 0];
232
+ }
233
+ function x(e, t) {
234
+ if (e.nodeType === Node.TEXT_NODE) {
235
+ let t = +!!w(e);
236
+ return (e.textContent?.length ?? 0) + t;
237
+ } else if (e.nodeName === "BR") return t === 0 ? 0 : 1;
238
+ else {
239
+ let n = 0, r = 0;
240
+ for (let i of e.childNodes) {
241
+ if (r === t) break;
242
+ n += x(i, -1), r++;
243
+ }
244
+ return n;
245
+ }
246
+ }
247
+ function S(e, t, n) {
248
+ let r = e.nodeType === Node.TEXT_NODE, i = e.parentElement?.hasAttribute("data-mention-type");
249
+ if (e === t) return r ? n > (e.textContent?.length ?? 0) ? {
250
+ found: !1,
251
+ offset: 0
252
+ } : i ? {
253
+ found: !0,
254
+ offset: n === 0 ? 0 : 1
255
+ } : {
256
+ found: !0,
257
+ offset: n
258
+ } : {
259
+ found: !0,
260
+ offset: x(e, n)
261
+ };
262
+ if (r) {
263
+ let t = +!!w(e);
264
+ return k(e) ? {
265
+ found: !1,
266
+ offset: t
267
+ } : i ? {
268
+ found: !1,
269
+ offset: 1 + t
270
+ } : {
271
+ found: !1,
272
+ offset: (e.textContent?.length ?? 0) + t
273
+ };
274
+ } else if (e.nodeName === "BR") return {
275
+ found: !1,
276
+ offset: 1
277
+ };
278
+ else {
279
+ let r = 0;
280
+ for (let i of e.childNodes) {
281
+ let e = S(i, t, n);
282
+ if (e.found) return {
283
+ found: !0,
284
+ offset: r + e.offset
285
+ };
286
+ r += e.offset;
287
+ }
288
+ return {
289
+ found: !1,
290
+ offset: r
291
+ };
292
+ }
293
+ }
294
+ function C(e, t, n) {
295
+ if (t === e && n === e.childNodes.length - 1) return x(e, -1) - 1;
296
+ let r = new Range();
297
+ r.setStart(e, 0), r.setEnd(e, e.childNodes.length);
298
+ let i = r.comparePoint(t, 0);
299
+ if (i === -1) return 0;
300
+ if (i === 1) return x(e, -1) - 1;
301
+ let a = S(e, t, n);
302
+ return a.found ? a.offset : -1;
303
+ }
304
+ function w(e) {
305
+ if (e === null) return !1;
306
+ let t = e, n = E(t.parentNode);
307
+ for (; t;) {
308
+ let e = t.nextSibling;
309
+ if (e && E(e) || n && e && !D(e) || e && e.nodeName === "BR") break;
310
+ if (D(t)) return !0;
311
+ t = t.parentNode;
312
+ }
313
+ return !1;
314
+ }
315
+ var T = [
316
+ "EM",
317
+ "U",
318
+ "STRONG",
319
+ "DEL",
320
+ "CODE",
321
+ "A"
322
+ ], ee = [
323
+ "OL",
324
+ "UL",
325
+ "LI",
326
+ "PRE",
327
+ "BLOCKQUOTE",
328
+ "P"
329
+ ];
330
+ function E(e) {
331
+ return e === null ? !1 : T.includes(e.nodeName || "");
332
+ }
333
+ function D(e) {
334
+ return ee.includes(e.nodeName || "");
335
+ }
336
+ function O(e) {
337
+ return T.includes(e.nodeName) && e.textContent?.length === 0;
338
+ }
339
+ function k(e) {
340
+ let t = e.parentNode?.childNodes.length === 1, n = e.parentNode?.nodeName === "P", r = e.textContent === "\xA0";
341
+ return n && t && r;
342
+ }
343
+ //#endregion
344
+ //#region lib/constants.ts
345
+ var A = [
346
+ "bold",
347
+ "italic",
348
+ "strikeThrough",
349
+ "underline",
350
+ "undo",
351
+ "redo",
352
+ "orderedList",
353
+ "unorderedList",
354
+ "inlineCode",
355
+ "clear",
356
+ "link",
357
+ "codeBlock",
358
+ "quote",
359
+ "indent",
360
+ "unindent"
361
+ ], j = [
362
+ "@",
363
+ "#",
364
+ "/",
365
+ "",
366
+ ":"
367
+ ];
368
+ //#endregion
369
+ //#region lib/useListeners/utils.ts
370
+ function M() {
371
+ return A.reduce((e, t) => (e[t] = "enabled", e), {});
372
+ }
373
+ function N(e) {
374
+ let t = {};
375
+ for (let [n, r] of e) t[n.substring(0, 1).toLowerCase() + n.substring(1)] = r.toLowerCase();
376
+ return t;
377
+ }
378
+ //#endregion
379
+ //#region lib/utils.ts
380
+ function P() {
381
+ let e = navigator.userAgent.toLowerCase();
382
+ return e.includes("iphone") || e.includes("ipad") ? "iOS" : e.includes("android") ? "Android" : e.includes("win") ? "Windows" : e.includes("mac") ? "macOS" : e.includes("linux") ? "Linux" : null;
383
+ }
384
+ //#endregion
385
+ //#region lib/useListeners/event.ts
386
+ function F(e, t, n, r) {
387
+ n?.preventDefault(), n?.stopPropagation(), e.dispatchEvent(new CustomEvent("wysiwygInput", { detail: {
388
+ blockType: t,
389
+ data: r
390
+ } }));
391
+ }
392
+ function te(e, t, n, r, i) {
393
+ if (e.shiftKey && e.altKey) switch (e.key) {
394
+ case "5": return "formatStrikeThrough";
395
+ }
396
+ let a = P();
397
+ if ((a === "Windows" || a === "Linux") && e.ctrlKey) switch (e.key) {
398
+ case "Backspace": return "deleteWordBackward";
399
+ }
400
+ if (e.ctrlKey || e.metaKey) switch (e.key) {
401
+ case "b": return "formatBold";
402
+ case "i": return "formatItalic";
403
+ case "u": return "formatUnderline";
404
+ case "e": return "formatInlineCode";
405
+ case "y": return "historyRedo";
406
+ case "z": return "historyUndo";
407
+ case "Z": return "historyRedo";
408
+ case "Enter": return "sendMessage";
409
+ case "Backspace": return "deleteSoftLineBackward";
410
+ }
411
+ return m(e, {
412
+ actions: n,
413
+ content: () => t.get_content_as_html(),
414
+ messageContent: () => t.get_content_as_message_html()
415
+ }, r, i), null;
416
+ }
417
+ function ne(e, t, n, r, i) {
418
+ let a = te(e, n, r, t, i);
419
+ a && F(t, a, e);
420
+ }
421
+ function I(e) {
422
+ return N(e.action_states);
423
+ }
424
+ function L(e, t, n, r, i, a, o, s, c) {
425
+ let l = h(e, n, i.traceAction, a, t, o, s, c);
426
+ if (l) {
427
+ let e = l.text_update().replace_all;
428
+ e && (v(t, e.replacement_html, e.start_utf16_codeunit, e.end_utf16_codeunit), i.setEditorHtml(e.replacement_html)), t.focus(), r && g(r, n);
429
+ let a = l.menu_state().update(), o = l.menu_action().suggestion()?.suggestion_pattern, s = a ? I(a) : null, c = o || null;
430
+ return {
431
+ content: e?.replacement_html,
432
+ actionStates: s,
433
+ suggestion: c
434
+ };
435
+ }
436
+ }
437
+ function R(e, t, { traceAction: n, getSelectionAccordingToActions: r }) {
438
+ let [i, a] = b(e, document.getSelection()), o = t.selection_start(), s = t.selection_end(), [c, l] = r();
439
+ if (i === o && i === c && a === s && a === l || i === s && i === l && a === o && a === c) return;
440
+ let u = t.select(i, a);
441
+ n(null, "select", i, a);
442
+ let d = u.menu_state().update();
443
+ if (d) return I(d);
444
+ }
445
+ //#endregion
446
+ //#region lib/useListeners/useListeners.ts
447
+ function z(e, n, a, o, s, c, d, f) {
448
+ let [p, m] = i({
449
+ content: null,
450
+ actionStates: M(),
451
+ suggestion: null
452
+ }), h = r(void 0), [g, _] = i(!1);
453
+ return t(() => {
454
+ a && (m({
455
+ content: a.get_content_as_html(),
456
+ actionStates: N(a.action_states()),
457
+ suggestion: null
458
+ }), h.current = a.get_content_as_plain_text());
459
+ }, [a]), t(() => {
460
+ let t = e.current;
461
+ if (!a || !t) return;
462
+ let r = (e) => {
463
+ try {
464
+ let r = L(e, t, a, n.current, o, s, p.suggestion, d, f);
465
+ r && (m((e) => ({
466
+ content: r.content === void 0 ? e.content : r.content,
467
+ actionStates: r.actionStates || e.actionStates,
468
+ suggestion: r.suggestion
469
+ })), h.current = a.get_content_as_plain_text());
470
+ } catch {
471
+ c(h.current);
472
+ }
473
+ }, i = (e) => {
474
+ l(e) && !e.isComposing && r(e);
475
+ };
476
+ t.addEventListener("input", i);
477
+ let g = (e) => {
478
+ let t = l(e) && e.inputType === "insertFromPaste" && e.dataTransfer !== null;
479
+ (u(e) || t) && (e.preventDefault(), e.stopPropagation(), r(e));
480
+ };
481
+ t.addEventListener("paste", g);
482
+ let v = ((e) => {
483
+ r({
484
+ inputType: e.detail.blockType,
485
+ data: e.detail.data
486
+ });
487
+ });
488
+ t.addEventListener("wysiwygInput", v);
489
+ let y = (e) => {
490
+ ne(e, t, a, s, d);
491
+ };
492
+ t.addEventListener("keydown", y);
493
+ let b = () => {
494
+ try {
495
+ let e = R(t, a, o);
496
+ e && m(({ content: t, suggestion: n }) => ({
497
+ content: t,
498
+ actionStates: e,
499
+ suggestion: n
500
+ })), h.current = a.get_content_as_plain_text();
501
+ } catch {
502
+ c(h.current);
503
+ }
504
+ };
505
+ document.addEventListener("selectionchange", b);
506
+ let x = (e) => {
507
+ if (!(l(e) && e.isComposing)) return g(e);
508
+ };
509
+ t.addEventListener("beforeinput", x);
510
+ let S = (e) => {
511
+ i(new InputEvent("input", {
512
+ data: e.data,
513
+ inputType: "insertCompositionText"
514
+ }));
515
+ };
516
+ return t.addEventListener("compositionend", S), _(!0), () => {
517
+ _(!1), t.removeEventListener("input", i), t.removeEventListener("paste", g), t.removeEventListener("wysiwygInput", v), t.removeEventListener("keydown", y), t.removeEventListener("beforeinput", x), t.removeEventListener("compositionend", S), document.removeEventListener("selectionchange", b);
518
+ };
519
+ }, [
520
+ e,
521
+ f,
522
+ a,
523
+ s,
524
+ n,
525
+ o,
526
+ d,
527
+ c,
528
+ h,
529
+ p.suggestion
530
+ ]), {
531
+ areListenersReady: g,
532
+ ...p
533
+ };
534
+ }
535
+ //#endregion
536
+ //#region lib/useFormattingFunctions.ts
537
+ function B(e, t) {
538
+ return n(() => {
539
+ let n = (t, n) => {
540
+ e.current && F(e.current, t, void 0, n);
541
+ };
542
+ return {
543
+ bold: () => n("formatBold"),
544
+ italic: () => n("formatItalic"),
545
+ strikeThrough: () => n("formatStrikeThrough"),
546
+ underline: () => n("formatUnderline"),
547
+ undo: () => n("historyUndo"),
548
+ redo: () => n("historyRedo"),
549
+ orderedList: () => n("insertOrderedList"),
550
+ unorderedList: () => n("insertUnorderedList"),
551
+ inlineCode: () => n("formatInlineCode"),
552
+ clear: () => n("clear"),
553
+ insertText: (e) => n("insertText", e),
554
+ link: (e, t) => n("insertLink", {
555
+ url: e,
556
+ text: t
557
+ }),
558
+ removeLinks: () => n("removeLinks"),
559
+ getLink: () => t?.get_link_action()?.edit_link?.url || "",
560
+ codeBlock: () => n("insertCodeBlock"),
561
+ quote: () => n("insertQuote"),
562
+ indent: () => n("formatIndent"),
563
+ unindent: () => n("formatOutdent"),
564
+ mention: (e, t, r) => n("insertSuggestion", {
565
+ url: e,
566
+ text: t,
567
+ attributes: r
568
+ }),
569
+ command: (e) => n("insertCommand", e),
570
+ emoji: (e) => n("insertEmoji", e),
571
+ mentionAtRoom: (e) => n("insertAtRoomSuggestion", { attributes: e })
572
+ };
573
+ }, [e, t]);
574
+ }
575
+ //#endregion
576
+ //#region lib/useComposerModel.ts
577
+ var V = null;
578
+ async function H() {
579
+ return V ||= o(), V;
580
+ }
581
+ function U(n, r, a) {
582
+ let [o, l] = i(null), u = e(async (e) => {
583
+ await H();
584
+ let t;
585
+ if (e) try {
586
+ let r = c(e, 0, e.length);
587
+ if (t = r, n.current) {
588
+ let e = r.get_content_as_html();
589
+ v(n.current, e, 0, e.length);
590
+ }
591
+ } catch {
592
+ t = s();
593
+ }
594
+ else t = s();
595
+ l(t);
596
+ }, [l, n]);
597
+ return t(() => {
598
+ o && a && o.set_custom_suggestion_patterns(a);
599
+ }, [o, a]), t(() => {
600
+ n.current && u(r);
601
+ }, [
602
+ n,
603
+ u,
604
+ r
605
+ ]), {
606
+ composerModel: o,
607
+ onError: u
608
+ };
609
+ }
610
+ //#endregion
611
+ //#region lib/useTestCases/assert.ts
612
+ function W(e) {
613
+ return e[0] === "select";
614
+ }
615
+ //#endregion
616
+ //#region lib/useTestCases/utils.ts
617
+ function G(e, t, n) {
618
+ return (r, i, a, o) => !e || !n ? r : (console.debug(o === void 0 ? a === void 0 ? `composer_model.${i}()` : `composer_model.${i}(${a})` : `composer_model.${i}(${a}, ${o})`), t.push([
619
+ i,
620
+ a,
621
+ o
622
+ ]), q(e, n, r, t), r);
397
623
  }
398
624
  function K(e) {
399
- return _e.includes(e.nodeName || "");
625
+ return () => {
626
+ for (let t = e.length - 1; t >= 0; t--) {
627
+ let n = e[t];
628
+ if (W(n)) return [n[1], n[2]];
629
+ }
630
+ return [-1, -1];
631
+ };
632
+ }
633
+ function q(e, t, n, r) {
634
+ n && n.text_update(), e.innerText = J(r, t.to_example_format()), e.scrollTo(0, e.scrollHeight - e.clientHeight);
635
+ }
636
+ function J(e, t) {
637
+ let n = "";
638
+ function r(e, t, r) {
639
+ e === "select" ? n += `model.select(Location::from(${t}), Location::from(${r}));\n` : r === void 0 ? e === "replace_text" ? n += `model.${e}("${t ?? ""}");\n` : e === "send" ? n += `// Send: ${t ?? ""}\n` : n += `model.${e}(${t ?? ""});\n` : n += `model.${e}(${t ?? ""}, ${r});\n`;
640
+ }
641
+ function i() {
642
+ let e = Y(s, c[0], c[1]);
643
+ n += `let mut model = cm("${e}");\n`;
644
+ }
645
+ let a = null, o = !0, s = "", c = [0, 0];
646
+ for (let t of e) {
647
+ let [e, l, u] = t;
648
+ if (o) e === "replace_text" ? s += re(l) : W(t) ? c = [t[1], t[2]] : (o = !1, i(), r(e, l, u));
649
+ else if (a === "select" && e === "select") {
650
+ let t = n.lastIndexOf("\n", n.length - 2);
651
+ t > -1 && (n = n.substring(0, t) + "\n", r(e, l, u));
652
+ } else r(e, l, u);
653
+ a = e;
654
+ }
655
+ return o && i(), n += `assert_eq!(tx(&model), "${t}");\n`, n;
400
656
  }
401
- function ge(e) {
402
- return J.includes(e.nodeName) && e.textContent?.length === 0;
403
- }
404
- function Z(e) {
405
- const t = e.parentNode?.childNodes.length === 1, n = e.parentNode?.nodeName === "P", s = e.textContent === " ";
406
- return n && t && s;
407
- }
408
- const pe = [
409
- "bold",
410
- "italic",
411
- "strikeThrough",
412
- "underline",
413
- "undo",
414
- "redo",
415
- "orderedList",
416
- "unorderedList",
417
- "inlineCode",
418
- "clear",
419
- "link",
420
- "codeBlock",
421
- "quote",
422
- "indent",
423
- "unindent"
424
- ], he = ["@", "#", "/", ""];
425
- function ye() {
426
- return pe.reduce((e, t) => (e[t] = "enabled", e), {});
427
- }
428
- function M(e) {
429
- const t = {};
430
- for (const [n, s] of e)
431
- t[n.substring(0, 1).toLowerCase() + n.substring(1)] = s.toLowerCase();
432
- return t;
433
- }
434
- function Ee() {
435
- const e = navigator.userAgent.toLowerCase();
436
- return e.includes("iphone") || e.includes("ipad") ? "iOS" : e.includes("android") ? "Android" : e.includes("win") ? "Windows" : e.includes("mac") ? "macOS" : e.includes("linux") ? "Linux" : null;
437
- }
438
- function ee(e, t, n, s) {
439
- n?.preventDefault(), n?.stopPropagation(), e.dispatchEvent(
440
- new CustomEvent("wysiwygInput", { detail: { blockType: t, data: s } })
441
- );
442
- }
443
- function ke(e, t, n, s, r) {
444
- if (e.shiftKey && e.altKey)
445
- switch (e.key) {
446
- case "5":
447
- return "formatStrikeThrough";
448
- }
449
- const i = Ee();
450
- if ((i === "Windows" || i === "Linux") && e.ctrlKey)
451
- switch (e.key) {
452
- case "Backspace":
453
- return "deleteWordBackward";
454
- }
455
- if (e.ctrlKey || e.metaKey)
456
- switch (e.key) {
457
- case "b":
458
- return "formatBold";
459
- case "i":
460
- return "formatItalic";
461
- case "u":
462
- return "formatUnderline";
463
- case "e":
464
- return "formatInlineCode";
465
- case "y":
466
- return "historyRedo";
467
- case "z":
468
- return "historyUndo";
469
- case "Z":
470
- return "historyRedo";
471
- case "Enter":
472
- return "sendMessage";
473
- case "Backspace":
474
- return "deleteSoftLineBackward";
475
- }
476
- return X(
477
- e,
478
- {
479
- actions: n,
480
- content: () => t.get_content_as_html(),
481
- messageContent: () => t.get_content_as_message_html()
482
- },
483
- s,
484
- r
485
- ), null;
486
- }
487
- function xe(e, t, n, s, r) {
488
- const i = ke(
489
- e,
490
- n,
491
- s,
492
- t,
493
- r
494
- );
495
- i && ee(t, i, e);
496
- }
497
- function te(e) {
498
- return M(e.action_states);
499
- }
500
- function Ce(e, t, n, s, r, i, c, l, a) {
501
- const u = fe(
502
- e,
503
- n,
504
- r.traceAction,
505
- i,
506
- t,
507
- c,
508
- l,
509
- a
510
- );
511
- if (u) {
512
- const o = u.text_update().replace_all;
513
- o && (z(
514
- t,
515
- o.replacement_html,
516
- o.start_utf16_codeunit,
517
- o.end_utf16_codeunit
518
- ), r.setEditorHtml(o.replacement_html)), t.focus(), s && de(s, n);
519
- const f = u.menu_state().update(), m = u.menu_action().suggestion()?.suggestion_pattern, d = f ? te(f) : null, g = m || null;
520
- return {
521
- content: o?.replacement_html,
522
- actionStates: d,
523
- suggestion: g
524
- };
525
- }
526
- }
527
- function Te(e, t, { traceAction: n, getSelectionAccordingToActions: s }) {
528
- const [r, i] = Q(e, document.getSelection()), c = t.selection_start(), l = t.selection_end(), [a, u] = s();
529
- if (r === c && r === a && i === l && i === u || r === l && r === u && i === c && i === a)
530
- return;
531
- const o = t.select(r, i);
532
- n(null, "select", r, i);
533
- const f = o.menu_state().update();
534
- if (f)
535
- return te(f);
536
- }
537
- function Se(e, t, n, s, r, i, c, l) {
538
- const [a, u] = E({
539
- content: null,
540
- actionStates: ye(),
541
- suggestion: null
542
- }), o = b(void 0), [f, m] = E(!1);
543
- return h(() => {
544
- n && (u({
545
- content: n.get_content_as_html(),
546
- actionStates: M(
547
- n.action_states()
548
- ),
549
- suggestion: null
550
- }), o.current = n.get_content_as_plain_text());
551
- }, [n]), h(() => {
552
- const d = e.current;
553
- if (!n || !d)
554
- return;
555
- const g = (_) => {
556
- try {
557
- const p = Ce(
558
- _,
559
- d,
560
- n,
561
- t.current,
562
- s,
563
- r,
564
- a.suggestion,
565
- c,
566
- l
567
- );
568
- p && (u((k) => {
569
- const re = p.content !== void 0 ? p.content : k.content, ie = p.actionStates || k.actionStates, oe = p.suggestion;
570
- return {
571
- content: re,
572
- actionStates: ie,
573
- suggestion: oe
574
- };
575
- }), o.current = n.get_content_as_plain_text());
576
- } catch {
577
- i(o.current);
578
- }
579
- }, S = (_) => {
580
- y(_) && !_.isComposing && g(_);
581
- };
582
- d.addEventListener("input", S);
583
- const v = (_) => {
584
- const p = y(_) && _.inputType === "insertFromPaste" && _.dataTransfer !== null;
585
- (V(_) || p) && (_.preventDefault(), _.stopPropagation(), g(_));
586
- };
587
- d.addEventListener("paste", v);
588
- const R = (_) => {
589
- g({
590
- inputType: _.detail.blockType,
591
- data: _.detail.data
592
- });
593
- };
594
- d.addEventListener("wysiwygInput", R);
595
- const B = (_) => {
596
- xe(
597
- _,
598
- d,
599
- n,
600
- r,
601
- c
602
- );
603
- };
604
- d.addEventListener("keydown", B);
605
- const D = () => {
606
- try {
607
- const _ = Te(
608
- d,
609
- n,
610
- s
611
- );
612
- _ && u(({ content: p, suggestion: k }) => ({
613
- content: p,
614
- actionStates: _,
615
- suggestion: k
616
- })), o.current = n.get_content_as_plain_text();
617
- } catch {
618
- i(o.current);
619
- }
620
- };
621
- document.addEventListener("selectionchange", D);
622
- const $ = (_) => {
623
- if (!(y(_) && _.isComposing))
624
- return v(_);
625
- };
626
- d.addEventListener("beforeinput", $);
627
- const U = (_) => {
628
- const p = new InputEvent("input", {
629
- data: _.data,
630
- inputType: "insertCompositionText"
631
- });
632
- S(p);
633
- };
634
- return d.addEventListener("compositionend", U), m(!0), () => {
635
- m(!1), d.removeEventListener("input", S), d.removeEventListener("paste", v), d.removeEventListener("wysiwygInput", R), d.removeEventListener("keydown", B), d.removeEventListener("beforeinput", $), d.removeEventListener("compositionend", U), document.removeEventListener("selectionchange", D);
636
- };
637
- }, [
638
- e,
639
- l,
640
- n,
641
- r,
642
- t,
643
- s,
644
- c,
645
- i,
646
- o,
647
- a.suggestion
648
- ]), { areListenersReady: f, ...a };
649
- }
650
- function we(e, t) {
651
- return C(() => {
652
- const s = (r, i) => {
653
- e.current && ee(
654
- e.current,
655
- r,
656
- void 0,
657
- i
658
- );
659
- };
660
- return {
661
- bold: () => s("formatBold"),
662
- italic: () => s("formatItalic"),
663
- strikeThrough: () => s("formatStrikeThrough"),
664
- underline: () => s("formatUnderline"),
665
- undo: () => s("historyUndo"),
666
- redo: () => s("historyRedo"),
667
- orderedList: () => s("insertOrderedList"),
668
- unorderedList: () => s("insertUnorderedList"),
669
- inlineCode: () => s("formatInlineCode"),
670
- clear: () => s("clear"),
671
- insertText: (r) => s("insertText", r),
672
- link: (r, i) => s("insertLink", { url: r, text: i }),
673
- removeLinks: () => s("removeLinks"),
674
- getLink: () => t?.get_link_action()?.edit_link?.url || "",
675
- codeBlock: () => s("insertCodeBlock"),
676
- quote: () => s("insertQuote"),
677
- indent: () => s("formatIndent"),
678
- unindent: () => s("formatOutdent"),
679
- mention: (r, i, c) => s("insertSuggestion", { url: r, text: i, attributes: c }),
680
- command: (r) => s("insertCommand", r),
681
- mentionAtRoom: (r) => s("insertAtRoomSuggestion", { attributes: r })
682
- };
683
- }, [e, t]);
684
- }
685
- let W = !1, N = !1;
686
- async function O() {
687
- if (N)
688
- return Promise.resolve();
689
- if (W)
690
- return new Promise((e) => {
691
- function t() {
692
- N && e(), setTimeout(t, 200);
693
- }
694
- t();
695
- });
696
- W = !0, await ae(), N = !0;
697
- }
698
- function Le(e, t, n) {
699
- const [s, r] = E(
700
- null
701
- ), i = A(
702
- async (c) => {
703
- await O();
704
- let l;
705
- if (c)
706
- try {
707
- const a = G(
708
- c,
709
- 0,
710
- c.length
711
- );
712
- if (l = a, e.current) {
713
- const u = a.get_content_as_html();
714
- z(
715
- e.current,
716
- u,
717
- 0,
718
- u.length
719
- );
720
- }
721
- } catch {
722
- l = w();
723
- }
724
- else
725
- l = w();
726
- r(l);
727
- },
728
- [r, e]
729
- );
730
- return h(() => {
731
- s && n && s.set_custom_suggestion_patterns(
732
- n
733
- );
734
- }, [s, n]), h(() => {
735
- e.current && i(t);
736
- }, [e, i, t]), { composerModel: s, onError: i };
737
- }
738
- function ne(e) {
739
- return e[0] === "select";
740
- }
741
- function be(e, t, n) {
742
- return (s, r, i, c) => (!e || !n || (console.debug(c !== void 0 ? `composer_model.${r}(${i}, ${c})` : i !== void 0 ? `composer_model.${r}(${i})` : `composer_model.${r}()`), t.push([r, i, c]), se(e, n, s, t)), s);
743
- }
744
- function ve(e) {
745
- return () => {
746
- for (let t = e.length - 1; t >= 0; t--) {
747
- const n = e[t];
748
- if (ne(n))
749
- return [n[1], n[2]];
750
- }
751
- return [-1, -1];
752
- };
753
- }
754
- function se(e, t, n, s) {
755
- n && n.text_update(), e.innerText = Ne(
756
- s,
757
- t.to_example_format()
758
- ), e.scrollTo(0, e.scrollHeight - e.clientHeight);
759
- }
760
- function Ne(e, t) {
761
- let n = "";
762
- function s(u, o, f) {
763
- u === "select" ? n += `model.select(Location::from(${o}), Location::from(${f}));
764
- ` : f !== void 0 ? n += `model.${u}(${o ?? ""}, ${f});
765
- ` : u === "replace_text" ? n += `model.${u}("${o ?? ""}");
766
- ` : u === "send" ? n += `// Send: ${o ?? ""}
767
- ` : n += `model.${u}(${o ?? ""});
768
- `;
769
- }
770
- function r() {
771
- const u = Ae(l, a[0], a[1]);
772
- n += `let mut model = cm("${u}");
773
- `;
774
- }
775
- let i = null, c = !0, l = "", a = [0, 0];
776
- for (const u of e) {
777
- const [o, f, m] = u;
778
- if (c)
779
- o === "replace_text" ? l += Oe(f) : ne(u) ? a = [u[1], u[2]] : (c = !1, r(), s(o, f, m));
780
- else if (i === "select" && o === "select") {
781
- const d = n.lastIndexOf(`
782
- `, n.length - 2);
783
- d > -1 && (n = n.substring(0, d) + `
784
- `, s(o, f, m));
785
- } else
786
- s(o, f, m);
787
- i = o;
788
- }
789
- return c && r(), n += `assert_eq!(tx(&model), "${t}");
790
- `, n;
791
- }
792
- function Ae(e, t, n) {
793
- const s = G(e, -1, -1);
794
- return s.select(t, n), s.to_example_format();
795
- }
796
- function Ie(e, t, n, s) {
797
- const [r, i] = Q(e, document.getSelection()), c = [
798
- ["replace_text", s],
799
- ["select", r, i]
800
- ];
801
- return se(t, n, null, c), c;
802
- }
803
- function Oe(e) {
804
- if (!e)
805
- return "";
806
- const t = document.createElement("p");
807
- return t.appendChild(document.createTextNode(e.toString())), t.innerHTML;
808
- }
809
- function Re(e, t) {
810
- const n = b(null), [s, r] = E([]), [i, c] = E(""), l = A(
811
- (m) => {
812
- n.current && c(m);
813
- },
814
- [n]
815
- ), a = C(
816
- () => be(n.current, s, t),
817
- [n, s, t]
818
- ), u = C(
819
- () => ve(s),
820
- [s]
821
- ), o = A(
822
- () => e.current && n.current && t && r(
823
- Ie(
824
- e.current,
825
- n.current,
826
- t,
827
- i
828
- )
829
- ),
830
- [e, n, t, i]
831
- ), f = C(
832
- () => ({
833
- traceAction: a,
834
- getSelectionAccordingToActions: u,
835
- onResetTestCase: o,
836
- setEditorHtml: l
837
- }),
838
- [
839
- a,
840
- u,
841
- o,
842
- l
843
- ]
844
- );
845
- return {
846
- testRef: n,
847
- utilities: f
848
- };
849
- }
850
- function Be(e) {
851
- return he[e.key.key_type] || "";
852
- }
853
- function De(e) {
854
- switch (e.key.key_type) {
855
- case 0:
856
- case 1:
857
- return "mention";
858
- case 2:
859
- return "command";
860
- case 3:
861
- return "custom";
862
- default:
863
- return "unknown";
864
- }
865
- }
866
- function $e(e) {
867
- return e === null ? e : {
868
- text: e.text,
869
- keyChar: Be(e),
870
- type: De(e)
871
- };
872
- }
873
- const x = `
874
- `, Ue = (e) => {
875
- let t = e;
876
- return t.endsWith(x) && (t = t.slice(0, -1)), t.replaceAll(/\\/g, "");
877
- };
878
- async function Ge(e, t) {
879
- if (e.length === 0)
880
- return "";
881
- await O();
882
- const n = w();
883
- n.set_content_from_html(e);
884
- const s = t ? n.get_content_as_message_markdown() : n.get_content_as_markdown();
885
- return Ue(s);
886
- }
887
- async function Ve(e, t) {
888
- if (e.length === 0)
889
- return "";
890
- await O();
891
- const n = He(e), s = w();
892
- return s.set_content_from_markdown(n), t ? s.get_content_as_message_html() : s.get_content_as_html();
893
- }
894
- function He(e) {
895
- const { body: t } = new DOMParser().parseFromString(
896
- e,
897
- "text/html"
898
- ), n = document.createNodeIterator(
899
- t,
900
- void 0,
901
- Pe
902
- );
903
- let s = n.nextNode(), r = "";
904
- for (; s !== null; ) {
905
- const i = s.nodeName === "#text", c = s.nodeName === "A", l = s.nodeName === "DIV" && s.childNodes.length === 1 && s.firstChild?.nodeName === "BR", a = !Fe.includes(s.nodeName);
906
- if (l)
907
- r += x;
908
- else if (i) {
909
- let u = s.textContent;
910
- q(s) && (u += x), r += u;
911
- } else if (c) {
912
- let u = s.firstChild?.parentElement?.outerHTML ?? "";
913
- q(s) && (u += x), r += u;
914
- } else a && console.debug(`Converting unexpected node type ${s.nodeName}`);
915
- s = n.nextNode();
916
- }
917
- return r.endsWith(x.repeat(2)) && (r = r.slice(0, -1)), r;
918
- }
919
- const Fe = ["#text", "BR", "A", "DIV", "BODY"];
920
- function q(e) {
921
- return (e.nextSibling || e.parentElement?.nextSibling)?.nodeName === "DIV";
922
- }
923
- function Pe(e) {
924
- return e.nodeName === "#text" && e.parentElement?.hasAttribute("data-mention-type") ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_ACCEPT;
925
- }
926
- function Ke(e, t = !1) {
927
- h(() => {
928
- if (t) {
929
- const n = setTimeout(() => e.current?.focus(), 200);
930
- return () => clearTimeout(n);
931
- }
932
- }, [e, t]);
933
- }
934
- function We() {
935
- const e = b(null);
936
- return h(() => {
937
- e.current?.childElementCount || e.current?.appendChild(document.createElement("br"));
938
- }, [e]), e;
939
- }
940
- function j(e) {
941
- const t = e?.keys();
942
- return t ? Array.from(t) : [];
943
- }
944
- function Xe(e) {
945
- const t = We(), n = b(null), [s, r] = E(
946
- j(e?.emojiSuggestions)
947
- );
948
- h(() => {
949
- r(j(e?.emojiSuggestions));
950
- }, [e?.emojiSuggestions]);
951
- const { composerModel: i, onError: c } = Le(
952
- t,
953
- e?.initialContent,
954
- s
955
- ), { testRef: l, utilities: a } = Re(
956
- t,
957
- i
958
- ), u = we(t, i), { content: o, actionStates: f, areListenersReady: m, suggestion: d } = Se(
959
- t,
960
- n,
961
- i,
962
- a,
963
- u,
964
- c,
965
- e?.inputEventProcessor,
966
- e?.emojiSuggestions
967
- );
968
- Ke(t, e?.isAutoFocusEnabled);
969
- const g = C(
970
- () => $e(d),
971
- [d]
972
- );
973
- return {
974
- ref: t,
975
- isWysiwygReady: m,
976
- wysiwyg: u,
977
- content: o,
978
- actionStates: f,
979
- debug: {
980
- modelRef: n,
981
- testRef: l,
982
- resetTestCase: a.onResetTestCase,
983
- traceAction: a.traceAction
984
- },
985
- suggestion: g,
986
- messageContent: i?.get_content_as_message_html() ?? null
987
- };
988
- }
989
- export {
990
- O as initOnce,
991
- Ve as plainToRich,
992
- Ge as richToPlain,
993
- Xe as useWysiwyg
657
+ function Y(e, t, n) {
658
+ let r = c(e, -1, -1);
659
+ return r.select(t, n), r.to_example_format();
660
+ }
661
+ function X(e, t, n, r) {
662
+ let [i, a] = b(e, document.getSelection()), o = [["replace_text", r], [
663
+ "select",
664
+ i,
665
+ a
666
+ ]];
667
+ return q(t, n, null, o), o;
668
+ }
669
+ function re(e) {
670
+ if (!e) return "";
671
+ let t = document.createElement("p");
672
+ return t.appendChild(document.createTextNode(e.toString())), t.innerHTML;
673
+ }
674
+ //#endregion
675
+ //#region lib/useTestCases/useTestCases.ts
676
+ function ie(t, a) {
677
+ let o = r(null), [s, c] = i([]), [l, u] = i(""), d = e((e) => {
678
+ o.current && u(e);
679
+ }, [o]), f = n(() => G(o.current, s, a), [
680
+ o,
681
+ s,
682
+ a
683
+ ]), p = n(() => K(s), [s]), m = e(() => t.current && o.current && a && c(X(t.current, o.current, a, l)), [
684
+ t,
685
+ o,
686
+ a,
687
+ l
688
+ ]);
689
+ return {
690
+ testRef: o,
691
+ utilities: n(() => ({
692
+ traceAction: f,
693
+ getSelectionAccordingToActions: p,
694
+ onResetTestCase: m,
695
+ setEditorHtml: d
696
+ }), [
697
+ f,
698
+ p,
699
+ m,
700
+ d
701
+ ])
702
+ };
703
+ }
704
+ //#endregion
705
+ //#region lib/suggestion.ts
706
+ function ae(e) {
707
+ return j[e.key.key_type] || "";
708
+ }
709
+ function oe(e) {
710
+ switch (e.key.key_type) {
711
+ case 0:
712
+ case 1: return "mention";
713
+ case 2: return "command";
714
+ case 3: return "custom";
715
+ case 4: return "emoji";
716
+ default: return "unknown";
717
+ }
718
+ }
719
+ function se(e) {
720
+ return e === null ? e : {
721
+ text: e.text,
722
+ keyChar: ae(e),
723
+ type: oe(e)
724
+ };
725
+ }
726
+ //#endregion
727
+ //#region lib/conversion.ts
728
+ var Z = "\n", ce = (e) => {
729
+ let t = e;
730
+ return t.endsWith(Z) && (t = t.slice(0, -1)), t.replaceAll(/\\/g, "");
994
731
  };
732
+ async function le(e, t) {
733
+ if (e.length === 0) return "";
734
+ await H();
735
+ let n = s();
736
+ return n.set_content_from_html(e), ce(t ? n.get_content_as_message_markdown() : n.get_content_as_markdown());
737
+ }
738
+ async function ue(e, t) {
739
+ if (e.length === 0) return "";
740
+ await H();
741
+ let n = de(e), r = s();
742
+ return r.set_content_from_markdown(n), t ? r.get_content_as_message_html() : r.get_content_as_html();
743
+ }
744
+ function de(e) {
745
+ let { body: t } = new DOMParser().parseFromString(e, "text/html"), n = document.createNodeIterator(t, void 0, pe), r = n.nextNode(), i = "";
746
+ for (; r !== null;) {
747
+ let e = r.nodeName === "#text", t = r.nodeName === "A", a = r.nodeName === "DIV" && r.childNodes.length === 1 && r.firstChild?.nodeName === "BR", o = !fe.includes(r.nodeName);
748
+ if (a) i += Z;
749
+ else if (e) {
750
+ let e = r.textContent;
751
+ Q(r) && (e += Z), i += e;
752
+ } else if (t) {
753
+ let e = r.firstChild?.parentElement?.outerHTML ?? "";
754
+ Q(r) && (e += Z), i += e;
755
+ } else o && console.debug(`Converting unexpected node type ${r.nodeName}`);
756
+ r = n.nextNode();
757
+ }
758
+ return i.endsWith(Z.repeat(2)) && (i = i.slice(0, -1)), i;
759
+ }
760
+ var fe = [
761
+ "#text",
762
+ "BR",
763
+ "A",
764
+ "DIV",
765
+ "BODY"
766
+ ];
767
+ function Q(e) {
768
+ return (e.nextSibling || e.parentElement?.nextSibling)?.nodeName === "DIV";
769
+ }
770
+ function pe(e) {
771
+ return e.nodeName === "#text" && e.parentElement?.hasAttribute("data-mention-type") ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_ACCEPT;
772
+ }
773
+ //#endregion
774
+ //#region lib/useWysiwyg.ts
775
+ function me(e, n = !1) {
776
+ t(() => {
777
+ if (n) {
778
+ let t = setTimeout(() => e.current?.focus(), 200);
779
+ return () => clearTimeout(t);
780
+ }
781
+ }, [e, n]);
782
+ }
783
+ function he() {
784
+ let e = r(null);
785
+ return t(() => {
786
+ e.current?.childElementCount || e.current?.appendChild(document.createElement("br"));
787
+ }, [e]), e;
788
+ }
789
+ function $(e) {
790
+ let t = e?.keys();
791
+ return t ? Array.from(t) : [];
792
+ }
793
+ function ge(e) {
794
+ let a = he(), o = r(null), [s, c] = i($(e?.emojiSuggestions));
795
+ t(() => {
796
+ c($(e?.emojiSuggestions));
797
+ }, [e?.emojiSuggestions]);
798
+ let { composerModel: l, onError: u } = U(a, e?.initialContent, s), { testRef: d, utilities: f } = ie(a, l), p = B(a, l), { content: m, actionStates: h, areListenersReady: g, suggestion: _ } = z(a, o, l, f, p, u, e?.inputEventProcessor, e?.emojiSuggestions);
799
+ me(a, e?.isAutoFocusEnabled);
800
+ let v = n(() => se(_), [_]);
801
+ return {
802
+ ref: a,
803
+ isWysiwygReady: g,
804
+ wysiwyg: p,
805
+ content: m,
806
+ actionStates: h,
807
+ debug: {
808
+ modelRef: o,
809
+ testRef: d,
810
+ resetTestCase: f.onResetTestCase,
811
+ traceAction: f.traceAction
812
+ },
813
+ suggestion: v,
814
+ messageContent: l?.get_content_as_message_html() ?? null
815
+ };
816
+ }
817
+ //#endregion
818
+ export { H as initOnce, ue as plainToRich, le as richToPlain, ge as useWysiwyg };