@codemirror/state 6.2.0 → 6.3.0

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/index.d.ts CHANGED
@@ -85,6 +85,11 @@ declare abstract class Text implements Iterable<string> {
85
85
  */
86
86
  iterLines(from?: number, to?: number): TextIterator;
87
87
  /**
88
+ Return the document as a string, using newline characters to
89
+ separate lines.
90
+ */
91
+ toString(): string;
92
+ /**
88
93
  Convert the document to an array of lines (which can be
89
94
  deserialized again via [`Text.of`](https://codemirror.net/6/docs/ref/#state.Text^of)).
90
95
  */
@@ -252,7 +257,7 @@ plain object describing a change (a deletion, insertion, or
252
257
  replacement, depending on which fields are present), a [change
253
258
  set](https://codemirror.net/6/docs/ref/#state.ChangeSet), or an array of change specs.
254
259
  */
255
- declare type ChangeSpec = {
260
+ type ChangeSpec = {
256
261
  from: number;
257
262
  to?: number;
258
263
  insert?: string | Text;
@@ -479,7 +484,7 @@ declare class EditorSelection {
479
484
  static range(anchor: number, head: number, goalColumn?: number, bidiLevel?: number): SelectionRange;
480
485
  }
481
486
 
482
- declare type FacetConfig<Input, Output> = {
487
+ type FacetConfig<Input, Output> = {
483
488
  /**
484
489
  How to combine the input values into a single output value. When
485
490
  not given, the array of input values becomes the output. This
@@ -523,11 +528,19 @@ Examples of uses of facets are the [tab
523
528
  size](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize), [editor
524
529
  attributes](https://codemirror.net/6/docs/ref/#view.EditorView^editorAttributes), and [update
525
530
  listeners](https://codemirror.net/6/docs/ref/#view.EditorView^updateListener).
531
+
532
+ Note that `Facet` instances can be used anywhere where
533
+ [`FacetReader`](https://codemirror.net/6/docs/ref/#state.FacetReader) is expected.
526
534
  */
527
- declare class Facet<Input, Output = readonly Input[]> {
535
+ declare class Facet<Input, Output = readonly Input[]> implements FacetReader<Output> {
528
536
  private isStatic;
529
537
  private constructor();
530
538
  /**
539
+ Returns a facet reader for this facet, which can be used to
540
+ [read](https://codemirror.net/6/docs/ref/#state.EditorState.facet) it but not to define values for it.
541
+ */
542
+ get reader(): FacetReader<Output>;
543
+ /**
531
544
  Define a new facet.
532
545
  */
533
546
  static define<Input, Output = readonly Input[]>(config?: FacetConfig<Input, Output>): Facet<Input, Output>;
@@ -558,9 +571,24 @@ declare class Facet<Input, Output = readonly Input[]> {
558
571
  */
559
572
  from<T extends Input>(field: StateField<T>): Extension;
560
573
  from<T>(field: StateField<T>, get: (value: T) => Input): Extension;
574
+ tag: typeof FacetTag;
561
575
  }
562
- declare type Slot<T> = Facet<any, T> | StateField<T> | "doc" | "selection";
563
- declare type StateFieldSpec<Value> = {
576
+ declare const FacetTag: unique symbol;
577
+ /**
578
+ A facet reader can be used to fetch the value of a facet, though
579
+ [`EditorState.facet`](https://codemirror.net/6/docs/ref/#state.EditorState.facet) or as a dependency
580
+ in [`Facet.compute`](https://codemirror.net/6/docs/ref/#state.Facet.compute), but not to define new
581
+ values for the facet.
582
+ */
583
+ type FacetReader<Output> = {
584
+ /**
585
+ Dummy tag that makes sure TypeScript doesn't consider all object
586
+ types as conforming to this type.
587
+ */
588
+ tag: typeof FacetTag;
589
+ };
590
+ type Slot<T> = FacetReader<T> | StateField<T> | "doc" | "selection";
591
+ type StateFieldSpec<Value> = {
564
592
  /**
565
593
  Creates the initial value for the field when a state is created.
566
594
  */
@@ -635,7 +663,7 @@ providers](https://codemirror.net/6/docs/ref/#state.Facet.of), or objects with a
635
663
  `extension` property. Extensions can be nested in arrays
636
664
  arbitrarily deep—they will be flattened when processed.
637
665
  */
638
- declare type Extension = {
666
+ type Extension = {
639
667
  extension: Extension;
640
668
  } | readonly Extension[];
641
669
  /**
@@ -780,7 +808,10 @@ declare class StateEffect<Value> {
780
808
  is<T>(type: StateEffectType<T>): this is StateEffect<T>;
781
809
  /**
782
810
  Define a new effect type. The type parameter indicates the type
783
- of values that his effect holds.
811
+ of values that his effect holds. It should be a type that
812
+ doesn't include `undefined`, since that is used in
813
+ [mapping](https://codemirror.net/6/docs/ref/#state.StateEffect.map) to indicate that an effect is
814
+ removed.
784
815
  */
785
816
  static define<Value = null>(spec?: StateEffectSpec<Value>): StateEffectType<Value>;
786
817
  /**
@@ -1115,7 +1146,7 @@ declare class EditorState {
1115
1146
  /**
1116
1147
  Get the value of a state [facet](https://codemirror.net/6/docs/ref/#state.Facet).
1117
1148
  */
1118
- facet<Output>(facet: Facet<any, Output>): Output;
1149
+ facet<Output>(facet: FacetReader<Output>): Output;
1119
1150
  /**
1120
1151
  Convert this state to a JSON-serializable object. When custom
1121
1152
  fields should be serialized, you can pass them in as an object
@@ -1317,7 +1348,7 @@ Subtype of [`Command`](https://codemirror.net/6/docs/ref/#view.Command) that doe
1317
1348
  to the actual editor view. Mostly useful to define commands that
1318
1349
  can be run and tested outside of a browser environment.
1319
1350
  */
1320
- declare type StateCommand = (target: {
1351
+ type StateCommand = (target: {
1321
1352
  state: EditorState;
1322
1353
  dispatch: (transaction: Transaction) => void;
1323
1354
  }) => boolean;
@@ -1459,7 +1490,7 @@ interface RangeCursor<T> {
1459
1490
  */
1460
1491
  to: number;
1461
1492
  }
1462
- declare type RangeSetUpdate<T extends RangeValue> = {
1493
+ type RangeSetUpdate<T extends RangeValue> = {
1463
1494
  /**
1464
1495
  An array of ranges to add. If given, this should be sorted by
1465
1496
  `from` position and `startSide` unless
@@ -1538,8 +1569,7 @@ declare class RangeSet<T extends RangeValue> {
1538
1569
  static compare<T extends RangeValue>(oldSets: readonly RangeSet<T>[], newSets: readonly RangeSet<T>[],
1539
1570
  /**
1540
1571
  This indicates how the underlying data changed between these
1541
- ranges, and is needed to synchronize the iteration. `from` and
1542
- `to` are coordinates in the _new_ space, after these changes.
1572
+ ranges, and is needed to synchronize the iteration.
1543
1573
  */
1544
1574
  textDiff: ChangeDesc, comparator: RangeComparator<T>,
1545
1575
  /**
@@ -1653,4 +1683,4 @@ situation.
1653
1683
  */
1654
1684
  declare function findColumn(string: string, col: number, tabSize: number, strict?: boolean): number;
1655
1685
 
1656
- export { Annotation, AnnotationType, ChangeDesc, ChangeSet, ChangeSpec, CharCategory, Compartment, EditorSelection, EditorState, EditorStateConfig, Extension, Facet, Line, MapMode, Prec, Range, RangeComparator, RangeCursor, RangeSet, RangeSetBuilder, RangeValue, SelectionRange, SpanIterator, StateCommand, StateEffect, StateEffectType, StateField, Text, TextIterator, Transaction, TransactionSpec, codePointAt, codePointSize, combineConfig, countColumn, findClusterBreak, findColumn, fromCodePoint };
1686
+ export { Annotation, AnnotationType, ChangeDesc, ChangeSet, ChangeSpec, CharCategory, Compartment, EditorSelection, EditorState, EditorStateConfig, Extension, Facet, FacetReader, Line, MapMode, Prec, Range, RangeComparator, RangeCursor, RangeSet, RangeSetBuilder, RangeValue, SelectionRange, SpanIterator, StateCommand, StateEffect, StateEffectType, StateField, Text, TextIterator, Transaction, TransactionSpec, codePointAt, codePointSize, combineConfig, countColumn, findClusterBreak, findColumn, fromCodePoint };
package/dist/index.js CHANGED
@@ -2,10 +2,6 @@
2
2
  The data structure for documents. @nonabstract
3
3
  */
4
4
  class Text {
5
- /**
6
- @internal
7
- */
8
- constructor() { }
9
5
  /**
10
6
  Get the line description around the given position.
11
7
  */
@@ -100,7 +96,8 @@ class Text {
100
96
  return new LineCursor(inner);
101
97
  }
102
98
  /**
103
- @internal
99
+ Return the document as a string, using newline characters to
100
+ separate lines.
104
101
  */
105
102
  toString() { return this.sliceString(0); }
106
103
  /**
@@ -113,6 +110,10 @@ class Text {
113
110
  return lines;
114
111
  }
115
112
  /**
113
+ @internal
114
+ */
115
+ constructor() { }
116
+ /**
116
117
  Create a `Text` instance for the given array of lines.
117
118
  */
118
119
  static of(text) {
@@ -1311,12 +1312,12 @@ class SelectionRange {
1311
1312
  The anchor of the range—the side that doesn't move when you
1312
1313
  extend it.
1313
1314
  */
1314
- get anchor() { return this.flags & 16 /* RangeFlag.Inverted */ ? this.to : this.from; }
1315
+ get anchor() { return this.flags & 32 /* RangeFlag.Inverted */ ? this.to : this.from; }
1315
1316
  /**
1316
1317
  The head of the range, which is moved when the range is
1317
1318
  [extended](https://codemirror.net/6/docs/ref/#state.SelectionRange.extend).
1318
1319
  */
1319
- get head() { return this.flags & 16 /* RangeFlag.Inverted */ ? this.from : this.to; }
1320
+ get head() { return this.flags & 32 /* RangeFlag.Inverted */ ? this.from : this.to; }
1320
1321
  /**
1321
1322
  True when `anchor` and `head` are at the same position.
1322
1323
  */
@@ -1327,14 +1328,14 @@ class SelectionRange {
1327
1328
  the character before its position, 1 the character after, and 0
1328
1329
  means no association.
1329
1330
  */
1330
- get assoc() { return this.flags & 4 /* RangeFlag.AssocBefore */ ? -1 : this.flags & 8 /* RangeFlag.AssocAfter */ ? 1 : 0; }
1331
+ get assoc() { return this.flags & 8 /* RangeFlag.AssocBefore */ ? -1 : this.flags & 16 /* RangeFlag.AssocAfter */ ? 1 : 0; }
1331
1332
  /**
1332
1333
  The bidirectional text level associated with this cursor, if
1333
1334
  any.
1334
1335
  */
1335
1336
  get bidiLevel() {
1336
- let level = this.flags & 3 /* RangeFlag.BidiLevelMask */;
1337
- return level == 3 ? null : level;
1337
+ let level = this.flags & 7 /* RangeFlag.BidiLevelMask */;
1338
+ return level == 7 ? null : level;
1338
1339
  }
1339
1340
  /**
1340
1341
  The goal column (stored vertical offset) associated with a
@@ -1343,8 +1344,8 @@ class SelectionRange {
1343
1344
  lines of different length.
1344
1345
  */
1345
1346
  get goalColumn() {
1346
- let value = this.flags >> 5 /* RangeFlag.GoalColumnOffset */;
1347
- return value == 33554431 /* RangeFlag.NoGoalColumn */ ? undefined : value;
1347
+ let value = this.flags >> 6 /* RangeFlag.GoalColumnOffset */;
1348
+ return value == 16777215 /* RangeFlag.NoGoalColumn */ ? undefined : value;
1348
1349
  }
1349
1350
  /**
1350
1351
  Map this range through a change, producing a valid range in the
@@ -1504,18 +1505,18 @@ class EditorSelection {
1504
1505
  safely ignore the optional arguments in most situations.
1505
1506
  */
1506
1507
  static cursor(pos, assoc = 0, bidiLevel, goalColumn) {
1507
- return SelectionRange.create(pos, pos, (assoc == 0 ? 0 : assoc < 0 ? 4 /* RangeFlag.AssocBefore */ : 8 /* RangeFlag.AssocAfter */) |
1508
- (bidiLevel == null ? 3 : Math.min(2, bidiLevel)) |
1509
- ((goalColumn !== null && goalColumn !== void 0 ? goalColumn : 33554431 /* RangeFlag.NoGoalColumn */) << 5 /* RangeFlag.GoalColumnOffset */));
1508
+ return SelectionRange.create(pos, pos, (assoc == 0 ? 0 : assoc < 0 ? 8 /* RangeFlag.AssocBefore */ : 16 /* RangeFlag.AssocAfter */) |
1509
+ (bidiLevel == null ? 7 : Math.min(6, bidiLevel)) |
1510
+ ((goalColumn !== null && goalColumn !== void 0 ? goalColumn : 16777215 /* RangeFlag.NoGoalColumn */) << 6 /* RangeFlag.GoalColumnOffset */));
1510
1511
  }
1511
1512
  /**
1512
1513
  Create a selection range.
1513
1514
  */
1514
1515
  static range(anchor, head, goalColumn, bidiLevel) {
1515
- let flags = ((goalColumn !== null && goalColumn !== void 0 ? goalColumn : 33554431 /* RangeFlag.NoGoalColumn */) << 5 /* RangeFlag.GoalColumnOffset */) |
1516
- (bidiLevel == null ? 3 : Math.min(2, bidiLevel));
1517
- return head < anchor ? SelectionRange.create(head, anchor, 16 /* RangeFlag.Inverted */ | 8 /* RangeFlag.AssocAfter */ | flags)
1518
- : SelectionRange.create(anchor, head, (head > anchor ? 4 /* RangeFlag.AssocBefore */ : 0) | flags);
1516
+ let flags = ((goalColumn !== null && goalColumn !== void 0 ? goalColumn : 16777215 /* RangeFlag.NoGoalColumn */) << 6 /* RangeFlag.GoalColumnOffset */) |
1517
+ (bidiLevel == null ? 7 : Math.min(6, bidiLevel));
1518
+ return head < anchor ? SelectionRange.create(head, anchor, 32 /* RangeFlag.Inverted */ | 16 /* RangeFlag.AssocAfter */ | flags)
1519
+ : SelectionRange.create(anchor, head, (head > anchor ? 8 /* RangeFlag.AssocBefore */ : 0) | flags);
1519
1520
  }
1520
1521
  /**
1521
1522
  @internal
@@ -1552,6 +1553,9 @@ Examples of uses of facets are the [tab
1552
1553
  size](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize), [editor
1553
1554
  attributes](https://codemirror.net/6/docs/ref/#view.EditorView^editorAttributes), and [update
1554
1555
  listeners](https://codemirror.net/6/docs/ref/#view.EditorView^updateListener).
1556
+
1557
+ Note that `Facet` instances can be used anywhere where
1558
+ [`FacetReader`](https://codemirror.net/6/docs/ref/#state.FacetReader) is expected.
1555
1559
  */
1556
1560
  class Facet {
1557
1561
  constructor(
@@ -1579,6 +1583,11 @@ class Facet {
1579
1583
  this.extensions = typeof enables == "function" ? enables(this) : enables;
1580
1584
  }
1581
1585
  /**
1586
+ Returns a facet reader for this facet, which can be used to
1587
+ [read](https://codemirror.net/6/docs/ref/#state.EditorState.facet) it but not to define values for it.
1588
+ */
1589
+ get reader() { return this; }
1590
+ /**
1582
1591
  Define a new facet.
1583
1592
  */
1584
1593
  static define(config = {}) {
@@ -2156,7 +2165,10 @@ class StateEffect {
2156
2165
  is(type) { return this.type == type; }
2157
2166
  /**
2158
2167
  Define a new effect type. The type parameter indicates the type
2159
- of values that his effect holds.
2168
+ of values that his effect holds. It should be a type that
2169
+ doesn't include `undefined`, since that is used in
2170
+ [mapping](https://codemirror.net/6/docs/ref/#state.StateEffect.map) to indicate that an effect is
2171
+ removed.
2160
2172
  */
2161
2173
  static define(spec = {}) {
2162
2174
  return new StateEffectType(spec.map || (v => v));
@@ -3299,8 +3311,7 @@ class RangeSet {
3299
3311
  static compare(oldSets, newSets,
3300
3312
  /**
3301
3313
  This indicates how the underlying data changed between these
3302
- ranges, and is needed to synchronize the iteration. `from` and
3303
- `to` are coordinates in the _new_ space, after these changes.
3314
+ ranges, and is needed to synchronize the iteration.
3304
3315
  */
3305
3316
  textDiff, comparator,
3306
3317
  /**
@@ -3411,6 +3422,18 @@ A range set builder is a data structure that helps build up a
3411
3422
  an array of [`Range`](https://codemirror.net/6/docs/ref/#state.Range) objects.
3412
3423
  */
3413
3424
  class RangeSetBuilder {
3425
+ finishChunk(newArrays) {
3426
+ this.chunks.push(new Chunk(this.from, this.to, this.value, this.maxPoint));
3427
+ this.chunkPos.push(this.chunkStart);
3428
+ this.chunkStart = -1;
3429
+ this.setMaxPoint = Math.max(this.setMaxPoint, this.maxPoint);
3430
+ this.maxPoint = -1;
3431
+ if (newArrays) {
3432
+ this.from = [];
3433
+ this.to = [];
3434
+ this.value = [];
3435
+ }
3436
+ }
3414
3437
  /**
3415
3438
  Create an empty builder.
3416
3439
  */
@@ -3428,18 +3451,6 @@ class RangeSetBuilder {
3428
3451
  this.setMaxPoint = -1;
3429
3452
  this.nextLayer = null;
3430
3453
  }
3431
- finishChunk(newArrays) {
3432
- this.chunks.push(new Chunk(this.from, this.to, this.value, this.maxPoint));
3433
- this.chunkPos.push(this.chunkStart);
3434
- this.chunkStart = -1;
3435
- this.setMaxPoint = Math.max(this.setMaxPoint, this.maxPoint);
3436
- this.maxPoint = -1;
3437
- if (newArrays) {
3438
- this.from = [];
3439
- this.to = [];
3440
- this.value = [];
3441
- }
3442
- }
3443
3454
  /**
3444
3455
  Add a range. Ranges should be added in sorted (by `from` and
3445
3456
  `value.startSide`) order.
@@ -3799,7 +3810,7 @@ function compare(a, startA, b, startB, length, comparator) {
3799
3810
  let end = diff < 0 ? a.to + dPos : b.to, clipEnd = Math.min(end, endB);
3800
3811
  if (a.point || b.point) {
3801
3812
  if (!(a.point && b.point && (a.point == b.point || a.point.eq(b.point)) &&
3802
- sameValues(a.activeForPoint(a.to + dPos), b.activeForPoint(b.to))))
3813
+ sameValues(a.activeForPoint(a.to), b.activeForPoint(b.to))))
3803
3814
  comparator.comparePoint(pos, clipEnd, a.point, b.point);
3804
3815
  }
3805
3816
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/state",
3
- "version": "6.2.0",
3
+ "version": "6.3.0",
4
4
  "description": "Editor state data structures for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",
@@ -12,7 +12,7 @@
12
12
  ],
13
13
  "author": {
14
14
  "name": "Marijn Haverbeke",
15
- "email": "marijnh@gmail.com",
15
+ "email": "marijn@haverbeke.berlin",
16
16
  "url": "http://marijnhaverbeke.nl"
17
17
  },
18
18
  "type": "module",
@@ -26,7 +26,7 @@
26
26
  "sideEffects": false,
27
27
  "license": "MIT",
28
28
  "devDependencies": {
29
- "@codemirror/buildhelper": "^0.1.5"
29
+ "@codemirror/buildhelper": "^1.0.0"
30
30
  },
31
31
  "repository": {
32
32
  "type": "git",