|
1 | 1 | import $ from 'jquery' |
2 | 2 | import rangy from 'rangy' |
| 3 | +import 'rangy/lib/rangy-textrange' |
3 | 4 |
|
4 | 5 | import * as config from './config' |
5 | 6 | import error from './util/error' |
@@ -332,19 +333,36 @@ const Editable = module.exports = class Editable { |
332 | 333 | } |
333 | 334 | } |
334 | 335 |
|
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 | + ) |
348 | 366 | } |
349 | 367 |
|
350 | 368 | removeHighlight ({editableHost, highlightId}) { |
|
0 commit comments