@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.
Files changed (144) hide show
  1. package/CHANGELOG.md +0 -302
  2. package/lib/components.js +116 -0
  3. package/lib/components.js.map +1 -0
  4. package/lib/editor.js +418 -103
  5. package/lib/editor.js.map +1 -1
  6. package/lib/index.js +101 -155
  7. package/lib/index.js.map +1 -1
  8. package/lib/new-serialization.js +320 -0
  9. package/lib/new-serialization.js.map +1 -0
  10. package/lib/old-serialization.js +330 -0
  11. package/lib/parse-html.js +1 -1
  12. package/lib/parse-html.js.map +1 -1
  13. package/lib/plugins/characters/custom-popper.js +1 -1
  14. package/lib/plugins/characters/custom-popper.js.map +1 -1
  15. package/lib/plugins/characters/index.js +21 -19
  16. package/lib/plugins/characters/index.js.map +1 -1
  17. package/lib/plugins/characters/utils.js +1 -1
  18. package/lib/plugins/characters/utils.js.map +1 -1
  19. package/lib/plugins/hotKeys/index.js +67 -0
  20. package/lib/plugins/hotKeys/index.js.map +1 -0
  21. package/lib/plugins/image/alt-dialog.js +1 -6
  22. package/lib/plugins/image/alt-dialog.js.map +1 -1
  23. package/lib/plugins/image/component.js +70 -53
  24. package/lib/plugins/image/component.js.map +1 -1
  25. package/lib/plugins/image/image-toolbar.js +7 -9
  26. package/lib/plugins/image/image-toolbar.js.map +1 -1
  27. package/lib/plugins/image/index.js +83 -27
  28. package/lib/plugins/image/index.js.map +1 -1
  29. package/lib/plugins/image/insert-image-handler.js +72 -33
  30. package/lib/plugins/image/insert-image-handler.js.map +1 -1
  31. package/lib/plugins/index.js +23 -41
  32. package/lib/plugins/index.js.map +1 -1
  33. package/lib/plugins/list/index.js +64 -100
  34. package/lib/plugins/list/index.js.map +1 -1
  35. package/lib/plugins/math/index.js +86 -60
  36. package/lib/plugins/math/index.js.map +1 -1
  37. package/lib/plugins/media/index.js +202 -132
  38. package/lib/plugins/media/index.js.map +1 -1
  39. package/lib/plugins/media/media-dialog.js +17 -16
  40. package/lib/plugins/media/media-dialog.js.map +1 -1
  41. package/lib/plugins/media/media-toolbar.js +3 -3
  42. package/lib/plugins/media/media-toolbar.js.map +1 -1
  43. package/lib/plugins/media/media-wrapper.js +21 -58
  44. package/lib/plugins/media/media-wrapper.js.map +1 -1
  45. package/lib/plugins/respArea/drag-in-the-blank/choice.js +3 -3
  46. package/lib/plugins/respArea/drag-in-the-blank/choice.js.map +1 -1
  47. package/lib/plugins/respArea/drag-in-the-blank/index.js +3 -2
  48. package/lib/plugins/respArea/drag-in-the-blank/index.js.map +1 -1
  49. package/lib/plugins/respArea/explicit-constructed-response/index.js +3 -2
  50. package/lib/plugins/respArea/explicit-constructed-response/index.js.map +1 -1
  51. package/lib/plugins/respArea/icons/index.js +13 -15
  52. package/lib/plugins/respArea/icons/index.js.map +1 -1
  53. package/lib/plugins/respArea/index.js +87 -53
  54. package/lib/plugins/respArea/index.js.map +1 -1
  55. package/lib/plugins/respArea/inline-dropdown/index.js +4 -3
  56. package/lib/plugins/respArea/inline-dropdown/index.js.map +1 -1
  57. package/lib/plugins/respArea/utils.js +17 -20
  58. package/lib/plugins/respArea/utils.js.map +1 -1
  59. package/lib/plugins/table/icons/index.js +1 -1
  60. package/lib/plugins/table/icons/index.js.map +1 -1
  61. package/lib/plugins/table/index.js +381 -212
  62. package/lib/plugins/table/index.js.map +1 -1
  63. package/lib/plugins/table/table-toolbar.js +5 -6
  64. package/lib/plugins/table/table-toolbar.js.map +1 -1
  65. package/lib/plugins/toolbar/default-toolbar.js +55 -11
  66. package/lib/plugins/toolbar/default-toolbar.js.map +1 -1
  67. package/lib/plugins/toolbar/done-button.js +1 -1
  68. package/lib/plugins/toolbar/done-button.js.map +1 -1
  69. package/lib/plugins/toolbar/editor-and-toolbar.js +186 -232
  70. package/lib/plugins/toolbar/editor-and-toolbar.js.map +1 -1
  71. package/lib/plugins/toolbar/index.js +1 -2
  72. package/lib/plugins/toolbar/index.js.map +1 -1
  73. package/lib/plugins/toolbar/toolbar-buttons.js +1 -1
  74. package/lib/plugins/toolbar/toolbar-buttons.js.map +1 -1
  75. package/lib/plugins/toolbar/toolbar.js +253 -239
  76. package/lib/plugins/toolbar/toolbar.js.map +1 -1
  77. package/lib/plugins/utils.js +27 -2
  78. package/lib/plugins/utils.js.map +1 -1
  79. package/lib/serialization.js +1 -1
  80. package/lib/serialization.js.map +1 -1
  81. package/lib/slate-editor.js +302 -0
  82. package/lib/test-serializer.js +189 -0
  83. package/lib/test-serializer.js.map +1 -0
  84. package/lib/theme.js +1 -1
  85. package/lib/theme.js.map +1 -1
  86. package/package.json +18 -14
  87. package/playground/image/data.js +20 -20
  88. package/playground/image/index.html +22 -20
  89. package/playground/image/index.jsx +12 -10
  90. package/playground/index.html +25 -23
  91. package/playground/mathquill/index.html +23 -20
  92. package/playground/mathquill/index.jsx +18 -22
  93. package/playground/prod-test/index.html +24 -20
  94. package/playground/prod-test/index.jsx +5 -3
  95. package/playground/schema-override/data.js +10 -10
  96. package/playground/schema-override/image-plugin.jsx +3 -4
  97. package/playground/schema-override/index.html +21 -19
  98. package/playground/schema-override/index.jsx +13 -14
  99. package/playground/serialization/data.js +10 -10
  100. package/playground/serialization/image-plugin.jsx +3 -4
  101. package/playground/serialization/index.html +22 -20
  102. package/playground/table-examples.html +5 -8
  103. package/playground/webpack.config.js +10 -10
  104. package/src/components.js +135 -0
  105. package/src/editor.jsx +478 -141
  106. package/src/index.jsx +71 -95
  107. package/src/new-serialization.jsx +291 -0
  108. package/src/parse-html.js +1 -1
  109. package/src/plugins/characters/custom-popper.js +7 -7
  110. package/src/plugins/characters/index.jsx +33 -34
  111. package/src/plugins/characters/utils.js +81 -81
  112. package/src/plugins/hotKeys/index.js +54 -0
  113. package/src/plugins/image/alt-dialog.jsx +4 -5
  114. package/src/plugins/image/component.jsx +106 -89
  115. package/src/plugins/image/image-toolbar.jsx +27 -19
  116. package/src/plugins/image/index.jsx +75 -43
  117. package/src/plugins/image/insert-image-handler.js +62 -27
  118. package/src/plugins/index.jsx +23 -41
  119. package/src/plugins/list/index.jsx +70 -95
  120. package/src/plugins/math/index.jsx +102 -82
  121. package/src/plugins/media/index.jsx +159 -124
  122. package/src/plugins/media/media-dialog.js +98 -71
  123. package/src/plugins/media/media-toolbar.jsx +8 -8
  124. package/src/plugins/media/media-wrapper.jsx +29 -30
  125. package/src/plugins/respArea/drag-in-the-blank/choice.jsx +21 -19
  126. package/src/plugins/respArea/drag-in-the-blank/index.jsx +14 -11
  127. package/src/plugins/respArea/explicit-constructed-response/index.jsx +7 -6
  128. package/src/plugins/respArea/icons/index.jsx +11 -14
  129. package/src/plugins/respArea/index.jsx +92 -52
  130. package/src/plugins/respArea/inline-dropdown/index.jsx +9 -8
  131. package/src/plugins/respArea/utils.jsx +26 -35
  132. package/src/plugins/table/icons/index.jsx +17 -11
  133. package/src/plugins/table/index.jsx +288 -231
  134. package/src/plugins/table/table-toolbar.jsx +15 -11
  135. package/src/plugins/toolbar/default-toolbar.jsx +65 -19
  136. package/src/plugins/toolbar/done-button.jsx +4 -4
  137. package/src/plugins/toolbar/editor-and-toolbar.jsx +150 -145
  138. package/src/plugins/toolbar/index.jsx +2 -3
  139. package/src/plugins/toolbar/toolbar-buttons.jsx +11 -11
  140. package/src/plugins/toolbar/toolbar.jsx +244 -221
  141. package/src/plugins/utils.js +21 -4
  142. package/src/serialization.jsx +32 -32
  143. package/src/test-serializer.js +139 -0
  144. package/src/test-serializer.js.rej +20 -0
@@ -30,7 +30,7 @@ export const BLOCK_TAGS = {
30
30
  h3: 'heading-three',
31
31
  h4: 'heading-four',
32
32
  h5: 'heading-five',
33
- h6: 'heading-six',
33
+ h6: 'heading-six'
34
34
  };
35
35
 
36
36
  /**
@@ -45,10 +45,10 @@ const MARK_TAGS = {
45
45
  u: 'underline',
46
46
  s: 'strikethrough',
47
47
  code: 'code',
48
- strong: 'bold',
48
+ strong: 'bold'
49
49
  };
50
50
 
51
- export const parseStyleString = (s) => {
51
+ export const parseStyleString = s => {
52
52
  const regex = /([\w-]*)\s*:\s*([^;]*)/g;
53
53
  let match;
54
54
  const result = {};
@@ -58,18 +58,18 @@ export const parseStyleString = (s) => {
58
58
  return result;
59
59
  };
60
60
 
61
- export const getBase64 = (file) => {
61
+ export const getBase64 = file => {
62
62
  return new Promise((resolve, reject) => {
63
63
  const reader = new FileReader();
64
64
  reader.readAsDataURL(file);
65
65
  reader.onload = () => resolve(reader.result);
66
- reader.onerror = (error) => reject(error);
66
+ reader.onerror = error => reject(error);
67
67
  });
68
68
  };
69
69
 
70
- export const reactAttributes = (o) => toStyleObject(o, { camelize: true, addUnits: false });
70
+ export const reactAttributes = o => toStyleObject(o, { camelize: true, addUnits: false });
71
71
 
72
- const attributesToMap = (el) => (acc, attribute) => {
72
+ const attributesToMap = el => (acc, attribute) => {
73
73
  const value = el.getAttribute(attribute);
74
74
  if (value) {
75
75
  if (attribute === 'style') {
@@ -113,7 +113,7 @@ const blocks = {
113
113
  * Here for rendering styles for all block elements
114
114
  */
115
115
  data: { attributes: attributes.reduce(attributesToMap(el), {}) },
116
- nodes: next(el.childNodes),
116
+ nodes: next(el.childNodes)
117
117
  };
118
118
  },
119
119
  serialize: (object, children) => {
@@ -131,7 +131,7 @@ const blocks = {
131
131
  return <Tag {...jsonData.attributes}>{children}</Tag>;
132
132
  }
133
133
  }
134
- },
134
+ }
135
135
  };
136
136
 
137
137
  const marks = {
@@ -142,7 +142,7 @@ const marks = {
142
142
  return {
143
143
  object: 'mark',
144
144
  type: mark,
145
- nodes: next(el.childNodes),
145
+ nodes: next(el.childNodes)
146
146
  };
147
147
  },
148
148
  serialize(object, children) {
@@ -154,10 +154,10 @@ const marks = {
154
154
  }
155
155
  }
156
156
  }
157
- },
157
+ }
158
158
  };
159
159
 
160
- const findPreviousText = (el) => {
160
+ const findPreviousText = el => {
161
161
  if (el.nodeName === '#text') {
162
162
  return el;
163
163
  }
@@ -182,9 +182,9 @@ export const TEXT_RULE = {
182
182
  leaves: [
183
183
  {
184
184
  object: 'leaf',
185
- text: '\n',
186
- },
187
- ],
185
+ text: '\n'
186
+ }
187
+ ]
188
188
  };
189
189
  }
190
190
 
@@ -197,9 +197,9 @@ export const TEXT_RULE = {
197
197
  leaves: [
198
198
  {
199
199
  object: 'leaf',
200
- text: el.nodeValue,
201
- },
202
- ],
200
+ text: el.nodeValue
201
+ }
202
+ ]
203
203
  };
204
204
  }
205
205
  },
@@ -212,7 +212,7 @@ export const TEXT_RULE = {
212
212
  return array;
213
213
  }, []);
214
214
  }
215
- },
215
+ }
216
216
  };
217
217
 
218
218
  const RULES = [
@@ -224,7 +224,7 @@ const RULES = [
224
224
  responseAreaSerialization,
225
225
  TEXT_RULE,
226
226
  blocks,
227
- marks,
227
+ marks
228
228
  ];
229
229
 
230
230
  function allWhitespace(node) {
@@ -235,7 +235,7 @@ function allWhitespace(node) {
235
235
  function defaultParseHtml(html) {
236
236
  if (typeof DOMParser === 'undefined') {
237
237
  throw new Error(
238
- '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.',
238
+ '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.'
239
239
  );
240
240
  }
241
241
 
@@ -259,14 +259,14 @@ function defaultParseHtml(html) {
259
259
  const parseHtml =
260
260
  typeof window === 'undefined'
261
261
  ? () => ({
262
- childNodes: [],
262
+ childNodes: []
263
263
  })
264
264
  : defaultParseHtml;
265
265
 
266
266
  const serializer = new Html({
267
267
  defaultBlock: 'div',
268
268
  rules: RULES,
269
- parseHtml,
269
+ parseHtml
270
270
  });
271
271
 
272
272
  const _extends =
@@ -320,7 +320,7 @@ serializer.deserialize = function deserialize(html) {
320
320
  }
321
321
 
322
322
  const block = _extends({ object: 'block', data: {}, isVoid: false }, defaultBlock, {
323
- nodes: [node],
323
+ nodes: [node]
324
324
  });
325
325
 
326
326
  memo.push(block);
@@ -330,8 +330,8 @@ serializer.deserialize = function deserialize(html) {
330
330
  if (nodes.length === 0) {
331
331
  nodes = [
332
332
  _extends({ object: 'block', data: {}, isVoid: false }, defaultBlock, {
333
- nodes: [{ object: 'text', leaves: [{ object: 'leaf', text: '', marks: [] }] }],
334
- }),
333
+ nodes: [{ object: 'text', leaves: [{ object: 'leaf', text: '', marks: [] }] }]
334
+ })
335
335
  ];
336
336
  }
337
337
 
@@ -340,11 +340,11 @@ serializer.deserialize = function deserialize(html) {
340
340
  document: {
341
341
  object: 'document',
342
342
  data: {},
343
- nodes: nodes,
343
+ nodes: nodes
344
344
  },
345
345
  schema: {
346
- rules: [],
347
- },
346
+ rules: []
347
+ }
348
348
  };
349
349
 
350
350
  let i;
@@ -352,7 +352,7 @@ serializer.deserialize = function deserialize(html) {
352
352
  for (i = 0; i < 3000; i++) {
353
353
  json.schema.rules.push({
354
354
  match: { object: 'document' },
355
- nodes: [{ match: { object: 'block' } }],
355
+ nodes: [{ match: { object: 'block' } }]
356
356
  });
357
357
  }
358
358
 
@@ -365,7 +365,7 @@ serializer.deserialize = function deserialize(html) {
365
365
  return null;
366
366
  };
367
367
 
368
- export const htmlToValue = (html) => {
368
+ export const htmlToValue = html => {
369
369
  try {
370
370
  return serializer.deserialize(html);
371
371
  } catch (e) {
@@ -374,7 +374,7 @@ export const htmlToValue = (html) => {
374
374
  }
375
375
  };
376
376
 
377
- export const valueToHtml = (value) => serializer.serialize(value);
377
+ export const valueToHtml = value => serializer.serialize(value);
378
378
 
379
379
  /**
380
380
  *
@@ -0,0 +1,139 @@
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
+
7
+ class Html {
8
+ constructor(props) {
9
+ this.defaultBlock = props.defaultBlock;
10
+ this.parseHtml = props.parseHtml;
11
+ this.rules = props.rules;
12
+ }
13
+
14
+ serializeEls = node => {
15
+ if (Text.isText(node)) {
16
+ let string = escapeHtml(node.text);
17
+ if (node.bold) {
18
+ string = <strong>{string}</strong>;
19
+ }
20
+ return string;
21
+ }
22
+
23
+ let children = (node.children || []).map(n => this.serializeEls(n));
24
+
25
+ const correctRule = this.rules.reduce((res, rule) => {
26
+ return res || rule.serialize(node, children);
27
+ }, null);
28
+
29
+ if (correctRule) {
30
+ return correctRule;
31
+ }
32
+
33
+ switch (node.type) {
34
+ case 'quote':
35
+ return (
36
+ <blockquote>
37
+ <p>{children}</p>
38
+ </blockquote>
39
+ );
40
+ case 'paragraph':
41
+ return <p>{children}</p>;
42
+ case 'link':
43
+ return <a href={escapeHtml(node.url)}>{children}</a>;
44
+ default:
45
+ return children;
46
+ }
47
+ };
48
+
49
+ serialize = node => {
50
+ const deserialized = this.serializeEls(node);
51
+ const html = ReactServer.renderToStaticMarkup(React.createElement(
52
+ 'body',
53
+ null,
54
+ deserialized
55
+ ));
56
+ const inner = html.slice(6, -7);
57
+ return inner;
58
+ };
59
+
60
+ deserialize = html => {
61
+ let body = this.parseHtml(html);
62
+
63
+ if (!body.firstChild || body.firstChild.nodeType === Node.TEXT_NODE) {
64
+ body = this.parseHtml(`<p>${html}</p>`);
65
+ }
66
+
67
+ return this.deserializeEls(body);
68
+ };
69
+
70
+ deserializeEls = (element, markAttributes = {}) => {
71
+ if (element.nodeType === Node.TEXT_NODE) {
72
+ return jsx('text', markAttributes, element.textContent);
73
+ } else if (element.nodeType !== Node.ELEMENT_NODE) {
74
+ return null;
75
+ }
76
+
77
+ const nodeAttributes = { ...markAttributes };
78
+
79
+ // define attributes for text nodes
80
+ if (element.nodeName === 'STRONG') {
81
+ nodeAttributes.bold = true;
82
+ }
83
+
84
+ const nextFn = nodes => {
85
+ const childNodes = Array.from(nodes);
86
+ const children = Array.from(childNodes)
87
+ .map(node => this.deserializeEls(node, nodeAttributes))
88
+ .flat();
89
+
90
+ if (children.length === 0) {
91
+ children.push(jsx('text', nodeAttributes, ''));
92
+ }
93
+
94
+ return children;
95
+ };
96
+
97
+ const correctRule = this.rules.reduce((res, rule) => {
98
+ return res || rule.deserialize(element, nextFn);
99
+ }, null);
100
+
101
+ if (correctRule) {
102
+ return correctRule;
103
+ }
104
+
105
+ const childNodes = Array.from(element.childNodes);
106
+ const children = Array.from(childNodes)
107
+ .map(node => this.deserializeEls(node, nodeAttributes))
108
+ .flat();
109
+
110
+ if (children.length === 0) {
111
+ children.push(jsx('text', nodeAttributes, ''));
112
+ }
113
+
114
+ switch (element.nodeName) {
115
+ case 'TABLE':
116
+ return jsx('element', { type: 'table' }, children);
117
+ case 'TBODY':
118
+ return jsx('element', { type: 'tbody' }, children);
119
+ case 'TR':
120
+ return jsx('element', { type: 'tr' }, children);
121
+ case 'TD':
122
+ return jsx('element', { type: 'td' }, children);
123
+ case 'BODY':
124
+ return jsx('fragment', {}, children);
125
+ case 'BR':
126
+ return '\n';
127
+ case 'BLOCKQUOTE':
128
+ return jsx('element', { type: 'quote' }, children);
129
+ case 'P':
130
+ return jsx('element', { type: 'paragraph' }, children);
131
+ case 'A':
132
+ return jsx('element', { type: 'link', url: element.getAttribute('href') }, children);
133
+ default:
134
+ return children;
135
+ }
136
+ };
137
+ }
138
+
139
+ export default Html;
@@ -0,0 +1,20 @@
1
+ diff a/packages/editable-html/src/test-serializer.js b/packages/editable-html/src/test-serializer.js (rejected hunks)
2
+ @@ -4 +4 @@ import { jsx } from 'slate-hyperscript';
3
+ -import { unWrapMath } from "@pie-lib/math-rendering";
4
+ +import { unWrapMath } from '@pie-lib/math-rendering';
5
+ @@ -82,4 +82,11 @@ class Html {
6
+ - return jsx('element', { type: 'math', data: {
7
+ - latex: unwrapped,
8
+ - wrapper: wrapType
9
+ - } }, children);
10
+ + return jsx(
11
+ + 'element',
12
+ + {
13
+ + type: 'math',
14
+ + data: {
15
+ + latex: unwrapped,
16
+ + wrapper: wrapType
17
+ + }
18
+ + },
19
+ + children
20
+ + );