@20minutes/draft-convert 3.0.2 → 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 +31 -35
- package/dist/draft-convert.js +0 -440
- package/dist/draft-convert.min.js +0 -1
package/esm/convertToHTML.js
CHANGED
|
@@ -10,98 +10,76 @@ import blockTypeObjectFunction from './util/blockTypeObjectFunction';
|
|
|
10
10
|
import getBlockTags from './util/getBlockTags';
|
|
11
11
|
import getNestedBlockTags from './util/getNestedBlockTags';
|
|
12
12
|
import defaultBlockHTML from './default/defaultBlockHTML';
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
var _ref$styleToHTML = _ref.styleToHTML,
|
|
21
|
-
styleToHTML = _ref$styleToHTML === void 0 ? {} : _ref$styleToHTML,
|
|
22
|
-
_ref$blockToHTML = _ref.blockToHTML,
|
|
23
|
-
blockToHTML = _ref$blockToHTML === void 0 ? {} : _ref$blockToHTML,
|
|
24
|
-
_ref$entityToHTML = _ref.entityToHTML,
|
|
25
|
-
entityToHTML = _ref$entityToHTML === void 0 ? defaultEntityToHTML : _ref$entityToHTML,
|
|
26
|
-
_ref$validateHTML = _ref.validateHTML,
|
|
27
|
-
validateHTML = _ref$validateHTML === void 0 ? defaultValidateHTML : _ref$validateHTML;
|
|
28
|
-
return function (contentState) {
|
|
29
|
-
invariant(contentState !== null && contentState !== undefined, 'Expected contentState to be non-null');
|
|
30
|
-
var getBlockHTML;
|
|
31
|
-
if (blockToHTML.__isMiddleware === true) {
|
|
32
|
-
getBlockHTML = blockToHTML(blockTypeObjectFunction(defaultBlockHTML));
|
|
33
|
-
} else {
|
|
34
|
-
getBlockHTML = accumulateFunction(blockTypeObjectFunction(blockToHTML), blockTypeObjectFunction(defaultBlockHTML));
|
|
35
|
-
}
|
|
36
|
-
var rawState = convertToRaw(contentState);
|
|
37
|
-
var listStack = [];
|
|
38
|
-
var result = rawState.blocks.map(function (block) {
|
|
39
|
-
var type = block.type,
|
|
40
|
-
depth = block.depth;
|
|
41
|
-
var closeNestTags = '';
|
|
42
|
-
var openNestTags = '';
|
|
43
|
-
var blockHTMLResult = getBlockHTML(block);
|
|
44
|
-
if (!blockHTMLResult) {
|
|
45
|
-
throw new Error("convertToHTML: missing HTML definition for block with type ".concat(block.type));
|
|
46
|
-
}
|
|
47
|
-
if (!blockHTMLResult.nest) {
|
|
48
|
-
// this block can't be nested, so reset all nesting if necessary
|
|
49
|
-
closeNestTags = listStack.reduceRight(function (string, nestedBlock) {
|
|
50
|
-
return string + getNestedBlockTags(getBlockHTML(nestedBlock), depth).nestEnd;
|
|
51
|
-
}, '');
|
|
52
|
-
listStack = [];
|
|
53
|
-
} else {
|
|
54
|
-
while (depth + 1 !== listStack.length || type !== listStack[depth].type) {
|
|
55
|
-
if (depth + 1 === listStack.length) {
|
|
56
|
-
// depth is right but doesn't match type
|
|
57
|
-
var blockToClose = listStack[depth];
|
|
58
|
-
closeNestTags += getNestedBlockTags(getBlockHTML(blockToClose), depth).nestEnd;
|
|
59
|
-
openNestTags += getNestedBlockTags(getBlockHTML(block), depth).nestStart;
|
|
60
|
-
listStack[depth] = block;
|
|
61
|
-
} else if (depth + 1 < listStack.length) {
|
|
62
|
-
var _blockToClose = listStack[listStack.length - 1];
|
|
63
|
-
closeNestTags += getNestedBlockTags(getBlockHTML(_blockToClose), depth).nestEnd;
|
|
64
|
-
listStack = listStack.slice(0, -1);
|
|
65
|
-
} else {
|
|
66
|
-
openNestTags += getNestedBlockTags(getBlockHTML(block), depth).nestStart;
|
|
67
|
-
listStack.push(block);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
var innerHTML = blockInlineStyles(blockEntities(encodeBlock(block), rawState.entityMap, entityToHTML), styleToHTML);
|
|
72
|
-
var blockHTML = getBlockTags(getBlockHTML(block));
|
|
73
|
-
var html;
|
|
74
|
-
if (typeof blockHTML === 'string') {
|
|
75
|
-
html = blockHTML;
|
|
76
|
-
} else {
|
|
77
|
-
html = blockHTML.start + innerHTML + blockHTML.end;
|
|
78
|
-
}
|
|
79
|
-
if (innerHTML.length === 0 && Object.prototype.hasOwnProperty.call(blockHTML, 'empty')) {
|
|
80
|
-
if (/*#__PURE__*/React.isValidElement(blockHTML.empty)) {
|
|
81
|
-
html = ReactDOMServer.renderToStaticMarkup(blockHTML.empty);
|
|
13
|
+
const defaultEntityToHTML = (entity, originalText)=>originalText;
|
|
14
|
+
const defaultValidateHTML = (html)=>true;
|
|
15
|
+
const convertToHTML = ({ styleToHTML = {}, blockToHTML = {}, entityToHTML = defaultEntityToHTML, validateHTML = defaultValidateHTML })=>(contentState)=>{
|
|
16
|
+
invariant(contentState !== null && contentState !== undefined, 'Expected contentState to be non-null');
|
|
17
|
+
let getBlockHTML;
|
|
18
|
+
if (blockToHTML.__isMiddleware === true) {
|
|
19
|
+
getBlockHTML = blockToHTML(blockTypeObjectFunction(defaultBlockHTML));
|
|
82
20
|
} else {
|
|
83
|
-
|
|
21
|
+
getBlockHTML = accumulateFunction(blockTypeObjectFunction(blockToHTML), blockTypeObjectFunction(defaultBlockHTML));
|
|
84
22
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
})
|
|
23
|
+
const rawState = convertToRaw(contentState);
|
|
24
|
+
let listStack = [];
|
|
25
|
+
let result = rawState.blocks.map((block)=>{
|
|
26
|
+
const { type, depth } = block;
|
|
27
|
+
let closeNestTags = '';
|
|
28
|
+
let openNestTags = '';
|
|
29
|
+
const blockHTMLResult = getBlockHTML(block);
|
|
30
|
+
if (!blockHTMLResult) {
|
|
31
|
+
throw new Error(`convertToHTML: missing HTML definition for block with type ${block.type}`);
|
|
32
|
+
}
|
|
33
|
+
if (!blockHTMLResult.nest) {
|
|
34
|
+
// this block can't be nested, so reset all nesting if necessary
|
|
35
|
+
closeNestTags = listStack.reduceRight((string, nestedBlock)=>string + getNestedBlockTags(getBlockHTML(nestedBlock), depth).nestEnd, '');
|
|
36
|
+
listStack = [];
|
|
37
|
+
} else {
|
|
38
|
+
while(depth + 1 !== listStack.length || type !== listStack[depth].type){
|
|
39
|
+
if (depth + 1 === listStack.length) {
|
|
40
|
+
// depth is right but doesn't match type
|
|
41
|
+
const blockToClose = listStack[depth];
|
|
42
|
+
closeNestTags += getNestedBlockTags(getBlockHTML(blockToClose), depth).nestEnd;
|
|
43
|
+
openNestTags += getNestedBlockTags(getBlockHTML(block), depth).nestStart;
|
|
44
|
+
listStack[depth] = block;
|
|
45
|
+
} else if (depth + 1 < listStack.length) {
|
|
46
|
+
const blockToClose = listStack[listStack.length - 1];
|
|
47
|
+
closeNestTags += getNestedBlockTags(getBlockHTML(blockToClose), depth).nestEnd;
|
|
48
|
+
listStack = listStack.slice(0, -1);
|
|
49
|
+
} else {
|
|
50
|
+
openNestTags += getNestedBlockTags(getBlockHTML(block), depth).nestStart;
|
|
51
|
+
listStack.push(block);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const innerHTML = blockInlineStyles(blockEntities(encodeBlock(block), rawState.entityMap, entityToHTML), styleToHTML);
|
|
56
|
+
const blockHTML = getBlockTags(getBlockHTML(block));
|
|
57
|
+
let html;
|
|
58
|
+
if (typeof blockHTML === 'string') {
|
|
59
|
+
html = blockHTML;
|
|
60
|
+
} else {
|
|
61
|
+
html = blockHTML.start + innerHTML + blockHTML.end;
|
|
62
|
+
}
|
|
63
|
+
if (innerHTML.length === 0 && Object.prototype.hasOwnProperty.call(blockHTML, 'empty')) {
|
|
64
|
+
if (/*#__PURE__*/ React.isValidElement(blockHTML.empty)) {
|
|
65
|
+
html = ReactDOMServer.renderToStaticMarkup(blockHTML.empty);
|
|
66
|
+
} else {
|
|
67
|
+
html = blockHTML.empty;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const finalHtml = closeNestTags + openNestTags + html;
|
|
71
|
+
if (!validateHTML(finalHtml)) {
|
|
72
|
+
return '';
|
|
73
|
+
}
|
|
74
|
+
return finalHtml;
|
|
75
|
+
}).join('');
|
|
76
|
+
result = listStack.reduce((res, nestBlock)=>res + getNestedBlockTags(getBlockHTML(nestBlock), nestBlock.depth).nestEnd, result);
|
|
77
|
+
return result;
|
|
78
|
+
};
|
|
79
|
+
export default ((...args)=>{
|
|
80
|
+
if (args.length === 1 && Object.prototype.hasOwnProperty.call(args[0], '_map') && args[0].getBlockMap != null) {
|
|
81
|
+
// skip higher-order function and use defaults
|
|
82
|
+
return convertToHTML({})(...args);
|
|
83
|
+
}
|
|
84
|
+
return convertToHTML(...args);
|
|
85
|
+
});
|
|
@@ -1,33 +1,36 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
|
|
3
2
|
// based on Draft.js' custom list depth styling
|
|
4
|
-
|
|
3
|
+
const ORDERED_LIST_TYPES = [
|
|
4
|
+
'1',
|
|
5
|
+
'a',
|
|
6
|
+
'i'
|
|
7
|
+
];
|
|
5
8
|
export default {
|
|
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
|
-
};
|
|
9
|
+
unstyled: /*#__PURE__*/ React.createElement("p", null),
|
|
10
|
+
paragraph: /*#__PURE__*/ React.createElement("p", null),
|
|
11
|
+
'header-one': /*#__PURE__*/ React.createElement("h1", null),
|
|
12
|
+
'header-two': /*#__PURE__*/ React.createElement("h2", null),
|
|
13
|
+
'header-two-unordered': /*#__PURE__*/ React.createElement("h2", null),
|
|
14
|
+
'header-two-ordered': /*#__PURE__*/ React.createElement("h2", null),
|
|
15
|
+
'header-three': /*#__PURE__*/ React.createElement("h3", null),
|
|
16
|
+
'header-four': /*#__PURE__*/ React.createElement("h4", null),
|
|
17
|
+
'header-five': /*#__PURE__*/ React.createElement("h5", null),
|
|
18
|
+
'header-six': /*#__PURE__*/ React.createElement("h6", null),
|
|
19
|
+
'code-block': /*#__PURE__*/ React.createElement("pre", null),
|
|
20
|
+
blockquote: /*#__PURE__*/ React.createElement("blockquote", null),
|
|
21
|
+
'unordered-list-item': {
|
|
22
|
+
element: /*#__PURE__*/ React.createElement("li", null),
|
|
23
|
+
nest: /*#__PURE__*/ React.createElement("ul", null)
|
|
24
|
+
},
|
|
25
|
+
'ordered-list-item': {
|
|
26
|
+
element: /*#__PURE__*/ React.createElement("li", null),
|
|
27
|
+
nest: (depth)=>{
|
|
28
|
+
const type = ORDERED_LIST_TYPES[depth % 3];
|
|
29
|
+
return /*#__PURE__*/ React.createElement("ol", {
|
|
30
|
+
type: type
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
media: /*#__PURE__*/ React.createElement("figure", null),
|
|
35
|
+
atomic: /*#__PURE__*/ React.createElement("figure", null)
|
|
36
|
+
};
|
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
export default function defaultInlineHTML(style) {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
3
|
+
switch(style){
|
|
4
|
+
case 'BOLD':
|
|
5
|
+
return /*#__PURE__*/ React.createElement("strong", null);
|
|
6
|
+
case 'ITALIC':
|
|
7
|
+
return /*#__PURE__*/ React.createElement("em", null);
|
|
8
|
+
case 'UNDERLINE':
|
|
9
|
+
return /*#__PURE__*/ React.createElement("u", null);
|
|
10
|
+
case 'CODE':
|
|
11
|
+
return /*#__PURE__*/ React.createElement("code", null);
|
|
12
|
+
default:
|
|
13
|
+
return {
|
|
14
|
+
start: '',
|
|
15
|
+
end: ''
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
package/esm/encodeBlock.js
CHANGED
|
@@ -1,44 +1,40 @@
|
|
|
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
1
|
import updateMutation from './util/updateMutation';
|
|
6
2
|
import rangeSort from './util/rangeSort';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
3
|
+
const ENTITY_MAP = {
|
|
4
|
+
'&': '&',
|
|
5
|
+
'<': '<',
|
|
6
|
+
'>': '>',
|
|
7
|
+
'"': '"',
|
|
8
|
+
"'": ''',
|
|
9
|
+
'`': '`',
|
|
10
|
+
'\n': '<br/>'
|
|
15
11
|
};
|
|
16
|
-
export default (
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
12
|
+
export default ((block)=>{
|
|
13
|
+
const blockText = [
|
|
14
|
+
...block.text
|
|
15
|
+
];
|
|
16
|
+
let entities = block.entityRanges.sort(rangeSort);
|
|
17
|
+
let styles = block.inlineStyleRanges.sort(rangeSort);
|
|
18
|
+
let resultText = '';
|
|
19
|
+
for(let index = 0; index < blockText.length; index += 1){
|
|
20
|
+
const char = blockText[index];
|
|
21
|
+
if (ENTITY_MAP[char] !== undefined) {
|
|
22
|
+
const encoded = ENTITY_MAP[char];
|
|
23
|
+
const resultIndex = [
|
|
24
|
+
...resultText
|
|
25
|
+
].length;
|
|
26
|
+
resultText += encoded;
|
|
27
|
+
const updateForChar = (mutation)=>updateMutation(mutation, resultIndex, char.length, encoded.length, 0, 0);
|
|
28
|
+
entities = entities.map(updateForChar);
|
|
29
|
+
styles = styles.map(updateForChar);
|
|
30
|
+
} else {
|
|
31
|
+
resultText += char;
|
|
32
|
+
}
|
|
34
33
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
entityRanges: entities
|
|
43
|
-
});
|
|
44
|
-
});
|
|
34
|
+
return {
|
|
35
|
+
...block,
|
|
36
|
+
text: resultText,
|
|
37
|
+
inlineStyleRanges: styles,
|
|
38
|
+
entityRanges: entities
|
|
39
|
+
};
|
|
40
|
+
});
|
package/esm/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
export default (
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
};
|
|
9
|
-
});
|
|
1
|
+
export default ((newFn, rest)=>(...args)=>{
|
|
2
|
+
const newResult = newFn(...args);
|
|
3
|
+
if (newResult !== undefined && newResult !== null) {
|
|
4
|
+
return newResult;
|
|
5
|
+
}
|
|
6
|
+
return rest(...args);
|
|
7
|
+
});
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
export default (
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
};
|
|
9
|
-
});
|
|
1
|
+
export default ((typeObject)=>(block)=>{
|
|
2
|
+
if (typeof typeObject === 'function') {
|
|
3
|
+
// handle case where typeObject is already a function
|
|
4
|
+
return typeObject(block);
|
|
5
|
+
}
|
|
6
|
+
return typeObject[block.type];
|
|
7
|
+
});
|
package/esm/util/getBlockTags.js
CHANGED
|
@@ -1,27 +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
1
|
import invariant from 'invariant';
|
|
5
2
|
import React from 'react';
|
|
6
3
|
import ReactDOMServer from 'react-dom/server';
|
|
7
4
|
import splitReactElement from './splitReactElement';
|
|
8
5
|
function hasChildren(element) {
|
|
9
|
-
|
|
6
|
+
return /*#__PURE__*/ React.isValidElement(element) && React.Children.count(element.props.children) > 0;
|
|
10
7
|
}
|
|
11
8
|
export default function getBlockTags(blockHTML) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
9
|
+
invariant(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null');
|
|
10
|
+
if (typeof blockHTML === 'string') {
|
|
11
|
+
return blockHTML;
|
|
12
|
+
}
|
|
13
|
+
if (/*#__PURE__*/ React.isValidElement(blockHTML)) {
|
|
14
|
+
if (hasChildren(blockHTML)) {
|
|
15
|
+
return ReactDOMServer.renderToStaticMarkup(blockHTML);
|
|
16
|
+
}
|
|
17
|
+
return splitReactElement(blockHTML);
|
|
18
|
+
}
|
|
19
|
+
if (Object.prototype.hasOwnProperty.call(blockHTML, 'element') && /*#__PURE__*/ React.isValidElement(blockHTML.element)) {
|
|
20
|
+
return {
|
|
21
|
+
...blockHTML,
|
|
22
|
+
...splitReactElement(blockHTML.element)
|
|
23
|
+
};
|
|
19
24
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
}
|
|
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
|
+
}
|
|
@@ -1,36 +1,32 @@
|
|
|
1
|
-
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
2
1
|
import invariant from 'invariant';
|
|
3
2
|
import React from 'react';
|
|
4
3
|
import ReactDOMServer from 'react-dom/server';
|
|
5
4
|
import splitReactElement from './splitReactElement';
|
|
6
5
|
function hasChildren(element) {
|
|
7
|
-
|
|
6
|
+
return /*#__PURE__*/ React.isValidElement(element) && React.Children.count(element.props.children) > 0;
|
|
8
7
|
}
|
|
9
|
-
export default function getElementHTML(element) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return element;
|
|
16
|
-
}
|
|
17
|
-
if (/*#__PURE__*/React.isValidElement(element)) {
|
|
18
|
-
if (hasChildren(element)) {
|
|
19
|
-
return ReactDOMServer.renderToStaticMarkup(element);
|
|
8
|
+
export default function getElementHTML(element, text = null) {
|
|
9
|
+
if (element === undefined || element === null) {
|
|
10
|
+
return element;
|
|
11
|
+
}
|
|
12
|
+
if (typeof element === 'string') {
|
|
13
|
+
return element;
|
|
20
14
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
15
|
+
if (/*#__PURE__*/ React.isValidElement(element)) {
|
|
16
|
+
if (hasChildren(element)) {
|
|
17
|
+
return ReactDOMServer.renderToStaticMarkup(element);
|
|
18
|
+
}
|
|
19
|
+
const tags = splitReactElement(element);
|
|
20
|
+
if (text !== null && typeof tags === 'object') {
|
|
21
|
+
const { start, end } = tags;
|
|
22
|
+
return start + text + end;
|
|
23
|
+
}
|
|
24
|
+
return tags;
|
|
26
25
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
35
|
-
return element;
|
|
36
|
-
}
|
|
26
|
+
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');
|
|
27
|
+
if (text !== null) {
|
|
28
|
+
const { start, end } = element;
|
|
29
|
+
return start + text + end;
|
|
30
|
+
}
|
|
31
|
+
return element;
|
|
32
|
+
}
|
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
|
2
1
|
import React from 'react';
|
|
3
2
|
import splitReactElement from './splitReactElement';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
const getElementTagLength = (element, type = 'start')=>{
|
|
4
|
+
if (/*#__PURE__*/ React.isValidElement(element)) {
|
|
5
|
+
const splitElement = splitReactElement(element);
|
|
6
|
+
if (typeof splitElement === 'string') {
|
|
7
|
+
return 0;
|
|
8
|
+
}
|
|
9
|
+
const { length } = splitElement[type];
|
|
10
|
+
const child = React.Children.toArray(element.props.children)[0];
|
|
11
|
+
return length + (child && /*#__PURE__*/ React.isValidElement(child) ? getElementTagLength(child, type) : 0);
|
|
10
12
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
if (_typeof(element) === 'object') {
|
|
16
|
-
return element[type] ? element[type].length : 0;
|
|
17
|
-
}
|
|
18
|
-
return 0;
|
|
13
|
+
if (typeof element === 'object') {
|
|
14
|
+
return element[type] ? element[type].length : 0;
|
|
15
|
+
}
|
|
16
|
+
return 0;
|
|
19
17
|
};
|
|
20
|
-
export default
|
|
18
|
+
export default getElementTagLength;
|
|
@@ -1,29 +1,24 @@
|
|
|
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
|
import invariant from 'invariant';
|
|
5
2
|
import React from 'react';
|
|
6
3
|
import splitReactElement from './splitReactElement';
|
|
7
4
|
export default function getNestedBlockTags(blockHTML, depth) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return blockHTML;
|
|
29
|
-
}
|
|
5
|
+
invariant(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null');
|
|
6
|
+
if (typeof blockHTML.nest === 'function') {
|
|
7
|
+
const { start, end } = splitReactElement(blockHTML.nest(depth));
|
|
8
|
+
return {
|
|
9
|
+
...blockHTML,
|
|
10
|
+
nestStart: start,
|
|
11
|
+
nestEnd: end
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
if (/*#__PURE__*/ React.isValidElement(blockHTML.nest)) {
|
|
15
|
+
const { start, end } = splitReactElement(blockHTML.nest);
|
|
16
|
+
return {
|
|
17
|
+
...blockHTML,
|
|
18
|
+
nestStart: start,
|
|
19
|
+
nestEnd: end
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
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');
|
|
23
|
+
return blockHTML;
|
|
24
|
+
}
|
package/esm/util/parseHTML.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
const fallback = (html)=>{
|
|
2
|
+
const doc = document.implementation.createHTMLDocument('');
|
|
3
|
+
doc.documentElement.innerHTML = html;
|
|
4
|
+
return doc;
|
|
5
5
|
};
|
|
6
6
|
export default function parseHTML(html) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
let doc;
|
|
8
|
+
if (typeof DOMParser !== 'undefined') {
|
|
9
|
+
const 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);
|
|
13
16
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
return doc.body;
|
|
18
|
-
}
|
|
17
|
+
return doc.body;
|
|
18
|
+
}
|