39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
/**
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
* @providesModule getRangesForDraftEntity
|
|
* @format
|
|
*
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
var invariant = require('fbjs/lib/invariant');
|
|
|
|
/**
|
|
* Obtain the start and end positions of the range that has the
|
|
* specified entity applied to it.
|
|
*
|
|
* Entity keys are applied only to contiguous stretches of text, so this
|
|
* method searches for the first instance of the entity key and returns
|
|
* the subsequent range.
|
|
*/
|
|
function getRangesForDraftEntity(block, key) {
|
|
var ranges = [];
|
|
block.findEntityRanges(function (c) {
|
|
return c.getEntity() === key;
|
|
}, function (start, end) {
|
|
ranges.push({ start: start, end: end });
|
|
});
|
|
|
|
!!!ranges.length ? process.env.NODE_ENV !== 'production' ? invariant(false, 'Entity key not found in this range.') : invariant(false) : void 0;
|
|
|
|
return ranges;
|
|
}
|
|
|
|
module.exports = getRangesForDraftEntity; |