@20minutes/draft-convert 3.1.1 → 3.1.2
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 → dist/blockEntities.mjs} +4 -4
- package/{esm/blockInlineStyles.js → dist/blockInlineStyles.mjs} +5 -5
- package/{lib → dist}/convertFromHTML.js +8 -46
- package/{esm/convertFromHTML.js → dist/convertFromHTML.mjs} +7 -4
- package/{lib → dist}/convertToHTML.js +2 -43
- package/{esm/convertToHTML.js → dist/convertToHTML.mjs} +9 -9
- package/{esm/encodeBlock.js → dist/encodeBlock.mjs} +2 -2
- package/dist/index.d.ts +121 -0
- package/dist/index.mjs +4 -0
- package/{esm/util/getBlockTags.js → dist/util/getBlockTags.mjs} +1 -1
- package/{esm/util/getElementHTML.js → dist/util/getElementHTML.mjs} +1 -1
- package/{esm/util/getElementTagLength.js → dist/util/getElementTagLength.mjs} +1 -1
- package/{esm/util/getNestedBlockTags.js → dist/util/getNestedBlockTags.mjs} +1 -1
- package/package.json +14 -14
- package/esm/index.js +0 -4
- package/types/index.d.ts +0 -114
- /package/{lib → dist}/blockEntities.js +0 -0
- /package/{lib → dist}/blockInlineStyles.js +0 -0
- /package/{lib → dist}/default/defaultBlockHTML.js +0 -0
- /package/{esm/default/defaultBlockHTML.js → dist/default/defaultBlockHTML.mjs} +0 -0
- /package/{lib → dist}/default/defaultInlineHTML.js +0 -0
- /package/{esm/default/defaultInlineHTML.js → dist/default/defaultInlineHTML.mjs} +0 -0
- /package/{lib → dist}/encodeBlock.js +0 -0
- /package/{lib/index.js → dist/index.cjs} +0 -0
- /package/{lib → dist}/util/accumulateFunction.js +0 -0
- /package/{esm/util/accumulateFunction.js → dist/util/accumulateFunction.mjs} +0 -0
- /package/{lib → dist}/util/blockTypeObjectFunction.js +0 -0
- /package/{esm/util/blockTypeObjectFunction.js → dist/util/blockTypeObjectFunction.mjs} +0 -0
- /package/{lib → dist}/util/getBlockTags.js +0 -0
- /package/{lib → dist}/util/getElementHTML.js +0 -0
- /package/{lib → dist}/util/getElementTagLength.js +0 -0
- /package/{lib → dist}/util/getNestedBlockTags.js +0 -0
- /package/{lib → dist}/util/parseHTML.js +0 -0
- /package/{esm/util/parseHTML.js → dist/util/parseHTML.mjs} +0 -0
- /package/{lib → dist}/util/rangeSort.js +0 -0
- /package/{esm/util/rangeSort.js → dist/util/rangeSort.mjs} +0 -0
- /package/{lib → dist}/util/splitReactElement.js +0 -0
- /package/{esm/util/splitReactElement.js → dist/util/splitReactElement.mjs} +0 -0
- /package/{lib → dist}/util/styleObjectFunction.js +0 -0
- /package/{esm/util/styleObjectFunction.js → dist/util/styleObjectFunction.mjs} +0 -0
- /package/{lib → dist}/util/updateMutation.js +0 -0
- /package/{esm/util/updateMutation.js → dist/util/updateMutation.mjs} +0 -0
|
@@ -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.mjs';
|
|
2
|
+
import rangeSort from './util/rangeSort.mjs';
|
|
3
|
+
import getElementHTML from './util/getElementHTML.mjs';
|
|
4
|
+
import getElementTagLength from './util/getElementTagLength.mjs';
|
|
5
5
|
const converter = (entity = {}, originalText = '')=>originalText;
|
|
6
6
|
export default ((block, entityMap, entityConverter = converter)=>{
|
|
7
7
|
let resultText = [
|
|
@@ -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.mjs';
|
|
3
|
+
import accumulateFunction from './util/accumulateFunction.mjs';
|
|
4
|
+
import getElementHTML from './util/getElementHTML.mjs';
|
|
5
|
+
import rangeSort from './util/rangeSort.mjs';
|
|
6
|
+
import defaultInlineHTML from './default/defaultInlineHTML.mjs';
|
|
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];
|
|
@@ -8,7 +8,9 @@
|
|
|
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
|
-
*/
|
|
11
|
+
*/ // immutable is CJS-only; default import maps to module.exports for Node ESM interop.
|
|
12
|
+
// eslint-disable-next-line import/default
|
|
13
|
+
"use strict";
|
|
12
14
|
Object.defineProperty(exports, "__esModule", {
|
|
13
15
|
value: true
|
|
14
16
|
});
|
|
@@ -18,8 +20,8 @@ Object.defineProperty(exports, "default", {
|
|
|
18
20
|
return _default;
|
|
19
21
|
}
|
|
20
22
|
});
|
|
21
|
-
const _immutable = /*#__PURE__*/
|
|
22
|
-
const _draftjs = /*#__PURE__*/
|
|
23
|
+
const _immutable = /*#__PURE__*/ _interop_require_default(require("immutable"));
|
|
24
|
+
const _draftjs = /*#__PURE__*/ _interop_require_default(require("draft-js"));
|
|
23
25
|
const _parseHTML = /*#__PURE__*/ _interop_require_default(require("./util/parseHTML.js"));
|
|
24
26
|
const _rangeSort = /*#__PURE__*/ _interop_require_default(require("./util/rangeSort.js"));
|
|
25
27
|
function _interop_require_default(obj) {
|
|
@@ -27,49 +29,9 @@ function _interop_require_default(obj) {
|
|
|
27
29
|
default: obj
|
|
28
30
|
};
|
|
29
31
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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;
|
|
32
|
+
// eslint-disable-next-line import/no-named-as-default-member
|
|
33
|
+
const { List, Map, OrderedSet } = _immutable.default;
|
|
34
|
+
const { BlockMapBuilder, CharacterMetadata, ContentBlock, ContentState, Entity, SelectionState, genKey } = _draftjs.default;
|
|
73
35
|
const SPACE = ' ';
|
|
74
36
|
// Arbitrary max indent
|
|
75
37
|
const MAX_DEPTH = 4;
|
|
@@ -8,10 +8,13 @@
|
|
|
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
|
-
|
|
13
|
-
import
|
|
14
|
-
import
|
|
11
|
+
*/ // immutable is CJS-only; default import maps to module.exports for Node ESM interop.
|
|
12
|
+
// eslint-disable-next-line import/default
|
|
13
|
+
import Immutable from 'immutable';
|
|
14
|
+
import DraftJS from 'draft-js';
|
|
15
|
+
import getSafeBodyFromHTML from './util/parseHTML.mjs';
|
|
16
|
+
import rangeSort from './util/rangeSort.mjs';
|
|
17
|
+
// eslint-disable-next-line import/no-named-as-default-member
|
|
15
18
|
const { List, Map, OrderedSet } = Immutable;
|
|
16
19
|
const { BlockMapBuilder, CharacterMetadata, ContentBlock, ContentState, Entity, SelectionState, genKey } = DraftJS;
|
|
17
20
|
const SPACE = ' ';
|
|
@@ -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 _draftjs = /*#__PURE__*/
|
|
14
|
+
const _draftjs = /*#__PURE__*/ _interop_require_default(require("draft-js"));
|
|
15
15
|
const _encodeBlock = /*#__PURE__*/ _interop_require_default(require("./encodeBlock.js"));
|
|
16
16
|
const _blockEntities = /*#__PURE__*/ _interop_require_default(require("./blockEntities.js"));
|
|
17
17
|
const _blockInlineStyles = /*#__PURE__*/ _interop_require_default(require("./blockInlineStyles.js"));
|
|
@@ -25,48 +25,7 @@ function _interop_require_default(obj) {
|
|
|
25
25
|
default: obj
|
|
26
26
|
};
|
|
27
27
|
}
|
|
28
|
-
|
|
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
|
+
const { convertToRaw } = _draftjs.default;
|
|
70
29
|
const defaultEntityToHTML = (entity, originalText)=>originalText;
|
|
71
30
|
const defaultValidateHTML = (html)=>true;
|
|
72
31
|
const convertToHTML = ({ styleToHTML = {}, blockToHTML = {}, entityToHTML = defaultEntityToHTML, validateHTML = defaultValidateHTML })=>(contentState)=>{
|
|
@@ -1,15 +1,15 @@
|
|
|
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 DraftJS from 'draft-js';
|
|
5
|
+
import encodeBlock from './encodeBlock.mjs';
|
|
6
|
+
import blockEntities from './blockEntities.mjs';
|
|
7
|
+
import blockInlineStyles from './blockInlineStyles.mjs';
|
|
8
|
+
import accumulateFunction from './util/accumulateFunction.mjs';
|
|
9
|
+
import blockTypeObjectFunction from './util/blockTypeObjectFunction.mjs';
|
|
10
|
+
import getBlockTags from './util/getBlockTags.mjs';
|
|
11
|
+
import getNestedBlockTags from './util/getNestedBlockTags.mjs';
|
|
12
|
+
import defaultBlockHTML from './default/defaultBlockHTML.mjs';
|
|
13
13
|
const { convertToRaw } = DraftJS;
|
|
14
14
|
const defaultEntityToHTML = (entity, originalText)=>originalText;
|
|
15
15
|
const defaultValidateHTML = (html)=>true;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Based on: https://github.com/HubSpot/draft-convert/issues/107#issuecomment-488581709 by <https://github.com/sbusch>
|
|
2
|
+
// Grabbed from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/draft-convert/index.d.ts
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
ContentState,
|
|
6
|
+
DraftBlockType,
|
|
7
|
+
DraftEntityMutability,
|
|
8
|
+
DraftInlineStyleType,
|
|
9
|
+
Entity,
|
|
10
|
+
RawDraftContentBlock,
|
|
11
|
+
RawDraftEntity,
|
|
12
|
+
} from 'draft-js'
|
|
13
|
+
import { ReactNode } from 'react'
|
|
14
|
+
|
|
15
|
+
export type RawDraftContentBlockWithCustomType<T> = Omit<
|
|
16
|
+
RawDraftContentBlock,
|
|
17
|
+
'type'
|
|
18
|
+
> & {
|
|
19
|
+
type: T
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type ExtendedHTMLElement<T> = (HTMLElement | HTMLLinkElement) & T
|
|
23
|
+
|
|
24
|
+
export type EntityKey = string
|
|
25
|
+
|
|
26
|
+
export type ContentStateConverter = (contentState: ContentState) => string
|
|
27
|
+
export type HTMLConverter = (html: string) => ContentState
|
|
28
|
+
|
|
29
|
+
export type Tag =
|
|
30
|
+
| ReactNode
|
|
31
|
+
| { start: string; end: string; empty?: string | undefined }
|
|
32
|
+
| {
|
|
33
|
+
element: ReactNode
|
|
34
|
+
empty?: ReactNode | undefined
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface IConvertToHTMLConfig<
|
|
38
|
+
S = DraftInlineStyleType,
|
|
39
|
+
B extends DraftBlockType = DraftBlockType,
|
|
40
|
+
E extends RawDraftEntity = RawDraftEntity,
|
|
41
|
+
> {
|
|
42
|
+
// Inline styles:
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
44
|
+
styleToHTML?: ((style: S) => Tag | null | undefined | void) | undefined
|
|
45
|
+
|
|
46
|
+
// Block styles:
|
|
47
|
+
blockToHTML?: ((block: RawDraftContentBlockWithCustomType<B>) => Tag | null | undefined) | undefined
|
|
48
|
+
|
|
49
|
+
// Entity styling:
|
|
50
|
+
entityToHTML?: ((entity: E, originalText: string) => Tag | null | undefined) | undefined
|
|
51
|
+
|
|
52
|
+
// HTML validation:
|
|
53
|
+
validateHTML?: ((html: string) => boolean) | undefined
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export interface IConvertFromHTMLConfig<
|
|
57
|
+
S extends {
|
|
58
|
+
[name: string]: unknown
|
|
59
|
+
} = DOMStringMap,
|
|
60
|
+
B extends DraftBlockType = DraftBlockType,
|
|
61
|
+
E extends RawDraftEntity = RawDraftEntity,
|
|
62
|
+
> {
|
|
63
|
+
// Inline styles:
|
|
64
|
+
htmlToStyle?:
|
|
65
|
+
| ((nodeName: string, node: ExtendedHTMLElement<S>, currentStyle: Set<string>) => Set<string>)
|
|
66
|
+
| undefined
|
|
67
|
+
|
|
68
|
+
// Block styles:
|
|
69
|
+
htmlToBlock?:
|
|
70
|
+
| ((
|
|
71
|
+
nodeName: string,
|
|
72
|
+
node: ExtendedHTMLElement<S>
|
|
73
|
+
) => B | { type: B; data: object } | false | undefined)
|
|
74
|
+
| undefined
|
|
75
|
+
|
|
76
|
+
// Html entities
|
|
77
|
+
htmlToEntity?:
|
|
78
|
+
| ((
|
|
79
|
+
nodeName: string,
|
|
80
|
+
node: ExtendedHTMLElement<S>,
|
|
81
|
+
createEntity: (type: E['type'], mutability: DraftEntityMutability, data: E['data']) => EntityKey,
|
|
82
|
+
getEntity: (key: EntityKey) => Entity,
|
|
83
|
+
mergeEntityData: (key: string, data: object) => void,
|
|
84
|
+
replaceEntityData: (key: string, data: object) => void
|
|
85
|
+
) => EntityKey | undefined)
|
|
86
|
+
| undefined
|
|
87
|
+
|
|
88
|
+
// Text entities
|
|
89
|
+
textToEntity?:
|
|
90
|
+
| ((
|
|
91
|
+
text: string,
|
|
92
|
+
createEntity: (type: E['type'], mutability: DraftEntityMutability, data: E['data']) => EntityKey,
|
|
93
|
+
getEntity: (key: EntityKey) => Entity,
|
|
94
|
+
mergeEntityData: (key: string, data: object) => void,
|
|
95
|
+
replaceEntityData: (key: string, data: object) => void
|
|
96
|
+
) => Array<{
|
|
97
|
+
entity: EntityKey
|
|
98
|
+
offset: number
|
|
99
|
+
length: number
|
|
100
|
+
result?: string | undefined
|
|
101
|
+
}>)
|
|
102
|
+
| undefined
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function convertToHTML<
|
|
106
|
+
S = DraftInlineStyleType,
|
|
107
|
+
B extends DraftBlockType = DraftBlockType,
|
|
108
|
+
E extends RawDraftEntity = RawDraftEntity,
|
|
109
|
+
>(config: IConvertToHTMLConfig<S, B, E>): ContentStateConverter
|
|
110
|
+
export function convertToHTML(contentState: ContentState): string
|
|
111
|
+
|
|
112
|
+
export function convertFromHTML<
|
|
113
|
+
S extends {
|
|
114
|
+
[name: string]: unknown
|
|
115
|
+
} = DOMStringMap,
|
|
116
|
+
B extends DraftBlockType = DraftBlockType,
|
|
117
|
+
E extends RawDraftEntity = RawDraftEntity,
|
|
118
|
+
>(config: IConvertFromHTMLConfig<S, B, E>): HTMLConverter
|
|
119
|
+
export function convertFromHTML(html: string): ContentState
|
|
120
|
+
|
|
121
|
+
export function parseHTML(html: string): HTMLBodyElement
|
package/dist/index.mjs
ADDED
|
@@ -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.mjs';
|
|
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.mjs';
|
|
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.mjs';
|
|
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.mjs';
|
|
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/package.json
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@20minutes/draft-convert",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.2",
|
|
4
4
|
"description": "Extensibly serialize & deserialize Draft.js ContentState",
|
|
5
|
-
"main": "
|
|
6
|
-
"
|
|
7
|
-
"
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
-
"types": "./
|
|
11
|
-
"
|
|
12
|
-
"
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.mjs",
|
|
12
|
+
"require": "./dist/index.cjs",
|
|
13
|
+
"default": "./dist/index.mjs"
|
|
13
14
|
}
|
|
14
15
|
},
|
|
15
16
|
"repository": "20minutes/draft-convert",
|
|
16
17
|
"scripts": {
|
|
17
|
-
"build": "yarn build:cjs && yarn build:
|
|
18
|
-
"build:cjs": "swc src -d
|
|
19
|
-
"build:esm": "swc src -d
|
|
18
|
+
"build": "yarn clean && yarn build:esm && yarn build:cjs && yarn build:types",
|
|
19
|
+
"build:cjs": "swc src -d dist --config-file .swcrc.cjs --strip-leading-paths && node scripts/rename-dist-cjs-entry.js",
|
|
20
|
+
"build:esm": "swc src -d dist --config-file .swcrc.esm --strip-leading-paths --delete-dir-on-start --out-file-extension mjs && node scripts/fix-dist-esm-imports.js",
|
|
21
|
+
"build:types": "node scripts/build-dist-types.js",
|
|
20
22
|
"test": "jest",
|
|
21
23
|
"test:watch": "jest --watch",
|
|
22
|
-
"clean": "rimraf ./lib ./esm",
|
|
24
|
+
"clean": "rimraf ./dist ./lib ./esm",
|
|
23
25
|
"build-and-test": "yarn clean && yarn test",
|
|
24
26
|
"lint": "eslint src/ test/"
|
|
25
27
|
},
|
|
26
28
|
"files": [
|
|
27
|
-
"
|
|
28
|
-
"lib",
|
|
29
|
-
"esm"
|
|
29
|
+
"dist"
|
|
30
30
|
],
|
|
31
31
|
"keywords": [
|
|
32
32
|
"draft",
|
package/esm/index.js
DELETED
package/types/index.d.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
// Based on: https://github.com/HubSpot/draft-convert/issues/107#issuecomment-488581709 by <https://github.com/sbusch>
|
|
2
|
-
// Grabbed from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/draft-convert/index.d.ts
|
|
3
|
-
|
|
4
|
-
declare module "@20minutes/draft-convert" {
|
|
5
|
-
import {
|
|
6
|
-
ContentState,
|
|
7
|
-
DraftBlockType,
|
|
8
|
-
DraftEntityMutability,
|
|
9
|
-
DraftInlineStyleType,
|
|
10
|
-
Entity,
|
|
11
|
-
RawDraftContentBlock,
|
|
12
|
-
RawDraftEntity,
|
|
13
|
-
} from "draft-js";
|
|
14
|
-
import { ReactNode } from "react";
|
|
15
|
-
|
|
16
|
-
type RawDraftContentBlockWithCustomType<T> = Omit<RawDraftContentBlock, "type"> & {
|
|
17
|
-
type: T;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
type ExtendedHTMLElement<T> = (HTMLElement | HTMLLinkElement) & T;
|
|
21
|
-
|
|
22
|
-
interface IConvertToHTMLConfig<
|
|
23
|
-
S = DraftInlineStyleType,
|
|
24
|
-
B extends DraftBlockType = DraftBlockType,
|
|
25
|
-
E extends RawDraftEntity = RawDraftEntity,
|
|
26
|
-
> {
|
|
27
|
-
// Inline styles:
|
|
28
|
-
// eslint-disable-next-line @typescript-eslint/no-invalid-void-type
|
|
29
|
-
styleToHTML?: ((style: S) => Tag | void) | undefined;
|
|
30
|
-
|
|
31
|
-
// Block styles:
|
|
32
|
-
blockToHTML?: ((block: RawDraftContentBlockWithCustomType<B>) => Tag) | undefined;
|
|
33
|
-
|
|
34
|
-
// Entity styling:
|
|
35
|
-
entityToHTML?: ((entity: E, originalText: string) => Tag) | undefined;
|
|
36
|
-
|
|
37
|
-
// HTML validation:
|
|
38
|
-
validateHTML?: ((html: string) => boolean) | undefined;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
type EntityKey = string;
|
|
42
|
-
|
|
43
|
-
interface IConvertFromHTMLConfig<
|
|
44
|
-
S extends {
|
|
45
|
-
[name: string]: unknown;
|
|
46
|
-
} = DOMStringMap,
|
|
47
|
-
B extends DraftBlockType = DraftBlockType,
|
|
48
|
-
E extends RawDraftEntity = RawDraftEntity,
|
|
49
|
-
> {
|
|
50
|
-
// Inline styles:
|
|
51
|
-
htmlToStyle?:
|
|
52
|
-
| ((nodeName: string, node: ExtendedHTMLElement<S>, currentStyle: Set<string>) => Set<string>)
|
|
53
|
-
| undefined;
|
|
54
|
-
|
|
55
|
-
// Block styles:
|
|
56
|
-
htmlToBlock?:
|
|
57
|
-
| ((
|
|
58
|
-
nodeName: string,
|
|
59
|
-
node: ExtendedHTMLElement<S>,
|
|
60
|
-
) => B | { type: B; data: object } | false | undefined)
|
|
61
|
-
| undefined;
|
|
62
|
-
|
|
63
|
-
// Html entities
|
|
64
|
-
htmlToEntity?:
|
|
65
|
-
| ((
|
|
66
|
-
nodeName: string,
|
|
67
|
-
node: ExtendedHTMLElement<S>,
|
|
68
|
-
createEntity: (type: E["type"], mutability: DraftEntityMutability, data: E["data"]) => EntityKey,
|
|
69
|
-
getEntity: (key: EntityKey) => Entity,
|
|
70
|
-
mergeEntityData: (key: string, data: object) => void,
|
|
71
|
-
replaceEntityData: (key: string, data: object) => void,
|
|
72
|
-
) => EntityKey | undefined)
|
|
73
|
-
| undefined;
|
|
74
|
-
|
|
75
|
-
// Text entities
|
|
76
|
-
textToEntity?:
|
|
77
|
-
| ((
|
|
78
|
-
text: string,
|
|
79
|
-
createEntity: (type: E["type"], mutability: DraftEntityMutability, data: E["data"]) => EntityKey,
|
|
80
|
-
getEntity: (key: EntityKey) => Entity,
|
|
81
|
-
mergeEntityData: (key: string, data: object) => void,
|
|
82
|
-
replaceEntityData: (key: string, data: object) => void,
|
|
83
|
-
) => Array<{
|
|
84
|
-
entity: EntityKey;
|
|
85
|
-
offset: number;
|
|
86
|
-
length: number;
|
|
87
|
-
result?: string | undefined;
|
|
88
|
-
}>)
|
|
89
|
-
| undefined;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
type ContentStateConverter = (contentState: ContentState) => string;
|
|
93
|
-
type HTMLConverter = (html: string) => ContentState;
|
|
94
|
-
|
|
95
|
-
type Tag = ReactNode | { start: string; end: string; empty?: string | undefined } | {
|
|
96
|
-
element: ReactNode;
|
|
97
|
-
empty?: ReactNode | undefined;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
export function convertToHTML<
|
|
101
|
-
S = DraftInlineStyleType,
|
|
102
|
-
B extends DraftBlockType = DraftBlockType,
|
|
103
|
-
E extends RawDraftEntity = RawDraftEntity,
|
|
104
|
-
>(config: IConvertToHTMLConfig<S, B, E>): ContentStateConverter;
|
|
105
|
-
export function convertToHTML(contentState: ContentState): string;
|
|
106
|
-
export function convertFromHTML<
|
|
107
|
-
S extends {
|
|
108
|
-
[name: string]: unknown;
|
|
109
|
-
} = DOMStringMap,
|
|
110
|
-
B extends DraftBlockType = DraftBlockType,
|
|
111
|
-
E extends RawDraftEntity = RawDraftEntity,
|
|
112
|
-
>(config: IConvertFromHTMLConfig<S, B, E>): HTMLConverter;
|
|
113
|
-
export function convertFromHTML(html: string): ContentState;
|
|
114
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|