@storyteller-platform/align 0.1.9 → 0.1.11

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.
Files changed (113) hide show
  1. package/dist/align/__tests__/align.test.cjs +6 -5
  2. package/dist/align/__tests__/align.test.js +6 -5
  3. package/dist/align/align.cjs +133 -81
  4. package/dist/align/align.d.cts +1 -0
  5. package/dist/align/align.d.ts +1 -0
  6. package/dist/align/align.js +133 -81
  7. package/dist/align/getSentenceRanges.cjs +78 -149
  8. package/dist/align/getSentenceRanges.d.cts +1 -1
  9. package/dist/align/getSentenceRanges.d.ts +1 -1
  10. package/dist/align/getSentenceRanges.js +78 -149
  11. package/dist/align/slugify.cjs +16 -8
  12. package/dist/align/slugify.js +16 -8
  13. package/dist/errorAlign/__tests__/errorAlign.test.cjs +100 -0
  14. package/dist/errorAlign/__tests__/errorAlign.test.d.cts +2 -0
  15. package/dist/errorAlign/__tests__/errorAlign.test.d.ts +2 -0
  16. package/dist/errorAlign/__tests__/errorAlign.test.js +77 -0
  17. package/dist/errorAlign/__tests__/native.test.cjs +118 -0
  18. package/dist/errorAlign/__tests__/native.test.d.cts +2 -0
  19. package/dist/errorAlign/__tests__/native.test.d.ts +2 -0
  20. package/dist/errorAlign/__tests__/native.test.js +107 -0
  21. package/dist/errorAlign/backtraceGraph.cjs +298 -0
  22. package/dist/errorAlign/backtraceGraph.d.cts +103 -0
  23. package/dist/errorAlign/backtraceGraph.d.ts +103 -0
  24. package/dist/errorAlign/backtraceGraph.js +270 -0
  25. package/dist/errorAlign/beamSearch.cjs +302 -0
  26. package/dist/errorAlign/beamSearch.d.cts +53 -0
  27. package/dist/errorAlign/beamSearch.d.ts +53 -0
  28. package/dist/errorAlign/beamSearch.js +268 -0
  29. package/dist/errorAlign/core.cjs +33 -0
  30. package/dist/errorAlign/core.d.cts +5 -0
  31. package/dist/errorAlign/core.d.ts +5 -0
  32. package/dist/errorAlign/core.js +11 -0
  33. package/dist/errorAlign/editDistance.cjs +115 -0
  34. package/dist/errorAlign/editDistance.d.cts +46 -0
  35. package/dist/errorAlign/editDistance.d.ts +46 -0
  36. package/dist/errorAlign/editDistance.js +90 -0
  37. package/dist/errorAlign/errorAlign.cjs +159 -0
  38. package/dist/errorAlign/errorAlign.d.cts +15 -0
  39. package/dist/errorAlign/errorAlign.d.ts +15 -0
  40. package/dist/errorAlign/errorAlign.js +145 -0
  41. package/dist/errorAlign/graphMetadata.cjs +97 -0
  42. package/dist/errorAlign/graphMetadata.d.cts +44 -0
  43. package/dist/errorAlign/graphMetadata.d.ts +44 -0
  44. package/dist/errorAlign/graphMetadata.js +64 -0
  45. package/dist/errorAlign/hash.cjs +173 -0
  46. package/dist/errorAlign/hash.d.cts +28 -0
  47. package/dist/errorAlign/hash.d.ts +28 -0
  48. package/dist/errorAlign/hash.js +150 -0
  49. package/dist/errorAlign/native.cjs +60 -0
  50. package/dist/errorAlign/native.d.cts +18 -0
  51. package/dist/errorAlign/native.d.ts +18 -0
  52. package/dist/errorAlign/native.js +24 -0
  53. package/dist/errorAlign/node-gyp-build.d.cjs +1 -0
  54. package/dist/errorAlign/node-gyp-build.d.d.cts +3 -0
  55. package/dist/errorAlign/node-gyp-build.d.d.ts +3 -0
  56. package/dist/errorAlign/node-gyp-build.d.js +0 -0
  57. package/dist/errorAlign/pathToAlignment.cjs +122 -0
  58. package/dist/errorAlign/pathToAlignment.d.cts +11 -0
  59. package/dist/errorAlign/pathToAlignment.d.ts +11 -0
  60. package/dist/errorAlign/pathToAlignment.js +89 -0
  61. package/dist/errorAlign/utils.cjs +301 -0
  62. package/dist/errorAlign/utils.d.cts +107 -0
  63. package/dist/errorAlign/utils.d.ts +107 -0
  64. package/dist/errorAlign/utils.js +248 -0
  65. package/dist/index.d.cts +1 -0
  66. package/dist/index.d.ts +1 -0
  67. package/dist/markup/__tests__/markup.test.cjs +108 -81
  68. package/dist/markup/__tests__/markup.test.js +109 -82
  69. package/dist/markup/__tests__/parseDom.test.cjs +112 -0
  70. package/dist/markup/__tests__/parseDom.test.d.cts +2 -0
  71. package/dist/markup/__tests__/parseDom.test.d.ts +2 -0
  72. package/dist/markup/__tests__/parseDom.test.js +89 -0
  73. package/dist/markup/__tests__/serializeDom.test.cjs +120 -0
  74. package/dist/markup/__tests__/serializeDom.test.d.cts +2 -0
  75. package/dist/markup/__tests__/serializeDom.test.d.ts +2 -0
  76. package/dist/markup/__tests__/serializeDom.test.js +97 -0
  77. package/dist/markup/__tests__/transform.test.cjs +122 -0
  78. package/dist/markup/__tests__/transform.test.d.cts +2 -0
  79. package/dist/markup/__tests__/transform.test.d.ts +2 -0
  80. package/dist/markup/__tests__/transform.test.js +99 -0
  81. package/dist/markup/map.cjs +261 -0
  82. package/dist/markup/map.d.cts +50 -0
  83. package/dist/markup/map.d.ts +50 -0
  84. package/dist/markup/map.js +236 -0
  85. package/dist/markup/markup.cjs +23 -201
  86. package/dist/markup/markup.d.cts +5 -9
  87. package/dist/markup/markup.d.ts +5 -9
  88. package/dist/markup/markup.js +24 -203
  89. package/dist/markup/model.cjs +172 -0
  90. package/dist/markup/model.d.cts +57 -0
  91. package/dist/markup/model.d.ts +57 -0
  92. package/dist/markup/model.js +145 -0
  93. package/dist/markup/parseDom.cjs +59 -0
  94. package/dist/markup/parseDom.d.cts +7 -0
  95. package/dist/markup/parseDom.d.ts +7 -0
  96. package/dist/markup/parseDom.js +35 -0
  97. package/dist/markup/segmentation.cjs +11 -57
  98. package/dist/markup/segmentation.d.cts +6 -2
  99. package/dist/markup/segmentation.d.ts +6 -2
  100. package/dist/markup/segmentation.js +11 -58
  101. package/dist/markup/serializeDom.cjs +87 -0
  102. package/dist/markup/serializeDom.d.cts +7 -0
  103. package/dist/markup/serializeDom.d.ts +7 -0
  104. package/dist/markup/serializeDom.js +63 -0
  105. package/dist/markup/transform.cjs +92 -0
  106. package/dist/markup/transform.d.cts +11 -0
  107. package/dist/markup/transform.d.ts +11 -0
  108. package/dist/markup/transform.js +71 -0
  109. package/dist/types/node-gyp-build.d.cjs +1 -0
  110. package/dist/types/node-gyp-build.d.d.cts +3 -0
  111. package/dist/types/node-gyp-build.d.d.ts +3 -0
  112. package/dist/types/node-gyp-build.d.js +0 -0
  113. package/package.json +11 -4
@@ -0,0 +1,261 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var map_exports = {};
20
+ __export(map_exports, {
21
+ MapResult: () => MapResult,
22
+ Mapping: () => Mapping,
23
+ StepMap: () => StepMap
24
+ });
25
+ module.exports = __toCommonJS(map_exports);
26
+ const lower16 = 65535;
27
+ const factor16 = Math.pow(2, 16);
28
+ function makeRecover(index, offset) {
29
+ return index + offset * factor16;
30
+ }
31
+ function recoverIndex(value) {
32
+ return value & lower16;
33
+ }
34
+ function recoverOffset(value) {
35
+ return (value - (value & lower16)) / factor16;
36
+ }
37
+ const DEL_BEFORE = 1, DEL_AFTER = 2, DEL_ACROSS = 4, DEL_SIDE = 8;
38
+ class MapResult {
39
+ /// @internal
40
+ constructor(pos, delInfo, recover) {
41
+ this.pos = pos;
42
+ this.delInfo = delInfo;
43
+ this.recover = recover;
44
+ }
45
+ /// Tells you whether the position was deleted, that is, whether the
46
+ /// step removed the token on the side queried (via the `assoc`)
47
+ /// argument from the document.
48
+ get deleted() {
49
+ return (this.delInfo & DEL_SIDE) > 0;
50
+ }
51
+ /// Tells you whether the token before the mapped position was deleted.
52
+ get deletedBefore() {
53
+ return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0;
54
+ }
55
+ /// True when the token after the mapped position was deleted.
56
+ get deletedAfter() {
57
+ return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0;
58
+ }
59
+ /// Tells whether any of the steps mapped through deletes across the
60
+ /// position (including both the token before and after the
61
+ /// position).
62
+ get deletedAcross() {
63
+ return (this.delInfo & DEL_ACROSS) > 0;
64
+ }
65
+ }
66
+ class StepMap {
67
+ /// Create a position map. The modifications to the document are
68
+ /// represented as an array of numbers, in which each group of three
69
+ /// represents a modified chunk as `[start, oldSize, newSize]`.
70
+ constructor(ranges, inverted = false) {
71
+ this.ranges = ranges;
72
+ this.inverted = inverted;
73
+ if (!ranges.length) return StepMap.empty;
74
+ }
75
+ /// @internal
76
+ recover(value) {
77
+ let diff = 0, index = recoverIndex(value);
78
+ if (!this.inverted)
79
+ for (let i = 0; i < index; i++)
80
+ diff += this.ranges[i * 3 + 2] - this.ranges[i * 3 + 1];
81
+ return this.ranges[index * 3] + diff + recoverOffset(value);
82
+ }
83
+ mapResult(pos, assoc = 1) {
84
+ return this._map(pos, assoc, false);
85
+ }
86
+ map(pos, assoc = 1) {
87
+ return this._map(pos, assoc, true);
88
+ }
89
+ /// @internal
90
+ _map(pos, assoc, simple) {
91
+ let diff = 0, oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
92
+ for (let i = 0; i < this.ranges.length; i += 3) {
93
+ let start = this.ranges[i] - (this.inverted ? diff : 0);
94
+ if (start > pos) break;
95
+ let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex], end = start + oldSize;
96
+ if (pos <= end) {
97
+ let side = !oldSize ? assoc : pos == start ? -1 : pos == end ? 1 : assoc;
98
+ let result = start + diff + (side < 0 ? 0 : newSize);
99
+ if (simple) return result;
100
+ let recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
101
+ let del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
102
+ if (assoc < 0 ? pos != start : pos != end) del |= DEL_SIDE;
103
+ return new MapResult(result, del, recover);
104
+ }
105
+ diff += newSize - oldSize;
106
+ }
107
+ return simple ? pos + diff : new MapResult(pos + diff, 0, null);
108
+ }
109
+ /// @internal
110
+ touches(pos, recover) {
111
+ let diff = 0, index = recoverIndex(recover);
112
+ let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
113
+ for (let i = 0; i < this.ranges.length; i += 3) {
114
+ let start = this.ranges[i] - (this.inverted ? diff : 0);
115
+ if (start > pos) break;
116
+ let oldSize = this.ranges[i + oldIndex], end = start + oldSize;
117
+ if (pos <= end && i == index * 3) return true;
118
+ diff += this.ranges[i + newIndex] - oldSize;
119
+ }
120
+ return false;
121
+ }
122
+ /// Calls the given function on each of the changed ranges included in
123
+ /// this map.
124
+ forEach(f) {
125
+ let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
126
+ for (let i = 0, diff = 0; i < this.ranges.length; i += 3) {
127
+ let start = this.ranges[i], oldStart = start - (this.inverted ? diff : 0), newStart = start + (this.inverted ? 0 : diff);
128
+ let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex];
129
+ f(oldStart, oldStart + oldSize, newStart, newStart + newSize);
130
+ diff += newSize - oldSize;
131
+ }
132
+ }
133
+ /// Create an inverted version of this map. The result can be used to
134
+ /// map positions in the post-step document to the pre-step document.
135
+ invert() {
136
+ return new StepMap(this.ranges, !this.inverted);
137
+ }
138
+ /// @internal
139
+ toString() {
140
+ return (this.inverted ? "-" : "") + JSON.stringify(this.ranges);
141
+ }
142
+ /// Create a map that moves all positions by offset `n` (which may be
143
+ /// negative). This can be useful when applying steps meant for a
144
+ /// sub-document to a larger document, or vice-versa.
145
+ static offset(n) {
146
+ return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n]);
147
+ }
148
+ /// A StepMap that contains no changed ranges.
149
+ static empty = new StepMap([]);
150
+ }
151
+ class Mapping {
152
+ /// Create a new mapping with the given position maps.
153
+ constructor(maps, mirror, from = 0, to = maps ? maps.length : 0) {
154
+ this.mirror = mirror;
155
+ this.from = from;
156
+ this.to = to;
157
+ this._maps = maps || [];
158
+ this.ownData = !(maps || mirror);
159
+ }
160
+ /// The step maps in this mapping.
161
+ get maps() {
162
+ return this._maps;
163
+ }
164
+ _maps;
165
+ // False if maps/mirror are shared arrays that we shouldn't mutate
166
+ ownData;
167
+ /// Create a mapping that maps only through a part of this one.
168
+ slice(from = 0, to = this.maps.length) {
169
+ return new Mapping(this._maps, this.mirror, from, to);
170
+ }
171
+ /// Add a step map to the end of this mapping. If `mirrors` is
172
+ /// given, it should be the index of the step map that is the mirror
173
+ /// image of this one.
174
+ appendMap(map, mirrors) {
175
+ if (!this.ownData) {
176
+ this._maps = this._maps.slice();
177
+ this.mirror = this.mirror && this.mirror.slice();
178
+ this.ownData = true;
179
+ }
180
+ this.to = this._maps.push(map);
181
+ if (mirrors != null) this.setMirror(this._maps.length - 1, mirrors);
182
+ }
183
+ /// Add all the step maps in a given mapping to this one (preserving
184
+ /// mirroring information).
185
+ appendMapping(mapping) {
186
+ for (let i = 0, startSize = this._maps.length; i < mapping._maps.length; i++) {
187
+ let mirr = mapping.getMirror(i);
188
+ this.appendMap(
189
+ mapping._maps[i],
190
+ mirr != null && mirr < i ? startSize + mirr : void 0
191
+ );
192
+ }
193
+ }
194
+ /// Finds the offset of the step map that mirrors the map at the
195
+ /// given offset, in this mapping (as per the second argument to
196
+ /// `appendMap`).
197
+ getMirror(n) {
198
+ if (this.mirror) {
199
+ for (let i = 0; i < this.mirror.length; i++)
200
+ if (this.mirror[i] == n) return this.mirror[i + (i % 2 ? -1 : 1)];
201
+ }
202
+ return void 0;
203
+ }
204
+ /// @internal
205
+ setMirror(n, m) {
206
+ if (!this.mirror) this.mirror = [];
207
+ this.mirror.push(n, m);
208
+ }
209
+ /// Append the inverse of the given mapping to this one.
210
+ appendMappingInverted(mapping) {
211
+ for (let i = mapping.maps.length - 1, totalSize = this._maps.length + mapping._maps.length; i >= 0; i--) {
212
+ let mirr = mapping.getMirror(i);
213
+ this.appendMap(
214
+ mapping._maps[i].invert(),
215
+ mirr != null && mirr > i ? totalSize - mirr - 1 : void 0
216
+ );
217
+ }
218
+ }
219
+ /// Create an inverted version of this mapping.
220
+ invert() {
221
+ let inverse = new Mapping();
222
+ inverse.appendMappingInverted(this);
223
+ return inverse;
224
+ }
225
+ /// Map a position through this mapping.
226
+ map(pos, assoc = 1) {
227
+ if (this.mirror) return this._map(pos, assoc, true);
228
+ for (let i = this.from; i < this.to; i++)
229
+ pos = this._maps[i].map(pos, assoc);
230
+ return pos;
231
+ }
232
+ /// Map a position through this mapping, returning a mapping
233
+ /// result.
234
+ mapResult(pos, assoc = 1) {
235
+ return this._map(pos, assoc, false);
236
+ }
237
+ /// @internal
238
+ _map(pos, assoc, simple) {
239
+ let delInfo = 0;
240
+ for (let i = this.from; i < this.to; i++) {
241
+ let map = this._maps[i], result = map.mapResult(pos, assoc);
242
+ if (result.recover != null) {
243
+ let corr = this.getMirror(i);
244
+ if (corr != null && corr > i && corr < this.to) {
245
+ i = corr;
246
+ pos = this._maps[corr].recover(result.recover);
247
+ continue;
248
+ }
249
+ }
250
+ delInfo |= result.delInfo;
251
+ pos = result.pos;
252
+ }
253
+ return simple ? pos : new MapResult(pos, delInfo, null);
254
+ }
255
+ }
256
+ // Annotate the CommonJS export names for ESM import in node:
257
+ 0 && (module.exports = {
258
+ MapResult,
259
+ Mapping,
260
+ StepMap
261
+ });
@@ -0,0 +1,50 @@
1
+ interface Mappable {
2
+ map: (pos: number, assoc?: number) => number;
3
+ mapResult: (pos: number, assoc?: number) => MapResult;
4
+ }
5
+ declare class MapResult {
6
+ readonly pos: number;
7
+ readonly delInfo: number;
8
+ readonly recover: number | null;
9
+ constructor(pos: number, delInfo: number, recover: number | null);
10
+ get deleted(): boolean;
11
+ get deletedBefore(): boolean;
12
+ get deletedAfter(): boolean;
13
+ get deletedAcross(): boolean;
14
+ }
15
+ declare class StepMap implements Mappable {
16
+ readonly ranges: readonly number[];
17
+ readonly inverted: boolean;
18
+ constructor(ranges: readonly number[], inverted?: boolean);
19
+ recover(value: number): number;
20
+ mapResult(pos: number, assoc?: number): MapResult;
21
+ map(pos: number, assoc?: number): number;
22
+ _map(pos: number, assoc: number, simple: boolean): number | MapResult;
23
+ touches(pos: number, recover: number): boolean;
24
+ forEach(f: (oldStart: number, oldEnd: number, newStart: number, newEnd: number) => void): void;
25
+ invert(): StepMap;
26
+ toString(): string;
27
+ static offset(n: number): StepMap;
28
+ static empty: StepMap;
29
+ }
30
+ declare class Mapping implements Mappable {
31
+ mirror?: number[] | undefined;
32
+ from: number;
33
+ to: number;
34
+ constructor(maps?: readonly StepMap[], mirror?: number[] | undefined, from?: number, to?: number);
35
+ get maps(): readonly StepMap[];
36
+ private _maps;
37
+ private ownData;
38
+ slice(from?: number, to?: number): Mapping;
39
+ appendMap(map: StepMap, mirrors?: number): void;
40
+ appendMapping(mapping: Mapping): void;
41
+ getMirror(n: number): number | undefined;
42
+ setMirror(n: number, m: number): void;
43
+ appendMappingInverted(mapping: Mapping): void;
44
+ invert(): Mapping;
45
+ map(pos: number, assoc?: number): number;
46
+ mapResult(pos: number, assoc?: number): MapResult;
47
+ _map(pos: number, assoc: number, simple: boolean): number | MapResult;
48
+ }
49
+
50
+ export { MapResult, type Mappable, Mapping, StepMap };
@@ -0,0 +1,50 @@
1
+ interface Mappable {
2
+ map: (pos: number, assoc?: number) => number;
3
+ mapResult: (pos: number, assoc?: number) => MapResult;
4
+ }
5
+ declare class MapResult {
6
+ readonly pos: number;
7
+ readonly delInfo: number;
8
+ readonly recover: number | null;
9
+ constructor(pos: number, delInfo: number, recover: number | null);
10
+ get deleted(): boolean;
11
+ get deletedBefore(): boolean;
12
+ get deletedAfter(): boolean;
13
+ get deletedAcross(): boolean;
14
+ }
15
+ declare class StepMap implements Mappable {
16
+ readonly ranges: readonly number[];
17
+ readonly inverted: boolean;
18
+ constructor(ranges: readonly number[], inverted?: boolean);
19
+ recover(value: number): number;
20
+ mapResult(pos: number, assoc?: number): MapResult;
21
+ map(pos: number, assoc?: number): number;
22
+ _map(pos: number, assoc: number, simple: boolean): number | MapResult;
23
+ touches(pos: number, recover: number): boolean;
24
+ forEach(f: (oldStart: number, oldEnd: number, newStart: number, newEnd: number) => void): void;
25
+ invert(): StepMap;
26
+ toString(): string;
27
+ static offset(n: number): StepMap;
28
+ static empty: StepMap;
29
+ }
30
+ declare class Mapping implements Mappable {
31
+ mirror?: number[] | undefined;
32
+ from: number;
33
+ to: number;
34
+ constructor(maps?: readonly StepMap[], mirror?: number[] | undefined, from?: number, to?: number);
35
+ get maps(): readonly StepMap[];
36
+ private _maps;
37
+ private ownData;
38
+ slice(from?: number, to?: number): Mapping;
39
+ appendMap(map: StepMap, mirrors?: number): void;
40
+ appendMapping(mapping: Mapping): void;
41
+ getMirror(n: number): number | undefined;
42
+ setMirror(n: number, m: number): void;
43
+ appendMappingInverted(mapping: Mapping): void;
44
+ invert(): Mapping;
45
+ map(pos: number, assoc?: number): number;
46
+ mapResult(pos: number, assoc?: number): MapResult;
47
+ _map(pos: number, assoc: number, simple: boolean): number | MapResult;
48
+ }
49
+
50
+ export { MapResult, type Mappable, Mapping, StepMap };
@@ -0,0 +1,236 @@
1
+ import "../chunk-BIEQXUOY.js";
2
+ const lower16 = 65535;
3
+ const factor16 = Math.pow(2, 16);
4
+ function makeRecover(index, offset) {
5
+ return index + offset * factor16;
6
+ }
7
+ function recoverIndex(value) {
8
+ return value & lower16;
9
+ }
10
+ function recoverOffset(value) {
11
+ return (value - (value & lower16)) / factor16;
12
+ }
13
+ const DEL_BEFORE = 1, DEL_AFTER = 2, DEL_ACROSS = 4, DEL_SIDE = 8;
14
+ class MapResult {
15
+ /// @internal
16
+ constructor(pos, delInfo, recover) {
17
+ this.pos = pos;
18
+ this.delInfo = delInfo;
19
+ this.recover = recover;
20
+ }
21
+ /// Tells you whether the position was deleted, that is, whether the
22
+ /// step removed the token on the side queried (via the `assoc`)
23
+ /// argument from the document.
24
+ get deleted() {
25
+ return (this.delInfo & DEL_SIDE) > 0;
26
+ }
27
+ /// Tells you whether the token before the mapped position was deleted.
28
+ get deletedBefore() {
29
+ return (this.delInfo & (DEL_BEFORE | DEL_ACROSS)) > 0;
30
+ }
31
+ /// True when the token after the mapped position was deleted.
32
+ get deletedAfter() {
33
+ return (this.delInfo & (DEL_AFTER | DEL_ACROSS)) > 0;
34
+ }
35
+ /// Tells whether any of the steps mapped through deletes across the
36
+ /// position (including both the token before and after the
37
+ /// position).
38
+ get deletedAcross() {
39
+ return (this.delInfo & DEL_ACROSS) > 0;
40
+ }
41
+ }
42
+ class StepMap {
43
+ /// Create a position map. The modifications to the document are
44
+ /// represented as an array of numbers, in which each group of three
45
+ /// represents a modified chunk as `[start, oldSize, newSize]`.
46
+ constructor(ranges, inverted = false) {
47
+ this.ranges = ranges;
48
+ this.inverted = inverted;
49
+ if (!ranges.length) return StepMap.empty;
50
+ }
51
+ /// @internal
52
+ recover(value) {
53
+ let diff = 0, index = recoverIndex(value);
54
+ if (!this.inverted)
55
+ for (let i = 0; i < index; i++)
56
+ diff += this.ranges[i * 3 + 2] - this.ranges[i * 3 + 1];
57
+ return this.ranges[index * 3] + diff + recoverOffset(value);
58
+ }
59
+ mapResult(pos, assoc = 1) {
60
+ return this._map(pos, assoc, false);
61
+ }
62
+ map(pos, assoc = 1) {
63
+ return this._map(pos, assoc, true);
64
+ }
65
+ /// @internal
66
+ _map(pos, assoc, simple) {
67
+ let diff = 0, oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
68
+ for (let i = 0; i < this.ranges.length; i += 3) {
69
+ let start = this.ranges[i] - (this.inverted ? diff : 0);
70
+ if (start > pos) break;
71
+ let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex], end = start + oldSize;
72
+ if (pos <= end) {
73
+ let side = !oldSize ? assoc : pos == start ? -1 : pos == end ? 1 : assoc;
74
+ let result = start + diff + (side < 0 ? 0 : newSize);
75
+ if (simple) return result;
76
+ let recover = pos == (assoc < 0 ? start : end) ? null : makeRecover(i / 3, pos - start);
77
+ let del = pos == start ? DEL_AFTER : pos == end ? DEL_BEFORE : DEL_ACROSS;
78
+ if (assoc < 0 ? pos != start : pos != end) del |= DEL_SIDE;
79
+ return new MapResult(result, del, recover);
80
+ }
81
+ diff += newSize - oldSize;
82
+ }
83
+ return simple ? pos + diff : new MapResult(pos + diff, 0, null);
84
+ }
85
+ /// @internal
86
+ touches(pos, recover) {
87
+ let diff = 0, index = recoverIndex(recover);
88
+ let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
89
+ for (let i = 0; i < this.ranges.length; i += 3) {
90
+ let start = this.ranges[i] - (this.inverted ? diff : 0);
91
+ if (start > pos) break;
92
+ let oldSize = this.ranges[i + oldIndex], end = start + oldSize;
93
+ if (pos <= end && i == index * 3) return true;
94
+ diff += this.ranges[i + newIndex] - oldSize;
95
+ }
96
+ return false;
97
+ }
98
+ /// Calls the given function on each of the changed ranges included in
99
+ /// this map.
100
+ forEach(f) {
101
+ let oldIndex = this.inverted ? 2 : 1, newIndex = this.inverted ? 1 : 2;
102
+ for (let i = 0, diff = 0; i < this.ranges.length; i += 3) {
103
+ let start = this.ranges[i], oldStart = start - (this.inverted ? diff : 0), newStart = start + (this.inverted ? 0 : diff);
104
+ let oldSize = this.ranges[i + oldIndex], newSize = this.ranges[i + newIndex];
105
+ f(oldStart, oldStart + oldSize, newStart, newStart + newSize);
106
+ diff += newSize - oldSize;
107
+ }
108
+ }
109
+ /// Create an inverted version of this map. The result can be used to
110
+ /// map positions in the post-step document to the pre-step document.
111
+ invert() {
112
+ return new StepMap(this.ranges, !this.inverted);
113
+ }
114
+ /// @internal
115
+ toString() {
116
+ return (this.inverted ? "-" : "") + JSON.stringify(this.ranges);
117
+ }
118
+ /// Create a map that moves all positions by offset `n` (which may be
119
+ /// negative). This can be useful when applying steps meant for a
120
+ /// sub-document to a larger document, or vice-versa.
121
+ static offset(n) {
122
+ return n == 0 ? StepMap.empty : new StepMap(n < 0 ? [0, -n, 0] : [0, 0, n]);
123
+ }
124
+ /// A StepMap that contains no changed ranges.
125
+ static empty = new StepMap([]);
126
+ }
127
+ class Mapping {
128
+ /// Create a new mapping with the given position maps.
129
+ constructor(maps, mirror, from = 0, to = maps ? maps.length : 0) {
130
+ this.mirror = mirror;
131
+ this.from = from;
132
+ this.to = to;
133
+ this._maps = maps || [];
134
+ this.ownData = !(maps || mirror);
135
+ }
136
+ /// The step maps in this mapping.
137
+ get maps() {
138
+ return this._maps;
139
+ }
140
+ _maps;
141
+ // False if maps/mirror are shared arrays that we shouldn't mutate
142
+ ownData;
143
+ /// Create a mapping that maps only through a part of this one.
144
+ slice(from = 0, to = this.maps.length) {
145
+ return new Mapping(this._maps, this.mirror, from, to);
146
+ }
147
+ /// Add a step map to the end of this mapping. If `mirrors` is
148
+ /// given, it should be the index of the step map that is the mirror
149
+ /// image of this one.
150
+ appendMap(map, mirrors) {
151
+ if (!this.ownData) {
152
+ this._maps = this._maps.slice();
153
+ this.mirror = this.mirror && this.mirror.slice();
154
+ this.ownData = true;
155
+ }
156
+ this.to = this._maps.push(map);
157
+ if (mirrors != null) this.setMirror(this._maps.length - 1, mirrors);
158
+ }
159
+ /// Add all the step maps in a given mapping to this one (preserving
160
+ /// mirroring information).
161
+ appendMapping(mapping) {
162
+ for (let i = 0, startSize = this._maps.length; i < mapping._maps.length; i++) {
163
+ let mirr = mapping.getMirror(i);
164
+ this.appendMap(
165
+ mapping._maps[i],
166
+ mirr != null && mirr < i ? startSize + mirr : void 0
167
+ );
168
+ }
169
+ }
170
+ /// Finds the offset of the step map that mirrors the map at the
171
+ /// given offset, in this mapping (as per the second argument to
172
+ /// `appendMap`).
173
+ getMirror(n) {
174
+ if (this.mirror) {
175
+ for (let i = 0; i < this.mirror.length; i++)
176
+ if (this.mirror[i] == n) return this.mirror[i + (i % 2 ? -1 : 1)];
177
+ }
178
+ return void 0;
179
+ }
180
+ /// @internal
181
+ setMirror(n, m) {
182
+ if (!this.mirror) this.mirror = [];
183
+ this.mirror.push(n, m);
184
+ }
185
+ /// Append the inverse of the given mapping to this one.
186
+ appendMappingInverted(mapping) {
187
+ for (let i = mapping.maps.length - 1, totalSize = this._maps.length + mapping._maps.length; i >= 0; i--) {
188
+ let mirr = mapping.getMirror(i);
189
+ this.appendMap(
190
+ mapping._maps[i].invert(),
191
+ mirr != null && mirr > i ? totalSize - mirr - 1 : void 0
192
+ );
193
+ }
194
+ }
195
+ /// Create an inverted version of this mapping.
196
+ invert() {
197
+ let inverse = new Mapping();
198
+ inverse.appendMappingInverted(this);
199
+ return inverse;
200
+ }
201
+ /// Map a position through this mapping.
202
+ map(pos, assoc = 1) {
203
+ if (this.mirror) return this._map(pos, assoc, true);
204
+ for (let i = this.from; i < this.to; i++)
205
+ pos = this._maps[i].map(pos, assoc);
206
+ return pos;
207
+ }
208
+ /// Map a position through this mapping, returning a mapping
209
+ /// result.
210
+ mapResult(pos, assoc = 1) {
211
+ return this._map(pos, assoc, false);
212
+ }
213
+ /// @internal
214
+ _map(pos, assoc, simple) {
215
+ let delInfo = 0;
216
+ for (let i = this.from; i < this.to; i++) {
217
+ let map = this._maps[i], result = map.mapResult(pos, assoc);
218
+ if (result.recover != null) {
219
+ let corr = this.getMirror(i);
220
+ if (corr != null && corr > i && corr < this.to) {
221
+ i = corr;
222
+ pos = this._maps[corr].recover(result.recover);
223
+ continue;
224
+ }
225
+ }
226
+ delInfo |= result.delInfo;
227
+ pos = result.pos;
228
+ }
229
+ return simple ? pos : new MapResult(pos, delInfo, null);
230
+ }
231
+ }
232
+ export {
233
+ MapResult,
234
+ Mapping,
235
+ StepMap
236
+ };