@emailmaker/extensions-sandbox 0.9.0-dev.1

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.
package/types.d.ts ADDED
@@ -0,0 +1,1002 @@
1
+ /**
2
+ * Interface for a basic plugin.
3
+ * Defines the logic for initialization, execution, and cleanup.
4
+ */
5
+ export interface IPlugin {
6
+ [name: string]: any;
7
+
8
+ /**
9
+ * Plugin initialization method.
10
+ * This method is called immediately after the plugin is created.
11
+ * Can be synchronous or asynchronous.
12
+ */
13
+ init?(): Promise<void> | void;
14
+
15
+ /**
16
+ * Method that is invoked after all plugins have been initialized.
17
+ * Useful for resolving circular dependencies between plugins.
18
+ */
19
+ afterInit?(): Promise<void> | void;
20
+
21
+ /**
22
+ * Plugin disposal method.
23
+ * This method is called before the plugin system is destroyed.
24
+ * Used for cleanup and releasing resources.
25
+ */
26
+ dispose?(): Promise<void> | void;
27
+ }
28
+
29
+
30
+
31
+ /**
32
+ * Class-based plugin constructor type.
33
+ * Defines a constructor that takes a plugin context instance and returns a plugin.
34
+ */
35
+ export type PluginClassConstructor<Plugin extends IPlugin = IPlugin> = {
36
+ new(
37
+ instance: App,
38
+ ): Plugin;
39
+ };
40
+
41
+ /**
42
+ * Function-based plugin constructor type.
43
+ * Defines a function that takes a plugin context instance and returns a plugin or nothing (void).
44
+ */
45
+ export type PluginFunctionConstructor<Plugin extends IPlugin = IPlugin> = (instance: App) => void | Plugin;
46
+
47
+ /**
48
+ * General plugin constructor type.
49
+ * Can be either a class-based constructor or a function-based constructor.
50
+ */
51
+ export type IPluginConstructor<Plugin extends IPlugin = IPlugin> = PluginClassConstructor<Plugin> | PluginFunctionConstructor<Plugin>;
52
+
53
+ /**
54
+ * Extracts the instance type of a given plugin constructor.
55
+ */
56
+ export type InstanceOfPluginType<PluginConstructor> =
57
+ PluginConstructor extends IPluginConstructor<infer X> ? X : never;
58
+
59
+
60
+
61
+
62
+
63
+ /**
64
+ * The `App` class manages the plugin system within an iframe global context.
65
+ * It follows the Singleton pattern to ensure only one instance exists.
66
+ */
67
+ export declare class App {
68
+
69
+ /**
70
+ * Retrieves the singleton instance of `App`.
71
+ *
72
+ * @returns {App} The single instance of `App`.
73
+ */
74
+ static get instance(): App;
75
+
76
+ /**
77
+ * Registers a new plugin in the global registry.
78
+ *
79
+ * @template Key - The plugin key from `SandboxModules`.
80
+ * @param {Key} key - The unique identifier of the plugin.
81
+ * @param {PluginTypeMap[Key]} pluginConstructor - The constructor of the plugin.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * App.registerPlugin('myPlugin', MyPlugin);
86
+ * ```
87
+ */
88
+ static registerPlugin<Key extends keyof PluginTypeMap>(
89
+ key: Key,
90
+ pluginConstructor: PluginTypeMap[Key]
91
+ ): void;
92
+
93
+ /**
94
+ * Retrieves an instance of a plugin by its key or constructor.
95
+ * If the plugin has not been created before, it will be instantiated.
96
+ *
97
+ * @param key - The key of a registered plugin or its constructor.
98
+ * @returns The instance of the requested plugin.
99
+ */
100
+ static use<Key extends keyof PluginTypeMap>(key: Key): InstanceOfPluginType<PluginTypeMap[Key]>;
101
+ static use<PluginConstructor extends IPluginConstructor>(key: PluginConstructor): InstanceOfPluginType<PluginConstructor>;
102
+ /**
103
+ * Retrieves an instance of a registered plugin by its key or constructor.
104
+ *
105
+ * @param {keyof PluginTypeMap | IPluginConstructor<App>} key - The plugin key or constructor.
106
+ * @returns {IPlugin} The requested plugin instance.
107
+ */
108
+ static use(key: keyof PluginTypeMap | IPluginConstructor<App>): IPlugin;
109
+
110
+
111
+ /**
112
+ * Retrieves an instance of a plugin by its key or constructor.
113
+ * If the plugin has not been created before, it will be instantiated.
114
+ *
115
+ * @param key - The key of a registered plugin or its constructor.
116
+ * @returns The instance of the requested plugin.
117
+ */
118
+ use<Key extends keyof PluginTypeMap>(key: Key): InstanceOfPluginType<PluginTypeMap[Key]>;
119
+ use<PluginConstructor extends IPluginConstructor<this>>(key: PluginConstructor): InstanceOfPluginType<PluginConstructor>;
120
+
121
+ /**
122
+ * Retrieves an instance of a registered plugin by its key or constructor.
123
+ *
124
+ * @param {keyof PluginTypeMap | IPluginConstructor<App>} key - The plugin key or constructor.
125
+ * @returns {IPlugin} The requested plugin instance.
126
+ */
127
+ use(key: keyof PluginTypeMap | IPluginConstructor<App>): IPlugin;
128
+
129
+ /**
130
+ * Disposes of all active plugins.
131
+ * This should be called before terminating `App` to release resources.
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * app.dispose();
136
+ * ```
137
+ */
138
+ dispose(): void;
139
+ }
140
+
141
+
142
+ export interface PluginTypeMap {
143
+ /**
144
+ * API for interacting with the `MessageService`.
145
+ * Provides methods to send and receive messages, as well as manage event listeners.
146
+ */
147
+ MessageService: () => MessageService;
148
+ /**
149
+ * SyncService provides methods to synchronize and manage HTML document structures.
150
+ */
151
+ SyncService: () => SyncService;
152
+ /**
153
+ * Internal synchronization service for documents.
154
+ */
155
+ SyncServiceInternals: () => SyncServiceInternals;
156
+ /**
157
+ * Represents a text document.
158
+ */
159
+ TextDocument: () => ITextDocument;
160
+ /**
161
+ * Provides mapping functionalities for document text.
162
+ */
163
+ Mapping: () => DocumentMapping;
164
+ /**
165
+ * Represents the focus state of the editor.
166
+ */
167
+ FocusState: () => FocusState;
168
+ }
169
+
170
+
171
+ type MayBePromise<T> = T | Promise<T>;
172
+
173
+ export type Identifier<Type0 = any, Type1 = any, Type2 = any> = { type0?: Type0; type1: Type1; type2: Type2 } & string;
174
+
175
+ export interface IDisposable {
176
+ dispose(): void;
177
+ }
178
+
179
+ /**
180
+ * API for interacting with the `MessageService`.
181
+ * Provides methods to send and receive messages, as well as manage event listeners.
182
+ */
183
+ export interface MessageService {
184
+
185
+ // /**
186
+ // * Creates an instance of the Message Service API.
187
+ // *
188
+ // * @param {BaseEditor} editor - The editor instance used to obtain the message service.
189
+ // */
190
+ // constructor(editor: BaseEditor);
191
+
192
+ /**
193
+ * Sends a message through `MessageService`.
194
+ *
195
+ * @template Type - The type of data being sent in the message.
196
+ * @param {Identifier<Type>} messageTypeId - The identifier for the message type.
197
+ * @param {Type} data - The payload to be sent in the message.
198
+ * @param {boolean} [queued] - Optional parameter indicating whether the message should be queued.
199
+ * @example
200
+ * ```ts
201
+ * messageServiceApi.send('USER_LOGGED_IN', { userId: 123 });
202
+ * ```
203
+ */
204
+ send<Type>(messageTypeId: Identifier<Type>, data: Type, queued?: boolean): void;
205
+
206
+ /**
207
+ * Sends a message without specifying a message type identifier.
208
+ *
209
+ * @param {any} data - The payload to be sent in the message.
210
+ * @param {boolean} [queued] - Optional parameter indicating whether the message should be queued.
211
+ */
212
+ send(data: any, queued?: boolean): void;
213
+
214
+ /**
215
+ * Sends an asynchronous message and waits for a response.
216
+ *
217
+ * @template Type - The type of data being sent in the message.
218
+ * @template Result - The expected type of the response.
219
+ * @param {Identifier<Type, Result>} messageTypeId - The identifier for the message type.
220
+ * @param {Type} data - The payload to be sent in the message.
221
+ * @param {boolean} [queued=true] - Optional parameter indicating whether the message should be queued.
222
+ * @returns {Promise<Result | void>} - A promise resolving to the response result or `void` if no response is expected.
223
+ * @example
224
+ * ```ts
225
+ * const response = await messageServiceApi.sendAsync('FETCH_USER', { userId: 123 });
226
+ * console.log(response); // { name: "John Doe", age: 30 }
227
+ * ```
228
+ */
229
+ sendAsync<Type, Result>(
230
+ messageTypeId: Identifier<Type, Result>,
231
+ data: Type,
232
+ queued?: boolean,
233
+ ): Promise<Result | void>;
234
+
235
+ /**
236
+ * Adds a global event listener for message events.
237
+ *
238
+ * @param {(e: MessageEvent) => void} listener - The event listener function.
239
+ * @returns {IDisposable} - An object to remove the subscription.
240
+ */
241
+ addListener(listener: (e: MessageEvent) => void): IDisposable;
242
+
243
+ /**
244
+ * Adds a listener for a specific message type.
245
+ *
246
+ * @template Type - The type of data in the message.
247
+ * @param {Identifier<Type>} messageTypeId - The identifier for the message type.
248
+ * @param {(value: Type) => void} listener - The callback function to execute when the message is received.
249
+ * @param {number} [priority] - The priority of the listener (higher values execute earlier).
250
+ * @returns {IDisposable} - An object to remove the subscription.
251
+ * @example
252
+ * ```ts
253
+ * const dispose = messageServiceApi.addListener('USER_LOGGED_IN', (data) => {
254
+ * console.log('User logged in:', data);
255
+ * });
256
+ *
257
+ * // To remove the listener:
258
+ * dispose();
259
+ * ```
260
+ */
261
+ addListener<Type = any>(
262
+ messageTypeId: Identifier<Type>,
263
+ listener: (value: Type) => void,
264
+ priority?: number,
265
+ ): IDisposable;
266
+
267
+ /**
268
+ * Adds an asynchronous listener for a specific message type.
269
+ *
270
+ * @template Type - The type of data in the message.
271
+ * @template Result - The expected return type from the listener.
272
+ * @param {Identifier<Type, Result>} messageTypeId - The identifier for the message type.
273
+ * @param {(value: Type) => MayBePromise<Result>} listener - The callback function that may return a promise.
274
+ * @param {number} [priority=0] - The priority of the listener.
275
+ * @returns {IDisposable} - An object to remove the subscription.
276
+ * @example
277
+ * ```ts
278
+ * const dispose = messageServiceApi.addListenerAsync('FETCH_DATA', async (request) => {
279
+ * return await fetchDataFromServer(request);
280
+ * });
281
+ * ```
282
+ */
283
+ addListenerAsync<Type = any, Result = any>(
284
+ messageTypeId: Identifier<Type, Result>,
285
+ listener: (value: Type) => MayBePromise<Result>,
286
+ priority?: number,
287
+ ): IDisposable;
288
+
289
+ /**
290
+ * Adds a listener for the initialization event.
291
+ *
292
+ * @param {(target: unknown) => void} listener - The callback function.
293
+ * @param {boolean} [immediately=true] - Whether the callback should be triggered immediately if initialization has already occurred.
294
+ * @param {number} [priority=0] - The priority of the listener.
295
+ * @returns {IDisposable} - An object to remove the subscription.
296
+ */
297
+ onInit(listener: (target: unknown) => void, immediately?: boolean, priority?: number): IDisposable;
298
+
299
+ /**
300
+ * Checks whether initialization has been completed.
301
+ *
302
+ * @returns {boolean} - `true` if initialization is complete, otherwise `false`.
303
+ * @example
304
+ * ```ts
305
+ * if (messageServiceApi.isInited()) {
306
+ * console.log("Message service is ready!");
307
+ * }
308
+ * ```
309
+ */
310
+ isInited(): boolean;
311
+ }
312
+
313
+
314
+ /**
315
+ * Represents different types of mutations that can occur in the document.
316
+ */
317
+ export type MutationType = "Add" | "Remove" | "Attribute" | "Move" | "Update" | "Set" | "ClassList" | "Style" | "Hidden";
318
+
319
+ export interface AddMutation<T> {
320
+ readonly type: "Add";
321
+ readonly parent: T | null;
322
+ readonly node: T;
323
+ readonly nextSibling?: T;
324
+ }
325
+
326
+ export interface RemoveMutation<T> {
327
+ readonly type: "Remove";
328
+ readonly node: T;
329
+ }
330
+
331
+ export interface AttributeMutation<T> {
332
+ readonly type: "Attribute";
333
+ readonly node: T;
334
+ readonly name: string;
335
+ readonly value?: string | null;
336
+ }
337
+
338
+ export interface UpdateMutation<T> {
339
+ readonly type: "Update";
340
+ readonly prev: T;
341
+ readonly next: T;
342
+ }
343
+
344
+ export interface MoveMutation<T> {
345
+ readonly type: "Move";
346
+ readonly parent: T | null;
347
+ readonly node: T;
348
+ readonly nextSibling?: T;
349
+ }
350
+
351
+ export interface SetMutation<T> {
352
+ readonly type: "Set";
353
+ readonly node: T;
354
+ readonly data: any;
355
+ }
356
+
357
+ export interface ClassListMutation<T> {
358
+ readonly type: "ClassList";
359
+ readonly node: T;
360
+ readonly addedClasses?: string[];
361
+ readonly removedClasses?: string[];
362
+ }
363
+
364
+ export interface StyleMutation<T> {
365
+ readonly type: "Style";
366
+ readonly node: T;
367
+ readonly value: string | null;
368
+ }
369
+
370
+ export interface HiddenMutation<T> {
371
+ readonly type: "Hidden";
372
+ readonly node: T;
373
+ readonly value: boolean;
374
+ }
375
+
376
+ export type Mutation<T> =
377
+ | AddMutation<T>
378
+ | RemoveMutation<T>
379
+ | AttributeMutation<T>
380
+ | UpdateMutation<T>
381
+ | MoveMutation<T>
382
+ | SetMutation<T>
383
+ | ClassListMutation<T>
384
+ | StyleMutation<T>
385
+ | HiddenMutation<T>;
386
+
387
+ /**
388
+ * Interface for a disposable object.
389
+ */
390
+ export interface IDisposable {
391
+ dispose(): void;
392
+ }
393
+
394
+ /**
395
+ * Defines a listener function that receives an array of summaries and returns either void or false.
396
+ * @param listener - Function that processes summaries.
397
+ * @param priority - Optional priority value for the listener.
398
+ * @returns An IDisposable instance.
399
+ */
400
+ export interface Listener {
401
+ (listener: (value: Summary[]) => void | false, priority?: number): IDisposable;
402
+ }
403
+ /**
404
+ * Describes a collection of modifications applied to the DOM.
405
+ */
406
+ export interface Summary {
407
+ /**
408
+ * A list of elements that are now present in the subtree with the specified attribute,
409
+ * but were either previously missing from the subtree, lacked the attribute, or both.
410
+ */
411
+ added: Node[];
412
+
413
+ /**
414
+ * A list of elements that were previously in the subtree with the specified attribute,
415
+ * but are now either removed from the subtree, no longer have the attribute, or both.
416
+ */
417
+ removed: Node[];
418
+
419
+ /**
420
+ * A list of nodes that have been moved from one parent to another.
421
+ */
422
+ reparented: Node[] | undefined;
423
+
424
+ /**
425
+ * Nodes that remain within the same subtree and have the same parent but
426
+ * have changed their order within the parent's child list.
427
+ */
428
+ reordered: Node[] | undefined;
429
+
430
+ /**
431
+ * A list of elements that have remained in the subtree with the specified attribute,
432
+ * but the value of that attribute has changed.
433
+ */
434
+ valueChanged: Node[] | undefined;
435
+
436
+ /**
437
+ * An object that tracks attribute changes. Each key corresponds to an attribute name
438
+ * found in `elementAttributes`, and its value is an array of elements that remained
439
+ * in the subtree and had their attribute values updated.
440
+ */
441
+ attributeChanged: Record<string, Element[]> | undefined;
442
+
443
+ /**
444
+ * A list of character data nodes that existed before and still exist,
445
+ * but their text content has changed.
446
+ */
447
+ characterDataChanged: Node[] | undefined;
448
+
449
+ /**
450
+ * Retrieves the previous parent node of a given node. The node must be
451
+ * included in the removed elements array; otherwise, an error is thrown.
452
+ *
453
+ * @param node The node whose previous parent is being retrieved.
454
+ */
455
+ getOldParentNode(node: Node): Node;
456
+
457
+ /**
458
+ * Returns the previous value of a specified attribute for an element.
459
+ * The element must be included in the `valueChanged` array; otherwise, an error is thrown.
460
+ *
461
+ * @param element The element whose previous attribute value is being retrieved.
462
+ * @param name The name of the attribute to retrieve the previous value for.
463
+ */
464
+ getOldAttribute(element: Node, name: string): string;
465
+
466
+ /**
467
+ * Returns the previous text content of a given node.
468
+ * The node must be in the `valueChanged` array; otherwise, an error is thrown.
469
+ *
470
+ * @param node The node whose previous character data is being retrieved.
471
+ */
472
+ getOldCharacterData(node: Node): string;
473
+
474
+ /**
475
+ * Returns the previous sibling of a given node.
476
+ * The node must be included in the `reordered` array; otherwise, an error is thrown.
477
+ *
478
+ * @param node The node whose previous sibling is being retrieved.
479
+ */
480
+ getOldPreviousSibling(node: Node): Node;
481
+ }
482
+
483
+ /**
484
+ * Represents an event mechanism that allows adding listeners with an optional priority.
485
+ *
486
+ * @template T The type of value emitted by the event.
487
+ * @param listener A function that receives the event value. If it returns `false`, the event propagation stops.
488
+ * @param priority A number indicating the priority of the listener. Higher values mean higher priority (default is `0`).
489
+ * @returns An `IDisposable` that allows removing the listener.
490
+ */
491
+ export type Event<T> = (listener: (value: T) => void | false, priority: number) => IDisposable;
492
+
493
+ /**
494
+ * Defines different types of nodes that can exist in a document structure.
495
+ */
496
+ export type NodeType =
497
+ /** Represents a generic HTML node. */
498
+ 'html' |
499
+
500
+ /** Represents a standard element node. */
501
+ 'element' |
502
+
503
+ /** Represents the root node of a document. */
504
+ 'root' |
505
+
506
+ /** Represents a comment node. */
507
+ 'comment' |
508
+
509
+ /** Represents a document type declaration. */
510
+ 'doctype';
511
+
512
+ /**
513
+ * Represents a synchronized node in a document structure.
514
+ */
515
+ export interface SyncNode {
516
+ start: number;
517
+ end: number;
518
+ children: SyncNode[];
519
+ /** The textual content of the node (if applicable). */
520
+ content?: string;
521
+
522
+ /** The type of the node (default is `ELEMENT`). */
523
+ type: any;
524
+
525
+ /** The tag name of the node, if applicable. */
526
+ tag: string | undefined;
527
+
528
+ /** Indicates whether the node is self-closing (default is `false`). */
529
+ closed?: boolean;
530
+
531
+ /** The position where the start tag ends, if known. */
532
+ startTagEnd: number | undefined;
533
+
534
+ /** The position where the end tag starts, if known. */
535
+ endTagStart: number | undefined;
536
+
537
+ /** A collection of attributes associated with the node. */
538
+ attributes: { [name: string]: string | null } | undefined;
539
+
540
+ /** Returns an array of attribute names present in the node. */
541
+ readonly attributeNames: string[];
542
+
543
+ /** Identifies the source that created the node (if applicable). */
544
+ createdBy?: string;
545
+
546
+ /** Indicates whether the node has been modified. */
547
+ isDirty?: boolean;
548
+
549
+ /** Returns the root node of the document tree. */
550
+ readonly root: SyncNode;
551
+
552
+ /**
553
+ * Checks if the node has the same tag name as the given lowercase tag.
554
+ *
555
+ * @param tagInLowerCase The tag name to compare (in lowercase).
556
+ * @returns `true` if the tag names match, otherwise `false`.
557
+ */
558
+ isSameTag(tagInLowerCase: string | undefined): boolean;
559
+
560
+ /** Retrieves the first child node, if present. */
561
+ readonly firstChild: SyncNode | undefined;
562
+
563
+ /** Retrieves the last child node, if present. */
564
+ readonly lastChild: SyncNode | undefined;
565
+
566
+ /**
567
+ * Finds the closest node located before the specified offset.
568
+ *
569
+ * @param offset The position to search before.
570
+ * @returns The nearest node before the given offset.
571
+ */
572
+ findNodeBefore(offset: number): SyncNode;
573
+
574
+ /**
575
+ * Finds the node located at the specified offset.
576
+ *
577
+ * @param offset The exact position to search.
578
+ * @returns The node found at the given offset.
579
+ */
580
+ findNodeAt(offset: number): SyncNode;
581
+
582
+ /**
583
+ * Searches for a node that matches the given predicate.
584
+ *
585
+ * @param predicate A function that returns `true` for the desired node.
586
+ * @returns The first node that satisfies the predicate, or `undefined` if none match.
587
+ */
588
+ find(predicate: (node: SyncNode) => boolean | undefined): SyncNode | undefined;
589
+
590
+ /**
591
+ * Finds the closest ancestor node that satisfies the given condition.
592
+ *
593
+ * @param predicate A function that returns `true` for the desired ancestor.
594
+ * @returns The nearest ancestor node that meets the condition, or `undefined` if none are found.
595
+ */
596
+ closest(predicate: (node: SyncNode) => boolean): SyncNode | undefined;
597
+
598
+ /** Retrieves all ancestor nodes of the current node. */
599
+ parents(): SyncNode[];
600
+
601
+ /**
602
+ * Finds the closest matching node from the root, optionally including the current node.
603
+ *
604
+ * @param predicate A function that determines whether a node is a match.
605
+ * @param self Whether to include the current node in the search (default is `true`).
606
+ * @returns The closest matching node, or `undefined` if none are found.
607
+ */
608
+ closestFromRoot(predicate: (node: SyncNode) => boolean, self?: boolean): SyncNode | undefined;
609
+
610
+ /**
611
+ * Determines whether the current node contains the given node as a descendant.
612
+ *
613
+ * @param node The node to check.
614
+ * @returns `true` if the given node is a descendant, otherwise `false`.
615
+ */
616
+ contains(node: SyncNode): boolean;
617
+
618
+ /**
619
+ * Creates a copy of the current node.
620
+ *
621
+ * @param parent The parent node for the clone (default is `undefined`).
622
+ * @param startOffset The offset where the clone should begin (default is `this.start`).
623
+ * @param deep Whether to perform a deep clone, including child nodes (default is `true`).
624
+ * @returns A new `SyncNode` instance that is a copy of the original.
625
+ */
626
+ clone(parent: SyncNode | undefined, startOffse?: number, deep?: boolean): SyncNode;
627
+ }
628
+
629
+ /**
630
+ * SyncService provides methods to synchronize and manage HTML document structures.
631
+ */
632
+ export interface SyncService {
633
+ /**
634
+ * Starts a text update session.
635
+ * This method should be called before making multiple text changes to improve performance.
636
+ */
637
+ beginTextUpdate(): void;
638
+
639
+ /**
640
+ * Ends a text update session.
641
+ * Should be called after making multiple text changes to apply them efficiently.
642
+ */
643
+ endTextUpdate(): void;
644
+
645
+ /**
646
+ * Commits all pending document changes.
647
+ * @param pushUndoStop - Whether to push an undo stop.
648
+ * @param locked - Whether the commit should be locked.
649
+ * @returns A promise that resolves when the commit is complete.
650
+ */
651
+ commit(pushUndoStop?: boolean, locked?: boolean): Promise<void>;
652
+
653
+ /**
654
+ * Retrieves the mapped parent node of a given node.
655
+ * @param node - The node whose mapped parent is to be found.
656
+ * @returns The mapped parent node if available; otherwise, `undefined`.
657
+ */
658
+ getMappedParent(node: Node | null): Node | undefined;
659
+
660
+ /**
661
+ * Retrieves the context node of a given node.
662
+ * @param node - The node whose context is to be determined.
663
+ * @returns The corresponding context node.
664
+ */
665
+ getContext(node: Node): Node;
666
+
667
+ /**
668
+ * Checks whether a given node is inside an external document.
669
+ * @param node - The node to check.
670
+ * @returns `true` if the node is in an external document; otherwise, `false`.
671
+ */
672
+ isInExternal(node: Node): boolean;
673
+
674
+ /**
675
+ * Determines if a given node is visible in the document.
676
+ * @param node - The node to check.
677
+ * @returns `true` if the node is visible; otherwise, `false`.
678
+ */
679
+ isVisible(node: Node): boolean;
680
+
681
+ /**
682
+ * Retrieves the closest visible ancestor node of a given node.
683
+ * @param node - The node to check.
684
+ * @returns The closest visible node if available; otherwise, `undefined`.
685
+ */
686
+ getVisibleNode(node: Node): Node | undefined;
687
+
688
+ /**
689
+ * Corrects the structure of given nodes in the document.
690
+ * @param nodes - The list of tree nodes to be corrected.
691
+ */
692
+ correctNodes(nodes: SyncNode[]): void;
693
+
694
+ /**
695
+ * Gets the inner HTML of a given node.
696
+ * @param element - The node whose inner HTML should be retrieved.
697
+ * @returns The inner HTML string.
698
+ */
699
+ getInnerHtml(element: Node): string;
700
+
701
+ /**
702
+ * Gets the outer HTML of a given node.
703
+ * @param element - The node whose outer HTML should be retrieved.
704
+ * @returns The outer HTML string.
705
+ */
706
+ getOuterHtml(element: Node): string;
707
+
708
+ /**
709
+ * Sets the inner HTML of a given node.
710
+ * @param element - The node whose inner HTML should be updated.
711
+ * @param innerHTML - The new inner HTML content.
712
+ */
713
+ setInnerHtml(element: Node, innerHTML: string): void;
714
+
715
+ /**
716
+ * Retrieves a document element at a given position.
717
+ * @param position - The position in the document.
718
+ * @returns The corresponding node if found; otherwise, `undefined`.
719
+ */
720
+ getElementByPosition(position: number): Node | undefined;
721
+
722
+ /**
723
+ * Retrieves the text range of a given element.
724
+ * @param node - The node whose range is to be found.
725
+ * @param findParent - If `true`, will search for the nearest parent with a defined range.
726
+ * @returns The range if found; otherwise, `undefined`.
727
+ */
728
+ getRangeByElement(node: Node, findParent?: boolean): IRange | undefined;
729
+
730
+ /**
731
+ * Event triggered before a document change occurs.
732
+ */
733
+ readonly beforeChange: Event<{
734
+ domMutations: Mutation<Node>[];
735
+ treeMutations: Mutation<SyncNode>[];
736
+ }>;
737
+
738
+ /**
739
+ * Event triggered before committing changes.
740
+ */
741
+ readonly beforeCommit: Event<void>;
742
+
743
+ /**
744
+ * Event triggered when a native mutation occurs.
745
+ */
746
+ readonly onChange: Event<Summary[]>;
747
+
748
+ /**
749
+ * Event triggered when the document is refreshed.
750
+ */
751
+ readonly onRefresh: Event<void>;
752
+ }
753
+
754
+
755
+ export interface Edit {
756
+ range: IRange;
757
+ text: string;
758
+ }
759
+
760
+ /**
761
+ * Internal synchronization service for documents.
762
+ */
763
+ export interface SyncServiceInternals {
764
+ /**
765
+ * Applies edits to the document.
766
+ * @param source The source of the edits.
767
+ * @param edits The list of edits.
768
+ */
769
+ applyEdits(source: any, edits: Edit[]): void;
770
+
771
+ /**
772
+ * Corrects the offset in the document text.
773
+ * @param offset The original offset.
774
+ * @param invert Whether to invert the correction.
775
+ * @returns The corrected offset.
776
+ */
777
+ correctOffset(offset: number, invert?: boolean): number;
778
+
779
+ /**
780
+ * Corrects the text range in the document.
781
+ * @param range The original range.
782
+ * @param invert Whether to invert the correction.
783
+ * @returns The corrected range.
784
+ */
785
+ correctRange(range: { start: number, end: number }, invert?: boolean): IRange;
786
+
787
+ /**
788
+ * Saves document changes.
789
+ * @param pushUndoStop Whether to record an undo stop.
790
+ * @returns Whether the save was successful.
791
+ */
792
+ saveChanges(pushUndoStop?: boolean): boolean;
793
+
794
+ /**
795
+ * Asynchronously saves document changes.
796
+ * @param pushUndoStop Whether to record an undo stop.
797
+ * @returns A promise that resolves when the save is complete.
798
+ */
799
+ saveChangesAsync(pushUndoStop?: boolean): Promise<void>;
800
+
801
+ /**
802
+ * Checks if there are unsaved changes.
803
+ * @returns true if there are unsaved changes, otherwise false.
804
+ */
805
+ hasChanges(): boolean;
806
+
807
+ /**
808
+ * Event triggered when the document is ready.
809
+ */
810
+ onReady: Event<void>;
811
+ }
812
+
813
+
814
+ /**
815
+ * Represents a text change within a document.
816
+ */
817
+ export interface TextChange {
818
+ /**
819
+ * The starting position of the original text.
820
+ */
821
+ readonly oldPosition: number;
822
+
823
+ /**
824
+ * The original text before the change.
825
+ */
826
+ readonly oldText: string;
827
+
828
+ /**
829
+ * The starting position of the new text.
830
+ */
831
+ readonly newPosition: number;
832
+
833
+ /**
834
+ * The new text after the change.
835
+ */
836
+ readonly newText: string;
837
+
838
+ /**
839
+ * The length of the original text before the change.
840
+ */
841
+ readonly oldLength: number;
842
+
843
+ /**
844
+ * The end position of the original text.
845
+ */
846
+ readonly oldEnd: number;
847
+
848
+ /**
849
+ * The length of the new text after the change.
850
+ */
851
+ readonly newLength: number;
852
+
853
+ /**
854
+ * The end position of the new text.
855
+ */
856
+ readonly newEnd: number;
857
+ }
858
+
859
+ /**
860
+ * Represents the data associated with an edit event.
861
+ */
862
+ export type EditEventData = {
863
+ /**
864
+ * The source of the edit.
865
+ */
866
+ source: any;
867
+
868
+ /**
869
+ * A list of text changes applied in the edit event.
870
+ */
871
+ changes: TextChange[];
872
+ };
873
+
874
+ /**
875
+ * Represents an event triggered when an edit occurs.
876
+ */
877
+ export type EditEvent = Event<EditEventData>;
878
+
879
+ /**
880
+ * Represents an event triggered when the document content changes.
881
+ */
882
+ export type ChangedEvent = Event<EditEventData>;
883
+
884
+
885
+ /**
886
+ * Represents a text document.
887
+ */
888
+ export interface ITextDocument {
889
+ getText(range?: IRange): string;
890
+ /**
891
+ * Applies edits to the document.
892
+ * @param source The source of the edits.
893
+ * @param edits The list of edits to apply.
894
+ */
895
+ applyEdits(source: any, edits: Edit[]): void;
896
+
897
+ /**
898
+ * Event triggered when the document changes.
899
+ */
900
+ onChanged: ChangedEvent;
901
+
902
+ /**
903
+ * Creates a clone of the document.
904
+ * @returns A new instance of ITextDocument.
905
+ */
906
+ clone(): ITextDocument;
907
+ }
908
+
909
+ /**
910
+ * Represents the focus state of the editor.
911
+ */
912
+ export interface FocusState {
913
+ /**
914
+ * Indicates whether the editor is currently in focus.
915
+ */
916
+ readonly isEditorInFocus: boolean;
917
+
918
+ /**
919
+ * Sets the focus state of the editor.
920
+ * @param focus Whether the editor should be focused.
921
+ */
922
+ setFocus(focus: boolean): void;
923
+
924
+ /**
925
+ * Asynchronously triggered when the editor loses focus.
926
+ * @param context Optional context.
927
+ * @returns A promise resolving with an array of summaries.
928
+ */
929
+ whenEditorLostFocusAsync(context?: any): Promise<Summary[]>;
930
+
931
+ /**
932
+ * Registers a callback to be executed when the editor loses focus.
933
+ * @param callback The function to be called with summaries.
934
+ * @param context Optional context.
935
+ */
936
+ whenEditorLostFocus(callback: (summaries: Summary[]) => void, context?: any): void;
937
+
938
+ /**
939
+ * Event listener triggered when the editor loses focus.
940
+ * @param listener The function to be executed.
941
+ * @param priority Optional priority of the listener.
942
+ * @returns A disposable object to remove the listener.
943
+ */
944
+ editorLostFocus(listener: (summaries: Summary[]) => void, priority?: number): IDisposable;
945
+ }
946
+
947
+ /**
948
+ * Represents a range of text.
949
+ */
950
+ export interface IRange {
951
+ /**
952
+ * The start position of the range.
953
+ */
954
+ start: number;
955
+
956
+ /**
957
+ * The end position of the range.
958
+ */
959
+ end: number;
960
+ }
961
+
962
+ /**
963
+ * Provides mapping functionalities for document text.
964
+ */
965
+ export interface DocumentMapping {
966
+ /**
967
+ * Corrects an offset within the document.
968
+ * @param offset The original offset.
969
+ * @param inverse Whether to invert the correction.
970
+ * @returns The corrected offset.
971
+ */
972
+ correctOffset(offset: number, inverse?: boolean): number;
973
+
974
+ /**
975
+ * Corrects a range within the document.
976
+ * @param range The original range.
977
+ * @param inverse Whether to invert the correction.
978
+ * @returns The corrected range.
979
+ */
980
+ correctRange(range: IRange, inverse?: boolean): IRange;
981
+
982
+ /**
983
+ * Checks if a mapped element exists at the given position.
984
+ * @param position The position to check.
985
+ * @returns true if a mapped element exists, otherwise false.
986
+ */
987
+ isMappedElementInPosition(position: number): boolean;
988
+
989
+ /**
990
+ * Escapes special characters in the given text.
991
+ * @param text The text to escape.
992
+ * @returns The escaped text.
993
+ */
994
+ escape(text: string): string;
995
+
996
+ /**
997
+ * Unescapes special characters in the given text.
998
+ * @param text The text to unescape.
999
+ * @returns The unescaped text.
1000
+ */
1001
+ unescape(text: string): string;
1002
+ }