@20minutes/draft-convert 3.0.3 → 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -3
- package/esm/blockEntities.js +58 -61
- package/esm/blockInlineStyles.js +64 -87
- package/esm/convertFromHTML.js +442 -478
- package/esm/convertToHTML.js +71 -93
- package/esm/default/defaultBlockHTML.js +33 -30
- package/esm/default/defaultInlineHTML.js +16 -16
- package/esm/encodeBlock.js +36 -40
- package/esm/index.js +1 -1
- package/esm/util/accumulateFunction.js +7 -9
- package/esm/util/blockTypeObjectFunction.js +7 -9
- package/esm/util/getBlockTags.js +19 -19
- package/esm/util/getElementHTML.js +24 -28
- package/esm/util/getElementTagLength.js +14 -16
- package/esm/util/getNestedBlockTags.js +20 -25
- package/esm/util/parseHTML.js +15 -15
- package/esm/util/rangeSort.js +6 -6
- package/esm/util/splitReactElement.js +27 -13
- package/esm/util/styleObjectFunction.js +6 -8
- package/esm/util/updateMutation.js +57 -47
- package/lib/blockEntities.js +74 -69
- package/lib/blockInlineStyles.js +81 -96
- package/lib/convertFromHTML.js +459 -487
- package/lib/convertToHTML.js +93 -107
- package/lib/default/defaultBlockHTML.js +47 -35
- package/lib/default/defaultInlineHTML.js +29 -21
- package/lib/encodeBlock.js +50 -46
- package/lib/index.js +25 -23
- package/lib/util/accumulateFunction.js +13 -11
- package/lib/util/blockTypeObjectFunction.js +13 -11
- package/lib/util/getBlockTags.js +35 -27
- package/lib/util/getElementHTML.js +40 -36
- package/lib/util/getElementTagLength.js +28 -22
- package/lib/util/getNestedBlockTags.js +35 -32
- package/lib/util/parseHTML.js +22 -18
- package/lib/util/rangeSort.js +13 -9
- package/lib/util/splitReactElement.js +42 -19
- package/lib/util/styleObjectFunction.js +12 -10
- package/lib/util/updateMutation.js +64 -51
- package/package.json +24 -28
- package/dist/draft-convert.js +0 -440
- package/dist/draft-convert.min.js +0 -1
package/esm/util/rangeSort.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export default (
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
});
|
|
1
|
+
export default ((r1, r2)=>{
|
|
2
|
+
if (r1.offset === r2.offset) {
|
|
3
|
+
return r2.length - r1.length;
|
|
4
|
+
}
|
|
5
|
+
return r1.offset - r2.offset;
|
|
6
|
+
});
|
|
@@ -1,18 +1,32 @@
|
|
|
1
1
|
import invariant from 'invariant';
|
|
2
2
|
import React from 'react';
|
|
3
3
|
import ReactDOMServer from 'react-dom/server';
|
|
4
|
-
|
|
5
4
|
// see http://w3c.github.io/html/syntax.html#writing-html-documents-elements
|
|
6
|
-
|
|
5
|
+
const VOID_TAGS = [
|
|
6
|
+
'area',
|
|
7
|
+
'base',
|
|
8
|
+
'br',
|
|
9
|
+
'col',
|
|
10
|
+
'embed',
|
|
11
|
+
'hr',
|
|
12
|
+
'img',
|
|
13
|
+
'input',
|
|
14
|
+
'link',
|
|
15
|
+
'meta',
|
|
16
|
+
'param',
|
|
17
|
+
'source',
|
|
18
|
+
'track',
|
|
19
|
+
'wbr'
|
|
20
|
+
];
|
|
7
21
|
export default function splitReactElement(element) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
22
|
+
if (VOID_TAGS.indexOf(element.type) !== -1) {
|
|
23
|
+
return ReactDOMServer.renderToStaticMarkup(element);
|
|
24
|
+
}
|
|
25
|
+
const tags = ReactDOMServer.renderToStaticMarkup(/*#__PURE__*/ React.cloneElement(element, {}, '\r')).split('\r');
|
|
26
|
+
invariant(tags.length > 1, `convertToHTML: Element of type ${element.type} must render children`);
|
|
27
|
+
invariant(tags.length < 3, `convertToHTML: Element of type ${element.type} cannot use carriage return character`);
|
|
28
|
+
return {
|
|
29
|
+
start: tags[0],
|
|
30
|
+
end: tags[1]
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
export default (
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
});
|
|
1
|
+
export default ((object)=>(style)=>{
|
|
2
|
+
if (typeof object === 'function') {
|
|
3
|
+
return object(style);
|
|
4
|
+
}
|
|
5
|
+
return object[style];
|
|
6
|
+
});
|
|
@@ -1,48 +1,58 @@
|
|
|
1
|
-
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
3
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
4
1
|
export default function updateMutation(mutation, originalOffset, originalLength, newLength, prefixLength, suffixLength) {
|
|
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
|
-
|
|
2
|
+
// three cases we can reasonably adjust - disjoint mutations that
|
|
3
|
+
// happen later on where the offset will need to be changed,
|
|
4
|
+
// mutations that completely contain the new one where we can adjust
|
|
5
|
+
// the length, and mutations that occur partially within the new one.
|
|
6
|
+
const lengthDiff = newLength - originalLength;
|
|
7
|
+
const mutationAfterChange = originalOffset + originalLength <= mutation.offset;
|
|
8
|
+
if (mutationAfterChange) {
|
|
9
|
+
return {
|
|
10
|
+
...mutation,
|
|
11
|
+
offset: mutation.offset + lengthDiff
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const mutationContainsChange = originalOffset >= mutation.offset && originalOffset + originalLength <= mutation.offset + mutation.length;
|
|
15
|
+
if (mutationContainsChange) {
|
|
16
|
+
return {
|
|
17
|
+
...mutation,
|
|
18
|
+
length: mutation.length + lengthDiff
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
const mutationWithinPrefixChange = mutation.offset >= originalOffset && mutation.offset + mutation.length <= originalOffset + originalLength && prefixLength > 0;
|
|
22
|
+
if (mutationWithinPrefixChange) {
|
|
23
|
+
return {
|
|
24
|
+
...mutation,
|
|
25
|
+
offset: mutation.offset + prefixLength
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const mutationContainsPrefix = mutation.offset < originalOffset && mutation.offset + mutation.length <= originalOffset + originalLength && mutation.offset + mutation.length > originalOffset && prefixLength > 0;
|
|
29
|
+
if (mutationContainsPrefix) {
|
|
30
|
+
return [
|
|
31
|
+
{
|
|
32
|
+
...mutation,
|
|
33
|
+
length: originalOffset - mutation.offset
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
...mutation,
|
|
37
|
+
offset: originalOffset + prefixLength,
|
|
38
|
+
length: mutation.offset - originalOffset + mutation.length
|
|
39
|
+
}
|
|
40
|
+
];
|
|
41
|
+
}
|
|
42
|
+
const mutationContainsSuffix = mutation.offset >= originalOffset && mutation.offset + mutation.length > originalOffset + originalLength && originalOffset + originalLength > mutation.offset && suffixLength > 0;
|
|
43
|
+
if (mutationContainsSuffix) {
|
|
44
|
+
return [
|
|
45
|
+
{
|
|
46
|
+
...mutation,
|
|
47
|
+
offset: mutation.offset + prefixLength,
|
|
48
|
+
length: originalOffset + originalLength - mutation.offset
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
...mutation,
|
|
52
|
+
offset: originalOffset + originalLength + prefixLength + suffixLength,
|
|
53
|
+
length: mutation.offset + mutation.length - (originalOffset + originalLength)
|
|
54
|
+
}
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
return mutation;
|
|
58
|
+
}
|
package/lib/blockEntities.js
CHANGED
|
@@ -1,74 +1,79 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
2
|
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
|
|
3
|
+
value: true
|
|
6
4
|
});
|
|
7
|
-
exports
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
};
|
|
21
|
-
var _default = exports["default"] = function _default(block, entityMap) {
|
|
22
|
-
var entityConverter = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : converter;
|
|
23
|
-
var resultText = (0, _toConsumableArray2["default"])(block.text);
|
|
24
|
-
var getEntityHTML = entityConverter;
|
|
25
|
-
if (entityConverter.__isMiddleware) {
|
|
26
|
-
getEntityHTML = entityConverter(converter);
|
|
27
|
-
}
|
|
28
|
-
if (Object.prototype.hasOwnProperty.call(block, 'entityRanges') && block.entityRanges.length > 0) {
|
|
29
|
-
var entities = block.entityRanges.sort(_rangeSort["default"]);
|
|
30
|
-
var styles = block.inlineStyleRanges;
|
|
31
|
-
var _loop = function _loop(index) {
|
|
32
|
-
var entityRange = entities[index];
|
|
33
|
-
var entity = entityMap[entityRange.key];
|
|
34
|
-
var originalText = resultText.slice(entityRange.offset, entityRange.offset + entityRange.length).join('');
|
|
35
|
-
var entityHTML = getEntityHTML(entity, originalText);
|
|
36
|
-
var elementHTML = (0, _getElementHTML["default"])(entityHTML, originalText);
|
|
37
|
-
var converted;
|
|
38
|
-
if (!!elementHTML || elementHTML === '') {
|
|
39
|
-
converted = (0, _toConsumableArray2["default"])(elementHTML);
|
|
40
|
-
} else {
|
|
41
|
-
converted = originalText;
|
|
42
|
-
}
|
|
43
|
-
var prefixLength = (0, _getElementTagLength["default"])(entityHTML, 'start');
|
|
44
|
-
var suffixLength = (0, _getElementTagLength["default"])(entityHTML, 'end');
|
|
45
|
-
var updateLaterMutation = function updateLaterMutation(mutation, mutationIndex) {
|
|
46
|
-
if (mutationIndex > index || Object.prototype.hasOwnProperty.call(mutation, 'style')) {
|
|
47
|
-
return (0, _updateMutation["default"])(mutation, entityRange.offset, entityRange.length, converted.length, prefixLength, suffixLength);
|
|
48
|
-
}
|
|
49
|
-
return mutation;
|
|
50
|
-
};
|
|
51
|
-
var updateLaterMutations = function updateLaterMutations(mutationList) {
|
|
52
|
-
return mutationList.reduce(function (acc, mutation, mutationIndex) {
|
|
53
|
-
var updatedMutation = updateLaterMutation(mutation, mutationIndex);
|
|
54
|
-
if (Array.isArray(updatedMutation)) {
|
|
55
|
-
return acc.concat(updatedMutation);
|
|
56
|
-
}
|
|
57
|
-
return acc.concat([updatedMutation]);
|
|
58
|
-
}, []);
|
|
59
|
-
};
|
|
60
|
-
entities = updateLaterMutations(entities);
|
|
61
|
-
styles = updateLaterMutations(styles);
|
|
62
|
-
resultText = [].concat((0, _toConsumableArray2["default"])(resultText.slice(0, entityRange.offset)), (0, _toConsumableArray2["default"])(converted), (0, _toConsumableArray2["default"])(resultText.slice(entityRange.offset + entityRange.length)));
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return _default;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _updateMutation = /*#__PURE__*/ _interop_require_default(require("./util/updateMutation"));
|
|
12
|
+
const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort"));
|
|
13
|
+
const _getElementHTML = /*#__PURE__*/ _interop_require_default(require("./util/getElementHTML"));
|
|
14
|
+
const _getElementTagLength = /*#__PURE__*/ _interop_require_default(require("./util/getElementTagLength"));
|
|
15
|
+
function _interop_require_default(obj) {
|
|
16
|
+
return obj && obj.__esModule ? obj : {
|
|
17
|
+
default: obj
|
|
63
18
|
};
|
|
64
|
-
|
|
65
|
-
|
|
19
|
+
}
|
|
20
|
+
const converter = (entity = {}, originalText = '')=>originalText;
|
|
21
|
+
const _default = (block, entityMap, entityConverter = converter)=>{
|
|
22
|
+
let resultText = [
|
|
23
|
+
...block.text
|
|
24
|
+
];
|
|
25
|
+
let getEntityHTML = entityConverter;
|
|
26
|
+
if (entityConverter.__isMiddleware) {
|
|
27
|
+
getEntityHTML = entityConverter(converter);
|
|
66
28
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
29
|
+
if (Object.prototype.hasOwnProperty.call(block, 'entityRanges') && block.entityRanges.length > 0) {
|
|
30
|
+
let entities = block.entityRanges.sort(_rangeSort.default);
|
|
31
|
+
let styles = block.inlineStyleRanges;
|
|
32
|
+
for(let index = 0; index < entities.length; index += 1){
|
|
33
|
+
const entityRange = entities[index];
|
|
34
|
+
const entity = entityMap[entityRange.key];
|
|
35
|
+
const originalText = resultText.slice(entityRange.offset, entityRange.offset + entityRange.length).join('');
|
|
36
|
+
const entityHTML = getEntityHTML(entity, originalText);
|
|
37
|
+
const elementHTML = (0, _getElementHTML.default)(entityHTML, originalText);
|
|
38
|
+
let converted;
|
|
39
|
+
if (!!elementHTML || elementHTML === '') {
|
|
40
|
+
converted = [
|
|
41
|
+
...elementHTML
|
|
42
|
+
];
|
|
43
|
+
} else {
|
|
44
|
+
converted = originalText;
|
|
45
|
+
}
|
|
46
|
+
const prefixLength = (0, _getElementTagLength.default)(entityHTML, 'start');
|
|
47
|
+
const suffixLength = (0, _getElementTagLength.default)(entityHTML, 'end');
|
|
48
|
+
const updateLaterMutation = (mutation, mutationIndex)=>{
|
|
49
|
+
if (mutationIndex > index || Object.prototype.hasOwnProperty.call(mutation, 'style')) {
|
|
50
|
+
return (0, _updateMutation.default)(mutation, entityRange.offset, entityRange.length, converted.length, prefixLength, suffixLength);
|
|
51
|
+
}
|
|
52
|
+
return mutation;
|
|
53
|
+
};
|
|
54
|
+
const updateLaterMutations = (mutationList)=>mutationList.reduce((acc, mutation, mutationIndex)=>{
|
|
55
|
+
const updatedMutation = updateLaterMutation(mutation, mutationIndex);
|
|
56
|
+
if (Array.isArray(updatedMutation)) {
|
|
57
|
+
return acc.concat(updatedMutation);
|
|
58
|
+
}
|
|
59
|
+
return acc.concat([
|
|
60
|
+
updatedMutation
|
|
61
|
+
]);
|
|
62
|
+
}, []);
|
|
63
|
+
entities = updateLaterMutations(entities);
|
|
64
|
+
styles = updateLaterMutations(styles);
|
|
65
|
+
resultText = [
|
|
66
|
+
...resultText.slice(0, entityRange.offset),
|
|
67
|
+
...converted,
|
|
68
|
+
...resultText.slice(entityRange.offset + entityRange.length)
|
|
69
|
+
];
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
...block,
|
|
73
|
+
text: resultText.join(''),
|
|
74
|
+
inlineStyleRanges: styles,
|
|
75
|
+
entityRanges: entities
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
return block;
|
|
79
|
+
};
|
package/lib/blockInlineStyles.js
CHANGED
|
@@ -1,105 +1,90 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
2
|
Object.defineProperty(exports, "__esModule", {
|
|
5
|
-
|
|
3
|
+
value: true
|
|
6
4
|
});
|
|
7
|
-
exports
|
|
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
|
-
|
|
5
|
+
Object.defineProperty(exports, "default", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return _default;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _invariant = /*#__PURE__*/ _interop_require_default(require("invariant"));
|
|
12
|
+
const _styleObjectFunction = /*#__PURE__*/ _interop_require_default(require("./util/styleObjectFunction"));
|
|
13
|
+
const _accumulateFunction = /*#__PURE__*/ _interop_require_default(require("./util/accumulateFunction"));
|
|
14
|
+
const _getElementHTML = /*#__PURE__*/ _interop_require_default(require("./util/getElementHTML"));
|
|
15
|
+
const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort"));
|
|
16
|
+
const _defaultInlineHTML = /*#__PURE__*/ _interop_require_default(require("./default/defaultInlineHTML"));
|
|
17
|
+
function _interop_require_default(obj) {
|
|
18
|
+
return obj && obj.__esModule ? obj : {
|
|
19
|
+
default: obj
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
const subtractStyles = (original, toRemove)=>original.filter((el)=>!toRemove.some((elToRemove)=>elToRemove.style === el.style));
|
|
23
|
+
const popEndingStyles = (styleStack, endingStyles)=>endingStyles.reduceRight((stack, style)=>{
|
|
24
|
+
const styleToRemove = stack[stack.length - 1];
|
|
25
|
+
(0, _invariant.default)(styleToRemove.style === style.style, `Style ${styleToRemove.style} to be removed doesn't match expected ${style.style}`);
|
|
26
|
+
return stack.slice(0, -1);
|
|
27
|
+
}, styleStack);
|
|
28
|
+
const characterStyles = (offset, ranges)=>ranges.filter((range)=>offset >= range.offset && offset < range.offset + range.length);
|
|
29
|
+
const rangeIsSubset = (firstRange, secondRange)=>{
|
|
30
|
+
// returns true if the second range is a subset of the first
|
|
31
|
+
const secondStartWithinFirst = firstRange.offset <= secondRange.offset;
|
|
32
|
+
const secondEndWithinFirst = firstRange.offset + firstRange.length >= secondRange.offset + secondRange.length;
|
|
33
|
+
return secondStartWithinFirst && secondEndWithinFirst;
|
|
33
34
|
};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
const latestStyleLast = (s1, s2)=>{
|
|
36
|
+
// make sure longer-lasting styles are added first
|
|
37
|
+
const s2endIndex = s2.offset + s2.length;
|
|
38
|
+
const s1endIndex = s1.offset + s1.length;
|
|
39
|
+
return s2endIndex - s1endIndex;
|
|
39
40
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
const getStylesToReset = (remainingStyles, newStyles)=>{
|
|
42
|
+
let i = 0;
|
|
43
|
+
while(i < remainingStyles.length){
|
|
44
|
+
if (newStyles.every(rangeIsSubset.bind(null, remainingStyles[i]))) {
|
|
45
|
+
i += 1;
|
|
46
|
+
} else {
|
|
47
|
+
return remainingStyles.slice(i);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return [];
|
|
45
51
|
};
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
52
|
+
const appendStartMarkup = (inlineHTML, string, styleRange)=>string + (0, _getElementHTML.default)(inlineHTML(styleRange.style)).start;
|
|
53
|
+
const prependEndMarkup = (inlineHTML, string, styleRange)=>(0, _getElementHTML.default)(inlineHTML(styleRange.style)).end + string;
|
|
54
|
+
const defaultCustomInlineHTML = (next)=>(style)=>next(style);
|
|
55
|
+
defaultCustomInlineHTML.__isMiddleware = true;
|
|
56
|
+
const _default = (rawBlock, customInlineHTML = defaultCustomInlineHTML)=>{
|
|
57
|
+
(0, _invariant.default)(rawBlock !== null && rawBlock !== undefined, 'Expected raw block to be non-null');
|
|
58
|
+
let inlineHTML;
|
|
59
|
+
if (customInlineHTML.__isMiddleware === true) {
|
|
60
|
+
inlineHTML = customInlineHTML(_defaultInlineHTML.default);
|
|
51
61
|
} else {
|
|
52
|
-
|
|
62
|
+
inlineHTML = (0, _accumulateFunction.default)((0, _styleObjectFunction.default)(customInlineHTML), (0, _styleObjectFunction.default)(_defaultInlineHTML.default));
|
|
53
63
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
let result = '';
|
|
65
|
+
let styleStack = [];
|
|
66
|
+
const sortedRanges = rawBlock.inlineStyleRanges.sort(_rangeSort.default);
|
|
67
|
+
const originalTextArray = [
|
|
68
|
+
...rawBlock.text
|
|
69
|
+
];
|
|
70
|
+
for(let i = 0; i < originalTextArray.length; i += 1){
|
|
71
|
+
const styles = characterStyles(i, sortedRanges);
|
|
72
|
+
const endingStyles = subtractStyles(styleStack, styles);
|
|
73
|
+
const newStyles = subtractStyles(styles, styleStack);
|
|
74
|
+
const remainingStyles = subtractStyles(styleStack, endingStyles);
|
|
75
|
+
// reset styles: look for any already existing styles that will need to
|
|
76
|
+
// end before styles that are being added on this character. to solve this
|
|
77
|
+
// close out those current tags and all nested children,
|
|
78
|
+
// then open new ones nested within the new styles.
|
|
79
|
+
const resetStyles = getStylesToReset(remainingStyles, newStyles);
|
|
80
|
+
const openingStyles = resetStyles.concat(newStyles).sort(latestStyleLast);
|
|
81
|
+
const openingStyleTags = openingStyles.reduce(appendStartMarkup.bind(null, inlineHTML), '');
|
|
82
|
+
const endingStyleTags = endingStyles.concat(resetStyles).reduce(prependEndMarkup.bind(null, inlineHTML), '');
|
|
83
|
+
result += endingStyleTags + openingStyleTags + originalTextArray[i];
|
|
84
|
+
styleStack = popEndingStyles(styleStack, resetStyles.concat(endingStyles));
|
|
85
|
+
styleStack = styleStack.concat(openingStyles);
|
|
86
|
+
(0, _invariant.default)(styleStack.length === styles.length, `Character ${i}: ${styleStack.length - styles.length} styles left on stack that should no longer be there`);
|
|
87
|
+
}
|
|
88
|
+
result = styleStack.reduceRight((res, openStyle)=>res + (0, _getElementHTML.default)(inlineHTML(openStyle.style)).end, result);
|
|
89
|
+
return result;
|
|
67
90
|
};
|
|
68
|
-
defaultCustomInlineHTML.__isMiddleware = true;
|
|
69
|
-
var _default = exports["default"] = function _default(rawBlock) {
|
|
70
|
-
var customInlineHTML = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultCustomInlineHTML;
|
|
71
|
-
(0, _invariant["default"])(rawBlock !== null && rawBlock !== undefined, 'Expected raw block to be non-null');
|
|
72
|
-
var inlineHTML;
|
|
73
|
-
if (customInlineHTML.__isMiddleware === true) {
|
|
74
|
-
inlineHTML = customInlineHTML(_defaultInlineHTML["default"]);
|
|
75
|
-
} else {
|
|
76
|
-
inlineHTML = (0, _accumulateFunction["default"])((0, _styleObjectFunction["default"])(customInlineHTML), (0, _styleObjectFunction["default"])(_defaultInlineHTML["default"]));
|
|
77
|
-
}
|
|
78
|
-
var result = '';
|
|
79
|
-
var styleStack = [];
|
|
80
|
-
var sortedRanges = rawBlock.inlineStyleRanges.sort(_rangeSort["default"]);
|
|
81
|
-
var originalTextArray = (0, _toConsumableArray2["default"])(rawBlock.text);
|
|
82
|
-
for (var i = 0; i < originalTextArray.length; i += 1) {
|
|
83
|
-
var styles = characterStyles(i, sortedRanges);
|
|
84
|
-
var endingStyles = subtractStyles(styleStack, styles);
|
|
85
|
-
var newStyles = subtractStyles(styles, styleStack);
|
|
86
|
-
var remainingStyles = subtractStyles(styleStack, endingStyles);
|
|
87
|
-
|
|
88
|
-
// reset styles: look for any already existing styles that will need to
|
|
89
|
-
// end before styles that are being added on this character. to solve this
|
|
90
|
-
// close out those current tags and all nested children,
|
|
91
|
-
// then open new ones nested within the new styles.
|
|
92
|
-
var resetStyles = getStylesToReset(remainingStyles, newStyles);
|
|
93
|
-
var openingStyles = resetStyles.concat(newStyles).sort(latestStyleLast);
|
|
94
|
-
var openingStyleTags = openingStyles.reduce(appendStartMarkup.bind(null, inlineHTML), '');
|
|
95
|
-
var endingStyleTags = endingStyles.concat(resetStyles).reduce(prependEndMarkup.bind(null, inlineHTML), '');
|
|
96
|
-
result += endingStyleTags + openingStyleTags + originalTextArray[i];
|
|
97
|
-
styleStack = popEndingStyles(styleStack, resetStyles.concat(endingStyles));
|
|
98
|
-
styleStack = styleStack.concat(openingStyles);
|
|
99
|
-
(0, _invariant["default"])(styleStack.length === styles.length, "Character ".concat(i, ": ").concat(styleStack.length - styles.length, " styles left on stack that should no longer be there"));
|
|
100
|
-
}
|
|
101
|
-
result = styleStack.reduceRight(function (res, openStyle) {
|
|
102
|
-
return res + (0, _getElementHTML["default"])(inlineHTML(openStyle.style)).end;
|
|
103
|
-
}, result);
|
|
104
|
-
return result;
|
|
105
|
-
};
|