@pie-lib/editable-html 9.5.13 → 10.0.0-beta.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/CHANGELOG.md +0 -302
- package/lib/components.js +116 -0
- package/lib/components.js.map +1 -0
- package/lib/editor.js +418 -103
- package/lib/editor.js.map +1 -1
- package/lib/index.js +101 -155
- package/lib/index.js.map +1 -1
- package/lib/new-serialization.js +320 -0
- package/lib/new-serialization.js.map +1 -0
- package/lib/old-serialization.js +330 -0
- package/lib/parse-html.js +1 -1
- package/lib/parse-html.js.map +1 -1
- package/lib/plugins/characters/custom-popper.js +1 -1
- package/lib/plugins/characters/custom-popper.js.map +1 -1
- package/lib/plugins/characters/index.js +21 -19
- package/lib/plugins/characters/index.js.map +1 -1
- package/lib/plugins/characters/utils.js +1 -1
- package/lib/plugins/characters/utils.js.map +1 -1
- package/lib/plugins/hotKeys/index.js +67 -0
- package/lib/plugins/hotKeys/index.js.map +1 -0
- package/lib/plugins/image/alt-dialog.js +1 -6
- package/lib/plugins/image/alt-dialog.js.map +1 -1
- package/lib/plugins/image/component.js +70 -53
- package/lib/plugins/image/component.js.map +1 -1
- package/lib/plugins/image/image-toolbar.js +7 -9
- package/lib/plugins/image/image-toolbar.js.map +1 -1
- package/lib/plugins/image/index.js +83 -27
- package/lib/plugins/image/index.js.map +1 -1
- package/lib/plugins/image/insert-image-handler.js +72 -33
- package/lib/plugins/image/insert-image-handler.js.map +1 -1
- package/lib/plugins/index.js +23 -41
- package/lib/plugins/index.js.map +1 -1
- package/lib/plugins/list/index.js +64 -100
- package/lib/plugins/list/index.js.map +1 -1
- package/lib/plugins/math/index.js +86 -60
- package/lib/plugins/math/index.js.map +1 -1
- package/lib/plugins/media/index.js +202 -132
- package/lib/plugins/media/index.js.map +1 -1
- package/lib/plugins/media/media-dialog.js +17 -16
- package/lib/plugins/media/media-dialog.js.map +1 -1
- package/lib/plugins/media/media-toolbar.js +3 -3
- package/lib/plugins/media/media-toolbar.js.map +1 -1
- package/lib/plugins/media/media-wrapper.js +21 -58
- package/lib/plugins/media/media-wrapper.js.map +1 -1
- package/lib/plugins/respArea/drag-in-the-blank/choice.js +3 -3
- package/lib/plugins/respArea/drag-in-the-blank/choice.js.map +1 -1
- package/lib/plugins/respArea/drag-in-the-blank/index.js +3 -2
- package/lib/plugins/respArea/drag-in-the-blank/index.js.map +1 -1
- package/lib/plugins/respArea/explicit-constructed-response/index.js +3 -2
- package/lib/plugins/respArea/explicit-constructed-response/index.js.map +1 -1
- package/lib/plugins/respArea/icons/index.js +13 -15
- package/lib/plugins/respArea/icons/index.js.map +1 -1
- package/lib/plugins/respArea/index.js +87 -53
- package/lib/plugins/respArea/index.js.map +1 -1
- package/lib/plugins/respArea/inline-dropdown/index.js +4 -3
- package/lib/plugins/respArea/inline-dropdown/index.js.map +1 -1
- package/lib/plugins/respArea/utils.js +17 -20
- package/lib/plugins/respArea/utils.js.map +1 -1
- package/lib/plugins/table/icons/index.js +1 -1
- package/lib/plugins/table/icons/index.js.map +1 -1
- package/lib/plugins/table/index.js +381 -212
- package/lib/plugins/table/index.js.map +1 -1
- package/lib/plugins/table/table-toolbar.js +5 -6
- package/lib/plugins/table/table-toolbar.js.map +1 -1
- package/lib/plugins/toolbar/default-toolbar.js +55 -11
- package/lib/plugins/toolbar/default-toolbar.js.map +1 -1
- package/lib/plugins/toolbar/done-button.js +1 -1
- package/lib/plugins/toolbar/done-button.js.map +1 -1
- package/lib/plugins/toolbar/editor-and-toolbar.js +186 -232
- package/lib/plugins/toolbar/editor-and-toolbar.js.map +1 -1
- package/lib/plugins/toolbar/index.js +1 -2
- package/lib/plugins/toolbar/index.js.map +1 -1
- package/lib/plugins/toolbar/toolbar-buttons.js +1 -1
- package/lib/plugins/toolbar/toolbar-buttons.js.map +1 -1
- package/lib/plugins/toolbar/toolbar.js +253 -239
- package/lib/plugins/toolbar/toolbar.js.map +1 -1
- package/lib/plugins/utils.js +27 -2
- package/lib/plugins/utils.js.map +1 -1
- package/lib/serialization.js +1 -1
- package/lib/serialization.js.map +1 -1
- package/lib/slate-editor.js +302 -0
- package/lib/test-serializer.js +189 -0
- package/lib/test-serializer.js.map +1 -0
- package/lib/theme.js +1 -1
- package/lib/theme.js.map +1 -1
- package/package.json +18 -14
- package/playground/image/data.js +20 -20
- package/playground/image/index.html +22 -20
- package/playground/image/index.jsx +12 -10
- package/playground/index.html +25 -23
- package/playground/mathquill/index.html +23 -20
- package/playground/mathquill/index.jsx +18 -22
- package/playground/prod-test/index.html +24 -20
- package/playground/prod-test/index.jsx +5 -3
- package/playground/schema-override/data.js +10 -10
- package/playground/schema-override/image-plugin.jsx +3 -4
- package/playground/schema-override/index.html +21 -19
- package/playground/schema-override/index.jsx +13 -14
- package/playground/serialization/data.js +10 -10
- package/playground/serialization/image-plugin.jsx +3 -4
- package/playground/serialization/index.html +22 -20
- package/playground/table-examples.html +5 -8
- package/playground/webpack.config.js +10 -10
- package/src/components.js +135 -0
- package/src/editor.jsx +478 -141
- package/src/index.jsx +71 -95
- package/src/new-serialization.jsx +291 -0
- package/src/parse-html.js +1 -1
- package/src/plugins/characters/custom-popper.js +7 -7
- package/src/plugins/characters/index.jsx +33 -34
- package/src/plugins/characters/utils.js +81 -81
- package/src/plugins/hotKeys/index.js +54 -0
- package/src/plugins/image/alt-dialog.jsx +4 -5
- package/src/plugins/image/component.jsx +106 -89
- package/src/plugins/image/image-toolbar.jsx +27 -19
- package/src/plugins/image/index.jsx +75 -43
- package/src/plugins/image/insert-image-handler.js +62 -27
- package/src/plugins/index.jsx +23 -41
- package/src/plugins/list/index.jsx +70 -95
- package/src/plugins/math/index.jsx +102 -82
- package/src/plugins/media/index.jsx +159 -124
- package/src/plugins/media/media-dialog.js +98 -71
- package/src/plugins/media/media-toolbar.jsx +8 -8
- package/src/plugins/media/media-wrapper.jsx +29 -30
- package/src/plugins/respArea/drag-in-the-blank/choice.jsx +21 -19
- package/src/plugins/respArea/drag-in-the-blank/index.jsx +14 -11
- package/src/plugins/respArea/explicit-constructed-response/index.jsx +7 -6
- package/src/plugins/respArea/icons/index.jsx +11 -14
- package/src/plugins/respArea/index.jsx +92 -52
- package/src/plugins/respArea/inline-dropdown/index.jsx +9 -8
- package/src/plugins/respArea/utils.jsx +26 -35
- package/src/plugins/table/icons/index.jsx +17 -11
- package/src/plugins/table/index.jsx +288 -231
- package/src/plugins/table/table-toolbar.jsx +15 -11
- package/src/plugins/toolbar/default-toolbar.jsx +65 -19
- package/src/plugins/toolbar/done-button.jsx +4 -4
- package/src/plugins/toolbar/editor-and-toolbar.jsx +150 -145
- package/src/plugins/toolbar/index.jsx +2 -3
- package/src/plugins/toolbar/toolbar-buttons.jsx +11 -11
- package/src/plugins/toolbar/toolbar.jsx +244 -221
- package/src/plugins/utils.js +21 -4
- package/src/serialization.jsx +32 -32
- package/src/test-serializer.js +139 -0
- package/src/test-serializer.js.rej +20 -0
package/src/index.jsx
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import React from 'react';
|
|
1
|
+
import React, { useState, useRef, useEffect } from 'react';
|
|
2
|
+
import { useSlate } from 'slate-react';
|
|
2
3
|
import PropTypes from 'prop-types';
|
|
3
4
|
import Editor, { DEFAULT_PLUGINS, ALL_PLUGINS } from './editor';
|
|
4
|
-
import { htmlToValue, valueToHtml } from './serialization';
|
|
5
|
+
import { htmlToValue, valueToHtml } from './new-serialization';
|
|
5
6
|
import { parseDegrees } from './parse-html';
|
|
6
7
|
import debug from 'debug';
|
|
7
|
-
import { Range } from 'slate';
|
|
8
8
|
|
|
9
9
|
const log = debug('@pie-lib:editable-html');
|
|
10
10
|
/**
|
|
@@ -12,78 +12,51 @@ const log = debug('@pie-lib:editable-html');
|
|
|
12
12
|
*/
|
|
13
13
|
export { htmlToValue, valueToHtml, Editor, DEFAULT_PLUGINS, ALL_PLUGINS };
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
* Because of the mismatch between the markup and the `Value` we need to convert the incoming markup to a value and
|
|
18
|
-
* compare it. TODO: This is an interim fix, we'll need to strip back `Editor` and look how best to maintain the
|
|
19
|
-
* `markup` api whilst avoiding the serialization mismatch. We should be making better use of schemas w/ normalize.
|
|
20
|
-
*/
|
|
15
|
+
const useConstructor = (callback = () => {}) => {
|
|
16
|
+
const [hasBeenCalled, setHasBeenCalled] = useState(false);
|
|
21
17
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return markup.replace(/(<br\s*\/?>){3,}/gi, '<br>');
|
|
25
|
-
} catch (e) {
|
|
26
|
-
console.log("Couldn't remove <br/> tags: ", e);
|
|
18
|
+
if (hasBeenCalled) {
|
|
19
|
+
return;
|
|
27
20
|
}
|
|
28
21
|
|
|
29
|
-
|
|
22
|
+
callback();
|
|
23
|
+
setHasBeenCalled(true);
|
|
30
24
|
};
|
|
31
25
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
markup: PropTypes.string.isRequired,
|
|
37
|
-
allowValidation: PropTypes.bool,
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
static defaultProps = {
|
|
41
|
-
onDone: () => {},
|
|
42
|
-
allowValidation: false,
|
|
43
|
-
};
|
|
26
|
+
const EditableHtml = props => {
|
|
27
|
+
const editorRef = useRef(null);
|
|
28
|
+
const rootRef = useRef(null);
|
|
29
|
+
const [value, setValue] = useState();
|
|
44
30
|
|
|
45
|
-
|
|
46
|
-
super(props);
|
|
31
|
+
useConstructor(() => {
|
|
47
32
|
const v = htmlToValue(props.markup);
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// eslint-disable-next-line react/no-deprecated
|
|
54
|
-
componentWillReceiveProps(props) {
|
|
55
|
-
if (!props.allowValidation && props.markup === this.props.markup) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const v = htmlToValue(reduceMultipleBrs(props.markup));
|
|
60
|
-
const current = htmlToValue(reduceMultipleBrs(this.props.markup));
|
|
61
|
-
|
|
62
|
-
if (v.equals && !v.equals(current)) {
|
|
63
|
-
this.setState({ value: v });
|
|
64
|
-
}
|
|
65
|
-
}
|
|
33
|
+
setValue(v);
|
|
34
|
+
});
|
|
66
35
|
|
|
67
|
-
onChange = (value, done) => {
|
|
36
|
+
const onChange = (value, done) => {
|
|
68
37
|
const html = valueToHtml(value);
|
|
69
38
|
const htmlParsed = parseDegrees(html);
|
|
70
39
|
|
|
71
40
|
log('value as html: ', html);
|
|
72
41
|
|
|
73
|
-
if (html !==
|
|
74
|
-
|
|
42
|
+
if (html !== props.markup) {
|
|
43
|
+
props.onChange(htmlParsed);
|
|
75
44
|
}
|
|
76
45
|
|
|
77
46
|
if (done) {
|
|
78
|
-
|
|
47
|
+
props.onDone(htmlParsed);
|
|
79
48
|
}
|
|
80
49
|
};
|
|
81
50
|
|
|
82
|
-
focus = (position, node
|
|
51
|
+
const focus = (position, node) => {
|
|
83
52
|
if (this.editorRef) {
|
|
84
|
-
this.editorRef.change(
|
|
85
|
-
const lastText = node
|
|
86
|
-
|
|
53
|
+
this.editorRef.change(c => {
|
|
54
|
+
const lastText = node
|
|
55
|
+
? c.value.document.getNextText(node.key)
|
|
56
|
+
: c.value.document.getLastText();
|
|
57
|
+
const editorDOM = document.querySelector(
|
|
58
|
+
`[data-key="${this.editorRef.value.document.key}"]`
|
|
59
|
+
);
|
|
87
60
|
|
|
88
61
|
if (editorDOM !== document.activeElement) {
|
|
89
62
|
document.activeElement.blur();
|
|
@@ -92,18 +65,10 @@ export default class EditableHtml extends React.Component {
|
|
|
92
65
|
c.focus();
|
|
93
66
|
|
|
94
67
|
if (position === 'end' && lastText) {
|
|
95
|
-
c.moveFocusTo(lastText.key, lastText.text?.length).moveAnchorTo(
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
anchorOffset: 0,
|
|
100
|
-
focusKey: lastText.key,
|
|
101
|
-
focusOffset: lastText.text?.length,
|
|
102
|
-
isFocused: true,
|
|
103
|
-
isBackward: false,
|
|
104
|
-
});
|
|
105
|
-
c.select(range);
|
|
106
|
-
}
|
|
68
|
+
c.moveFocusTo(lastText.key, lastText.text?.length).moveAnchorTo(
|
|
69
|
+
lastText.key,
|
|
70
|
+
lastText.text?.length
|
|
71
|
+
);
|
|
107
72
|
}
|
|
108
73
|
|
|
109
74
|
if (position === 'beginning' && lastText) {
|
|
@@ -113,38 +78,49 @@ export default class EditableHtml extends React.Component {
|
|
|
113
78
|
}
|
|
114
79
|
};
|
|
115
80
|
|
|
116
|
-
finishEditing = () => {
|
|
81
|
+
const finishEditing = () => {
|
|
117
82
|
if (this.editorRef) {
|
|
118
83
|
this.editorRef.props.onEditingDone();
|
|
119
84
|
}
|
|
120
85
|
};
|
|
121
86
|
|
|
122
|
-
|
|
123
|
-
const { value } = this.state;
|
|
124
|
-
const { toolbarOpts, error } = this.props;
|
|
87
|
+
const { toolbarOpts, error } = props;
|
|
125
88
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
const props = {
|
|
131
|
-
...this.props,
|
|
132
|
-
markup: null,
|
|
133
|
-
value,
|
|
134
|
-
onChange: this.onChange,
|
|
135
|
-
focus: this.focus,
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
return (
|
|
139
|
-
<Editor
|
|
140
|
-
onRef={(ref) => {
|
|
141
|
-
if (ref) {
|
|
142
|
-
this.rootRef = ref;
|
|
143
|
-
}
|
|
144
|
-
}}
|
|
145
|
-
editorRef={(ref) => ref && (this.editorRef = ref)}
|
|
146
|
-
{...props}
|
|
147
|
-
/>
|
|
148
|
-
);
|
|
89
|
+
if (toolbarOpts) {
|
|
90
|
+
toolbarOpts.error = error;
|
|
149
91
|
}
|
|
150
|
-
|
|
92
|
+
|
|
93
|
+
const newProps = {
|
|
94
|
+
...props,
|
|
95
|
+
markup: null,
|
|
96
|
+
value,
|
|
97
|
+
onChange
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
return (
|
|
101
|
+
<Editor
|
|
102
|
+
{...newProps}
|
|
103
|
+
onRef={ref => {
|
|
104
|
+
if (ref) {
|
|
105
|
+
rootRef.current = ref;
|
|
106
|
+
}
|
|
107
|
+
}}
|
|
108
|
+
editorRef={ref => ref && (editorRef.current = ref)}
|
|
109
|
+
/>
|
|
110
|
+
);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
EditableHtml.propTypes = {
|
|
114
|
+
onChange: PropTypes.func.isRequired,
|
|
115
|
+
onDone: PropTypes.func,
|
|
116
|
+
onEditor: PropTypes.func,
|
|
117
|
+
markup: PropTypes.string.isRequired,
|
|
118
|
+
allowValidation: PropTypes.bool
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
EditableHtml.defaultProps = {
|
|
122
|
+
onDone: () => {},
|
|
123
|
+
allowValidation: false
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export default EditableHtml;
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import TestSerializer from './test-serializer';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import debug from 'debug';
|
|
4
|
+
import { object as toStyleObject } from 'to-style';
|
|
5
|
+
|
|
6
|
+
import { serialization as imgSerialization } from './plugins/image';
|
|
7
|
+
import { serialization as mathSerialization } from './plugins/math';
|
|
8
|
+
import { serialization as mediaSerialization } from './plugins/media';
|
|
9
|
+
import { serialization as listSerialization } from './plugins/list';
|
|
10
|
+
import { serialization as tableSerialization } from './plugins/table';
|
|
11
|
+
import { serialization as responseAreaSerialization } from './plugins/respArea';
|
|
12
|
+
import { Mark, Value } from 'slate';
|
|
13
|
+
import { jsx } from "slate-hyperscript";
|
|
14
|
+
|
|
15
|
+
const log = debug('@pie-lib:editable-html:serialization');
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Tags to blocks.
|
|
19
|
+
*
|
|
20
|
+
* @type {Object}
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export const BLOCK_TAGS = {
|
|
24
|
+
div: 'div',
|
|
25
|
+
span: 'span',
|
|
26
|
+
p: 'paragraph',
|
|
27
|
+
blockquote: 'quote',
|
|
28
|
+
pre: 'code',
|
|
29
|
+
h1: 'heading-one',
|
|
30
|
+
h2: 'heading-two',
|
|
31
|
+
h3: 'heading-three',
|
|
32
|
+
h4: 'heading-four',
|
|
33
|
+
h5: 'heading-five',
|
|
34
|
+
h6: 'heading-six'
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Tags to marks.
|
|
39
|
+
*
|
|
40
|
+
* @type {Object}
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
const MARK_TAGS = {
|
|
44
|
+
b: 'bold',
|
|
45
|
+
em: 'italic',
|
|
46
|
+
u: 'underline',
|
|
47
|
+
s: 'strikethrough',
|
|
48
|
+
code: 'code',
|
|
49
|
+
strong: 'bold'
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const parseStyleString = s => {
|
|
53
|
+
const regex = /([\w-]*)\s*:\s*([^;]*)/g;
|
|
54
|
+
let match;
|
|
55
|
+
const result = {};
|
|
56
|
+
while ((match = regex.exec(s))) {
|
|
57
|
+
result[match[1]] = match[2].trim();
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const getBase64 = file => {
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
const reader = new FileReader();
|
|
65
|
+
reader.readAsDataURL(file);
|
|
66
|
+
reader.onload = () => resolve(reader.result);
|
|
67
|
+
reader.onerror = error => reject(error);
|
|
68
|
+
});
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export const reactAttributes = o => toStyleObject(o, { camelize: true, addUnits: false });
|
|
72
|
+
|
|
73
|
+
const attributesToMap = el => (acc, attribute) => {
|
|
74
|
+
const value = el.getAttribute(attribute);
|
|
75
|
+
if (value) {
|
|
76
|
+
if (attribute === 'style') {
|
|
77
|
+
const styleString = el.getAttribute(attribute);
|
|
78
|
+
const reactStyleObject = reactAttributes(parseStyleString(styleString));
|
|
79
|
+
acc['style'] = reactStyleObject;
|
|
80
|
+
} else {
|
|
81
|
+
acc[attribute] = el.getAttribute(attribute);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return acc;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const attributes = ['border', 'cellpadding', 'cellspacing', 'class', 'style'];
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Serializer rules.
|
|
91
|
+
*
|
|
92
|
+
* @type {Array}
|
|
93
|
+
*/
|
|
94
|
+
|
|
95
|
+
const blocks = {
|
|
96
|
+
deserialize(el, next) {
|
|
97
|
+
log('[blocks:deserialize] block: ', el);
|
|
98
|
+
const block = BLOCK_TAGS[el.tagName.toLowerCase()];
|
|
99
|
+
if (!block) return;
|
|
100
|
+
log('[blocks:deserialize] block: ', block);
|
|
101
|
+
|
|
102
|
+
if (el.childNodes.length === 1) {
|
|
103
|
+
const cn = el.childNodes[0];
|
|
104
|
+
if (cn && cn.tagName && cn.tagName.toLowerCase() === block) {
|
|
105
|
+
log('[we have a child node of the same]...');
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return jsx(
|
|
111
|
+
'element',
|
|
112
|
+
{
|
|
113
|
+
type: block,
|
|
114
|
+
/**
|
|
115
|
+
* Here for rendering styles for all block elements
|
|
116
|
+
*/
|
|
117
|
+
data: { attributes: attributes.reduce(attributesToMap(el), {}) }
|
|
118
|
+
},
|
|
119
|
+
next(el.childNodes)
|
|
120
|
+
);
|
|
121
|
+
},
|
|
122
|
+
serialize: (object, children) => {
|
|
123
|
+
if (object.object !== 'block') return;
|
|
124
|
+
|
|
125
|
+
const jsonData = object.data.toJSON();
|
|
126
|
+
|
|
127
|
+
log('[blocks:serialize] object: ', object, children);
|
|
128
|
+
let key;
|
|
129
|
+
|
|
130
|
+
for (key in BLOCK_TAGS) {
|
|
131
|
+
if (BLOCK_TAGS[key] === object.type) {
|
|
132
|
+
const Tag = key;
|
|
133
|
+
|
|
134
|
+
return <Tag {...jsonData.attributes}>{children}</Tag>;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const marks = {
|
|
141
|
+
deserialize(el, next) {
|
|
142
|
+
const mark = MARK_TAGS[el.tagName.toLowerCase()];
|
|
143
|
+
if (!mark) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
log('[deserialize] mark: ', mark);
|
|
147
|
+
|
|
148
|
+
return jsx('element', { type: mark }, next(el.childNodes));
|
|
149
|
+
},
|
|
150
|
+
serialize(object, children) {
|
|
151
|
+
/*if (Mark.isMark(object)) {
|
|
152
|
+
for (var key in MARK_TAGS) {
|
|
153
|
+
if (MARK_TAGS[key] === object.type) {
|
|
154
|
+
const Tag = key;
|
|
155
|
+
return <Tag>{children}</Tag>;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}*/
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const findPreviousText = el => {
|
|
163
|
+
if (el.nodeName === '#text') {
|
|
164
|
+
return el;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (el.previousSibling) {
|
|
168
|
+
return findPreviousText(el.previousSibling);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return null;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
export const TEXT_RULE = {
|
|
175
|
+
deserialize(el) {
|
|
176
|
+
/**
|
|
177
|
+
* This needs to be called on the dom element in order to merge the adjacent text nodes together
|
|
178
|
+
* */
|
|
179
|
+
el.normalize();
|
|
180
|
+
|
|
181
|
+
if (el.tagName && el.tagName.toLowerCase() === 'br') {
|
|
182
|
+
return jsx('text', {});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (el.nodeName === '#text') {
|
|
186
|
+
if (el.nodeValue && el.nodeValue.match(/<!--.*?-->/)) return;
|
|
187
|
+
|
|
188
|
+
log('[text:deserialize] return text object..');
|
|
189
|
+
return jsx('text', {}, el.nodeValue);
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
|
|
193
|
+
serialize(obj, children) {
|
|
194
|
+
if (obj.object === 'string') {
|
|
195
|
+
return children.split('\n').reduce((array, text, i) => {
|
|
196
|
+
if (i !== 0) array.push(<br />);
|
|
197
|
+
array.push(text);
|
|
198
|
+
return array;
|
|
199
|
+
}, []);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const RULES = [
|
|
205
|
+
listSerialization,
|
|
206
|
+
mathSerialization,
|
|
207
|
+
mediaSerialization,
|
|
208
|
+
imgSerialization,
|
|
209
|
+
tableSerialization,
|
|
210
|
+
responseAreaSerialization,
|
|
211
|
+
TEXT_RULE,
|
|
212
|
+
blocks,
|
|
213
|
+
marks
|
|
214
|
+
];
|
|
215
|
+
|
|
216
|
+
function allWhitespace(node) {
|
|
217
|
+
// Use ECMA-262 Edition 3 String and RegExp features
|
|
218
|
+
return !/[^\t\n\r ]/.test(node.textContent);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function defaultParseHtml(html) {
|
|
222
|
+
if (typeof DOMParser === 'undefined') {
|
|
223
|
+
throw new Error(
|
|
224
|
+
'The native `DOMParser` global which the `Html` serializer uses by default is not present in this environment. You must supply the `options.parseHtml` function instead.'
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const parsed = new DOMParser().parseFromString(html, 'text/html');
|
|
229
|
+
|
|
230
|
+
const { body } = parsed;
|
|
231
|
+
const textNodes = document.createTreeWalker(body, NodeFilter.SHOW_TEXT, null, null);
|
|
232
|
+
let n = textNodes.nextNode();
|
|
233
|
+
|
|
234
|
+
while (n) {
|
|
235
|
+
if (allWhitespace(n) || n.nodeValue === '\u200B') {
|
|
236
|
+
n.parentNode.removeChild(n);
|
|
237
|
+
}
|
|
238
|
+
n = textNodes.nextNode();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return body;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/** If this lib is used on the server side, we need to bypass using the DOMParser - just put in a stub. */
|
|
245
|
+
const parseHtml =
|
|
246
|
+
typeof window === 'undefined'
|
|
247
|
+
? () => ({
|
|
248
|
+
childNodes: []
|
|
249
|
+
})
|
|
250
|
+
: defaultParseHtml;
|
|
251
|
+
|
|
252
|
+
const serializer = new TestSerializer({
|
|
253
|
+
defaultBlock: 'div',
|
|
254
|
+
rules: RULES,
|
|
255
|
+
parseHtml
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
const _extends =
|
|
259
|
+
Object.assign ||
|
|
260
|
+
function(target) {
|
|
261
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
262
|
+
var source = arguments[i];
|
|
263
|
+
|
|
264
|
+
for (var key in source) {
|
|
265
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
266
|
+
target[key] = source[key];
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return target;
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
export const htmlToValue = html => {
|
|
275
|
+
try {
|
|
276
|
+
return serializer.deserialize(html);
|
|
277
|
+
} catch (e) {
|
|
278
|
+
console.log("Couldn't parse html: ", e);
|
|
279
|
+
return {};
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
export const valueToHtml = value => serializer.serialize(value);
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
*
|
|
287
|
+
* <div><div>a</div></div> -> <div>a</div>
|
|
288
|
+
*
|
|
289
|
+
* <div><div>a</div><div>b</div></div> -> <div>a</div><div>b</div>
|
|
290
|
+
* <div><div>a</div>4444<div>b</div></div> -> <div>a</div>4444<div>b</div>
|
|
291
|
+
*/
|
package/src/parse-html.js
CHANGED
|
@@ -8,17 +8,17 @@ const styles = () => ({
|
|
|
8
8
|
background: '#fff',
|
|
9
9
|
padding: '10px',
|
|
10
10
|
pointerEvents: 'none',
|
|
11
|
-
zIndex: 99999
|
|
11
|
+
zIndex: 99999
|
|
12
12
|
},
|
|
13
13
|
paper: {
|
|
14
14
|
padding: 20,
|
|
15
15
|
height: 'auto',
|
|
16
|
-
width: 'auto'
|
|
16
|
+
width: 'auto'
|
|
17
17
|
},
|
|
18
18
|
typography: {
|
|
19
19
|
fontSize: 50,
|
|
20
|
-
textAlign: 'center'
|
|
21
|
-
}
|
|
20
|
+
textAlign: 'center'
|
|
21
|
+
}
|
|
22
22
|
});
|
|
23
23
|
|
|
24
24
|
const CustomPopper = withStyles(styles)(({ classes, children, ...props }) => (
|
|
@@ -27,15 +27,15 @@ const CustomPopper = withStyles(styles)(({ classes, children, ...props }) => (
|
|
|
27
27
|
open
|
|
28
28
|
className={classes.popover}
|
|
29
29
|
classes={{
|
|
30
|
-
paper: classes.paper
|
|
30
|
+
paper: classes.paper
|
|
31
31
|
}}
|
|
32
32
|
anchorOrigin={{
|
|
33
33
|
vertical: 'bottom',
|
|
34
|
-
horizontal: 'left'
|
|
34
|
+
horizontal: 'left'
|
|
35
35
|
}}
|
|
36
36
|
transformOrigin={{
|
|
37
37
|
vertical: 'top',
|
|
38
|
-
horizontal: 'left'
|
|
38
|
+
horizontal: 'left'
|
|
39
39
|
}}
|
|
40
40
|
disableRestoreFocus
|
|
41
41
|
disableAutoFocus
|