Skip to content

Commit 18d4231

Browse files
committed
feat(highlighting): Can display textrange-based highlights
1 parent 2bcc82f commit 18d4231

File tree

2 files changed

+50
-13
lines changed

2 files changed

+50
-13
lines changed

src/core.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import $ from 'jquery'
22
import rangy from 'rangy'
3+
import 'rangy/lib/rangy-textrange'
34

45
import * as config from './config'
56
import error from './util/error'
@@ -332,19 +333,36 @@ const Editable = module.exports = class Editable {
332333
}
333334
}
334335

335-
// Highlight text within an editable.
336-
//
337-
// The first occurrence of the provided 'text' will be highlighted.
338-
//
339-
// The markup used for the highlighting will be removed from
340-
// the final content.
341-
//
342-
// @param editableHost {DomNode}
343-
// @param text {String}
344-
// @param highlightId {String} Optional
345-
// Added to the highlight markups in the property `data-word-id`
346-
highlight ({editableHost, text, highlightId}) {
347-
return highlightSupport.highlightText(editableHost, text, highlightId)
336+
/**
337+
* Highlight text within an editable.
338+
*
339+
* By default highlights all occurences of `text`.
340+
* Pass it a `textRange` object to highlight a
341+
* specific text portion.
342+
*
343+
* The markup used for the highlighting will be removed
344+
* from the final content.
345+
*
346+
*
347+
* @param {Object} options
348+
* @param {DOMNode} options.editableHost
349+
* @param {String} options.text
350+
* @param {String} options.highlightId Added to the highlight markups in the property `data-word-id`
351+
* @param {Object} [options.textRange] An optional range which gets used to set the markers.
352+
* @param {Number} [options.textRange.start]
353+
* @param {Number} [options.textRange.end]
354+
* @return {Number}
355+
*/
356+
highlight ({editableHost, text, highlightId, textRange}) {
357+
return !textRange
358+
? highlightSupport.highlightText(editableHost, text, highlightId)
359+
: (
360+
typeof textRange.start === 'number' && typeof textRange.end === 'number'
361+
? highlightSupport.highlightRange(editableHost, text, highlightId, textRange.start, textRange.end)
362+
: !error(
363+
'Error in editable.highlight: You passed a textRange object with invalid keys. Expected shape: { start: Number, end: Number }'
364+
) && -1
365+
)
348366
}
349367

350368
removeHighlight ({editableHost, highlightId}) {

src/highlight-support.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@ const highlightSupport = {
2424
}
2525
},
2626

27+
highlightRange (editableHost, text, highlightId, startIndex, endIndex) {
28+
if (this.hasHighlight(editableHost, highlightId)) return
29+
const marker = highlightSupport.createMarkerNode(
30+
'<span class="highlight-comment"></span>',
31+
'highlight',
32+
this.win
33+
)
34+
35+
const match = {
36+
id: highlightId,
37+
startIndex,
38+
endIndex,
39+
match: text,
40+
marker: marker
41+
}
42+
highlightText.highlightMatches(editableHost, [match])
43+
return match.startIndex
44+
},
45+
2746
updateHighlight (editableHost, highlightId, addCssClass, removeCssClass) {
2847
if (!document.documentElement.classList) return
2948

0 commit comments

Comments
 (0)