@g9tm/lynxcontrols-richtext 0.0.0-stage → 2.5.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +32 -2
  2. package/catalog/cards/G9RichLines.md +16 -0
  3. package/catalog/cards/G9RichText.md +17 -0
  4. package/catalog/cards/G9RichTextEditor.md +17 -0
  5. package/catalog/catalog.json +1667 -0
  6. package/catalog/index.md +7 -0
  7. package/dist/.tsbuildinfo +1 -0
  8. package/dist/bidi.d.ts +24 -0
  9. package/dist/bidi.d.ts.map +1 -0
  10. package/dist/bidi.jsx +102 -0
  11. package/dist/editor.d.ts +111 -0
  12. package/dist/editor.d.ts.map +1 -0
  13. package/dist/editor.jsx +556 -0
  14. package/dist/element-core.d.ts +60 -0
  15. package/dist/element-core.d.ts.map +1 -0
  16. package/dist/element-core.jsx +173 -0
  17. package/dist/element-types.d.ts +31 -0
  18. package/dist/element-types.d.ts.map +1 -0
  19. package/dist/element-types.jsx +1 -0
  20. package/dist/index.d.ts +10 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.jsx +8 -0
  23. package/dist/lines-view.d.ts +27 -0
  24. package/dist/lines-view.d.ts.map +1 -0
  25. package/dist/lines-view.jsx +100 -0
  26. package/dist/mentions.d.ts +30 -0
  27. package/dist/mentions.d.ts.map +1 -0
  28. package/dist/mentions.jsx +40 -0
  29. package/dist/ot.d.ts +49 -0
  30. package/dist/ot.d.ts.map +1 -0
  31. package/dist/ot.jsx +306 -0
  32. package/dist/protocol.d.ts +199 -0
  33. package/dist/protocol.d.ts.map +1 -0
  34. package/dist/protocol.jsx +11 -0
  35. package/dist/rich-text.d.ts +48 -0
  36. package/dist/rich-text.d.ts.map +1 -0
  37. package/dist/rich-text.jsx +186 -0
  38. package/dist/richtext.css +411 -0
  39. package/dist/richtext.css.map +1 -0
  40. package/dist/session.d.ts +142 -0
  41. package/dist/session.d.ts.map +1 -0
  42. package/dist/session.jsx +665 -0
  43. package/dist/yjs.d.ts +3 -0
  44. package/dist/yjs.d.ts.map +1 -0
  45. package/dist/yjs.jsx +2 -0
  46. package/dist/ymodel.d.ts +178 -0
  47. package/dist/ymodel.d.ts.map +1 -0
  48. package/dist/ymodel.jsx +1379 -0
  49. package/package.json +68 -4
@@ -0,0 +1,1379 @@
1
+ import { MARK, OBJ, SOFT_BREAK } from "./protocol.jsx";
2
+ import * as __rspack_external_yjs from "yjs";
3
+ function _define_property(obj, key, value) {
4
+ if (key in obj) Object.defineProperty(obj, key, {
5
+ value: value,
6
+ enumerable: true,
7
+ configurable: true,
8
+ writable: true
9
+ });
10
+ else obj[key] = value;
11
+ return obj;
12
+ }
13
+ const LOCAL_ORIGIN = {
14
+ g9: 'richtext'
15
+ };
16
+ const TEXTBLOCKS = new Set([
17
+ 'paragraph',
18
+ 'heading',
19
+ 'codeBlock'
20
+ ]);
21
+ const ATOM_BLOCKS = new Set([
22
+ 'horizontalRule',
23
+ 'docImage'
24
+ ]);
25
+ const LISTS = new Set([
26
+ 'bulletList',
27
+ 'orderedList',
28
+ 'taskList'
29
+ ]);
30
+ const ITEMS = new Set([
31
+ 'listItem',
32
+ 'taskItem'
33
+ ]);
34
+ const CELLS = new Set([
35
+ 'tableCell',
36
+ 'tableHeader'
37
+ ]);
38
+ const PARAGRAPHS_ONLY = new Set([
39
+ 'callout'
40
+ ]);
41
+ const plainContext = {
42
+ mention: (_id, label)=>`@${label}`,
43
+ chip: (href)=>({
44
+ k: 'chip',
45
+ t: href
46
+ }),
47
+ date: (key)=>key,
48
+ imageSrc: ()=>void 0,
49
+ number: (n)=>`${n}.`
50
+ };
51
+ const LIST_NAME = {
52
+ bullet: 'bulletList',
53
+ ordered: 'orderedList',
54
+ task: 'taskList'
55
+ };
56
+ class DocModel {
57
+ destroy() {
58
+ this.root.unobserveDeep(this.observer);
59
+ this.cache.clear();
60
+ }
61
+ replaceContent(source) {
62
+ const blocks = source.toArray().filter((child)=>child instanceof __rspack_external_yjs.XmlElement);
63
+ this.transact(()=>{
64
+ if (this.root.length) this.root.delete(0, this.root.length);
65
+ this.root.insert(0, blocks.length ? blocks.map((block)=>block.clone()) : [
66
+ new __rspack_external_yjs.XmlElement('paragraph')
67
+ ]);
68
+ });
69
+ }
70
+ setContext(ctx) {
71
+ this.ctx = ctx;
72
+ this.relabel();
73
+ }
74
+ relabel() {
75
+ for (const content of this.cache.values())content.runs = void 0;
76
+ this.current = null;
77
+ }
78
+ get flat() {
79
+ if (!this.current) this.current = this.build();
80
+ return this.current;
81
+ }
82
+ content(el) {
83
+ const known = this.cache.get(el);
84
+ if (known) return known;
85
+ const pieces = [];
86
+ let text = '';
87
+ const children = el.toArray();
88
+ for(let idx = 0; idx < children.length; idx++){
89
+ const child = children[idx];
90
+ if (child instanceof __rspack_external_yjs.XmlText) {
91
+ const delta = child.toDelta().filter((op)=>'string' == typeof op.insert);
92
+ let len = 0;
93
+ for (const op of delta)len += op.insert.length;
94
+ pieces.push({
95
+ k: 'text',
96
+ y: child,
97
+ idx,
98
+ at: text.length,
99
+ len,
100
+ delta
101
+ });
102
+ for (const op of delta)text += op.insert;
103
+ } else if (child instanceof __rspack_external_yjs.XmlElement) {
104
+ pieces.push({
105
+ k: 'inline',
106
+ y: child,
107
+ idx,
108
+ at: text.length,
109
+ name: child.nodeName
110
+ });
111
+ text += 'hardBreak' === child.nodeName ? '\n' : OBJ;
112
+ }
113
+ }
114
+ const content = {
115
+ text,
116
+ pieces
117
+ };
118
+ this.cache.set(el, content);
119
+ return content;
120
+ }
121
+ build() {
122
+ const segs = [];
123
+ let pos = 0;
124
+ const walk = (parent, frames)=>{
125
+ for (const child of parent.toArray()){
126
+ if (!(child instanceof __rspack_external_yjs.XmlElement)) continue;
127
+ const name = child.nodeName;
128
+ if (TEXTBLOCKS.has(name)) {
129
+ const content = this.content(child);
130
+ segs.push({
131
+ el: child,
132
+ name,
133
+ kind: 'codeBlock' === name ? 'code' : 'text',
134
+ start: pos,
135
+ len: content.text.length,
136
+ text: content.text,
137
+ pieces: content.pieces,
138
+ frames
139
+ });
140
+ pos += content.text.length + 1;
141
+ } else if (ATOM_BLOCKS.has(name) || 0 === child.length && !isContainer(name)) {
142
+ segs.push({
143
+ el: child,
144
+ name,
145
+ kind: 'atom',
146
+ start: pos,
147
+ len: 1,
148
+ text: OBJ,
149
+ pieces: [],
150
+ frames
151
+ });
152
+ pos += 2;
153
+ } else walk(child, [
154
+ ...frames,
155
+ child
156
+ ]);
157
+ }
158
+ };
159
+ walk(this.root, []);
160
+ const text = segs.map((seg)=>seg.text).join('\n');
161
+ const lines = [];
162
+ for (const seg of segs)this.linesOf(seg, lines);
163
+ if (0 === lines.length) lines.push({
164
+ t: 'p',
165
+ r: []
166
+ });
167
+ return {
168
+ text,
169
+ segs,
170
+ lines
171
+ };
172
+ }
173
+ runsOf(seg) {
174
+ const content = this.content(seg.el);
175
+ if (content.runs) return content.runs;
176
+ const lines = [
177
+ []
178
+ ];
179
+ const push = (run)=>{
180
+ var _last_m, _run_m;
181
+ const line = lines[lines.length - 1];
182
+ const last = line[line.length - 1];
183
+ if (!last || last.a || run.a || (null != (_last_m = last.m) ? _last_m : 0) !== (null != (_run_m = run.m) ? _run_m : 0) || last.href !== run.href) line.push(run);
184
+ else last.n += run.n;
185
+ };
186
+ for (const piece of content.pieces){
187
+ if ('inline' === piece.k) {
188
+ if ('hardBreak' === piece.name) lines.push([]);
189
+ else push({
190
+ n: 1,
191
+ a: this.atomOf(piece.y)
192
+ });
193
+ continue;
194
+ }
195
+ for (const op of piece.delta){
196
+ const { m, href } = 'code' === seg.kind ? {
197
+ m: 0,
198
+ href: void 0
199
+ } : markBits(op.attributes);
200
+ const parts = op.insert.split('\n');
201
+ parts.forEach((part, index)=>{
202
+ if (index > 0) lines.push([]);
203
+ if (part.length) push(m || href ? {
204
+ n: part.length,
205
+ ...m ? {
206
+ m
207
+ } : {},
208
+ ...href ? {
209
+ href
210
+ } : {}
211
+ } : {
212
+ n: part.length
213
+ });
214
+ });
215
+ }
216
+ }
217
+ content.runs = lines;
218
+ return lines;
219
+ }
220
+ atomOf(el) {
221
+ const attr = (key)=>{
222
+ const value = el.getAttribute(key);
223
+ return null == value ? '' : String(value);
224
+ };
225
+ switch(el.nodeName){
226
+ case 'mention':
227
+ return {
228
+ k: 'mention',
229
+ t: this.ctx.mention(attr('id'), attr('label'))
230
+ };
231
+ case 'refChip':
232
+ return this.ctx.chip(attr('href'), attr('kind') || 'doc');
233
+ case 'dateChip':
234
+ return {
235
+ k: 'date',
236
+ t: this.ctx.date(attr('date'))
237
+ };
238
+ default:
239
+ return {
240
+ k: 'chip',
241
+ t: el.nodeName
242
+ };
243
+ }
244
+ }
245
+ linesOf(seg, out) {
246
+ const style = {};
247
+ let ind = 0;
248
+ let q = 0;
249
+ let done = false;
250
+ for (const frame of seg.frames){
251
+ const name = frame.nodeName;
252
+ if (LISTS.has(name)) ind++;
253
+ else if ('blockquote' === name) q++;
254
+ else if ('callout' === name) style.co = toneOf(frame.getAttribute('tone'));
255
+ else if ('taskItem' === name && true === frame.getAttribute('checked')) done = true;
256
+ }
257
+ if (ind) style.ind = ind;
258
+ if (q) style.q = q;
259
+ if (done) style.done = true;
260
+ let dir;
261
+ for(let i = seg.frames.length; i >= 0 && !dir; i--){
262
+ const el = i === seg.frames.length ? seg.el : seg.frames[i];
263
+ const value = el.getAttribute('dir');
264
+ if ('rtl' === value || 'ltr' === value) dir = value;
265
+ }
266
+ if (dir) style.dir = dir;
267
+ const cell = cellOf(seg);
268
+ let cellInfo;
269
+ if (cell) {
270
+ const row = cell.parent instanceof __rspack_external_yjs.XmlElement ? cell.parent : null;
271
+ const table = (null == row ? void 0 : row.parent) instanceof __rspack_external_yjs.XmlElement ? row.parent : null;
272
+ cellInfo = {
273
+ r: table && row ? indexIn(table, row) : 0,
274
+ c: row ? indexIn(row, cell) : 0,
275
+ ...'tableHeader' === cell.nodeName ? {
276
+ h: true
277
+ } : {}
278
+ };
279
+ }
280
+ let marker;
281
+ const parent = seg.frames[seg.frames.length - 1];
282
+ if (parent && ITEMS.has(parent.nodeName) && parent.get(0) === seg.el) {
283
+ var _ref;
284
+ const list = seg.frames[seg.frames.length - 2];
285
+ const listName = null != (_ref = null == list ? void 0 : list.nodeName) ? _ref : 'bulletList';
286
+ if ('taskList' === listName || 'taskItem' === parent.nodeName) marker = {
287
+ k: 't',
288
+ c: true === parent.getAttribute('checked')
289
+ };
290
+ else if ('orderedList' === listName) {
291
+ var _list_getAttribute;
292
+ const start = Number(null != (_list_getAttribute = list.getAttribute('start')) ? _list_getAttribute : 1);
293
+ marker = {
294
+ k: 'n',
295
+ n: this.ctx.number((Number.isFinite(start) ? start : 1) + indexIn(list, parent))
296
+ };
297
+ } else marker = {
298
+ k: 'b'
299
+ };
300
+ }
301
+ if ('atom' === seg.kind) {
302
+ const line = {
303
+ t: 'horizontalRule' === seg.name ? 'hr' : 'img',
304
+ ...style,
305
+ r: [
306
+ {
307
+ n: 1
308
+ }
309
+ ]
310
+ };
311
+ if ('docImage' === seg.name) {
312
+ var _seg_el_getAttribute;
313
+ const key = String(null != (_seg_el_getAttribute = seg.el.getAttribute('key')) ? _seg_el_getAttribute : '');
314
+ const alt = seg.el.getAttribute('alt');
315
+ const src = key ? this.ctx.imageSrc(key) : void 0;
316
+ line.img = {
317
+ key,
318
+ ...src ? {
319
+ src
320
+ } : {},
321
+ ...'string' == typeof alt && alt ? {
322
+ alt
323
+ } : {}
324
+ };
325
+ }
326
+ if (cellInfo) line.cell = {
327
+ ...cellInfo,
328
+ ...isFirstInCell(seg, cell) ? {
329
+ top: true
330
+ } : {}
331
+ };
332
+ out.push(line);
333
+ return;
334
+ }
335
+ const t = 'code' === seg.kind ? 'code' : 'heading' === seg.name ? headingType(seg.el.getAttribute('level')) : 'p';
336
+ const runs = this.runsOf(seg);
337
+ runs.forEach((r, index)=>{
338
+ const line = {
339
+ t,
340
+ ...style,
341
+ r
342
+ };
343
+ if (0 === index && marker) line.mk = marker;
344
+ if ('code' === seg.kind) {
345
+ if (0 === index) line.cf = true;
346
+ if (index === runs.length - 1) line.cl = true;
347
+ }
348
+ if (cellInfo) line.cell = {
349
+ ...cellInfo,
350
+ ...0 === index && isFirstInCell(seg, cell) ? {
351
+ top: true
352
+ } : {}
353
+ };
354
+ out.push(line);
355
+ });
356
+ }
357
+ locate(pos) {
358
+ const segs = this.flat.segs;
359
+ if (0 === segs.length) return null;
360
+ let lo = 0;
361
+ let hi = segs.length - 1;
362
+ while(lo < hi){
363
+ const mid = lo + hi + 1 >> 1;
364
+ if (segs[mid].start <= pos) lo = mid;
365
+ else hi = mid - 1;
366
+ }
367
+ const seg = segs[lo];
368
+ return {
369
+ seg,
370
+ off: Math.max(0, Math.min(seg.len, pos - seg.start))
371
+ };
372
+ }
373
+ segsIn(from, to) {
374
+ const segs = this.flat.segs;
375
+ return segs.filter((seg)=>seg.start <= Math.max(from, to) && seg.start + seg.len >= Math.min(from, to));
376
+ }
377
+ marksAt(pos) {
378
+ const hit = this.locate(pos);
379
+ if (!hit || 'text' !== hit.seg.kind) return {};
380
+ const { seg, off } = hit;
381
+ const at = off > 0 ? off - 1 : off;
382
+ for (const piece of seg.pieces){
383
+ if ('text' !== piece.k || at < piece.at || at >= piece.at + piece.len) continue;
384
+ let p = piece.at;
385
+ for (const op of piece.delta){
386
+ var _op_attributes;
387
+ if (at < p + op.insert.length) return {
388
+ ...null != (_op_attributes = op.attributes) ? _op_attributes : {}
389
+ };
390
+ p += op.insert.length;
391
+ }
392
+ }
393
+ return {};
394
+ }
395
+ activeMarks(from, to) {
396
+ if (from === to) return this.marksAt(from);
397
+ let common = null;
398
+ for (const seg of this.segsIn(from, to)){
399
+ if ('text' !== seg.kind) continue;
400
+ const s = Math.max(0, from - seg.start);
401
+ const e = Math.min(seg.len, to - seg.start);
402
+ for (const piece of seg.pieces){
403
+ if ('text' !== piece.k) continue;
404
+ let p = piece.at;
405
+ for (const op of piece.delta){
406
+ var _op_attributes;
407
+ const os = p;
408
+ const oe = p + op.insert.length;
409
+ p = oe;
410
+ if (oe <= s || os >= e) continue;
411
+ const attrs = null != (_op_attributes = op.attributes) ? _op_attributes : {};
412
+ if (common) {
413
+ for (const key of Object.keys(common))if (!(key in attrs)) delete common[key];
414
+ } else common = {
415
+ ...attrs
416
+ };
417
+ }
418
+ }
419
+ }
420
+ return null != common ? common : {};
421
+ }
422
+ transact(fn) {
423
+ this.doc.transact(fn, LOCAL_ORIGIN);
424
+ }
425
+ replace(from, to, text, marks) {
426
+ if (to > from) this.deleteRange(from, to);
427
+ if (!text) return;
428
+ let cursor = from;
429
+ const parts = splitBreaks(text);
430
+ let attrs = null != marks ? marks : this.marksAt(from);
431
+ for (const part of parts)if (part) {
432
+ if ('\n' === part) cursor = this.split(cursor);
433
+ else if (part === SOFT_BREAK) {
434
+ this.insertInline(cursor, 'hardBreak', {});
435
+ cursor += 1;
436
+ } else {
437
+ this.insertText(cursor, part, attrs);
438
+ cursor += part.length;
439
+ }
440
+ attrs = null != marks ? marks : attrs;
441
+ }
442
+ }
443
+ insertText(pos, text, attrs = {}) {
444
+ this.ensureBlock();
445
+ const hit = this.locate(pos);
446
+ if (!hit) return;
447
+ const { seg, off } = hit;
448
+ if ('atom' === seg.kind) return void this.transact(()=>{
449
+ const parent = parentOf(seg.el);
450
+ const p = new __rspack_external_yjs.XmlElement('paragraph');
451
+ parent.insert(indexIn(parent, seg.el) + (off > 0 ? 1 : 0), [
452
+ p
453
+ ]);
454
+ appendItems(p, [
455
+ {
456
+ text,
457
+ attrs
458
+ }
459
+ ]);
460
+ });
461
+ const clean = 'code' === seg.kind ? {} : attrs;
462
+ this.transact(()=>insertAt(seg, off, text, clean));
463
+ }
464
+ split(pos) {
465
+ this.ensureBlock();
466
+ const hit = this.locate(pos);
467
+ if (!hit) return pos;
468
+ const { seg, off } = hit;
469
+ if ('code' === seg.kind) {
470
+ if (off === seg.len && seg.text.endsWith('\n\n')) {
471
+ this.transact(()=>{
472
+ deleteIn(seg, seg.len - 2, seg.len);
473
+ const parent = parentOf(seg.el);
474
+ parent.insert(indexIn(parent, seg.el) + 1, [
475
+ new __rspack_external_yjs.XmlElement('paragraph')
476
+ ]);
477
+ });
478
+ return pos - 1;
479
+ }
480
+ this.transact(()=>insertAt(seg, off, '\n', {}));
481
+ return pos + 1;
482
+ }
483
+ if ('atom' === seg.kind) {
484
+ this.transact(()=>{
485
+ const parent = parentOf(seg.el);
486
+ parent.insert(indexIn(parent, seg.el) + (off > 0 ? 1 : 0), [
487
+ new __rspack_external_yjs.XmlElement('paragraph')
488
+ ]);
489
+ });
490
+ return off > 0 ? pos + 1 : pos + 1;
491
+ }
492
+ const parent = seg.frames[seg.frames.length - 1];
493
+ if (parent && ITEMS.has(parent.nodeName) && parent.get(0) === seg.el) {
494
+ if (0 === seg.len && 1 === parent.length) {
495
+ this.transact(()=>this.liftItem(parent));
496
+ return pos;
497
+ }
498
+ this.transact(()=>{
499
+ const tail = itemsFrom(seg, off);
500
+ deleteIn(seg, off, seg.len);
501
+ const list = parentOf(parent);
502
+ const item = new __rspack_external_yjs.XmlElement(parent.nodeName);
503
+ list.insert(indexIn(list, parent) + 1, [
504
+ item
505
+ ]);
506
+ if ('taskItem' === parent.nodeName) item.setAttribute('checked', false);
507
+ const p = new __rspack_external_yjs.XmlElement('paragraph');
508
+ item.insert(0, [
509
+ p
510
+ ]);
511
+ copyDir(seg.el, p);
512
+ appendItems(p, tail);
513
+ const rest = parent.toArray().slice(1).filter((child)=>child instanceof __rspack_external_yjs.XmlElement);
514
+ if (rest.length) {
515
+ item.insert(1, rest.map((child)=>child.clone()));
516
+ parent.delete(1, parent.length - 1);
517
+ }
518
+ });
519
+ return pos + 1;
520
+ }
521
+ this.transact(()=>{
522
+ const tail = itemsFrom(seg, off);
523
+ deleteIn(seg, off, seg.len);
524
+ const container = parentOf(seg.el);
525
+ const isHeading = 'heading' === seg.name;
526
+ const block = new __rspack_external_yjs.XmlElement(isHeading && off === seg.len ? 'paragraph' : seg.name);
527
+ container.insert(indexIn(container, seg.el) + 1, [
528
+ block
529
+ ]);
530
+ if ('heading' === block.nodeName) block.setAttribute('level', seg.el.getAttribute('level'));
531
+ copyDir(seg.el, block);
532
+ appendItems(block, tail);
533
+ });
534
+ return pos + 1;
535
+ }
536
+ deleteRange(from, to) {
537
+ if (to <= from) return;
538
+ const a = this.locate(from);
539
+ const b = this.locate(to);
540
+ if (!a || !b) return;
541
+ if (a.seg === b.seg) {
542
+ if ('atom' === a.seg.kind) {
543
+ if (0 === a.off && 1 === b.off) this.transact(()=>removeNode(a.seg.el));
544
+ return;
545
+ }
546
+ this.transact(()=>deleteIn(a.seg, a.off, b.off));
547
+ return;
548
+ }
549
+ const segs = this.flat.segs;
550
+ const ia = segs.indexOf(a.seg);
551
+ const ib = segs.indexOf(b.seg);
552
+ const between = segs.slice(ia + 1, ib);
553
+ const sameCell = cellOf(a.seg) === cellOf(b.seg);
554
+ const join = 'atom' !== a.seg.kind && 'atom' !== b.seg.kind && sameCell;
555
+ const tables = new Map();
556
+ for (const seg of segs){
557
+ var _tables_get;
558
+ const table = tableOf(seg);
559
+ if (table) tables.set(table, (null != (_tables_get = tables.get(table)) ? _tables_get : 0) + 1);
560
+ }
561
+ const covered = new Map();
562
+ for (const seg of between){
563
+ var _covered_get;
564
+ const table = tableOf(seg);
565
+ if (table) covered.set(table, (null != (_covered_get = covered.get(table)) ? _covered_get : 0) + 1);
566
+ }
567
+ const whole = new Set([
568
+ ...covered
569
+ ].filter(([table, count])=>tables.get(table) === count).map(([table])=>table));
570
+ this.transact(()=>{
571
+ const tail = join ? itemsFrom(b.seg, b.off) : [];
572
+ for (const table of whole)removeNode(table);
573
+ for(let i = between.length - 1; i >= 0; i--){
574
+ const seg = between[i];
575
+ const table = tableOf(seg);
576
+ if (!(table && whole.has(table))) if (cellOf(seg) && (cellOf(seg) !== cellOf(a.seg) || !sameCell)) if ('atom' !== seg.kind) deleteIn(seg, 0, seg.len);
577
+ else removeNode(seg.el);
578
+ else removeNode(seg.el);
579
+ }
580
+ if ('atom' === b.seg.kind) {
581
+ if (1 === b.off) removeNode(b.seg.el);
582
+ } else if (join) {
583
+ this.moveFollowers(b.seg, a.seg);
584
+ removeNode(b.seg.el);
585
+ } else deleteIn(b.seg, 0, b.off);
586
+ if ('atom' === a.seg.kind) {
587
+ if (0 === a.off) removeNode(a.seg.el);
588
+ } else {
589
+ deleteIn(a.seg, a.off, a.seg.len);
590
+ if (join) appendItems(a.seg.el, 'code' === a.seg.kind ? toCode(tail) : 'code' === b.seg.kind ? fromCode(tail) : tail);
591
+ }
592
+ ensureRoot(this.root);
593
+ });
594
+ }
595
+ moveFollowers(from, into) {
596
+ const item = from.frames[from.frames.length - 1];
597
+ if (!item || !ITEMS.has(item.nodeName) || item.get(0) !== from.el) return;
598
+ const rest = item.toArray().slice(1).filter((child)=>child instanceof __rspack_external_yjs.XmlElement);
599
+ if (!rest.length) return;
600
+ const clones = rest.map((child)=>child.clone());
601
+ item.delete(1, item.length - 1);
602
+ let anchor = into.el;
603
+ let target = parentOf(anchor);
604
+ while(target instanceof __rspack_external_yjs.XmlElement && PARAGRAPHS_ONLY.has(target.nodeName)){
605
+ anchor = target;
606
+ target = parentOf(anchor);
607
+ }
608
+ target.insert(indexIn(target, anchor) + 1, clones);
609
+ }
610
+ ensureBlock() {
611
+ if (this.flat.segs.length) return;
612
+ this.transact(()=>ensureRoot(this.root, true));
613
+ }
614
+ insertInline(pos, name, attrs) {
615
+ this.ensureBlock();
616
+ const hit = this.locate(pos);
617
+ if (!hit || 'text' !== hit.seg.kind) return;
618
+ const { seg, off } = hit;
619
+ this.transact(()=>{
620
+ const el = new __rspack_external_yjs.XmlElement(name);
621
+ for (const [key, value] of Object.entries(attrs))if (null != value) el.setAttribute(key, value);
622
+ insertElementAt(seg, off, el);
623
+ });
624
+ }
625
+ insertBlock(pos, kind, attrs = {}) {
626
+ this.ensureBlock();
627
+ const hit = this.locate(pos);
628
+ if (!hit) return pos;
629
+ const { seg, off } = hit;
630
+ const make = ()=>{
631
+ if ('hr' === kind) return new __rspack_external_yjs.XmlElement('horizontalRule');
632
+ if ('image' === kind) {
633
+ const el = new __rspack_external_yjs.XmlElement('docImage');
634
+ for (const [key, value] of Object.entries(attrs))if (null != value) el.setAttribute(key, value);
635
+ return el;
636
+ }
637
+ return new __rspack_external_yjs.XmlElement('table');
638
+ };
639
+ this.transact(()=>{
640
+ var _attrs_rows, _attrs_cols;
641
+ let after = true;
642
+ if ('atom' !== seg.kind && off > 0 && off < seg.len) {
643
+ const tail = itemsFrom(seg, off);
644
+ deleteIn(seg, off, seg.len);
645
+ const rest = new __rspack_external_yjs.XmlElement('heading' === seg.name ? 'heading' : seg.name);
646
+ const container = parentOf(seg.el);
647
+ container.insert(indexIn(container, seg.el) + 1, [
648
+ rest
649
+ ]);
650
+ if ('heading' === seg.name) rest.setAttribute('level', seg.el.getAttribute('level'));
651
+ copyDir(seg.el, rest);
652
+ appendItems(rest, tail);
653
+ } else if ('atom' !== seg.kind && 0 === off && seg.len > 0) {
654
+ const parent = seg.frames[seg.frames.length - 1];
655
+ after = Boolean(parent && ITEMS.has(parent.nodeName) && parent.get(0) === seg.el);
656
+ }
657
+ let anchor = seg.el;
658
+ let target = parentOf(anchor);
659
+ while(target instanceof __rspack_external_yjs.XmlElement && PARAGRAPHS_ONLY.has(target.nodeName)){
660
+ anchor = target;
661
+ target = parentOf(anchor);
662
+ }
663
+ const replaceEmpty = 'text' === seg.kind && 0 === seg.len && anchor === seg.el && !(seg.frames.length && ITEMS.has(seg.frames[seg.frames.length - 1].nodeName) && seg.frames[seg.frames.length - 1].get(0) === seg.el);
664
+ const block = make();
665
+ const index = indexIn(target, anchor) + (after ? 1 : 0);
666
+ target.insert(index, [
667
+ block
668
+ ]);
669
+ if ('table' === kind) fillTable(block, Number(null != (_attrs_rows = attrs.rows) ? _attrs_rows : 3), Number(null != (_attrs_cols = attrs.cols) ? _attrs_cols : 3), false !== attrs.header);
670
+ if (replaceEmpty) removeNode(seg.el);
671
+ const next = block.parent.get(indexIn(block.parent, block) + 1);
672
+ if (!(next instanceof __rspack_external_yjs.XmlElement)) block.parent.insert(indexIn(block.parent, block) + 1, [
673
+ new __rspack_external_yjs.XmlElement('paragraph')
674
+ ]);
675
+ });
676
+ return pos;
677
+ }
678
+ toggleMark(from, to, mark, value = {}) {
679
+ const name = mark;
680
+ const active = name in this.activeMarks(from, to);
681
+ this.setMark(from, to, name, active ? null : value);
682
+ }
683
+ setMark(from, to, name, value) {
684
+ if (to <= from) return;
685
+ const segs = this.segsIn(from, to).filter((seg)=>'text' === seg.kind);
686
+ this.transact(()=>{
687
+ for (const seg of segs){
688
+ const s = Math.max(0, from - seg.start);
689
+ const e = Math.min(seg.len, to - seg.start);
690
+ for (const piece of seg.pieces){
691
+ if ('text' !== piece.k) continue;
692
+ const ps = Math.max(s, piece.at);
693
+ const pe = Math.min(e, piece.at + piece.len);
694
+ if (pe > ps) piece.y.format(ps - piece.at, pe - ps, {
695
+ [name]: value
696
+ });
697
+ }
698
+ }
699
+ });
700
+ }
701
+ setBlockType(from, to, type) {
702
+ const segs = this.segsIn(from, to).filter((seg)=>'atom' !== seg.kind);
703
+ if (!segs.length) return;
704
+ const all = segs.every((seg)=>blockTypeOf(seg) === type);
705
+ const target = all && 'p' !== type ? 'p' : type;
706
+ this.transact(()=>{
707
+ for (const seg of segs){
708
+ if (blockTypeOf(seg) === target) continue;
709
+ const parent = seg.frames[seg.frames.length - 1];
710
+ const firstOfItem = parent && ITEMS.has(parent.nodeName) && parent.get(0) === seg.el;
711
+ if ('p' !== target && (firstOfItem || parent && PARAGRAPHS_ONLY.has(parent.nodeName))) continue;
712
+ const name = 'p' === target ? 'paragraph' : 'code' === target ? 'codeBlock' : 'heading';
713
+ const block = new __rspack_external_yjs.XmlElement(name);
714
+ const container = parentOf(seg.el);
715
+ container.insert(indexIn(container, seg.el), [
716
+ block
717
+ ]);
718
+ if ('heading' === name) block.setAttribute('level', Number(target.slice(1)));
719
+ copyDir(seg.el, block);
720
+ const items = itemsFrom(seg, 0);
721
+ appendItems(block, 'codeBlock' === name ? toCode(items) : 'code' === seg.kind ? fromCode(items) : items);
722
+ removeNode(seg.el, false);
723
+ }
724
+ });
725
+ }
726
+ setDirection(from, to, dir) {
727
+ const segs = this.segsIn(from, to).filter((seg)=>'atom' !== seg.kind);
728
+ this.transact(()=>{
729
+ for (const seg of segs)if (dir) seg.el.setAttribute('dir', dir);
730
+ else if (void 0 !== seg.el.getAttribute('dir')) seg.el.removeAttribute('dir');
731
+ });
732
+ }
733
+ directionAt(pos) {
734
+ var _this_locate;
735
+ const seg = null == (_this_locate = this.locate(pos)) ? void 0 : _this_locate.seg;
736
+ const value = null == seg ? void 0 : seg.el.getAttribute('dir');
737
+ return 'rtl' === value || 'ltr' === value ? value : null;
738
+ }
739
+ toggleList(from, to, kind) {
740
+ const segs = this.segsIn(from, to);
741
+ if (!segs.length) return;
742
+ const listName = LIST_NAME[kind];
743
+ const items = itemsOfSegs(segs);
744
+ if (items.length === segs.length || items.length && segs.every((seg)=>innermostItem(seg))) {
745
+ const lists = [
746
+ ...new Set(items.map((item)=>parentOf(item)))
747
+ ];
748
+ if (lists.every((list)=>list.nodeName === listName)) return void this.transact(()=>{
749
+ for (const item of [
750
+ ...items
751
+ ].reverse())if (alive(item)) this.liftItem(item);
752
+ });
753
+ this.transact(()=>{
754
+ for (const list of lists){
755
+ if (list.nodeName === listName) continue;
756
+ const replacement = new __rspack_external_yjs.XmlElement(listName);
757
+ const container = parentOf(list);
758
+ container.insert(indexIn(container, list), [
759
+ replacement
760
+ ]);
761
+ copyDir(list, replacement);
762
+ const converted = list.toArray().filter((child)=>child instanceof __rspack_external_yjs.XmlElement).map((item)=>convertItem(item, kind));
763
+ replacement.insert(0, converted);
764
+ removeNode(list, false);
765
+ }
766
+ });
767
+ return;
768
+ }
769
+ const range = blockRange(segs);
770
+ if (!range || range.parent instanceof __rspack_external_yjs.XmlElement && PARAGRAPHS_ONLY.has(range.parent.nodeName)) return;
771
+ this.transact(()=>{
772
+ const { parent, blocks } = range;
773
+ const list = new __rspack_external_yjs.XmlElement(listName);
774
+ parent.insert(indexIn(parent, blocks[0]), [
775
+ list
776
+ ]);
777
+ const newItem = ()=>{
778
+ const item = new __rspack_external_yjs.XmlElement('task' === kind ? 'taskItem' : 'listItem');
779
+ list.insert(list.length, [
780
+ item
781
+ ]);
782
+ if ('task' === kind) item.setAttribute('checked', false);
783
+ return item;
784
+ };
785
+ for (const block of blocks)if (TEXTBLOCKS.has(block.nodeName)) {
786
+ const item = newItem();
787
+ const p = new __rspack_external_yjs.XmlElement('paragraph');
788
+ item.insert(0, [
789
+ p
790
+ ]);
791
+ copyDir(block, p);
792
+ const seg = segs.find((s)=>s.el === block);
793
+ const content = seg ? itemsFrom(seg, 0) : [];
794
+ appendItems(p, 'codeBlock' === block.nodeName ? fromCode(content) : content);
795
+ } else if (LISTS.has(block.nodeName)) {
796
+ const converted = block.toArray().filter((child)=>child instanceof __rspack_external_yjs.XmlElement).map((child)=>convertItem(child, kind));
797
+ list.insert(list.length, converted);
798
+ } else newItem().insert(0, [
799
+ new __rspack_external_yjs.XmlElement('paragraph'),
800
+ block.clone()
801
+ ]);
802
+ for (const block of blocks)removeNode(block, false);
803
+ });
804
+ }
805
+ toggleQuote(from, to) {
806
+ this.toggleWrap(from, to, 'blockquote');
807
+ }
808
+ toggleCallout(from, to, tone = 'info') {
809
+ this.toggleWrap(from, to, 'callout', tone);
810
+ }
811
+ toggleWrap(from, to, name, tone) {
812
+ const segs = this.segsIn(from, to);
813
+ if (!segs.length) return;
814
+ const wrappers = segs.map((seg)=>{
815
+ var _reverse_find;
816
+ return null != (_reverse_find = [
817
+ ...seg.frames
818
+ ].reverse().find((frame)=>frame.nodeName === name)) ? _reverse_find : null;
819
+ });
820
+ if (wrappers.every(Boolean)) return void this.transact(()=>{
821
+ for (const wrapper of [
822
+ ...new Set(wrappers)
823
+ ]){
824
+ if (!alive(wrapper)) continue;
825
+ const children = wrapper.toArray().filter((child)=>child instanceof __rspack_external_yjs.XmlElement);
826
+ const inside = segs.filter((_, index)=>wrappers[index] === wrapper);
827
+ const top = (seg)=>{
828
+ const chain = [
829
+ ...seg.frames,
830
+ seg.el
831
+ ];
832
+ return chain[chain.indexOf(wrapper) + 1];
833
+ };
834
+ const i = children.indexOf(top(inside[0]));
835
+ const j = children.indexOf(top(inside[inside.length - 1]));
836
+ if (i < 0 || j < 0) continue;
837
+ const container = parentOf(wrapper);
838
+ const at = indexIn(container, wrapper) + 1;
839
+ const lifted = children.slice(i, j + 1).map((child)=>child.clone());
840
+ const after = children.slice(j + 1);
841
+ if (after.length) {
842
+ const rest = new __rspack_external_yjs.XmlElement(wrapper.nodeName);
843
+ container.insert(at, [
844
+ ...lifted,
845
+ rest
846
+ ]);
847
+ for (const [key, value] of Object.entries(wrapper.getAttributes()))rest.setAttribute(key, value);
848
+ rest.insert(0, after.map((child)=>child.clone()));
849
+ } else container.insert(at, lifted);
850
+ const raw = indexIn(wrapper, children[i]);
851
+ wrapper.delete(raw, wrapper.length - raw);
852
+ if (0 === wrapper.length) removeNode(wrapper, false);
853
+ }
854
+ });
855
+ const range = blockRange(segs);
856
+ if (!range) return;
857
+ if ('callout' === name && range.blocks.some((block)=>!TEXTBLOCKS.has(block.nodeName))) return;
858
+ if (range.parent instanceof __rspack_external_yjs.XmlElement && PARAGRAPHS_ONLY.has(range.parent.nodeName)) return;
859
+ this.transact(()=>{
860
+ const wrapper = new __rspack_external_yjs.XmlElement(name);
861
+ range.parent.insert(indexIn(range.parent, range.blocks[0]), [
862
+ wrapper
863
+ ]);
864
+ if ('callout' === name) wrapper.setAttribute('tone', null != tone ? tone : 'info');
865
+ const clones = range.blocks.map((block)=>{
866
+ if ('callout' === name && 'paragraph' !== block.nodeName) {
867
+ const p = new __rspack_external_yjs.XmlElement('paragraph');
868
+ return {
869
+ block,
870
+ p
871
+ };
872
+ }
873
+ return {
874
+ block,
875
+ p: null
876
+ };
877
+ });
878
+ wrapper.insert(0, clones.map(({ block, p })=>null != p ? p : block.clone()));
879
+ for (const { block, p } of clones){
880
+ if (!p) continue;
881
+ copyDir(block, p);
882
+ const seg = segs.find((s)=>s.el === block);
883
+ const items = seg ? itemsFrom(seg, 0) : [];
884
+ appendItems(p, 'codeBlock' === block.nodeName ? fromCode(items) : items);
885
+ }
886
+ for (const block of range.blocks)removeNode(block, false);
887
+ });
888
+ }
889
+ setCalloutTone(pos, tone) {
890
+ var _this_locate;
891
+ const seg = null == (_this_locate = this.locate(pos)) ? void 0 : _this_locate.seg;
892
+ const callout = seg ? [
893
+ ...seg.frames
894
+ ].reverse().find((frame)=>'callout' === frame.nodeName) : void 0;
895
+ if (callout) this.transact(()=>callout.setAttribute('tone', tone));
896
+ }
897
+ indent(from, to) {
898
+ const items = itemsOfSegs(this.segsIn(from, to));
899
+ if (!items.length) return;
900
+ this.transact(()=>{
901
+ for (const item of items){
902
+ if (!alive(item)) continue;
903
+ const list = parentOf(item);
904
+ const index = indexIn(list, item);
905
+ if (0 === index) continue;
906
+ const previous = list.get(index - 1);
907
+ if (!(previous instanceof __rspack_external_yjs.XmlElement)) continue;
908
+ const last = previous.get(previous.length - 1);
909
+ const clone = item.clone();
910
+ if (last instanceof __rspack_external_yjs.XmlElement && last.nodeName === list.nodeName) last.insert(last.length, [
911
+ clone
912
+ ]);
913
+ else {
914
+ const nested = new __rspack_external_yjs.XmlElement(list.nodeName);
915
+ previous.insert(previous.length, [
916
+ nested
917
+ ]);
918
+ nested.insert(0, [
919
+ clone
920
+ ]);
921
+ }
922
+ list.delete(indexIn(list, item), 1);
923
+ }
924
+ });
925
+ }
926
+ outdent(from, to) {
927
+ const items = itemsOfSegs(this.segsIn(from, to));
928
+ if (!items.length) return;
929
+ this.transact(()=>{
930
+ for (const item of [
931
+ ...items
932
+ ].reverse())if (alive(item)) this.liftItem(item);
933
+ });
934
+ }
935
+ toggleCheck(line) {
936
+ const seg = this.segOfLine(line);
937
+ const item = seg ? [
938
+ ...seg.frames
939
+ ].reverse().find((frame)=>'taskItem' === frame.nodeName) : void 0;
940
+ if (!item) return false;
941
+ const checked = true === item.getAttribute('checked');
942
+ this.transact(()=>item.setAttribute('checked', !checked));
943
+ return true;
944
+ }
945
+ segOfLine(line) {
946
+ let count = 0;
947
+ for (const seg of this.flat.segs){
948
+ const lines = 'atom' === seg.kind ? 1 : seg.text.split('\n').length;
949
+ if (line < count + lines) return seg;
950
+ count += lines;
951
+ }
952
+ return null;
953
+ }
954
+ lineStart(line) {
955
+ const text = this.flat.text;
956
+ let pos = 0;
957
+ for(let i = 0; i < line; i++){
958
+ const next = text.indexOf('\n', pos);
959
+ if (next < 0) return text.length;
960
+ pos = next + 1;
961
+ }
962
+ return pos;
963
+ }
964
+ inlineAt(pos) {
965
+ const hit = this.locate(pos);
966
+ if (!hit) return null;
967
+ for (const piece of hit.seg.pieces)if ('inline' === piece.k && piece.at === hit.off) return {
968
+ name: piece.name,
969
+ attrs: piece.y.getAttributes()
970
+ };
971
+ return null;
972
+ }
973
+ liftItem(item) {
974
+ const list = parentOf(item);
975
+ const index = indexIn(list, item);
976
+ const after = list.toArray().slice(index + 1).filter((child)=>child instanceof __rspack_external_yjs.XmlElement);
977
+ const outer = parentOf(list);
978
+ if (outer instanceof __rspack_external_yjs.XmlElement && ITEMS.has(outer.nodeName)) {
979
+ const outerList = parentOf(outer);
980
+ const outerKind = outerList instanceof __rspack_external_yjs.XmlElement && 'taskList' === outerList.nodeName ? 'task' : 'bullet';
981
+ const lifted = convertItem(item, outerKind);
982
+ if (after.length) {
983
+ const nested = new __rspack_external_yjs.XmlElement(list.nodeName);
984
+ nested.insert(0, after.map((child)=>child.clone()));
985
+ lifted.insert(lifted.length, [
986
+ nested
987
+ ]);
988
+ }
989
+ outerList.insert(indexIn(outerList, outer) + 1, [
990
+ lifted
991
+ ]);
992
+ list.delete(index, list.length - index);
993
+ if (0 === list.length) removeNode(list);
994
+ return;
995
+ }
996
+ const blocks = item.toArray().filter((child)=>child instanceof __rspack_external_yjs.XmlElement).map((child)=>child.clone());
997
+ const at = indexIn(outer, list) + 1;
998
+ const inserted = [
999
+ ...blocks
1000
+ ];
1001
+ if (after.length) {
1002
+ const rest = new __rspack_external_yjs.XmlElement(list.nodeName);
1003
+ inserted.push(rest);
1004
+ outer.insert(at, inserted);
1005
+ rest.insert(0, after.map((child)=>child.clone()));
1006
+ } else outer.insert(at, inserted);
1007
+ list.delete(index, list.length - index);
1008
+ if (0 === list.length) removeNode(list);
1009
+ }
1010
+ constructor(doc, ctx = plainContext, field = 'default'){
1011
+ _define_property(this, "ctx", void 0);
1012
+ _define_property(this, "doc", void 0);
1013
+ _define_property(this, "root", void 0);
1014
+ _define_property(this, "cache", void 0);
1015
+ _define_property(this, "current", void 0);
1016
+ _define_property(this, "observer", void 0);
1017
+ this.ctx = ctx;
1018
+ this.cache = new Map();
1019
+ this.current = null;
1020
+ this.doc = doc;
1021
+ this.root = doc.getXmlFragment(field);
1022
+ this.observer = (events)=>{
1023
+ for (const event of events){
1024
+ let type = event.target;
1025
+ while(type && type !== this.root){
1026
+ var _ref;
1027
+ var _type__item;
1028
+ if (type instanceof __rspack_external_yjs.XmlElement) this.cache.delete(type);
1029
+ type = null != (_ref = null == (_type__item = type._item) ? void 0 : _type__item.parent) ? _ref : null;
1030
+ }
1031
+ }
1032
+ this.current = null;
1033
+ };
1034
+ this.root.observeDeep(this.observer);
1035
+ }
1036
+ }
1037
+ function splitBreaks(text) {
1038
+ const parts = [];
1039
+ let start = 0;
1040
+ for(let i = 0; i < text.length; i++){
1041
+ const c = text[i];
1042
+ if ('\n' === c || c === SOFT_BREAK) {
1043
+ if (i > start) parts.push(text.slice(start, i));
1044
+ parts.push(c);
1045
+ start = i + 1;
1046
+ }
1047
+ }
1048
+ if (start < text.length) parts.push(text.slice(start));
1049
+ return parts;
1050
+ }
1051
+ function isContainer(name) {
1052
+ return LISTS.has(name) || ITEMS.has(name) || CELLS.has(name) || 'blockquote' === name || 'callout' === name || 'table' === name || 'tableRow' === name;
1053
+ }
1054
+ function toneOf(value) {
1055
+ return 'success' === value || 'warning' === value || 'danger' === value ? value : 'info';
1056
+ }
1057
+ function headingType(level) {
1058
+ const n = Number(level);
1059
+ return 2 === n ? 'h2' : n >= 3 ? 'h3' : 'h1';
1060
+ }
1061
+ function blockTypeOf(seg) {
1062
+ if ('atom' === seg.kind) return null;
1063
+ if ('code' === seg.kind) return 'code';
1064
+ return 'heading' === seg.name ? headingType(seg.el.getAttribute('level')) : 'p';
1065
+ }
1066
+ function markBits(attrs) {
1067
+ if (!attrs) return {
1068
+ m: 0
1069
+ };
1070
+ let m = 0;
1071
+ let href;
1072
+ for (const key of Object.keys(attrs)){
1073
+ const name = key.split('--')[0];
1074
+ const bit = MARK[name];
1075
+ if (bit && null !== attrs[key] && void 0 !== attrs[key]) {
1076
+ m |= bit;
1077
+ if ('link' === name) {
1078
+ const value = attrs[key];
1079
+ if (value && 'string' == typeof value.href) href = value.href;
1080
+ }
1081
+ }
1082
+ }
1083
+ return href ? {
1084
+ m,
1085
+ href
1086
+ } : {
1087
+ m
1088
+ };
1089
+ }
1090
+ function alive(el) {
1091
+ let type = el;
1092
+ while(type && type._item){
1093
+ if (type._item.deleted) return false;
1094
+ type = type._item.parent;
1095
+ }
1096
+ return true;
1097
+ }
1098
+ function parentOf(el) {
1099
+ return el.parent;
1100
+ }
1101
+ function indexIn(parent, child) {
1102
+ let index = 0;
1103
+ let item = parent._start;
1104
+ while(item){
1105
+ if (!item.deleted && item.countable) {
1106
+ if (item.content.type === child) return index;
1107
+ index += item.length;
1108
+ }
1109
+ item = item.right;
1110
+ }
1111
+ return -1;
1112
+ }
1113
+ function cellOf(seg) {
1114
+ for(let i = seg.frames.length - 1; i >= 0; i--)if (CELLS.has(seg.frames[i].nodeName)) return seg.frames[i];
1115
+ return null;
1116
+ }
1117
+ function tableOf(seg) {
1118
+ for(let i = seg.frames.length - 1; i >= 0; i--)if ('table' === seg.frames[i].nodeName) return seg.frames[i];
1119
+ return null;
1120
+ }
1121
+ function isFirstInCell(seg, cell) {
1122
+ if (!cell) return false;
1123
+ let first = cell;
1124
+ while(first && !TEXTBLOCKS.has(first.nodeName) && !ATOM_BLOCKS.has(first.nodeName)){
1125
+ const child = first.get(0);
1126
+ first = child instanceof __rspack_external_yjs.XmlElement ? child : null;
1127
+ }
1128
+ return first === seg.el;
1129
+ }
1130
+ function copyDir(from, to) {
1131
+ const dir = from.getAttribute('dir');
1132
+ if ('rtl' === dir || 'ltr' === dir) to.setAttribute('dir', dir);
1133
+ }
1134
+ function itemsFrom(seg, from) {
1135
+ const items = [];
1136
+ for (const piece of seg.pieces){
1137
+ if ('inline' === piece.k) {
1138
+ if (piece.at >= from) items.push({
1139
+ el: piece.y
1140
+ });
1141
+ continue;
1142
+ }
1143
+ let p = piece.at;
1144
+ for (const op of piece.delta){
1145
+ var _op_attributes;
1146
+ const os = p;
1147
+ const oe = p + op.insert.length;
1148
+ p = oe;
1149
+ if (!(oe <= from)) items.push({
1150
+ text: op.insert.slice(Math.max(0, from - os)),
1151
+ attrs: {
1152
+ ...null != (_op_attributes = op.attributes) ? _op_attributes : {}
1153
+ }
1154
+ });
1155
+ }
1156
+ }
1157
+ return items;
1158
+ }
1159
+ function toCode(items) {
1160
+ let text = '';
1161
+ for (const item of items)if ('text' in item) text += item.text;
1162
+ else {
1163
+ var _item_el_getAttribute, _item_el_getAttribute1, _item_el_getAttribute2;
1164
+ const name = item.el.nodeName;
1165
+ if ('hardBreak' === name) text += '\n';
1166
+ else if ('mention' === name) text += `@${String(null != (_item_el_getAttribute = item.el.getAttribute('label')) ? _item_el_getAttribute : '')}`;
1167
+ else if ('refChip' === name) text += String(null != (_item_el_getAttribute1 = item.el.getAttribute('href')) ? _item_el_getAttribute1 : '');
1168
+ else if ('dateChip' === name) text += String(null != (_item_el_getAttribute2 = item.el.getAttribute('date')) ? _item_el_getAttribute2 : '');
1169
+ }
1170
+ return text ? [
1171
+ {
1172
+ text,
1173
+ attrs: {}
1174
+ }
1175
+ ] : [];
1176
+ }
1177
+ function fromCode(items) {
1178
+ const out = [];
1179
+ for (const item of items){
1180
+ if (!('text' in item)) {
1181
+ out.push(item);
1182
+ continue;
1183
+ }
1184
+ item.text.split('\n').forEach((part, index)=>{
1185
+ if (index > 0) out.push({
1186
+ el: new __rspack_external_yjs.XmlElement('hardBreak')
1187
+ });
1188
+ if (part) out.push({
1189
+ text: part,
1190
+ attrs: {}
1191
+ });
1192
+ });
1193
+ }
1194
+ return out;
1195
+ }
1196
+ function appendItems(el, items) {
1197
+ for (const item of items)if ('text' in item) {
1198
+ if (!item.text) continue;
1199
+ const last = el.length ? el.get(el.length - 1) : null;
1200
+ if (last instanceof __rspack_external_yjs.XmlText) last.insert(last.length, item.text, item.attrs);
1201
+ else {
1202
+ const text = new __rspack_external_yjs.XmlText();
1203
+ el.insert(el.length, [
1204
+ text
1205
+ ]);
1206
+ text.insert(0, item.text, item.attrs);
1207
+ }
1208
+ } else el.insert(el.length, [
1209
+ item.el.doc ? item.el.clone() : item.el
1210
+ ]);
1211
+ }
1212
+ function insertAt(seg, off, text, attrs) {
1213
+ let target = null;
1214
+ for (const piece of seg.pieces)if ('text' === piece.k && off >= piece.at && off <= piece.at + piece.len) {
1215
+ target = piece;
1216
+ if (off < piece.at + piece.len || 0 === piece.len) break;
1217
+ }
1218
+ if (target) return void target.y.insert(off - target.at, text, attrs);
1219
+ const index = childIndexAt(seg, off);
1220
+ const node = new __rspack_external_yjs.XmlText();
1221
+ seg.el.insert(index, [
1222
+ node
1223
+ ]);
1224
+ node.insert(0, text, attrs);
1225
+ }
1226
+ function childIndexAt(seg, off) {
1227
+ for (const piece of seg.pieces){
1228
+ if ('inline' === piece.k && piece.at >= off) return piece.idx;
1229
+ if ('text' === piece.k && piece.at >= off && piece.len > 0) return piece.idx;
1230
+ }
1231
+ return seg.el.length;
1232
+ }
1233
+ function insertElementAt(seg, off, el) {
1234
+ for (const piece of seg.pieces){
1235
+ var _op_attributes;
1236
+ if ('text' !== piece.k || off <= piece.at || off >= piece.at + piece.len) continue;
1237
+ const tail = [];
1238
+ let p = piece.at;
1239
+ for (const op of piece.delta){
1240
+ const os = p;
1241
+ p += op.insert.length;
1242
+ if (!(p <= off)) tail.push({
1243
+ insert: op.insert.slice(Math.max(0, off - os)),
1244
+ attributes: op.attributes
1245
+ });
1246
+ }
1247
+ piece.y.delete(off - piece.at, piece.len - (off - piece.at));
1248
+ const rest = new __rspack_external_yjs.XmlText();
1249
+ seg.el.insert(piece.idx + 1, [
1250
+ el,
1251
+ rest
1252
+ ]);
1253
+ for (const op of tail)rest.insert(rest.length, op.insert, {
1254
+ ...null != (_op_attributes = op.attributes) ? _op_attributes : {}
1255
+ });
1256
+ return;
1257
+ }
1258
+ seg.el.insert(childIndexAt(seg, off), [
1259
+ el
1260
+ ]);
1261
+ }
1262
+ function deleteIn(seg, from, to) {
1263
+ if (to <= from) return;
1264
+ for(let i = seg.pieces.length - 1; i >= 0; i--){
1265
+ const piece = seg.pieces[i];
1266
+ if ('inline' === piece.k) {
1267
+ if (piece.at >= from && piece.at < to) seg.el.delete(piece.idx, 1);
1268
+ continue;
1269
+ }
1270
+ const s = Math.max(from, piece.at);
1271
+ const e = Math.min(to, piece.at + piece.len);
1272
+ if (e > s) piece.y.delete(s - piece.at, e - s);
1273
+ }
1274
+ }
1275
+ function removeNode(el, cleanup = true) {
1276
+ const parent = parentOf(el);
1277
+ const index = indexIn(parent, el);
1278
+ if (index < 0) return;
1279
+ parent.delete(index, 1);
1280
+ if (cleanup) ensureValid(parent);
1281
+ }
1282
+ function ensureValid(node) {
1283
+ if (!(node instanceof __rspack_external_yjs.XmlElement)) return void ensureRoot(node);
1284
+ const name = node.nodeName;
1285
+ if (ITEMS.has(name)) if (0 === node.length) removeNode(node);
1286
+ else {
1287
+ const first = node.get(0);
1288
+ if (!(first instanceof __rspack_external_yjs.XmlElement) || 'paragraph' !== first.nodeName) node.insert(0, [
1289
+ new __rspack_external_yjs.XmlElement('paragraph')
1290
+ ]);
1291
+ }
1292
+ else if (LISTS.has(name) || 'blockquote' === name || 'callout' === name || 'tableRow' === name || 'table' === name) {
1293
+ if (0 === node.length) removeNode(node);
1294
+ } else if (CELLS.has(name)) {
1295
+ if (0 === node.length) node.insert(0, [
1296
+ new __rspack_external_yjs.XmlElement('paragraph')
1297
+ ]);
1298
+ }
1299
+ }
1300
+ function ensureRoot(root, force = false) {
1301
+ if (0 === root.length || force) {
1302
+ const hasBlock = root.toArray().some((child)=>child instanceof __rspack_external_yjs.XmlElement);
1303
+ if (!hasBlock) root.insert(root.length, [
1304
+ new __rspack_external_yjs.XmlElement('paragraph')
1305
+ ]);
1306
+ }
1307
+ }
1308
+ function fillTable(table, rows, cols, header) {
1309
+ const made = [];
1310
+ for(let r = 0; r < Math.max(1, rows); r++){
1311
+ const row = new __rspack_external_yjs.XmlElement('tableRow');
1312
+ made.push(row);
1313
+ }
1314
+ table.insert(0, made);
1315
+ made.forEach((row, r)=>{
1316
+ const cells = [];
1317
+ for(let c = 0; c < Math.max(1, cols); c++)cells.push(new __rspack_external_yjs.XmlElement(header && 0 === r ? 'tableHeader' : 'tableCell'));
1318
+ row.insert(0, cells);
1319
+ for (const cell of cells){
1320
+ cell.setAttribute('colspan', 1);
1321
+ cell.setAttribute('rowspan', 1);
1322
+ cell.insert(0, [
1323
+ new __rspack_external_yjs.XmlElement('paragraph')
1324
+ ]);
1325
+ }
1326
+ });
1327
+ }
1328
+ function innermostItem(seg) {
1329
+ const parent = seg.frames[seg.frames.length - 1];
1330
+ return parent && ITEMS.has(parent.nodeName) ? parent : null;
1331
+ }
1332
+ function itemsOfSegs(segs) {
1333
+ const out = [];
1334
+ for (const seg of segs)for(let i = seg.frames.length - 1; i >= 0; i--)if (ITEMS.has(seg.frames[i].nodeName)) {
1335
+ if (!out.includes(seg.frames[i])) out.push(seg.frames[i]);
1336
+ break;
1337
+ }
1338
+ return out;
1339
+ }
1340
+ function convertItem(item, kind) {
1341
+ const wanted = 'task' === kind ? 'taskItem' : 'listItem';
1342
+ if (item.nodeName === wanted) return item.clone();
1343
+ const converted = new __rspack_external_yjs.XmlElement(wanted);
1344
+ if ('taskItem' === wanted) converted.setAttribute('checked', false);
1345
+ copyDir(item, converted);
1346
+ converted.insert(0, item.toArray().map((child)=>child instanceof __rspack_external_yjs.XmlElement || child instanceof __rspack_external_yjs.XmlText ? child.clone() : child));
1347
+ return converted;
1348
+ }
1349
+ function blockRange(segs) {
1350
+ const chains = segs.map((seg)=>[
1351
+ ...seg.frames,
1352
+ seg.el
1353
+ ]);
1354
+ let depth = 0;
1355
+ while(chains.every((chain)=>chain.length > depth + 1 && chain[depth] === chains[0][depth]))depth++;
1356
+ let parent = 0 === depth ? chains[0][0].parent : chains[0][depth - 1];
1357
+ const first = chains[0][depth];
1358
+ const last = chains[chains.length - 1][depth];
1359
+ if (!first || !last) return null;
1360
+ const children = parent.toArray().filter((child)=>child instanceof __rspack_external_yjs.XmlElement);
1361
+ const i = children.indexOf(first);
1362
+ const j = children.indexOf(last);
1363
+ if (i < 0 || j < 0) return null;
1364
+ let blocks = children.slice(i, j + 1);
1365
+ while(parent instanceof __rspack_external_yjs.XmlElement){
1366
+ const name = parent.nodeName;
1367
+ if (LISTS.has(name) || 'table' === name || 'tableRow' === name || ITEMS.has(name) && blocks[0] === parent.get(0)) {
1368
+ blocks = [
1369
+ parent
1370
+ ];
1371
+ parent = parentOf(parent);
1372
+ } else break;
1373
+ }
1374
+ return {
1375
+ parent,
1376
+ blocks
1377
+ };
1378
+ }
1379
+ export { DocModel, LOCAL_ORIGIN, blockTypeOf, indexIn, parentOf, plainContext, removeNode };