@iyio/rtxt 0.0.2

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 (75) hide show
  1. package/esm/package.json +3 -0
  2. package/esm/src/index.d.ts +9 -0
  3. package/esm/src/index.js +10 -0
  4. package/esm/src/index.js.map +1 -0
  5. package/esm/src/lib/RTxtEditor.d.ts +47 -0
  6. package/esm/src/lib/RTxtEditor.js +635 -0
  7. package/esm/src/lib/RTxtEditor.js.map +1 -0
  8. package/esm/src/lib/RTxtRenderer.d.ts +32 -0
  9. package/esm/src/lib/RTxtRenderer.js +370 -0
  10. package/esm/src/lib/RTxtRenderer.js.map +1 -0
  11. package/esm/src/lib/rtxt-descriptors.d.ts +2 -0
  12. package/esm/src/lib/rtxt-descriptors.js +92 -0
  13. package/esm/src/lib/rtxt-descriptors.js.map +1 -0
  14. package/esm/src/lib/rtxt-icons.d.ts +11 -0
  15. package/esm/src/lib/rtxt-icons.js +29 -0
  16. package/esm/src/lib/rtxt-icons.js.map +1 -0
  17. package/esm/src/lib/rtxt-internal.d.ts +10 -0
  18. package/esm/src/lib/rtxt-internal.js +63 -0
  19. package/esm/src/lib/rtxt-internal.js.map +1 -0
  20. package/esm/src/lib/rtxt-lib.d.ts +22 -0
  21. package/esm/src/lib/rtxt-lib.js +336 -0
  22. package/esm/src/lib/rtxt-lib.js.map +1 -0
  23. package/esm/src/lib/rtxt-style-editor.d.ts +2 -0
  24. package/esm/src/lib/rtxt-style-editor.js +131 -0
  25. package/esm/src/lib/rtxt-style-editor.js.map +1 -0
  26. package/esm/src/lib/rtxt-style-renderer.d.ts +2 -0
  27. package/esm/src/lib/rtxt-style-renderer.js +98 -0
  28. package/esm/src/lib/rtxt-style-renderer.js.map +1 -0
  29. package/esm/src/lib/rtxt-tool-lib.d.ts +34 -0
  30. package/esm/src/lib/rtxt-tool-lib.js +151 -0
  31. package/esm/src/lib/rtxt-tool-lib.js.map +1 -0
  32. package/esm/src/lib/rtxt-tools.d.ts +3 -0
  33. package/esm/src/lib/rtxt-tools.js +200 -0
  34. package/esm/src/lib/rtxt-tools.js.map +1 -0
  35. package/esm/src/lib/rtxt-types.d.ts +203 -0
  36. package/esm/src/lib/rtxt-types.js +16 -0
  37. package/esm/src/lib/rtxt-types.js.map +1 -0
  38. package/package.json +17 -0
  39. package/src/index.d.ts +9 -0
  40. package/src/index.js +13 -0
  41. package/src/index.js.map +1 -0
  42. package/src/lib/RTxtEditor.d.ts +47 -0
  43. package/src/lib/RTxtEditor.js +639 -0
  44. package/src/lib/RTxtEditor.js.map +1 -0
  45. package/src/lib/RTxtRenderer.d.ts +32 -0
  46. package/src/lib/RTxtRenderer.js +374 -0
  47. package/src/lib/RTxtRenderer.js.map +1 -0
  48. package/src/lib/rtxt-descriptors.d.ts +2 -0
  49. package/src/lib/rtxt-descriptors.js +96 -0
  50. package/src/lib/rtxt-descriptors.js.map +1 -0
  51. package/src/lib/rtxt-icons.d.ts +11 -0
  52. package/src/lib/rtxt-icons.js +33 -0
  53. package/src/lib/rtxt-icons.js.map +1 -0
  54. package/src/lib/rtxt-internal.d.ts +10 -0
  55. package/src/lib/rtxt-internal.js +71 -0
  56. package/src/lib/rtxt-internal.js.map +1 -0
  57. package/src/lib/rtxt-lib.d.ts +22 -0
  58. package/src/lib/rtxt-lib.js +356 -0
  59. package/src/lib/rtxt-lib.js.map +1 -0
  60. package/src/lib/rtxt-style-editor.d.ts +2 -0
  61. package/src/lib/rtxt-style-editor.js +135 -0
  62. package/src/lib/rtxt-style-editor.js.map +1 -0
  63. package/src/lib/rtxt-style-renderer.d.ts +2 -0
  64. package/src/lib/rtxt-style-renderer.js +102 -0
  65. package/src/lib/rtxt-style-renderer.js.map +1 -0
  66. package/src/lib/rtxt-tool-lib.d.ts +34 -0
  67. package/src/lib/rtxt-tool-lib.js +158 -0
  68. package/src/lib/rtxt-tool-lib.js.map +1 -0
  69. package/src/lib/rtxt-tools.d.ts +3 -0
  70. package/src/lib/rtxt-tools.js +205 -0
  71. package/src/lib/rtxt-tools.js.map +1 -0
  72. package/src/lib/rtxt-types.d.ts +203 -0
  73. package/src/lib/rtxt-types.js +19 -0
  74. package/src/lib/rtxt-types.js.map +1 -0
  75. package/src/package.json +4 -0
@@ -0,0 +1,47 @@
1
+ import { ReadonlySubject } from "@iyio/common";
2
+ import { RTxtRenderer } from "./RTxtRenderer";
3
+ import { RTxtAlignment, RTxtEditorOptions, RTxtLine, RTxtSelection, RTxtTool } from "./rtxt-types";
4
+ export declare class RTxtEditor {
5
+ private readonly _renderer;
6
+ get rendererSubject(): ReadonlySubject<RTxtRenderer | null>;
7
+ get renderer(): RTxtRenderer | null;
8
+ set renderer(value: RTxtRenderer | null);
9
+ menu: HTMLElement | null;
10
+ private readonly _selection;
11
+ get selectionSubject(): ReadonlySubject<RTxtSelection | null>;
12
+ get selection(): RTxtSelection | null;
13
+ readonly tools: RTxtTool[];
14
+ readonly options: RTxtEditorOptions;
15
+ readonly classPrefix: string;
16
+ constructor(options?: RTxtEditorOptions);
17
+ private _isDisposed;
18
+ get isDisposed(): boolean;
19
+ dispose(): void;
20
+ private readonly onInput;
21
+ private readonly onFocus;
22
+ private readonly onBlur;
23
+ private readonly bodyMouseDown;
24
+ private readonly bodyMouseUp;
25
+ private readonly onSelectionChange;
26
+ private deselectAllTools;
27
+ private setToolSelection;
28
+ private toolElemGroupings;
29
+ private renderMenu;
30
+ private menuVisible;
31
+ showMenu(left: number, top: number, bottom: number): void;
32
+ hideMenu(): void;
33
+ toggleSelection(selection: RTxtSelection, nodeType: string, atts?: Record<string, string>): void;
34
+ applyToSection(selection: RTxtSelection, nodeType: string, atts?: Record<string, string>): void;
35
+ removeFromSelection(selection: RTxtSelection, nodeType: string): void;
36
+ someHaveNodeType(selection: RTxtSelection, nodeType: string): boolean;
37
+ allHaveNodeType(selection: RTxtSelection, nodeType: string): boolean;
38
+ syncElemToDoc(): void;
39
+ getElementAt(index: number): Element | undefined;
40
+ getStyleAt(index: number): CSSStyleDeclaration | undefined;
41
+ getCursorLine(): RTxtLine | undefined;
42
+ getSelectedLines(): RTxtLine[];
43
+ setSelectedLineAlignment(align: RTxtAlignment): void;
44
+ private readonly rendererQueue;
45
+ queueRenderer(renderer: RTxtRenderer): void;
46
+ dequeueRenderer(renderer: RTxtRenderer): void;
47
+ }
@@ -0,0 +1,639 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RTxtEditor = void 0;
4
+ const common_1 = require("@iyio/common");
5
+ const rxjs_1 = require("rxjs");
6
+ const RTxtRenderer_1 = require("./RTxtRenderer");
7
+ const rtxt_internal_1 = require("./rtxt-internal");
8
+ const rtxt_lib_1 = require("./rtxt-lib");
9
+ const rtxt_style_editor_1 = require("./rtxt-style-editor");
10
+ const rtxt_tools_1 = require("./rtxt-tools");
11
+ const rtxt_types_1 = require("./rtxt-types");
12
+ class RTxtEditor {
13
+ get rendererSubject() { return this._renderer; }
14
+ get renderer() { return this._renderer.value; }
15
+ set renderer(value) {
16
+ if (value == this._renderer.value || (this._isDisposed && value)) {
17
+ return;
18
+ }
19
+ if (this._renderer.value) {
20
+ this._renderer.value.editModeRefCount--;
21
+ if (this._renderer.value.elem) {
22
+ this._renderer.value.elem.removeEventListener('input', this.onInput);
23
+ this._renderer.value.elem.removeEventListener('focus', this.onFocus);
24
+ this._renderer.value.elem.removeEventListener('blur', this.onBlur);
25
+ this._renderer.value.elem.classList.remove(`${this._renderer.value.classPrefix}focus`);
26
+ }
27
+ }
28
+ if (value) {
29
+ value.editModeRefCount++;
30
+ if (value.elem) {
31
+ value.elem.addEventListener('input', this.onInput);
32
+ value.elem.addEventListener('focus', this.onFocus);
33
+ value.elem.addEventListener('blur', this.onBlur);
34
+ }
35
+ }
36
+ else {
37
+ this._selection.next(null);
38
+ this.deselectAllTools();
39
+ this.hideMenu();
40
+ }
41
+ this._renderer.next(value);
42
+ }
43
+ get selectionSubject() { return this._selection; }
44
+ get selection() { return this._selection.value; }
45
+ constructor(options = {}) {
46
+ var _a, _b, _c, _d, _e;
47
+ this._renderer = new rxjs_1.BehaviorSubject(null);
48
+ this._selection = new rxjs_1.BehaviorSubject(null);
49
+ this._isDisposed = false;
50
+ this.onInput = () => {
51
+ var _a;
52
+ this.syncElemToDoc();
53
+ (_a = this.renderer) === null || _a === void 0 ? void 0 : _a.queueExport();
54
+ };
55
+ this.onFocus = () => {
56
+ var _a, _b;
57
+ (_b = (_a = this.renderer) === null || _a === void 0 ? void 0 : _a.elem) === null || _b === void 0 ? void 0 : _b.classList.add(`${this.renderer.classPrefix}focus`);
58
+ };
59
+ this.onBlur = () => {
60
+ var _a, _b;
61
+ (_b = (_a = this.renderer) === null || _a === void 0 ? void 0 : _a.elem) === null || _b === void 0 ? void 0 : _b.classList.remove(`${this.renderer.classPrefix}focus`);
62
+ };
63
+ this.bodyMouseDown = (e) => {
64
+ if (!this.menu || !(e.target instanceof Node)) {
65
+ return;
66
+ }
67
+ if (!(0, common_1.isDomNodeDescendantOf)(e.target, this.menu, true)) {
68
+ this.menu.classList.add(`${this.classPrefix}disable-mouse`);
69
+ }
70
+ };
71
+ this.bodyMouseUp = () => {
72
+ var _a;
73
+ (_a = this.menu) === null || _a === void 0 ? void 0 : _a.classList.remove(`${this.classPrefix}disable-mouse`);
74
+ };
75
+ this.onSelectionChange = () => {
76
+ var _a;
77
+ if (!this.renderer) {
78
+ return;
79
+ }
80
+ const sel = (_a = globalThis.document) === null || _a === void 0 ? void 0 : _a.getSelection();
81
+ if (this.renderer.isEmpty ||
82
+ !sel ||
83
+ !sel.rangeCount ||
84
+ !sel.anchorNode ||
85
+ !sel.focusNode ||
86
+ (sel.anchorNode === sel.focusNode && sel.anchorOffset === sel.focusOffset) ||
87
+ !(0, common_1.isDomNodeDescendantOf)(sel === null || sel === void 0 ? void 0 : sel.anchorNode, this.renderer.elem, true) ||
88
+ !(0, common_1.isDomNodeDescendantOf)(sel === null || sel === void 0 ? void 0 : sel.focusNode, this.renderer.elem, true)) {
89
+ if (!sel ||
90
+ (!(0, common_1.isDomNodeDescendantOf)(sel.anchorNode, this.menu, false) &&
91
+ !(0, common_1.isDomNodeDescendantOf)(sel.focusNode, this.menu, false))) {
92
+ this._selection.next(null);
93
+ this.deselectAllTools();
94
+ this.hideMenu();
95
+ }
96
+ return;
97
+ }
98
+ const selection = (0, rtxt_lib_1.getRTxtSelection)(sel, this.renderer.nodeLookup, this.renderer.elem);
99
+ this._selection.next(selection);
100
+ //console.info('selection',sel,selection,selection?rTxtSelectionToString(selection,this.renderer.nodeLookup):null);
101
+ const pos = sel.getRangeAt(0).getBoundingClientRect();
102
+ if (selection) {
103
+ this.setToolSelection(selection);
104
+ }
105
+ else {
106
+ this.deselectAllTools();
107
+ }
108
+ this.showMenu(pos.left + pos.width / 2, pos.top, pos.bottom);
109
+ };
110
+ this.toolElemGroupings = [];
111
+ this.menuVisible = false;
112
+ this.rendererQueue = [];
113
+ const { renderer, useCustomStyleSheet, tools = (0, rtxt_tools_1.getDefaultRTxtTools)(renderer instanceof RTxtRenderer_1.RTxtRenderer ? renderer.options : renderer), classPrefix, } = options;
114
+ this.options = options;
115
+ if (renderer instanceof RTxtRenderer_1.RTxtRenderer) {
116
+ this.renderer = renderer;
117
+ }
118
+ else if (renderer) {
119
+ this.renderer = new RTxtRenderer_1.RTxtRenderer(renderer, true);
120
+ }
121
+ this.classPrefix = (_b = classPrefix !== null && classPrefix !== void 0 ? classPrefix : (_a = this.renderer) === null || _a === void 0 ? void 0 : _a.options.classPrefix) !== null && _b !== void 0 ? _b : rtxt_types_1.defaultRTxtClassNamePrefix;
122
+ this.tools = tools;
123
+ if (!useCustomStyleSheet) {
124
+ (0, rtxt_style_editor_1.insertRTxtEditorStyleSheet)();
125
+ }
126
+ (_c = globalThis.document) === null || _c === void 0 ? void 0 : _c.addEventListener('selectionchange', this.onSelectionChange);
127
+ (_d = globalThis.document) === null || _d === void 0 ? void 0 : _d.body.addEventListener('mousedown', this.bodyMouseDown);
128
+ (_e = globalThis.document) === null || _e === void 0 ? void 0 : _e.body.addEventListener('mouseup', this.bodyMouseUp);
129
+ this.menu = this.renderMenu();
130
+ }
131
+ get isDisposed() { return this._isDisposed; }
132
+ dispose() {
133
+ var _a, _b, _c, _d;
134
+ if (this._isDisposed) {
135
+ return;
136
+ }
137
+ this._isDisposed = true;
138
+ (_a = globalThis.document) === null || _a === void 0 ? void 0 : _a.removeEventListener('selectionchange', this.onSelectionChange);
139
+ (_b = globalThis.document) === null || _b === void 0 ? void 0 : _b.body.removeEventListener('mousedown', this.bodyMouseDown);
140
+ (_c = globalThis.document) === null || _c === void 0 ? void 0 : _c.body.removeEventListener('mouseup', this.bodyMouseUp);
141
+ (_d = this.menu) === null || _d === void 0 ? void 0 : _d.remove();
142
+ this.renderer = null;
143
+ }
144
+ deselectAllTools() {
145
+ var _a, _b;
146
+ const className = `${this.classPrefix}selected`;
147
+ for (const group of this.toolElemGroupings) {
148
+ for (const elem of group.elems) {
149
+ elem.classList.remove(className);
150
+ }
151
+ (_b = (_a = group.tool).onSelectionChange) === null || _b === void 0 ? void 0 : _b.call(_a, {
152
+ editor: this,
153
+ selection: null,
154
+ descriptors: [],
155
+ nodes: [],
156
+ lines: [],
157
+ toolElems: group.elems,
158
+ toolElem: (0, rtxt_internal_1.getToolElem)(group.elems),
159
+ });
160
+ }
161
+ }
162
+ setToolSelection(selection) {
163
+ var _a, _b, _c, _d;
164
+ if (!this.renderer) {
165
+ this.deselectAllTools();
166
+ return;
167
+ }
168
+ const className = `${this.classPrefix}selected`;
169
+ const singleAry = [''];
170
+ const elemAtCursor = this.getElementAt(selection.cursorIndex);
171
+ const styleAtCursor = elemAtCursor ? (_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.getComputedStyle) === null || _b === void 0 ? void 0 : _b.call(_a, (0, rtxt_internal_1.getDeepestElem)(elemAtCursor)) : undefined;
172
+ const colorAtCursor = (styleAtCursor === null || styleAtCursor === void 0 ? void 0 : styleAtCursor.color) ? (0, common_1.convertRgbToHex)(styleAtCursor.color) : undefined;
173
+ const sizeAtCursor = styleAtCursor === null || styleAtCursor === void 0 ? void 0 : styleAtCursor.fontSize;
174
+ const nodeAtCursor = this.renderer.nodeLookup[selection.cursorIndex];
175
+ for (const group of this.toolElemGroupings) {
176
+ const types = [];
177
+ const nodes = [];
178
+ const lines = [];
179
+ for (const node of selection.nodes) {
180
+ let nodeTypes;
181
+ if (!node.t) {
182
+ continue;
183
+ }
184
+ if (typeof node.t === 'string') {
185
+ singleAry[0] = node.t;
186
+ nodeTypes = singleAry;
187
+ }
188
+ else {
189
+ nodeTypes = node.t;
190
+ }
191
+ for (const type of nodeTypes) {
192
+ if (group.tool.types.includes(type)) {
193
+ nodes.push(node);
194
+ const nodeLine = (0, rtxt_lib_1.getRTxtNodeLine)(this.renderer.doc, node);
195
+ if (nodeLine && !lines.includes(nodeLine)) {
196
+ lines.push(nodeLine);
197
+ }
198
+ if (!types.includes(type)) {
199
+ types.push(type);
200
+ }
201
+ }
202
+ }
203
+ }
204
+ let toolSel;
205
+ if (types.length) {
206
+ const decs = [];
207
+ for (const type of types) {
208
+ const desc = this.renderer.descriptors[type];
209
+ if (!desc) {
210
+ continue;
211
+ }
212
+ decs.push(desc);
213
+ }
214
+ toolSel = {
215
+ editor: this,
216
+ selection,
217
+ descriptors: decs,
218
+ nodes,
219
+ lines,
220
+ toolElems: group.elems,
221
+ toolElem: (0, rtxt_internal_1.getToolElem)(group.elems),
222
+ elemAtCursor,
223
+ styleAtCursor,
224
+ colorAtCursor,
225
+ sizeAtCursor,
226
+ nodeAtCursor,
227
+ };
228
+ }
229
+ else {
230
+ toolSel = {
231
+ editor: this,
232
+ selection,
233
+ descriptors: [],
234
+ nodes: [],
235
+ lines,
236
+ toolElems: group.elems,
237
+ toolElem: (0, rtxt_internal_1.getToolElem)(group.elems),
238
+ elemAtCursor,
239
+ styleAtCursor,
240
+ colorAtCursor,
241
+ sizeAtCursor,
242
+ nodeAtCursor,
243
+ };
244
+ }
245
+ for (const elem of group.elems) {
246
+ if (group.tool.isSelected) {
247
+ if (group.tool.isSelected(toolSel)) {
248
+ elem.classList.add(className);
249
+ }
250
+ else {
251
+ elem.classList.remove(className);
252
+ }
253
+ }
254
+ else {
255
+ if (types.length) {
256
+ elem.classList.add(className);
257
+ }
258
+ else {
259
+ elem.classList.remove(className);
260
+ }
261
+ }
262
+ }
263
+ (_d = (_c = group.tool).onSelectionChange) === null || _d === void 0 ? void 0 : _d.call(_c, toolSel);
264
+ }
265
+ }
266
+ renderMenu() {
267
+ var _a, _b, _c;
268
+ const container = (_a = globalThis.document) === null || _a === void 0 ? void 0 : _a.createElement('div');
269
+ const menu = (_b = globalThis.document) === null || _b === void 0 ? void 0 : _b.createElement('div');
270
+ if (!container || !menu) {
271
+ return null;
272
+ }
273
+ container.classList.add(this.classPrefix + 'editor-menu-container');
274
+ container.classList.add(this.classPrefix + 'editor-menu-container-closed');
275
+ container.appendChild(menu);
276
+ menu.classList.add(`${this.classPrefix}editor-menu`);
277
+ this.toolElemGroupings = [];
278
+ for (const tool of this.tools) {
279
+ const elem = tool.render(this);
280
+ if (!elem) {
281
+ continue;
282
+ }
283
+ const grouping = { tool, elems: [] };
284
+ this.toolElemGroupings.push(grouping);
285
+ if (Array.isArray(elem)) {
286
+ for (const e of elem) {
287
+ menu.appendChild(e);
288
+ grouping.elems.push(e);
289
+ }
290
+ }
291
+ else {
292
+ menu.append(elem);
293
+ grouping.elems.push(elem);
294
+ }
295
+ }
296
+ (_c = global.document) === null || _c === void 0 ? void 0 : _c.body.appendChild(container);
297
+ return container;
298
+ }
299
+ showMenu(left, top, bottom) {
300
+ if (!this.menu) {
301
+ return;
302
+ }
303
+ let y = top - this.menu.clientHeight;
304
+ if (y < 2) {
305
+ y = bottom;
306
+ }
307
+ this.menu.style.transform = `translate(${Math.round(left - this.menu.clientWidth / 2)}px,${Math.round(y)}px)`;
308
+ if (!this.menuVisible) {
309
+ this.menuVisible = true;
310
+ this.menu.classList.remove(this.classPrefix + 'editor-menu-container-closed');
311
+ }
312
+ }
313
+ hideMenu() {
314
+ if (!this.menuVisible || !this.menu) {
315
+ return;
316
+ }
317
+ this.menuVisible = false;
318
+ this.menu.classList.add(this.classPrefix + 'editor-menu-container-closed');
319
+ }
320
+ toggleSelection(selection, nodeType, atts) {
321
+ if (this.allHaveNodeType(selection, nodeType)) {
322
+ this.removeFromSelection(selection, nodeType);
323
+ }
324
+ else {
325
+ this.applyToSection(selection, nodeType, atts);
326
+ }
327
+ }
328
+ applyToSection(selection, nodeType, atts) {
329
+ var _a, _b, _c;
330
+ if (!this.renderer) {
331
+ return;
332
+ }
333
+ for (const node of selection.nodes) {
334
+ if (!node.t) {
335
+ node.t = [];
336
+ }
337
+ if (typeof node.t === 'string') {
338
+ node.t = [node.t];
339
+ }
340
+ if (!node.t.includes(nodeType)) {
341
+ const desc = this.renderer.descriptors[nodeType];
342
+ if (desc === null || desc === void 0 ? void 0 : desc.group) {
343
+ for (let i = 0; i < node.t.length; i++) {
344
+ const otherDesc = this.renderer.descriptors[(_a = node.t[i]) !== null && _a !== void 0 ? _a : ''];
345
+ if ((otherDesc === null || otherDesc === void 0 ? void 0 : otherDesc.group) === desc.group) {
346
+ (_b = otherDesc.remove) === null || _b === void 0 ? void 0 : _b.call(otherDesc, node);
347
+ node.t.splice(i, 1);
348
+ i--;
349
+ }
350
+ }
351
+ }
352
+ node.t.push(nodeType);
353
+ (0, rtxt_lib_1.sortRTxtNodeTypes)(node, this.renderer.descriptors);
354
+ }
355
+ if (atts) {
356
+ if (!node.atts) {
357
+ node.atts = {};
358
+ }
359
+ for (const e in atts) {
360
+ const v = atts[e];
361
+ if (v !== undefined) {
362
+ node.atts[e] = v;
363
+ }
364
+ }
365
+ }
366
+ }
367
+ (_c = this.renderer) === null || _c === void 0 ? void 0 : _c.queueExport();
368
+ }
369
+ removeFromSelection(selection, nodeType) {
370
+ var _a, _b;
371
+ if (!this.renderer) {
372
+ return;
373
+ }
374
+ for (const node of selection.nodes) {
375
+ if (!node.t) {
376
+ node.t = [];
377
+ }
378
+ if (typeof node.t === 'string') {
379
+ node.t = [node.t];
380
+ }
381
+ if ((0, common_1.aryRemoveItem)(node.t, nodeType)) {
382
+ const desc = this.renderer.descriptors[nodeType];
383
+ (_a = desc === null || desc === void 0 ? void 0 : desc.remove) === null || _a === void 0 ? void 0 : _a.call(desc, node);
384
+ }
385
+ }
386
+ (_b = this.renderer) === null || _b === void 0 ? void 0 : _b.queueExport();
387
+ }
388
+ someHaveNodeType(selection, nodeType) {
389
+ var _a;
390
+ for (const node of selection.nodes) {
391
+ if (typeof node.t === 'string') {
392
+ if (node.t === nodeType) {
393
+ return true;
394
+ }
395
+ }
396
+ else if ((_a = node.t) === null || _a === void 0 ? void 0 : _a.length) {
397
+ if (node.t.includes(nodeType)) {
398
+ return true;
399
+ }
400
+ }
401
+ else if (nodeType === rtxt_types_1.defaultRTxtNodeType) {
402
+ return true;
403
+ }
404
+ }
405
+ return false;
406
+ }
407
+ allHaveNodeType(selection, nodeType) {
408
+ var _a;
409
+ for (const node of selection.nodes) {
410
+ if (typeof node.t === 'string') {
411
+ if (node.t !== nodeType) {
412
+ return false;
413
+ }
414
+ }
415
+ else if ((_a = node.t) === null || _a === void 0 ? void 0 : _a.length) {
416
+ if (!node.t.includes(nodeType)) {
417
+ return false;
418
+ }
419
+ }
420
+ else if (nodeType !== rtxt_types_1.defaultRTxtNodeType) {
421
+ return false;
422
+ }
423
+ }
424
+ return true;
425
+ }
426
+ syncElemToDoc() {
427
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
428
+ if (!this.renderer) {
429
+ return;
430
+ }
431
+ const startEmpty = this.renderer.isEmpty;
432
+ const selection = (_a = globalThis.document) === null || _a === void 0 ? void 0 : _a.getSelection();
433
+ const caretNode = (selection && (selection === null || selection === void 0 ? void 0 : selection.anchorNode) === (selection === null || selection === void 0 ? void 0 : selection.focusNode)) ? selection.anchorNode : null;
434
+ let setCaretElem = null;
435
+ let reRender = false;
436
+ const docElem = this.renderer.elem;
437
+ if (!docElem) {
438
+ return;
439
+ }
440
+ let hasChange = false;
441
+ const lineElemType = (_b = this.renderer.options.lineElem) !== null && _b !== void 0 ? _b : rtxt_types_1.defaultRTxtLineElem;
442
+ let lineIndex = 0;
443
+ let nodeIndex = 0;
444
+ let lastFormattedElem = null;
445
+ for (let i = 0; i < docElem.childNodes.length; i++) {
446
+ const lineElem = docElem.childNodes.item(i);
447
+ if (!lineElem || (0, rtxt_lib_1.shouldIgnoreRTxtLineElem)(lineElem)) {
448
+ continue;
449
+ }
450
+ if (!(lineElem instanceof Element)) {
451
+ hasChange = true;
452
+ reRender = true;
453
+ const replaceWith = (_c = globalThis.document) === null || _c === void 0 ? void 0 : _c.createElement(lineElemType);
454
+ if (replaceWith) {
455
+ replaceWith.textContent = lineElem.textContent;
456
+ docElem.insertBefore(replaceWith, lineElem);
457
+ }
458
+ lineElem.remove();
459
+ continue;
460
+ }
461
+ const lineIsNode = lineElem.getAttribute(rtxt_types_1.rTxtNodeAtt);
462
+ if (!lineIsNode) {
463
+ hasChange = true;
464
+ reRender = true;
465
+ lineElem.innerHTML = `<${lineElemType}>${(0, common_1.escapeHtml)((_d = lineElem.textContent) !== null && _d !== void 0 ? _d : '')}</${lineElemType}>`;
466
+ }
467
+ const lineAttValue = lineElem.getAttribute(rtxt_types_1.rTxtLineIndexAtt);
468
+ if (!lineAttValue || lineAttValue !== lineIndex.toString()) {
469
+ hasChange = true;
470
+ lineElem.setAttribute(rtxt_types_1.rTxtLineIndexAtt, lineIndex.toString());
471
+ }
472
+ for (let li = 0; li < lineElem.childNodes.length; li++) {
473
+ let _elem = lineElem.childNodes.item(li);
474
+ if (!_elem) {
475
+ continue;
476
+ }
477
+ if (!(_elem instanceof Element)) {
478
+ const replaceWith = (_e = globalThis.document) === null || _e === void 0 ? void 0 : _e.createElement(rtxt_types_1.defaultRTxtNodeType);
479
+ replaceWith.textContent = _elem.textContent;
480
+ lineElem.insertBefore(replaceWith, _elem);
481
+ _elem.remove();
482
+ _elem = replaceWith;
483
+ }
484
+ let elem = _elem;
485
+ if (!elem.textContent || elem.getAttribute(rtxt_types_1.rTxtIgnoreAtt) || elem instanceof HTMLBRElement) {
486
+ elem.remove();
487
+ li--;
488
+ hasChange = true;
489
+ continue;
490
+ }
491
+ else if (elem.getAttribute(rtxt_types_1.rTxtNodeAtt)) {
492
+ lastFormattedElem = elem;
493
+ }
494
+ else if (lastFormattedElem) {
495
+ reRender = true;
496
+ hasChange = true;
497
+ const replaceWith = lastFormattedElem.cloneNode(true);
498
+ replaceWith.textContent = elem.textContent;
499
+ lineElem.insertBefore(replaceWith, elem);
500
+ elem.remove();
501
+ elem = replaceWith;
502
+ }
503
+ else if ((0, rtxt_internal_1.hasForeignElems)(elem)) {
504
+ reRender = true;
505
+ hasChange = true;
506
+ elem.innerHTML = (0, common_1.escapeHtml)((_f = elem.textContent) !== null && _f !== void 0 ? _f : '');
507
+ }
508
+ const indexAttValue = elem.getAttribute(rtxt_types_1.rTxtIndexLookupAtt);
509
+ if (!indexAttValue || indexAttValue !== nodeIndex.toString()) {
510
+ hasChange = true;
511
+ elem.setAttribute(rtxt_types_1.rTxtIndexLookupAtt, nodeIndex.toString());
512
+ }
513
+ const hasFocus = caretNode ? true : false;
514
+ const textNode = (0, rtxt_internal_1.getTextNode)(elem);
515
+ if ((textNode === null || textNode === void 0 ? void 0 : textNode.textContent) && textNode.textContent.length !== 1 && Array.from(textNode.textContent).length !== 1) {
516
+ hasChange = true;
517
+ //split
518
+ const text = Array.from(textNode.textContent);
519
+ textNode.textContent = (_g = text[0]) !== null && _g !== void 0 ? _g : null;
520
+ const elemNode = (0, rtxt_internal_1.getNodeElem)(textNode);
521
+ if (!elemNode) {
522
+ break;
523
+ }
524
+ const insertBefore = elemNode.nextSibling;
525
+ for (let ti = 1; ti < text.length; ti++) {
526
+ const char = text[ti];
527
+ const nextNode = elemNode.cloneNode(true);
528
+ nextNode.setAttribute(rtxt_types_1.rTxtIndexLookupAtt, nodeIndex.toString());
529
+ const nextTextNode = (0, rtxt_internal_1.getTextNode)(nextNode);
530
+ if (nextTextNode) {
531
+ nextTextNode.textContent = char !== null && char !== void 0 ? char : null;
532
+ }
533
+ (_h = elemNode.parentElement) === null || _h === void 0 ? void 0 : _h.insertBefore(nextNode, insertBefore);
534
+ if (hasFocus) {
535
+ setCaretElem = nextNode;
536
+ }
537
+ }
538
+ }
539
+ nodeIndex++;
540
+ }
541
+ lineIndex++;
542
+ }
543
+ this.renderer.isEmpty = nodeIndex === 0;
544
+ if (this.renderer.isEmpty !== startEmpty) {
545
+ hasChange = true;
546
+ reRender = true;
547
+ }
548
+ if (hasChange) {
549
+ const { doc, lookup } = (0, rtxt_lib_1.elemToRTxtDoc)(docElem);
550
+ this.renderer.nodeLookup = lookup;
551
+ this.renderer.doc = doc;
552
+ }
553
+ if (reRender) {
554
+ const caretIndex = setCaretElem === null || setCaretElem === void 0 ? void 0 : setCaretElem.getAttribute(rtxt_types_1.rTxtIndexLookupAtt);
555
+ this.renderer.render();
556
+ if (caretIndex) {
557
+ setCaretElem = docElem.querySelector(`[${rtxt_types_1.rTxtIndexLookupAtt}='${caretIndex}']`);
558
+ }
559
+ }
560
+ const caretTo = setCaretElem ? (0, rtxt_internal_1.getTextNode)(setCaretElem) : null;
561
+ if (this.renderer.isEmpty) {
562
+ this.renderer.render();
563
+ this.renderer.focus();
564
+ }
565
+ else if (caretTo && selection) {
566
+ selection.collapse(caretTo, ((_j = caretTo.textContent) === null || _j === void 0 ? void 0 : _j.length) || 1);
567
+ }
568
+ else if (selection) {
569
+ const last = (0, rtxt_internal_1.getTextNode)(docElem, true, false);
570
+ if (last) {
571
+ selection.removeAllRanges();
572
+ selection.collapse(last, (_k = last.textContent) === null || _k === void 0 ? void 0 : _k.length);
573
+ }
574
+ }
575
+ }
576
+ getElementAt(index) {
577
+ var _a, _b;
578
+ const docElem = (_a = this.renderer) === null || _a === void 0 ? void 0 : _a.elem;
579
+ if (!docElem) {
580
+ return undefined;
581
+ }
582
+ return (_b = docElem.querySelector(`[${rtxt_types_1.rTxtIndexLookupAtt}='${index}']`)) !== null && _b !== void 0 ? _b : undefined;
583
+ }
584
+ getStyleAt(index) {
585
+ var _a, _b, _c;
586
+ const elem = this.getElementAt(index);
587
+ if (!elem) {
588
+ return undefined;
589
+ }
590
+ return (_c = (_b = (_a = globalThis.window) === null || _a === void 0 ? void 0 : _a.getComputedStyle) === null || _b === void 0 ? void 0 : _b.call(_a, (0, rtxt_internal_1.getDeepestElem)(elem))) !== null && _c !== void 0 ? _c : undefined;
591
+ }
592
+ getCursorLine() {
593
+ var _a;
594
+ const node = (_a = this.selection) === null || _a === void 0 ? void 0 : _a.cursorNode;
595
+ if (!node || !this.renderer) {
596
+ return undefined;
597
+ }
598
+ return (0, rtxt_lib_1.getRTxtNodeLine)(this.renderer.doc, node);
599
+ }
600
+ getSelectedLines() {
601
+ if (!this.selection || !this.renderer) {
602
+ return [];
603
+ }
604
+ return (0, rtxt_lib_1.getRTxtNodesLines)(this.renderer.doc, this.selection.nodes);
605
+ }
606
+ setSelectedLineAlignment(align) {
607
+ if (!this.renderer) {
608
+ return;
609
+ }
610
+ const sel = this.selection;
611
+ const elem = this.renderer.elem;
612
+ const lines = this.getSelectedLines();
613
+ for (const line of lines) {
614
+ if (align === 'start') {
615
+ delete line.align;
616
+ }
617
+ else {
618
+ line.align = align;
619
+ }
620
+ }
621
+ this.renderer.render();
622
+ if (sel && elem) {
623
+ (0, rtxt_lib_1.selectRTxtSelection)(sel, elem);
624
+ }
625
+ }
626
+ queueRenderer(renderer) {
627
+ var _a;
628
+ this.rendererQueue.push(renderer);
629
+ this.renderer = (_a = this.rendererQueue[this.rendererQueue.length - 1]) !== null && _a !== void 0 ? _a : null;
630
+ }
631
+ dequeueRenderer(renderer) {
632
+ var _a;
633
+ if ((0, common_1.aryRemoveItem)(this.rendererQueue, renderer)) {
634
+ this.renderer = (_a = this.rendererQueue[this.rendererQueue.length - 1]) !== null && _a !== void 0 ? _a : null;
635
+ }
636
+ }
637
+ }
638
+ exports.RTxtEditor = RTxtEditor;
639
+ //# sourceMappingURL=RTxtEditor.js.map