@@ -10,8 +10,11 @@ import type {Model} from '../../json-crdt/model';
1010import type { Printable } from '../../util/print/types' ;
1111import type { SliceType } from './types' ;
1212import type { PersistedSlice } from './slice/PersistedSlice' ;
13- import { CONST } from '../../json-hash' ;
1413
14+ /**
15+ * Context for a Peritext instance. Contains all the data and methods needed to
16+ * interact with the text.
17+ */
1518export class Peritext implements Printable {
1619 public readonly slices : Slices ;
1720 public readonly editor : Editor ;
@@ -25,29 +28,86 @@ export class Peritext implements Printable {
2528 this . editor = new Editor ( this ) ;
2629 }
2730
31+ // ------------------------------------------------------------------- Points
32+
33+ /**
34+ * Creates a point at a character ID.
35+ *
36+ * @param id Character ID to which the point should be attached.
37+ * @param anchor Whether the point should be before or after the character.
38+ * @returns The point.
39+ */
2840 public point ( id : ITimestampStruct , anchor : Anchor = Anchor . After ) : Point {
2941 return new Point ( this , id , anchor ) ;
3042 }
3143
44+ /**
45+ * Creates a point at a view position in the text.
46+ *
47+ * @param pos View position in the text.
48+ * @param anchor Whether the point should attach before or after a character.
49+ * @returns The point.
50+ */
3251 public pointAt ( pos : number , anchor : Anchor = Anchor . Before ) : Point {
52+ // TODO: Provide ability to attach to the beginning of the text?
53+ // TODO: Provide ability to attach to the end of the text?
3354 const str = this . str ;
3455 const id = str . find ( pos ) ;
3556 if ( ! id ) return this . point ( str . id , Anchor . After ) ;
3657 return this . point ( id , anchor ) ;
3758 }
3859
60+ /**
61+ * Creates a point which is attached to the start of the text, before the
62+ * first character.
63+ *
64+ * @returns A point at the start of the text.
65+ */
3966 public pointAtStart ( ) : Point {
4067 return this . point ( this . str . id , Anchor . After ) ;
4168 }
4269
70+ /**
71+ * Creates a point which is attached to the end of the text, after the last
72+ * character.
73+ *
74+ * @returns A point at the end of the text.
75+ */
4376 public pointAtEnd ( ) : Point {
4477 return this . point ( this . str . id , Anchor . Before ) ;
4578 }
4679
80+ // ------------------------------------------------------------------- Ranges
81+
82+ /**
83+ * Creates a range from two points. The points can be in any order.
84+ *
85+ * @param p1 Point
86+ * @param p2 Point
87+ * @returns A range with points in correct order.
88+ */
89+ public rangeFromPoints ( p1 : Point , p2 : Point ) : Range {
90+ return Range . from ( this , p1 , p2 ) ;
91+ }
92+
93+ /**
94+ * Creates a range from two points, the points have to be in the correct order.
95+ *
96+ * @param start Start point of the range, must be before or equal to end.
97+ * @param end End point of the range, must be after or equal to start.
98+ * @returns A range with the given start and end points.
99+ */
47100 public range ( start : Point , end : Point ) : Range {
48101 return new Range ( this , start , end ) ;
49102 }
50103
104+ /**
105+ * Creates a range from a view position and a length.
106+ *
107+ * @param start Position in the text.
108+ * @param length Length of the range.
109+ * @returns A range from the given position with the given length.
110+ */
51111 public rangeAt ( start : number , length : number = 0 ) : Range {
52112 const str = this . str ;
53113 if ( ! length ) {
@@ -62,11 +122,27 @@ export class Peritext implements Printable {
62122 return this . range ( startEndpoint , endEndpoint ) ;
63123 }
64124
125+ // --------------------------------------------------------------- Insertions
126+
127+ /**
128+ * Insert plain text at a view position in the text.
129+ *
130+ * @param pos View position in the text.
131+ * @param text Text to insert.
132+ */
65133 public insAt ( pos : number , text : string ) : void {
66134 const str = this . model . api . wrap ( this . str ) ;
67135 str . ins ( pos , text ) ;
68136 }
69137
138+ /**
139+ * Insert plain text after a character referenced by its ID and return the
140+ * ID of the insertion operation.
141+ *
142+ * @param after Character ID after which the text should be inserted.
143+ * @param text Text to insert.
144+ * @returns ID of the insertion operation.
145+ */
70146 public ins ( after : ITimestampStruct , text : string ) : ITimestampStruct {
71147 if ( ! text ) throw new Error ( 'NO_TEXT' ) ;
72148 const api = this . model . api ;
@@ -87,6 +163,8 @@ export class Peritext implements Printable {
87163 return slice ;
88164 }
89165
166+ // ---------------------------------------------------------------- Deletions
167+
90168 public delSlice ( sliceId : ITimestampStruct ) : void {
91169 this . slices . del ( sliceId ) ;
92170 }
0 commit comments