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