@nerd-bible/wordgard 0.0.0-beta3

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,1940 @@
1
+ import * as wordgard_doc from 'wordgard/doc';
2
+ import { Elt, Node as Node$1, ChangeSet, Plot, Pos } from 'wordgard/doc';
3
+ import { GardState, TextblockMap, Transaction, GardSelection } from 'wordgard/state';
4
+ import { PhraseSet } from 'wordgard/phrases';
5
+ import { StyleModule, StyleSpec } from 'style-mod';
6
+ import { Command, Menu } from 'wordgard/command';
7
+
8
+ type MakeSelectionStyle = (wg: Wordgard, event: MouseEvent) => Wordgard.MouseSelectionStyle | null;
9
+
10
+ /**
11
+ A widget describes a piece of DOM content that can be used to
12
+ render a node, a part of a node, or an extra element added via a
13
+ decoration. The `Widget` object is separate from its DOM
14
+ representation. It describes how the DOM widget is to be rendered
15
+ and how it behaves, but it itself is an immutable value.
16
+ */
17
+ declare class Widget<Param = unknown> {
18
+ /**
19
+ The parameter for this widget.
20
+ */
21
+ readonly value: Param;
22
+ private constructor();
23
+ /**
24
+ Compare this widget to another widget object.
25
+ */
26
+ eq(other: any): boolean;
27
+ /**
28
+ Define a widget type.
29
+ */
30
+ static define<Param>(spec: Widget.Spec<Param>): Widget.Type<Param>;
31
+ /**
32
+ Create a singleton widget.
33
+ */
34
+ static create(spec: Widget.Spec<null>): Widget<null>;
35
+ /**
36
+ This widget's type. The type mangling is a kludge to make sure
37
+ `Widget<Param>` is a subtype of `Widget<unknown>`.
38
+ */
39
+ readonly type: Widget.Type<unknown extends Param ? any : Param>;
40
+ }
41
+ declare namespace Widget {
42
+ /**
43
+ Specifies a widget type.
44
+ */
45
+ type Spec<Param> = {
46
+ /**
47
+ How to render the widget as DOM content.
48
+ */
49
+ render: (value: Param, wg: Wordgard) => Element | Text;
50
+ /**
51
+ Compare the widget value for equality. Will default to `===`.
52
+ */
53
+ eq?: (a: Param, b: Param) => boolean;
54
+ /**
55
+ Called when a widget of this type is added to an editor that
56
+ is connected to a DOM document, or an editor with the widget
57
+ in it is connected.
58
+ */
59
+ connect?: (value: Param, dom: Element | Text) => void;
60
+ /**
61
+ Called when a widget of this type is removed from an editor
62
+ that is connected to a document, or when the editor containing
63
+ the widget is disconnected.
64
+ */
65
+ disconnect?: (value: Param, dom: Element | Text) => void;
66
+ /**
67
+ Used to determine whether events originating from the widget's
68
+ DOM should ignored by the editor. `false` or a function that
69
+ returns `false` for the event will prevent the editor's
70
+ regular event handling for the event.
71
+ */
72
+ propagateEvent?: boolean | ((event: Event) => boolean);
73
+ /**
74
+ Set this to false for widgets that either aren't visible or
75
+ are positioned outside of the regular document flow.
76
+ */
77
+ inFlow?: boolean;
78
+ /**
79
+ By default, widgets are set to be ineditable. Set this to
80
+ `true` to suppress that.
81
+ */
82
+ editable?: boolean;
83
+ };
84
+ /**
85
+ Each widget has an associated type that describes how it
86
+ behaves.
87
+ */
88
+ class Type<Param> {
89
+ private constructor();
90
+ /**
91
+ Create an instance of this widget type.
92
+ */
93
+ of(value: Param): Widget<Param>;
94
+ }
95
+ }
96
+ type DecoElt = Elt<Widget | string>;
97
+ declare namespace Decoration {
98
+ /**
99
+ Node shapes can be either a widget or an element which may
100
+ contain widgets.
101
+ */
102
+ type Shape = Widget | DecoElt;
103
+ namespace Tag {
104
+ /**
105
+ Override the way a given node type is drawn in the editor. By
106
+ default, the {@link doc.Node.Spec.shape `shape`} field in the
107
+ type's definition will be used, but extensions created with
108
+ this function can provide an alternative shape for a given
109
+ type.
110
+
111
+ When providing a function for the shape, keep in mind that the
112
+ result will be cached by tag, and you should make sure your
113
+ function is pure.
114
+
115
+ When providing a function that returns a shape that changes
116
+ whether the node is rendered as an atom, you need to provide
117
+ the `atom`.
118
+ */
119
+ function shape<T extends Node$1.Type.Ref<any>>(type: T, shape: Shape | ((tag: Node$1.Tag.For<T>) => Shape), config?: {
120
+ atom?: boolean;
121
+ }): GardState.Extension;
122
+ namespace shape {
123
+ /**
124
+ This function allows you to define a {@link Decoration.Tag.shape
125
+ custom node shape} that depends on the editor state. It will
126
+ automatically track what slots (see {@link
127
+ GardState.Facet.compute}) you use, and make sure the nodes
128
+ are redrawn when those change.
129
+
130
+ If your shape function returns a function from a tag, you
131
+ must be careful do any state access you need in the _outer_
132
+ function, not the returned function, or it won't be tracked.
133
+
134
+ You generally don't want to make your shapes depend on
135
+ constantly-changing slots like the document or selection,
136
+ because when the document is big, there's a non-trivial
137
+ amount of work involved when a node shape changes (or may
138
+ have changed).
139
+
140
+ When providing a shape for a plot that changes whether it is
141
+ rendered as an atom, provide the `atom` option.
142
+ */
143
+ function dynamic<T extends Node$1.Type<any>>(// FIXME find better name?
144
+ type: T, shape: (state: GardState) => Shape | ((tag: Node$1.Tag.For<T>) => Shape), config?: {
145
+ atom?: boolean;
146
+ }): GardState.Extension;
147
+ }
148
+ /**
149
+ Define a wrapper to be added around a given node type, or some
150
+ part of it. The given elt should include a hole (`0`) to
151
+ indicate where the original shape goes.
152
+
153
+ If a `target` option is given, and matching some element in
154
+ the node's existing shape, only that element will be wrapped.
155
+ Uses a subset of CSS selectors that supports only tag name and
156
+ class names (`img.x.y`).
157
+ */
158
+ function wrapper(type: Node$1.Type.Ref<any>, wrapper: DecoElt, options?: {
159
+ target?: string;
160
+ }): GardState.Extension;
161
+ /**
162
+ Add a widget to every instance of the given node type. Such
163
+ widgets can appear before or after the node, and for plots
164
+ that aren't rendered as atoms, at its start or end.
165
+
166
+ When a function, `widget` will be cached by tag, and should be
167
+ pure.
168
+ */
169
+ function widget<T extends Node$1.Type.Ref<any>>(type: T, place: "before" | "after" | "start" | "end", widget: Widget | ((tag: Node$1.Tag.For<T>) => Widget)): GardState.Extension;
170
+ namespace widget {
171
+ /**
172
+ Define a node widget decoration that depends on some aspect
173
+ of the editor state. See the notes for {@link
174
+ Decoration.Tag.shape.dynamic}.
175
+ */
176
+ function dynamic<T extends Node$1.Type.Ref<any>>(type: T, place: "before" | "after" | "start" | "end", widget: (state: GardState) => Widget | ((tag: Node$1.Tag.For<T>) => Widget)): GardState.Extension;
177
+ }
178
+ /**
179
+ Add an attribute to the representation of a given node type.
180
+
181
+ By default, the attribute is added to the outer element (or a
182
+ wrapper element if the node is rendered as a widget). If the
183
+ `target` option is given, and
184
+ [matches](#editor.Decoration.Tag.wrapper.options.target) an
185
+ element in the representation, it will be added to that
186
+ element instead.
187
+ */
188
+ function attribute<T extends Node$1.Type.Ref<any>>(type: T, attr: string, value: string | ((tag: Node$1.Tag.For<T>) => string), options?: {
189
+ target?: string;
190
+ }): GardState.Extension;
191
+ }
192
+ /**
193
+ A point decoration is a decoration that targets a given position
194
+ in the document, or the node after a given position. Sets of
195
+ point decorations can be provided as point sets through {@link
196
+ Decoration.Point.source}.
197
+ */
198
+ abstract class Point implements PointSet.Value {
199
+ abstract eq(other: PointSet.Value): boolean;
200
+ abstract side: number;
201
+ abstract trackMode: ChangeSet.TrackMode | undefined;
202
+ /**
203
+ Display a widget at this point.
204
+ */
205
+ static widget(widget: Widget, options?: {
206
+ /**
207
+ Determines where this widget appears relative to the cursor
208
+ (negative means before, positive after, zero means to make
209
+ it depend on the cursor's own side) and other widgets in the
210
+ same position. Defaults to zero.
211
+ */
212
+ side?: number;
213
+ /**
214
+ What side to track when changes happen around the widget.
215
+ The default is to keep the widget around unless the content
216
+ on both sides is deleted. You can pass undefined to indicate
217
+ the widget should not be deleted by changes, or
218
+ `"before"`/`"after"` to use one specific side.
219
+ */
220
+ trackMode?: ChangeSet.TrackMode | undefined;
221
+ }): Point;
222
+ /**
223
+ Add a set of attributes to the node after this decoration's
224
+ position.
225
+
226
+ You can target a [specific
227
+ element](#editor.Decoration.Tag.wrapper.options.target) in the
228
+ node's representation with the `target` option.
229
+ */
230
+ static attributes(attrs: Record<string, string>, options?: {
231
+ target?: string;
232
+ }): Point;
233
+ /**
234
+ Override the shape of the node after the decoration's point
235
+ with the given one.
236
+ */
237
+ static shape(shape: Shape): Point;
238
+ /**
239
+ Wrap the node, or inner node selected with `target`, at the
240
+ given position with a wrapper.
241
+ */
242
+ static wrapper(wrapper: DecoElt, spec?: {
243
+ target?: string;
244
+ }): Point;
245
+ /**
246
+ The facet used to register a point decoration source.
247
+ Functions provided in this way will be called on every editor
248
+ update, so computing the set on the fly will only perform well
249
+ for very simple decoration sets, and you'll usually want to
250
+ keep your set in a state field and update it incrementally.
251
+ */
252
+ static source: GardState.Facet<(state: GardState) => PointSet<Point>, readonly ((state: GardState) => PointSet<Point>)[]>;
253
+ }
254
+ /**
255
+ Range decorations apply to a document range. They are stored in
256
+ {@link RangeSet}s and registered in an editor configuration with
257
+ {@link Decoration.Range.source}.
258
+ */
259
+ abstract class Range implements RangeSet.Value {
260
+ /**
261
+ @hidden
262
+ */
263
+ protected constructor(spec: Decoration.Range.Spec);
264
+ get inclusiveStart(): boolean;
265
+ get inclusiveEnd(): boolean;
266
+ abstract eq(other: RangeSet.Value): boolean;
267
+ /**
268
+ Create a range decoration that wraps nodes in a range with
269
+ an element, using the given tag name.
270
+ */
271
+ static wrapper(tagName: string, spec: Decoration.Range.WrapperSpec): Range;
272
+ /**
273
+ Create a range decoration that adds an attribute to nodes in a
274
+ range.
275
+ */
276
+ static attribute(attr: string, value: string, options?: Decoration.Range.Spec): Range;
277
+ /**
278
+ The facet used to register range decoration sources. The
279
+ source function will be called on every update. Generating big
280
+ range sets on the fly will not perform well, so you'll often
281
+ want to store these in a state field.
282
+ */
283
+ static source: GardState.Facet<(state: GardState) => RangeSet<Range>, readonly ((state: GardState) => RangeSet<Range>)[]>;
284
+ }
285
+ namespace Range {
286
+ /**
287
+ Configuration object for range decorations.
288
+ */
289
+ interface Spec {
290
+ /**
291
+ Determines whether content inserted next to the range is
292
+ included when mapping the range through a change. Defaults
293
+ to false.
294
+ */
295
+ inclusive?: boolean | "start" | "end";
296
+ /**
297
+ If given, apply this decoration only to matching nodes.
298
+ */
299
+ query?: Node$1.Query;
300
+ /**
301
+ The type of nodes in the range to apply the decoration to.
302
+ Defaults to `"atom"`.
303
+ */
304
+ scope?: "atom" | "inlineatom" | "all";
305
+ }
306
+ /**
307
+ Configuration object for wrapper range decorations.
308
+ */
309
+ interface WrapperSpec extends Decoration.Range.Spec {
310
+ /**
311
+ Attributes to add to the wrapper element.
312
+ */
313
+ attributes?: Record<string, string>;
314
+ /**
315
+ A wrapper's rank determines the nesting order between it and
316
+ other wrappers created by range decorations or marks. Should be
317
+ a number between 0 and 100, if given.
318
+ */
319
+ rank?: number;
320
+ /**
321
+ Whether this wrapper may span multiple sibling nodes.
322
+ Non-spanning wrappers will be created separately for each
323
+ node. Defaults to true.
324
+ */
325
+ spanning?: boolean;
326
+ }
327
+ }
328
+ }
329
+ /**
330
+ Data structure used to store sets of points and then track them
331
+ across document changes. Mostly used for {@link Decoration.Point
332
+ point decorations}, but can also track your own types, if you make
333
+ sure they implement the {@link PointSet.Value} interface.
334
+ */
335
+ declare class PointSet<T extends PointSet.Value = PointSet.Value> {
336
+ /**
337
+ The values in this set.
338
+ */
339
+ readonly values: readonly T[];
340
+ /**
341
+ The positions of the values in this set.
342
+ */
343
+ readonly positions: readonly number[];
344
+ private constructor();
345
+ /**
346
+ The number of points in this set.
347
+ */
348
+ get length(): number;
349
+ /**
350
+ Adjust the points for a set of document changes. Returns a new
351
+ set with the adjusted points. May delete points when the content
352
+ around them was deleted.
353
+ */
354
+ map(changes: ChangeSet, start?: number): PointSet<T>;
355
+ /**
356
+ Returns the union of this set and the given set. If
357
+ `maskFrom`/`maskTo` are given, drop any points from `this`
358
+ between or at those positions.
359
+ */
360
+ merge(other: PointSet<T>, maskFrom?: number, maskTo?: number | undefined): PointSet<T>;
361
+ /**
362
+ Get the value at the given position, if any. If there's multiple
363
+ values at that position, the one with the lowest side is
364
+ returned.
365
+ */
366
+ at(pos: number): T | undefined;
367
+ /**
368
+ Create a point set from an iterable of `[position, value]`
369
+ tuples, or a function that calls its argument for every point to
370
+ add.
371
+ */
372
+ static create<T extends PointSet.Value>(source: Iterable<[number, T]> | ((add: (pos: number, value: T) => void) => void)): PointSet<T>;
373
+ /**
374
+ The empty point set.
375
+ */
376
+ static empty: PointSet<any>;
377
+ }
378
+ declare namespace PointSet {
379
+ /**
380
+ Objects stored in a point set must conform to this interface.
381
+ */
382
+ interface Value {
383
+ /**
384
+ The side of the point. Used to provide a sorting of points at
385
+ the same position
386
+ */
387
+ side: number;
388
+ /**
389
+ Specifies whether the point should be deleted when content
390
+ next to it is deleted. See {@link ChangeSet.mapPos}.
391
+ */
392
+ trackMode: ChangeSet.TrackMode | undefined;
393
+ /**
394
+ Method to compare this value to another.
395
+ */
396
+ eq(other: PointSet.Value): boolean;
397
+ }
398
+ }
399
+ /**
400
+ Data structure that stores sets of ranges, for use with {@link
401
+ Decoration.Range range decorations} or other data types
402
+ implementing {@link RangeSet.Value}.
403
+ */
404
+ declare class RangeSet<T extends RangeSet.Value = RangeSet.Value> {
405
+ /**
406
+ The value associated with the ranges in the set.
407
+ */
408
+ readonly values: readonly T[];
409
+ /**
410
+ The start positions of the ranges in this set.
411
+ */
412
+ readonly from: readonly number[];
413
+ /**
414
+ The end positions of the ranges.
415
+ */
416
+ readonly to: readonly number[];
417
+ private constructor();
418
+ /**
419
+ The number of ranges stored in this set.
420
+ */
421
+ get length(): number;
422
+ /**
423
+ Adjust the positions of the ranges for the given change set.
424
+ Returns a set with the updated ranges.
425
+ */
426
+ map(changes: ChangeSet, start?: number): RangeSet<T>;
427
+ /**
428
+ Merge this set with another set. If `maskFrom`/`maskTo` are
429
+ given, any ranges overlapping the masked range in `this` are
430
+ not included in the merged set.
431
+ */
432
+ merge(other: RangeSet<T>, maskFrom?: number, maskTo?: number | undefined): RangeSet<T>;
433
+ /**
434
+ Create a range set from an iterable of `[from, to, value]`
435
+ tuples, or a function that calls its argument for every range to
436
+ add.
437
+ */
438
+ static create<T extends RangeSet.Value>(source: Iterable<[number, number, T]> | ((add: (from: number, to: number, value: T) => void) => void)): RangeSet<T>;
439
+ /**
440
+ The empty range set.
441
+ */
442
+ static empty: RangeSet<any>;
443
+ }
444
+ declare namespace RangeSet {
445
+ /**
446
+ Values stored in a range set must conform to this interface.
447
+ */
448
+ interface Value {
449
+ /**
450
+ Whether content inserted at the start of this value's range is
451
+ included in the range.
452
+ */
453
+ inclusiveStart: boolean;
454
+ /**
455
+ Whether content inserted at the end is included.
456
+ */
457
+ inclusiveEnd: boolean;
458
+ /**
459
+ Compare this value to another.
460
+ */
461
+ eq(other: Value): boolean;
462
+ }
463
+ }
464
+
465
+ declare const enum TileFlag {
466
+ None = 0,
467
+ NodeInner = 1,
468
+ PlotContent = 2,
469
+ Spanning = 4,
470
+ Wrapper = 8,
471
+ Point = 16,
472
+ PointBefore = 32,
473
+ PointAfter = 64,
474
+ PointSide = 96,
475
+ Composition = 128,
476
+ Synced = 256,// Node has been synced. DOM content matches child list / text content, child array becomes read-only
477
+ Atom = 512,// Composite tile whose length isn't determined by child length
478
+ HasContent = 1024,// EltTile whose elt has a content hole
479
+ AfterContent = 2048,// Tiles that sit after their parent's content position
480
+ ContentNotLast = 4096,// EltTile that has children with AfterContent flag
481
+ Dirty = 8192
482
+ }
483
+ declare const enum Orientation {
484
+ Row = 0,
485
+ Col = 1
486
+ }
487
+ declare class CoordPos {
488
+ readonly pos: number;
489
+ readonly target: number | null;
490
+ readonly side: -1 | 1;
491
+ readonly vertOutside: boolean;
492
+ constructor(pos: number, target: number | null, side: -1 | 1, vertOutside: boolean);
493
+ map(mapping: ChangeSet): CoordPos;
494
+ static create(pos: number, side: -1 | 1, target?: number | null, vertOutside?: boolean): CoordPos;
495
+ }
496
+ declare abstract class Tile {
497
+ dom: Element | Text;
498
+ parent: CompositeTile | null;
499
+ abstract children: Tile[];
500
+ length: number;
501
+ flags: TileFlag;
502
+ constructor(dom: Element | Text, flags: number);
503
+ get isAtom(): boolean;
504
+ get isNodeOuter(): boolean;
505
+ get isNodeInner(): boolean;
506
+ get isNode(): boolean;
507
+ get isPlotContent(): boolean;
508
+ get isText(): boolean;
509
+ get isDoc(): boolean;
510
+ get isWrapper(): boolean;
511
+ get isSpanning(): boolean;
512
+ get isComposition(): boolean;
513
+ get isPoint(): boolean;
514
+ get node(): Node$1 | null;
515
+ posBeforeChild(child: Tile, ownStart?: number): number;
516
+ get posBefore(): number;
517
+ get posAtStart(): number;
518
+ get posAfter(): number;
519
+ get posAtEnd(): number;
520
+ get boundary(): 0 | 1;
521
+ get firstChild(): Tile | null;
522
+ get lastChild(): Tile | null;
523
+ get nodeParent(): Tile;
524
+ ignoreEvent(event: Event): boolean;
525
+ get ignoreMutations(): boolean;
526
+ toString(): string;
527
+ abstract sync(): void;
528
+ connect(): void;
529
+ disconnect(reused?: Map<Tile, Reused>): void;
530
+ nearestNode(): Tile;
531
+ markDirty(): void;
532
+ posAtCoords(state: GardState, x: number, y: number): CoordPos;
533
+ abstract posAtCoordsInner(start: number, state: GardState, x: number, y: number, textblock: TextblockMap | null, orientation: Orientation): CoordPos;
534
+ static get(node: DOMNode): Tile | undefined;
535
+ }
536
+ declare class CompositeTile extends Tile {
537
+ children: Tile[];
538
+ dom: Element;
539
+ addChild(child: Tile): void;
540
+ sync(): void;
541
+ syncChildren(): void;
542
+ posAtCoordsInner(start: number, state: GardState, x: number, y: number, textblock: TextblockMap | null, orientation: Orientation): CoordPos;
543
+ posAtCoordsRow(start: number, state: GardState, x: number, y: number, textblock: TextblockMap | null): CoordPos | null;
544
+ posAtCoordsCol(start: number, state: GardState, x: number, y: number, textblock: TextblockMap | null): CoordPos;
545
+ }
546
+ declare const enum Reused {
547
+ Full = 1,
548
+ DOM = 2
549
+ }
550
+
551
+ type DOMNode = Node;
552
+ declare global {
553
+ interface Node {
554
+ wgTile?: Tile;
555
+ }
556
+ }
557
+
558
+ /**
559
+ This class implements the editor's user interface. It wraps the
560
+ editable DOM surface and possibly other elements such as panels.
561
+ */
562
+ declare class Wordgard {
563
+ /**
564
+ Construct a new editor. You'll want to either provide a `parent`
565
+ option, or put the editor's {@link Wordgard.dom DOM element}
566
+ into your document after creating an editor, so that the user
567
+ can see it.
568
+ */
569
+ static create(spec: Wordgard.Spec): Wordgard;
570
+ /**
571
+ The current editor state.
572
+ */
573
+ get state(): GardState;
574
+ /**
575
+ Indicates whether the user is currently composing text via
576
+ [IME](https://en.wikipedia.org/wiki/Input_method), and at least
577
+ one change has been made in the current composition.
578
+ */
579
+ get composing(): boolean;
580
+ /**
581
+ Indicates whether the user is currently in composing state. Note
582
+ that on some platforms, like Android, this will be the case a
583
+ lot, since just putting the cursor on a word starts a
584
+ composition there.
585
+ */
586
+ get compositionStarted(): boolean | null;
587
+ /**
588
+ Queries whether the editor's DOM is {@link Wordgard#editable
589
+ editable}.
590
+ */
591
+ get editable(): boolean;
592
+ /**
593
+ Returns true if the editor can be focused (is {@link
594
+ Wordgard.editable editable} or has a tabindex).
595
+ */
596
+ get focusable(): boolean;
597
+ /**
598
+ The document or shadow root that the editor lives in.
599
+ */
600
+ root: DocumentOrShadowRoot;
601
+ /**
602
+ The outer DOM element that represents the editor.
603
+ */
604
+ readonly dom: HTMLElement;
605
+ /**
606
+ The DOM element that can be styled to scroll. (Note that it may
607
+ not have been, so you can't assume this is scrollable.)
608
+ */
609
+ readonly scrollDOM: HTMLElement;
610
+ /**
611
+ The editable DOM element holding the editor content. You should
612
+ not, usually, interact with this content directly though the
613
+ DOM, since the editor will immediately undo most of the changes
614
+ you make. Instead, {@link Wordgard.dispatch dispatch} {@link
615
+ Transaction transactions} to modify content, and {@link
616
+ Decoration decorations} to style it.
617
+ */
618
+ readonly contentDOM: HTMLElement;
619
+ private announceDOM;
620
+ private id;
621
+ private pluginMap;
622
+ private editorAttrs;
623
+ private contentAttrs;
624
+ private styleModules;
625
+ /**
626
+ True when the editor is connected to a DOM document.
627
+ */
628
+ connected: boolean;
629
+ private flushing;
630
+ private willFlush;
631
+ private flushFunc;
632
+ private autoColorScheme;
633
+ private domReaders;
634
+ private domWriters;
635
+ private pendingTransactionListeners;
636
+ private constructor();
637
+ /**
638
+ All editor state updates go through this. It takes a transaction
639
+ or transaction spec and updates the editor to show the new state
640
+ produced by that transaction. This function is bound to the editor
641
+ instance, so it does not have to be called as a method.
642
+
643
+ Will apply {@link Transaction.appender transaction appenders}
644
+ and include any extra transactions they produce in the editor's
645
+ state.
646
+
647
+ Updates will be immediately be reflected in the object's `state`
648
+ property, but updating the DOM will be deferred to the next
649
+ display update.
650
+ */
651
+ dispatch(tr: Transaction | Transaction.Spec): void;
652
+ /**
653
+ Force a flush on the editor content, updating its DOM
654
+ representation for any pending changes.
655
+ */
656
+ flush(): void;
657
+ private scrollTo;
658
+ private runUpdate;
659
+ private updatePlugins;
660
+ private updateAttrs;
661
+ private checkDir;
662
+ private showAnnouncements;
663
+ private mountStyles;
664
+ /**
665
+ Schedule a function that needs to read from the (flushed) DOM.
666
+ During an editor update, when doing anything that needs to
667
+ access the DOM layout, it is important to schedule it with this
668
+ method, to avoid forcing unnecessary DOM layouts.
669
+ */
670
+ scheduleDOMRead(read: (wg: Wordgard) => void): void;
671
+ /**
672
+ Schedule a function that needs to modify the DOM. When doing any
673
+ kind of DOM mutation that depends on a {@link
674
+ Wordgard.scheduleDOMRead | DOM read}, use this method, so that
675
+ read and write phases remain separate.
676
+ */
677
+ scheduleDOMWrite(write: (wg: Wordgard) => void): void;
678
+ /**
679
+ Get the value of a specific plugin, if present. Note that
680
+ plugins that crash can be dropped from an editor, so even when
681
+ you know you registered a given plugin, it is recommended to
682
+ check the return value of this method.
683
+ */
684
+ plugin<T extends Wordgard.Plugin.Value>(plugin: Wordgard.Plugin<T>): T | null;
685
+ private ensureFlushed;
686
+ /**
687
+ Find the position at the end or start of the (wrapped) line. If
688
+ the given position isn't in a textblock, this will return null.
689
+ */
690
+ moveToLineBoundary(start: GardSelection, forward: boolean): GardSelection.Text | null;
691
+ /**
692
+ Move a cursor position vertically. When `distance` isn't given,
693
+ it defaults to moving to the vertical element below or above the
694
+ start position. Otherwise, `distance` should provide a positive
695
+ distance in pixels.
696
+
697
+ When `start` has a
698
+ {@link GardSelection.goalColumn `goalColumn`}, the vertical
699
+ motion will use that as a target horizontal position. Otherwise,
700
+ the cursor's own horizontal position is used. The returned
701
+ cursor will have its goal column set to whichever column was
702
+ used. If `allowNode` is true, this may return a node selection
703
+ on a block node.
704
+ */
705
+ moveVertically(start: GardSelection, forward: boolean, distance?: number, allowNode?: boolean): GardSelection | null;
706
+ /**
707
+ Find the DOM parent node and offset (child offset if `node` is
708
+ an element, character offset when it is a text node) at the
709
+ given document position.
710
+ */
711
+ domAtPos(pos: number, assoc?: -1 | 1): {
712
+ node: DOMNode;
713
+ offset: number;
714
+ };
715
+ /**
716
+ Get the DOM element for the node at the given position, if any.
717
+ */
718
+ nodeDOM(pos: number): Element | null;
719
+ /**
720
+ Find the document position at the given DOM node. Can be useful
721
+ for associating positions with DOM events. Will raise an error
722
+ when `node` isn't part of the editor content.
723
+ */
724
+ posAtDOM(node: DOMNode, offset?: number): number;
725
+ /**
726
+ Find the Wordgard node represented by the given DOM node, or one
727
+ of its parent nodes, if any. Will not return the outer document node.
728
+ */
729
+ nodeFromDOM(node: Element): {
730
+ pos: number;
731
+ node: Node$1;
732
+ } | null;
733
+ /**
734
+ Get the document position at the given screen coordinates.
735
+ */
736
+ posAtCoords(coords: {
737
+ x: number;
738
+ y: number;
739
+ }): {
740
+ pos: number;
741
+ side: -1 | 1;
742
+ target: number | null;
743
+ };
744
+ /**
745
+ Get the screen coordinates at the given document position.
746
+ `side` determines whether the coordinates are based on the
747
+ element before (-1) or after (1) the position (if no element is
748
+ available on the given side, the method will transparently use
749
+ another strategy to get reasonable coordinates).
750
+ */
751
+ coordsAtPos(pos: number, assoc?: -1 | 1): DOMRect;
752
+ /**
753
+ Return the rectangle around a given node or character. If there
754
+ is no element directly after `pos`, this will return null. For
755
+ space characters that are a line wrap point, this will return
756
+ the position before the line break.
757
+ */
758
+ coordsForElement(pos: number): DOMRect | null;
759
+ /**
760
+ Check whether the editor has focus.
761
+ */
762
+ get hasFocus(): boolean;
763
+ /**
764
+ Put focus on the editor.
765
+ */
766
+ focus(): void;
767
+ /**
768
+ Get the CSS classes for the currently active editor themes.
769
+ */
770
+ get themeClasses(): string;
771
+ /**
772
+ Returns an effect that can be {@link Transaction.Spec.effects
773
+ added} to a transaction to cause it to scroll the given position
774
+ or range into view.
775
+ */
776
+ static scrollIntoView(pos: number | GardSelection, options?: Wordgard.ScrollSpec): Transaction.Effect<unknown>;
777
+ /**
778
+ Add an
779
+ [`aria-label`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-label)
780
+ attribute to the editable element holding the given string or
781
+ phrase.
782
+ */
783
+ static label(label: string | PhraseSet.Ref): GardState.Extension;
784
+ /**
785
+ Filter functions provided through this facet will be run on a
786
+ slice before it is serialized to the clipboard.
787
+ */
788
+ static clipboardOutputFilter: GardState.Facet<(content: wordgard_doc.Slice, state: GardState) => wordgard_doc.Slice, readonly ((content: wordgard_doc.Slice, state: GardState) => wordgard_doc.Slice)[]>;
789
+ /**
790
+ Filter functions provided through this facet will be run on an
791
+ HTML string before it put onto the clipboard.
792
+ */
793
+ static clipboardOutputHTMLFilter: GardState.Facet<(html: string, state: GardState) => string, readonly ((html: string, state: GardState) => string)[]>;
794
+ /**
795
+ This can be used to provide a function that converts a document
796
+ slice to a string that is put onto the plain-text clipboard.
797
+ Serializers are tried in order of precedence until one returns
798
+ a string.
799
+ */
800
+ static clipboardTextSerializer: GardState.Facet<(slice: wordgard_doc.Slice, context: readonly wordgard_doc.Plot.Tag[], state: GardState) => string | null, readonly ((slice: wordgard_doc.Slice, context: readonly wordgard_doc.Plot.Tag[], state: GardState) => string | null)[]>;
801
+ /**
802
+ Filter to run on the plain text representation of content put
803
+ onto the clipboard.
804
+ */
805
+ static clipboardOutputTextFilter: GardState.Facet<(html: string, state: GardState) => string, readonly ((html: string, state: GardState) => string)[]>;
806
+ /**
807
+ Filter functions provided through this facet will be run on a
808
+ slice after it is read from the clipboard.
809
+ */
810
+ static clipboardInputFilter: GardState.Facet<(content: wordgard_doc.Slice, state: GardState) => wordgard_doc.Slice, readonly ((content: wordgard_doc.Slice, state: GardState) => wordgard_doc.Slice)[]>;
811
+ /**
812
+ Filter functions to run on HTML text that is read from the
813
+ clipboard.
814
+ */
815
+ static clipboardInputHTMLFilter: GardState.Facet<(html: string, state: GardState) => string, readonly ((html: string, state: GardState) => string)[]>;
816
+ /**
817
+ When the editor reads plain text from the clipboard, this facet
818
+ can be used to provide a custom parser. Each provided function
819
+ is tried in order of precedence, until one returns a slice.
820
+ */
821
+ static clipboardTextParser: GardState.Facet<(text: string, state: GardState) => wordgard_doc.Slice | null, readonly ((text: string, state: GardState) => wordgard_doc.Slice | null)[]>;
822
+ /**
823
+ Filter to run on plain text read from the clipboard.
824
+ */
825
+ static clipboardInputTextFilter: GardState.Facet<(html: string, state: GardState) => string, readonly ((html: string, state: GardState) => string)[]>;
826
+ /**
827
+ Facet that allows you to register handlers to override paste
828
+ behavior.
829
+ */
830
+ static pasteHandler: GardState.Facet<(wg: Wordgard, event: ClipboardEvent, slice: wordgard_doc.Slice, context: readonly wordgard_doc.Plot.Tag[]) => boolean, readonly ((wg: Wordgard, event: ClipboardEvent, slice: wordgard_doc.Slice, context: readonly wordgard_doc.Plot.Tag[]) => boolean)[]>;
831
+ /**
832
+ Facet for custom drop handlers. When the drop is done inside the
833
+ editor and should move an existing range, the `move` parameter
834
+ will hold the origin range.
835
+ */
836
+ static dropHandler: GardState.Facet<(wg: Wordgard, event: DragEvent, pos: number, move: {
837
+ from: number;
838
+ to: number;
839
+ } | null, slice: wordgard_doc.Slice, context: readonly wordgard_doc.Plot.Tag[]) => boolean, readonly ((wg: Wordgard, event: DragEvent, pos: number, move: {
840
+ from: number;
841
+ to: number;
842
+ } | null, slice: wordgard_doc.Slice, context: readonly wordgard_doc.Plot.Tag[]) => boolean)[]>;
843
+ /**
844
+ This annotation is added to transactions created because the
845
+ editor's focused status changed. It holds `true` when the editor
846
+ gained focus, `false` when it lost focus.
847
+ */
848
+ static isFocusChange: Transaction.Annotation.Type<boolean>;
849
+ /**
850
+ Facet to add a [style
851
+ module](https://github.com/marijnh/style-mod#documentation) to
852
+ an editor. The editor will ensure that the module is mounted in
853
+ its {@link Wordgard.root document root}.
854
+ */
855
+ static styleModule: GardState.Facet<StyleModule, readonly StyleModule[]>;
856
+ /**
857
+ Returns an extension that can be used to add a DOM event handler
858
+ to the editor. For any given event, such functions are ordered
859
+ by extension precedence, and the first handler to return true
860
+ will be assumed to have handled that event, and no other
861
+ handlers or built-in behavior will be activated for it. These
862
+ are registered on the {@link Wordgard.contentDOM content
863
+ element}, except for `scroll` handlers, which will be called any
864
+ time the editor's {@link Wordgard.scrollDOM scroll element} or
865
+ one of its parent nodes is scrolled.
866
+ */
867
+ static domEventHandler<Event extends keyof HTMLElementEventMap>(event: Event, handler: (event: HTMLElementEventMap[Event], wg: Wordgard) => boolean | void): GardState.Extension;
868
+ /**
869
+ Create an extension that registers a DOM event observers. Contrary
870
+ to event {@link Wordgard.domEventHandler handlers},
871
+ observers can't be prevented from running by a higher-precedence
872
+ handler returning true. They also don't prevent other handlers
873
+ and observers from running when they return true, and should not
874
+ call `preventDefault`.
875
+ */
876
+ static domEventObserver<Event extends keyof HTMLElementEventMap>(event: Event, observer: (event: HTMLElementEventMap[Event], wg: Wordgard) => void): GardState.Extension;
877
+ /**
878
+ Scroll handlers can override how editor content is scrolled into
879
+ view. If they return `true`, no further handling happens for the
880
+ scrolling. If they return false, the default scroll behavior is
881
+ applied. Scroll handlers should never initiate editor updates.
882
+ */
883
+ static scrollHandler: GardState.Facet<(wg: Wordgard, target: {
884
+ from: number;
885
+ to: number;
886
+ } & Wordgard.ScrollSpec) => boolean, readonly ((wg: Wordgard, target: {
887
+ from: number;
888
+ to: number;
889
+ } & Wordgard.ScrollSpec) => boolean)[]>;
890
+ /**
891
+ Allows you to provide a function that should be called when the
892
+ library catches an exception from an extension (mostly from
893
+ plugins, but may be used by other extensions to route exceptions
894
+ from user-code-provided callbacks). This is mostly useful for
895
+ debugging and logging. See {@link Wordgard.logException}.
896
+ */
897
+ static exceptionSink: GardState.Facet<(exception: any) => void, readonly ((exception: any) => void)[]>;
898
+ /**
899
+ Registers a listener function to be called whenever a set of
900
+ transactions is applied to the editor. This function may
901
+ dispatch additional transactions, if needed.
902
+ */
903
+ static transactionListener: GardState.Facet<(trs: readonly Transaction[], wg: Wordgard) => void, readonly ((trs: readonly Transaction[], wg: Wordgard) => void)[]>;
904
+ private runTransactionListeners;
905
+ /**
906
+ A facet that can be used to register a function to be called
907
+ after the editor flushes updates to the DOM. Dispatching
908
+ transactions from such a function is allowed, but will cause a
909
+ new, separate update to happen.
910
+ */
911
+ static updateListener: GardState.Facet<(update: Wordgard.Update) => void, readonly ((update: Wordgard.Update) => void)[]>;
912
+ /**
913
+ Facet that controls whether the editor content DOM is editable.
914
+ When its highest-precedence value is `false`, the element will
915
+ not have its `contenteditable` attribute set. (Note that this
916
+ doesn't affect API calls that change the editor content, even
917
+ when those are bound to keys or buttons. See the {@link
918
+ GardState.readOnly `readOnly` facet} for that.)
919
+
920
+ A non-editable editor will, by default, not be focusable. You
921
+ can set a {@link Wordgard.contentAttributes content attribute}
922
+ of `tabindex: 0` to make an uneditable Wordgard focusable.
923
+ */
924
+ static editable: GardState.Facet<boolean, boolean>;
925
+ /**
926
+ Controls the length of a full cursor blink cycle, in milliseconds.
927
+ Defaults to 1200. Can be set to 0 to disable blinking.
928
+ */
929
+ static cursorBlinkRate: GardState.Facet<number, number>;
930
+ /**
931
+ Allows you to influence the way mouse selection happens. The
932
+ functions in this facet will be called for a `mousedown` event
933
+ on the editor, and can return an object that overrides the way a
934
+ selection is computed from that mouse click or drag.
935
+ */
936
+ static mouseSelectionStyle: GardState.Facet<MakeSelectionStyle, readonly MakeSelectionStyle[]>;
937
+ /**
938
+ Facet used to configure whether a given selection drag event
939
+ should move or copy the selection. The given predicate will be
940
+ called with the `mousedown` event, and can return `true` when
941
+ the drag should move the content. The default behavior is to
942
+ copy when holding Alt on Mac and Control on other platforms, and
943
+ move otherwise.
944
+ */
945
+ static dragMovesSelection: GardState.Facet<(event: MouseEvent) => boolean, readonly ((event: MouseEvent) => boolean)[]>;
946
+ /**
947
+ Create a theme extension. The first argument can be a
948
+ [`style-mod`](https://github.com/marijnh/style-mod#documentation)
949
+ style spec providing the styles for the theme. These will be
950
+ prefixed with a generated scope class.
951
+
952
+ Because the selectors are prefixed, rules that directly match
953
+ the editor's {@link Wordgard.dom wrapper element} (to which the
954
+ scope class will be added) need to be explicitly differentiated
955
+ by adding an `&` to the selector for that element—for example
956
+ `&:has(wg-content:focus)`.
957
+ */
958
+ static theme(spec: Record<string, StyleSpec>): GardState.Extension;
959
+ /**
960
+ This facet controls whether a dark or light color scheme is
961
+ active, which determines whether style rules with a `&dark` or
962
+ `&light` selector are applied. Defaults to `"light"`. If set to
963
+ `"auto"`, the editor uses a CSS `prefers-color-scheme: dark`
964
+ query to determine whether to enable light or dark mode.
965
+
966
+ Note that setting this to dark will not automatically make the
967
+ editor look dark. The default styling does not override the
968
+ inherited background and color of the editor. In case of a
969
+ page-wide `prefers-color-scheme` selection, those might already
970
+ be dark. But when setting an editor on a light background to
971
+ explicitly to use a dark theme, you'll need to make sure you
972
+ also load styles for that.
973
+ */
974
+ static colorScheme: GardState.Facet<"auto" | "dark" | "light", "auto" | "dark" | "light">;
975
+ /**
976
+ Create an extension that loads a set of style rules. Like
977
+ with {@link Wordgard.theme `theme`}, use `&` to indicate the
978
+ place of the editor wrapper element when directly targeting
979
+ that. You can also use `&dark` or `&light` instead to only
980
+ target editors with a dark or light {@link Wordgard.colorScheme
981
+ color scheme}.
982
+ */
983
+ static styles(spec: Record<string, StyleSpec>): GardState.Extension;
984
+ /**
985
+ Creates a simple theme that sets a height (given in pixels or,
986
+ if a string, a CSS number + unit) and automatic overflow
987
+ scrolling on the editor. (The default styling makes the editor
988
+ height fit its content.)
989
+ */
990
+ static scrolling(height: number | string): GardState.Extension;
991
+ /**
992
+ Provides a Content Security Policy nonce to use when creating
993
+ the style sheets for the editor. Holds the empty string when no
994
+ nonce has been provided.
995
+ */
996
+ static cspNonce: GardState.Facet<string, string>;
997
+ /**
998
+ Facet that provides additional DOM attributes for the editor's
999
+ editable DOM element, either directly, or as a function from the
1000
+ editor state.
1001
+ */
1002
+ static contentAttributes: GardState.Facet<AttrSource, readonly AttrSource[]>;
1003
+ /**
1004
+ Facet that provides DOM attributes for the editor's outer
1005
+ element.
1006
+ */
1007
+ static editorAttributes: GardState.Facet<AttrSource, readonly AttrSource[]>;
1008
+ /**
1009
+ State effect used to include screen reader announcements in a
1010
+ transaction. These will be added to the DOM in a visually hidden
1011
+ element with `aria-live="polite"` set, and should be used to
1012
+ describe effects that are visually obvious but may not be
1013
+ noticed by screen reader users (such as moving to the next
1014
+ search match).
1015
+ */
1016
+ static announce: Transaction.Effect.Type<string>;
1017
+ /**
1018
+ Facet that allows extensions to indicate that some amount of
1019
+ space around the sides of the scrolling element should be
1020
+ considered blocked from view when scrolling something into view.
1021
+ This is only used by plugins that introduce elements that cover
1022
+ part of the editor (for example a gutter).
1023
+ */
1024
+ static coveredMargins: GardState.Facet<(wg: Wordgard) => Partial<DOMRect> | null, readonly ((wg: Wordgard) => Partial<DOMRect> | null)[]>;
1025
+ }
1026
+ declare namespace Wordgard {
1027
+ /**
1028
+ The type of object given to {@link Wordgard.create}.
1029
+ */
1030
+ interface Spec extends Partial<GardState.Spec> {
1031
+ /**
1032
+ The editor's initial state. If not given, a new state is
1033
+ created by passing this configuration object to {@link
1034
+ GardState.create}, using its `doc`, `selection`, and
1035
+ `config` fields (if provided).
1036
+ */
1037
+ state?: GardState;
1038
+ /**
1039
+ When present, the editor is immediately appended to the given
1040
+ element on creation. (Otherwise, you'll have to place the
1041
+ editor {@link Wordgard.dom element} in the document yourself.)
1042
+ */
1043
+ parent?: Element | DocumentFragment;
1044
+ /**
1045
+ Pass an effect created with {@link Wordgard.scrollIntoView}
1046
+ here to set an initial scroll position.
1047
+ */
1048
+ scrollTo?: Transaction.Effect<any>;
1049
+ }
1050
+ /**
1051
+ Options passed to {@link Wordgard.scrollIntoView}.
1052
+ */
1053
+ type ScrollSpec = {
1054
+ /**
1055
+ By default (`"nearest"`) the position will be vertically
1056
+ scrolled only the minimal amount required to move the given
1057
+ position into view. You can set this to `"start"` to move it
1058
+ to the top of the editor, `"end"` to move it to the bottom, or
1059
+ `"center"` to move it to the center.
1060
+ */
1061
+ y?: "nearest" | "start" | "end" | "center";
1062
+ /**
1063
+ Effect similar to `y`, but for the horizontal scroll position.
1064
+ */
1065
+ x?: "nearest" | "start" | "end" | "center";
1066
+ /**
1067
+ Extra vertical distance to add when moving something into
1068
+ view. Not used with the `"center"` strategy. Defaults to 5.
1069
+ Must be less than the height of the editor.
1070
+ */
1071
+ yMargin?: number;
1072
+ /**
1073
+ Extra horizontal distance to add. Not used with the `"center"`
1074
+ strategy. Defaults to 5. Must be less than the width of the
1075
+ editor.
1076
+ */
1077
+ xMargin?: number;
1078
+ };
1079
+ /**
1080
+ The interface that objects registered with {@link
1081
+ Wordgard.mouseSelectionStyle} must conform to.
1082
+ */
1083
+ interface MouseSelectionStyle {
1084
+ /**
1085
+ Return a new selection for the mouse gesture that starts with
1086
+ the event that was originally given to the constructor, and ends
1087
+ with the event passed here. In case of a plain click, those may
1088
+ both be the `mousedown` event, in case of a drag gesture, the
1089
+ latest `mousemove` event will be passed.
1090
+
1091
+ When `extend` is true, that means the new selection should, if
1092
+ possible, extend the start selection.
1093
+ */
1094
+ get: (curEvent: MouseEvent, extend: boolean) => GardSelection;
1095
+ /**
1096
+ Called when the editor is updated while the gesture is in
1097
+ progress. When the document changes, it may be necessary to map
1098
+ some data (like the original selection or start position)
1099
+ through the changes.
1100
+
1101
+ This may return `true` to indicate that the `get` method should
1102
+ get queried again after the update, because something in the
1103
+ update could change its result. Be wary of infinite loops when
1104
+ using this (where `get` returns a new selection, which will
1105
+ trigger `update`, which schedules another `get` in response).
1106
+ */
1107
+ update: (update: Wordgard.Update) => boolean | void;
1108
+ }
1109
+ /**
1110
+ Log or report an unhandled exception in client code. Should
1111
+ probably only be used by extension code that allows client code to
1112
+ provide functions, and calls those functions in a context where an
1113
+ exception can't be propagated to calling code in a reasonable way
1114
+ (for example when in an event handler).
1115
+
1116
+ Either calls a handler registered with {@link
1117
+ Wordgard.exceptionSink}, `window.onerror`, if defined, or
1118
+ `console.error` (in which case it'll pass `context`, when given,
1119
+ as first argument).
1120
+ */
1121
+ function logException(state: GardState, exception: any, context?: string): void;
1122
+ /**
1123
+ Plugins associate stateful values with an editor. They can be
1124
+ useful for displaying interface elements, or keeping ephemeral
1125
+ interface state.
1126
+ */
1127
+ class Plugin<V extends Wordgard.Plugin.Value> {
1128
+ /**
1129
+ Instances of this class act as extensions.
1130
+ */
1131
+ extension: GardState.Extension;
1132
+ private constructor();
1133
+ /**
1134
+ Define a plugin from a constructor function that creates the
1135
+ plugin's value, given an editor.
1136
+ */
1137
+ static define<V extends Wordgard.Plugin.Value>(create: (wg: Wordgard) => V, provide?: (plugin: Wordgard.Plugin<V>) => GardState.Extension): Plugin<V>;
1138
+ /**
1139
+ Create a plugin for a class whose constructor takes an editor
1140
+ as only argument.
1141
+ */
1142
+ static fromClass<V extends Wordgard.Plugin.Value>(cls: {
1143
+ new (wg: Wordgard): V;
1144
+ }, provide?: (plugin: Wordgard.Plugin<V>) => GardState.Extension): Plugin<V>;
1145
+ /**
1146
+ Create an {@link Wordgard.domEventHandler event handler} for this
1147
+ plugin. Usually called from the plugin's `provide` function.
1148
+ */
1149
+ eventHandler<Event extends keyof HTMLElementEventMap>(event: Event, handler: (event: HTMLElementEventMap[Event], wg: Wordgard, value: V) => boolean | void): GardState.Extension;
1150
+ /**
1151
+ Create an {@link Wordgard.domEventObserver event observer} for this
1152
+ plugin.
1153
+ */
1154
+ eventObserver<Event extends keyof HTMLElementEventMap>(event: Event, observer: (event: HTMLElementEventMap[Event], wg: Wordgard, value: V) => void): GardState.Extension;
1155
+ }
1156
+ namespace Plugin {
1157
+ /**
1158
+ This is the interface plugin objects must expose.
1159
+ */
1160
+ interface Value {
1161
+ /**
1162
+ Notifies the plugin of an update that happened in the
1163
+ editor. This is called _before_ the editor updates its own
1164
+ DOM. It is responsible for updating the plugin's internal
1165
+ state (including any state that may be read by plugin
1166
+ fields) and _writing_ to the DOM for the changes in the
1167
+ update. To avoid unnecessary layout recomputations, it
1168
+ should _not_ read the DOM layout—use {@link
1169
+ Wordgard.scheduleDOMRead} to schedule your
1170
+ code in a DOM reading phase if you need to.
1171
+ */
1172
+ update?(update: Wordgard.Update): void;
1173
+ /**
1174
+ When present, this will be called when an update causes any
1175
+ changes in the DOM representation of the document.
1176
+ */
1177
+ docUpdate?(wg: Wordgard): void;
1178
+ /**
1179
+ Called when the editor is attached to the DOM. If the plugin
1180
+ needs to allocate any resource that must be released, or modify
1181
+ something outside the editor, it should do it in this method,
1182
+ and make sure to release/undo it in its `disconnect` method.
1183
+ */
1184
+ connect?(wg: Wordgard): void;
1185
+ /**
1186
+ Called when the editor is removed from the DOM, or the
1187
+ plugin is removed from the editor.
1188
+ */
1189
+ disconnect?(wg: Wordgard): void;
1190
+ /**
1191
+ Called when the plugin is removed from an editor. This
1192
+ should clean up any changes it made to the editor itself. If
1193
+ the editor was connected to a document, {@link
1194
+ Wordgard.Plugin.Value.disconnect `disconnect`} will be called
1195
+ before this.
1196
+ */
1197
+ remove?(wg: Wordgard): void;
1198
+ }
1199
+ }
1200
+ /**
1201
+ Editor {@link Wordgard.Plugin plugins} and {@link
1202
+ Wordgard.updateListener update listeners} are given instances of
1203
+ this class whenever the editor is updated.
1204
+ */
1205
+ class Update {
1206
+ /**
1207
+ The editor that the update is associated with.
1208
+ */
1209
+ readonly editor: Wordgard;
1210
+ /**
1211
+ The previous editor state.
1212
+ */
1213
+ readonly startState: GardState;
1214
+ /**
1215
+ The new editor state.
1216
+ */
1217
+ readonly state: GardState;
1218
+ /**
1219
+ The transactions involved in the update. May be empty.
1220
+ */
1221
+ readonly transactions: readonly Transaction[];
1222
+ /**
1223
+ The changes made to the document by this update.
1224
+ */
1225
+ readonly changes: ChangeSet;
1226
+ private constructor();
1227
+ /**
1228
+ Returns true when the document was modified or when the size
1229
+ of the editor, or elements within the editor, changed.
1230
+ */
1231
+ get geometryChanged(): boolean;
1232
+ /**
1233
+ True when this update indicates a focus change.
1234
+ */
1235
+ get focusChanged(): boolean;
1236
+ /**
1237
+ Whether the document changed in this update.
1238
+ */
1239
+ get docChanged(): boolean;
1240
+ /**
1241
+ Whether the selection was explicitly set in this update.
1242
+ */
1243
+ get selectionSet(): boolean;
1244
+ }
1245
+ }
1246
+ type AttrSource = Record<string, string | null> | ((wg: Wordgard) => Record<string, string | null>);
1247
+
1248
+ /**
1249
+ Key bindings associate keys with functions that should be run when
1250
+ a matching keyboard event happens.
1251
+
1252
+ A key binding can either specify a specific {@link
1253
+ KeyBinding.Spec.char character} to match on, which will be
1254
+ compared against the actual character produced by a key event, or
1255
+ describe a {@link KeyBinding.Spec.key key combination}.
1256
+
1257
+ Bindings for a given key event are evaluated in order of
1258
+ precedence, with each getting a chance to handle the event,
1259
+ stopping when the first handler returns true.
1260
+
1261
+ Key combinations are described by strings like
1262
+ `"Shift-Ctrl-Enter"`—a key identifier prefixed with zero or more
1263
+ modifiers. Key identifiers are based on the strings that can
1264
+ appear in
1265
+ [`KeyEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key).
1266
+ Use lowercase letters to refer to letter keys. You can use
1267
+ `"Space"` as an alias for the `" "` name.
1268
+
1269
+ Modifiers can be given in any order. `Shift-` (or `s-`), `Alt-` (or
1270
+ `a-`), `Ctrl-` (or `c-` or `Control-`) and `Cmd-` (or `m-` or
1271
+ `Meta-`) are recognized.
1272
+
1273
+ You can use `Mod-` as a shorthand for `Cmd-` on Mac and `Ctrl-` on
1274
+ other platforms. So `Mod-b` is `Ctrl-b` on Linux but `Cmd-b` on
1275
+ macOS.
1276
+
1277
+ Unlike character bindings, key combination bindings should refer
1278
+ to the unmodified base key that is being pressed, not the
1279
+ character produced by combining that key with Shift or AltGraph.
1280
+ Keyboard mappings that rearrange the positions of Latin characters
1281
+ _are_ taken into account for this (the mapped position is used),
1282
+ but the library tries to 'see through' keyboard mappings that
1283
+ assign non-Latin characters to keys (so that both the Latin and
1284
+ the non-Latin name can be used).
1285
+ */
1286
+ declare class KeyBinding {
1287
+ /**
1288
+ The configuration object used to define this binding.
1289
+ */
1290
+ readonly spec: KeyBinding.Spec;
1291
+ /**
1292
+ Bindings count as extensions and can be included in an editor
1293
+ configuration.
1294
+ */
1295
+ extension: GardState.Extension;
1296
+ private constructor();
1297
+ /**
1298
+ Define a binding.
1299
+ */
1300
+ static of(spec: KeyBinding.Spec): KeyBinding;
1301
+ }
1302
+ declare namespace KeyBinding {
1303
+ /**
1304
+ A description of a key binding.
1305
+ */
1306
+ interface Spec {
1307
+ /**
1308
+ A textual character that this binding should trigger for.
1309
+ */
1310
+ char?: string;
1311
+ /**
1312
+ A key combination to use for this binding. If the
1313
+ platform-specific property (`mac`, `win`, or `linux`) for the
1314
+ current platform is used as well in the binding, that one takes
1315
+ precedence. If `key` isn't defined and the platform-specific
1316
+ binding isn't either, a binding is ignored.
1317
+ */
1318
+ key?: string;
1319
+ /**
1320
+ Key to use specifically on macOS.
1321
+ */
1322
+ mac?: string;
1323
+ /**
1324
+ Key to use specifically on Windows.
1325
+ */
1326
+ win?: string;
1327
+ /**
1328
+ Key to use specifically on Linux.
1329
+ */
1330
+ linux?: string;
1331
+ /**
1332
+ The command to execute when this binding is triggered.
1333
+ */
1334
+ run: Command.Bound | Command;
1335
+ /**
1336
+ When given, this defines a second binding, using the (possibly
1337
+ platform-specific) key name, prefixed with `Shift-`, to activate
1338
+ this command.
1339
+ */
1340
+ shift?: Command.Bound | Command;
1341
+ /**
1342
+ When this property is present, the function is called for every
1343
+ key, and may return true to indicate the key was handled.
1344
+ */
1345
+ any?: (wg: Wordgard, event: KeyboardEvent) => boolean;
1346
+ /**
1347
+ By default, key bindings apply when focus is on the editor
1348
+ content (the `"editor"` scope). Some extensions, mostly those
1349
+ that define their own panels, might want to allow registering
1350
+ bindings local to that panel. Such bindings should use a custom
1351
+ scope name. You may also assign multiple scope names to a
1352
+ binding, separating them by spaces.
1353
+ */
1354
+ scope?: string;
1355
+ /**
1356
+ By default, all keys events for which a handler exists have
1357
+ their `preventDefault` called, even if no handler returns
1358
+ true. You can set this to true to disable that behavior.
1359
+ */
1360
+ allowDefault?: boolean;
1361
+ }
1362
+ /**
1363
+ Run the key handlers registered for a given scope. The event
1364
+ object should be a `"keydown"` event. Returns true if any of the
1365
+ handlers handled it.
1366
+ */
1367
+ function runScopeHandlers(wg: Wordgard, event: KeyboardEvent, scope: string): boolean;
1368
+ /**
1369
+ Facet used for registering key bindings. Extension precedence
1370
+ determines the order in which bindings that match the same key
1371
+ are called. When a handler has returned `true` for a given key,
1372
+ no further handlers are called.
1373
+ */
1374
+ const source: GardState.Facet<KeyBinding, readonly KeyBinding[]>;
1375
+ /**
1376
+ By default, the {@link KeyBinding.defaultKeymap default keymap}
1377
+ is automatically active. You can configure this to false if you
1378
+ want to completely replace it.
1379
+ */
1380
+ const useDefaultKeymap: GardState.Facet<boolean, boolean>;
1381
+ /**
1382
+ The editor's set of default key bindings. Binds the following
1383
+ keys. Most cursor motion bindings include a `Shift-` variant
1384
+ that passes the `extend` flag to the command. Enabled by default
1385
+ unless {@link KeyBinding.useDefaultKeymap} is disabled.
1386
+
1387
+ - `Enter` to {@link command.enter}
1388
+ - `Shift-Enter` to {@link command.insertLineBreak}
1389
+ - `Backspace` to {@link command.deleteUnit} (`"backward"`)
1390
+ - `Delete` to {@link command.deleteUnit} (`"forward"`)
1391
+ - `Ctrl-Backspace` (`Alt-Backspace` on MacOS) to {@link command.deleteWord} (`"backward"`)
1392
+ - `Ctrl-Delete` (`Alt-Delete` on MacOS) to {@link command.deleteWord} (`"forward"`)
1393
+ - `Cmd-Backspace` (MacOS) to {@link command.deleteToLineEnd} (`"backward"`)
1394
+ - `Cmd-Delete` (MacOS) to {@link command.deleteToLineEnd} (`"forward"`)
1395
+ - `ArrowLeft` to {@link command.moveByUnit} (`{dir: "left"}`)
1396
+ - `ArrowRight` to {@link command.moveByUnit} (`{dir: "right"}`)
1397
+ - `ArrowUp` to {@link command.moveByLine} (`{dir: "up"}`)
1398
+ - `ArrowDown` to {@link command.moveByLine} (`{dir: "down"}`)
1399
+ - `Ctrl-AllowLeft` (`Cmd-ArrowLeft` on MacOS) to {@link command.moveByWord} (`{dir: "left"}`)
1400
+ - `Ctrl-AllowRight` (`Cmd-ArrowRight` on MacOS) to {@link command.moveByWord} (`{dir: "right"}`)
1401
+ - `Cmd-ArrowUp` (MacOS) to {@link command.moveToDocSide} (`{side: "start"}`)
1402
+ - `Cmd-ArrowDown` (MacOS) to {@link command.moveToDocSide} (`{side: "end"}`)
1403
+ - `Ctrl-ArrowUp` (MacOS) to {@link command.moveByPage} (`{dir: "up"}`)
1404
+ - `Ctrl-ArrowDown` (MacOS) to {@link command.moveByPage} (`{dir: "down"}`)
1405
+ - `PageUp` to {@link command.moveByPage} (`{dir: "up"}`)
1406
+ - `PageDown` to {@link command.moveByPage} (`{dir: "down"}`)
1407
+ - `Home` to {@link command.moveToLineSide} (`{dir: "backward"}`)
1408
+ - `End` to {@link command.moveToLineSide} (`{dir: "forward"}`)
1409
+ - `Ctrl-Home` (`Cmd-Home` on MacOS) to {@link command.moveToDocSide} (`{side: "start"}`)
1410
+ - `Ctrl-End` (`Cmd-End` on MacOS) to {@link command.moveToDocSide} (`{side: "end"}`)
1411
+ - `Ctrl-a` (`Cmd-a` on MacOS) to {@link command.selectAll}
1412
+ - `Ctrl-z` (`Cmd-z` on MacOS) to {@link command.undo}
1413
+ - `Ctrl-y` (`Shift-Cmd-z` on MacOS) to {@link command.redo}
1414
+
1415
+ On MacOS, the following Emacs-style bindings are available:
1416
+
1417
+ - `Ctrl-b` to {@link command.moveByUnit} (`{dir: "backward"}`)
1418
+ - `Ctrl-f` to {@link command.moveByUnit} (`{dir: "forward"}`)
1419
+ - `Ctrl-p` to {@link command.moveByLine} (`{dir: "up"}`)
1420
+ - `Ctrl-n` to {@link command.moveByLine} (`{dir: "down"}`)
1421
+ - `Ctrl-a` to {@link command.moveToTextblockSide} (`{dir: "backward"}`)
1422
+ - `Ctrl-e` to {@link command.moveToTextblockSide} (`{dir: "forward"}`)
1423
+ - `Ctrl-d` to {@link command.deleteUnit} (`"forward"`)
1424
+ - `Ctrl-h` to {@link command.deleteUnit} (`"backward"`)
1425
+ - `Ctrl-k` to {@link command.deleteToLineEnd} (`"forward"`)
1426
+ - `Ctrl-Alt-h` to {@link command.deleteWord} (`"backward"`)
1427
+ - `Ctrl-o` to {@link command.insertLineBreak}
1428
+ - `Ctrl-t` to {@link command.transposeChars}
1429
+ - `Ctrl-v` to {@link command.moveByPage} (`{dir: "down"}`)
1430
+ */
1431
+ const defaultKeymap: readonly KeyBinding[];
1432
+ }
1433
+
1434
+ type PanelConfig = {
1435
+ /**
1436
+ By default, panels will be placed inside the editor's DOM
1437
+ structure. You can use this option to override where panels with
1438
+ `top: true` are placed.
1439
+ */
1440
+ topContainer?: HTMLElement;
1441
+ /**
1442
+ Override where panels with `top: false` are placed.
1443
+ */
1444
+ bottomContainer?: HTMLElement;
1445
+ };
1446
+ /**
1447
+ Object that describes an active panel.
1448
+ */
1449
+ interface Panel {
1450
+ /**
1451
+ The element representing this panel. The library will add the
1452
+ `"wg-panel"` DOM class to this.
1453
+ */
1454
+ dom: HTMLElement;
1455
+ /**
1456
+ Controls whether the panel should be at the top or bottom of the
1457
+ editor. Defaults to false.
1458
+ */
1459
+ top?: boolean;
1460
+ /**
1461
+ Update the panel DOM for a given editor update.
1462
+ */
1463
+ update?(update: Wordgard.Update): void;
1464
+ /**
1465
+ Called, when present, when the panel has been added the DOM.
1466
+ */
1467
+ connect?(wg: Wordgard): void;
1468
+ /**
1469
+ Called when the editor with the panel is disconnected from the
1470
+ DOM, or the panel is removed from an editor.
1471
+ */
1472
+ disconnect?(wg: Wordgard): void;
1473
+ /**
1474
+ Called when the panel is removed from the editor.
1475
+ */
1476
+ remove?(wg: Wordgard): void;
1477
+ }
1478
+ declare namespace Panel {
1479
+ /**
1480
+ A function that initializes a panel. Used in {@link Panel.show}.
1481
+ */
1482
+ type Constructor = (wg: Wordgard) => Panel;
1483
+ /**
1484
+ Opening a panel is done by providing a constructor function for
1485
+ the panel through this facet. (The panel is closed again when its
1486
+ constructor is no longer provided.) Values of `null` are ignored.
1487
+ */
1488
+ const show: GardState.Facet<Constructor | null, readonly (Constructor | null)[]>;
1489
+ /**
1490
+ Get the active panel created by the given constructor, if any.
1491
+ This can be useful when you need access to your panels' DOM
1492
+ structure.
1493
+ */
1494
+ function get<T extends Panel>(wg: Wordgard, constructor: (wg: Wordgard) => T): T | null;
1495
+ /**
1496
+ Configures the panel-managing extension.
1497
+ */
1498
+ function configure(config?: PanelConfig): GardState.Extension;
1499
+ }
1500
+
1501
+ /**
1502
+ Provides a menu bar that displays menu items defined via the
1503
+ {@link command.Menu menu system} in a button bar at the top of the
1504
+ editor. The same menu items can be used by custom menu
1505
+ implementations, but this extension provides a solid default menu
1506
+ style.
1507
+ */
1508
+ declare function menuBar(config?: {
1509
+ template?: Menu.Template | readonly Menu.Template[];
1510
+ }): GardState.Extension;
1511
+
1512
+ /**
1513
+ Dialogs are {@link Panel panels} opened as a side-effect, and
1514
+ closed by user action. This interface is used to describe them.
1515
+ */
1516
+ interface Dialog {
1517
+ /**
1518
+ A function to render the content of the dialog. The result
1519
+ should contain at least one `<form>` element. Submit handlers
1520
+ and a handler for the Escape key will be added to the form.
1521
+
1522
+ If this is not given, the `label`, `input`, and `submitLabel`
1523
+ fields will be used to create a simple form for you.
1524
+ */
1525
+ content?: (wg: Wordgard, close: () => void) => Element;
1526
+ /**
1527
+ When `content` isn't given, this provides the text shown in the
1528
+ dialog.
1529
+ */
1530
+ label?: string;
1531
+ /**
1532
+ The attributes for an input element shown next to the label. If
1533
+ not given, no input element is added.
1534
+ */
1535
+ input?: {
1536
+ [attr: string]: string;
1537
+ };
1538
+ /**
1539
+ The label for the button that submits the form. Defaults to
1540
+ `"OK"`.
1541
+ */
1542
+ submitLabel?: string;
1543
+ /**
1544
+ Extra classes to add to the panel.
1545
+ */
1546
+ class?: string;
1547
+ /**
1548
+ A query selector to find the field that should be focused when
1549
+ the dialog is opened. When set to true, this picks the first
1550
+ `<input>` or `<button>` element in the form. When set to
1551
+ `false`, focus is not moved into the dialog.
1552
+ */
1553
+ focus?: string | boolean;
1554
+ /**
1555
+ By default, dialogs are shown above the editor. Set this to
1556
+ `false` to have it show up at the bottom.
1557
+ */
1558
+ top?: boolean;
1559
+ }
1560
+ declare namespace Dialog {
1561
+ /**
1562
+ Show a dialog to display a message or prompt the user for input.
1563
+ Returns an effect that can be dispatched to close the dialog,
1564
+ and a promise that resolves when the dialog is closed or a form
1565
+ inside of it is submitted.
1566
+
1567
+ You are encouraged, if your handling of the result of the promise
1568
+ dispatches a transaction, to include the `close` effect in it. If
1569
+ you don't, this function will automatically dispatch a separate
1570
+ transaction right after.
1571
+ */
1572
+ function show(wg: Wordgard, config: Dialog): {
1573
+ close: Transaction.Effect<unknown>;
1574
+ result: Promise<HTMLFormElement | null>;
1575
+ };
1576
+ /**
1577
+ Find the {@link Panel} for an open dialog, using a class name as
1578
+ identifier.
1579
+ */
1580
+ function get(wg: Wordgard, className: string): Panel | null;
1581
+ /**
1582
+ Close the {@link Panel} for an open dialog, by class name.
1583
+ */
1584
+ function close(wg: Wordgard, className: string): boolean;
1585
+ }
1586
+
1587
+ /**
1588
+ Describes a tooltip. Values of this type, when provided through
1589
+ the {@link Tooltip.show} facet, provide the active tooltips on an
1590
+ editor.
1591
+ */
1592
+ interface Tooltip {
1593
+ /**
1594
+ The document position at which to show the tooltip.
1595
+ */
1596
+ pos: number;
1597
+ /**
1598
+ The end of the range annotated by this tooltip, if different
1599
+ from `pos`.
1600
+ */
1601
+ end?: number;
1602
+ /**
1603
+ A constructor function that creates the tooltip's {@link
1604
+ Tooltip.View DOM representation}.
1605
+ */
1606
+ create(wg: Wordgard): Tooltip.View;
1607
+ /**
1608
+ Whether the tooltip should be shown above or below the target
1609
+ position. Not guaranteed to be respected for hover tooltips
1610
+ since all hover tooltips for the same range are always
1611
+ positioned together. Defaults to false.
1612
+ */
1613
+ above?: boolean;
1614
+ /**
1615
+ Whether the `above` option should be honored when there isn't
1616
+ enough space on that side to show the tooltip inside the
1617
+ viewport. Defaults to false.
1618
+ */
1619
+ strictSide?: boolean;
1620
+ /**
1621
+ When set to true, show a triangle connecting the tooltip element
1622
+ to position `pos`.
1623
+ */
1624
+ arrow?: boolean;
1625
+ /**
1626
+ By default, tooltips are hidden when their position is outside
1627
+ of the visible editor content. Set this to false to turn that
1628
+ off.
1629
+ */
1630
+ clip?: boolean;
1631
+ }
1632
+ declare namespace Tooltip {
1633
+ /**
1634
+ Creates an extension that configures tooltip behavior.
1635
+ */
1636
+ function configure(config?: {
1637
+ /**
1638
+ By default, tooltips use `"fixed"`
1639
+ [positioning](https://developer.mozilla.org/en-US/docs/Web/CSS/position),
1640
+ which has the advantage that tooltips don't get cut off by
1641
+ scrollable parent elements. However, CSS rules like `contain:
1642
+ layout` can break fixed positioning in child nodes, which can be
1643
+ worked about by using `"absolute"` here.
1644
+
1645
+ On iOS, which at the time of writing still doesn't properly
1646
+ support fixed positioning, the library always uses absolute
1647
+ positioning.
1648
+
1649
+ If the tooltip parent element sits in a transformed element, the
1650
+ library also falls back to absolute positioning.
1651
+ */
1652
+ position?: "fixed" | "absolute";
1653
+ /**
1654
+ The element to put the tooltips into. By default, they are put
1655
+ in the editor (`<wordgard-editor>`) element, and that is
1656
+ usually what you want. But in some layouts that can lead to
1657
+ positioning issues, and you need to use a different parent to
1658
+ work around those.
1659
+ */
1660
+ parent?: HTMLElement;
1661
+ /**
1662
+ By default, when figuring out whether there is room for a
1663
+ tooltip at a given position, the extension considers the entire
1664
+ space between 0,0 and
1665
+ `documentElement.clientWidth`/`clientHeight` to be available for
1666
+ showing tooltips. You can provide a function here that returns
1667
+ an alternative rectangle.
1668
+ */
1669
+ tooltipSpace?: (wg: Wordgard) => DOMRect;
1670
+ }): GardState.Extension;
1671
+ /**
1672
+ Describes the way a tooltip is displayed.
1673
+ */
1674
+ interface View {
1675
+ /**
1676
+ The DOM element to position over the editor.
1677
+ */
1678
+ dom: HTMLElement;
1679
+ /**
1680
+ Adjust the position of the tooltip relative to its anchor
1681
+ position. A positive `x` value will move the tooltip
1682
+ horizontally along with the text direction (so right in
1683
+ left-to-right context, left in right-to-left). A positive `y`
1684
+ will move the tooltip up when it is above its anchor, and down
1685
+ otherwise.
1686
+ */
1687
+ offset?: {
1688
+ x: number;
1689
+ y: number;
1690
+ };
1691
+ /**
1692
+ By default, a tooltip's screen position will be based on the
1693
+ document position of its `pos` property. This method can be
1694
+ provided to make the tooltip view itself responsible for finding
1695
+ its screen position.
1696
+ */
1697
+ getCoords?: (pos: number) => DOMRect;
1698
+ /**
1699
+ By default, tooltips are moved when they overlap with other
1700
+ tooltips. Set this to `true` to disable that behavior for this
1701
+ tooltip.
1702
+ */
1703
+ overlap?: boolean;
1704
+ /**
1705
+ Update the DOM element for a change in the view's state.
1706
+ */
1707
+ update?(update: Wordgard.Update): void;
1708
+ /**
1709
+ Called when the tooltip is added to a DOM-connected editor.
1710
+ */
1711
+ connect?(wg: Wordgard): void;
1712
+ /**
1713
+ Called when the editor containing the tooltip is disconnected,
1714
+ or before the tooltip is removed.
1715
+ */
1716
+ disconnect?(wg: Wordgard): void;
1717
+ /**
1718
+ Called when the tooltip is removed from the editor.
1719
+ */
1720
+ remove?(wg: Wordgard): void;
1721
+ /**
1722
+ Called when the tooltip has been (re)positioned. The argument
1723
+ is the {@link Tooltip.configure.config.tooltipSpace space}
1724
+ available to the tooltip.
1725
+ */
1726
+ positioned?(space: DOMRect): void;
1727
+ /**
1728
+ By default, the library will restrict the size of tooltips so
1729
+ that they don't stick out of the available space. Set this to
1730
+ false to disable that.
1731
+ */
1732
+ resize?: boolean;
1733
+ }
1734
+ /**
1735
+ Facet to which an extension can add a value to show a tooltip.
1736
+ */
1737
+ const show: GardState.Facet<Tooltip | null, readonly (Tooltip | null)[]>;
1738
+ /**
1739
+ Get the active tooltip view for a given tooltip or tooltip
1740
+ constructor, if available.
1741
+ */
1742
+ function get<T extends Tooltip>(wg: Wordgard, tooltip: T): ReturnType<T["create"]> | null;
1743
+ function get<T extends Tooltip.View>(wg: Wordgard, create: (wg: Wordgard) => T): T | null;
1744
+ /**
1745
+ Tell the tooltip extension to recompute the position of the active
1746
+ tooltips. This can be useful when something happens (such as a
1747
+ re-positioning or CSS change affecting the editor) that could
1748
+ invalidate the existing tooltip positions but isn't detected by
1749
+ the extension.
1750
+ */
1751
+ function reposition(wg: Wordgard): void;
1752
+ /**
1753
+ Set up a hover tooltip, which shows up when the pointer hovers
1754
+ over ranges of text. The callback is called when the mouse hovers
1755
+ over the document text. It should, if there is a tooltip
1756
+ associated with position `pos`, return the tooltip description
1757
+ (either directly or in a promise). The `side` argument indicates
1758
+ on which side of the position the pointer is—it will be -1 if the
1759
+ pointer is before the position, 1 if after the position.
1760
+
1761
+ Note that all hover tooltips are hosted within a single tooltip
1762
+ container element. This allows multiple tooltips over the same
1763
+ range to be "merged" together without overlapping.
1764
+
1765
+ Returns an {@link GardState.Extension editor extension} that
1766
+ installs the hover behavior and a state field that can be used
1767
+ to read the currently active tooltips produced by this
1768
+ extension.
1769
+ */
1770
+ function hover(source: HoverTooltipSource, options?: hover.Spec): {
1771
+ extension: GardState.Extension;
1772
+ active: GardState.Field<readonly Tooltip[]>;
1773
+ };
1774
+ namespace hover {
1775
+ /**
1776
+ Options given to {@link Tooltip.hover}.
1777
+ */
1778
+ type Spec = {
1779
+ /**
1780
+ Controls whether a transaction hides the tooltip. The default
1781
+ is to not hide.
1782
+ */
1783
+ hideOn?: (tr: Transaction, tooltip: Tooltip) => boolean;
1784
+ /**
1785
+ When enabled (this defaults to false), close the tooltip
1786
+ whenever the document changes or the selection is set.
1787
+ */
1788
+ hideOnChange?: boolean | "touch";
1789
+ /**
1790
+ Hover time after which the tooltip should appear, in
1791
+ milliseconds. Defaults to 300ms.
1792
+ */
1793
+ hoverTime?: number;
1794
+ };
1795
+ /**
1796
+ Returns true if any hover tooltips are currently active.
1797
+ */
1798
+ function has(state: GardState): boolean;
1799
+ /**
1800
+ Transaction effect that closes all hover tooltips.
1801
+ */
1802
+ const closeAll: Transaction.Effect<null>;
1803
+ }
1804
+ }
1805
+ /**
1806
+ The type of function that can be used as a {@hoverTooltip.source
1807
+ hover tooltip source}.
1808
+ */
1809
+ type HoverTooltipSource = (wg: Wordgard, pos: number, side: -1 | 1) => Tooltip | readonly Tooltip[] | null | Promise<Tooltip | readonly Tooltip[] | null>;
1810
+
1811
+ /**
1812
+ Objects of this type represent input rules.
1813
+ */
1814
+ declare class InputRule {
1815
+ /**
1816
+ Rules can be added to a configuration as extension values.
1817
+ */
1818
+ extension: GardState.Extension;
1819
+ private constructor();
1820
+ /**
1821
+ Define an input rule.
1822
+ */
1823
+ static define(spec: InputRule.Spec): InputRule;
1824
+ /**
1825
+ Build an input rule for automatically wrapping a textblock when
1826
+ a given string is typed. You'll probably want the regexp to
1827
+ start with `^`, so that the pattern can only occur at the start
1828
+ of a textblock. `tag` gives the type of plot to wrap in.
1829
+
1830
+ When `empty` is given as `true`, the rule only applies when the
1831
+ expression matches the textblock's entire content.
1832
+ */
1833
+ static wrapping(expr: RegExp, tag: Plot.Tag | ((match: InputRule.MatchArray) => Plot.Tag), empty?: boolean): InputRule;
1834
+ /**
1835
+ Build an input rule that changes the type of a textblock when the
1836
+ matched text is typed into it. You'll usually want to start your
1837
+ regexp with `^` so that it is only matched at the start of a
1838
+ textblock. The optional `getAttrs` parameter can be used to compute
1839
+ the new node's attributes, and works the same as in the
1840
+ `InputRule.wrapping` function.
1841
+ */
1842
+ static textblockType(expr: RegExp, tag: Plot.Tag | ((match: InputRule.MatchArray) => Plot.Tag), empty?: boolean): InputRule;
1843
+ }
1844
+ declare namespace InputRule {
1845
+ /**
1846
+ Configuration given to {@link InputRule.define}.
1847
+ */
1848
+ interface Spec {
1849
+ /**
1850
+ The regular expression to match against the text before the
1851
+ input. This expression should end in a `$` marker.
1852
+ */
1853
+ expr: RegExp;
1854
+ /**
1855
+ Handler to call when this rule matches. `match` will contain
1856
+ the document positions of the full match and all matched
1857
+ groups in `expr`. Should return `true` when it has taken an
1858
+ action, `false` when it didn't. You probably want to include
1859
+ {@link history.history.isolate}`.of(true)` in any
1860
+ transactions you dispatch from a rule handler, so that users
1861
+ can undo the adjustment if it wasn't what they wanted.
1862
+
1863
+ When given as a string, the full match will be replaced by
1864
+ that string.
1865
+ */
1866
+ apply: ((state: GardState, match: InputRule.MatchArray) => Transaction.Spec | null) | string;
1867
+ /**
1868
+ Because the regular expression given in `expr` must end at the
1869
+ cursor, it is matched against a string that stops at the
1870
+ cursor, and cannot look beyond it. You can provide an
1871
+ additional expression here (which should start with `^`) to
1872
+ enforce a lookahead condition.
1873
+ */
1874
+ lookahead?: RegExp;
1875
+ /**
1876
+ By default, input rules don't apply inside nodes with the
1877
+ {@link Node.Role.Code `Code` role}. Set this to `true` to
1878
+ allow matches in code.
1879
+ */
1880
+ inCode?: boolean;
1881
+ }
1882
+ /**
1883
+ An object representing a matched group for an input rule. Holds
1884
+ the start and end positions of the group in the document, along
1885
+ with the matched text content.
1886
+ */
1887
+ type Match = {
1888
+ from: Pos;
1889
+ to: Pos;
1890
+ text: string;
1891
+ };
1892
+ /**
1893
+ An array of {@link InputRule.Match matches}.
1894
+ */
1895
+ type MatchArray = readonly (InputRule.Match | null)[] & {
1896
+ 0: InputRule.Match;
1897
+ };
1898
+ /**
1899
+ Input rule that converts double dashes to an emdash.
1900
+ */
1901
+ const emDash: InputRule;
1902
+ /**
1903
+ Rule that converts three dots to an ellipsis character.
1904
+ */
1905
+ const ellipsis: InputRule;
1906
+ /**
1907
+ “Smart” opening double quotes.
1908
+ */
1909
+ const openDoubleQuote: InputRule;
1910
+ /**
1911
+ “Smart” closing double quotes.
1912
+ */
1913
+ const closeDoubleQuote: InputRule;
1914
+ /**
1915
+ ‘Smart’ opening single quotes.
1916
+ */
1917
+ const openSingleQuote: InputRule;
1918
+ /**
1919
+ ‘Smart’ closing single quotes.
1920
+ */
1921
+ const closeSingleQuote: InputRule;
1922
+ /**
1923
+ Smart-quote related input rules.
1924
+ */
1925
+ const smartQuotes: readonly InputRule[];
1926
+ }
1927
+
1928
+ /**
1929
+ Extension that enables a placeholder—a piece of example content
1930
+ to show when the editor is empty.
1931
+ */
1932
+ declare function placeholder(content: string | (() => Element)): GardState.Extension;
1933
+
1934
+ /**
1935
+ Draws a cursor at the current drop position when something is
1936
+ being dragged over the editor.
1937
+ */
1938
+ declare function dropCursor(): GardState.Extension;
1939
+
1940
+ export { Decoration, Dialog, InputRule, KeyBinding, Panel, PointSet, RangeSet, Tooltip, Widget, Wordgard, dropCursor, menuBar, placeholder };