@20minutes/draft-convert 3.0.0 → 3.0.1
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 +29 -0
- package/dist/draft-convert.js +440 -0
- package/dist/draft-convert.min.js +1 -0
- package/esm/blockEntities.js +67 -0
- package/esm/blockInlineStyles.js +98 -0
- package/esm/convertFromHTML.js +537 -0
- package/esm/convertToHTML.js +107 -0
- package/esm/default/defaultBlockHTML.js +31 -0
- package/esm/default/defaultInlineHTML.js +18 -0
- package/esm/encodeBlock.js +44 -0
- package/esm/index.js +4 -0
- package/esm/util/accumulateFunction.js +9 -0
- package/esm/util/blockTypeObjectFunction.js +9 -0
- package/esm/util/getBlockTags.js +27 -0
- package/esm/util/getElementHTML.js +36 -0
- package/esm/util/getElementTagLength.js +20 -0
- package/esm/util/getNestedBlockTags.js +29 -0
- package/esm/util/parseHTML.js +18 -0
- package/esm/util/rangeSort.js +6 -0
- package/esm/util/splitReactElement.js +18 -0
- package/esm/util/styleObjectFunction.js +8 -0
- package/esm/util/updateMutation.js +48 -0
- package/lib/blockEntities.js +74 -0
- package/lib/blockInlineStyles.js +105 -0
- package/lib/convertFromHTML.js +544 -0
- package/lib/convertToHTML.js +114 -0
- package/lib/default/defaultBlockHTML.js +37 -0
- package/lib/default/defaultInlineHTML.js +25 -0
- package/lib/encodeBlock.js +51 -0
- package/lib/index.js +27 -0
- package/lib/util/accumulateFunction.js +15 -0
- package/lib/util/blockTypeObjectFunction.js +15 -0
- package/lib/util/getBlockTags.js +34 -0
- package/lib/util/getElementHTML.js +43 -0
- package/lib/util/getElementTagLength.js +27 -0
- package/lib/util/getNestedBlockTags.js +36 -0
- package/lib/util/parseHTML.js +24 -0
- package/lib/util/rangeSort.js +12 -0
- package/lib/util/splitReactElement.js +24 -0
- package/lib/util/styleObjectFunction.js +14 -0
- package/lib/util/updateMutation.js +55 -0
- package/package.json +1 -1
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
|
+
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
3
|
+
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; }
|
|
4
|
+
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; }
|
|
5
|
+
import updateMutation from './util/updateMutation';
|
|
6
|
+
import rangeSort from './util/rangeSort';
|
|
7
|
+
var ENTITY_MAP = {
|
|
8
|
+
'&': '&',
|
|
9
|
+
'<': '<',
|
|
10
|
+
'>': '>',
|
|
11
|
+
'"': '"',
|
|
12
|
+
"'": ''',
|
|
13
|
+
'`': '`',
|
|
14
|
+
'\n': '<br/>'
|
|
15
|
+
};
|
|
16
|
+
export default (function (block) {
|
|
17
|
+
var blockText = _toConsumableArray(block.text);
|
|
18
|
+
var entities = block.entityRanges.sort(rangeSort);
|
|
19
|
+
var styles = block.inlineStyleRanges.sort(rangeSort);
|
|
20
|
+
var resultText = '';
|
|
21
|
+
var _loop = function _loop() {
|
|
22
|
+
var _char = blockText[index];
|
|
23
|
+
if (ENTITY_MAP[_char] !== undefined) {
|
|
24
|
+
var encoded = ENTITY_MAP[_char];
|
|
25
|
+
var resultIndex = _toConsumableArray(resultText).length;
|
|
26
|
+
resultText += encoded;
|
|
27
|
+
var updateForChar = function updateForChar(mutation) {
|
|
28
|
+
return updateMutation(mutation, resultIndex, _char.length, encoded.length, 0, 0);
|
|
29
|
+
};
|
|
30
|
+
entities = entities.map(updateForChar);
|
|
31
|
+
styles = styles.map(updateForChar);
|
|
32
|
+
} else {
|
|
33
|
+
resultText += _char;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
for (var index = 0; index < blockText.length; index += 1) {
|
|
37
|
+
_loop();
|
|
38
|
+
}
|
|
39
|
+
return _objectSpread(_objectSpread({}, block), {}, {
|
|
40
|
+
text: resultText,
|
|
41
|
+
inlineStyleRanges: styles,
|
|
42
|
+
entityRanges: entities
|
|
43
|
+
});
|
|
44
|
+
});
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
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
|
+
import invariant from 'invariant';
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import ReactDOMServer from 'react-dom/server';
|
|
7
|
+
import splitReactElement from './splitReactElement';
|
|
8
|
+
function hasChildren(element) {
|
|
9
|
+
return /*#__PURE__*/React.isValidElement(element) && React.Children.count(element.props.children) > 0;
|
|
10
|
+
}
|
|
11
|
+
export default function getBlockTags(blockHTML) {
|
|
12
|
+
invariant(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null');
|
|
13
|
+
if (typeof blockHTML === 'string') {
|
|
14
|
+
return blockHTML;
|
|
15
|
+
}
|
|
16
|
+
if ( /*#__PURE__*/React.isValidElement(blockHTML)) {
|
|
17
|
+
if (hasChildren(blockHTML)) {
|
|
18
|
+
return ReactDOMServer.renderToStaticMarkup(blockHTML);
|
|
19
|
+
}
|
|
20
|
+
return splitReactElement(blockHTML);
|
|
21
|
+
}
|
|
22
|
+
if (Object.prototype.hasOwnProperty.call(blockHTML, 'element') && /*#__PURE__*/React.isValidElement(blockHTML.element)) {
|
|
23
|
+
return _objectSpread(_objectSpread({}, blockHTML), splitReactElement(blockHTML.element));
|
|
24
|
+
}
|
|
25
|
+
invariant(Object.prototype.hasOwnProperty.call(blockHTML, 'start') && Object.prototype.hasOwnProperty.call(blockHTML, 'end'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags');
|
|
26
|
+
return blockHTML;
|
|
27
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
2
|
+
import invariant from 'invariant';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import ReactDOMServer from 'react-dom/server';
|
|
5
|
+
import splitReactElement from './splitReactElement';
|
|
6
|
+
function hasChildren(element) {
|
|
7
|
+
return /*#__PURE__*/React.isValidElement(element) && React.Children.count(element.props.children) > 0;
|
|
8
|
+
}
|
|
9
|
+
export default function getElementHTML(element) {
|
|
10
|
+
var text = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
11
|
+
if (element === undefined || element === null) {
|
|
12
|
+
return element;
|
|
13
|
+
}
|
|
14
|
+
if (typeof element === 'string') {
|
|
15
|
+
return element;
|
|
16
|
+
}
|
|
17
|
+
if ( /*#__PURE__*/React.isValidElement(element)) {
|
|
18
|
+
if (hasChildren(element)) {
|
|
19
|
+
return ReactDOMServer.renderToStaticMarkup(element);
|
|
20
|
+
}
|
|
21
|
+
var tags = splitReactElement(element);
|
|
22
|
+
if (text !== null && _typeof(tags) === 'object') {
|
|
23
|
+
var start = tags.start,
|
|
24
|
+
end = tags.end;
|
|
25
|
+
return start + text + end;
|
|
26
|
+
}
|
|
27
|
+
return tags;
|
|
28
|
+
}
|
|
29
|
+
invariant(Object.prototype.hasOwnProperty.call(element, 'start') && Object.prototype.hasOwnProperty.call(element, 'end'), 'convertToHTML: received conversion data without either an HTML string, ReactElement or an object with start/end tags');
|
|
30
|
+
if (text !== null) {
|
|
31
|
+
var _start = element.start,
|
|
32
|
+
_end = element.end;
|
|
33
|
+
return _start + text + _end;
|
|
34
|
+
}
|
|
35
|
+
return element;
|
|
36
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import splitReactElement from './splitReactElement';
|
|
4
|
+
var getElementTagLength = function getElementTagLength(element) {
|
|
5
|
+
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'start';
|
|
6
|
+
if ( /*#__PURE__*/React.isValidElement(element)) {
|
|
7
|
+
var splitElement = splitReactElement(element);
|
|
8
|
+
if (typeof splitElement === 'string') {
|
|
9
|
+
return 0;
|
|
10
|
+
}
|
|
11
|
+
var length = splitElement[type].length;
|
|
12
|
+
var child = React.Children.toArray(element.props.children)[0];
|
|
13
|
+
return length + (child && /*#__PURE__*/React.isValidElement(child) ? getElementTagLength(child, type) : 0);
|
|
14
|
+
}
|
|
15
|
+
if (_typeof(element) === 'object') {
|
|
16
|
+
return element[type] ? element[type].length : 0;
|
|
17
|
+
}
|
|
18
|
+
return 0;
|
|
19
|
+
};
|
|
20
|
+
export default getElementTagLength;
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
import invariant from 'invariant';
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import splitReactElement from './splitReactElement';
|
|
7
|
+
export default function getNestedBlockTags(blockHTML, depth) {
|
|
8
|
+
invariant(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null');
|
|
9
|
+
if (typeof blockHTML.nest === 'function') {
|
|
10
|
+
var _splitReactElement = splitReactElement(blockHTML.nest(depth)),
|
|
11
|
+
start = _splitReactElement.start,
|
|
12
|
+
end = _splitReactElement.end;
|
|
13
|
+
return _objectSpread(_objectSpread({}, blockHTML), {}, {
|
|
14
|
+
nestStart: start,
|
|
15
|
+
nestEnd: end
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
if ( /*#__PURE__*/React.isValidElement(blockHTML.nest)) {
|
|
19
|
+
var _splitReactElement2 = splitReactElement(blockHTML.nest),
|
|
20
|
+
_start = _splitReactElement2.start,
|
|
21
|
+
_end = _splitReactElement2.end;
|
|
22
|
+
return _objectSpread(_objectSpread({}, blockHTML), {}, {
|
|
23
|
+
nestStart: _start,
|
|
24
|
+
nestEnd: _end
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
invariant(Object.prototype.hasOwnProperty.call(blockHTML, 'nestStart') && Object.prototype.hasOwnProperty.call(blockHTML, 'nestEnd'), 'convertToHTML: received block information without either a ReactElement or an object with start/end tags');
|
|
28
|
+
return blockHTML;
|
|
29
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var fallback = function fallback(html) {
|
|
2
|
+
var doc = document.implementation.createHTMLDocument('');
|
|
3
|
+
doc.documentElement.innerHTML = html;
|
|
4
|
+
return doc;
|
|
5
|
+
};
|
|
6
|
+
export default function parseHTML(html) {
|
|
7
|
+
var doc;
|
|
8
|
+
if (typeof DOMParser !== 'undefined') {
|
|
9
|
+
var parser = new DOMParser();
|
|
10
|
+
doc = parser.parseFromString(html, 'text/html');
|
|
11
|
+
if (doc === null || doc.body === null) {
|
|
12
|
+
doc = fallback(html);
|
|
13
|
+
}
|
|
14
|
+
} else {
|
|
15
|
+
doc = fallback(html);
|
|
16
|
+
}
|
|
17
|
+
return doc.body;
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import invariant from 'invariant';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import ReactDOMServer from 'react-dom/server';
|
|
4
|
+
|
|
5
|
+
// see http://w3c.github.io/html/syntax.html#writing-html-documents-elements
|
|
6
|
+
var VOID_TAGS = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
|
|
7
|
+
export default function splitReactElement(element) {
|
|
8
|
+
if (VOID_TAGS.indexOf(element.type) !== -1) {
|
|
9
|
+
return ReactDOMServer.renderToStaticMarkup(element);
|
|
10
|
+
}
|
|
11
|
+
var tags = ReactDOMServer.renderToStaticMarkup( /*#__PURE__*/React.cloneElement(element, {}, '\r')).split('\r');
|
|
12
|
+
invariant(tags.length > 1, "convertToHTML: Element of type ".concat(element.type, " must render children"));
|
|
13
|
+
invariant(tags.length < 3, "convertToHTML: Element of type ".concat(element.type, " cannot use carriage return character"));
|
|
14
|
+
return {
|
|
15
|
+
start: tags[0],
|
|
16
|
+
end: tags[1]
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
export default function updateMutation(mutation, originalOffset, originalLength, newLength, prefixLength, suffixLength) {
|
|
5
|
+
// three cases we can reasonably adjust - disjoint mutations that
|
|
6
|
+
// happen later on where the offset will need to be changed,
|
|
7
|
+
// mutations that completely contain the new one where we can adjust
|
|
8
|
+
// the length, and mutations that occur partially within the new one.
|
|
9
|
+
var lengthDiff = newLength - originalLength;
|
|
10
|
+
var mutationAfterChange = originalOffset + originalLength <= mutation.offset;
|
|
11
|
+
if (mutationAfterChange) {
|
|
12
|
+
return _objectSpread(_objectSpread({}, mutation), {}, {
|
|
13
|
+
offset: mutation.offset + lengthDiff
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
var mutationContainsChange = originalOffset >= mutation.offset && originalOffset + originalLength <= mutation.offset + mutation.length;
|
|
17
|
+
if (mutationContainsChange) {
|
|
18
|
+
return _objectSpread(_objectSpread({}, mutation), {}, {
|
|
19
|
+
length: mutation.length + lengthDiff
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
var mutationWithinPrefixChange = mutation.offset >= originalOffset && mutation.offset + mutation.length <= originalOffset + originalLength && prefixLength > 0;
|
|
23
|
+
if (mutationWithinPrefixChange) {
|
|
24
|
+
return _objectSpread(_objectSpread({}, mutation), {}, {
|
|
25
|
+
offset: mutation.offset + prefixLength
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
var mutationContainsPrefix = mutation.offset < originalOffset && mutation.offset + mutation.length <= originalOffset + originalLength && mutation.offset + mutation.length > originalOffset && prefixLength > 0;
|
|
29
|
+
if (mutationContainsPrefix) {
|
|
30
|
+
return [_objectSpread(_objectSpread({}, mutation), {}, {
|
|
31
|
+
length: originalOffset - mutation.offset
|
|
32
|
+
}), _objectSpread(_objectSpread({}, mutation), {}, {
|
|
33
|
+
offset: originalOffset + prefixLength,
|
|
34
|
+
length: mutation.offset - originalOffset + mutation.length
|
|
35
|
+
})];
|
|
36
|
+
}
|
|
37
|
+
var mutationContainsSuffix = mutation.offset >= originalOffset && mutation.offset + mutation.length > originalOffset + originalLength && originalOffset + originalLength > mutation.offset && suffixLength > 0;
|
|
38
|
+
if (mutationContainsSuffix) {
|
|
39
|
+
return [_objectSpread(_objectSpread({}, mutation), {}, {
|
|
40
|
+
offset: mutation.offset + prefixLength,
|
|
41
|
+
length: originalOffset + originalLength - mutation.offset
|
|
42
|
+
}), _objectSpread(_objectSpread({}, mutation), {}, {
|
|
43
|
+
offset: originalOffset + originalLength + prefixLength + suffixLength,
|
|
44
|
+
length: mutation.offset + mutation.length - (originalOffset + originalLength)
|
|
45
|
+
})];
|
|
46
|
+
}
|
|
47
|
+
return mutation;
|
|
48
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports["default"] = void 0;
|
|
8
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
9
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
10
|
+
var _updateMutation = _interopRequireDefault(require("./util/updateMutation"));
|
|
11
|
+
var _rangeSort = _interopRequireDefault(require("./util/rangeSort"));
|
|
12
|
+
var _getElementHTML = _interopRequireDefault(require("./util/getElementHTML"));
|
|
13
|
+
var _getElementTagLength = _interopRequireDefault(require("./util/getElementTagLength"));
|
|
14
|
+
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; }
|
|
15
|
+
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) { (0, _defineProperty2["default"])(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; }
|
|
16
|
+
var converter = function converter() {
|
|
17
|
+
var entity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
18
|
+
var originalText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
|
|
19
|
+
return originalText;
|
|
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)));
|
|
63
|
+
};
|
|
64
|
+
for (var index = 0; index < entities.length; index += 1) {
|
|
65
|
+
_loop(index);
|
|
66
|
+
}
|
|
67
|
+
return _objectSpread(_objectSpread({}, block), {}, {
|
|
68
|
+
text: resultText.join(''),
|
|
69
|
+
inlineStyleRanges: styles,
|
|
70
|
+
entityRanges: entities
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return block;
|
|
74
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports["default"] = void 0;
|
|
8
|
+
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
|
9
|
+
var _invariant = _interopRequireDefault(require("invariant"));
|
|
10
|
+
var _styleObjectFunction = _interopRequireDefault(require("./util/styleObjectFunction"));
|
|
11
|
+
var _accumulateFunction = _interopRequireDefault(require("./util/accumulateFunction"));
|
|
12
|
+
var _getElementHTML = _interopRequireDefault(require("./util/getElementHTML"));
|
|
13
|
+
var _rangeSort = _interopRequireDefault(require("./util/rangeSort"));
|
|
14
|
+
var _defaultInlineHTML = _interopRequireDefault(require("./default/defaultInlineHTML"));
|
|
15
|
+
var subtractStyles = function subtractStyles(original, toRemove) {
|
|
16
|
+
return original.filter(function (el) {
|
|
17
|
+
return !toRemove.some(function (elToRemove) {
|
|
18
|
+
return elToRemove.style === el.style;
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
var popEndingStyles = function popEndingStyles(styleStack, endingStyles) {
|
|
23
|
+
return endingStyles.reduceRight(function (stack, style) {
|
|
24
|
+
var styleToRemove = stack[stack.length - 1];
|
|
25
|
+
(0, _invariant["default"])(styleToRemove.style === style.style, "Style ".concat(styleToRemove.style, " to be removed doesn't match expected ").concat(style.style));
|
|
26
|
+
return stack.slice(0, -1);
|
|
27
|
+
}, styleStack);
|
|
28
|
+
};
|
|
29
|
+
var characterStyles = function characterStyles(offset, ranges) {
|
|
30
|
+
return ranges.filter(function (range) {
|
|
31
|
+
return offset >= range.offset && offset < range.offset + range.length;
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var rangeIsSubset = function rangeIsSubset(firstRange, secondRange) {
|
|
35
|
+
// returns true if the second range is a subset of the first
|
|
36
|
+
var secondStartWithinFirst = firstRange.offset <= secondRange.offset;
|
|
37
|
+
var secondEndWithinFirst = firstRange.offset + firstRange.length >= secondRange.offset + secondRange.length;
|
|
38
|
+
return secondStartWithinFirst && secondEndWithinFirst;
|
|
39
|
+
};
|
|
40
|
+
var latestStyleLast = function latestStyleLast(s1, s2) {
|
|
41
|
+
// make sure longer-lasting styles are added first
|
|
42
|
+
var s2endIndex = s2.offset + s2.length;
|
|
43
|
+
var s1endIndex = s1.offset + s1.length;
|
|
44
|
+
return s2endIndex - s1endIndex;
|
|
45
|
+
};
|
|
46
|
+
var getStylesToReset = function getStylesToReset(remainingStyles, newStyles) {
|
|
47
|
+
var i = 0;
|
|
48
|
+
while (i < remainingStyles.length) {
|
|
49
|
+
if (newStyles.every(rangeIsSubset.bind(null, remainingStyles[i]))) {
|
|
50
|
+
i += 1;
|
|
51
|
+
} else {
|
|
52
|
+
return remainingStyles.slice(i);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return [];
|
|
56
|
+
};
|
|
57
|
+
var appendStartMarkup = function appendStartMarkup(inlineHTML, string, styleRange) {
|
|
58
|
+
return string + (0, _getElementHTML["default"])(inlineHTML(styleRange.style)).start;
|
|
59
|
+
};
|
|
60
|
+
var prependEndMarkup = function prependEndMarkup(inlineHTML, string, styleRange) {
|
|
61
|
+
return (0, _getElementHTML["default"])(inlineHTML(styleRange.style)).end + string;
|
|
62
|
+
};
|
|
63
|
+
var defaultCustomInlineHTML = function defaultCustomInlineHTML(next) {
|
|
64
|
+
return function (style) {
|
|
65
|
+
return next(style);
|
|
66
|
+
};
|
|
67
|
+
};
|
|
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
|
+
};
|