@nerd-bible/wordgard 0.5.3-beta7 → 0.6.2-beta10

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/dist/set.js CHANGED
@@ -99,6 +99,7 @@ class HeapCursor {
99
99
  heapSink(this.heap, this.heap.length - 1, this.cmp);
100
100
  }
101
101
  }
102
+ this.fill();
102
103
  }
103
104
  next() {
104
105
  if (this.heap.length) {
@@ -163,7 +164,11 @@ let SetBuilder$1 = class SetBuilder {
163
164
  addRange(value, from, to) {
164
165
  if (from >= to)
165
166
  throw new Error("Ranges cannot be empty");
166
- (this.lastTo <= from ? this : this.next).addInner(value, from, to);
167
+ for (let target = this;; target = target.next)
168
+ if (target.lastTo <= from) {
169
+ target.addInner(value, from, to);
170
+ break;
171
+ }
167
172
  }
168
173
  addInner(value, from, to) {
169
174
  let chunk;
@@ -245,13 +250,13 @@ class RangeSet extends Set {
245
250
  let replI = 0, posA = 0, posB = 0;
246
251
  let build = new SetBuilder$1();
247
252
  for (let i = 0; i < sections.length;) {
248
- let len = sections[i++], ins = sections[i++];
253
+ let len = sections[i++], ins = sections[i++], endA = posA + len;
249
254
  if (ins < 0) {
250
255
  while (i < sections.length && sections[i + 1] < 0) {
251
256
  len += sections[i];
252
257
  i += 2;
253
258
  }
254
- let upto = i == sections.length ? 1e9 : posA + len, off = posB - posA;
259
+ let upto = i == sections.length ? 1e9 : endA, off = posB - posA;
255
260
  while (cursor.cur) {
256
261
  let chunk = cursor.cur;
257
262
  if (cursor.i == 0 && chunk.end < upto) {
@@ -276,12 +281,12 @@ class RangeSet extends Set {
276
281
  if (repl.add)
277
282
  build.add(repl.add);
278
283
  }
279
- if (len)
280
- cursor.goto(posA + len - 1);
281
- copyMappedUpto$1(cursor, posA + len, map, build, replace, replStartI);
284
+ if (len && cursor.to < endA)
285
+ cursor.goto(endA - 1);
286
+ copyMappedUpto$1(cursor, endA, map, build, replace, replStartI);
282
287
  posB = endB;
283
288
  }
284
- posA += len;
289
+ posA = endA;
285
290
  }
286
291
  return build.finish(this.next && this.next.mapInner(sections, map, replace));
287
292
  }
@@ -555,15 +560,16 @@ class PointSet {
555
560
  cursors.push(set.cursor(from, side));
556
561
  return cursors.length == 0 ? PointSet.empty.cursor() : cursors.length == 1 ? cursors[0] : new HeapCursor(cmpCursor, cursors);
557
562
  }
558
- at(pos) {
563
+ at(pos, pred) {
559
564
  for (let chunk of this.chunks) {
560
- if (chunk.end > pos)
561
- break;
562
565
  if (chunk.start > pos)
566
+ break;
567
+ if (chunk.end < pos)
563
568
  continue;
564
- let index = findAbove(chunk.pos, 0, pos - 1);
565
- if (index < chunk.pos.length && chunk.pos[index] == pos)
566
- return chunk.value[index];
569
+ for (let index = findAbove(chunk.pos, 0, pos - chunk.start - 1); index < chunk.pos.length && chunk.pos[index] + chunk.start == pos; index++) {
570
+ if (!pred || pred(chunk.value[index]))
571
+ return chunk.value[index];
572
+ }
567
573
  }
568
574
  return undefined;
569
575
  }
package/dist/state.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ChangeSet, Node, Pos, Plot, Mark, Schema } from 'wordgard/doc';
1
+ import { Plot, Slice, ChangeSet, Node, Pos, Mark, Schema } from 'wordgard/doc';
2
2
 
3
3
  type wgNode = Node;
4
4
  /**
@@ -87,6 +87,12 @@ declare abstract class GardSelection {
87
87
  anchorSide: -1 | 1;
88
88
  };
89
89
  /**
90
+ Create a content slice and context from this selection. The
91
+ default implementation concatenates the slices for the
92
+ selection's `ranges`.
93
+ */
94
+ toSlice(doc: Plot.Doc): Slice;
95
+ /**
90
96
  The side that the selection head is associated with. -1 means it
91
97
  is after the element before its position, 1 means it is before
92
98
  the element after its position. This influences where the
@@ -144,6 +150,10 @@ declare abstract class GardSelection {
144
150
  */
145
151
  static range(anchor: number, head?: number, headSide?: -1 | 1, goalColumn?: number): GardSelection.Text;
146
152
  /**
153
+ Create a text selection.
154
+ */
155
+ static undirectionalRange(from: number, to: number): GardSelection.Text;
156
+ /**
147
157
  Create a node selection.
148
158
  */
149
159
  static node(pos: number, node: wgNode, goalColumn?: number): GardSelection.Node;
@@ -196,6 +206,13 @@ declare namespace GardSelection {
196
206
  class Text extends GardSelection {
197
207
  private _headSide;
198
208
  /**
209
+ When `false`, indicates that this selection may swap `head`
210
+ and `anchor` when extended. Used for things like selections
211
+ created by double-clicking, which don't have a clearly
212
+ defined anchor.
213
+ */
214
+ readonly directional: boolean;
215
+ /**
199
216
  A set of active marks that should be applied to content
200
217
  inserted at this selection (replacing the contextual marks).
201
218
  Used mostly for making the effect of toggling inline styles
@@ -248,6 +265,11 @@ declare namespace GardSelection {
248
265
  used for things like toggling emphasis on a cursor selection.
249
266
  */
250
267
  marks?: Mark.Set;
268
+ /**
269
+ Set to `false` to indicate that this selection isn't {@link
270
+ GardSelection.Text.directional directional}.
271
+ */
272
+ directional?: boolean;
251
273
  };
252
274
  /**
253
275
  The representation of a text selection when serialized to JSON.
@@ -256,6 +278,7 @@ declare namespace GardSelection {
256
278
  anchor: number;
257
279
  head?: number;
258
280
  side?: -1 | 1;
281
+ directional?: boolean;
259
282
  marks?: Record<string, any>;
260
283
  };
261
284
  }
@@ -375,6 +398,10 @@ declare class Transaction {
375
398
  */
376
399
  readonly effects: readonly Transaction.Effect<any>[];
377
400
  /**
401
+ The annotations on this transaction.
402
+ */
403
+ readonly annotations: readonly Transaction.Annotation<any>[];
404
+ /**
378
405
  Whether the selection should be scrolled into view after this
379
406
  transaction is dispatched.
380
407
  */
@@ -469,7 +496,7 @@ declare namespace Transaction {
469
496
  scrollIntoView?: boolean;
470
497
  /**
471
498
  Only meaningful for specs that are combined with another
472
- transaction spec (via {@link Transaction.merge or by being
499
+ transaction spec (via {@link Transaction.merge} or by being
473
500
  returned from an {@link Transaction.extender extender}.
474
501
  Normally, when specs are combined, the positions in `changes`
475
502
  are taken to refer to the document positions in the initial
@@ -546,7 +573,7 @@ declare namespace Transaction {
546
573
  /**
547
574
  Define a new type of annotation.
548
575
  */
549
- static define<T>(): Annotation.Type<T>;
576
+ static define<T>(name?: string): Annotation.Type<T>;
550
577
  private _isAnnotation;
551
578
  }
552
579
  namespace Annotation {
@@ -555,13 +582,17 @@ declare namespace Transaction {
555
582
  annotation}.
556
583
  */
557
584
  class Type<T> {
585
+ /**
586
+ This type's debug name.
587
+ */
588
+ readonly name: string | null;
589
+ private constructor();
558
590
  /**
559
591
  Create an instance of this annotation.
560
592
  */
561
593
  of(value: T): Transaction.Annotation<T>;
562
594
  }
563
595
  }
564
- const foo: number;
565
596
  /**
566
597
  Annotation used to store transaction timestamps. Automatically
567
598
  added to every transaction, holding `Date.now()`.
@@ -665,6 +696,11 @@ declare namespace Transaction {
665
696
  */
666
697
  readonly map: (value: any, mapping: ChangeSet) => any | undefined;
667
698
  /**
699
+ The debug name of this effect.
700
+ */
701
+ readonly name: string | null;
702
+ private constructor();
703
+ /**
668
704
  Create an {@link Transaction.Effect effect} instance of this
669
705
  type.
670
706
  */
@@ -674,6 +710,10 @@ declare namespace Transaction {
674
710
  Options passed when defining an effect.
675
711
  */
676
712
  interface Spec<Value> {
713
+ /**
714
+ An optional debug name to assign to this effect.
715
+ */
716
+ name?: string;
677
717
  /**
678
718
  Provides a way to map an effect like this through a position
679
719
  mapping. When not given, the effects will simply not be mapped.
@@ -771,15 +811,6 @@ declare class TextblockMap {
771
811
  Get the document position for a given string index.
772
812
  */
773
813
  fromIndex(index: number): number;
774
- /**
775
- Get the position at the start or end of the textblock. Note that
776
- in bidirectional text this may not be the actual start or end
777
- position of the node.
778
- */
779
- visualSide(start: boolean): {
780
- pos: number;
781
- side: -1 | 1;
782
- };
783
814
  }
784
815
 
785
816
  type DocSource = Plot.Doc | HTMLElement | DocumentFragment | string | Node.JSON | ((schema: Schema) => Plot.Doc);
@@ -834,12 +865,16 @@ declare class GardState {
834
865
  */
835
866
  get sel(): GardSelection.Resolved;
836
867
  /**
837
- Retrieve the value of a {@link GardState.Field state field}.
838
- Throws an error when the state doesn't have that field, unless
839
- you pass `false` as second parameter.
868
+ Retrieve the value of a {@link GardState.Field state field} or
869
+ {@link GardState.Field.Derived derived field}. Throws an error
870
+ when the state doesn't have that field, unless you pass `false`
871
+ as second parameter, in which case you'll get `undefined` for a
872
+ regular field, or the value will be eagerly computed for a
873
+ derived field.
840
874
  */
841
- field<T>(field: GardState.Field<T>): T;
875
+ field<T>(field: GardState.Field<T> | GardState.Field.Derived<T>): T;
842
876
  field<T>(field: GardState.Field<T>, require: false): T | undefined;
877
+ field<T>(field: GardState.Field.Derived<T>, require: false): T;
843
878
  /**
844
879
  Get the value of a state {@link GardState.Facet facet}.
845
880
  */
@@ -895,9 +930,9 @@ declare class GardState {
895
930
  isAtom(type: Node.Type): boolean;
896
931
  /**
897
932
  Return the extent of the word around the given position, as a
898
- text selection.
933
+ text selection, or null if there is no word there.
899
934
  */
900
- wordAt(pos: number, bias?: -1 | 1): GardSelection.Text;
935
+ wordAt(pos: number, bias?: -1 | 1): GardSelection.Text | null;
901
936
  /**
902
937
  This effect can be used to reconfigure the root extensions of
903
938
  the editor. Doing this will discard any extensions {@link
@@ -958,6 +993,10 @@ declare namespace GardState {
958
993
  Define a state field.
959
994
  */
960
995
  static define<Value>(config: GardState.Field.Spec<Value>): GardState.Field<Value>;
996
+ /**
997
+ The field's debugging name, if given.
998
+ */
999
+ name: string | null;
961
1000
  private create;
962
1001
  /**
963
1002
  State field instances can be used as {@link
@@ -977,6 +1016,11 @@ declare namespace GardState {
977
1016
  The options passed when defining a state field.
978
1017
  */
979
1018
  type Spec<Value> = {
1019
+ /**
1020
+ An optional name to associate with this field for debugging
1021
+ purposes.
1022
+ */
1023
+ name?: string;
980
1024
  /**
981
1025
  Creates the initial value for the field when a state is created.
982
1026
  */
@@ -1016,6 +1060,42 @@ declare namespace GardState {
1016
1060
  */
1017
1061
  fromJSON?: (json: any, state: GardState) => Value;
1018
1062
  };
1063
+ /**
1064
+ Define a field-like state slot that is computed from other
1065
+ slots in the state. Automatically tracks dependencies and only
1066
+ recomputes when one of the inputs accessed by the given
1067
+ function changes. Use {@link GardState.field} to read the
1068
+ value of such a field.
1069
+ */
1070
+ class Derived<Value> {
1071
+ private compareF;
1072
+ /**
1073
+ This field's debugging name, if given.
1074
+ */
1075
+ readonly name: string | null;
1076
+ private constructor();
1077
+ /**
1078
+ Define a derived field.
1079
+ */
1080
+ static define<Value>(compute: (state: GardState) => Value, spec?: {
1081
+ /**
1082
+ An optional debugging name for this field.
1083
+ */
1084
+ name?: string;
1085
+ /**
1086
+ Optional function used to compare the value of the field,
1087
+ to prevent propagating a change when a recomputed result
1088
+ is the same as the old value. Should return `true` when
1089
+ the inputs can be considered the same. Defaults to `===`.
1090
+ */
1091
+ compare?: (a: Value, b: Value) => boolean;
1092
+ }): Derived<Value>;
1093
+ /**
1094
+ Derived fields must be included in your state configuration
1095
+ for them to be used.
1096
+ */
1097
+ get extension(): GardState.Extension;
1098
+ }
1019
1099
  }
1020
1100
  /**
1021
1101
  A facet is a labeled value that is associated with an editor
@@ -1042,6 +1122,10 @@ declare namespace GardState {
1042
1122
  */
1043
1123
  readonly isStatic: boolean;
1044
1124
  /**
1125
+ The facet's debug name.
1126
+ */
1127
+ readonly name: string | null;
1128
+ /**
1045
1129
  The output of the facet when it has no inputs.
1046
1130
  */
1047
1131
  readonly default: Output;
@@ -1061,7 +1145,7 @@ declare namespace GardState {
1061
1145
  */
1062
1146
  of(value: Input): GardState.Extension;
1063
1147
  /**
1064
- Create an extension that computes a value for the facet from a
1148
+ Create an extension that derives a value for the facet from a
1065
1149
  state. The given function should only depend on the state, not
1066
1150
  any external non-constant inputs. Its return value will be kept
1067
1151
  on state update, unless any of the fields or facets (including
@@ -1072,21 +1156,20 @@ declare namespace GardState {
1072
1156
  can use the {@link GardState.Facet.from `from`} method
1073
1157
  instead.
1074
1158
  */
1075
- compute(get: (state: GardState) => Input): GardState.Extension;
1159
+ derive(get: (state: GardState) => Input): GardState.Extension;
1076
1160
  /**
1077
- Create an extension that computes zero or more values for this
1161
+ Create an extension that derives zero or more values for this
1078
1162
  facet from a state.
1079
1163
  */
1080
- computeN(get: (state: GardState) => readonly Input[]): GardState.Extension;
1164
+ deriveN(get: (state: GardState) => readonly Input[]): GardState.Extension;
1081
1165
  /**
1082
1166
  Shorthand method for registering a facet source with a state
1083
1167
  field as input. If the field's type corresponds to this facet's
1084
1168
  input type, the getter function can be omitted. If given, it
1085
1169
  will be used to produce the input from the field value.
1086
1170
  */
1087
- from<T extends Input>(field: GardState.Field<T>): GardState.Extension;
1088
- from<T>(field: GardState.Field<T>, get: (value: T) => Input): GardState.Extension;
1089
- tag: Output;
1171
+ from<T extends Input>(field: GardState.Field<T> | GardState.Field.Derived<T>): GardState.Extension;
1172
+ from<T>(field: GardState.Field<T> | GardState.Field.Derived<T>, get: (value: T) => Input): GardState.Extension;
1090
1173
  }
1091
1174
  namespace Facet {
1092
1175
  /**
@@ -1094,6 +1177,10 @@ declare namespace GardState {
1094
1177
  facet.
1095
1178
  */
1096
1179
  type Spec<Input, Output> = {
1180
+ /**
1181
+ And optional name, to recognize the facet when debugging.
1182
+ */
1183
+ name?: string;
1097
1184
  /**
1098
1185
  How to combine the input values into a single output value. When
1099
1186
  not given, the array of input values becomes the output. This
@@ -1136,7 +1223,7 @@ declare namespace GardState {
1136
1223
  /**
1137
1224
  A facet reader can be used to fetch the value of a facet,
1138
1225
  through {@link GardState.facet} or as a dependency in {@link
1139
- GardState.Facet.compute `Facet.compute`}, but not to define
1226
+ GardState.Facet.derive `Facet.derive`}, but not to define
1140
1227
  new values for the facet.
1141
1228
  */
1142
1229
  type Reader<Output> = {
@@ -1240,11 +1327,15 @@ declare namespace GardState {
1240
1327
  transaction.
1241
1328
  */
1242
1329
  class Compartment {
1330
+ /**
1331
+ The compartment's debugging name, if given.
1332
+ */
1333
+ readonly name: string | null;
1243
1334
  private constructor();
1244
1335
  /**
1245
1336
  Define a new compartment.
1246
1337
  */
1247
- static define(): Compartment;
1338
+ static define(name?: string): Compartment;
1248
1339
  /**
1249
1340
  Create an instance of this compartment to add to your {@link
1250
1341
  GardState.Spec.config state configuration}.
@@ -1314,7 +1405,7 @@ declare namespace GardState {
1314
1405
  */
1315
1406
  const visualCursorMotion: Facet<boolean, boolean>;
1316
1407
  /**
1317
- @hidden FIXME expose this?
1408
+ @hidden
1318
1409
  */
1319
1410
  const isAtom: Facet<[Plot.Type<unknown>, boolean], Map<Plot.Type<unknown>, boolean>>;
1320
1411
  }