UNPKG

1.02 kBJavaScriptView Raw
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 *
9 * @emails oncall+draft_js
10 */
11'use strict';
12
13var invariant = require("fbjs/lib/invariant");
14/**
15 * Obtain the start and end positions of the range that has the
16 * specified entity applied to it.
17 *
18 * Entity keys are applied only to contiguous stretches of text, so this
19 * method searches for the first instance of the entity key and returns
20 * the subsequent range.
21 */
22
23
24function getRangesForDraftEntity(block, key) {
25 var ranges = [];
26 block.findEntityRanges(function (c) {
27 return c.getEntity() === key;
28 }, function (start, end) {
29 ranges.push({
30 start: start,
31 end: end
32 });
33 });
34 !!!ranges.length ? process.env.NODE_ENV !== "production" ? invariant(false, 'Entity key not found in this range.') : invariant(false) : void 0;
35 return ranges;
36}
37
38module.exports = getRangesForDraftEntity;
\No newline at end of file