Skip to content

Commit 62b18d6

Browse files
committed
feat: export the range text on the extracted range object
1 parent 0cd0e93 commit 62b18d6

File tree

3 files changed

+41
-64
lines changed

3 files changed

+41
-64
lines changed

spec/highlighting.spec.js

Lines changed: 31 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,15 @@ describe('Highlighting', function () {
105105
const startIndex = this.highlightRange('myId', 3, 7)
106106
const expectedRanges = {
107107
myId: {
108+
text: 'ple ',
108109
start: 3,
109110
end: 7
110111
}
111112
}
112-
const expectedTexts = {myId: 'ple '}
113113

114114
const extractedRanges = this.extract()
115-
const extractedTexts = this.extractTexts(extractedRanges)
116115

117116
expect(extractedRanges).toEqual(expectedRanges)
118-
expect(extractedTexts).toEqual(expectedTexts)
119117
expect(startIndex).toEqual(3)
120118
})
121119

@@ -127,34 +125,31 @@ describe('Highlighting', function () {
127125

128126
const expectedRanges = {
129127
first: {
128+
text: 'P',
130129
start: 0,
131130
end: 1
132131
},
133132
second: {
133+
text: 'e',
134134
start: 1,
135135
end: 2
136136
},
137137
third: {
138+
text: 'o',
138139
start: 2,
139140
end: 3
140141
},
141142
fourth: {
143+
text: 'p',
142144
start: 3,
143145
end: 4
144146
}
145147
}
146-
const expectedTexts = {
147-
first: 'P',
148-
second: 'e',
149-
third: 'o',
150-
fourth: 'p'
151-
}
152148

153-
const extractedRanges = this.extract()
154-
const extractedTexts = this.extractTexts(extractedRanges)
155149

150+
const extractedRanges = this.extract()
156151
expect(extractedRanges).toEqual(expectedRanges)
157-
expect(extractedTexts).toEqual(expectedTexts)
152+
158153
})
159154

160155
it('can handle nested highlights', () => {
@@ -164,36 +159,29 @@ describe('Highlighting', function () {
164159
this.highlightRange('fourth', 0, 6)
165160
const expectedRanges = {
166161
first: {
162+
text: 'P',
167163
start: 0,
168164
end: 1
169165
},
170166
second: {
167+
text: 'e',
171168
start: 1,
172169
end: 2
173170
},
174171
third: {
172+
text: 'ople',
175173
start: 2,
176174
end: 6
177175
},
178176
fourth: {
177+
text: 'People',
179178
start: 0,
180179
end: 6
181180
}
182181
}
183182

184-
185-
const expectedTexts = {
186-
first: 'P',
187-
second: 'e',
188-
third: 'ople',
189-
fourth: 'People'
190-
}
191-
192183
const extractedRanges = this.extract()
193-
const extractedTexts = this.extractTexts(extractedRanges)
194-
195184
expect(extractedRanges).toEqual(expectedRanges)
196-
expect(extractedTexts).toEqual(expectedTexts)
197185
})
198186

199187
it('can handle intersecting highlights', () => {
@@ -202,91 +190,76 @@ describe('Highlighting', function () {
202190
this.highlightRange('third', 4, 6)
203191
const expectedRanges = {
204192
first: {
193+
text: 'Peo',
205194
start: 0,
206195
end: 3
207196
},
208197
second: {
198+
text: 'ple ',
209199
start: 3,
210200
end: 7
211201
},
212202
third: {
203+
text: 'le',
213204
start: 4,
214205
end: 6
215206
}
216207
}
217-
218-
const expectedTexts = {
219-
first: 'Peo',
220-
second: 'ple ',
221-
third: 'le'
222-
}
223208
const extractedRanges = this.extract()
224-
const extractedTexts = this.extractTexts(extractedRanges)
225-
226209
expect(extractedRanges).toEqual(expectedRanges)
227-
expect(extractedTexts).toEqual(expectedTexts)
210+
228211
})
229212

230213
it('can handle highlights containing newlines', () => {
231214
this.highlightRange('first', 11, 22)
232215
const expectedRanges = {
233216
first: {
217+
text: ' The \nWorld',
234218
start: 11,
235219
end: 22
236220
}
237221
}
238-
const expectedTexts = {
239-
first: ' The \nWorld'
240-
}
241-
const extractedRanges = this.extract()
242-
const extractedTexts = this.extractTexts(extractedRanges)
243222

223+
const extractedRanges = this.extract()
244224
expect(extractedRanges).toEqual(expectedRanges)
245-
expect(extractedTexts).toEqual(expectedTexts)
225+
246226
})
247227

248228
it('can handle identical ranges', () => {
249229
this.highlightRange('first', 11, 22)
250230
this.highlightRange('second', 11, 22)
251231
const expectedRanges = {
252232
first: {
233+
text: ' The \nWorld',
253234
start: 11,
254235
end: 22
255236
},
256237
second: {
238+
text: ' The \nWorld',
257239
start: 11,
258240
end: 22
259241
}
260242
}
261-
const expectedTexts = {
262-
first: ' The \nWorld',
263-
second: ' The \nWorld'
264-
}
265-
const extractedRanges = this.extract()
266-
const extractedTexts = this.extractTexts(extractedRanges)
267243

244+
const extractedRanges = this.extract()
268245
expect(extractedRanges).toEqual(expectedRanges)
269-
expect(extractedTexts).toEqual(expectedTexts)
246+
270247
})
271248

272249
it('will update any existing range found under `highlightId` aka upsert', () => {
273250
this.highlightRange('first', 11, 22)
274251
this.highlightRange('first', 8, 9)
275252
const expectedRanges = {
276253
first: {
254+
text: 'a',
277255
start: 8,
278256
end: 9
279257
}
280258
}
281-
const expectedTexts = {
282-
first: 'a'
283-
}
284259

285-
const extractedRanges = this.extract()
286-
const extractedTexts = this.extractTexts(extractedRanges)
287260

261+
const extractedRanges = this.extract()
288262
expect(extractedRanges).toEqual(expectedRanges)
289-
expect(extractedTexts).toEqual(expectedTexts)
290263
})
291264

292265
it('can handle all cases combined and creates consistent output', () => {
@@ -300,43 +273,39 @@ describe('Highlighting', function () {
300273

301274
const expectedRanges = {
302275
first: {
276+
text: 'People Make The \nWorld G',
303277
start: 0,
304278
end: 24
305279
},
306280
second: {
281+
text: 'ople Mak',
307282
start: 2,
308283
end: 10
309284
},
310285
third: {
286+
text: 'l',
311287
start: 4,
312288
end: 5
313289
},
314290
fourth: {
291+
text: 'ld Go Round',
315292
start: 20,
316293
end: 31
317294
},
318295
fifth: {
296+
text: ' ',
319297
start: 15,
320298
end: 16
321299
},
322300
sixth: {
301+
text: ' ',
323302
start: 15,
324303
end: 16
325304
}
326305
}
327-
const expectedTexts = {
328-
first: 'People Make The \nWorld G',
329-
second: 'ople Mak',
330-
third: 'l',
331-
fourth: 'ld Go Round',
332-
sixth: ' ',
333-
fifth: ' '
334-
}
335-
const extractedRanges = this.extract()
336-
const extractedTexts = this.extractTexts(extractedRanges)
337306

307+
const extractedRanges = this.extract()
338308
expect(extractedRanges).toEqual(expectedRanges)
339-
expect(extractedTexts).toEqual(expectedTexts)
340309

341310
const content = this.editable.getContent(this.$div[0])
342311
this.$div.html(content)

src/core.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,10 @@ const Editable = module.exports = class Editable {
374374
* Extracts positions of all DOMNodes that match `[data-word-id]`.
375375
*
376376
* Returns an object where the keys represent a highlight id and the value
377-
* a text range object.
377+
* a text range object of shape:
378+
* ```
379+
* { start: number, end: number, text: string}
380+
* ```
378381
*
379382
* @param {Object} options
380383
* @param {DOMNode} options.editableHos

src/highlight-support.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,12 @@ const highlightSupport = {
107107
} else {
108108
range.selectNode(markers[0])
109109
}
110-
return range.toCharacterRange(editableHost)
110+
const textRange = range.toCharacterRange(editableHost)
111+
return {
112+
start: textRange.start,
113+
end: textRange.end,
114+
text: range.text()
115+
}
111116
},
112117

113118
createMarkerNode (markerMarkup, highlightType, win) {

0 commit comments

Comments
 (0)