forked from OpenPecha/Lopenling-App-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnnotatedText.js
865 lines (806 loc) · 31.4 KB
/
AnnotatedText.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
// @flow
import Annotation, {
ANNOTATION_TYPES,
AnnotationUniqueId,
} from "lib/Annotation";
import SegmentedText from "lib/SegmentedText";
import TextSegment from "lib/TextSegment";
import Witness from "lib/Witness";
import Text from "lib/Text";
import _, { update } from "lodash";
export const BASE_ANNOTATION_ID = -1;
export const WORKING_VERSION_ANNOTATION_ID = -2;
export const INSERTION_KEY = "i";
export const DELETION_KEY = "d";
export const PAGE_BREAK_KEY = "p";
export const LINE_BREAK_KEY = "l";
type Segmenter = (content: string) => TextSegment[];
type AnnotationsByUniqueId = { [id: AnnotationUniqueId]: Annotation };
export default class AnnotatedText {
originalText: SegmentedText;
segmenter: Segmenter | null;
baseWitness: Witness;
activeWitness: Witness;
_generatedText: SegmentedText | null;
/**
* Each key represents the position in the original text the AnnotatedText is
* based upon.
*
* First {number} index is position in current text;
* Second {boolean} is whether original character has been deleted.
*
* */
_orginalCurrentSegmentPositions: {
[position: string | number]: [number, boolean],
} | null;
_currentOriginalSegmentPositions: { [position: number]: number } | null;
_annotations: AnnotationsByUniqueId;
_annotationsByType: { [string]: AnnotationsByUniqueId };
_uniqueId: string | null;
_generatedTextLength: number | null;
_originalTextLength: number | null;
_version: number;
/**
*
* @param originalText -
* @param [annotations]
* @param {segmenter} [segmenter] - segments strings into TextSegments
* @param {Witness} [baseWitness] - witness this text is originally based on
* @param {Witness} [activeWitness] - witness this text is representing
*/
constructor(
originalText: SegmentedText,
annotations: Annotation[] = [],
segmenter: Segmenter | null = null,
baseWitness: Witness,
activeWitness: Witness | null = null
) {
this.originalText = originalText;
this.segmenter = segmenter;
this.baseWitness = baseWitness;
this.activeWitness = activeWitness ? activeWitness : baseWitness;
/** @type {SegmentedText} */
this._generatedText = null;
this._orginalCurrentSegmentPositions = {};
this._currentOriginalSegmentPositions = {};
this._annotationsByType = {};
this._annotations = {};
for (let i = 0; i < annotations.length; i++) {
this.addAnnotation(annotations[i]);
}
this._uniqueId = null;
this._generatedTextLength = null;
this._originalTextLength = null;
this._version = 1;
}
get annotations(): Annotation[] {
return (Object.values(this._annotations): any);
}
get textInfo(): Text {
return this.baseWitness.text;
}
addAnnotation(annotation: Annotation) {
if (!this._annotations.hasOwnProperty(annotation.uniqueId)) {
this._annotations[annotation.uniqueId] = annotation;
this._generatedText = null;
if (!this._annotationsByType.hasOwnProperty(annotation.type)) {
this._annotationsByType[annotation.type] = {};
}
this._annotationsByType[annotation.type][annotation.uniqueId] =
annotation;
this.didMutate();
}
}
removeAnnotation(annotationId: AnnotationUniqueId) {
if (this._annotations.hasOwnProperty(annotationId)) {
const annotation = this._annotations[annotationId];
delete this._annotations[annotationId];
if (this._annotationsByType.hasOwnProperty(annotation.type)) {
if (
this._annotationsByType[annotation.type].hasOwnProperty(
annotationId
)
) {
delete this._annotationsByType[annotation.type][
annotationId
];
}
}
this.didMutate();
}
}
didMutate() {
this.resetUniqueId();
this._version++;
this._orginalCurrentSegmentPositions = null;
this._currentOriginalSegmentPositions = null;
}
get orginalCurrentSegmentPositions(): {
[position: string | number]: [number, boolean],
} {
if (!this._generatedText || !this._orginalCurrentSegmentPositions) {
this.regenerateText();
}
return this._orginalCurrentSegmentPositions || {};
}
get currentOriginalSegmentPositions(): { [position: number]: number } {
if (!this._generatedText || !this._currentOriginalSegmentPositions) {
this.regenerateText();
}
return this._currentOriginalSegmentPositions || {};
}
regenerateText(): SegmentedText {
this._generatedText = this.generateText(
this.originalText,
this.variants
);
return this._generatedText;
}
get version(): number {
return this._version;
}
getAnnotationsOfType(
type: string
): { [AnnotationUniqueId]: Annotation } | null {
if (this._annotationsByType.hasOwnProperty(type)) {
return this._annotationsByType[type];
} else {
return null;
}
}
get variants(): Annotation[] {
let variants = this.getAnnotationsOfType(ANNOTATION_TYPES.variant);
if (!variants) {
variants = [];
} else {
variants = (Object.values(variants): any);
}
return variants;
}
get segmentedText(): SegmentedText {
if (!this._generatedText) {
this._generatedText = this.generateText(
this.originalText,
this.variants
);
}
return this._generatedText;
}
getUniqueId() {
if (!this._uniqueId) {
let id = this.baseWitness.id + "-" + this.activeWitness.id + "-";
let uniqueIds = Object.keys(this._annotations);
uniqueIds.sort();
for (let i = 0, len = uniqueIds.length; i < len; i++) {
const uniqueId = uniqueIds[i];
const annotation = this._annotations[uniqueId];
id += annotation.uniqueId + annotation.content;
}
this._uniqueId = id;
}
return this._uniqueId;
}
resetUniqueId() {
this._uniqueId = null;
}
getText(): string {
return this.segmentedText.getText();
}
/**
* Return list of annotations for the given position in the generated text.
*
* @param {number} position - position in the generated text
* @returns {Annotation<>[]}
*/
annotationsForPosition(position: number): Annotation[] {
const orginalCurrentSegmentPositions =
this.orginalCurrentSegmentPositions;
return this.variants.filter((annotation) => {
let start = annotation.start;
let end = annotation.end;
if (annotation.isInsertion) {
start = String(start) + INSERTION_KEY;
end = start;
}
if (
!orginalCurrentSegmentPositions[String(start)] ||
!orginalCurrentSegmentPositions[String(end)]
) {
return false;
}
let [currentStart] = orginalCurrentSegmentPositions[String(start)];
let [currentEnd] = orginalCurrentSegmentPositions[String(end)];
return currentStart <= position && currentEnd >= position;
});
}
/**
* Return the starting position and length of the text segment that
* this annotation would be contained in in the generated text.
*
* @return Array, first element is start, second is length
*/
getPositionOfAnnotation(
annotation: Annotation
): [number | null, number | null] {
const orginalCurrentSegmentPositions =
this.orginalCurrentSegmentPositions;
let startKey = annotation.start;
let isActive = this._annotations.hasOwnProperty(annotation.uniqueId);
if (annotation.isInsertion && isActive) {
// only use insertion key if it is an active annotation
startKey = String(annotation.start) + INSERTION_KEY;
}
if (orginalCurrentSegmentPositions[String(startKey)] == undefined) {
if (this.originalText.getText().length === annotation.start) {
// if the annotation is an insertion at the end of the text
return [this.getText().length, 0];
} else {
console.warn(
"Invalid annotation passed to getPositionOfAnnotation: %o",
annotation
);
return [null, null];
}
}
const [startPos, startWasDeleted] =
orginalCurrentSegmentPositions[String(startKey)];
let length = null;
if (startWasDeleted) {
length = 0;
} else if (annotation.isInsertion && !isActive) {
length = 0;
} else if (annotation.type === ANNOTATION_TYPES.pageBreak && isActive) {
length = 0;
} else if (annotation.type === ANNOTATION_TYPES.lineBreak && isActive) {
length = 0;
} else if (annotation.type === ANNOTATION_TYPES.question && isActive) {
length = annotation.length;
} else {
const startSegment = this.segmentedText.segmentAtPosition(startPos);
let endSegment;
if (isActive || annotation.id === WORKING_VERSION_ANNOTATION_ID) {
let contentLength = annotation.content.length;
if (contentLength > 0) contentLength -= 1;
endSegment = this.segmentedText.segmentAtPosition(
startPos + contentLength
);
} else {
endSegment = this.segmentAtOriginalPosition(annotation.end);
}
if (
startSegment &&
endSegment &&
endSegment instanceof TextSegment
) {
length = startSegment.length;
if (startSegment.end < endSegment.end) {
length =
endSegment.start +
endSegment.length -
startSegment.start;
}
}
}
return [startPos, length];
}
/**
* Get an annotation pointing to the original content that
* the given positions refer to.
*/
getBaseAnnotation(start: number, length: number): Annotation {
const currentOriginalSegmentPositions =
this.currentOriginalSegmentPositions;
let startPos = currentOriginalSegmentPositions[start];
let origLength = 0;
if (startPos === undefined) {
// end of text insertion
startPos = this.originalText.getText().length;
origLength = 0;
} else if (length === 0) {
let annotations = this.annotationsForPosition(start);
if (annotations.length > 0) {
let annotation = annotations[0];
if (annotation.isDeletion) {
startPos = annotation.start;
origLength = annotation.length;
} else {
// Assuming we are getting this for an insertion, so want to get
// the original starting position.
startPos = currentOriginalSegmentPositions[start];
}
} else {
startPos = currentOriginalSegmentPositions[start];
}
} else {
for (let i = start; i < start + length; i++) {
let annotations = this.annotationsForPosition(i);
if (annotations.length > 0) {
let annotation = annotations[0];
if (i === start) {
if (annotation.isDeletion) {
startPos = annotation.start + annotation.length;
} else {
startPos = annotation.start;
}
} else {
// Only add deletion length if not at the start.
// Otherwise, it is assumed to be immediately before
// the requested position.
if (annotation.isDeletion) {
origLength += annotation.length;
}
}
if (annotation.isDeletion) {
origLength++;
} else {
origLength += annotation.length;
i += annotation.content.length - 1;
}
} else {
if (i === start) {
startPos = currentOriginalSegmentPositions[i];
}
origLength++;
}
}
}
let content = "";
if (origLength > 0) {
let segments = this.originalText.segmentsInRange(
startPos,
origLength
);
content = segments.reduce((content, segment) => {
return content + segment.text;
}, "");
}
if (origLength !== content.length) {
console.log(
"Base annotation has different content length to length",
origLength,
content.length,
content
);
}
let annotation = new Annotation(
BASE_ANNOTATION_ID,
this.baseWitness,
startPos,
origLength,
content,
ANNOTATION_TYPES.variant,
this.baseWitness
);
return annotation;
}
/**
* Get an annotation that represents the content in the generated text.
*
* If any annotations have been applied, it should return the applied
* annotation.
*
* @param start - Start position in original text
* @param length - Length in original text
* @returns {Annotation}
*/
getAnnotation(start: number, length: number): Annotation | null {
this.segmentedText;
let posKey = start;
if (length === 0) posKey = start + INSERTION_KEY;
const orginalCurrentSegmentPositions =
this.orginalCurrentSegmentPositions;
let [startPos, deleted] = orginalCurrentSegmentPositions[posKey];
let annotation = null;
if (startPos === undefined) {
} else if (length === 0) {
const annotations = this.annotationsForPosition(startPos);
if (annotations.length > 0) {
annotation = annotations[0];
}
} else {
let annotations: Annotation[] = [];
for (let i = startPos; i < startPos + length; i++) {
annotations = annotations.concat(
this.annotationsForPosition(i)
);
}
if (annotations.length > 0) {
let content = "";
let creatorUser = null;
let creatorWitness = this.activeWitness;
// Figure out who created the annotation
// and set creatorUser accordingly.
let foundVariant = null;
for (let i = 0; i < annotations.length; i++) {
let foundAnnotation = annotations[i];
if (
foundAnnotation.type === ANNOTATION_TYPES.variant &&
foundAnnotation.id !== BASE_ANNOTATION_ID
) {
creatorWitness = foundAnnotation.creatorWitness;
creatorUser = foundAnnotation.creatorUser;
if (!foundVariant) {
foundVariant = foundAnnotation;
} else {
if (
foundAnnotation.creatorWitness !==
creatorWitness &&
foundAnnotation.creatorUser !== creatorUser
) {
console.warn(
"Found second variant",
annotations
);
}
}
}
}
if (foundVariant !== null) {
if (
foundVariant.start == start &&
foundVariant.length >= length
) {
annotation = foundVariant;
} else {
annotation = new Annotation(
null,
this.baseWitness,
start,
length,
" ",
ANNOTATION_TYPES.variant,
this.activeWitness
);
let segments = this.segmentsForAnnotation(annotation);
content = segments.reduce(
(
previousValue: string,
currentValue: TextSegment | number
) => {
let value = previousValue;
if (currentValue instanceof TextSegment) {
value += currentValue.text;
}
return value;
},
""
);
annotation.content = content;
}
}
}
}
if (annotation == null && startPos) {
annotation = this.getBaseAnnotation(startPos, length);
}
return annotation;
}
getContentForRange(start: number, length: number): string {
const currentOriginalSegmentPositions =
this.currentOriginalSegmentPositions;
let startPos = currentOriginalSegmentPositions[start];
let content = "";
if (startPos === undefined) {
} else if (length === 0) {
} else {
let segments: TextSegment[] = [];
for (let i = start; i < start + length; i++) {
let segment = this.segmentAtOriginalPosition(i);
if (
segment instanceof TextSegment &&
segments.indexOf(segment) !== -1
) {
segments.push(segment);
}
}
content = segments.reduce(
(previousValue: string, currentValue: TextSegment) => {
return previousValue + currentValue.text;
},
""
);
}
return content;
}
/**
* Return segments for the given annotation in the current version of the text
*
* The annotation should be referring to positions in the base text.
*
* @param {Annotation} annotation
* @return {TextSegment|number[]}
*/
segmentsForAnnotation(annotation: Annotation): Array<TextSegment | number> {
this.segmentedText;
let segments = [];
let isActive = false;
if (this._annotations.hasOwnProperty(annotation.uniqueId)) {
isActive = true;
}
const orginalCurrentSegmentPositions =
this.orginalCurrentSegmentPositions;
if (isActive || annotation.id === WORKING_VERSION_ANNOTATION_ID) {
let key = annotation.start;
if (annotation.isInsertion) {
key += INSERTION_KEY;
}
let [start, deleted] = orginalCurrentSegmentPositions[String(key)];
let end = start + annotation.content.length;
for (let i = start; i < end; i++) {
let segment = this.segmentedText.segmentAtPosition(i);
if (segment && segments.indexOf(segment) == -1) {
segments.push(segment);
}
}
// check it's not an insertion at the end of the text
} else if (
!annotation.isInsertion ||
annotation.start !== this._originalTextLength
) {
let start = annotation.start;
let end = annotation.end;
let processedAnnotations = [];
for (let i = start; i <= end; i++) {
let segment = null;
let [pos, deleted] = orginalCurrentSegmentPositions[String(i)];
let posAnnotations = this.annotationsForPosition(pos);
let longestAnnotation;
if (posAnnotations.length > 0) {
for (let i = 0; i < posAnnotations.length; i++) {
let current = posAnnotations[i];
if (
!longestAnnotation ||
current.length > longestAnnotation.length
)
longestAnnotation = current;
}
}
if (deleted) {
segment = pos;
} else if (annotation.isInsertion) {
segment = new TextSegment(pos, annotation.content);
} else if (
longestAnnotation &&
processedAnnotations.indexOf(longestAnnotation) === -1
) {
let to = pos + longestAnnotation.content.length;
for (let j = pos; j < to; j++) {
let annoSegment =
this.segmentedText.segmentAtPosition(j);
if (
annoSegment &&
segments.indexOf(annoSegment) == -1
) {
segments.push(annoSegment);
}
}
processedAnnotations.push(longestAnnotation);
} else {
segment = this.segmentedText.segmentAtPosition(pos);
}
if (segment && segments.indexOf(segment) == -1) {
segments.push(segment);
}
}
}
return segments;
}
originalSegmentsForAnnotation(annotation: Annotation): TextSegment[] {
return this.originalText.segmentsInRange(
annotation.start,
annotation.length
);
}
createSegments(annotation: Annotation, start: number) {
const deleted = annotation.content.length === 0;
let segments = [];
if (this.segmenter !== null && !deleted) {
let annotationSegments = this.segmenter(annotation.content);
let annoSegStart = start;
for (let j = 0; j < annotationSegments.length; j++) {
let annotationSegment = annotationSegments[j];
annotationSegment.start = annoSegStart;
annotationSegment._annotation = annotation;
annoSegStart += annotationSegment.text.length;
}
segments = annotationSegments;
} else {
let annotationSegment = new TextSegment(start, annotation.content);
annotationSegment._annotation = annotation;
segments = [annotationSegment];
}
return segments;
}
indexOfSegment(segment: TextSegment, segments: TextSegment[]) {
let foundSegmentIndex = -1;
let minIndex = 0;
let maxIndex = segments.length - 1;
let currentIndex;
let currentSegment;
let position = segment.start;
while (minIndex <= maxIndex) {
currentIndex = ((minIndex + maxIndex) / 2) | 0;
currentSegment = segments[currentIndex];
const segmentEnd =
currentSegment.start + currentSegment.text.length - 1;
if (segmentEnd < position) {
minIndex = currentIndex + 1;
} else if (currentSegment.start > position) {
maxIndex = currentIndex - 1;
} else if (currentSegment.text !== segment.text) {
// These should be equal, so if not just punt and use
// indexOf instead.
return segments.indexOf(segment);
} else {
return currentIndex;
}
}
return foundSegmentIndex;
}
/**
* Generate a new SegmentedText with the given annotations.
*/
generateText(
text: SegmentedText,
annotations: Annotation[]
): SegmentedText {
const segments = text.segments;
var newSegments = segments.slice();
let replacedSegments = {};
for (let i = 0, len = annotations.length; i < len; i++) {
let annotation = annotations[i];
let targets = text.segmentsInRange(
annotation.start,
annotation.length
);
if (targets.length > 0) {
let start = targets[0].start;
let firstIndex = this.indexOfSegment(targets[0], newSegments);
if (firstIndex === -1) {
let found = newSegments.filter(
(seg) => seg.start === targets[0].start
);
if (found) {
firstIndex = newSegments.indexOf(found[0]);
}
}
const segments = this.createSegments(annotation, start);
if (annotation.isInsertion) {
newSegments.splice(firstIndex, 0, ...segments);
} else {
newSegments.splice(firstIndex, targets.length, ...segments);
}
// store replaced segments to use when setting position below
if (!annotation.isInsertion) {
replacedSegments[annotation.uniqueId] = targets;
}
} else {
// Assume insertion at end of text, otherwise a target segment would exist
const segments = this.createSegments(
annotation,
annotation.start
);
newSegments = newSegments.concat(segments);
if (!annotation.isInsertion) {
console.warn(
"Annotation with missing text segments that is not an insertion: %o",
annotation
);
}
}
}
let processedSegmentAnnotations = {};
let currentPosition = 0;
let originalPosition = 0;
let updatedSegments = new Array(newSegments.length);
this._orginalCurrentSegmentPositions = {};
this._currentOriginalSegmentPositions = {};
for (let i = 0, len = newSegments.length; i < len; i++) {
let segment = newSegments[i];
if (segment._annotation) {
const annotation = segment._annotation;
const deleted = segment.text.length == 0;
const replaced = replacedSegments[annotation.uniqueId];
const alreadyProcessed = processedSegmentAnnotations[
segment._annotation.uniqueId
]
? true
: false;
if (!alreadyProcessed && annotation.isInsertion) {
this._orginalCurrentSegmentPositions[
String(segment.start) + INSERTION_KEY
] = [currentPosition, deleted];
for (let j = 0; j < annotation.content.length; j++) {
this._currentOriginalSegmentPositions[
currentPosition + j
] = segment.start;
}
originalPosition = segment.start;
} else if (!alreadyProcessed && replaced) {
const firstReplacedSeg = replaced[0];
const replacedLength = replaced.reduce(
(length, seg) => length + seg.length,
0
);
for (let j = 0; j < replacedLength; j++) {
this._orginalCurrentSegmentPositions[
segment.start + j
] = [currentPosition, deleted];
}
originalPosition = segment.start + replacedLength;
for (let m = 0; m < annotation.content.length; m++) {
this._currentOriginalSegmentPositions[
currentPosition + m
] = firstReplacedSeg.start;
}
}
processedSegmentAnnotations[annotation.uniqueId] = true;
} else {
const segmentPos = segment.start;
for (let j = 0; j < segment.text.length; j++) {
this._orginalCurrentSegmentPositions[segmentPos + j] = [
currentPosition + j,
false,
];
this._currentOriginalSegmentPositions[currentPosition + j] =
segmentPos + j;
}
originalPosition = segment.start + segment.text.length;
}
segment = new TextSegment(currentPosition, newSegments[i].text);
updatedSegments[i] = segment;
currentPosition += segment.length;
}
this._generatedTextLength = currentPosition;
this._originalTextLength = originalPosition;
return new SegmentedText(updatedSegments);
}
/**
* Get the segment in the text that would have been at the given position
* in the original text.
*
* If the segment was deleted rather than just replaced, it will
* return a number, which is the position the segment would have been
* at in the current text.
*/
segmentAtOriginalPosition(position: number): TextSegment | number | null {
const orginalCurrentSegmentPositions =
this.orginalCurrentSegmentPositions;
let newPos, wasDeleted;
try {
[newPos, wasDeleted] =
orginalCurrentSegmentPositions[String(position)];
} catch (e) {
console.warn(
"Error getting orginalCurrentSegmentPositions, position: %o",
position
);
return null;
}
if (newPos !== undefined) {
if (wasDeleted) {
return newPos;
} else {
return this.segmentedText.segmentAtPosition(newPos);
}
} else {
console.warn("Could not get segment at position: %d", position);
return null;
}
}
/**
* Get the segment of the text from the original version that
* corresponds to the given position in the current version.
*
* @param Position in the current version
* @return {TextSegment|null}
*/
originalSegmentAtPosition(position: number) {
const pos = this.currentOriginalSegmentPositions[position];
if (pos !== undefined) {
return this.originalText.segmentAtPosition(pos);
} else {
return null;
}
}
}