draft-convert-greger
Version:
Extensibly serialize & deserialize Draft.js ContentState
50 lines (41 loc) • 1.28 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import updateMutation from './util/updateMutation';
import rangeSort from './util/rangeSort';
var ENTITY_MAP = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'`': '`',
'\n': '<br/>'
};
export default (function (block) {
var blockText = _toConsumableArray(block.text);
var entities = block.entityRanges.sort(rangeSort);
var styles = block.inlineStyleRanges.sort(rangeSort);
var resultText = '';
var _loop = function _loop(index) {
var _char = blockText[index];
if (ENTITY_MAP[_char] !== undefined) {
var encoded = ENTITY_MAP[_char];
var resultIndex = _toConsumableArray(resultText).length;
resultText += encoded;
var updateForChar = function updateForChar(mutation) {
return updateMutation(mutation, resultIndex, _char.length, encoded.length, 0, 0);
};
entities = entities.map(updateForChar);
styles = styles.map(updateForChar);
} else {
resultText += _char;
}
};
for (var index = 0; index < blockText.length; index++) {
_loop(index);
}
return Object.assign({}, block, {
text: resultText,
inlineStyleRanges: styles,
entityRanges: entities
});
});