@pie-lib/editable-html 10.0.0-beta.7 → 10.0.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/CHANGELOG.json +1 -1
- package/CHANGELOG.md +81 -0
- package/LICENSE.md +5 -0
- package/lib/editor.js +410 -543
- package/lib/editor.js.map +1 -1
- package/lib/index.js +200 -101
- package/lib/index.js.map +1 -1
- package/lib/parse-html.js +5 -6
- package/lib/parse-html.js.map +1 -1
- package/lib/plugins/characters/custom-popper.js +12 -2
- package/lib/plugins/characters/custom-popper.js.map +1 -1
- package/lib/plugins/characters/index.js +71 -19
- package/lib/plugins/characters/index.js.map +1 -1
- package/lib/plugins/characters/utils.js.map +1 -1
- package/lib/plugins/html/icons/index.js +38 -0
- package/lib/plugins/html/icons/index.js.map +1 -0
- package/lib/plugins/html/index.js +75 -0
- package/lib/plugins/html/index.js.map +1 -0
- package/lib/plugins/image/alt-dialog.js +26 -0
- package/lib/plugins/image/alt-dialog.js.map +1 -1
- package/lib/plugins/image/component.js +124 -90
- package/lib/plugins/image/component.js.map +1 -1
- package/lib/plugins/image/image-toolbar.js +45 -7
- package/lib/plugins/image/image-toolbar.js.map +1 -1
- package/lib/plugins/image/index.js +91 -113
- package/lib/plugins/image/index.js.map +1 -1
- package/lib/plugins/image/insert-image-handler.js +54 -72
- package/lib/plugins/image/insert-image-handler.js.map +1 -1
- package/lib/plugins/index.js +71 -31
- package/lib/plugins/index.js.map +1 -1
- package/lib/plugins/list/index.js +129 -58
- package/lib/plugins/list/index.js.map +1 -1
- package/lib/plugins/math/index.js +152 -118
- package/lib/plugins/math/index.js.map +1 -1
- package/lib/plugins/media/index.js +185 -168
- package/lib/plugins/media/index.js.map +1 -1
- package/lib/plugins/media/media-dialog.js +197 -110
- package/lib/plugins/media/media-dialog.js.map +1 -1
- package/lib/plugins/media/media-toolbar.js +24 -4
- package/lib/plugins/media/media-toolbar.js.map +1 -1
- package/lib/plugins/media/media-wrapper.js +65 -23
- package/lib/plugins/media/media-wrapper.js.map +1 -1
- package/lib/plugins/respArea/drag-in-the-blank/choice.js +50 -10
- package/lib/plugins/respArea/drag-in-the-blank/choice.js.map +1 -1
- package/lib/plugins/respArea/drag-in-the-blank/index.js +22 -9
- package/lib/plugins/respArea/drag-in-the-blank/index.js.map +1 -1
- package/lib/plugins/respArea/explicit-constructed-response/index.js +9 -4
- package/lib/plugins/respArea/explicit-constructed-response/index.js.map +1 -1
- package/lib/plugins/respArea/icons/index.js +18 -1
- package/lib/plugins/respArea/icons/index.js.map +1 -1
- package/lib/plugins/respArea/index.js +133 -122
- package/lib/plugins/respArea/index.js.map +1 -1
- package/lib/plugins/respArea/inline-dropdown/index.js +10 -4
- package/lib/plugins/respArea/inline-dropdown/index.js.map +1 -1
- package/lib/plugins/respArea/utils.js +33 -15
- package/lib/plugins/respArea/utils.js.map +1 -1
- package/lib/plugins/table/icons/index.js +7 -0
- package/lib/plugins/table/icons/index.js.map +1 -1
- package/lib/plugins/table/index.js +279 -390
- package/lib/plugins/table/index.js.map +1 -1
- package/lib/plugins/table/table-toolbar.js +47 -14
- package/lib/plugins/table/table-toolbar.js.map +1 -1
- package/lib/plugins/toolbar/default-toolbar.js +63 -51
- package/lib/plugins/toolbar/default-toolbar.js.map +1 -1
- package/lib/plugins/toolbar/done-button.js +9 -1
- package/lib/plugins/toolbar/done-button.js.map +1 -1
- package/lib/plugins/toolbar/editor-and-toolbar.js +140 -83
- package/lib/plugins/toolbar/editor-and-toolbar.js.map +1 -1
- package/lib/plugins/toolbar/index.js +5 -0
- package/lib/plugins/toolbar/index.js.map +1 -1
- package/lib/plugins/toolbar/toolbar-buttons.js +39 -8
- package/lib/plugins/toolbar/toolbar-buttons.js.map +1 -1
- package/lib/plugins/toolbar/toolbar.js +261 -225
- package/lib/plugins/toolbar/toolbar.js.map +1 -1
- package/lib/plugins/utils.js +16 -19
- package/lib/plugins/utils.js.map +1 -1
- package/lib/serialization.js +70 -11
- package/lib/serialization.js.map +1 -1
- package/lib/theme.js.map +1 -1
- package/package.json +18 -17
- package/src/editor.jsx +139 -434
- package/src/index.jsx +96 -62
- package/src/plugins/characters/index.jsx +17 -12
- package/src/plugins/html/icons/index.jsx +19 -0
- package/src/plugins/html/index.jsx +68 -0
- package/src/plugins/image/component.jsx +38 -60
- package/src/plugins/image/index.jsx +42 -95
- package/src/plugins/image/insert-image-handler.js +27 -62
- package/src/plugins/index.jsx +39 -21
- package/src/plugins/list/index.jsx +90 -62
- package/src/plugins/math/index.jsx +70 -93
- package/src/plugins/media/index.jsx +117 -146
- package/src/plugins/media/media-dialog.js +9 -10
- package/src/plugins/media/media-wrapper.jsx +27 -29
- package/src/plugins/respArea/drag-in-the-blank/index.jsx +4 -5
- package/src/plugins/respArea/explicit-constructed-response/index.jsx +1 -2
- package/src/plugins/respArea/index.jsx +84 -114
- package/src/plugins/respArea/inline-dropdown/index.jsx +2 -3
- package/src/plugins/respArea/utils.jsx +28 -23
- package/src/plugins/table/index.jsx +214 -334
- package/src/plugins/table/table-toolbar.jsx +4 -3
- package/src/plugins/toolbar/default-toolbar.jsx +30 -48
- package/src/plugins/toolbar/editor-and-toolbar.jsx +114 -114
- package/src/plugins/toolbar/toolbar.jsx +224 -254
- package/src/plugins/utils.js +0 -16
- package/src/serialization.jsx +1 -1
- package/lib/components.js +0 -92
- package/lib/components.js.map +0 -1
- package/lib/new-serialization.js +0 -280
- package/lib/new-serialization.js.map +0 -1
- package/lib/plugins/hotKeys/index.js +0 -60
- package/lib/plugins/hotKeys/index.js.map +0 -1
- package/lib/test-serializer.js +0 -138
- package/lib/test-serializer.js.map +0 -1
- package/src/components.js +0 -135
- package/src/new-serialization.jsx +0 -310
- package/src/plugins/hotKeys/index.js +0 -54
- package/src/test-serializer.js +0 -132
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"test-serializer.js","names":["_react","_interopRequireDefault","require","_server","_escapeHtml","_slate","_slateHyperscript","_newSerialization","ownKeys","object","enumerableOnly","keys","Object","getOwnPropertySymbols","symbols","filter","sym","getOwnPropertyDescriptor","enumerable","push","apply","_objectSpread","target","i","arguments","length","source","forEach","key","_defineProperty2","getOwnPropertyDescriptors","defineProperties","defineProperty","Html","_createClass2","props","_this","_classCallCheck2","node","children","map","n","serializeEls","correctRule","rules","reduce","res","rule","serialize","type","createElement","href","escapeHtml","url","deserialized","html","ReactServer","renderToStaticMarkup","React","inner","slice","body","parseHtml","firstChild","nodeType","Node","TEXT_NODE","MARK_TAGS","includes","tagName","toLowerCase","concat","deserializeEls","element","markAttributes","undefined","jsx","textContent","ELEMENT_NODE","nodeAttributes","nodeName","bold","nextFn","nodes","childNodes","Array","from","flat","deserialize","getAttribute","defaultBlock","_default","exports"],"sources":["../src/test-serializer.js"],"sourcesContent":["import React from 'react';\nimport ReactServer from 'react-dom/server';\nimport escapeHtml from 'escape-html';\nimport { Text } from 'slate';\nimport { jsx } from 'slate-hyperscript';\nimport { MARK_TAGS } from './new-serialization';\n\nclass Html {\n constructor(props) {\n this.defaultBlock = props.defaultBlock;\n this.parseHtml = props.parseHtml;\n this.rules = props.rules;\n }\n\n serializeEls = node => {\n let children = (node.children || []).map(n => this.serializeEls(n));\n\n const correctRule = this.rules.reduce((res, rule) => {\n return res || rule.serialize(node, children);\n }, null);\n\n if (correctRule) {\n return correctRule;\n }\n\n switch (node.type) {\n case 'quote':\n return (\n <blockquote>\n <p>{children}</p>\n </blockquote>\n );\n case 'paragraph':\n return <p>{children}</p>;\n case 'link':\n return <a href={escapeHtml(node.url)}>{children}</a>;\n default:\n return children;\n }\n };\n\n serialize = node => {\n const deserialized = this.serializeEls(node);\n const html = ReactServer.renderToStaticMarkup(React.createElement(\n 'body',\n null,\n deserialized\n ));\n const inner = html.slice(6, -7);\n return inner;\n };\n\n deserialize = html => {\n let body = this.parseHtml(html);\n\n if (!body.firstChild || (body.firstChild.nodeType === Node.TEXT_NODE || Object.keys(MARK_TAGS).includes(body.firstChild.tagName.toLowerCase()))) {\n body = this.parseHtml(`<span>${html}</span>`);\n }\n\n return this.deserializeEls(body);\n };\n\n deserializeEls = (element, markAttributes = {}) => {\n if (element.nodeType === Node.TEXT_NODE) {\n return jsx('text', markAttributes, element.textContent);\n } else if (element.nodeType !== Node.ELEMENT_NODE) {\n return null;\n }\n\n const nodeAttributes = { ...markAttributes };\n\n // define attributes for text nodes\n if (element.nodeName === 'STRONG') {\n nodeAttributes.bold = true;\n }\n\n const nextFn = nodes => {\n const childNodes = Array.from(nodes);\n const children = Array.from(childNodes)\n .map(node => this.deserializeEls(node, nodeAttributes))\n .flat();\n\n if (children.length === 0) {\n children.push(jsx('text', nodeAttributes, ''));\n }\n\n return children;\n };\n\n const correctRule = this.rules.reduce((res, rule) => {\n return res || rule.deserialize(element, nextFn);\n }, null);\n\n if (correctRule) {\n return correctRule;\n }\n\n const childNodes = Array.from(element.childNodes);\n const children = Array.from(childNodes)\n .map(node => this.deserializeEls(node, nodeAttributes))\n .flat();\n\n if (children.length === 0) {\n children.push(jsx('text', nodeAttributes, ''));\n }\n\n switch (element.nodeName) {\n case 'TABLE':\n return jsx('element', { type: 'table' }, children);\n case 'TBODY':\n return jsx('element', { type: 'tbody' }, children);\n case 'TR':\n return jsx('element', { type: 'tr' }, children);\n case 'TD':\n return jsx('element', { type: 'td' }, children);\n case 'BODY':\n return jsx('fragment', {}, children);\n case 'BR':\n return '\\n';\n case 'BLOCKQUOTE':\n return jsx('element', { type: 'quote' }, children);\n case 'P':\n return jsx('element', { type: 'paragraph' }, children);\n case 'A':\n return jsx('element', { type: 'link', url: element.getAttribute('href') }, children);\n default:\n return children;\n }\n };\n}\n\nexport default Html;\n"],"mappings":";;;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AACA,IAAAC,OAAA,GAAAF,sBAAA,CAAAC,OAAA;AACA,IAAAE,WAAA,GAAAH,sBAAA,CAAAC,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AACA,IAAAI,iBAAA,GAAAJ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BAAG,EAAC,SAAS,EAAE;UAAE3B,IAAI,EAAE;QAAK,CAAC,EAAEV,QAAQ,CAAC;MACjD,KAAK,IAAI;QACP,OAAO,IAAAqC,qBAAG,EAAC,SAAS,EAAE;UAAE3B,IAAI,EAAE;QAAK,CAAC,EAAEV,QAAQ,CAAC;MACjD,KAAK,MAAM;QACT,OAAO,IAAAqC,qBAAG,EAAC,UAAU,EAAE,CAAC,CAAC,EAAErC,QAAQ,CAAC;MACtC,KAAK,IAAI;QACP,OAAO,IAAI;MACb,KAAK,YAAY;QACf,OAAO,IAAAqC,qBAAG,EAAC,SAAS,EAAE;UAAE3B,IAAI,EAAE;QAAQ,CAAC,EAAEV,QAAQ,CAAC;MACpD,KAAK,GAAG;QACN,OAAO,IAAAqC,qBAAG,EAAC,SAAS,EAAE;UAAE3B,IAAI,EAAE;QAAY,CAAC,EAAEV,QAAQ,CAAC;MACxD,KAAK,GAAG;QACN,OAAO,IAAAqC,qBAAG,EAAC,SAAS,EAAE;UAAE3B,IAAI,EAAE,MAAM;UAAEI,GAAG,EAAEoB,OAAO,CAACgB,YAAY,CAAC,MAAM;QAAE,CAAC,EAAElD,QAAQ,CAAC;MACtF;QACE,OAAOA,QAAQ;IACnB;EACF,CAAC;EAvHC,IAAI,CAACmD,YAAY,GAAGvD,KAAK,CAACuD,YAAY;EACtC,IAAI,CAAC5B,SAAS,GAAG3B,KAAK,CAAC2B,SAAS;EAChC,IAAI,CAAClB,KAAK,GAAGT,KAAK,CAACS,KAAK;AAC1B,CAAC;AAAA,IAAA+C,QAAA,GAuHY1D,IAAI;AAAA2D,OAAA,cAAAD,QAAA"}
|
package/src/components.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import ReactDOM from 'react-dom';
|
|
3
|
-
import { cx, css } from '@emotion/css';
|
|
4
|
-
|
|
5
|
-
export const Button = React.forwardRef(({ className, active, reversed, ...props }, ref) => (
|
|
6
|
-
<span
|
|
7
|
-
{...props}
|
|
8
|
-
ref={ref}
|
|
9
|
-
className={cx(
|
|
10
|
-
className,
|
|
11
|
-
css`
|
|
12
|
-
cursor: pointer;
|
|
13
|
-
|
|
14
|
-
color: ${reversed ? (active ? 'white' : '#aaa') : active ? 'black' : '#ccc'};
|
|
15
|
-
`,
|
|
16
|
-
)}
|
|
17
|
-
/>
|
|
18
|
-
));
|
|
19
|
-
|
|
20
|
-
export const EditorValue = React.forwardRef(({ className, value, ...props }, ref) => {
|
|
21
|
-
const textLines = value.document.nodes
|
|
22
|
-
.map((node) => node.text)
|
|
23
|
-
.toArray()
|
|
24
|
-
.join('\n');
|
|
25
|
-
return (
|
|
26
|
-
<div
|
|
27
|
-
ref={ref}
|
|
28
|
-
{...props}
|
|
29
|
-
className={cx(
|
|
30
|
-
className,
|
|
31
|
-
css`
|
|
32
|
-
margin: 30px -20px 0;
|
|
33
|
-
`,
|
|
34
|
-
)}
|
|
35
|
-
>
|
|
36
|
-
<div
|
|
37
|
-
className={css`
|
|
38
|
-
font-size: 14px;
|
|
39
|
-
padding: 5px 20px;
|
|
40
|
-
color: #404040;
|
|
41
|
-
border-top: 2px solid #eeeeee;
|
|
42
|
-
background: #f8f8f8;
|
|
43
|
-
`}
|
|
44
|
-
>
|
|
45
|
-
Slates value as text
|
|
46
|
-
</div>
|
|
47
|
-
<div
|
|
48
|
-
className={css`
|
|
49
|
-
color: #404040;
|
|
50
|
-
font: 12px monospace;
|
|
51
|
-
white-space: pre-wrap;
|
|
52
|
-
padding: 10px 20px;
|
|
53
|
-
|
|
54
|
-
div {
|
|
55
|
-
margin: 0 0 0.5em;
|
|
56
|
-
}
|
|
57
|
-
`}
|
|
58
|
-
>
|
|
59
|
-
{textLines}
|
|
60
|
-
</div>
|
|
61
|
-
</div>
|
|
62
|
-
);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
export const Icon = React.forwardRef(({ className, ...props }, ref) => (
|
|
66
|
-
<span
|
|
67
|
-
{...props}
|
|
68
|
-
ref={ref}
|
|
69
|
-
className={cx(
|
|
70
|
-
'material-icons',
|
|
71
|
-
className,
|
|
72
|
-
css`
|
|
73
|
-
font-size: 18px;
|
|
74
|
-
vertical-align: text-bottom;
|
|
75
|
-
`,
|
|
76
|
-
)}
|
|
77
|
-
/>
|
|
78
|
-
));
|
|
79
|
-
|
|
80
|
-
export const Instruction = React.forwardRef(({ className, ...props }, ref) => (
|
|
81
|
-
<div
|
|
82
|
-
{...props}
|
|
83
|
-
ref={ref}
|
|
84
|
-
className={cx(
|
|
85
|
-
className,
|
|
86
|
-
css`
|
|
87
|
-
white-space: pre-wrap;
|
|
88
|
-
margin: 0 -20px 10px;
|
|
89
|
-
padding: 10px 20px;
|
|
90
|
-
font-size: 14px;
|
|
91
|
-
background: #f8f8e8;
|
|
92
|
-
`,
|
|
93
|
-
)}
|
|
94
|
-
/>
|
|
95
|
-
));
|
|
96
|
-
|
|
97
|
-
export const Menu = React.forwardRef(({ className, ...props }, ref) => (
|
|
98
|
-
<div
|
|
99
|
-
{...props}
|
|
100
|
-
ref={ref}
|
|
101
|
-
className={cx(
|
|
102
|
-
className,
|
|
103
|
-
css`
|
|
104
|
-
& > * {
|
|
105
|
-
display: inline-block;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
& > * + * {
|
|
109
|
-
margin-left: 15px;
|
|
110
|
-
}
|
|
111
|
-
`,
|
|
112
|
-
)}
|
|
113
|
-
/>
|
|
114
|
-
));
|
|
115
|
-
|
|
116
|
-
export const Portal = ({ children }) => {
|
|
117
|
-
return typeof document === 'object' ? ReactDOM.createPortal(children, document.body) : null;
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
export const Toolbar = React.forwardRef(({ className, ...props }, ref) => (
|
|
121
|
-
<Menu
|
|
122
|
-
{...props}
|
|
123
|
-
ref={ref}
|
|
124
|
-
className={cx(
|
|
125
|
-
className,
|
|
126
|
-
css`
|
|
127
|
-
position: relative;
|
|
128
|
-
padding: 1px 18px 17px;
|
|
129
|
-
margin: 0 -20px;
|
|
130
|
-
border-bottom: 2px solid #eee;
|
|
131
|
-
margin-bottom: 20px;
|
|
132
|
-
`,
|
|
133
|
-
)}
|
|
134
|
-
/>
|
|
135
|
-
));
|
|
@@ -1,310 +0,0 @@
|
|
|
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, Text, Value } from 'slate';
|
|
13
|
-
import { jsx } from 'slate-hyperscript';
|
|
14
|
-
import escapeHtml from 'escape-html';
|
|
15
|
-
|
|
16
|
-
const log = debug('@pie-lib:editable-html:serialization');
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Tags to blocks.
|
|
20
|
-
*
|
|
21
|
-
* @type {Object}
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
export const BLOCK_TAGS = {
|
|
25
|
-
div: 'div',
|
|
26
|
-
span: 'span',
|
|
27
|
-
p: 'paragraph',
|
|
28
|
-
blockquote: 'quote',
|
|
29
|
-
pre: 'code',
|
|
30
|
-
h1: 'heading-one',
|
|
31
|
-
h2: 'heading-two',
|
|
32
|
-
h3: 'heading-three',
|
|
33
|
-
h4: 'heading-four',
|
|
34
|
-
h5: 'heading-five',
|
|
35
|
-
h6: 'heading-six',
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Tags to marks.
|
|
40
|
-
*
|
|
41
|
-
* @type {Object}
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
export const MARK_TAGS = {
|
|
45
|
-
b: 'bold',
|
|
46
|
-
em: 'italic',
|
|
47
|
-
u: 'underline',
|
|
48
|
-
s: 'strikethrough',
|
|
49
|
-
del: 'strikethrough',
|
|
50
|
-
code: 'code',
|
|
51
|
-
strong: 'bold',
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
export const parseStyleString = (s) => {
|
|
55
|
-
const regex = /([\w-]*)\s*:\s*([^;]*)/g;
|
|
56
|
-
let match;
|
|
57
|
-
const result = {};
|
|
58
|
-
while ((match = regex.exec(s))) {
|
|
59
|
-
result[match[1]] = match[2].trim();
|
|
60
|
-
}
|
|
61
|
-
return result;
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export const getBase64 = (file) => {
|
|
65
|
-
return new Promise((resolve, reject) => {
|
|
66
|
-
const reader = new FileReader();
|
|
67
|
-
reader.readAsDataURL(file);
|
|
68
|
-
reader.onload = () => resolve(reader.result);
|
|
69
|
-
reader.onerror = (error) => reject(error);
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
export const reactAttributes = (o) => toStyleObject(o, { camelize: true, addUnits: false });
|
|
74
|
-
|
|
75
|
-
const attributesToMap = (el) => (acc, attribute) => {
|
|
76
|
-
const value = el.getAttribute(attribute);
|
|
77
|
-
if (value) {
|
|
78
|
-
if (attribute === 'style') {
|
|
79
|
-
const styleString = el.getAttribute(attribute);
|
|
80
|
-
const reactStyleObject = reactAttributes(parseStyleString(styleString));
|
|
81
|
-
acc['style'] = reactStyleObject;
|
|
82
|
-
} else {
|
|
83
|
-
acc[attribute] = el.getAttribute(attribute);
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return acc;
|
|
87
|
-
};
|
|
88
|
-
|
|
89
|
-
const attributes = ['border', 'cellpadding', 'cellspacing', 'class', 'style'];
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Serializer rules.
|
|
93
|
-
*
|
|
94
|
-
* @type {Array}
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
const blocks = {
|
|
98
|
-
deserialize(el, next) {
|
|
99
|
-
log('[blocks:deserialize] block: ', el);
|
|
100
|
-
const block = BLOCK_TAGS[el.tagName.toLowerCase()];
|
|
101
|
-
if (!block) return;
|
|
102
|
-
log('[blocks:deserialize] block: ', block);
|
|
103
|
-
|
|
104
|
-
if (el.childNodes.length === 1) {
|
|
105
|
-
const cn = el.childNodes[0];
|
|
106
|
-
if (cn && cn.tagName && cn.tagName.toLowerCase() === block) {
|
|
107
|
-
log('[we have a child node of the same]...');
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
return jsx(
|
|
113
|
-
'element',
|
|
114
|
-
{
|
|
115
|
-
type: block,
|
|
116
|
-
/**
|
|
117
|
-
* Here for rendering styles for all block elements
|
|
118
|
-
*/
|
|
119
|
-
data: { attributes: attributes.reduce(attributesToMap(el), {}) },
|
|
120
|
-
},
|
|
121
|
-
next(el.childNodes),
|
|
122
|
-
);
|
|
123
|
-
},
|
|
124
|
-
serialize: (object, children) => {
|
|
125
|
-
if (object.object !== 'block') return;
|
|
126
|
-
|
|
127
|
-
const jsonData = object.data;
|
|
128
|
-
|
|
129
|
-
log('[blocks:serialize] object: ', object, children);
|
|
130
|
-
let key;
|
|
131
|
-
|
|
132
|
-
for (key in BLOCK_TAGS) {
|
|
133
|
-
if (BLOCK_TAGS[key] === object.type) {
|
|
134
|
-
const Tag = key;
|
|
135
|
-
|
|
136
|
-
return <Tag {...jsonData.attributes}>{children}</Tag>;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
};
|
|
141
|
-
|
|
142
|
-
const marks = {
|
|
143
|
-
deserialize(el, next) {
|
|
144
|
-
const mark = MARK_TAGS[el.tagName.toLowerCase()];
|
|
145
|
-
if (!mark) {
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
log('[deserialize] mark: ', mark);
|
|
149
|
-
|
|
150
|
-
return jsx('text', { [mark]: true }, next(el.childNodes));
|
|
151
|
-
},
|
|
152
|
-
serialize(object) {
|
|
153
|
-
if (Text.isText(object)) {
|
|
154
|
-
let string = escapeHtml(object.text);
|
|
155
|
-
|
|
156
|
-
if (object.bold) {
|
|
157
|
-
string = <strong>{string}</strong>;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
if (object.code) {
|
|
161
|
-
string = <code>{string}</code>;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
if (object.italic) {
|
|
165
|
-
string = <em>{string}</em>;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (object.underline) {
|
|
169
|
-
string = <u>{string}</u>;
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
if (object.strikethrough) {
|
|
173
|
-
string = <del>{string}</del>;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
return string;
|
|
177
|
-
}
|
|
178
|
-
},
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
const findPreviousText = (el) => {
|
|
182
|
-
if (el.nodeName === '#text') {
|
|
183
|
-
return el;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
if (el.previousSibling) {
|
|
187
|
-
return findPreviousText(el.previousSibling);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
return null;
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
export const TEXT_RULE = {
|
|
194
|
-
deserialize(el) {
|
|
195
|
-
/**
|
|
196
|
-
* This needs to be called on the dom element in order to merge the adjacent text nodes together
|
|
197
|
-
* */
|
|
198
|
-
el.normalize();
|
|
199
|
-
|
|
200
|
-
if (el.tagName && el.tagName.toLowerCase() === 'br') {
|
|
201
|
-
return jsx('text', {});
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (el.nodeName === '#text') {
|
|
205
|
-
if (el.nodeValue && el.nodeValue.match(/<!--.*?-->/)) return;
|
|
206
|
-
|
|
207
|
-
log('[text:deserialize] return text object..');
|
|
208
|
-
return jsx('text', {}, el.nodeValue);
|
|
209
|
-
}
|
|
210
|
-
},
|
|
211
|
-
|
|
212
|
-
serialize(obj, children) {
|
|
213
|
-
if (obj.object === 'string') {
|
|
214
|
-
return children.split('\n').reduce((array, text, i) => {
|
|
215
|
-
if (i !== 0) array.push(<br />);
|
|
216
|
-
array.push(text);
|
|
217
|
-
return array;
|
|
218
|
-
}, []);
|
|
219
|
-
}
|
|
220
|
-
},
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
const RULES = [
|
|
224
|
-
listSerialization,
|
|
225
|
-
mathSerialization,
|
|
226
|
-
mediaSerialization,
|
|
227
|
-
imgSerialization,
|
|
228
|
-
tableSerialization,
|
|
229
|
-
responseAreaSerialization,
|
|
230
|
-
TEXT_RULE,
|
|
231
|
-
blocks,
|
|
232
|
-
marks,
|
|
233
|
-
];
|
|
234
|
-
|
|
235
|
-
function allWhitespace(node) {
|
|
236
|
-
// Use ECMA-262 Edition 3 String and RegExp features
|
|
237
|
-
return !/[^\t\n\r ]/.test(node.textContent);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
function defaultParseHtml(html) {
|
|
241
|
-
if (typeof DOMParser === 'undefined') {
|
|
242
|
-
throw new Error(
|
|
243
|
-
'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.',
|
|
244
|
-
);
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
const parsed = new DOMParser().parseFromString(html, 'text/html');
|
|
248
|
-
|
|
249
|
-
const { body } = parsed;
|
|
250
|
-
const textNodes = document.createTreeWalker(body, NodeFilter.SHOW_TEXT, null, null);
|
|
251
|
-
let n = textNodes.nextNode();
|
|
252
|
-
|
|
253
|
-
while (n) {
|
|
254
|
-
if (allWhitespace(n) || n.nodeValue === '\u200B') {
|
|
255
|
-
n.parentNode.removeChild(n);
|
|
256
|
-
}
|
|
257
|
-
n = textNodes.nextNode();
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
return body;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/** If this lib is used on the server side, we need to bypass using the DOMParser - just put in a stub. */
|
|
264
|
-
const parseHtml =
|
|
265
|
-
typeof window === 'undefined'
|
|
266
|
-
? () => ({
|
|
267
|
-
childNodes: [],
|
|
268
|
-
})
|
|
269
|
-
: defaultParseHtml;
|
|
270
|
-
|
|
271
|
-
const serializer = new TestSerializer({
|
|
272
|
-
defaultBlock: 'div',
|
|
273
|
-
rules: RULES,
|
|
274
|
-
parseHtml,
|
|
275
|
-
});
|
|
276
|
-
|
|
277
|
-
const _extends =
|
|
278
|
-
Object.assign ||
|
|
279
|
-
function(target) {
|
|
280
|
-
for (var i = 1; i < arguments.length; i++) {
|
|
281
|
-
var source = arguments[i];
|
|
282
|
-
|
|
283
|
-
for (var key in source) {
|
|
284
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
285
|
-
target[key] = source[key];
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
return target;
|
|
291
|
-
};
|
|
292
|
-
|
|
293
|
-
export const htmlToValue = (html) => {
|
|
294
|
-
try {
|
|
295
|
-
return serializer.deserialize(html);
|
|
296
|
-
} catch (e) {
|
|
297
|
-
console.log("Couldn't parse html: ", e);
|
|
298
|
-
return {};
|
|
299
|
-
}
|
|
300
|
-
};
|
|
301
|
-
|
|
302
|
-
export const valueToHtml = (value) => serializer.serialize(value);
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
*
|
|
306
|
-
* <div><div>a</div></div> -> <div>a</div>
|
|
307
|
-
*
|
|
308
|
-
* <div><div>a</div><div>b</div></div> -> <div>a</div><div>b</div>
|
|
309
|
-
* <div><div>a</div>4444<div>b</div></div> -> <div>a</div>4444<div>b</div>
|
|
310
|
-
*/
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { Editor } from 'slate';
|
|
3
|
-
import debug from 'debug';
|
|
4
|
-
|
|
5
|
-
const log = debug('@pie-lib:editable-html:plugins');
|
|
6
|
-
|
|
7
|
-
const isMarkActive = (editor, format) => {
|
|
8
|
-
const marks = Editor.marks(editor);
|
|
9
|
-
return marks ? marks[format] === true : false;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
const toggleMark = (editor, format) => {
|
|
13
|
-
const isActive = isMarkActive(editor, format);
|
|
14
|
-
|
|
15
|
-
if (isActive) {
|
|
16
|
-
Editor.removeMark(editor, format);
|
|
17
|
-
} else {
|
|
18
|
-
Editor.addMark(editor, format, true);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export default function MarkHotkey(options) {
|
|
23
|
-
const { type, key, icon, tag } = options;
|
|
24
|
-
|
|
25
|
-
// Return our "plugin" object, containing the `onKeyDown` handler.
|
|
26
|
-
return {
|
|
27
|
-
toolbar: {
|
|
28
|
-
isMark: true,
|
|
29
|
-
type,
|
|
30
|
-
icon,
|
|
31
|
-
onToggle: (editor) => {
|
|
32
|
-
log('[onToggleMark] type: ', type);
|
|
33
|
-
toggleMark(editor, type);
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
renderMark(props) {
|
|
37
|
-
if (props.mark.type === type) {
|
|
38
|
-
const K = tag || type;
|
|
39
|
-
return <K>{props.children}</K>;
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
onKeyDown(event, change) {
|
|
43
|
-
// Check that the key pressed matches our `key` option.
|
|
44
|
-
if (!event.metaKey || event.key !== key) return;
|
|
45
|
-
|
|
46
|
-
// Prevent the default characters from being inserted.
|
|
47
|
-
event.preventDefault();
|
|
48
|
-
|
|
49
|
-
// Toggle the mark `type`.
|
|
50
|
-
change.toggleMark(type);
|
|
51
|
-
return true;
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
}
|
package/src/test-serializer.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import ReactServer from 'react-dom/server';
|
|
3
|
-
import escapeHtml from 'escape-html';
|
|
4
|
-
import { Text } from 'slate';
|
|
5
|
-
import { jsx } from 'slate-hyperscript';
|
|
6
|
-
import { MARK_TAGS } from './new-serialization';
|
|
7
|
-
|
|
8
|
-
class Html {
|
|
9
|
-
constructor(props) {
|
|
10
|
-
this.defaultBlock = props.defaultBlock;
|
|
11
|
-
this.parseHtml = props.parseHtml;
|
|
12
|
-
this.rules = props.rules;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
serializeEls = (node) => {
|
|
16
|
-
let children = (node.children || []).map((n) => this.serializeEls(n));
|
|
17
|
-
|
|
18
|
-
const correctRule = this.rules.reduce((res, rule) => {
|
|
19
|
-
return res || rule.serialize(node, children);
|
|
20
|
-
}, null);
|
|
21
|
-
|
|
22
|
-
if (correctRule) {
|
|
23
|
-
return correctRule;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
switch (node.type) {
|
|
27
|
-
case 'quote':
|
|
28
|
-
return (
|
|
29
|
-
<blockquote>
|
|
30
|
-
<p>{children}</p>
|
|
31
|
-
</blockquote>
|
|
32
|
-
);
|
|
33
|
-
case 'paragraph':
|
|
34
|
-
return <p>{children}</p>;
|
|
35
|
-
case 'link':
|
|
36
|
-
return <a href={escapeHtml(node.url)}>{children}</a>;
|
|
37
|
-
default:
|
|
38
|
-
return children;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
serialize = (node) => {
|
|
43
|
-
const deserialized = this.serializeEls(node);
|
|
44
|
-
const html = ReactServer.renderToStaticMarkup(React.createElement('body', null, deserialized));
|
|
45
|
-
const inner = html.slice(6, -7);
|
|
46
|
-
return inner;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
deserialize = (html) => {
|
|
50
|
-
let body = this.parseHtml(html);
|
|
51
|
-
|
|
52
|
-
if (
|
|
53
|
-
!body.firstChild ||
|
|
54
|
-
body.firstChild.nodeType === Node.TEXT_NODE ||
|
|
55
|
-
Object.keys(MARK_TAGS).includes(body.firstChild.tagName.toLowerCase())
|
|
56
|
-
) {
|
|
57
|
-
body = this.parseHtml(`<span>${html}</span>`);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return this.deserializeEls(body);
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
deserializeEls = (element, markAttributes = {}) => {
|
|
64
|
-
if (element.nodeType === Node.TEXT_NODE) {
|
|
65
|
-
return jsx('text', markAttributes, element.textContent);
|
|
66
|
-
} else if (element.nodeType !== Node.ELEMENT_NODE) {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const nodeAttributes = { ...markAttributes };
|
|
71
|
-
|
|
72
|
-
// define attributes for text nodes
|
|
73
|
-
if (element.nodeName === 'STRONG') {
|
|
74
|
-
nodeAttributes.bold = true;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const nextFn = (nodes) => {
|
|
78
|
-
const childNodes = Array.from(nodes);
|
|
79
|
-
const children = Array.from(childNodes)
|
|
80
|
-
.map((node) => this.deserializeEls(node, nodeAttributes))
|
|
81
|
-
.flat();
|
|
82
|
-
|
|
83
|
-
if (children.length === 0) {
|
|
84
|
-
children.push(jsx('text', nodeAttributes, ''));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return children;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
const correctRule = this.rules.reduce((res, rule) => {
|
|
91
|
-
return res || rule.deserialize(element, nextFn);
|
|
92
|
-
}, null);
|
|
93
|
-
|
|
94
|
-
if (correctRule) {
|
|
95
|
-
return correctRule;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const childNodes = Array.from(element.childNodes);
|
|
99
|
-
const children = Array.from(childNodes)
|
|
100
|
-
.map((node) => this.deserializeEls(node, nodeAttributes))
|
|
101
|
-
.flat();
|
|
102
|
-
|
|
103
|
-
if (children.length === 0) {
|
|
104
|
-
children.push(jsx('text', nodeAttributes, ''));
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
switch (element.nodeName) {
|
|
108
|
-
case 'TABLE':
|
|
109
|
-
return jsx('element', { type: 'table' }, children);
|
|
110
|
-
case 'TBODY':
|
|
111
|
-
return jsx('element', { type: 'tbody' }, children);
|
|
112
|
-
case 'TR':
|
|
113
|
-
return jsx('element', { type: 'tr' }, children);
|
|
114
|
-
case 'TD':
|
|
115
|
-
return jsx('element', { type: 'td' }, children);
|
|
116
|
-
case 'BODY':
|
|
117
|
-
return jsx('fragment', {}, children);
|
|
118
|
-
case 'BR':
|
|
119
|
-
return '\n';
|
|
120
|
-
case 'BLOCKQUOTE':
|
|
121
|
-
return jsx('element', { type: 'quote' }, children);
|
|
122
|
-
case 'P':
|
|
123
|
-
return jsx('element', { type: 'paragraph' }, children);
|
|
124
|
-
case 'A':
|
|
125
|
-
return jsx('element', { type: 'link', url: element.getAttribute('href') }, children);
|
|
126
|
-
default:
|
|
127
|
-
return children;
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export default Html;
|