/
blockEntities.js
92 lines (75 loc) · 2.65 KB
/
blockEntities.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import updateMutation from './util/updateMutation';
import rangeSort from './util/rangeSort';
import getElementHTML from './util/getElementHTML';
import getElementTagLength from './util/getElementTagLength';
const converter = (entity = {}, originalText) => {
return originalText;
};
export default (block, entityMap, entityConverter = converter) => {
let resultText = [...block.text];
let getEntityHTML = entityConverter;
if (entityConverter.__isMiddleware) {
getEntityHTML = entityConverter(converter);
}
if (
Object.prototype.hasOwnProperty.call(block, 'entityRanges') &&
block.entityRanges.length > 0
) {
let entities = block.entityRanges.sort(rangeSort);
let styles = block.inlineStyleRanges;
for (let index = 0; index < entities.length; index++) {
const entityRange = entities[index];
const entity = entityMap[entityRange.key];
const originalText = resultText
.slice(entityRange.offset, entityRange.offset + entityRange.length)
.join('');
const entityHTML = getEntityHTML(entity, originalText);
const elementHTML = getElementHTML(entityHTML, originalText);
let converted;
if (!!elementHTML || elementHTML === '') {
converted = [...elementHTML];
} else {
converted = originalText;
}
const prefixLength = getElementTagLength(entityHTML, 'start');
const suffixLength = getElementTagLength(entityHTML, 'end');
const updateLaterMutation = (mutation, mutationIndex) => {
if (
mutationIndex > index ||
Object.prototype.hasOwnProperty.call(mutation, 'style')
) {
return updateMutation(
mutation,
entityRange.offset,
entityRange.length,
converted.length,
prefixLength,
suffixLength
);
}
return mutation;
};
const updateLaterMutations = mutationList =>
mutationList.reduce((acc, mutation, mutationIndex) => {
const updatedMutation = updateLaterMutation(mutation, mutationIndex);
if (Array.isArray(updatedMutation)) {
return acc.concat(updatedMutation);
}
return acc.concat([updatedMutation]);
}, []);
entities = updateLaterMutations(entities);
styles = updateLaterMutations(styles);
resultText = [
...resultText.slice(0, entityRange.offset),
...converted,
...resultText.slice(entityRange.offset + entityRange.length),
];
}
return Object.assign({}, block, {
text: resultText.join(''),
inlineStyleRanges: styles,
entityRanges: entities,
});
}
return block;
};