about summary refs log tree commit diff
path: root/src/lib/strings/rich-text.ts
blob: 1df2144e066ec56137a769b359ef84466711105e (plain) (blame)
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
/*
= Rich Text Manipulation

When we sanitize rich text, we have to update the entity indices as the
text is modified. This can be modeled as inserts() and deletes() of the
rich text string. The possible scenarios are outlined below, along with
their expected behaviors.

NOTE: Slices are start inclusive, end exclusive

== richTextInsert()

Target string:

   0 1 2 3 4 5 6 7 8 910   // string indices
   h e l l o   w o r l d   // string value
       ^-------^           // target slice {start: 2, end: 7}

Scenarios:

A: ^                       // insert "test" at 0
B:        ^                // insert "test" at 4
C:                 ^       // insert "test" at 8

A = before           -> move both by num added
B = inner            -> move end by num added
C = after            -> noop

Results:

A: 0 1 2 3 4 5 6 7 8 910   // string indices
   t e s t h e l l o   w   // string value
               ^-------^   // target slice {start: 6, end: 11}

B: 0 1 2 3 4 5 6 7 8 910   // string indices
   h e l l t e s t o   w   // string value
       ^---------------^   // target slice {start: 2, end: 11}

C: 0 1 2 3 4 5 6 7 8 910   // string indices
   h e l l o   w o t e s   // string value
       ^-------^           // target slice {start: 2, end: 7}

== richTextDelete()

Target string:

   0 1 2 3 4 5 6 7 8 910   // string indices
   h e l l o   w o r l d   // string value
       ^-------^           // target slice {start: 2, end: 7}

Scenarios:

A: ^---------------^       // remove slice {start: 0, end: 9}
B:               ^-----^   // remove slice {start: 7, end: 11}
C:         ^-----------^   // remove slice {start: 4, end: 11}
D:       ^-^               // remove slice {start: 3, end: 5}
E:   ^-----^               // remove slice {start: 1, end: 5}
F: ^-^                     // remove slice {start: 0, end: 2}

A = entirely outer   -> delete slice
B = entirely after   -> noop
C = partially after  -> move end to remove-start
D = entirely inner   -> move end by num removed
E = partially before -> move start to remove-start index, move end by num removed
F = entirely before  -> move both by num removed

Results:

A: 0 1 2 3 4 5 6 7 8 910   // string indices
   l d                     // string value
                           // target slice (deleted)

B: 0 1 2 3 4 5 6 7 8 910   // string indices
   h e l l o   w           // string value
       ^-------^           // target slice {start: 2, end: 7}

C: 0 1 2 3 4 5 6 7 8 910   // string indices
   h e l l                 // string value
       ^-^                 // target slice {start: 2, end: 4}

D: 0 1 2 3 4 5 6 7 8 910   // string indices
   h e l   w o r l d       // string value
       ^---^               // target slice {start: 2, end: 5}

E: 0 1 2 3 4 5 6 7 8 910   // string indices
   h   w o r l d           // string value
     ^-^                   // target slice {start: 1, end: 3}

F: 0 1 2 3 4 5 6 7 8 910   // string indices
   l l o   w o r l d       // string value
   ^-------^               // target slice {start: 0, end: 5}
 */

import cloneDeep from 'lodash.clonedeep'
import {AppBskyFeedPost} from '@atproto/api'
import {removeExcessNewlines} from './rich-text-sanitize'

export type Entity = AppBskyFeedPost.Entity
export interface RichTextOpts {
  cleanNewlines?: boolean
}

export class RichText {
  constructor(
    public text: string,
    public entities?: Entity[],
    opts?: RichTextOpts,
  ) {
    if (opts?.cleanNewlines) {
      removeExcessNewlines(this).copyInto(this)
    }
  }

  clone() {
    return new RichText(this.text, cloneDeep(this.entities))
  }

  copyInto(target: RichText) {
    target.text = this.text
    target.entities = cloneDeep(this.entities)
  }

  insert(insertIndex: number, insertText: string) {
    this.text =
      this.text.slice(0, insertIndex) +
      insertText +
      this.text.slice(insertIndex)

    if (!this.entities?.length) {
      return this
    }

    const numCharsAdded = insertText.length
    for (const ent of this.entities) {
      // see comment at top of file for labels of each scenario
      // scenario A (before)
      if (insertIndex <= ent.index.start) {
        // move both by num added
        ent.index.start += numCharsAdded
        ent.index.end += numCharsAdded
      }
      // scenario B (inner)
      else if (insertIndex >= ent.index.start && insertIndex < ent.index.end) {
        // move end by num added
        ent.index.end += numCharsAdded
      }
      // scenario C (after)
      // noop
    }
    return this
  }

  delete(removeStartIndex: number, removeEndIndex: number) {
    this.text =
      this.text.slice(0, removeStartIndex) + this.text.slice(removeEndIndex)

    if (!this.entities?.length) {
      return this
    }

    const numCharsRemoved = removeEndIndex - removeStartIndex
    for (const ent of this.entities) {
      // see comment at top of file for labels of each scenario
      // scenario A (entirely outer)
      if (
        removeStartIndex <= ent.index.start &&
        removeEndIndex >= ent.index.end
      ) {
        // delete slice (will get removed in final pass)
        ent.index.start = 0
        ent.index.end = 0
      }
      // scenario B (entirely after)
      else if (removeStartIndex > ent.index.end) {
        // noop
      }
      // scenario C (partially after)
      else if (
        removeStartIndex > ent.index.start &&
        removeStartIndex <= ent.index.end &&
        removeEndIndex > ent.index.end
      ) {
        // move end to remove start
        ent.index.end = removeStartIndex
      }
      // scenario D (entirely inner)
      else if (
        removeStartIndex >= ent.index.start &&
        removeEndIndex <= ent.index.end
      ) {
        // move end by num removed
        ent.index.end -= numCharsRemoved
      }
      // scenario E (partially before)
      else if (
        removeStartIndex < ent.index.start &&
        removeEndIndex >= ent.index.start &&
        removeEndIndex <= ent.index.end
      ) {
        // move start to remove-start index, move end by num removed
        ent.index.start = removeStartIndex
        ent.index.end -= numCharsRemoved
      }
      // scenario F (entirely before)
      else if (removeEndIndex < ent.index.start) {
        // move both by num removed
        ent.index.start -= numCharsRemoved
        ent.index.end -= numCharsRemoved
      }
    }

    // filter out any entities that were made irrelevant
    this.entities = this.entities.filter(ent => ent.index.start < ent.index.end)
    return this
  }
}