/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format * @flow * @emails oncall+draft_js */ 'use strict'; import type { BlockNodeRecord } from "./BlockNodeRecord"; import type { EntityRange } from "./EntityRange"; const DraftStringKey = require("./DraftStringKey"); const UnicodeUtils = require("fbjs/lib/UnicodeUtils"); const { strlen } = UnicodeUtils; /** * Convert to UTF-8 character counts for storage. */ function encodeEntityRanges(block: BlockNodeRecord, storageMap: Object): Array { const encoded = []; block.findEntityRanges(character => !!character.getEntity(), ( /*number*/ start, /*number*/ end) => { const text = block.getText(); const 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;