prosemirror-transform 1.5.0 → 1.6.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/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 1.6.0 (2022-06-01)
2
+
3
+ ### Bug fixes
4
+
5
+ Allow replace steps to be mapped through changes that delete content next to their start and end points, as long as no delete spans across those points.
6
+
7
+ ### New features
8
+
9
+ `MapResult` objects now provide information about whether the tokens before, after, and around the position were deleted.
10
+
1
11
  ## 1.5.0 (2022-05-30)
2
12
 
3
13
  ### New features
package/dist/index.cjs CHANGED
@@ -22,12 +22,12 @@ function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Re
22
22
 
23
23
  function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
24
24
 
25
+ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
26
+
25
27
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
26
28
 
27
29
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
28
30
 
29
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
30
-
31
31
  Object.defineProperty(exports, '__esModule', {
32
32
  value: true
33
33
  });
@@ -49,16 +49,44 @@ function recoverOffset(value) {
49
49
  return (value - (value & lower16)) / factor16;
50
50
  }
51
51
 
52
- var MapResult = _createClass(function MapResult(pos) {
53
- var deleted = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
54
- var recover = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
52
+ var DEL_BEFORE = 1,
53
+ DEL_AFTER = 2,
54
+ DEL_ACROSS = 4,
55
+ DEL_SIDE = 8;
55
56
 
56
- _classCallCheck(this, MapResult);
57
+ var MapResult = function () {
58
+ function MapResult(pos, delInfo, recover) {
59
+ _classCallCheck(this, MapResult);
57
60
 
58
- this.pos = pos;
59
- this.deleted = deleted;
60
- this.recover = recover;
61
- });
61
+ this.pos = pos;
62
+ this.delInfo = delInfo;
63
+ this.recover = recover;
64
+ }
65
+
66
+ _createClass(MapResult, [{
67
+ key: "deleted",
68
+ get: function get() {
69
+ return (this.delInfo & DEL_SIDE) > 0;
70
+ }
71
+ }, {
72
+ key: "deletedBefore",
73
+ get: function get() {
74
+ return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0;
75
+ }
76
+ }, {
77
+ key: "deletedAfter",
78
+ get: function get() {
79
+ return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0;
80
+ }
81
+ }, {
82
+ key: "deletedAcross",
83
+ get: function get() {
84
+ return (this.delInfo & DEL_ACROSS) > 0;
85
+ }
86
+ }]);
87
+
88
+ return MapResult;
89
+ }();
62
90
 
63
91
  var StepMap = function () {
64
92
  function StepMap(ranges) {
@@ -112,13 +140,15 @@ var StepMap = function () {
112
140
  var result = start + diff + (side < 0 ? 0 : newSize);
113
141
  if (simple) return result;
114
142
  var recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
115
- return new MapResult(result, assoc < 0 ? pos != start : pos != end, recover);
143
+ var del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
144
+ if (assoc < 0 ? pos != start : pos != end) del |= DEL_SIDE;
145
+ return new MapResult(result, del, recover);
116
146
  }
117
147
 
118
148
  diff += newSize - oldSize;
119
149
  }
120
150
 
121
- return simple ? pos + diff : new MapResult(pos + diff);
151
+ return simple ? pos + diff : new MapResult(pos + diff, 0, null);
122
152
  }
123
153
  }, {
124
154
  key: "touches",
@@ -267,7 +297,7 @@ var Mapping = function () {
267
297
  }, {
268
298
  key: "_map",
269
299
  value: function _map(pos, assoc, simple) {
270
- var deleted = false;
300
+ var delInfo = 0;
271
301
 
272
302
  for (var i = this.from; i < this.to; i++) {
273
303
  var map = this.maps[i],
@@ -283,11 +313,11 @@ var Mapping = function () {
283
313
  }
284
314
  }
285
315
 
286
- if (result.deleted) deleted = true;
316
+ delInfo |= result.delInfo;
287
317
  pos = result.pos;
288
318
  }
289
319
 
290
- return simple ? pos : new MapResult(pos, deleted);
320
+ return simple ? pos : new MapResult(pos, delInfo, null);
291
321
  }
292
322
  }]);
293
323
 
@@ -562,7 +592,7 @@ var ReplaceStep = function (_Step3) {
562
592
  value: function map(mapping) {
563
593
  var from = mapping.mapResult(this.from, 1),
564
594
  to = mapping.mapResult(this.to, -1);
565
- if (from.deleted && to.deleted) return null;
595
+ if (from.deletedAcross && to.deletedAcross) return null;
566
596
  return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice);
567
597
  }
568
598
  }, {
@@ -657,7 +687,7 @@ var ReplaceAroundStep = function (_Step4) {
657
687
  to = mapping.mapResult(this.to, -1);
658
688
  var gapFrom = mapping.map(this.gapFrom, -1),
659
689
  gapTo = mapping.map(this.gapTo, 1);
660
- if (from.deleted && to.deleted || gapFrom < from.pos || gapTo > to.pos) return null;
690
+ if (from.deletedAcross && to.deletedAcross || gapFrom < from.pos || gapTo > to.pos) return null;
661
691
  return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure);
662
692
  }
663
693
  }, {
package/dist/index.d.ts CHANGED
@@ -32,10 +32,25 @@ declare class MapResult {
32
32
  */
33
33
  readonly pos: number;
34
34
  /**
35
- Tells you whether the position was deleted, that is, whether
36
- the step removed its surroundings from the document.
35
+ Tells you whether the position was deleted, that is, whether the
36
+ step removed the token on the side queried (via the `assoc`)
37
+ argument from the document.
37
38
  */
38
- readonly deleted: boolean;
39
+ get deleted(): boolean;
40
+ /**
41
+ Tells you whether the token before the mapped position was deleted.
42
+ */
43
+ get deletedBefore(): boolean;
44
+ /**
45
+ True when the token after the mapped position was deleted.
46
+ */
47
+ get deletedAfter(): boolean;
48
+ /**
49
+ Tells whether any of the steps mapped through deletes across the
50
+ position (including both the token before and after the
51
+ position).
52
+ */
53
+ get deletedAcross(): boolean;
39
54
  }
40
55
  /**
41
56
  A map describing the deletions and insertions made by a step, which
package/dist/index.js CHANGED
@@ -14,6 +14,7 @@ const factor16 = Math.pow(2, 16);
14
14
  function makeRecover(index, offset) { return index + offset * factor16; }
15
15
  function recoverIndex(value) { return value & lower16; }
16
16
  function recoverOffset(value) { return (value - (value & lower16)) / factor16; }
17
+ const DEL_BEFORE = 1, DEL_AFTER = 2, DEL_ACROSS = 4, DEL_SIDE = 8;
17
18
  /**
18
19
  An object representing a mapped position with extra
19
20
  information.
@@ -28,18 +29,37 @@ class MapResult {
28
29
  */
29
30
  pos,
30
31
  /**
31
- Tells you whether the position was deleted, that is, whether
32
- the step removed its surroundings from the document.
32
+ @internal
33
33
  */
34
- deleted = false,
34
+ delInfo,
35
35
  /**
36
36
  @internal
37
37
  */
38
- recover = null) {
38
+ recover) {
39
39
  this.pos = pos;
40
- this.deleted = deleted;
40
+ this.delInfo = delInfo;
41
41
  this.recover = recover;
42
42
  }
43
+ /**
44
+ Tells you whether the position was deleted, that is, whether the
45
+ step removed the token on the side queried (via the `assoc`)
46
+ argument from the document.
47
+ */
48
+ get deleted() { return (this.delInfo & DEL_SIDE) > 0; }
49
+ /**
50
+ Tells you whether the token before the mapped position was deleted.
51
+ */
52
+ get deletedBefore() { return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0; }
53
+ /**
54
+ True when the token after the mapped position was deleted.
55
+ */
56
+ get deletedAfter() { return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0; }
57
+ /**
58
+ Tells whether any of the steps mapped through deletes across the
59
+ position (including both the token before and after the
60
+ position).
61
+ */
62
+ get deletedAcross() { return (this.delInfo & DEL_ACROSS) > 0; }
43
63
  }
44
64
  /**
45
65
  A map describing the deletions and insertions made by a step, which
@@ -95,11 +115,14 @@ class StepMap {
95
115
  if (simple)
96
116
  return result;
97
117
  let recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
98
- return new MapResult(result, assoc < 0 ? pos != start : pos != end, recover);
118
+ let del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
119
+ if (assoc < 0 ? pos != start : pos != end)
120
+ del |= DEL_SIDE;
121
+ return new MapResult(result, del, recover);
99
122
  }
100
123
  diff += newSize - oldSize;
101
124
  }
102
- return simple ? pos + diff : new MapResult(pos + diff);
125
+ return simple ? pos + diff : new MapResult(pos + diff, 0, null);
103
126
  }
104
127
  /**
105
128
  @internal
@@ -279,7 +302,7 @@ class Mapping {
279
302
  @internal
280
303
  */
281
304
  _map(pos, assoc, simple) {
282
- let deleted = false;
305
+ let delInfo = 0;
283
306
  for (let i = this.from; i < this.to; i++) {
284
307
  let map = this.maps[i], result = map.mapResult(pos, assoc);
285
308
  if (result.recover != null) {
@@ -290,11 +313,10 @@ class Mapping {
290
313
  continue;
291
314
  }
292
315
  }
293
- if (result.deleted)
294
- deleted = true;
316
+ delInfo |= result.delInfo;
295
317
  pos = result.pos;
296
318
  }
297
- return simple ? pos : new MapResult(pos, deleted);
319
+ return simple ? pos : new MapResult(pos, delInfo, null);
298
320
  }
299
321
  }
300
322
 
@@ -583,7 +605,7 @@ class ReplaceStep extends Step {
583
605
  }
584
606
  map(mapping) {
585
607
  let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
586
- if (from.deleted && to.deleted)
608
+ if (from.deletedAcross && to.deletedAcross)
587
609
  return null;
588
610
  return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice);
589
611
  }
@@ -696,7 +718,7 @@ class ReplaceAroundStep extends Step {
696
718
  map(mapping) {
697
719
  let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1);
698
720
  let gapFrom = mapping.map(this.gapFrom, -1), gapTo = mapping.map(this.gapTo, 1);
699
- if ((from.deleted && to.deleted) || gapFrom < from.pos || gapTo > to.pos)
721
+ if ((from.deletedAcross && to.deletedAcross) || gapFrom < from.pos || gapTo > to.pos)
700
722
  return null;
701
723
  return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure);
702
724
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prosemirror-transform",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "ProseMirror document transformations",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
package/src/map.ts CHANGED
@@ -33,6 +33,8 @@ function makeRecover(index: number, offset: number) { return index + offset * fa
33
33
  function recoverIndex(value: number) { return value & lower16 }
34
34
  function recoverOffset(value: number) { return (value - (value & lower16)) / factor16 }
35
35
 
36
+ const DEL_BEFORE = 1, DEL_AFTER = 2, DEL_ACROSS = 4, DEL_SIDE = 8
37
+
36
38
  /// An object representing a mapped position with extra
37
39
  /// information.
38
40
  export class MapResult {
@@ -40,12 +42,27 @@ export class MapResult {
40
42
  constructor(
41
43
  /// The mapped version of the position.
42
44
  readonly pos: number,
43
- /// Tells you whether the position was deleted, that is, whether
44
- /// the step removed its surroundings from the document.
45
- readonly deleted: boolean = false,
46
45
  /// @internal
47
- readonly recover: number | null = null
46
+ readonly delInfo: number,
47
+ /// @internal
48
+ readonly recover: number | null
48
49
  ) {}
50
+
51
+ /// Tells you whether the position was deleted, that is, whether the
52
+ /// step removed the token on the side queried (via the `assoc`)
53
+ /// argument from the document.
54
+ get deleted() { return (this.delInfo & DEL_SIDE) > 0 }
55
+
56
+ /// Tells you whether the token before the mapped position was deleted.
57
+ get deletedBefore() { return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0 }
58
+
59
+ /// True when the token after the mapped position was deleted.
60
+ get deletedAfter() { return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0 }
61
+
62
+ /// Tells whether any of the steps mapped through deletes across the
63
+ /// position (including both the token before and after the
64
+ /// position).
65
+ get deletedAcross() { return (this.delInfo & DEL_ACROSS) > 0 }
49
66
  }
50
67
 
51
68
  /// A map describing the deletions and insertions made by a step, which
@@ -89,11 +106,13 @@ export class StepMap implements Mappable {
89
106
  let result = start + diff + (side < 0 ? 0 : newSize)
90
107
  if (simple) return result
91
108
  let recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start)
92
- return new MapResult(result, assoc < 0 ? pos != start : pos != end, recover)
109
+ let del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS
110
+ if (assoc < 0 ? pos != start : pos != end) del |= DEL_SIDE
111
+ return new MapResult(result, del, recover)
93
112
  }
94
113
  diff += newSize - oldSize
95
114
  }
96
- return simple ? pos + diff : new MapResult(pos + diff)
115
+ return simple ? pos + diff : new MapResult(pos + diff, 0, null)
97
116
  }
98
117
 
99
118
  /// @internal
@@ -234,7 +253,7 @@ export class Mapping implements Mappable {
234
253
 
235
254
  /// @internal
236
255
  _map(pos: number, assoc: number, simple: boolean) {
237
- let deleted = false
256
+ let delInfo = 0
238
257
 
239
258
  for (let i = this.from; i < this.to; i++) {
240
259
  let map = this.maps[i], result = map.mapResult(pos, assoc)
@@ -247,10 +266,10 @@ export class Mapping implements Mappable {
247
266
  }
248
267
  }
249
268
 
250
- if (result.deleted) deleted = true
269
+ delInfo |= result.delInfo
251
270
  pos = result.pos
252
271
  }
253
272
 
254
- return simple ? pos : new MapResult(pos, deleted)
273
+ return simple ? pos : new MapResult(pos, delInfo, null)
255
274
  }
256
275
  }
@@ -41,7 +41,7 @@ export class ReplaceStep extends Step {
41
41
 
42
42
  map(mapping: Mappable) {
43
43
  let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1)
44
- if (from.deleted && to.deleted) return null
44
+ if (from.deletedAcross && to.deletedAcross) return null
45
45
  return new ReplaceStep(from.pos, Math.max(from.pos, to.pos), this.slice)
46
46
  }
47
47
 
@@ -135,7 +135,7 @@ export class ReplaceAroundStep extends Step {
135
135
  map(mapping: Mappable) {
136
136
  let from = mapping.mapResult(this.from, 1), to = mapping.mapResult(this.to, -1)
137
137
  let gapFrom = mapping.map(this.gapFrom, -1), gapTo = mapping.map(this.gapTo, 1)
138
- if ((from.deleted && to.deleted) || gapFrom < from.pos || gapTo > to.pos) return null
138
+ if ((from.deletedAcross && to.deletedAcross) || gapFrom < from.pos || gapTo > to.pos) return null
139
139
  return new ReplaceAroundStep(from.pos, to.pos, gapFrom, gapTo, this.slice, this.insert, this.structure)
140
140
  }
141
141