forked from Gitlink/forgeplus-react
42 lines
1.2 KiB
Plaintext
42 lines
1.2 KiB
Plaintext
|
|
/**
|
||
|
|
* 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 encodeEntityRanges
|
||
|
|
* @format
|
||
|
|
* @flow
|
||
|
|
*/
|
||
|
|
|
||
|
|
'use strict';
|
||
|
|
|
||
|
|
import type { BlockNodeRecord } from './BlockNodeRecord';
|
||
|
|
import type { EntityRange } from './EntityRange';
|
||
|
|
|
||
|
|
var DraftStringKey = require('./DraftStringKey');
|
||
|
|
var UnicodeUtils = require('fbjs/lib/UnicodeUtils');
|
||
|
|
|
||
|
|
var { strlen } = UnicodeUtils;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Convert to UTF-8 character counts for storage.
|
||
|
|
*/
|
||
|
|
function encodeEntityRanges(block: BlockNodeRecord, storageMap: Object): Array<EntityRange> {
|
||
|
|
var encoded = [];
|
||
|
|
block.findEntityRanges(character => !!character.getEntity(), ( /*number*/start, /*number*/end) => {
|
||
|
|
var text = block.getText();
|
||
|
|
var key = block.getEntityAt(start);
|
||
|
|
encoded.push({
|
||
|
|
offset: strlen(text.slice(0, start)),
|
||
|
|
length: strlen(text.slice(start, end)),
|
||
|
|
// Encode the key as a number for range storage.
|
||
|
|
key: Number(storageMap[DraftStringKey.stringify(key)])
|
||
|
|
});
|
||
|
|
});
|
||
|
|
return encoded;
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = encodeEntityRanges;
|