@20minutes/draft-convert 3.1.0 → 3.1.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/esm/blockEntities.js +4 -4
- package/esm/blockInlineStyles.js +5 -5
- package/esm/convertFromHTML.js +6 -4
- package/esm/convertToHTML.js +10 -9
- package/esm/encodeBlock.js +2 -2
- package/esm/index.js +3 -3
- package/esm/util/getBlockTags.js +1 -1
- package/esm/util/getElementHTML.js +1 -1
- package/esm/util/getElementTagLength.js +1 -1
- package/esm/util/getNestedBlockTags.js +1 -1
- package/lib/blockEntities.js +4 -4
- package/lib/blockInlineStyles.js +5 -5
- package/lib/convertFromHTML.js +74 -31
- package/lib/convertToHTML.js +52 -10
- package/lib/encodeBlock.js +2 -2
- package/lib/index.js +3 -3
- package/lib/util/getBlockTags.js +1 -1
- package/lib/util/getElementHTML.js +1 -1
- package/lib/util/getElementTagLength.js +1 -1
- package/lib/util/getNestedBlockTags.js +1 -1
- package/package.json +4 -3
package/esm/blockEntities.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import updateMutation from './util/updateMutation';
|
|
2
|
-
import rangeSort from './util/rangeSort';
|
|
3
|
-
import getElementHTML from './util/getElementHTML';
|
|
4
|
-
import getElementTagLength from './util/getElementTagLength';
|
|
1
|
+
import updateMutation from './util/updateMutation.js';
|
|
2
|
+
import rangeSort from './util/rangeSort.js';
|
|
3
|
+
import getElementHTML from './util/getElementHTML.js';
|
|
4
|
+
import getElementTagLength from './util/getElementTagLength.js';
|
|
5
5
|
const converter = (entity = {}, originalText = '')=>originalText;
|
|
6
6
|
export default ((block, entityMap, entityConverter = converter)=>{
|
|
7
7
|
let resultText = [
|
package/esm/blockInlineStyles.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import invariant from 'invariant';
|
|
2
|
-
import styleObjectFunction from './util/styleObjectFunction';
|
|
3
|
-
import accumulateFunction from './util/accumulateFunction';
|
|
4
|
-
import getElementHTML from './util/getElementHTML';
|
|
5
|
-
import rangeSort from './util/rangeSort';
|
|
6
|
-
import defaultInlineHTML from './default/defaultInlineHTML';
|
|
2
|
+
import styleObjectFunction from './util/styleObjectFunction.js';
|
|
3
|
+
import accumulateFunction from './util/accumulateFunction.js';
|
|
4
|
+
import getElementHTML from './util/getElementHTML.js';
|
|
5
|
+
import rangeSort from './util/rangeSort.js';
|
|
6
|
+
import defaultInlineHTML from './default/defaultInlineHTML.js';
|
|
7
7
|
const subtractStyles = (original, toRemove)=>original.filter((el)=>!toRemove.some((elToRemove)=>elToRemove.style === el.style));
|
|
8
8
|
const popEndingStyles = (styleStack, endingStyles)=>endingStyles.reduceRight((stack, style)=>{
|
|
9
9
|
const styleToRemove = stack[stack.length - 1];
|
package/esm/convertFromHTML.js
CHANGED
|
@@ -8,10 +8,12 @@
|
|
|
8
8
|
* This source code is licensed under the BSD-style license found in the
|
|
9
9
|
* LICENSE file in the /src directory of this source tree. An additional grant
|
|
10
10
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
11
|
-
*/ import
|
|
12
|
-
import
|
|
13
|
-
import getSafeBodyFromHTML from './util/parseHTML';
|
|
14
|
-
import rangeSort from './util/rangeSort';
|
|
11
|
+
*/ import * as Immutable from 'immutable';
|
|
12
|
+
import * as DraftJS from 'draft-js';
|
|
13
|
+
import getSafeBodyFromHTML from './util/parseHTML.js';
|
|
14
|
+
import rangeSort from './util/rangeSort.js';
|
|
15
|
+
const { List, Map, OrderedSet } = Immutable;
|
|
16
|
+
const { BlockMapBuilder, CharacterMetadata, ContentBlock, ContentState, Entity, SelectionState, genKey } = DraftJS;
|
|
15
17
|
const SPACE = ' ';
|
|
16
18
|
// Arbitrary max indent
|
|
17
19
|
const MAX_DEPTH = 4;
|
package/esm/convertToHTML.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import invariant from 'invariant';
|
|
2
2
|
import React from 'react';
|
|
3
3
|
import ReactDOMServer from 'react-dom/server';
|
|
4
|
-
import
|
|
5
|
-
import encodeBlock from './encodeBlock';
|
|
6
|
-
import blockEntities from './blockEntities';
|
|
7
|
-
import blockInlineStyles from './blockInlineStyles';
|
|
8
|
-
import accumulateFunction from './util/accumulateFunction';
|
|
9
|
-
import blockTypeObjectFunction from './util/blockTypeObjectFunction';
|
|
10
|
-
import getBlockTags from './util/getBlockTags';
|
|
11
|
-
import getNestedBlockTags from './util/getNestedBlockTags';
|
|
12
|
-
import defaultBlockHTML from './default/defaultBlockHTML';
|
|
4
|
+
import * as DraftJS from 'draft-js';
|
|
5
|
+
import encodeBlock from './encodeBlock.js';
|
|
6
|
+
import blockEntities from './blockEntities.js';
|
|
7
|
+
import blockInlineStyles from './blockInlineStyles.js';
|
|
8
|
+
import accumulateFunction from './util/accumulateFunction.js';
|
|
9
|
+
import blockTypeObjectFunction from './util/blockTypeObjectFunction.js';
|
|
10
|
+
import getBlockTags from './util/getBlockTags.js';
|
|
11
|
+
import getNestedBlockTags from './util/getNestedBlockTags.js';
|
|
12
|
+
import defaultBlockHTML from './default/defaultBlockHTML.js';
|
|
13
|
+
const { convertToRaw } = DraftJS;
|
|
13
14
|
const defaultEntityToHTML = (entity, originalText)=>originalText;
|
|
14
15
|
const defaultValidateHTML = (html)=>true;
|
|
15
16
|
const convertToHTML = ({ styleToHTML = {}, blockToHTML = {}, entityToHTML = defaultEntityToHTML, validateHTML = defaultValidateHTML })=>(contentState)=>{
|
package/esm/encodeBlock.js
CHANGED
package/esm/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import convertToHTML from './convertToHTML';
|
|
2
|
-
import convertFromHTML from './convertFromHTML';
|
|
3
|
-
import parseHTML from './util/parseHTML';
|
|
1
|
+
import convertToHTML from './convertToHTML.js';
|
|
2
|
+
import convertFromHTML from './convertFromHTML.js';
|
|
3
|
+
import parseHTML from './util/parseHTML.js';
|
|
4
4
|
export { convertToHTML, convertFromHTML, parseHTML };
|
package/esm/util/getBlockTags.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import invariant from 'invariant';
|
|
2
2
|
import React from 'react';
|
|
3
3
|
import ReactDOMServer from 'react-dom/server';
|
|
4
|
-
import splitReactElement from './splitReactElement';
|
|
4
|
+
import splitReactElement from './splitReactElement.js';
|
|
5
5
|
function hasChildren(element) {
|
|
6
6
|
return /*#__PURE__*/ React.isValidElement(element) && React.Children.count(element.props.children) > 0;
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import invariant from 'invariant';
|
|
2
2
|
import React from 'react';
|
|
3
3
|
import ReactDOMServer from 'react-dom/server';
|
|
4
|
-
import splitReactElement from './splitReactElement';
|
|
4
|
+
import splitReactElement from './splitReactElement.js';
|
|
5
5
|
function hasChildren(element) {
|
|
6
6
|
return /*#__PURE__*/ React.isValidElement(element) && React.Children.count(element.props.children) > 0;
|
|
7
7
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import splitReactElement from './splitReactElement';
|
|
2
|
+
import splitReactElement from './splitReactElement.js';
|
|
3
3
|
const getElementTagLength = (element, type = 'start')=>{
|
|
4
4
|
if (/*#__PURE__*/ React.isValidElement(element)) {
|
|
5
5
|
const splitElement = splitReactElement(element);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import invariant from 'invariant';
|
|
2
2
|
import React from 'react';
|
|
3
|
-
import splitReactElement from './splitReactElement';
|
|
3
|
+
import splitReactElement from './splitReactElement.js';
|
|
4
4
|
export default function getNestedBlockTags(blockHTML, depth) {
|
|
5
5
|
invariant(blockHTML !== null && blockHTML !== undefined, 'Expected block HTML value to be non-null');
|
|
6
6
|
if (typeof blockHTML.nest === 'function') {
|
package/lib/blockEntities.js
CHANGED
|
@@ -8,10 +8,10 @@ Object.defineProperty(exports, "default", {
|
|
|
8
8
|
return _default;
|
|
9
9
|
}
|
|
10
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"));
|
|
11
|
+
const _updateMutation = /*#__PURE__*/ _interop_require_default(require("./util/updateMutation.js"));
|
|
12
|
+
const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort.js"));
|
|
13
|
+
const _getElementHTML = /*#__PURE__*/ _interop_require_default(require("./util/getElementHTML.js"));
|
|
14
|
+
const _getElementTagLength = /*#__PURE__*/ _interop_require_default(require("./util/getElementTagLength.js"));
|
|
15
15
|
function _interop_require_default(obj) {
|
|
16
16
|
return obj && obj.__esModule ? obj : {
|
|
17
17
|
default: obj
|
package/lib/blockInlineStyles.js
CHANGED
|
@@ -9,11 +9,11 @@ Object.defineProperty(exports, "default", {
|
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
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"));
|
|
12
|
+
const _styleObjectFunction = /*#__PURE__*/ _interop_require_default(require("./util/styleObjectFunction.js"));
|
|
13
|
+
const _accumulateFunction = /*#__PURE__*/ _interop_require_default(require("./util/accumulateFunction.js"));
|
|
14
|
+
const _getElementHTML = /*#__PURE__*/ _interop_require_default(require("./util/getElementHTML.js"));
|
|
15
|
+
const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort.js"));
|
|
16
|
+
const _defaultInlineHTML = /*#__PURE__*/ _interop_require_default(require("./default/defaultInlineHTML.js"));
|
|
17
17
|
function _interop_require_default(obj) {
|
|
18
18
|
return obj && obj.__esModule ? obj : {
|
|
19
19
|
default: obj
|
package/lib/convertFromHTML.js
CHANGED
|
@@ -18,15 +18,58 @@ Object.defineProperty(exports, "default", {
|
|
|
18
18
|
return _default;
|
|
19
19
|
}
|
|
20
20
|
});
|
|
21
|
-
const _immutable = require("immutable");
|
|
22
|
-
const _draftjs = require("draft-js");
|
|
23
|
-
const _parseHTML = /*#__PURE__*/ _interop_require_default(require("./util/parseHTML"));
|
|
24
|
-
const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort"));
|
|
21
|
+
const _immutable = /*#__PURE__*/ _interop_require_wildcard(require("immutable"));
|
|
22
|
+
const _draftjs = /*#__PURE__*/ _interop_require_wildcard(require("draft-js"));
|
|
23
|
+
const _parseHTML = /*#__PURE__*/ _interop_require_default(require("./util/parseHTML.js"));
|
|
24
|
+
const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort.js"));
|
|
25
25
|
function _interop_require_default(obj) {
|
|
26
26
|
return obj && obj.__esModule ? obj : {
|
|
27
27
|
default: obj
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
31
|
+
if (typeof WeakMap !== "function") return null;
|
|
32
|
+
var cacheBabelInterop = new WeakMap();
|
|
33
|
+
var cacheNodeInterop = new WeakMap();
|
|
34
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
35
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
36
|
+
})(nodeInterop);
|
|
37
|
+
}
|
|
38
|
+
function _interop_require_wildcard(obj, nodeInterop) {
|
|
39
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
40
|
+
return obj;
|
|
41
|
+
}
|
|
42
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
43
|
+
return {
|
|
44
|
+
default: obj
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
48
|
+
if (cache && cache.has(obj)) {
|
|
49
|
+
return cache.get(obj);
|
|
50
|
+
}
|
|
51
|
+
var newObj = {
|
|
52
|
+
__proto__: null
|
|
53
|
+
};
|
|
54
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
55
|
+
for(var key in obj){
|
|
56
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
57
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
58
|
+
if (desc && (desc.get || desc.set)) {
|
|
59
|
+
Object.defineProperty(newObj, key, desc);
|
|
60
|
+
} else {
|
|
61
|
+
newObj[key] = obj[key];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
newObj.default = obj;
|
|
66
|
+
if (cache) {
|
|
67
|
+
cache.set(obj, newObj);
|
|
68
|
+
}
|
|
69
|
+
return newObj;
|
|
70
|
+
}
|
|
71
|
+
const { List, Map, OrderedSet } = _immutable;
|
|
72
|
+
const { BlockMapBuilder, CharacterMetadata, ContentBlock, ContentState, Entity, SelectionState, genKey } = _draftjs;
|
|
30
73
|
const SPACE = ' ';
|
|
31
74
|
// Arbitrary max indent
|
|
32
75
|
const MAX_DEPTH = 4;
|
|
@@ -93,18 +136,18 @@ function getWhitespaceChunk(inEntity) {
|
|
|
93
136
|
return {
|
|
94
137
|
text: SPACE,
|
|
95
138
|
inlines: [
|
|
96
|
-
|
|
139
|
+
OrderedSet()
|
|
97
140
|
],
|
|
98
141
|
entities,
|
|
99
142
|
blocks: []
|
|
100
143
|
};
|
|
101
144
|
}
|
|
102
|
-
function getSoftNewlineChunk(block, depth, flat = false, data =
|
|
145
|
+
function getSoftNewlineChunk(block, depth, flat = false, data = Map()) {
|
|
103
146
|
if (flat === true) {
|
|
104
147
|
return {
|
|
105
148
|
text: '\r',
|
|
106
149
|
inlines: [
|
|
107
|
-
|
|
150
|
+
OrderedSet()
|
|
108
151
|
],
|
|
109
152
|
entities: new Array(1),
|
|
110
153
|
blocks: [
|
|
@@ -120,17 +163,17 @@ function getSoftNewlineChunk(block, depth, flat = false, data = (0, _immutable.M
|
|
|
120
163
|
return {
|
|
121
164
|
text: '\n',
|
|
122
165
|
inlines: [
|
|
123
|
-
|
|
166
|
+
OrderedSet()
|
|
124
167
|
],
|
|
125
168
|
entities: new Array(1),
|
|
126
169
|
blocks: []
|
|
127
170
|
};
|
|
128
171
|
}
|
|
129
|
-
function getBlockDividerChunk(block, depth, data =
|
|
172
|
+
function getBlockDividerChunk(block, depth, data = Map()) {
|
|
130
173
|
return {
|
|
131
174
|
text: '\r',
|
|
132
175
|
inlines: [
|
|
133
|
-
|
|
176
|
+
OrderedSet()
|
|
134
177
|
],
|
|
135
178
|
entities: new Array(1),
|
|
136
179
|
blocks: [
|
|
@@ -198,7 +241,7 @@ function processInlineTag(tag, node, currentStyle) {
|
|
|
198
241
|
}
|
|
199
242
|
return currentStyle;
|
|
200
243
|
}
|
|
201
|
-
function baseProcessInlineTag(tag, node, inlineStyles =
|
|
244
|
+
function baseProcessInlineTag(tag, node, inlineStyles = OrderedSet()) {
|
|
202
245
|
return processInlineTag(tag, node, inlineStyles);
|
|
203
246
|
}
|
|
204
247
|
function joinChunks(A, B, flat = false) {
|
|
@@ -314,10 +357,10 @@ function genFragment(node, inlineStyle, lastList, inBlock, fragmentBlockTags, de
|
|
|
314
357
|
blockInfo = blockInfo || {};
|
|
315
358
|
if (typeof blockInfo === 'string') {
|
|
316
359
|
blockType = blockInfo;
|
|
317
|
-
blockDataMap =
|
|
360
|
+
blockDataMap = Map();
|
|
318
361
|
} else {
|
|
319
362
|
blockType = typeof blockInfo === 'string' ? blockInfo : blockInfo.type;
|
|
320
|
-
blockDataMap = blockInfo.data ?
|
|
363
|
+
blockDataMap = blockInfo.data ? Map(blockInfo.data) : Map();
|
|
321
364
|
}
|
|
322
365
|
if (!inBlock && (fragmentBlockTags.indexOf(nodeName) !== -1 || blockType)) {
|
|
323
366
|
chunk = getBlockDividerChunk(blockType || getBlockTypeForTag(nodeName, lastList), depth, blockDataMap);
|
|
@@ -361,10 +404,10 @@ function genFragment(node, inlineStyle, lastList, inBlock, fragmentBlockTags, de
|
|
|
361
404
|
newBlockInfo = newBlockInfo || {};
|
|
362
405
|
if (typeof newBlockInfo === 'string') {
|
|
363
406
|
newBlockType = newBlockInfo;
|
|
364
|
-
newBlockData =
|
|
407
|
+
newBlockData = Map();
|
|
365
408
|
} else {
|
|
366
409
|
newBlockType = newBlockInfo.type || getBlockTypeForTag(nodeName, lastList);
|
|
367
|
-
newBlockData = newBlockInfo.data ?
|
|
410
|
+
newBlockData = newBlockInfo.data ? Map(newBlockInfo.data) : Map();
|
|
368
411
|
}
|
|
369
412
|
chunk = joinChunks(chunk, getSoftNewlineChunk(newBlockType, depth, options.flat, newBlockData), options.flat);
|
|
370
413
|
}
|
|
@@ -375,7 +418,7 @@ function genFragment(node, inlineStyle, lastList, inBlock, fragmentBlockTags, de
|
|
|
375
418
|
child = sibling;
|
|
376
419
|
}
|
|
377
420
|
if (newBlock) {
|
|
378
|
-
chunk = joinChunks(chunk, getBlockDividerChunk(nextBlockType, depth,
|
|
421
|
+
chunk = joinChunks(chunk, getBlockDividerChunk(nextBlockType, depth, Map()), options.flat);
|
|
379
422
|
}
|
|
380
423
|
return chunk;
|
|
381
424
|
}
|
|
@@ -395,7 +438,7 @@ function getChunkForHTML(html, processCustomInlineStyles, checkEntityNode, check
|
|
|
395
438
|
];
|
|
396
439
|
// Start with -1 block depth to offset the fact that we are passing in a fake
|
|
397
440
|
// UL block to sta rt with.
|
|
398
|
-
let chunk = genFragment(safeBody,
|
|
441
|
+
let chunk = genFragment(safeBody, OrderedSet(), 'ul', null, workingBlocks, -1, processCustomInlineStyles, checkEntityNode, checkEntityText, checkBlockType, createEntity, getEntity, mergeEntityData, replaceEntityData, options);
|
|
399
442
|
// join with previous block to prevent weirdness on paste
|
|
400
443
|
if (chunk.text.indexOf('\r') === 0) {
|
|
401
444
|
chunk = {
|
|
@@ -416,7 +459,7 @@ function getChunkForHTML(html, processCustomInlineStyles, checkEntityNode, check
|
|
|
416
459
|
if (chunk.blocks.length === 0) {
|
|
417
460
|
chunk.blocks.push({
|
|
418
461
|
type: 'unstyled',
|
|
419
|
-
data:
|
|
462
|
+
data: Map(),
|
|
420
463
|
depth: 0
|
|
421
464
|
});
|
|
422
465
|
}
|
|
@@ -426,7 +469,7 @@ function getChunkForHTML(html, processCustomInlineStyles, checkEntityNode, check
|
|
|
426
469
|
if (chunk.text.split('\r').length === chunk.blocks.length + 1) {
|
|
427
470
|
chunk.blocks.unshift({
|
|
428
471
|
type: 'unstyled',
|
|
429
|
-
data:
|
|
472
|
+
data: Map(),
|
|
430
473
|
depth: 0
|
|
431
474
|
});
|
|
432
475
|
}
|
|
@@ -447,7 +490,7 @@ function convertFromHTMLtoContentBlocks(html, processCustomInlineStyles, checkEn
|
|
|
447
490
|
const end = start + textBlock.length;
|
|
448
491
|
const inlines = nullthrows(chunk).inlines.slice(start, end);
|
|
449
492
|
const entities = nullthrows(chunk).entities.slice(start, end);
|
|
450
|
-
const characterList =
|
|
493
|
+
const characterList = List(inlines.map((style, entityIndex)=>{
|
|
451
494
|
const data = {
|
|
452
495
|
style,
|
|
453
496
|
entity: null
|
|
@@ -455,10 +498,10 @@ function convertFromHTMLtoContentBlocks(html, processCustomInlineStyles, checkEn
|
|
|
455
498
|
if (entities[entityIndex]) {
|
|
456
499
|
data.entity = entities[entityIndex];
|
|
457
500
|
}
|
|
458
|
-
return
|
|
501
|
+
return CharacterMetadata.create(data);
|
|
459
502
|
}));
|
|
460
503
|
start = end + 1;
|
|
461
|
-
return new
|
|
504
|
+
return new ContentBlock({
|
|
462
505
|
key: generateKey(),
|
|
463
506
|
type: nullthrows(chunk).blocks[blockIndex].type,
|
|
464
507
|
data: nullthrows(chunk).blocks[blockIndex].data,
|
|
@@ -470,42 +513,42 @@ function convertFromHTMLtoContentBlocks(html, processCustomInlineStyles, checkEn
|
|
|
470
513
|
}
|
|
471
514
|
const convertFromHTML = ({ htmlToStyle = defaultHTMLToStyle, htmlToEntity = defaultHTMLToEntity, textToEntity = defaultTextToEntity, htmlToBlock = defaultHTMLToBlock })=>(html, options = {
|
|
472
515
|
flat: false
|
|
473
|
-
}, DOMBuilder = _parseHTML.default, generateKey =
|
|
474
|
-
let contentState =
|
|
516
|
+
}, DOMBuilder = _parseHTML.default, generateKey = genKey)=>{
|
|
517
|
+
let contentState = ContentState.createFromText('');
|
|
475
518
|
const createEntityWithContentState = (...args)=>{
|
|
476
519
|
if (contentState.createEntity) {
|
|
477
520
|
contentState = contentState.createEntity(...args);
|
|
478
521
|
return contentState.getLastCreatedEntityKey();
|
|
479
522
|
}
|
|
480
|
-
return
|
|
523
|
+
return Entity.create(...args);
|
|
481
524
|
};
|
|
482
525
|
const getEntityWithContentState = (...args)=>{
|
|
483
526
|
if (contentState.getEntity) {
|
|
484
527
|
return contentState.getEntity(...args);
|
|
485
528
|
}
|
|
486
|
-
return
|
|
529
|
+
return Entity.get(...args);
|
|
487
530
|
};
|
|
488
531
|
const mergeEntityDataWithContentState = (...args)=>{
|
|
489
532
|
if (contentState.mergeEntityData) {
|
|
490
533
|
contentState = contentState.mergeEntityData(...args);
|
|
491
534
|
return;
|
|
492
535
|
}
|
|
493
|
-
|
|
536
|
+
Entity.mergeData(...args);
|
|
494
537
|
};
|
|
495
538
|
const replaceEntityDataWithContentState = (...args)=>{
|
|
496
539
|
if (contentState.replaceEntityData) {
|
|
497
540
|
contentState = contentState.replaceEntityData(...args);
|
|
498
541
|
return;
|
|
499
542
|
}
|
|
500
|
-
|
|
543
|
+
Entity.replaceData(...args);
|
|
501
544
|
};
|
|
502
545
|
const contentBlocks = convertFromHTMLtoContentBlocks(html, handleMiddleware(htmlToStyle, baseProcessInlineTag), handleMiddleware(htmlToEntity, defaultHTMLToEntity), handleMiddleware(textToEntity, defaultTextToEntity), handleMiddleware(htmlToBlock, baseCheckBlockType), createEntityWithContentState, getEntityWithContentState, mergeEntityDataWithContentState, replaceEntityDataWithContentState, options, DOMBuilder, generateKey);
|
|
503
|
-
const blockMap =
|
|
546
|
+
const blockMap = BlockMapBuilder.createFromArray(contentBlocks);
|
|
504
547
|
const firstBlockKey = contentBlocks[0].getKey();
|
|
505
548
|
return contentState.merge({
|
|
506
549
|
blockMap,
|
|
507
|
-
selectionBefore:
|
|
508
|
-
selectionAfter:
|
|
550
|
+
selectionBefore: SelectionState.createEmpty(firstBlockKey),
|
|
551
|
+
selectionAfter: SelectionState.createEmpty(firstBlockKey)
|
|
509
552
|
});
|
|
510
553
|
};
|
|
511
554
|
const _default = (...args)=>{
|
package/lib/convertToHTML.js
CHANGED
|
@@ -11,20 +11,62 @@ Object.defineProperty(exports, "default", {
|
|
|
11
11
|
const _invariant = /*#__PURE__*/ _interop_require_default(require("invariant"));
|
|
12
12
|
const _react = /*#__PURE__*/ _interop_require_default(require("react"));
|
|
13
13
|
const _server = /*#__PURE__*/ _interop_require_default(require("react-dom/server"));
|
|
14
|
-
const _draftjs = require("draft-js");
|
|
15
|
-
const _encodeBlock = /*#__PURE__*/ _interop_require_default(require("./encodeBlock"));
|
|
16
|
-
const _blockEntities = /*#__PURE__*/ _interop_require_default(require("./blockEntities"));
|
|
17
|
-
const _blockInlineStyles = /*#__PURE__*/ _interop_require_default(require("./blockInlineStyles"));
|
|
18
|
-
const _accumulateFunction = /*#__PURE__*/ _interop_require_default(require("./util/accumulateFunction"));
|
|
19
|
-
const _blockTypeObjectFunction = /*#__PURE__*/ _interop_require_default(require("./util/blockTypeObjectFunction"));
|
|
20
|
-
const _getBlockTags = /*#__PURE__*/ _interop_require_default(require("./util/getBlockTags"));
|
|
21
|
-
const _getNestedBlockTags = /*#__PURE__*/ _interop_require_default(require("./util/getNestedBlockTags"));
|
|
22
|
-
const _defaultBlockHTML = /*#__PURE__*/ _interop_require_default(require("./default/defaultBlockHTML"));
|
|
14
|
+
const _draftjs = /*#__PURE__*/ _interop_require_wildcard(require("draft-js"));
|
|
15
|
+
const _encodeBlock = /*#__PURE__*/ _interop_require_default(require("./encodeBlock.js"));
|
|
16
|
+
const _blockEntities = /*#__PURE__*/ _interop_require_default(require("./blockEntities.js"));
|
|
17
|
+
const _blockInlineStyles = /*#__PURE__*/ _interop_require_default(require("./blockInlineStyles.js"));
|
|
18
|
+
const _accumulateFunction = /*#__PURE__*/ _interop_require_default(require("./util/accumulateFunction.js"));
|
|
19
|
+
const _blockTypeObjectFunction = /*#__PURE__*/ _interop_require_default(require("./util/blockTypeObjectFunction.js"));
|
|
20
|
+
const _getBlockTags = /*#__PURE__*/ _interop_require_default(require("./util/getBlockTags.js"));
|
|
21
|
+
const _getNestedBlockTags = /*#__PURE__*/ _interop_require_default(require("./util/getNestedBlockTags.js"));
|
|
22
|
+
const _defaultBlockHTML = /*#__PURE__*/ _interop_require_default(require("./default/defaultBlockHTML.js"));
|
|
23
23
|
function _interop_require_default(obj) {
|
|
24
24
|
return obj && obj.__esModule ? obj : {
|
|
25
25
|
default: obj
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
|
+
function _getRequireWildcardCache(nodeInterop) {
|
|
29
|
+
if (typeof WeakMap !== "function") return null;
|
|
30
|
+
var cacheBabelInterop = new WeakMap();
|
|
31
|
+
var cacheNodeInterop = new WeakMap();
|
|
32
|
+
return (_getRequireWildcardCache = function(nodeInterop) {
|
|
33
|
+
return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
|
|
34
|
+
})(nodeInterop);
|
|
35
|
+
}
|
|
36
|
+
function _interop_require_wildcard(obj, nodeInterop) {
|
|
37
|
+
if (!nodeInterop && obj && obj.__esModule) {
|
|
38
|
+
return obj;
|
|
39
|
+
}
|
|
40
|
+
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
41
|
+
return {
|
|
42
|
+
default: obj
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
var cache = _getRequireWildcardCache(nodeInterop);
|
|
46
|
+
if (cache && cache.has(obj)) {
|
|
47
|
+
return cache.get(obj);
|
|
48
|
+
}
|
|
49
|
+
var newObj = {
|
|
50
|
+
__proto__: null
|
|
51
|
+
};
|
|
52
|
+
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
53
|
+
for(var key in obj){
|
|
54
|
+
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
55
|
+
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
56
|
+
if (desc && (desc.get || desc.set)) {
|
|
57
|
+
Object.defineProperty(newObj, key, desc);
|
|
58
|
+
} else {
|
|
59
|
+
newObj[key] = obj[key];
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
newObj.default = obj;
|
|
64
|
+
if (cache) {
|
|
65
|
+
cache.set(obj, newObj);
|
|
66
|
+
}
|
|
67
|
+
return newObj;
|
|
68
|
+
}
|
|
69
|
+
const { convertToRaw } = _draftjs;
|
|
28
70
|
const defaultEntityToHTML = (entity, originalText)=>originalText;
|
|
29
71
|
const defaultValidateHTML = (html)=>true;
|
|
30
72
|
const convertToHTML = ({ styleToHTML = {}, blockToHTML = {}, entityToHTML = defaultEntityToHTML, validateHTML = defaultValidateHTML })=>(contentState)=>{
|
|
@@ -35,7 +77,7 @@ const convertToHTML = ({ styleToHTML = {}, blockToHTML = {}, entityToHTML = defa
|
|
|
35
77
|
} else {
|
|
36
78
|
getBlockHTML = (0, _accumulateFunction.default)((0, _blockTypeObjectFunction.default)(blockToHTML), (0, _blockTypeObjectFunction.default)(_defaultBlockHTML.default));
|
|
37
79
|
}
|
|
38
|
-
const rawState =
|
|
80
|
+
const rawState = convertToRaw(contentState);
|
|
39
81
|
let listStack = [];
|
|
40
82
|
let result = rawState.blocks.map((block)=>{
|
|
41
83
|
const { type, depth } = block;
|
package/lib/encodeBlock.js
CHANGED
|
@@ -8,8 +8,8 @@ Object.defineProperty(exports, "default", {
|
|
|
8
8
|
return _default;
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
|
-
const _updateMutation = /*#__PURE__*/ _interop_require_default(require("./util/updateMutation"));
|
|
12
|
-
const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort"));
|
|
11
|
+
const _updateMutation = /*#__PURE__*/ _interop_require_default(require("./util/updateMutation.js"));
|
|
12
|
+
const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort.js"));
|
|
13
13
|
function _interop_require_default(obj) {
|
|
14
14
|
return obj && obj.__esModule ? obj : {
|
|
15
15
|
default: obj
|
package/lib/index.js
CHANGED
|
@@ -19,9 +19,9 @@ _export(exports, {
|
|
|
19
19
|
return _parseHTML.default;
|
|
20
20
|
}
|
|
21
21
|
});
|
|
22
|
-
const _convertToHTML = /*#__PURE__*/ _interop_require_default(require("./convertToHTML"));
|
|
23
|
-
const _convertFromHTML = /*#__PURE__*/ _interop_require_default(require("./convertFromHTML"));
|
|
24
|
-
const _parseHTML = /*#__PURE__*/ _interop_require_default(require("./util/parseHTML"));
|
|
22
|
+
const _convertToHTML = /*#__PURE__*/ _interop_require_default(require("./convertToHTML.js"));
|
|
23
|
+
const _convertFromHTML = /*#__PURE__*/ _interop_require_default(require("./convertFromHTML.js"));
|
|
24
|
+
const _parseHTML = /*#__PURE__*/ _interop_require_default(require("./util/parseHTML.js"));
|
|
25
25
|
function _interop_require_default(obj) {
|
|
26
26
|
return obj && obj.__esModule ? obj : {
|
|
27
27
|
default: obj
|
package/lib/util/getBlockTags.js
CHANGED
|
@@ -11,7 +11,7 @@ Object.defineProperty(exports, "default", {
|
|
|
11
11
|
const _invariant = /*#__PURE__*/ _interop_require_default(require("invariant"));
|
|
12
12
|
const _react = /*#__PURE__*/ _interop_require_default(require("react"));
|
|
13
13
|
const _server = /*#__PURE__*/ _interop_require_default(require("react-dom/server"));
|
|
14
|
-
const _splitReactElement = /*#__PURE__*/ _interop_require_default(require("./splitReactElement"));
|
|
14
|
+
const _splitReactElement = /*#__PURE__*/ _interop_require_default(require("./splitReactElement.js"));
|
|
15
15
|
function _interop_require_default(obj) {
|
|
16
16
|
return obj && obj.__esModule ? obj : {
|
|
17
17
|
default: obj
|
|
@@ -11,7 +11,7 @@ Object.defineProperty(exports, "default", {
|
|
|
11
11
|
const _invariant = /*#__PURE__*/ _interop_require_default(require("invariant"));
|
|
12
12
|
const _react = /*#__PURE__*/ _interop_require_default(require("react"));
|
|
13
13
|
const _server = /*#__PURE__*/ _interop_require_default(require("react-dom/server"));
|
|
14
|
-
const _splitReactElement = /*#__PURE__*/ _interop_require_default(require("./splitReactElement"));
|
|
14
|
+
const _splitReactElement = /*#__PURE__*/ _interop_require_default(require("./splitReactElement.js"));
|
|
15
15
|
function _interop_require_default(obj) {
|
|
16
16
|
return obj && obj.__esModule ? obj : {
|
|
17
17
|
default: obj
|
|
@@ -9,7 +9,7 @@ Object.defineProperty(exports, "default", {
|
|
|
9
9
|
}
|
|
10
10
|
});
|
|
11
11
|
const _react = /*#__PURE__*/ _interop_require_default(require("react"));
|
|
12
|
-
const _splitReactElement = /*#__PURE__*/ _interop_require_default(require("./splitReactElement"));
|
|
12
|
+
const _splitReactElement = /*#__PURE__*/ _interop_require_default(require("./splitReactElement.js"));
|
|
13
13
|
function _interop_require_default(obj) {
|
|
14
14
|
return obj && obj.__esModule ? obj : {
|
|
15
15
|
default: obj
|
|
@@ -10,7 +10,7 @@ Object.defineProperty(exports, "default", {
|
|
|
10
10
|
});
|
|
11
11
|
const _invariant = /*#__PURE__*/ _interop_require_default(require("invariant"));
|
|
12
12
|
const _react = /*#__PURE__*/ _interop_require_default(require("react"));
|
|
13
|
-
const _splitReactElement = /*#__PURE__*/ _interop_require_default(require("./splitReactElement"));
|
|
13
|
+
const _splitReactElement = /*#__PURE__*/ _interop_require_default(require("./splitReactElement.js"));
|
|
14
14
|
function _interop_require_default(obj) {
|
|
15
15
|
return obj && obj.__esModule ? obj : {
|
|
16
16
|
default: obj
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@20minutes/draft-convert",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "Extensibly serialize & deserialize Draft.js ContentState",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
},
|
|
51
51
|
"devDependencies": {
|
|
52
52
|
"@20minutes/eslint-config": "^2.0.0",
|
|
53
|
-
"@swc/cli": "^0.
|
|
53
|
+
"@swc/cli": "^0.8.0",
|
|
54
54
|
"@swc/core": "^1.9.2",
|
|
55
55
|
"@swc/jest": "^0.2.36",
|
|
56
56
|
"draft-js": "^0.11.7",
|
|
@@ -65,6 +65,7 @@
|
|
|
65
65
|
"prettier": "^3.6.2",
|
|
66
66
|
"react": "^18.0.0",
|
|
67
67
|
"react-dom": "^18.0.0",
|
|
68
|
-
"rimraf": "6.1.
|
|
68
|
+
"rimraf": "6.1.3",
|
|
69
|
+
"tsx": "^4.21.0"
|
|
69
70
|
}
|
|
70
71
|
}
|