@pie-lib/editable-html 11.4.0 → 11.5.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.
Files changed (106) hide show
  1. package/CHANGELOG.md +13 -65
  2. package/package.json +8 -7
  3. package/src/editor.jsx +2 -2
  4. package/src/plugins/media/media-dialog.js +1 -1
  5. package/src/plugins/respArea/drag-in-the-blank/choice.jsx +1 -1
  6. package/src/plugins/toolbar/editor-and-toolbar.jsx +1 -1
  7. package/lib/block-tags.js +0 -25
  8. package/lib/block-tags.js.map +0 -1
  9. package/lib/constants.js +0 -16
  10. package/lib/constants.js.map +0 -1
  11. package/lib/editor.js +0 -1355
  12. package/lib/editor.js.map +0 -1
  13. package/lib/index.js +0 -269
  14. package/lib/index.js.map +0 -1
  15. package/lib/parse-html.js +0 -16
  16. package/lib/parse-html.js.map +0 -1
  17. package/lib/plugins/characters/custom-popper.js +0 -73
  18. package/lib/plugins/characters/custom-popper.js.map +0 -1
  19. package/lib/plugins/characters/index.js +0 -305
  20. package/lib/plugins/characters/index.js.map +0 -1
  21. package/lib/plugins/characters/utils.js +0 -381
  22. package/lib/plugins/characters/utils.js.map +0 -1
  23. package/lib/plugins/css/icons/index.js +0 -37
  24. package/lib/plugins/css/icons/index.js.map +0 -1
  25. package/lib/plugins/css/index.js +0 -397
  26. package/lib/plugins/css/index.js.map +0 -1
  27. package/lib/plugins/customPlugin/index.js +0 -114
  28. package/lib/plugins/customPlugin/index.js.map +0 -1
  29. package/lib/plugins/html/icons/index.js +0 -38
  30. package/lib/plugins/html/icons/index.js.map +0 -1
  31. package/lib/plugins/html/index.js +0 -80
  32. package/lib/plugins/html/index.js.map +0 -1
  33. package/lib/plugins/image/alt-dialog.js +0 -129
  34. package/lib/plugins/image/alt-dialog.js.map +0 -1
  35. package/lib/plugins/image/component.js +0 -419
  36. package/lib/plugins/image/component.js.map +0 -1
  37. package/lib/plugins/image/image-toolbar.js +0 -177
  38. package/lib/plugins/image/image-toolbar.js.map +0 -1
  39. package/lib/plugins/image/index.js +0 -263
  40. package/lib/plugins/image/index.js.map +0 -1
  41. package/lib/plugins/image/insert-image-handler.js +0 -161
  42. package/lib/plugins/image/insert-image-handler.js.map +0 -1
  43. package/lib/plugins/index.js +0 -402
  44. package/lib/plugins/index.js.map +0 -1
  45. package/lib/plugins/list/index.js +0 -334
  46. package/lib/plugins/list/index.js.map +0 -1
  47. package/lib/plugins/math/index.js +0 -454
  48. package/lib/plugins/math/index.js.map +0 -1
  49. package/lib/plugins/media/index.js +0 -387
  50. package/lib/plugins/media/index.js.map +0 -1
  51. package/lib/plugins/media/media-dialog.js +0 -709
  52. package/lib/plugins/media/media-dialog.js.map +0 -1
  53. package/lib/plugins/media/media-toolbar.js +0 -101
  54. package/lib/plugins/media/media-toolbar.js.map +0 -1
  55. package/lib/plugins/media/media-wrapper.js +0 -93
  56. package/lib/plugins/media/media-wrapper.js.map +0 -1
  57. package/lib/plugins/rendering/index.js +0 -46
  58. package/lib/plugins/rendering/index.js.map +0 -1
  59. package/lib/plugins/respArea/drag-in-the-blank/choice.js +0 -254
  60. package/lib/plugins/respArea/drag-in-the-blank/choice.js.map +0 -1
  61. package/lib/plugins/respArea/drag-in-the-blank/index.js +0 -97
  62. package/lib/plugins/respArea/drag-in-the-blank/index.js.map +0 -1
  63. package/lib/plugins/respArea/explicit-constructed-response/index.js +0 -57
  64. package/lib/plugins/respArea/explicit-constructed-response/index.js.map +0 -1
  65. package/lib/plugins/respArea/icons/index.js +0 -95
  66. package/lib/plugins/respArea/icons/index.js.map +0 -1
  67. package/lib/plugins/respArea/index.js +0 -341
  68. package/lib/plugins/respArea/index.js.map +0 -1
  69. package/lib/plugins/respArea/inline-dropdown/index.js +0 -75
  70. package/lib/plugins/respArea/inline-dropdown/index.js.map +0 -1
  71. package/lib/plugins/respArea/math-templated/index.js +0 -130
  72. package/lib/plugins/respArea/math-templated/index.js.map +0 -1
  73. package/lib/plugins/respArea/utils.js +0 -125
  74. package/lib/plugins/respArea/utils.js.map +0 -1
  75. package/lib/plugins/table/CustomTablePlugin.js +0 -133
  76. package/lib/plugins/table/CustomTablePlugin.js.map +0 -1
  77. package/lib/plugins/table/icons/index.js +0 -69
  78. package/lib/plugins/table/icons/index.js.map +0 -1
  79. package/lib/plugins/table/index.js +0 -483
  80. package/lib/plugins/table/index.js.map +0 -1
  81. package/lib/plugins/table/table-toolbar.js +0 -187
  82. package/lib/plugins/table/table-toolbar.js.map +0 -1
  83. package/lib/plugins/textAlign/icons/index.js +0 -226
  84. package/lib/plugins/textAlign/icons/index.js.map +0 -1
  85. package/lib/plugins/textAlign/index.js +0 -34
  86. package/lib/plugins/textAlign/index.js.map +0 -1
  87. package/lib/plugins/toolbar/default-toolbar.js +0 -229
  88. package/lib/plugins/toolbar/default-toolbar.js.map +0 -1
  89. package/lib/plugins/toolbar/done-button.js +0 -53
  90. package/lib/plugins/toolbar/done-button.js.map +0 -1
  91. package/lib/plugins/toolbar/editor-and-toolbar.js +0 -286
  92. package/lib/plugins/toolbar/editor-and-toolbar.js.map +0 -1
  93. package/lib/plugins/toolbar/index.js +0 -34
  94. package/lib/plugins/toolbar/index.js.map +0 -1
  95. package/lib/plugins/toolbar/toolbar-buttons.js +0 -194
  96. package/lib/plugins/toolbar/toolbar-buttons.js.map +0 -1
  97. package/lib/plugins/toolbar/toolbar.js +0 -376
  98. package/lib/plugins/toolbar/toolbar.js.map +0 -1
  99. package/lib/plugins/utils.js +0 -62
  100. package/lib/plugins/utils.js.map +0 -1
  101. package/lib/serialization.js +0 -677
  102. package/lib/serialization.js.map +0 -1
  103. package/lib/shared/alert-dialog.js +0 -75
  104. package/lib/shared/index.js +0 -136
  105. package/lib/theme.js +0 -9
  106. package/lib/theme.js.map +0 -1
@@ -1,677 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.valueToHtml = exports.reduceMultipleBrs = exports.reactAttributes = exports.parseStyleString = exports.htmlToValue = exports.handleHtml = exports.getBase64 = exports.extraCSSRulesOpts = exports.TEXT_RULE = exports.INLINE_TAGS = void 0;
9
-
10
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
-
12
- var _slateHtmlSerializer = _interopRequireDefault(require("slate-html-serializer"));
13
-
14
- var _react = _interopRequireDefault(require("react"));
15
-
16
- var _debug = _interopRequireDefault(require("debug"));
17
-
18
- var _toStyle = require("to-style");
19
-
20
- var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));
21
-
22
- var _image = require("./plugins/image");
23
-
24
- var _math = require("./plugins/math");
25
-
26
- var _media = require("./plugins/media");
27
-
28
- var _list = require("./plugins/list");
29
-
30
- var _table = require("./plugins/table");
31
-
32
- var _respArea = require("./plugins/respArea");
33
-
34
- var _slate = require("slate");
35
-
36
- var _blockTags = require("./block-tags");
37
-
38
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
39
-
40
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
41
-
42
- var log = (0, _debug["default"])('@pie-lib:editable-html:serialization');
43
- /**
44
- * Tags to marks.
45
- *
46
- * @type {Object}
47
- */
48
-
49
- var MARK_TAGS = {
50
- b: 'bold',
51
- em: 'italic',
52
- u: 'underline',
53
- s: 'strikethrough',
54
- code: 'code',
55
- strong: 'bold',
56
- blockquote: 'blockquote',
57
- h3: 'h3',
58
- sup: 'sup',
59
- sub: 'sub'
60
- };
61
-
62
- var parseStyleString = function parseStyleString(s) {
63
- var regex = /([\w-]*)\s*:\s*([^;]*)/g;
64
- var match;
65
- var result = {};
66
-
67
- while (match = regex.exec(s)) {
68
- result[match[1]] = match[2].trim();
69
- }
70
-
71
- return result;
72
- };
73
-
74
- exports.parseStyleString = parseStyleString;
75
-
76
- var getBase64 = function getBase64(file) {
77
- return new Promise(function (resolve, reject) {
78
- var reader = new FileReader();
79
- reader.readAsDataURL(file);
80
-
81
- reader.onload = function () {
82
- return resolve(reader.result);
83
- };
84
-
85
- reader.onerror = function (error) {
86
- return reject(error);
87
- };
88
- });
89
- };
90
-
91
- exports.getBase64 = getBase64;
92
-
93
- var reactAttributes = function reactAttributes(o) {
94
- return (0, _toStyle.object)(o, {
95
- camelize: true,
96
- addUnits: false
97
- });
98
- };
99
-
100
- exports.reactAttributes = reactAttributes;
101
-
102
- var attributesToMap = function attributesToMap(el) {
103
- return function (acc, attribute) {
104
- var value = el.getAttribute(attribute);
105
-
106
- if (value) {
107
- if (attribute === 'style') {
108
- var styleString = el.getAttribute(attribute);
109
- var reactStyleObject = reactAttributes(parseStyleString(styleString));
110
- acc['style'] = reactStyleObject;
111
- } else {
112
- acc[attribute] = el.getAttribute(attribute);
113
- }
114
- }
115
-
116
- return acc;
117
- };
118
- };
119
-
120
- var attributes = ['border', 'cellpadding', 'cellspacing', 'class', 'style', 'align'];
121
- /**
122
- * Serializer rules.
123
- *
124
- * @type {Array}
125
- */
126
-
127
- var blocks = {
128
- deserialize: function deserialize(el, next) {
129
- log('[blocks:deserialize] block: ', el);
130
-
131
- var block = _blockTags.BLOCK_TAGS[el.tagName.toLowerCase()];
132
-
133
- if (!block) return;
134
- log('[blocks:deserialize] block: ', block);
135
-
136
- if (el.childNodes.length === 1) {
137
- var cn = el.childNodes[0];
138
-
139
- if (cn && cn.tagName && cn.tagName.toLowerCase() === block) {
140
- log('[we have a child node of the same]...');
141
- return;
142
- }
143
- }
144
-
145
- return {
146
- object: 'block',
147
- type: block,
148
-
149
- /**
150
- * Here for rendering styles for all block elements
151
- */
152
- data: {
153
- attributes: attributes.reduce(attributesToMap(el), {})
154
- },
155
- nodes: next(el.childNodes)
156
- };
157
- },
158
- serialize: function serialize(object, children) {
159
- if (object.object !== 'block') return;
160
- var jsonData = object.data.toJSON();
161
- log('[blocks:serialize] object: ', object, children);
162
- var key;
163
-
164
- for (key in _blockTags.BLOCK_TAGS) {
165
- if (_blockTags.BLOCK_TAGS[key] === object.type) {
166
- var Tag = key;
167
- return /*#__PURE__*/_react["default"].createElement(Tag, jsonData.attributes, children);
168
- }
169
- }
170
- }
171
- };
172
- var INLINE_TAGS = {
173
- span: 'span'
174
- };
175
- exports.INLINE_TAGS = INLINE_TAGS;
176
- var inlines = {
177
- deserialize: function deserialize(el, next) {
178
- log('[inlines:deserialize] inline: ', el);
179
- var inlineTag = INLINE_TAGS[el.tagName.toLowerCase()];
180
- if (!inlineTag) return;
181
- log('[inlines:deserialize] inline: ', inlineTag);
182
-
183
- if (el.childNodes.length === 1) {
184
- var cn = el.childNodes[0];
185
-
186
- if (cn && cn.tagName && cn.tagName.toLowerCase() === inlineTag) {
187
- log('[we have a child node of the same]...');
188
- return;
189
- }
190
- }
191
-
192
- return {
193
- object: 'inline',
194
- type: inlineTag,
195
-
196
- /**
197
- * Here for rendering styles for all inline elements
198
- */
199
- data: {
200
- attributes: attributes.reduce(attributesToMap(el), {})
201
- },
202
- nodes: next(el.childNodes)
203
- };
204
- },
205
- serialize: function serialize(object, children) {
206
- if (object.object !== 'inline') return;
207
- var jsonData = object.data.toJSON();
208
- log('[inlines:serialize] object: ', object, children);
209
- var key;
210
-
211
- for (key in INLINE_TAGS) {
212
- if (INLINE_TAGS[key] === object.type) {
213
- var Tag = key;
214
- return /*#__PURE__*/_react["default"].createElement(Tag, jsonData.attributes, children);
215
- }
216
- }
217
- }
218
- };
219
- var extraCSSRulesOpts = {};
220
- exports.extraCSSRulesOpts = extraCSSRulesOpts;
221
- var STYLES_MAP = {
222
- h3: {
223
- fontSize: 'inherit',
224
- fontWeight: 'inherit'
225
- },
226
- blockquote: {
227
- background: '#f9f9f9',
228
- borderLeft: '5px solid #ccc',
229
- margin: '1.5em 10px',
230
- padding: '.5em 10px'
231
- }
232
- };
233
- var reactToHTMLAttributesMap = {
234
- "class": 'className'
235
- };
236
- var marks = {
237
- deserialize: function deserialize(el, next) {
238
- var mark = MARK_TAGS[el.tagName.toLowerCase()];
239
- var elClasses = el.getAttribute('class') || '';
240
- var hasCSSRule = ((extraCSSRulesOpts === null || extraCSSRulesOpts === void 0 ? void 0 : extraCSSRulesOpts.names) || []).find(function (name) {
241
- return elClasses === null || elClasses === void 0 ? void 0 : elClasses.includes(name);
242
- });
243
-
244
- if (!mark && !hasCSSRule) {
245
- return;
246
- }
247
-
248
- log('[deserialize] mark: ', mark);
249
- var attrs = attributes.reduce(attributesToMap(el), {});
250
- var data = (0, _isEmpty["default"])(attrs) ? undefined : {
251
- attributes: attrs
252
- };
253
- return {
254
- object: 'mark',
255
- type: hasCSSRule ? 'css' : mark,
256
-
257
- /**
258
- * Here for rendering styles for all elements
259
- */
260
- data: data,
261
- nodes: next(el.childNodes)
262
- };
263
- },
264
- serialize: function serialize(object, children) {
265
- var _object$data, _jsonData$attributes;
266
-
267
- var jsonData = ((_object$data = object.data) === null || _object$data === void 0 ? void 0 : _object$data.toJSON()) || {};
268
- var elClasses = ((_jsonData$attributes = jsonData.attributes) === null || _jsonData$attributes === void 0 ? void 0 : _jsonData$attributes["class"]) || '';
269
- var hasCSSRule = ((extraCSSRulesOpts === null || extraCSSRulesOpts === void 0 ? void 0 : extraCSSRulesOpts.names) || []).find(function (name) {
270
- return elClasses === null || elClasses === void 0 ? void 0 : elClasses.includes(name);
271
- });
272
-
273
- if (hasCSSRule) {
274
- var htmlAttrs = Object.keys(jsonData.attributes).reduce(function (obj, key) {
275
- obj[reactToHTMLAttributesMap[key] || key] = jsonData.attributes[key];
276
- return obj;
277
- }, {});
278
- return /*#__PURE__*/_react["default"].createElement("span", htmlAttrs, children);
279
- }
280
-
281
- if (_slate.Mark.isMark(object)) {
282
- for (var key in MARK_TAGS) {
283
- if (MARK_TAGS[key] === object.type) {
284
- var Tag = key;
285
- var additionalStyles = STYLES_MAP[Tag];
286
-
287
- if (additionalStyles) {
288
- if (!jsonData.attributes) {
289
- jsonData.attributes = {};
290
- }
291
-
292
- jsonData.attributes.style = _objectSpread(_objectSpread({}, jsonData.attributes.style), additionalStyles);
293
- }
294
-
295
- return /*#__PURE__*/_react["default"].createElement(Tag, jsonData.attributes, children);
296
- }
297
- }
298
- }
299
- }
300
- }; // eslint-disable-next-line no-unused-vars
301
-
302
- var findPreviousText = function findPreviousText(el) {
303
- if (el.nodeName === '#text') {
304
- return el;
305
- }
306
-
307
- if (el.previousSibling) {
308
- return findPreviousText(el.previousSibling);
309
- }
310
-
311
- return null;
312
- };
313
-
314
- var TEXT_RULE = {
315
- deserialize: function deserialize(el) {
316
- /**
317
- * This needs to be called on the dom element in order to merge the adjacent text nodes together
318
- * */
319
- el.normalize();
320
-
321
- if (el.tagName && el.tagName.toLowerCase() === 'br') {
322
- return {
323
- object: 'text',
324
- leaves: [{
325
- object: 'leaf',
326
- text: '\n'
327
- }]
328
- };
329
- }
330
-
331
- if (el.nodeName === '#text') {
332
- if (el.nodeValue && el.nodeValue.match(/<!--.*?-->/)) return;
333
- log('[text:deserialize] return text object..');
334
- return {
335
- object: 'text',
336
- leaves: [{
337
- object: 'leaf',
338
- text: el.nodeValue
339
- }]
340
- };
341
- }
342
- },
343
- serialize: function serialize(obj, children) {
344
- if (obj.object === 'string') {
345
- return children.split('\n').reduce(function (array, text, i) {
346
- if (i !== 0) array.push( /*#__PURE__*/_react["default"].createElement("br", null));
347
- array.push(text);
348
- return array;
349
- }, []);
350
- }
351
- }
352
- };
353
- exports.TEXT_RULE = TEXT_RULE;
354
- var RULES = [_list.serialization, _math.serialization, _media.serialization, _image.serialization, _table.serialization, _respArea.serialization, TEXT_RULE, inlines, blocks, marks];
355
-
356
- function allWhitespace(node) {
357
- // Use ECMA-262 Edition 3 String and RegExp features
358
- return !/[^\t\n\r ]/.test(node.textContent);
359
- }
360
-
361
- function defaultParseHtml(html) {
362
- if (typeof DOMParser === 'undefined') {
363
- throw new Error('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.');
364
- }
365
-
366
- var parsed = new DOMParser().parseFromString(html, 'text/html');
367
- var body = parsed.body;
368
- var textNodes = document.createTreeWalker(body, NodeFilter.SHOW_TEXT, null, null);
369
- var n = textNodes.nextNode();
370
-
371
- while (n) {
372
- var _n$nextSibling, _n$nextSibling$tagNam, _n$previousSibling, _n$previousSibling$ta;
373
-
374
- var isWhiteSpace = allWhitespace(n);
375
- var isNotNearMarkup = !MARK_TAGS[(_n$nextSibling = n.nextSibling) === null || _n$nextSibling === void 0 ? void 0 : (_n$nextSibling$tagNam = _n$nextSibling.tagName) === null || _n$nextSibling$tagNam === void 0 ? void 0 : _n$nextSibling$tagNam.toLowerCase()] && !MARK_TAGS[(_n$previousSibling = n.previousSibling) === null || _n$previousSibling === void 0 ? void 0 : (_n$previousSibling$ta = _n$previousSibling.tagName) === null || _n$previousSibling$ta === void 0 ? void 0 : _n$previousSibling$ta.toLowerCase()];
376
-
377
- if (isWhiteSpace && isNotNearMarkup || n.nodeValue === "\u200B") {
378
- n.parentNode.removeChild(n);
379
- }
380
-
381
- n = textNodes.nextNode();
382
- }
383
-
384
- return body;
385
- }
386
- /** If this lib is used on the server side, we need to bypass using the DOMParser - just put in a stub. */
387
-
388
-
389
- var parseHtml = typeof window === 'undefined' ? function () {
390
- return {
391
- childNodes: []
392
- };
393
- } : defaultParseHtml;
394
- var serializer = new _slateHtmlSerializer["default"]({
395
- defaultBlock: 'div',
396
- rules: RULES,
397
- parseHtml: parseHtml
398
- });
399
-
400
- var _extends = Object.assign || function (target) {
401
- for (var i = 1; i < arguments.length; i++) {
402
- var source = arguments[i];
403
-
404
- for (var key in source) {
405
- if (Object.prototype.hasOwnProperty.call(source, key)) {
406
- target[key] = source[key];
407
- }
408
- }
409
- }
410
-
411
- return target;
412
- };
413
- /**
414
- * This is needed in order to override the function that eventually leads
415
- * to the max iteration of 12, which in most cases it's not enough. The newer versions
416
- * have a different way to calculate this, but updating to a newer version of slate
417
- * requires a lot of work fixing other issues. So we just increase the rules by 1000,
418
- * which means a lot of iterations.
419
- * Below is the code that calculates the max iterations.
420
- * var max = schema.stack.plugins.length + schema.rules.length + 1;
421
- */
422
-
423
-
424
- serializer.deserialize = function deserialize(html) {
425
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
426
- var _options$toJSON = options.toJSON;
427
- var toJSON = _options$toJSON === undefined ? false : _options$toJSON;
428
- var defaultBlock = this.defaultBlock;
429
- var parseHtml = this.parseHtml;
430
- var fragment = parseHtml(html);
431
- var children = Array.from(fragment.childNodes);
432
- var nodes = this.deserializeElements(children); // COMPAT: ensure that all top-level inline nodes are wrapped into a block.
433
-
434
- nodes = nodes.reduce(function (memo, node, i, original) {
435
- if (node.object === 'block') {
436
- memo.push(node);
437
- return memo;
438
- }
439
-
440
- if (i > 0 && original[i - 1].object !== 'block') {
441
- var _block = memo[memo.length - 1];
442
-
443
- _block.nodes.push(node);
444
-
445
- return memo;
446
- }
447
-
448
- var block = _extends({
449
- object: 'block',
450
- data: {},
451
- isVoid: false
452
- }, defaultBlock, {
453
- nodes: [node]
454
- });
455
-
456
- memo.push(block);
457
- return memo;
458
- }, []);
459
-
460
- if (nodes.length === 0) {
461
- nodes = [_extends({
462
- object: 'block',
463
- data: {},
464
- isVoid: false
465
- }, defaultBlock, {
466
- nodes: [{
467
- object: 'text',
468
- leaves: [{
469
- object: 'leaf',
470
- text: '',
471
- marks: []
472
- }]
473
- }]
474
- })];
475
- }
476
-
477
- var json = {
478
- object: 'value',
479
- document: {
480
- object: 'document',
481
- data: {},
482
- nodes: nodes
483
- },
484
- schema: {
485
- rules: []
486
- }
487
- };
488
- var i;
489
-
490
- for (i = 0; i < 3000; i++) {
491
- json.schema.rules.push({
492
- match: {
493
- object: 'document'
494
- },
495
- nodes: [{
496
- match: {
497
- object: 'block'
498
- }
499
- }]
500
- });
501
- }
502
-
503
- var ret = toJSON ? json : _slate.Value.fromJSON(json);
504
-
505
- if (ret) {
506
- return ret;
507
- }
508
-
509
- return null;
510
- };
511
-
512
- var reduceMultipleBrs = function reduceMultipleBrs(markup) {
513
- try {
514
- return markup.replace(/(<br\s*\/?>){3,}/gi, '<br>');
515
- } catch (e) {
516
- // eslint-disable-next-line no-console
517
- console.log("Couldn't remove <br/> tags: ", e);
518
- }
519
-
520
- return markup;
521
- };
522
-
523
- exports.reduceMultipleBrs = reduceMultipleBrs;
524
-
525
- var reduceRedundantNewLineCharacters = function reduceRedundantNewLineCharacters(markup) {
526
- try {
527
- return markup.replace(/\n/gi, '');
528
- } catch (e) {
529
- // eslint-disable-next-line no-console
530
- console.log("Couldn't remove <br/> tags: ", e);
531
- }
532
-
533
- return markup;
534
- };
535
- /**
536
- * Makes sure that the html provided respects the schema
537
- * rules for the slate editor.
538
- * @param markup
539
- * @returns {string}
540
- */
541
-
542
-
543
- var fixHtmlCode = function fixHtmlCode(markup) {
544
- var wrapperEl = document.createElement('div');
545
- wrapperEl.innerHTML = markup;
546
- /**
547
- * DIV elements that are at the same level as paragraphs
548
- * are replaced with P elements for normalizing purposes
549
- * @param child
550
- */
551
-
552
- var fixParagraphs = function fixParagraphs(child) {
553
- var p = document.createElement('p');
554
- p.innerHTML = child.innerHTML;
555
- Array.from(child.attributes).forEach(function (attr) {
556
- p.setAttribute(attr.name, attr.value);
557
- });
558
- child.replaceWith(p);
559
- };
560
- /**
561
- * @summary Makes sure that tables are placed in the root document.
562
- * @description This function removes the tables from the nodes that are
563
- * placed inside the root element and places them exactly near
564
- * the parent element.
565
- * @param tableArray
566
- */
567
-
568
-
569
- var fixTables = function fixTables(tableArray) {
570
- tableArray.forEach(function (el) {
571
- var index = el.index,
572
- child = el.child,
573
- parent = el.parent;
574
- var nodesBefore = [];
575
- var nodesAfter = [];
576
- var allNodes = Array.from(parent.childNodes);
577
- var i;
578
-
579
- for (i = 0; i < allNodes.length; i++) {
580
- var node = allNodes[i];
581
-
582
- if (i < index) {
583
- nodesBefore.push(node);
584
- } else if (i > index) {
585
- nodesAfter.push(node);
586
- }
587
- } // creating the element that is going to be placed instead of the parent
588
-
589
-
590
- var beforeEl = document.createElement(parent.nodeName);
591
- beforeEl.append.apply(beforeEl, nodesBefore); // replacing parent with the beforeElement
592
-
593
- parent.replaceWith(beforeEl); // adding the table right after the before element
594
-
595
- beforeEl.after(child); // creating the element that is going to be placed after the table
596
-
597
- var afterEl = document.createElement(parent.nodeName);
598
- afterEl.append.apply(afterEl, nodesAfter); // adding the after element near the table
599
-
600
- child.after(afterEl);
601
- });
602
- };
603
-
604
- var emptyBlockCheck = function emptyBlockCheck(node) {
605
- return (node.nodeName === 'DIV' || node.nodeName === 'P' || node.nodeName === 'LI') && node.childNodes.length === 0;
606
- };
607
-
608
- var parseNode = function parseNode(el) {
609
- var childArray = Array.from(el.childNodes);
610
- var hasParagraphs = childArray.find(function (child) {
611
- return child.nodeName === 'P';
612
- });
613
- var tables = [];
614
- childArray.forEach(function (child, index) {
615
- // removing empty blocks
616
- if (emptyBlockCheck(child)) {
617
- child.remove();
618
- return;
619
- }
620
-
621
- if (hasParagraphs && child.nodeName === 'DIV') {
622
- fixParagraphs(child);
623
- }
624
-
625
- if (wrapperEl !== el && child.nodeName === 'TABLE') {
626
- // we don't need to fix tables in the root element
627
- tables.push({
628
- index: index,
629
- child: child,
630
- parent: el
631
- });
632
- }
633
-
634
- parseNode(child);
635
- });
636
-
637
- if (tables.length) {
638
- fixTables(tables);
639
- }
640
- };
641
-
642
- parseNode(wrapperEl);
643
- return wrapperEl.innerHTML;
644
- };
645
-
646
- var handleHtml = function handleHtml(html) {
647
- return fixHtmlCode(reduceRedundantNewLineCharacters(reduceMultipleBrs(html)));
648
- };
649
-
650
- exports.handleHtml = handleHtml;
651
-
652
- var htmlToValue = function htmlToValue(html) {
653
- try {
654
- return serializer.deserialize(handleHtml(html));
655
- } catch (e) {
656
- // eslint-disable-next-line no-console
657
- console.log("Couldn't parse html: ", e);
658
- return {};
659
- }
660
- };
661
-
662
- exports.htmlToValue = htmlToValue;
663
-
664
- var valueToHtml = function valueToHtml(value) {
665
- return serializer.serialize(value);
666
- };
667
- /**
668
- *
669
- * <div><div>a</div></div> -> <div>a</div>
670
- *
671
- * <div><div>a</div><div>b</div></div> -> <div>a</div><div>b</div>
672
- * <div><div>a</div>4444<div>b</div></div> -> <div>a</div>4444<div>b</div>
673
- */
674
-
675
-
676
- exports.valueToHtml = valueToHtml;
677
- //# sourceMappingURL=serialization.js.map