@codemirror/view 6.29.0 → 6.29.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 6.29.1 (2024-07-29)
2
+
3
+ ### Bug fixes
4
+
5
+ Fix a crash on old Safari browsers that don't support `MediaQueryList.addEventListener`.
6
+
7
+ Fix an issue where `EditorView.viewportLineBlocks` (and thus other things like the gutter) might be out of date after some kinds of decoration changes.
8
+
1
9
  ## 6.29.0 (2024-07-25)
2
10
 
3
11
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -4605,6 +4605,9 @@ function firefoxCopyCutHack(doc) {
4605
4605
  }
4606
4606
 
4607
4607
  const wrappingWhiteSpace = ["pre-wrap", "normal", "pre-line", "break-spaces"];
4608
+ // Used to track, during updateHeight, if any actual heights changed
4609
+ let heightChangeFlag = false;
4610
+ function clearHeightChangeFlag() { heightChangeFlag = false; }
4608
4611
  class HeightOracle {
4609
4612
  constructor(lineWrapping) {
4610
4613
  this.lineWrapping = lineWrapping;
@@ -4614,8 +4617,6 @@ class HeightOracle {
4614
4617
  this.charWidth = 7;
4615
4618
  this.textHeight = 14; // The height of the actual font (font-size)
4616
4619
  this.lineLength = 30;
4617
- // Used to track, during updateHeight, if any actual heights changed
4618
- this.heightChanged = false;
4619
4620
  }
4620
4621
  heightForGap(from, to) {
4621
4622
  let lines = this.doc.lineAt(to).number - this.doc.lineAt(from).number + 1;
@@ -4774,10 +4775,10 @@ class HeightMap {
4774
4775
  }
4775
4776
  get outdated() { return (this.flags & 2 /* Flag.Outdated */) > 0; }
4776
4777
  set outdated(value) { this.flags = (value ? 2 /* Flag.Outdated */ : 0) | (this.flags & ~2 /* Flag.Outdated */); }
4777
- setHeight(oracle, height) {
4778
+ setHeight(height) {
4778
4779
  if (this.height != height) {
4779
4780
  if (Math.abs(this.height - height) > Epsilon)
4780
- oracle.heightChanged = true;
4781
+ heightChangeFlag = true;
4781
4782
  this.height = height;
4782
4783
  }
4783
4784
  }
@@ -4808,7 +4809,7 @@ class HeightMap {
4808
4809
  fromB += start.from - fromA;
4809
4810
  fromA = start.from;
4810
4811
  let nodes = NodeBuilder.build(oracle.setDoc(doc), decorations, fromB, toB);
4811
- me = me.replace(fromA, toA, nodes);
4812
+ me = replace(me, me.replace(fromA, toA, nodes));
4812
4813
  }
4813
4814
  return me.updateHeight(oracle, 0);
4814
4815
  }
@@ -4868,6 +4869,13 @@ class HeightMap {
4868
4869
  return new HeightMapBranch(HeightMap.of(nodes.slice(0, i)), brk, HeightMap.of(nodes.slice(j)));
4869
4870
  }
4870
4871
  }
4872
+ function replace(old, val) {
4873
+ if (old == val)
4874
+ return old;
4875
+ if (old.constructor != val.constructor)
4876
+ heightChangeFlag = true;
4877
+ return val;
4878
+ }
4871
4879
  HeightMap.prototype.size = 1;
4872
4880
  class HeightMapBlock extends HeightMap {
4873
4881
  constructor(length, height, deco) {
@@ -4886,7 +4894,7 @@ class HeightMapBlock extends HeightMap {
4886
4894
  }
4887
4895
  updateHeight(oracle, offset = 0, _force = false, measured) {
4888
4896
  if (measured && measured.from <= offset && measured.more)
4889
- this.setHeight(oracle, measured.heights[measured.index++]);
4897
+ this.setHeight(measured.heights[measured.index++]);
4890
4898
  this.outdated = false;
4891
4899
  return this;
4892
4900
  }
@@ -4920,9 +4928,9 @@ class HeightMapText extends HeightMapBlock {
4920
4928
  }
4921
4929
  updateHeight(oracle, offset = 0, force = false, measured) {
4922
4930
  if (measured && measured.from <= offset && measured.more)
4923
- this.setHeight(oracle, measured.heights[measured.index++]);
4931
+ this.setHeight(measured.heights[measured.index++]);
4924
4932
  else if (force || this.outdated)
4925
- this.setHeight(oracle, Math.max(this.widgetHeight, oracle.heightForLine(this.length - this.collapsed)) +
4933
+ this.setHeight(Math.max(this.widgetHeight, oracle.heightForLine(this.length - this.collapsed)) +
4926
4934
  this.breaks * oracle.lineHeight);
4927
4935
  this.outdated = false;
4928
4936
  return this;
@@ -5045,11 +5053,11 @@ class HeightMapGap extends HeightMap {
5045
5053
  let result = HeightMap.of(nodes);
5046
5054
  if (singleHeight < 0 || Math.abs(result.height - this.height) >= Epsilon ||
5047
5055
  Math.abs(singleHeight - this.heightMetrics(oracle, offset).perLine) >= Epsilon)
5048
- oracle.heightChanged = true;
5049
- return result;
5056
+ heightChangeFlag = true;
5057
+ return replace(this, result);
5050
5058
  }
5051
5059
  else if (force || this.outdated) {
5052
- this.setHeight(oracle, oracle.heightForGap(offset, offset + this.length));
5060
+ this.setHeight(oracle.heightForGap(offset, offset + this.length));
5053
5061
  this.outdated = false;
5054
5062
  }
5055
5063
  return this;
@@ -5147,9 +5155,9 @@ class HeightMapBranch extends HeightMap {
5147
5155
  balanced(left, right) {
5148
5156
  if (left.size > 2 * right.size || right.size > 2 * left.size)
5149
5157
  return HeightMap.of(this.break ? [left, null, right] : [left, right]);
5150
- this.left = left;
5151
- this.right = right;
5152
- this.height = left.height + right.height;
5158
+ this.left = replace(this.left, left);
5159
+ this.right = replace(this.right, right);
5160
+ this.setHeight(left.height + right.height);
5153
5161
  this.outdated = left.outdated || right.outdated;
5154
5162
  this.size = left.size + right.size;
5155
5163
  this.length = left.length + this.break + right.length;
@@ -5488,8 +5496,9 @@ class ViewState {
5488
5496
  let heightChanges = ChangedRange.extendWithRanges(contentChanges, heightRelevantDecoChanges(prevDeco, this.stateDeco, update ? update.changes : state.ChangeSet.empty(this.state.doc.length)));
5489
5497
  let prevHeight = this.heightMap.height;
5490
5498
  let scrollAnchor = this.scrolledToBottom ? null : this.scrollAnchorAt(this.scrollTop);
5499
+ clearHeightChangeFlag();
5491
5500
  this.heightMap = this.heightMap.applyChanges(this.stateDeco, update.startState.doc, this.heightOracle.setDoc(this.state.doc), heightChanges);
5492
- if (this.heightMap.height != prevHeight)
5501
+ if (this.heightMap.height != prevHeight || heightChangeFlag)
5493
5502
  update.flags |= 2 /* UpdateFlag.Height */;
5494
5503
  if (scrollAnchor) {
5495
5504
  this.scrollAnchorPos = update.changes.mapPos(scrollAnchor.from, -1);
@@ -5593,12 +5602,12 @@ class ViewState {
5593
5602
  bias = Math.max(dTop, dBottom);
5594
5603
  else if (dTop < 0 && dBottom < 0)
5595
5604
  bias = Math.min(dTop, dBottom);
5596
- oracle.heightChanged = false;
5605
+ clearHeightChangeFlag();
5597
5606
  for (let vp of this.viewports) {
5598
5607
  let heights = vp.from == this.viewport.from ? lineHeights : view.docView.measureVisibleLineHeights(vp);
5599
5608
  this.heightMap = (refresh ? HeightMap.empty().applyChanges(this.stateDeco, state.Text.empty, this.heightOracle, [new ChangedRange(0, 0, 0, view.state.doc.length)]) : this.heightMap).updateHeight(oracle, 0, refresh, new MeasuredHeights(vp.from, heights));
5600
5609
  }
5601
- if (oracle.heightChanged)
5610
+ if (heightChangeFlag)
5602
5611
  result |= 2 /* UpdateFlag.Height */;
5603
5612
  }
5604
5613
  let viewportChange = !this.viewportIsAppropriate(this.viewport, bias) ||
@@ -6718,7 +6727,7 @@ class DOMObserver {
6718
6727
  }, 50);
6719
6728
  }
6720
6729
  onPrint(event) {
6721
- if (event.type == "change" && !event.matches)
6730
+ if ((event.type == "change" || !event.type) && !event.matches)
6722
6731
  return;
6723
6732
  this.view.viewState.printing = true;
6724
6733
  this.view.measure();
@@ -7001,8 +7010,12 @@ class DOMObserver {
7001
7010
  }
7002
7011
  addWindowListeners(win) {
7003
7012
  win.addEventListener("resize", this.onResize);
7004
- if (this.printQuery)
7005
- this.printQuery.addEventListener("change", this.onPrint);
7013
+ if (this.printQuery) {
7014
+ if (this.printQuery.addEventListener)
7015
+ this.printQuery.addEventListener("change", this.onPrint);
7016
+ else
7017
+ this.printQuery.addListener(this.onPrint);
7018
+ }
7006
7019
  else
7007
7020
  win.addEventListener("beforeprint", this.onPrint);
7008
7021
  win.addEventListener("scroll", this.onScroll);
@@ -7011,8 +7024,12 @@ class DOMObserver {
7011
7024
  removeWindowListeners(win) {
7012
7025
  win.removeEventListener("scroll", this.onScroll);
7013
7026
  win.removeEventListener("resize", this.onResize);
7014
- if (this.printQuery)
7015
- this.printQuery.removeEventListener("change", this.onPrint);
7027
+ if (this.printQuery) {
7028
+ if (this.printQuery.removeEventListener)
7029
+ this.printQuery.removeEventListener("change", this.onPrint);
7030
+ else
7031
+ this.printQuery.removeListener(this.onPrint);
7032
+ }
7016
7033
  else
7017
7034
  win.removeEventListener("beforeprint", this.onPrint);
7018
7035
  win.document.removeEventListener("selectionchange", this.onSelectionChange);
package/dist/index.js CHANGED
@@ -4601,6 +4601,9 @@ function firefoxCopyCutHack(doc) {
4601
4601
  }
4602
4602
 
4603
4603
  const wrappingWhiteSpace = ["pre-wrap", "normal", "pre-line", "break-spaces"];
4604
+ // Used to track, during updateHeight, if any actual heights changed
4605
+ let heightChangeFlag = false;
4606
+ function clearHeightChangeFlag() { heightChangeFlag = false; }
4604
4607
  class HeightOracle {
4605
4608
  constructor(lineWrapping) {
4606
4609
  this.lineWrapping = lineWrapping;
@@ -4610,8 +4613,6 @@ class HeightOracle {
4610
4613
  this.charWidth = 7;
4611
4614
  this.textHeight = 14; // The height of the actual font (font-size)
4612
4615
  this.lineLength = 30;
4613
- // Used to track, during updateHeight, if any actual heights changed
4614
- this.heightChanged = false;
4615
4616
  }
4616
4617
  heightForGap(from, to) {
4617
4618
  let lines = this.doc.lineAt(to).number - this.doc.lineAt(from).number + 1;
@@ -4769,10 +4770,10 @@ class HeightMap {
4769
4770
  }
4770
4771
  get outdated() { return (this.flags & 2 /* Flag.Outdated */) > 0; }
4771
4772
  set outdated(value) { this.flags = (value ? 2 /* Flag.Outdated */ : 0) | (this.flags & ~2 /* Flag.Outdated */); }
4772
- setHeight(oracle, height) {
4773
+ setHeight(height) {
4773
4774
  if (this.height != height) {
4774
4775
  if (Math.abs(this.height - height) > Epsilon)
4775
- oracle.heightChanged = true;
4776
+ heightChangeFlag = true;
4776
4777
  this.height = height;
4777
4778
  }
4778
4779
  }
@@ -4803,7 +4804,7 @@ class HeightMap {
4803
4804
  fromB += start.from - fromA;
4804
4805
  fromA = start.from;
4805
4806
  let nodes = NodeBuilder.build(oracle.setDoc(doc), decorations, fromB, toB);
4806
- me = me.replace(fromA, toA, nodes);
4807
+ me = replace(me, me.replace(fromA, toA, nodes));
4807
4808
  }
4808
4809
  return me.updateHeight(oracle, 0);
4809
4810
  }
@@ -4863,6 +4864,13 @@ class HeightMap {
4863
4864
  return new HeightMapBranch(HeightMap.of(nodes.slice(0, i)), brk, HeightMap.of(nodes.slice(j)));
4864
4865
  }
4865
4866
  }
4867
+ function replace(old, val) {
4868
+ if (old == val)
4869
+ return old;
4870
+ if (old.constructor != val.constructor)
4871
+ heightChangeFlag = true;
4872
+ return val;
4873
+ }
4866
4874
  HeightMap.prototype.size = 1;
4867
4875
  class HeightMapBlock extends HeightMap {
4868
4876
  constructor(length, height, deco) {
@@ -4881,7 +4889,7 @@ class HeightMapBlock extends HeightMap {
4881
4889
  }
4882
4890
  updateHeight(oracle, offset = 0, _force = false, measured) {
4883
4891
  if (measured && measured.from <= offset && measured.more)
4884
- this.setHeight(oracle, measured.heights[measured.index++]);
4892
+ this.setHeight(measured.heights[measured.index++]);
4885
4893
  this.outdated = false;
4886
4894
  return this;
4887
4895
  }
@@ -4915,9 +4923,9 @@ class HeightMapText extends HeightMapBlock {
4915
4923
  }
4916
4924
  updateHeight(oracle, offset = 0, force = false, measured) {
4917
4925
  if (measured && measured.from <= offset && measured.more)
4918
- this.setHeight(oracle, measured.heights[measured.index++]);
4926
+ this.setHeight(measured.heights[measured.index++]);
4919
4927
  else if (force || this.outdated)
4920
- this.setHeight(oracle, Math.max(this.widgetHeight, oracle.heightForLine(this.length - this.collapsed)) +
4928
+ this.setHeight(Math.max(this.widgetHeight, oracle.heightForLine(this.length - this.collapsed)) +
4921
4929
  this.breaks * oracle.lineHeight);
4922
4930
  this.outdated = false;
4923
4931
  return this;
@@ -5040,11 +5048,11 @@ class HeightMapGap extends HeightMap {
5040
5048
  let result = HeightMap.of(nodes);
5041
5049
  if (singleHeight < 0 || Math.abs(result.height - this.height) >= Epsilon ||
5042
5050
  Math.abs(singleHeight - this.heightMetrics(oracle, offset).perLine) >= Epsilon)
5043
- oracle.heightChanged = true;
5044
- return result;
5051
+ heightChangeFlag = true;
5052
+ return replace(this, result);
5045
5053
  }
5046
5054
  else if (force || this.outdated) {
5047
- this.setHeight(oracle, oracle.heightForGap(offset, offset + this.length));
5055
+ this.setHeight(oracle.heightForGap(offset, offset + this.length));
5048
5056
  this.outdated = false;
5049
5057
  }
5050
5058
  return this;
@@ -5142,9 +5150,9 @@ class HeightMapBranch extends HeightMap {
5142
5150
  balanced(left, right) {
5143
5151
  if (left.size > 2 * right.size || right.size > 2 * left.size)
5144
5152
  return HeightMap.of(this.break ? [left, null, right] : [left, right]);
5145
- this.left = left;
5146
- this.right = right;
5147
- this.height = left.height + right.height;
5153
+ this.left = replace(this.left, left);
5154
+ this.right = replace(this.right, right);
5155
+ this.setHeight(left.height + right.height);
5148
5156
  this.outdated = left.outdated || right.outdated;
5149
5157
  this.size = left.size + right.size;
5150
5158
  this.length = left.length + this.break + right.length;
@@ -5483,8 +5491,9 @@ class ViewState {
5483
5491
  let heightChanges = ChangedRange.extendWithRanges(contentChanges, heightRelevantDecoChanges(prevDeco, this.stateDeco, update ? update.changes : ChangeSet.empty(this.state.doc.length)));
5484
5492
  let prevHeight = this.heightMap.height;
5485
5493
  let scrollAnchor = this.scrolledToBottom ? null : this.scrollAnchorAt(this.scrollTop);
5494
+ clearHeightChangeFlag();
5486
5495
  this.heightMap = this.heightMap.applyChanges(this.stateDeco, update.startState.doc, this.heightOracle.setDoc(this.state.doc), heightChanges);
5487
- if (this.heightMap.height != prevHeight)
5496
+ if (this.heightMap.height != prevHeight || heightChangeFlag)
5488
5497
  update.flags |= 2 /* UpdateFlag.Height */;
5489
5498
  if (scrollAnchor) {
5490
5499
  this.scrollAnchorPos = update.changes.mapPos(scrollAnchor.from, -1);
@@ -5588,12 +5597,12 @@ class ViewState {
5588
5597
  bias = Math.max(dTop, dBottom);
5589
5598
  else if (dTop < 0 && dBottom < 0)
5590
5599
  bias = Math.min(dTop, dBottom);
5591
- oracle.heightChanged = false;
5600
+ clearHeightChangeFlag();
5592
5601
  for (let vp of this.viewports) {
5593
5602
  let heights = vp.from == this.viewport.from ? lineHeights : view.docView.measureVisibleLineHeights(vp);
5594
5603
  this.heightMap = (refresh ? HeightMap.empty().applyChanges(this.stateDeco, Text.empty, this.heightOracle, [new ChangedRange(0, 0, 0, view.state.doc.length)]) : this.heightMap).updateHeight(oracle, 0, refresh, new MeasuredHeights(vp.from, heights));
5595
5604
  }
5596
- if (oracle.heightChanged)
5605
+ if (heightChangeFlag)
5597
5606
  result |= 2 /* UpdateFlag.Height */;
5598
5607
  }
5599
5608
  let viewportChange = !this.viewportIsAppropriate(this.viewport, bias) ||
@@ -6713,7 +6722,7 @@ class DOMObserver {
6713
6722
  }, 50);
6714
6723
  }
6715
6724
  onPrint(event) {
6716
- if (event.type == "change" && !event.matches)
6725
+ if ((event.type == "change" || !event.type) && !event.matches)
6717
6726
  return;
6718
6727
  this.view.viewState.printing = true;
6719
6728
  this.view.measure();
@@ -6996,8 +7005,12 @@ class DOMObserver {
6996
7005
  }
6997
7006
  addWindowListeners(win) {
6998
7007
  win.addEventListener("resize", this.onResize);
6999
- if (this.printQuery)
7000
- this.printQuery.addEventListener("change", this.onPrint);
7008
+ if (this.printQuery) {
7009
+ if (this.printQuery.addEventListener)
7010
+ this.printQuery.addEventListener("change", this.onPrint);
7011
+ else
7012
+ this.printQuery.addListener(this.onPrint);
7013
+ }
7001
7014
  else
7002
7015
  win.addEventListener("beforeprint", this.onPrint);
7003
7016
  win.addEventListener("scroll", this.onScroll);
@@ -7006,8 +7019,12 @@ class DOMObserver {
7006
7019
  removeWindowListeners(win) {
7007
7020
  win.removeEventListener("scroll", this.onScroll);
7008
7021
  win.removeEventListener("resize", this.onResize);
7009
- if (this.printQuery)
7010
- this.printQuery.removeEventListener("change", this.onPrint);
7022
+ if (this.printQuery) {
7023
+ if (this.printQuery.removeEventListener)
7024
+ this.printQuery.removeEventListener("change", this.onPrint);
7025
+ else
7026
+ this.printQuery.removeListener(this.onPrint);
7027
+ }
7011
7028
  else
7012
7029
  win.removeEventListener("beforeprint", this.onPrint);
7013
7030
  win.document.removeEventListener("selectionchange", this.onSelectionChange);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/view",
3
- "version": "6.29.0",
3
+ "version": "6.29.1",
4
4
  "description": "DOM view component for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",