@codemirror/state 6.3.1 → 6.3.3

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/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## 6.3.3 (2023-12-06)
2
+
3
+ ### Bug fixes
4
+
5
+ Fix an issue where `Text.slice` and `Text.replace` could return objects with incorrect `length` when the given `from`/`to` values were out of range for the text.
6
+
7
+ ## 6.3.2 (2023-11-27)
8
+
9
+ ### Bug fixes
10
+
11
+ Make sure transactions cannot add multiple selections when `allowMultipleSelections` is false.
12
+
13
+ Fix a bug that caused `Text.iterLines` to not return empty lines at the end of the iterated ranges.
14
+
1
15
  ## 6.3.1 (2023-10-18)
2
16
 
3
17
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -24,6 +24,7 @@ class Text {
24
24
  Replace a range of the text with the given content.
25
25
  */
26
26
  replace(from, to, text) {
27
+ [from, to] = clip(this, from, to);
27
28
  let parts = [];
28
29
  this.decompose(0, from, parts, 2 /* Open.To */);
29
30
  if (text.length)
@@ -41,6 +42,7 @@ class Text {
41
42
  Retrieve the text between the given points.
42
43
  */
43
44
  slice(from, to = this.length) {
45
+ [from, to] = clip(this, from, to);
44
46
  let parts = [];
45
47
  this.decompose(from, to, parts, 0);
46
48
  return TextNode.from(parts, to - from);
@@ -167,6 +169,7 @@ class TextLeaf extends Text {
167
169
  replace(from, to, text) {
168
170
  if (!(text instanceof TextLeaf))
169
171
  return super.replace(from, to, text);
172
+ [from, to] = clip(this, from, to);
170
173
  let lines = appendText(this.text, appendText(text.text, sliceText(this.text, 0, from)), to);
171
174
  let newLen = this.length + text.length - (to - from);
172
175
  if (lines.length <= 32 /* Tree.Branch */)
@@ -174,6 +177,7 @@ class TextLeaf extends Text {
174
177
  return TextNode.from(TextLeaf.split(lines, []), newLen);
175
178
  }
176
179
  sliceString(from, to = this.length, lineSep = "\n") {
180
+ [from, to] = clip(this, from, to);
177
181
  let result = "";
178
182
  for (let pos = 0, i = 0; pos <= to && i < this.text.length; i++) {
179
183
  let line = this.text[i], end = pos + line.length;
@@ -242,6 +246,7 @@ class TextNode extends Text {
242
246
  }
243
247
  }
244
248
  replace(from, to, text) {
249
+ [from, to] = clip(this, from, to);
245
250
  if (text.lines < this.lines)
246
251
  for (let i = 0, pos = 0; i < this.children.length; i++) {
247
252
  let child = this.children[i], end = pos + child.length;
@@ -264,6 +269,7 @@ class TextNode extends Text {
264
269
  return super.replace(from, to, text);
265
270
  }
266
271
  sliceString(from, to = this.length, lineSep = "\n") {
272
+ [from, to] = clip(this, from, to);
267
273
  let result = "";
268
274
  for (let i = 0, pos = 0; i < this.children.length && pos <= to; i++) {
269
275
  let child = this.children[i], end = pos + child.length;
@@ -485,7 +491,11 @@ class LineCursor {
485
491
  }
486
492
  next(skip = 0) {
487
493
  let { done, lineBreak, value } = this.inner.next(skip);
488
- if (done) {
494
+ if (done && this.afterBreak) {
495
+ this.value = "";
496
+ this.afterBreak = false;
497
+ }
498
+ else if (done) {
489
499
  this.done = true;
490
500
  this.value = "";
491
501
  }
@@ -547,6 +557,10 @@ class Line {
547
557
  */
548
558
  get length() { return this.to - this.from; }
549
559
  }
560
+ function clip(text, from, to) {
561
+ from = Math.max(0, Math.min(text.length, from));
562
+ return [from, Math.max(from, Math.min(text.length, to))];
563
+ }
550
564
 
551
565
  // Compressed representation of the Grapheme_Cluster_Break=Extend
552
566
  // information from
@@ -2643,7 +2657,8 @@ class EditorState {
2643
2657
  else {
2644
2658
  startValues = tr.startState.values.slice();
2645
2659
  }
2646
- new EditorState(conf, tr.newDoc, tr.newSelection, startValues, (state, slot) => slot.update(state, tr), tr);
2660
+ let selection = tr.startState.facet(allowMultipleSelections) ? tr.newSelection : tr.newSelection.asSingle();
2661
+ new EditorState(conf, tr.newDoc, selection, startValues, (state, slot) => slot.update(state, tr), tr);
2647
2662
  }
2648
2663
  /**
2649
2664
  Create a [transaction spec](https://codemirror.net/6/docs/ref/#state.TransactionSpec) that
package/dist/index.d.cts CHANGED
@@ -574,7 +574,7 @@ declare class Facet<Input, Output = readonly Input[]> implements FacetReader<Out
574
574
  tag: Output;
575
575
  }
576
576
  /**
577
- A facet reader can be used to fetch the value of a facet, though
577
+ A facet reader can be used to fetch the value of a facet, through
578
578
  [`EditorState.facet`](https://codemirror.net/6/docs/ref/#state.EditorState.facet) or as a dependency
579
579
  in [`Facet.compute`](https://codemirror.net/6/docs/ref/#state.Facet.compute), but not to define new
580
580
  values for the facet.
package/dist/index.d.ts CHANGED
@@ -574,7 +574,7 @@ declare class Facet<Input, Output = readonly Input[]> implements FacetReader<Out
574
574
  tag: Output;
575
575
  }
576
576
  /**
577
- A facet reader can be used to fetch the value of a facet, though
577
+ A facet reader can be used to fetch the value of a facet, through
578
578
  [`EditorState.facet`](https://codemirror.net/6/docs/ref/#state.EditorState.facet) or as a dependency
579
579
  in [`Facet.compute`](https://codemirror.net/6/docs/ref/#state.Facet.compute), but not to define new
580
580
  values for the facet.
package/dist/index.js CHANGED
@@ -22,6 +22,7 @@ class Text {
22
22
  Replace a range of the text with the given content.
23
23
  */
24
24
  replace(from, to, text) {
25
+ [from, to] = clip(this, from, to);
25
26
  let parts = [];
26
27
  this.decompose(0, from, parts, 2 /* Open.To */);
27
28
  if (text.length)
@@ -39,6 +40,7 @@ class Text {
39
40
  Retrieve the text between the given points.
40
41
  */
41
42
  slice(from, to = this.length) {
43
+ [from, to] = clip(this, from, to);
42
44
  let parts = [];
43
45
  this.decompose(from, to, parts, 0);
44
46
  return TextNode.from(parts, to - from);
@@ -165,6 +167,7 @@ class TextLeaf extends Text {
165
167
  replace(from, to, text) {
166
168
  if (!(text instanceof TextLeaf))
167
169
  return super.replace(from, to, text);
170
+ [from, to] = clip(this, from, to);
168
171
  let lines = appendText(this.text, appendText(text.text, sliceText(this.text, 0, from)), to);
169
172
  let newLen = this.length + text.length - (to - from);
170
173
  if (lines.length <= 32 /* Tree.Branch */)
@@ -172,6 +175,7 @@ class TextLeaf extends Text {
172
175
  return TextNode.from(TextLeaf.split(lines, []), newLen);
173
176
  }
174
177
  sliceString(from, to = this.length, lineSep = "\n") {
178
+ [from, to] = clip(this, from, to);
175
179
  let result = "";
176
180
  for (let pos = 0, i = 0; pos <= to && i < this.text.length; i++) {
177
181
  let line = this.text[i], end = pos + line.length;
@@ -240,6 +244,7 @@ class TextNode extends Text {
240
244
  }
241
245
  }
242
246
  replace(from, to, text) {
247
+ [from, to] = clip(this, from, to);
243
248
  if (text.lines < this.lines)
244
249
  for (let i = 0, pos = 0; i < this.children.length; i++) {
245
250
  let child = this.children[i], end = pos + child.length;
@@ -262,6 +267,7 @@ class TextNode extends Text {
262
267
  return super.replace(from, to, text);
263
268
  }
264
269
  sliceString(from, to = this.length, lineSep = "\n") {
270
+ [from, to] = clip(this, from, to);
265
271
  let result = "";
266
272
  for (let i = 0, pos = 0; i < this.children.length && pos <= to; i++) {
267
273
  let child = this.children[i], end = pos + child.length;
@@ -483,7 +489,11 @@ class LineCursor {
483
489
  }
484
490
  next(skip = 0) {
485
491
  let { done, lineBreak, value } = this.inner.next(skip);
486
- if (done) {
492
+ if (done && this.afterBreak) {
493
+ this.value = "";
494
+ this.afterBreak = false;
495
+ }
496
+ else if (done) {
487
497
  this.done = true;
488
498
  this.value = "";
489
499
  }
@@ -545,6 +555,10 @@ class Line {
545
555
  */
546
556
  get length() { return this.to - this.from; }
547
557
  }
558
+ function clip(text, from, to) {
559
+ from = Math.max(0, Math.min(text.length, from));
560
+ return [from, Math.max(from, Math.min(text.length, to))];
561
+ }
548
562
 
549
563
  // Compressed representation of the Grapheme_Cluster_Break=Extend
550
564
  // information from
@@ -2639,7 +2653,8 @@ class EditorState {
2639
2653
  else {
2640
2654
  startValues = tr.startState.values.slice();
2641
2655
  }
2642
- new EditorState(conf, tr.newDoc, tr.newSelection, startValues, (state, slot) => slot.update(state, tr), tr);
2656
+ let selection = tr.startState.facet(allowMultipleSelections) ? tr.newSelection : tr.newSelection.asSingle();
2657
+ new EditorState(conf, tr.newDoc, selection, startValues, (state, slot) => slot.update(state, tr), tr);
2643
2658
  }
2644
2659
  /**
2645
2660
  Create a [transaction spec](https://codemirror.net/6/docs/ref/#state.TransactionSpec) that
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/state",
3
- "version": "6.3.1",
3
+ "version": "6.3.3",
4
4
  "description": "Editor state data structures for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",