@g9tm/lynxcontrols-richtext 2.5.9 → 2.5.11

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.
@@ -0,0 +1,667 @@
1
+ import { TextOp, compose, diff, transform } from "./ot.js";
2
+ import { DocModel, LOCAL_ORIGIN } from "./ymodel.js";
3
+ import * as __rspack_external_yjs from "yjs";
4
+ function _define_property(obj, key, value) {
5
+ if (key in obj) Object.defineProperty(obj, key, {
6
+ value: value,
7
+ enumerable: true,
8
+ configurable: true,
9
+ writable: true
10
+ });
11
+ else obj[key] = value;
12
+ return obj;
13
+ }
14
+ const LINE_CACHE = 64;
15
+ class RichTextSession {
16
+ destroy() {
17
+ var _this_awareness;
18
+ if (this.destroyed) return;
19
+ this.destroyed = true;
20
+ this.offModel();
21
+ if (this.awareness && this.onAwareness) this.awareness.off('change', this.onAwareness);
22
+ null == (_this_awareness = this.awareness) || _this_awareness.setLocalStateField('cursor', null);
23
+ this.undoManager.destroy();
24
+ this.model.destroy();
25
+ }
26
+ reset() {
27
+ this.needFull = true;
28
+ this.ack = 0;
29
+ this.scheduleSend();
30
+ }
31
+ relabel(context) {
32
+ if (context) this.model.setContext(context);
33
+ else this.model.relabel();
34
+ this.modelChanged();
35
+ }
36
+ get plainText() {
37
+ return this.text;
38
+ }
39
+ edit(event) {
40
+ var _ref;
41
+ var _op_toReplacements_;
42
+ if (this.destroyed || event.seq <= this.ack) return;
43
+ this.confirm(event.base);
44
+ this.elemRev = event.base;
45
+ if (this.needFull) {
46
+ this.ack = event.seq;
47
+ this.scheduleSend();
48
+ return;
49
+ }
50
+ const expected = this.queue.length ? this.queue[0].op.baseLength : this.text.length;
51
+ if (event.len !== expected || event.from < 0 || event.to < event.from || event.to > event.len) {
52
+ this.ack = event.seq;
53
+ this.needFull = true;
54
+ this.scheduleSend();
55
+ return;
56
+ }
57
+ let op = TextOp.replace(event.len, event.from, event.to, event.text);
58
+ for (const entry of this.queue){
59
+ const [server, client] = transform(entry.op, op);
60
+ entry.op = server;
61
+ op = client;
62
+ }
63
+ const before = this.text;
64
+ const predicted = op.apply(before);
65
+ const marks = this.storedMarks;
66
+ this.applying = true;
67
+ try {
68
+ this.applyOp(op, marks);
69
+ } finally{
70
+ this.applying = false;
71
+ }
72
+ this.ack = event.seq;
73
+ if (event.text) this.storedMarks = null;
74
+ const after = this.model.flat.text;
75
+ this.rev++;
76
+ const inserted = lastInsertEnd(op);
77
+ const caret = inserted >= 0 ? inserted : null != (_ref = null == (_op_toReplacements_ = op.toReplacements()[0]) ? void 0 : _op_toReplacements_.from) ? _ref : 0;
78
+ if (after !== predicted) this.queue.push({
79
+ rev: this.rev,
80
+ op: diff(predicted, after, Math.min(caret, after.length))
81
+ });
82
+ this.caretAfterEdit = event.text ? lastInsertEnd(op) : -1;
83
+ this.commit(after);
84
+ }
85
+ select(event) {
86
+ var _this_options_onState, _this_options;
87
+ if (this.destroyed || event.seq < this.ack) return;
88
+ this.confirm(event.base);
89
+ if (event.seq > this.ack) return;
90
+ const a = this.toModel(event.a, event.base);
91
+ const h = this.toModel(event.h, event.base);
92
+ if (this.caretAfterEdit < 0 || a !== h || h !== this.caretAfterEdit) this.storedMarks = null;
93
+ this.caretAfterEdit = -1;
94
+ this.sel = {
95
+ a,
96
+ h
97
+ };
98
+ if (void 0 !== event.focus) this.focused = event.focus;
99
+ this.publishCursor();
100
+ null == (_this_options_onState = (_this_options = this.options).onState) || _this_options_onState.call(_this_options);
101
+ }
102
+ applied(event) {
103
+ if (this.destroyed) return;
104
+ this.confirm(event.rev);
105
+ if (event.rev === this.lastFullRev) this.needFull = false;
106
+ }
107
+ action(event) {
108
+ if (this.destroyed) return;
109
+ if ('resync' === event.kind) {
110
+ this.needFull = true;
111
+ this.scheduleSend();
112
+ } else if ('check' === event.kind) this.command(()=>this.model.toggleCheck(event.line));
113
+ else if ('key' === event.kind) {
114
+ const { a, h } = this.sel;
115
+ const from = Math.min(a, h);
116
+ const to = Math.max(a, h);
117
+ switch(event.key){
118
+ case 'undo':
119
+ this.undo();
120
+ break;
121
+ case 'redo':
122
+ this.redo();
123
+ break;
124
+ case 'bold':
125
+ case 'italic':
126
+ case 'underline':
127
+ case 'strike':
128
+ case 'code':
129
+ this.toggleMark(event.key);
130
+ break;
131
+ case 'indent':
132
+ this.command(()=>this.model.indent(from, to));
133
+ break;
134
+ case 'outdent':
135
+ this.command(()=>this.model.outdent(from, to));
136
+ break;
137
+ }
138
+ }
139
+ }
140
+ command(fn, select) {
141
+ var _this_options_onState, _this_options;
142
+ if (this.destroyed) return;
143
+ const before = this.text;
144
+ fn();
145
+ const after = this.model.flat.text;
146
+ if (after === before && !select) return;
147
+ const op = diff(before, after, this.sel.h);
148
+ const next = null == select ? void 0 : select(op);
149
+ if (next) {
150
+ this.pendingSel = next;
151
+ this.sel = next;
152
+ this.publishCursor();
153
+ }
154
+ null == (_this_options_onState = (_this_options = this.options).onState) || _this_options_onState.call(_this_options);
155
+ }
156
+ toggleMark(mark) {
157
+ const { a, h } = this.sel;
158
+ if (a === h) {
159
+ var _this_storedMarks;
160
+ var _this_options_onState, _this_options;
161
+ const current = null != (_this_storedMarks = this.storedMarks) ? _this_storedMarks : this.model.marksAt(h);
162
+ const next = {
163
+ ...current
164
+ };
165
+ if (mark in next) delete next[mark];
166
+ else next[mark] = {};
167
+ this.storedMarks = next;
168
+ this.caretAfterEdit = -1;
169
+ null == (_this_options_onState = (_this_options = this.options).onState) || _this_options_onState.call(_this_options);
170
+ return;
171
+ }
172
+ this.command(()=>this.model.toggleMark(Math.min(a, h), Math.max(a, h), mark));
173
+ }
174
+ setLink(href) {
175
+ const { a, h } = this.sel;
176
+ const from = Math.min(a, h);
177
+ const to = Math.max(a, h);
178
+ if (from === to) {
179
+ if (!href) return;
180
+ this.command(()=>this.model.replace(from, to, href, {
181
+ ...this.model.marksAt(from),
182
+ link: linkAttrs(href)
183
+ }), ()=>({
184
+ a: from + href.length,
185
+ h: from + href.length
186
+ }));
187
+ return;
188
+ }
189
+ this.command(()=>this.model.setMark(from, to, 'link', href ? linkAttrs(href) : null));
190
+ }
191
+ insertInline(name, attrs, range) {
192
+ var _ref, _ref1;
193
+ const from = null != (_ref = null == range ? void 0 : range.from) ? _ref : Math.min(this.sel.a, this.sel.h);
194
+ const to = null != (_ref1 = null == range ? void 0 : range.to) ? _ref1 : Math.max(this.sel.a, this.sel.h);
195
+ this.command(()=>{
196
+ if (to > from) this.model.replace(from, to, '');
197
+ this.model.insertInline(from, name, attrs);
198
+ this.model.insertText(from + 1, ' ', this.model.marksAt(from));
199
+ }, ()=>({
200
+ a: from + 2,
201
+ h: from + 2
202
+ }));
203
+ }
204
+ replaceText(from, to, text) {
205
+ this.command(()=>this.model.replace(from, to, text), ()=>({
206
+ a: from + text.length,
207
+ h: from + text.length
208
+ }));
209
+ }
210
+ insertBlock(kind, attrs = {}) {
211
+ const at = Math.max(this.sel.a, this.sel.h);
212
+ this.command(()=>this.model.insertBlock(at, kind, attrs));
213
+ }
214
+ undo() {
215
+ this.history(()=>this.undoManager.undo());
216
+ }
217
+ redo() {
218
+ this.history(()=>this.undoManager.redo());
219
+ }
220
+ get canUndo() {
221
+ return this.undoManager.undoStack.length > 0;
222
+ }
223
+ get canRedo() {
224
+ return this.undoManager.redoStack.length > 0;
225
+ }
226
+ history(fn) {
227
+ this.command(fn, (op)=>{
228
+ const end = lastInsertEnd(op);
229
+ if (end >= 0) return {
230
+ a: end,
231
+ h: end
232
+ };
233
+ const first = op.toReplacements()[0];
234
+ return first ? {
235
+ a: first.from,
236
+ h: first.from
237
+ } : null;
238
+ });
239
+ }
240
+ setSelection(sel, focus = false) {
241
+ var _this_options_onState, _this_options;
242
+ this.sel = sel;
243
+ this.pendingSel = sel;
244
+ this.focusNext || (this.focusNext = focus);
245
+ this.scheduleSend();
246
+ null == (_this_options_onState = (_this_options = this.options).onState) || _this_options_onState.call(_this_options);
247
+ }
248
+ focus() {
249
+ this.focusNext = true;
250
+ this.scheduleSend();
251
+ }
252
+ state() {
253
+ var _ref, _ref1;
254
+ const { a, h } = this.sel;
255
+ const from = Math.min(a, h);
256
+ const to = Math.max(a, h);
257
+ const marks = from === to && this.storedMarks ? this.storedMarks : this.model.activeMarks(from, to);
258
+ const hit = this.model.locate(h);
259
+ const seg = null == hit ? void 0 : hit.seg;
260
+ const frames = null != (_ref = null == seg ? void 0 : seg.frames) ? _ref : [];
261
+ const lineIndex = this.text.slice(0, h).split('\n').length - 1;
262
+ const line = this.model.flat.lines[lineIndex];
263
+ let list = null;
264
+ for(let i = frames.length - 1; i >= 0 && !list; i--){
265
+ const name = frames[i].nodeName;
266
+ if ('bulletList' === name) list = 'bullet';
267
+ else if ('orderedList' === name) list = 'ordered';
268
+ else if ('taskList' === name) list = 'task';
269
+ }
270
+ const item = [
271
+ ...frames
272
+ ].reverse().find((frame)=>'listItem' === frame.nodeName || 'taskItem' === frame.nodeName);
273
+ const itemIndex = item ? indexOf(item) : -1;
274
+ return {
275
+ marks,
276
+ block: null != (_ref1 = null == line ? void 0 : line.t) ? _ref1 : 'p',
277
+ list,
278
+ quote: frames.some((frame)=>'blockquote' === frame.nodeName),
279
+ callout: (()=>{
280
+ var _callout_getAttribute;
281
+ const callout = [
282
+ ...frames
283
+ ].reverse().find((frame)=>'callout' === frame.nodeName);
284
+ return callout ? String(null != (_callout_getAttribute = callout.getAttribute('tone')) ? _callout_getAttribute : 'info') : null;
285
+ })(),
286
+ dir: seg ? this.model.directionAt(seg.start) : null,
287
+ canIndent: itemIndex > 0,
288
+ canOutdent: Boolean(item),
289
+ inTable: frames.some((frame)=>'table' === frame.nodeName)
290
+ };
291
+ }
292
+ modelChanged() {
293
+ if (this.applying || this.destroyed) return;
294
+ const after = this.model.flat.text;
295
+ const before = this.text;
296
+ this.rev++;
297
+ if (after !== before) {
298
+ const op = diff(before, after, this.sel.h);
299
+ this.queue.push({
300
+ rev: this.rev,
301
+ op
302
+ });
303
+ this.sel = {
304
+ a: op.map(this.sel.a, -1),
305
+ h: op.map(this.sel.h, -1)
306
+ };
307
+ }
308
+ this.commit(after);
309
+ }
310
+ commit(after) {
311
+ var _this_options_onText, _this_options, _this_options_onState, _this_options1;
312
+ const changed = after !== this.text;
313
+ this.text = after;
314
+ this.linesAt.set(this.rev, this.model.flat.lines);
315
+ if (changed) null == (_this_options_onText = (_this_options = this.options).onText) || _this_options_onText.call(_this_options);
316
+ null == (_this_options_onState = (_this_options1 = this.options).onState) || _this_options_onState.call(_this_options1);
317
+ this.publishCursor();
318
+ this.scheduleSend();
319
+ }
320
+ confirm(rev) {
321
+ if (rev <= this.confirmed) return;
322
+ this.confirmed = rev;
323
+ this.queue = this.queue.filter((entry)=>entry.rev > rev);
324
+ for (const key of this.linesAt.keys())if (key < rev) this.linesAt.delete(key);
325
+ }
326
+ toModel(pos, base) {
327
+ let p = pos;
328
+ for (const entry of this.queue)if (entry.rev > base) p = entry.op.map(Math.min(p, entry.op.baseLength), 1);
329
+ return Math.max(0, Math.min(p, this.text.length));
330
+ }
331
+ scheduleSend() {
332
+ if (this.sendTimer || this.destroyed) return;
333
+ this.sendTimer = true;
334
+ Promise.resolve().then(()=>{
335
+ this.sendTimer = false;
336
+ if (!this.destroyed) this.flush();
337
+ });
338
+ }
339
+ flush() {
340
+ const lines = this.model.flat.lines;
341
+ const message = {
342
+ rev: this.rev,
343
+ ack: this.ack
344
+ };
345
+ const known = this.linesAt.get(this.elemRev);
346
+ if (this.needFull || !known) {
347
+ message.full = {
348
+ text: this.text,
349
+ lines
350
+ };
351
+ this.queue = [];
352
+ this.lastFullRev = this.rev;
353
+ this.needFull = true;
354
+ } else {
355
+ const pending = this.queue.filter((entry)=>entry.rev > this.elemRev);
356
+ if (pending.length) {
357
+ let op = pending[0].op;
358
+ for(let i = 1; i < pending.length; i++)op = compose(op, pending[i].op);
359
+ const ops = op.toReplacements();
360
+ if (ops.length) message.ops = ops;
361
+ }
362
+ const splice = spliceOf(known, lines);
363
+ if (splice) message.lines = {
364
+ ...splice,
365
+ of: this.elemRev
366
+ };
367
+ }
368
+ if (this.pendingSel) {
369
+ message.sel = this.pendingSel;
370
+ this.pendingSel = null;
371
+ }
372
+ if (this.focusNext) {
373
+ message.focus = true;
374
+ this.focusNext = false;
375
+ }
376
+ const peers = this.peers();
377
+ if (peers) message.peers = peers;
378
+ this.elemRev = this.rev;
379
+ if (this.linesAt.size > LINE_CACHE) {
380
+ const keys = [
381
+ ...this.linesAt.keys()
382
+ ].sort((x, y)=>x - y);
383
+ for (const key of keys.slice(0, keys.length - LINE_CACHE))if (key !== this.confirmed) this.linesAt.delete(key);
384
+ }
385
+ this.port.send(message);
386
+ }
387
+ applyOp(op, marks) {
388
+ const ranges = [];
389
+ let at = 0;
390
+ let current = null;
391
+ for (const c of op.ops)if ('number' == typeof c && c > 0) {
392
+ if (current) ranges.push(current);
393
+ current = null;
394
+ at += c;
395
+ } else if ('number' == typeof c) {
396
+ null != current || (current = {
397
+ from: at,
398
+ to: at,
399
+ text: ''
400
+ });
401
+ current.to += -c;
402
+ at += -c;
403
+ } else {
404
+ null != current || (current = {
405
+ from: at,
406
+ to: at,
407
+ text: ''
408
+ });
409
+ current.text += c;
410
+ }
411
+ if (current) ranges.push(current);
412
+ for(let i = ranges.length - 1; i >= 0; i--){
413
+ const range = ranges[i];
414
+ this.model.replace(range.from, range.to, range.text, marks);
415
+ if (' ' === range.text) this.inputRules(range.from);
416
+ }
417
+ }
418
+ inputRules(spaceAt) {
419
+ const hit = this.model.locate(spaceAt);
420
+ if (!hit || 'text' !== hit.seg.kind) return;
421
+ const { seg, off } = hit;
422
+ const head = seg.text.slice(0, off);
423
+ const start = seg.start;
424
+ const strip = ()=>this.model.replace(start, start + off + 1, '');
425
+ const parent = seg.frames[seg.frames.length - 1];
426
+ const inItem = parent && ('listItem' === parent.nodeName || 'taskItem' === parent.nodeName);
427
+ const match = /^(#{1,3})$/.exec(head);
428
+ if (match && !inItem) {
429
+ strip();
430
+ this.model.setBlockType(start, start, `h${match[1].length}`);
431
+ } else if (/^[-*+]$/.test(head)) {
432
+ strip();
433
+ this.model.toggleList(start, start, 'bullet');
434
+ } else if (/^(1|۱)[.)]$/.test(head)) {
435
+ strip();
436
+ this.model.toggleList(start, start, 'ordered');
437
+ } else if (/^\[( |x)?\]$/.test(head)) {
438
+ strip();
439
+ this.model.toggleList(start, start, 'task');
440
+ if ('[x]' === head) this.model.toggleCheck(this.text.slice(0, start).split('\n').length - 1);
441
+ } else if ('>' === head) {
442
+ strip();
443
+ this.model.toggleQuote(start, start);
444
+ } else if ('```' === head && !inItem) {
445
+ strip();
446
+ this.model.setBlockType(start, start, 'code');
447
+ }
448
+ }
449
+ publishCursor() {
450
+ var _awareness_getLocalState;
451
+ const awareness = this.awareness;
452
+ if (!awareness || this.destroyed) return;
453
+ if (!this.focused) {
454
+ var _awareness_getLocalState1;
455
+ if (null == (_awareness_getLocalState1 = awareness.getLocalState()) ? void 0 : _awareness_getLocalState1.cursor) awareness.setLocalStateField('cursor', null);
456
+ return;
457
+ }
458
+ const anchor = this.relative(this.sel.a);
459
+ const head = this.relative(this.sel.h);
460
+ if (!anchor || !head) return;
461
+ const state = null != (_awareness_getLocalState = awareness.getLocalState()) ? _awareness_getLocalState : {};
462
+ if (this.options.user && !state.user) awareness.setLocalStateField('user', this.options.user);
463
+ awareness.setLocalStateField('cursor', {
464
+ anchor: __rspack_external_yjs.relativePositionToJSON(anchor),
465
+ head: __rspack_external_yjs.relativePositionToJSON(head)
466
+ });
467
+ }
468
+ relative(pos) {
469
+ const hit = this.model.locate(pos);
470
+ if (!hit) return __rspack_external_yjs.createRelativePositionFromTypeIndex(this.model.root, 0, -1);
471
+ const { seg, off } = hit;
472
+ if ('atom' === seg.kind) {
473
+ const parent = seg.el.parent;
474
+ return __rspack_external_yjs.createRelativePositionFromTypeIndex(parent, childIndex(parent, seg.el) + (off > 0 ? 1 : 0));
475
+ }
476
+ for (const piece of seg.pieces)if ('text' === piece.k && off >= piece.at && off <= piece.at + piece.len) return __rspack_external_yjs.createRelativePositionFromTypeIndex(piece.y, off - piece.at, -1);
477
+ let index = seg.el.length;
478
+ for (const piece of seg.pieces)if (piece.at >= off) {
479
+ index = piece.idx;
480
+ break;
481
+ }
482
+ return __rspack_external_yjs.createRelativePositionFromTypeIndex(seg.el, index);
483
+ }
484
+ absolute(rel) {
485
+ const abs = __rspack_external_yjs.createAbsolutePositionFromRelativePosition(rel, this.model.doc);
486
+ if (!abs) return null;
487
+ const segs = this.model.flat.segs;
488
+ const type = abs.type;
489
+ if (type instanceof __rspack_external_yjs.XmlText) {
490
+ for (const seg of segs)for (const piece of seg.pieces)if ('text' === piece.k && piece.y === type) return seg.start + piece.at + Math.min(abs.index, piece.len);
491
+ return null;
492
+ }
493
+ const direct = segs.find((seg)=>seg.el === type);
494
+ if (direct) {
495
+ if ('atom' === direct.kind) return direct.start;
496
+ for (const piece of direct.pieces)if (piece.idx >= abs.index) return direct.start + piece.at;
497
+ return direct.start + direct.len;
498
+ }
499
+ const children = type.toArray();
500
+ const child = children[abs.index];
501
+ const inside = (seg, el)=>seg.el === el || seg.frames.includes(el);
502
+ if (child) {
503
+ const first = segs.find((seg)=>inside(seg, child));
504
+ if (first) return first.start;
505
+ }
506
+ const last = [
507
+ ...segs
508
+ ].reverse().find((seg)=>type === this.model.root || seg.frames.includes(type));
509
+ return last ? last.start + last.len : null;
510
+ }
511
+ peers() {
512
+ const awareness = this.awareness;
513
+ if (!awareness) return null;
514
+ const out = [];
515
+ for (const [client, state] of awareness.getStates()){
516
+ if (client === awareness.clientID) continue;
517
+ const cursor = state.cursor;
518
+ if ((null == cursor ? void 0 : cursor.anchor) && cursor.head) try {
519
+ var _ref;
520
+ var _this_options_peerName, _this_options;
521
+ const a = this.absolute(__rspack_external_yjs.createRelativePositionFromJSON(cursor.anchor));
522
+ const h = this.absolute(__rspack_external_yjs.createRelativePositionFromJSON(cursor.head));
523
+ if (null === a || null === h) continue;
524
+ const who = null != (_ref = null == (_this_options_peerName = (_this_options = this.options).peerName) ? void 0 : _this_options_peerName.call(_this_options, client, state)) ? _ref : userOf(state);
525
+ if (!who) continue;
526
+ out.push({
527
+ a,
528
+ h,
529
+ c: who.color,
530
+ n: who.name
531
+ });
532
+ } catch {}
533
+ }
534
+ return out;
535
+ }
536
+ searchText() {
537
+ return this.model.flat.segs.map((seg)=>{
538
+ if ('atom' === seg.kind) return '';
539
+ let text = '';
540
+ for (const piece of seg.pieces){
541
+ var _piece_y_getAttribute, _piece_y_getAttribute1;
542
+ if ('text' === piece.k) text += piece.delta.map((op)=>op.insert).join('');
543
+ else if ('hardBreak' === piece.name) text += '\n';
544
+ else if ('mention' === piece.name) text += `@${String(null != (_piece_y_getAttribute = piece.y.getAttribute('label')) ? _piece_y_getAttribute : '')}`;
545
+ else if ('dateChip' === piece.name) text += String(null != (_piece_y_getAttribute1 = piece.y.getAttribute('date')) ? _piece_y_getAttribute1 : '');
546
+ }
547
+ return text;
548
+ }).join('\n\n');
549
+ }
550
+ constructor(options){
551
+ _define_property(this, "options", void 0);
552
+ _define_property(this, "model", void 0);
553
+ _define_property(this, "undoManager", void 0);
554
+ _define_property(this, "port", void 0);
555
+ _define_property(this, "awareness", void 0);
556
+ _define_property(this, "rev", void 0);
557
+ _define_property(this, "queue", void 0);
558
+ _define_property(this, "confirmed", void 0);
559
+ _define_property(this, "elemRev", void 0);
560
+ _define_property(this, "ack", void 0);
561
+ _define_property(this, "text", void 0);
562
+ _define_property(this, "linesAt", void 0);
563
+ _define_property(this, "needFull", void 0);
564
+ _define_property(this, "sendTimer", void 0);
565
+ _define_property(this, "applying", void 0);
566
+ _define_property(this, "pendingSel", void 0);
567
+ _define_property(this, "focusNext", void 0);
568
+ _define_property(this, "storedMarks", void 0);
569
+ _define_property(this, "caretAfterEdit", void 0);
570
+ _define_property(this, "focused", void 0);
571
+ _define_property(this, "destroyed", void 0);
572
+ _define_property(this, "sel", void 0);
573
+ _define_property(this, "offModel", void 0);
574
+ _define_property(this, "onAwareness", void 0);
575
+ _define_property(this, "lastFullRev", void 0);
576
+ this.options = options;
577
+ this.rev = 1;
578
+ this.queue = [];
579
+ this.confirmed = 1;
580
+ this.elemRev = 1;
581
+ this.ack = 0;
582
+ this.linesAt = new Map();
583
+ this.needFull = true;
584
+ this.sendTimer = false;
585
+ this.applying = false;
586
+ this.pendingSel = null;
587
+ this.focusNext = false;
588
+ this.storedMarks = null;
589
+ this.caretAfterEdit = -1;
590
+ this.focused = false;
591
+ this.destroyed = false;
592
+ this.sel = {
593
+ a: 0,
594
+ h: 0
595
+ };
596
+ this.lastFullRev = -1;
597
+ this.port = options.port;
598
+ this.awareness = options.awareness;
599
+ this.model = new DocModel(options.doc, options.context);
600
+ this.text = this.model.flat.text;
601
+ this.linesAt.set(this.rev, this.model.flat.lines);
602
+ this.undoManager = new __rspack_external_yjs.UndoManager(this.model.root, {
603
+ trackedOrigins: new Set([
604
+ LOCAL_ORIGIN
605
+ ]),
606
+ captureTimeout: 600
607
+ });
608
+ const listener = ()=>this.modelChanged();
609
+ this.model.root.observeDeep(listener);
610
+ this.offModel = ()=>this.model.root.unobserveDeep(listener);
611
+ if (this.awareness) {
612
+ this.onAwareness = ()=>this.scheduleSend();
613
+ this.awareness.on('change', this.onAwareness);
614
+ }
615
+ }
616
+ }
617
+ function linkAttrs(href) {
618
+ return {
619
+ href,
620
+ target: '_blank',
621
+ rel: 'noopener noreferrer nofollow',
622
+ class: null,
623
+ title: null
624
+ };
625
+ }
626
+ function userOf(state) {
627
+ const user = state.user;
628
+ if (!user || 'string' != typeof user.name) return null;
629
+ return {
630
+ name: user.name,
631
+ color: 'string' == typeof user.color ? user.color : '#888888'
632
+ };
633
+ }
634
+ function lastInsertEnd(op) {
635
+ let target = 0;
636
+ let end = -1;
637
+ for (const c of op.ops)if ('string' == typeof c) {
638
+ target += c.length;
639
+ end = target;
640
+ } else if (c > 0) target += c;
641
+ return end;
642
+ }
643
+ function childIndex(parent, child) {
644
+ return parent.toArray().indexOf(child);
645
+ }
646
+ function indexOf(el) {
647
+ const parent = el.parent;
648
+ return parent ? parent.toArray().indexOf(el) : -1;
649
+ }
650
+ function spliceOf(before, after) {
651
+ if (before === after) return null;
652
+ const same = (x, y)=>x === y || JSON.stringify(x) === JSON.stringify(y);
653
+ let start = 0;
654
+ const max = Math.min(before.length, after.length);
655
+ while(start < max && same(before[start], after[start]))start++;
656
+ let end = 0;
657
+ while(end < max - start && same(before[before.length - 1 - end], after[after.length - 1 - end]))end++;
658
+ if (start === before.length && start === after.length) return null;
659
+ return {
660
+ at: start,
661
+ del: before.length - start - end,
662
+ ins: after.slice(start, after.length - end)
663
+ };
664
+ }
665
+ export { RichTextSession, spliceOf };
666
+
667
+ //# sourceMappingURL=session.js.map