@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.
Files changed (118) hide show
  1. package/CHANGELOG.json +1 -1
  2. package/CHANGELOG.md +81 -0
  3. package/LICENSE.md +5 -0
  4. package/lib/editor.js +410 -543
  5. package/lib/editor.js.map +1 -1
  6. package/lib/index.js +200 -101
  7. package/lib/index.js.map +1 -1
  8. package/lib/parse-html.js +5 -6
  9. package/lib/parse-html.js.map +1 -1
  10. package/lib/plugins/characters/custom-popper.js +12 -2
  11. package/lib/plugins/characters/custom-popper.js.map +1 -1
  12. package/lib/plugins/characters/index.js +71 -19
  13. package/lib/plugins/characters/index.js.map +1 -1
  14. package/lib/plugins/characters/utils.js.map +1 -1
  15. package/lib/plugins/html/icons/index.js +38 -0
  16. package/lib/plugins/html/icons/index.js.map +1 -0
  17. package/lib/plugins/html/index.js +75 -0
  18. package/lib/plugins/html/index.js.map +1 -0
  19. package/lib/plugins/image/alt-dialog.js +26 -0
  20. package/lib/plugins/image/alt-dialog.js.map +1 -1
  21. package/lib/plugins/image/component.js +124 -90
  22. package/lib/plugins/image/component.js.map +1 -1
  23. package/lib/plugins/image/image-toolbar.js +45 -7
  24. package/lib/plugins/image/image-toolbar.js.map +1 -1
  25. package/lib/plugins/image/index.js +91 -113
  26. package/lib/plugins/image/index.js.map +1 -1
  27. package/lib/plugins/image/insert-image-handler.js +54 -72
  28. package/lib/plugins/image/insert-image-handler.js.map +1 -1
  29. package/lib/plugins/index.js +71 -31
  30. package/lib/plugins/index.js.map +1 -1
  31. package/lib/plugins/list/index.js +129 -58
  32. package/lib/plugins/list/index.js.map +1 -1
  33. package/lib/plugins/math/index.js +152 -118
  34. package/lib/plugins/math/index.js.map +1 -1
  35. package/lib/plugins/media/index.js +185 -168
  36. package/lib/plugins/media/index.js.map +1 -1
  37. package/lib/plugins/media/media-dialog.js +197 -110
  38. package/lib/plugins/media/media-dialog.js.map +1 -1
  39. package/lib/plugins/media/media-toolbar.js +24 -4
  40. package/lib/plugins/media/media-toolbar.js.map +1 -1
  41. package/lib/plugins/media/media-wrapper.js +65 -23
  42. package/lib/plugins/media/media-wrapper.js.map +1 -1
  43. package/lib/plugins/respArea/drag-in-the-blank/choice.js +50 -10
  44. package/lib/plugins/respArea/drag-in-the-blank/choice.js.map +1 -1
  45. package/lib/plugins/respArea/drag-in-the-blank/index.js +22 -9
  46. package/lib/plugins/respArea/drag-in-the-blank/index.js.map +1 -1
  47. package/lib/plugins/respArea/explicit-constructed-response/index.js +9 -4
  48. package/lib/plugins/respArea/explicit-constructed-response/index.js.map +1 -1
  49. package/lib/plugins/respArea/icons/index.js +18 -1
  50. package/lib/plugins/respArea/icons/index.js.map +1 -1
  51. package/lib/plugins/respArea/index.js +133 -122
  52. package/lib/plugins/respArea/index.js.map +1 -1
  53. package/lib/plugins/respArea/inline-dropdown/index.js +10 -4
  54. package/lib/plugins/respArea/inline-dropdown/index.js.map +1 -1
  55. package/lib/plugins/respArea/utils.js +33 -15
  56. package/lib/plugins/respArea/utils.js.map +1 -1
  57. package/lib/plugins/table/icons/index.js +7 -0
  58. package/lib/plugins/table/icons/index.js.map +1 -1
  59. package/lib/plugins/table/index.js +279 -390
  60. package/lib/plugins/table/index.js.map +1 -1
  61. package/lib/plugins/table/table-toolbar.js +47 -14
  62. package/lib/plugins/table/table-toolbar.js.map +1 -1
  63. package/lib/plugins/toolbar/default-toolbar.js +63 -51
  64. package/lib/plugins/toolbar/default-toolbar.js.map +1 -1
  65. package/lib/plugins/toolbar/done-button.js +9 -1
  66. package/lib/plugins/toolbar/done-button.js.map +1 -1
  67. package/lib/plugins/toolbar/editor-and-toolbar.js +140 -83
  68. package/lib/plugins/toolbar/editor-and-toolbar.js.map +1 -1
  69. package/lib/plugins/toolbar/index.js +5 -0
  70. package/lib/plugins/toolbar/index.js.map +1 -1
  71. package/lib/plugins/toolbar/toolbar-buttons.js +39 -8
  72. package/lib/plugins/toolbar/toolbar-buttons.js.map +1 -1
  73. package/lib/plugins/toolbar/toolbar.js +261 -225
  74. package/lib/plugins/toolbar/toolbar.js.map +1 -1
  75. package/lib/plugins/utils.js +16 -19
  76. package/lib/plugins/utils.js.map +1 -1
  77. package/lib/serialization.js +70 -11
  78. package/lib/serialization.js.map +1 -1
  79. package/lib/theme.js.map +1 -1
  80. package/package.json +18 -17
  81. package/src/editor.jsx +139 -434
  82. package/src/index.jsx +96 -62
  83. package/src/plugins/characters/index.jsx +17 -12
  84. package/src/plugins/html/icons/index.jsx +19 -0
  85. package/src/plugins/html/index.jsx +68 -0
  86. package/src/plugins/image/component.jsx +38 -60
  87. package/src/plugins/image/index.jsx +42 -95
  88. package/src/plugins/image/insert-image-handler.js +27 -62
  89. package/src/plugins/index.jsx +39 -21
  90. package/src/plugins/list/index.jsx +90 -62
  91. package/src/plugins/math/index.jsx +70 -93
  92. package/src/plugins/media/index.jsx +117 -146
  93. package/src/plugins/media/media-dialog.js +9 -10
  94. package/src/plugins/media/media-wrapper.jsx +27 -29
  95. package/src/plugins/respArea/drag-in-the-blank/index.jsx +4 -5
  96. package/src/plugins/respArea/explicit-constructed-response/index.jsx +1 -2
  97. package/src/plugins/respArea/index.jsx +84 -114
  98. package/src/plugins/respArea/inline-dropdown/index.jsx +2 -3
  99. package/src/plugins/respArea/utils.jsx +28 -23
  100. package/src/plugins/table/index.jsx +214 -334
  101. package/src/plugins/table/table-toolbar.jsx +4 -3
  102. package/src/plugins/toolbar/default-toolbar.jsx +30 -48
  103. package/src/plugins/toolbar/editor-and-toolbar.jsx +114 -114
  104. package/src/plugins/toolbar/toolbar.jsx +224 -254
  105. package/src/plugins/utils.js +0 -16
  106. package/src/serialization.jsx +1 -1
  107. package/lib/components.js +0 -92
  108. package/lib/components.js.map +0 -1
  109. package/lib/new-serialization.js +0 -280
  110. package/lib/new-serialization.js.map +0 -1
  111. package/lib/plugins/hotKeys/index.js +0 -60
  112. package/lib/plugins/hotKeys/index.js.map +0 -1
  113. package/lib/test-serializer.js +0 -138
  114. package/lib/test-serializer.js.map +0 -1
  115. package/src/components.js +0 -135
  116. package/src/new-serialization.jsx +0 -310
  117. package/src/plugins/hotKeys/index.js +0 -54
  118. package/src/test-serializer.js +0 -132
package/lib/editor.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
4
5
  var _typeof = require("@babel/runtime/helpers/typeof");
6
+
5
7
  Object.defineProperty(exports, "__esModule", {
6
8
  value: true
7
9
  });
@@ -17,356 +19,69 @@ Object.defineProperty(exports, "DEFAULT_PLUGINS", {
17
19
  return _plugins.DEFAULT_PLUGINS;
18
20
  }
19
21
  });
20
- exports.serialization = exports["default"] = exports.EditorComponent = void 0;
22
+ exports.serialization = exports["default"] = exports.Editor = void 0;
23
+
21
24
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
25
+
22
26
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
27
+
23
28
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
29
+
24
30
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
31
+
25
32
  var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
33
+
26
34
  var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
35
+
27
36
  var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
37
+
28
38
  var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
39
+
29
40
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
30
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
31
- var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
32
- var _react = _interopRequireWildcard(require("react"));
41
+
33
42
  var _slateReact = require("slate-react");
34
- var _RootRef = _interopRequireDefault(require("@material-ui/core/RootRef"));
43
+
44
+ var _slatePropTypes = _interopRequireDefault(require("slate-prop-types"));
45
+
35
46
  var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
36
- var serialization = _interopRequireWildcard(require("./new-serialization"));
47
+
48
+ var serialization = _interopRequireWildcard(require("./serialization"));
49
+
37
50
  exports.serialization = serialization;
51
+
38
52
  var _propTypes = _interopRequireDefault(require("prop-types"));
53
+
54
+ var _react = _interopRequireDefault(require("react"));
55
+
39
56
  var _slate = require("slate");
57
+
40
58
  var _plugins = require("./plugins");
59
+
41
60
  var _debug = _interopRequireDefault(require("debug"));
61
+
42
62
  var _styles = require("@material-ui/core/styles");
63
+
43
64
  var _classnames = _interopRequireDefault(require("classnames"));
65
+
44
66
  var _renderUi = require("@pie-lib/render-ui");
67
+
45
68
  var _slatePlainSerializer = _interopRequireDefault(require("slate-plain-serializer"));
69
+
70
+ var _configUi = require("@pie-lib/config-ui");
71
+
46
72
  var _insertImageHandler = _interopRequireDefault(require("./plugins/image/insert-image-handler"));
47
- var _isHotkey = _interopRequireDefault(require("is-hotkey"));
48
- var _components = require("./components");
49
- var _editorAndToolbar = _interopRequireDefault(require("./plugins/toolbar/editor-and-toolbar"));
73
+
50
74
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
75
+
51
76
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
77
+
52
78
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
79
+
53
80
  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
54
- 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; }
55
- 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; }
56
- window.Path = _slate.Path;
57
- window.SlateNode = _slate.Node;
58
- window.ReactEditor = _slateReact.ReactEditor;
59
- window.Editor = _slate.Editor;
60
- var HOTKEYS = {
61
- 'mod+b': 'bold',
62
- 'mod+i': 'italic',
63
- 'mod+u': 'underline',
64
- 'mod+`': 'code'
65
- };
66
- var LIST_TYPES = ['numbered-list', 'bulleted-list'];
67
- var TEXT_ALIGN_TYPES = ['left', 'center', 'right', 'justify'];
68
- var initialValue = [{
69
- type: 'paragraph',
70
- children: [{
71
- type: 'math',
72
- data: {
73
- latex: '\\frac{1}{2}',
74
- wrapper: 'round_brackets'
75
- },
76
- children: [{
77
- text: '\\(\\frac{1}{2}\\)'
78
- }]
79
- }]
80
- }];
81
- var SlateEditor = function SlateEditor(editorProps) {
82
- var mounted = (0, _react.useRef)(false);
83
- var autoFocus = editorProps.autoFocus,
84
- value = editorProps.value,
85
- plugins = editorProps.plugins,
86
- actionsRef = editorProps.actionsRef,
87
- onEditingDone = editorProps.onEditingDone;
88
- var renderElement = (0, _react.useCallback)(function (props) {
89
- return /*#__PURE__*/_react["default"].createElement(Element, (0, _extends2["default"])({}, props, {
90
- plugins: plugins
91
- }));
92
- }, []);
93
- var renderLeaf = (0, _react.useCallback)(function (props) {
94
- return /*#__PURE__*/_react["default"].createElement(Leaf, props);
95
- }, []);
96
- var editor = (0, _react.useMemo)(function () {
97
- return (0, _plugins.withPlugins)((0, _slate.createEditor)(), plugins);
98
- }, []);
99
- var _useState = (0, _react.useState)(false),
100
- _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
101
- isFocused = _useState2[0],
102
- setIsFocused = _useState2[1];
103
- var editorRef = (0, _react.useRef)(null);
104
- (0, _react.useEffect)(function () {
105
- mounted.current = true;
106
- return function () {
107
- mounted.current = false;
108
- };
109
- }, []);
110
- (0, _react.useEffect)(function () {
111
- if (editorProps.onEditor) {
112
- editorProps.onEditor(editor);
113
- }
114
- if (autoFocus) {
115
- _slate.Transforms.select(editor, [0, 0]);
116
- _slateReact.ReactEditor.focus(editor);
117
- if (mounted.current) {
118
- setIsFocused(true);
119
- }
120
- }
121
- }, [editor]);
122
- var slateValue = (0, _react.useMemo)(function () {
123
- // Slate throws an error if the value on the initial render is invalid
124
- // so we directly set the value on the editor in order
125
- // to be able to trigger normalization on the initial value before rendering
126
- editor.children = value;
127
- editor.marks = {};
128
- _slate.Editor.normalize(editor, {
129
- force: true
130
- });
131
- // We return the normalized internal value so that the rendering can take over from here
132
- return editor.children;
133
- }, [editor, value]);
134
- window.editor = editor;
135
- var onKeyDown = function onKeyDown(event) {
136
- if (event.key === 'Enter' && event.shiftKey === true) {
137
- editor.insertText('\n');
138
- event.preventDefault();
139
- event.stopPropagation();
140
- return;
141
- }
142
- for (var hotkey in HOTKEYS) {
143
- if ((0, _isHotkey["default"])(hotkey, event)) {
144
- event.preventDefault();
145
- var mark = HOTKEYS[hotkey];
146
- toggleMark(editor, mark);
147
- }
148
- }
149
- };
150
- var onFocus = function onFocus() {
151
- return setIsFocused(true);
152
- };
153
- var onBlur = function onBlur(e) {
154
- setTimeout(function () {
155
- if (!editorRef.current || !editorRef.current.contains(document.activeElement)) {
156
- if (editorProps.onBlur) {
157
- editorProps.onBlur(e);
158
- }
159
- if (mounted.current) {
160
- setIsFocused(false);
161
- }
162
- }
163
- }, 50);
164
- };
165
- var actions = {
166
- focus: function focus(position, node) {
167
- var _ref = node ? _slate.Editor.leaf(editor, _slateReact.ReactEditor.findPath(editor, node), {
168
- edge: 'end'
169
- }) : _slate.Editor.leaf(editor, [0], {
170
- edge: 'end'
171
- }),
172
- _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
173
- textPath = _ref2[1];
174
- _slate.Transforms.select(editor, textPath);
175
- _slateReact.ReactEditor.focus(editor);
176
- },
177
- finishEditing: function finishEditing() {
178
- // if (!mounted.current) {
179
- // return;
180
- // }
181
81
 
182
- if (typeof onEditingDone === 'function') {
183
- onEditingDone(editor);
184
- }
185
- }
186
- };
187
- if (actionsRef) {
188
- actionsRef(actions);
189
- }
190
- return /*#__PURE__*/_react["default"].createElement(_slateReact.Slate, {
191
- editor: editor,
192
- initialValue: slateValue
193
- }, /*#__PURE__*/_react["default"].createElement(_RootRef["default"], {
194
- rootRef: editorRef
195
- }, /*#__PURE__*/_react["default"].createElement(_editorAndToolbar["default"], (0, _extends2["default"])({}, editorProps, {
196
- editor: editor,
197
- isFocused: isFocused,
198
- onDone: function onDone() {
199
- setIsFocused(false);
200
- document.activeElement.blur();
201
- editorProps.onDone(editor);
202
- }
203
- }), /*#__PURE__*/_react["default"].createElement(_slateReact.Editable, {
204
- renderElement: renderElement,
205
- renderLeaf: renderLeaf,
206
- placeholder: "Enter some rich text\u2026",
207
- spellCheck: true,
208
- onKeyDown: onKeyDown,
209
- onFocus: onFocus,
210
- onBlur: onBlur
211
- }))));
212
- };
213
- var toggleBlock = function toggleBlock(editor, format) {
214
- var isActive = isBlockActive(editor, format, TEXT_ALIGN_TYPES.includes(format) ? 'align' : 'type');
215
- var isList = LIST_TYPES.includes(format);
216
- _slate.Transforms.unwrapNodes(editor, {
217
- match: function match(n) {
218
- return !_slate.Editor.isEditor(n) && _slate.Element.isElement(n) && LIST_TYPES.includes(n.type) && !TEXT_ALIGN_TYPES.includes(format);
219
- },
220
- split: true
221
- });
222
- var newProperties;
223
- if (TEXT_ALIGN_TYPES.includes(format)) {
224
- newProperties = {
225
- align: isActive ? undefined : format
226
- };
227
- } else {
228
- newProperties = {
229
- type: isActive ? 'paragraph' : isList ? 'list_item' : format
230
- };
231
- }
232
- _slate.Transforms.setNodes(editor, newProperties);
233
- if (!isActive && isList) {
234
- var block = {
235
- type: format,
236
- children: []
237
- };
238
- _slate.Transforms.wrapNodes(editor, block);
239
- }
240
- };
241
- var toggleMark = function toggleMark(editor, format) {
242
- var isActive = isMarkActive(editor, format);
243
- if (isActive) {
244
- _slate.Editor.removeMark(editor, format);
245
- } else {
246
- _slate.Editor.addMark(editor, format, true);
247
- }
248
- };
249
- var isBlockActive = function isBlockActive(editor, format) {
250
- var blockType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'type';
251
- var selection = editor.selection;
252
- if (!selection) return false;
253
- var _Array$from = Array.from(_slate.Editor.nodes(editor, {
254
- at: _slate.Editor.unhangRange(editor, selection),
255
- match: function match(n) {
256
- return !_slate.Editor.isEditor(n) && _slate.Element.isElement(n) && n[blockType] === format;
257
- }
258
- })),
259
- _Array$from2 = (0, _slicedToArray2["default"])(_Array$from, 1),
260
- match = _Array$from2[0];
261
- return !!match;
262
- };
263
- var isMarkActive = function isMarkActive(editor, format) {
264
- var marks = _slate.Editor.marks(editor);
265
- return marks ? marks[format] === true : false;
266
- };
267
- var Element = function Element(props) {
268
- var editor = (0, _slateReact.useSlateStatic)();
269
- var focused = (0, _slateReact.useFocused)();
270
- var attributes = props.attributes,
271
- children = props.children,
272
- element = props.element,
273
- plugins = props.plugins;
274
- var style = {
275
- textAlign: element.align
276
- };
277
- var nodeProps = _objectSpread(_objectSpread(_objectSpread({}, attributes), props), {}, {
278
- node: _objectSpread({}, element),
279
- children: children
280
- });
281
- var pluginToRender = plugins.find(function (plugin) {
282
- return typeof plugin.supports === 'function' && plugin.supports(element);
283
- });
284
- if (pluginToRender) {
285
- return pluginToRender.renderNode(_objectSpread(_objectSpread({}, nodeProps), {}, {
286
- editor: editor,
287
- focused: focused
288
- }));
289
- }
290
- switch (element.type) {
291
- case 'block-quote':
292
- return /*#__PURE__*/_react["default"].createElement("blockquote", (0, _extends2["default"])({
293
- style: style
294
- }, attributes), children);
295
- case 'bulleted-list':
296
- return /*#__PURE__*/_react["default"].createElement("ul", (0, _extends2["default"])({
297
- style: style
298
- }, attributes), children);
299
- case 'heading-one':
300
- return /*#__PURE__*/_react["default"].createElement("h1", (0, _extends2["default"])({
301
- style: style
302
- }, attributes), children);
303
- case 'heading-two':
304
- return /*#__PURE__*/_react["default"].createElement("h2", (0, _extends2["default"])({
305
- style: style
306
- }, attributes), children);
307
- case 'list-item':
308
- return /*#__PURE__*/_react["default"].createElement("li", (0, _extends2["default"])({
309
- style: style
310
- }, attributes), children);
311
- case 'numbered-list':
312
- return /*#__PURE__*/_react["default"].createElement("ol", (0, _extends2["default"])({
313
- style: style
314
- }, attributes), children);
315
- default:
316
- return /*#__PURE__*/_react["default"].createElement("div", (0, _extends2["default"])({
317
- style: _objectSpread(_objectSpread({}, style), {}, {
318
- margin: 0
319
- })
320
- }, attributes), children);
321
- }
322
- };
323
- var Leaf = function Leaf(_ref3) {
324
- var attributes = _ref3.attributes,
325
- children = _ref3.children,
326
- leaf = _ref3.leaf;
327
- if (leaf.bold) {
328
- children = /*#__PURE__*/_react["default"].createElement("strong", null, children);
329
- }
330
- if (leaf.code) {
331
- children = /*#__PURE__*/_react["default"].createElement("code", null, children);
332
- }
333
- if (leaf.italic) {
334
- children = /*#__PURE__*/_react["default"].createElement("em", null, children);
335
- }
336
- if (leaf.underline) {
337
- children = /*#__PURE__*/_react["default"].createElement("u", null, children);
338
- }
339
- if (leaf.strikethrough) {
340
- children = /*#__PURE__*/_react["default"].createElement("del", null, children);
341
- }
342
- return /*#__PURE__*/_react["default"].createElement("span", attributes, children);
343
- };
344
- var BlockButton = function BlockButton(_ref4) {
345
- var format = _ref4.format,
346
- icon = _ref4.icon;
347
- var editor = (0, _slateReact.useSlate)();
348
- return /*#__PURE__*/_react["default"].createElement(_components.Button, {
349
- active: isBlockActive(editor, format, TEXT_ALIGN_TYPES.includes(format) ? 'align' : 'type'),
350
- onMouseDown: function onMouseDown(event) {
351
- event.preventDefault();
352
- toggleBlock(editor, format);
353
- }
354
- }, /*#__PURE__*/_react["default"].createElement(_components.Icon, null, icon));
355
- };
356
- var MarkButton = function MarkButton(_ref5) {
357
- var format = _ref5.format,
358
- icon = _ref5.icon;
359
- var editor = (0, _slateReact.useSlate)();
360
- return /*#__PURE__*/_react["default"].createElement(_components.Button, {
361
- active: isMarkActive(editor, format),
362
- onMouseDown: function onMouseDown(event) {
363
- event.preventDefault();
364
- toggleMark(editor, format);
365
- }
366
- }, /*#__PURE__*/_react["default"].createElement(_components.Icon, null, icon));
367
- };
82
+ 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; }
368
83
 
369
- // old-editable
84
+ 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; }
370
85
 
371
86
  var log = (0, _debug["default"])('editable-html:editor');
372
87
  var defaultToolbarOpts = {
@@ -382,26 +97,63 @@ var defaultResponseAreaProps = {
382
97
  onHandleAreaChange: function onHandleAreaChange() {}
383
98
  };
384
99
  var defaultLanguageCharactersProps = [];
385
- var createToolbarOpts = function createToolbarOpts(toolbarOpts, error) {
100
+
101
+ var createToolbarOpts = function createToolbarOpts(toolbarOpts, error, isHtmlMode) {
386
102
  return _objectSpread(_objectSpread(_objectSpread({}, defaultToolbarOpts), toolbarOpts), {}, {
387
- error: error
103
+ error: error,
104
+ isHtmlMode: isHtmlMode
388
105
  });
389
106
  };
390
- var EditorComponent = /*#__PURE__*/function (_React$Component) {
391
- (0, _inherits2["default"])(EditorComponent, _React$Component);
392
- var _super = _createSuper(EditorComponent);
393
- function EditorComponent(_props) {
107
+
108
+ var Editor = /*#__PURE__*/function (_React$Component) {
109
+ (0, _inherits2["default"])(Editor, _React$Component);
110
+
111
+ var _super = _createSuper(Editor);
112
+
113
+ function Editor(_props) {
394
114
  var _this;
395
- (0, _classCallCheck2["default"])(this, EditorComponent);
115
+
116
+ (0, _classCallCheck2["default"])(this, Editor);
396
117
  _this = _super.call(this, _props);
118
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleAlertDialog", function (open, extraDialogProps, callback) {
119
+ _this.setState({
120
+ dialog: _objectSpread({
121
+ open: open
122
+ }, extraDialogProps),
123
+ isEdited: false
124
+ }, callback);
125
+ });
126
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "toggleHtmlMode", function () {
127
+ _this.setState(function (prevState) {
128
+ return {
129
+ isHtmlMode: !prevState.isHtmlMode
130
+ };
131
+ }, function () {
132
+ var error = _this.props.error;
133
+ var toolbarOpts = _this.state.toolbarOpts;
134
+ var newToolbarOpts = createToolbarOpts(toolbarOpts, error, _this.state.isHtmlMode);
135
+
136
+ _this.setState({
137
+ toolbarOpts: newToolbarOpts
138
+ });
139
+ });
140
+ });
397
141
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handlePlugins", function (props) {
398
142
  var normalizedResponseAreaProps = _objectSpread(_objectSpread({}, defaultResponseAreaProps), props.responseAreaProps);
143
+
144
+ var htmlPluginOpts = {
145
+ isHtmlMode: _this.state.isHtmlMode,
146
+ isEdited: _this.state.isEdited,
147
+ toggleHtmlMode: _this.toggleHtmlMode,
148
+ handleAlertDialog: _this.handleAlertDialog
149
+ };
399
150
  _this.plugins = (0, _plugins.buildPlugins)(props.activePlugins, {
400
151
  math: _objectSpread({
401
152
  onClick: _this.onMathClick,
402
153
  onFocus: _this.onPluginFocus,
403
154
  onBlur: _this.onPluginBlur
404
155
  }, props.mathMlOptions),
156
+ html: htmlPluginOpts,
405
157
  image: {
406
158
  disableImageAlignmentButtons: props.disableImageAlignmentButtons,
407
159
  onDelete: props.imageSupport && props.imageSupport["delete"] && function (src, done) {
@@ -431,15 +183,38 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
431
183
  */
432
184
  disableScrollbar: !!props.disableScrollbar,
433
185
  disableUnderline: props.disableUnderline,
434
- autoWidth: props.autoWidthToolbar
186
+ autoWidth: props.autoWidthToolbar,
187
+ onDone: function onDone() {
188
+ var _this$state$value$sta, _this$state$value$sta2;
189
+
190
+ var nonEmpty = props.nonEmpty;
191
+ log('[onDone]');
192
+
193
+ _this.setState({
194
+ toolbarInFocus: false,
195
+ focusedNode: null
196
+ });
197
+
198
+ _this.editor.blur();
199
+
200
+ if (nonEmpty && ((_this$state$value$sta = _this.state.value.startText) === null || _this$state$value$sta === void 0 ? void 0 : (_this$state$value$sta2 = _this$state$value$sta.text) === null || _this$state$value$sta2 === void 0 ? void 0 : _this$state$value$sta2.length) === 0) {
201
+ _this.resetValue(true).then(function () {
202
+ _this.onEditingDone();
203
+ });
204
+ } else {
205
+ _this.onEditingDone();
206
+ }
207
+ }
435
208
  },
436
209
  table: {
437
210
  onFocus: function onFocus() {
438
211
  log('[table:onFocus]...');
212
+
439
213
  _this.onPluginFocus();
440
214
  },
441
215
  onBlur: function onBlur() {
442
216
  log('[table:onBlur]...');
217
+
443
218
  _this.onPluginBlur();
444
219
  }
445
220
  },
@@ -452,20 +227,26 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
452
227
  error: normalizedResponseAreaProps.error,
453
228
  onFocus: function onFocus() {
454
229
  log('[table:onFocus]...');
230
+
455
231
  _this.onPluginFocus();
456
232
  },
457
233
  onBlur: function onBlur() {
458
234
  log('[table:onBlur]...');
235
+
459
236
  _this.onPluginBlur();
460
237
  }
461
238
  },
462
239
  languageCharacters: props.languageCharactersProps,
463
240
  media: {
464
241
  focus: _this.focus,
242
+ createChange: function createChange() {
243
+ return _this.state.value.change();
244
+ },
465
245
  onChange: _this.onChange,
466
246
  uploadSoundSupport: props.uploadSoundSupport
467
247
  }
468
248
  });
249
+
469
250
  if (props.mathMlOptions.mmlOutput || props.mathMlOptions.mmlEditing) {
470
251
  _this.props.runSerializationOnMarkup();
471
252
  }
@@ -475,6 +256,7 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
475
256
  var target = e && e.relatedTarget;
476
257
  var node = target ? (0, _slateReact.findNode)(target, _this.state.value) : null;
477
258
  log('[onPluginBlur] node: ', node);
259
+
478
260
  _this.setState({
479
261
  focusedNode: node
480
262
  }, function () {
@@ -484,10 +266,12 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
484
266
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onPluginFocus", function (e) {
485
267
  log('[onPluginFocus]', e && e.target);
486
268
  var target = e && e.target;
269
+
487
270
  if (target) {
488
271
  var node = (0, _slateReact.findNode)(target, _this.state.value);
489
272
  log('[onPluginFocus] node: ', node);
490
273
  var stashedValue = _this.state.stashedValue || _this.state.value;
274
+
491
275
  _this.setState({
492
276
  focusedNode: node,
493
277
  stashedValue: stashedValue
@@ -497,60 +281,70 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
497
281
  focusedNode: null
498
282
  });
499
283
  }
284
+
500
285
  _this.stashValue();
501
286
  });
502
287
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onMathClick", function (node) {
288
+ _this.editor.change(function (c) {
289
+ return c.collapseToStartOf(node);
290
+ });
291
+
503
292
  _this.setState({
504
293
  selectedNode: node
505
294
  });
506
295
  });
507
- (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onEditingDone", function (editor) {
296
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onEditingDone", function () {
508
297
  log('[onEditingDone]');
509
- // this.setState({ stashedValue: null, focusedNode: null });
510
- log('[onEditingDone] value: ', _this.state.value);
511
- _this.props.onChange(editor, true);
512
- });
513
- (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onDone", function (editor) {
514
- var nonEmpty = _this.props.nonEmpty;
515
- log('[onDone]');
298
+
516
299
  _this.setState({
517
- toolbarInFocus: false,
300
+ stashedValue: null,
518
301
  focusedNode: null
519
302
  });
520
- _this.onEditingDone(editor);
303
+
304
+ log('[onEditingDone] value: ', _this.state.value);
305
+
306
+ _this.props.onChange(_this.state.value, true);
521
307
  });
522
- // Allowing time for onChange to take effect if it is called
523
308
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "handleBlur", function (resolve) {
524
309
  var nonEmpty = _this.props.nonEmpty;
525
310
  var doneOn = _this.state.toolbarOpts.doneOn;
311
+
526
312
  _this.setState({
527
313
  toolbarInFocus: false,
528
314
  focusedNode: null
529
315
  });
316
+
317
+ if (_this.editor) {
318
+ _this.editor.blur();
319
+ }
320
+
530
321
  if (doneOn === 'blur') {
531
- var _this$state$value$sta;
532
- if (nonEmpty && ((_this$state$value$sta = _this.state.value.startText) === null || _this$state$value$sta === void 0 || (_this$state$value$sta = _this$state$value$sta.text) === null || _this$state$value$sta === void 0 ? void 0 : _this$state$value$sta.length) === 0) {
322
+ var _this$state$value$sta3, _this$state$value$sta4;
323
+
324
+ if (nonEmpty && ((_this$state$value$sta3 = _this.state.value.startText) === null || _this$state$value$sta3 === void 0 ? void 0 : (_this$state$value$sta4 = _this$state$value$sta3.text) === null || _this$state$value$sta4 === void 0 ? void 0 : _this$state$value$sta4.length) === 0) {
533
325
  _this.resetValue(true).then(function () {
534
326
  _this.onEditingDone();
327
+
535
328
  resolve();
536
329
  });
537
330
  } else {
538
331
  _this.onEditingDone();
332
+
539
333
  resolve();
540
334
  }
541
335
  }
542
336
  });
543
337
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onBlur", function (event) {
544
- return _this.props.onBlur(event);
545
338
  log('[onBlur]');
546
339
  var target = event.relatedTarget;
547
- var node = _slateReact.ReactEditor.toSlateNode(editor, target);
340
+ var node = target ? (0, _slateReact.findNode)(target, _this.state.value) : null;
548
341
  log('[onBlur] node: ', node);
549
342
  return new Promise(function (resolve) {
550
343
  _this.setState({
551
344
  preBlurValue: _this.state.value,
552
345
  focusedNode: !node ? null : node
553
346
  }, _this.handleBlur.bind((0, _assertThisInitialized2["default"])(_this), resolve));
347
+
554
348
  _this.props.onBlur(event);
555
349
  });
556
350
  });
@@ -558,44 +352,44 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
558
352
  var editorDOM = document.querySelector("[data-key=\"".concat(_this.state.value.document.key, "\"]"));
559
353
  setTimeout(function () {
560
354
  var stateValue = _this.state.value;
355
+
561
356
  if (!_this.wrapperRef) {
562
357
  return;
563
358
  }
359
+
564
360
  var editorElement = !editorDOM || document.activeElement.closest("[class*=\"".concat(editorDOM.className, "\"]"));
565
361
  var toolbarElement = !_this.toolbarRef || document.activeElement.closest("[class*=\"".concat(_this.toolbarRef.className, "\"]"));
362
+
566
363
  var isInCurrentComponent = _this.wrapperRef.contains(editorElement) || _this.wrapperRef.contains(toolbarElement);
364
+
567
365
  if (!isInCurrentComponent) {
568
366
  editorDOM.removeEventListener('blur', _this.handleDomBlur);
367
+
569
368
  if (stateValue.isFocused) {
570
369
  _this.onBlur(e);
571
370
  }
572
371
  }
573
372
  }, 50);
574
373
  });
575
- /*
576
- * Needs to be wrapped otherwise it causes issues because of race conditions
577
- * Known issue for slatejs. See: https://github.com/ianstormtaylor/slate/issues/2097
578
- * Using timeout I wasn't able to test this
579
- *
580
- * Note: The use of promises has been causing issues with MathQuill
581
- * */
582
374
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onFocus", function () {
583
375
  return new Promise(function (resolve) {
584
376
  var editorDOM = document.querySelector("[data-key=\"".concat(_this.state.value.document.key, "\"]"));
585
377
  log('[onFocus]', document.activeElement);
586
-
587
378
  /**
588
379
  * This is a temporary hack - @see changeData below for some more information.
589
380
  */
381
+
590
382
  if (_this.__TEMPORARY_CHANGE_DATA) {
591
383
  var _this$__TEMPORARY_CHA = _this.__TEMPORARY_CHANGE_DATA,
592
- key = _this$__TEMPORARY_CHA.key,
593
- data = _this$__TEMPORARY_CHA.data;
384
+ key = _this$__TEMPORARY_CHA.key,
385
+ data = _this$__TEMPORARY_CHA.data;
594
386
  var domEl = document.querySelector("[data-key=\"".concat(key, "\"]"));
387
+
595
388
  if (domEl) {
596
389
  var change = _this.state.value.change().setNodeByKey(key, {
597
390
  data: data
598
391
  });
392
+
599
393
  _this.setState({
600
394
  value: change.value
601
395
  }, function () {
@@ -603,46 +397,53 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
603
397
  });
604
398
  }
605
399
  }
606
-
607
400
  /**
608
401
  * This is needed just in case the browser decides to make the editor
609
402
  * lose focus without triggering the onBlur event (can happen in a few cases).
610
403
  * This will also trigger onBlur if the user clicks outside of the page when the editor
611
404
  * is focused.
612
405
  */
406
+
407
+
613
408
  if (editorDOM === document.activeElement) {
614
409
  editorDOM.removeEventListener('blur', _this.handleDomBlur);
615
410
  editorDOM.addEventListener('blur', _this.handleDomBlur);
616
411
  }
412
+
617
413
  _this.stashValue();
414
+
618
415
  _this.props.onFocus();
416
+
619
417
  resolve();
620
418
  });
621
419
  });
622
420
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "stashValue", function () {
623
421
  log('[stashValue]');
422
+
624
423
  if (!_this.state.stashedValue) {
625
424
  _this.setState({
626
425
  stashedValue: _this.state.value
627
426
  });
628
427
  }
629
428
  });
630
- /**
631
- * Reset the value if the user didn't click done.
632
- */
633
429
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "resetValue", function (force) {
634
430
  var _this$state = _this.state,
635
- value = _this$state.value,
636
- focusedNode = _this$state.focusedNode;
431
+ value = _this$state.value,
432
+ focusedNode = _this$state.focusedNode;
433
+
637
434
  var stopReset = _this.plugins.reduce(function (s, p) {
638
435
  return s || p.stopReset && p.stopReset(_this.state.value);
639
436
  }, false);
437
+
640
438
  log('[resetValue]', value.isFocused, focusedNode, 'stopReset: ', stopReset);
439
+
641
440
  if (_this.state.stashedValue && !value.isFocused && !focusedNode && !stopReset || force) {
642
441
  log('[resetValue] resetting...');
643
442
  log('stashed', _this.state.stashedValue.document.toObject());
644
443
  log('current', _this.state.value.document.toObject());
444
+
645
445
  var newValue = _slate.Value.fromJSON(_this.state.stashedValue.toJSON());
446
+
646
447
  log('newValue: ', newValue.document);
647
448
  return new Promise(function (resolve) {
648
449
  setTimeout(function () {
@@ -659,19 +460,32 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
659
460
  return Promise.resolve({});
660
461
  }
661
462
  });
662
- (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onChange", function (editor, done) {
463
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onChange", function (change, done) {
663
464
  log('[onChange]');
465
+ var value = change.value;
664
466
  var charactersLimit = _this.props.charactersLimit;
665
- var allText = _slate.Editor.string(editor, []);
666
- if (allText > charactersLimit) {
467
+
468
+ if (value && value.document && value.document.text && value.document.text.length > charactersLimit) {
667
469
  return;
668
470
  }
669
- var html = (0, serialization.valueToHtml)(editor);
670
- var value = (0, serialization.htmlToValue)(html);
471
+
472
+ if (!_this.state.isHtmlMode) {
473
+ _this.setState({
474
+ isEdited: false
475
+ });
476
+ }
477
+
478
+ if (_this.state.isHtmlMode && !(0, _isEqual["default"])(_this.state.value.document.text, value.document.text)) {
479
+ _this.setState({
480
+ isEdited: true
481
+ });
482
+ }
483
+
671
484
  _this.setState({
672
485
  value: value
673
486
  }, function () {
674
487
  log('[onChange], call done()');
488
+
675
489
  if (done) {
676
490
  done();
677
491
  }
@@ -681,12 +495,14 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
681
495
  if (_this.state.value.isFocused) {
682
496
  return _this.state.value;
683
497
  }
498
+
684
499
  return _this.state.preBlurValue;
685
500
  });
686
501
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "valueToSize", function (v) {
687
502
  if (!v) {
688
503
  return;
689
504
  }
505
+
690
506
  if (typeof v === 'string') {
691
507
  if (v.endsWith('%')) {
692
508
  return undefined;
@@ -697,9 +513,11 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
697
513
  return isNaN(value) ? value : "".concat(value, "px");
698
514
  }
699
515
  }
516
+
700
517
  if (typeof v === 'number') {
701
518
  return "".concat(v, "px");
702
519
  }
520
+
703
521
  return;
704
522
  });
705
523
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "validateNode", function (node) {
@@ -709,123 +527,175 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
709
527
  if (last.type !== 'image') return;
710
528
  log('[validateNode] last is image..');
711
529
  var parent = last.getParent(last.key);
530
+
712
531
  var p = _slate.Block.getParent(last.key);
532
+
713
533
  log('[validateNode] parent:', parent, p);
714
534
  return undefined;
715
535
  });
536
+ (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "changeData", function (key, data) {
537
+ log('[changeData]. .. ', key, data);
538
+ /**
539
+ * HACK ALERT: We should be calling setState here and storing the change data:
540
+ *
541
+ * <code>this.setState({changeData: { key, data}})</code>
542
+ * However this is causing issues with the Mathquill instance. The 'input' event stops firing on the element and no
543
+ * more changes get through. The issues seem to be related to the promises in onBlur/onFocus. But removing these
544
+ * brings it's own problems. A major clean up is planned for this component so I've decided to temporarily settle
545
+ * on this hack rather than spend more time on this.
546
+ */
547
+ // Uncomment this line to see the bug described above.
548
+ // this.setState({changeData: {key, data}})
549
+
550
+ _this.__TEMPORARY_CHANGE_DATA = {
551
+ key: key,
552
+ data: data
553
+ };
554
+ });
716
555
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "focus", function (pos, node) {
717
556
  var position = pos || 'end';
557
+
718
558
  _this.props.focus(position, node);
719
559
  });
720
560
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "onDropPaste", /*#__PURE__*/function () {
721
- var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(event, change, dropContext) {
561
+ var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(event, change, dropContext) {
722
562
  var editor, transfer, file, type, fragment, text, src, inline, range, ch, handler, _change$value, _document, selection, startBlock, defaultBlock, defaultMarks, frag;
563
+
723
564
  return _regenerator["default"].wrap(function _callee$(_context) {
724
- while (1) switch (_context.prev = _context.next) {
725
- case 0:
726
- editor = change.editor;
727
- transfer = (0, _slateReact.getEventTransfer)(event);
728
- file = transfer.files && transfer.files[0];
729
- type = transfer.type;
730
- fragment = transfer.fragment;
731
- text = transfer.text;
732
- if (!(file && (file.type === 'image/jpeg' || file.type === 'image/jpg' || file.type === 'image/png'))) {
733
- _context.next = 28;
734
- break;
735
- }
736
- if (_this.props.imageSupport) {
737
- _context.next = 9;
738
- break;
739
- }
740
- return _context.abrupt("return");
741
- case 9:
742
- _context.prev = 9;
743
- log('[onDropPaste]');
744
- _context.next = 13;
745
- return (0, serialization.getBase64)(file);
746
- case 13:
747
- src = _context.sent;
748
- inline = _slate.Inline.create({
749
- type: 'image',
750
- isVoid: true,
751
- data: {
752
- loading: false,
753
- src: src
565
+ while (1) {
566
+ switch (_context.prev = _context.next) {
567
+ case 0:
568
+ editor = change.editor;
569
+ transfer = (0, _slateReact.getEventTransfer)(event);
570
+ file = transfer.files && transfer.files[0];
571
+ type = transfer.type;
572
+ fragment = transfer.fragment;
573
+ text = transfer.text;
574
+
575
+ if (!(file && (file.type === 'image/jpeg' || file.type === 'image/jpg' || file.type === 'image/png'))) {
576
+ _context.next = 28;
577
+ break;
754
578
  }
755
- });
756
- if (dropContext) {
757
- _this.focus();
758
- } else {
759
- range = (0, _slateReact.getEventRange)(event, editor);
760
- if (range) {
761
- change.select(range);
579
+
580
+ if (_this.props.imageSupport) {
581
+ _context.next = 9;
582
+ break;
762
583
  }
763
- }
764
- ch = change.insertInline(inline);
765
- _this.onChange(ch);
766
- handler = new _insertImageHandler["default"](inline, function () {
767
- return _this.state.value;
768
- }, _this.onChange, true);
769
- handler.fileChosen(file);
770
- _this.props.imageSupport.add(handler);
771
- _context.next = 26;
772
- break;
773
- case 23:
774
- _context.prev = 23;
775
- _context.t0 = _context["catch"](9);
776
- log('[onDropPaste] error: ', _context.t0);
777
- case 26:
778
- _context.next = 42;
779
- break;
780
- case 28:
781
- if (!(type === 'fragment')) {
782
- _context.next = 32;
584
+
585
+ return _context.abrupt("return");
586
+
587
+ case 9:
588
+ _context.prev = 9;
589
+ log('[onDropPaste]');
590
+ _context.next = 13;
591
+ return (0, serialization.getBase64)(file);
592
+
593
+ case 13:
594
+ src = _context.sent;
595
+ inline = _slate.Inline.create({
596
+ type: 'image',
597
+ isVoid: true,
598
+ data: {
599
+ loading: false,
600
+ src: src
601
+ }
602
+ });
603
+
604
+ if (dropContext) {
605
+ _this.focus();
606
+ } else {
607
+ range = (0, _slateReact.getEventRange)(event, editor);
608
+
609
+ if (range) {
610
+ change.select(range);
611
+ }
612
+ }
613
+
614
+ ch = change.insertInline(inline);
615
+
616
+ _this.onChange(ch);
617
+
618
+ handler = new _insertImageHandler["default"](inline, function () {
619
+ return _this.state.value;
620
+ }, _this.onChange, true);
621
+ handler.fileChosen(file);
622
+
623
+ _this.props.imageSupport.add(handler);
624
+
625
+ _context.next = 26;
783
626
  break;
784
- }
785
- change.insertFragment(fragment);
786
- _context.next = 42;
787
- break;
788
- case 32:
789
- if (!(type === 'text' || type === 'html')) {
627
+
628
+ case 23:
629
+ _context.prev = 23;
630
+ _context.t0 = _context["catch"](9);
631
+ log('[onDropPaste] error: ', _context.t0);
632
+
633
+ case 26:
790
634
  _context.next = 42;
791
635
  break;
792
- }
793
- if (text) {
794
- _context.next = 35;
795
- break;
796
- }
797
- return _context.abrupt("return");
798
- case 35:
799
- _change$value = change.value, _document = _change$value.document, selection = _change$value.selection, startBlock = _change$value.startBlock;
800
- if (!startBlock.isVoid) {
801
- _context.next = 38;
636
+
637
+ case 28:
638
+ if (!(type === 'fragment')) {
639
+ _context.next = 32;
640
+ break;
641
+ }
642
+
643
+ change.insertFragment(fragment);
644
+ _context.next = 42;
802
645
  break;
803
- }
804
- return _context.abrupt("return");
805
- case 38:
806
- defaultBlock = startBlock;
807
- defaultMarks = _document.getInsertMarksAtRange(selection);
808
- frag = _slatePlainSerializer["default"].deserialize(text, {
809
- defaultBlock: defaultBlock,
810
- defaultMarks: defaultMarks
811
- }).document;
812
- change.insertFragment(frag);
813
- case 42:
814
- case "end":
815
- return _context.stop();
646
+
647
+ case 32:
648
+ if (!(type === 'text' || type === 'html')) {
649
+ _context.next = 42;
650
+ break;
651
+ }
652
+
653
+ if (text) {
654
+ _context.next = 35;
655
+ break;
656
+ }
657
+
658
+ return _context.abrupt("return");
659
+
660
+ case 35:
661
+ _change$value = change.value, _document = _change$value.document, selection = _change$value.selection, startBlock = _change$value.startBlock;
662
+
663
+ if (!startBlock.isVoid) {
664
+ _context.next = 38;
665
+ break;
666
+ }
667
+
668
+ return _context.abrupt("return");
669
+
670
+ case 38:
671
+ defaultBlock = startBlock;
672
+ defaultMarks = _document.getInsertMarksAtRange(selection);
673
+ frag = _slatePlainSerializer["default"].deserialize(text, {
674
+ defaultBlock: defaultBlock,
675
+ defaultMarks: defaultMarks
676
+ }).document;
677
+ change.insertFragment(frag);
678
+
679
+ case 42:
680
+ case "end":
681
+ return _context.stop();
682
+ }
816
683
  }
817
684
  }, _callee, null, [[9, 23]]);
818
685
  }));
686
+
819
687
  return function (_x, _x2, _x3) {
820
- return _ref6.apply(this, arguments);
688
+ return _ref.apply(this, arguments);
821
689
  };
822
690
  }());
823
691
  (0, _defineProperty2["default"])((0, _assertThisInitialized2["default"])(_this), "renderPlaceholder", function (props) {
824
692
  var editor = props.editor;
825
693
  var document = editor.value.document;
694
+
826
695
  if (!editor.props.placeholder || document.text !== '' || document.nodes.size !== 1) {
827
696
  return false;
828
697
  }
698
+
829
699
  return /*#__PURE__*/_react["default"].createElement("span", {
830
700
  contentEditable: false,
831
701
  style: {
@@ -842,23 +712,40 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
842
712
  });
843
713
  _this.state = {
844
714
  value: _props.value,
845
- toolbarOpts: createToolbarOpts(_props.toolbarOpts, _props.error)
715
+ toolbarOpts: createToolbarOpts(_props.toolbarOpts, _props.error),
716
+ isHtmlMode: false,
717
+ isEdited: false,
718
+ dialog: {
719
+ open: false
720
+ }
846
721
  };
722
+ _this.toggleHtmlMode = _this.toggleHtmlMode.bind((0, _assertThisInitialized2["default"])(_this));
723
+
847
724
  _this.onResize = function () {
848
725
  _props.onChange(_this.state.value, true);
849
726
  };
727
+
850
728
  _this.handlePlugins(_this.props);
729
+
851
730
  return _this;
852
731
  }
853
- (0, _createClass2["default"])(EditorComponent, [{
732
+
733
+ (0, _createClass2["default"])(Editor, [{
854
734
  key: "componentDidMount",
855
735
  value: function componentDidMount() {
856
736
  var _this2 = this;
737
+
738
+ // onRef is needed to get the ref of the component because we export it using withStyles
739
+ this.props.onRef(this);
857
740
  window.addEventListener('resize', this.onResize);
741
+
858
742
  if (this.editor && this.props.autoFocus) {
859
743
  Promise.resolve().then(function () {
860
744
  if (_this2.editor) {
861
745
  var editorDOM = document.querySelector("[data-key=\"".concat(_this2.editor.value.document.key, "\"]"));
746
+
747
+ _this2.editor.focus();
748
+
862
749
  if (editorDOM) {
863
750
  editorDOM.focus();
864
751
  }
@@ -869,19 +756,25 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
869
756
  }, {
870
757
  key: "UNSAFE_componentWillReceiveProps",
871
758
  value: function UNSAFE_componentWillReceiveProps(nextProps) {
872
- var toolbarOpts = this.state.toolbarOpts;
873
- var newToolbarOpts = createToolbarOpts(nextProps.toolbarOpts, nextProps.error);
759
+ var _this$state2 = this.state,
760
+ isHtmlMode = _this$state2.isHtmlMode,
761
+ toolbarOpts = _this$state2.toolbarOpts;
762
+ var newToolbarOpts = createToolbarOpts(nextProps.toolbarOpts, nextProps.error, isHtmlMode);
763
+
874
764
  if (!(0, _isEqual["default"])(newToolbarOpts, toolbarOpts)) {
875
765
  this.setState({
876
766
  toolbarOpts: newToolbarOpts
877
767
  });
878
768
  }
769
+
879
770
  var differentCharacterProps = !(0, _isEqual["default"])(nextProps.languageCharactersProps, this.props.languageCharactersProps);
880
771
  var differentMathMlProps = !(0, _isEqual["default"])(nextProps.mathMlOptions, this.props.mathMlOptions);
772
+
881
773
  if (differentCharacterProps || differentMathMlProps) {
882
774
  this.handlePlugins(nextProps);
883
775
  }
884
- if (!(0, _isEqual["default"])(nextProps.value, this.props.value)) {
776
+
777
+ if (!nextProps.value.document.equals(this.props.value.document)) {
885
778
  this.setState({
886
779
  focus: false,
887
780
  value: nextProps.value
@@ -890,9 +783,14 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
890
783
  }
891
784
  }, {
892
785
  key: "componentDidUpdate",
893
- value: function componentDidUpdate() {
786
+ value: function componentDidUpdate(prevProps, prevState) {
894
787
  // The cursor is on a zero width element and when that is placed near void elements, it is not visible
895
788
  // so we increase the width to at least 2px in order for the user to see it
789
+ if (this.state.isHtmlMode !== prevState.isHtmlMode || prevState.isEdited !== this.state.isEdited) {
790
+ this.handlePlugins(this.props);
791
+ this.onEditingDone();
792
+ }
793
+
896
794
  var zeroWidthEls = document.querySelectorAll('[data-slate-zero-width="z"]');
897
795
  Array.from(zeroWidthEls).forEach(function (el) {
898
796
  el.style.minWidth = '2px';
@@ -907,15 +805,16 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
907
805
  */
908
806
  function componentWillUnmount() {
909
807
  window.removeEventListener('resize', this.onResize);
910
- }
808
+ } // Allowing time for onChange to take effect if it is called
809
+
911
810
  }, {
912
811
  key: "buildSizeStyle",
913
812
  value: function buildSizeStyle() {
914
813
  var _this$props = this.props,
915
- width = _this$props.width,
916
- minHeight = _this$props.minHeight,
917
- height = _this$props.height,
918
- maxHeight = _this$props.maxHeight;
814
+ width = _this$props.width,
815
+ minHeight = _this$props.minHeight,
816
+ height = _this$props.height,
817
+ maxHeight = _this$props.maxHeight;
919
818
  return {
920
819
  width: this.valueToSize(width),
921
820
  height: this.valueToSize(height),
@@ -927,82 +826,43 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
927
826
  key: "render",
928
827
  value: function render() {
929
828
  var _classNames,
930
- _this3 = this;
829
+ _this3 = this;
830
+
931
831
  var _this$props2 = this.props,
932
- autoFocus = _this$props2.autoFocus,
933
- disabled = _this$props2.disabled,
934
- spellCheck = _this$props2.spellCheck,
935
- highlightShape = _this$props2.highlightShape,
936
- classes = _this$props2.classes,
937
- className = _this$props2.className,
938
- placeholder = _this$props2.placeholder,
939
- pluginProps = _this$props2.pluginProps,
940
- onKeyDown = _this$props2.onKeyDown;
941
- var _this$state2 = this.state,
942
- value = _this$state2.value,
943
- focusedNode = _this$state2.focusedNode,
944
- toolbarOpts = _this$state2.toolbarOpts;
832
+ disabled = _this$props2.disabled,
833
+ spellCheck = _this$props2.spellCheck,
834
+ highlightShape = _this$props2.highlightShape,
835
+ classes = _this$props2.classes,
836
+ className = _this$props2.className,
837
+ placeholder = _this$props2.placeholder,
838
+ pluginProps = _this$props2.pluginProps,
839
+ onKeyDown = _this$props2.onKeyDown;
840
+ var _this$state3 = this.state,
841
+ value = _this$state3.value,
842
+ focusedNode = _this$state3.focusedNode,
843
+ toolbarOpts = _this$state3.toolbarOpts,
844
+ dialog = _this$state3.dialog;
945
845
  log('[render] value: ', value);
946
846
  var sizeStyle = this.buildSizeStyle();
947
- var names = (0, _classnames["default"])((_classNames = {}, (0, _defineProperty2["default"])(_classNames, classes.withBg, highlightShape), (0, _defineProperty2["default"])(_classNames, classes.toolbarOnTop, toolbarOpts.alwaysVisible && toolbarOpts.position === 'top'), _classNames), className, classes.slateEditor);
847
+ var names = (0, _classnames["default"])((_classNames = {}, (0, _defineProperty2["default"])(_classNames, classes.withBg, highlightShape), (0, _defineProperty2["default"])(_classNames, classes.toolbarOnTop, toolbarOpts.alwaysVisible && toolbarOpts.position === 'top'), _classNames), className);
948
848
  return /*#__PURE__*/_react["default"].createElement("div", {
949
- ref: function ref(_ref7) {
950
- return _this3.wrapperRef = _ref7;
849
+ ref: function ref(_ref2) {
850
+ return _this3.wrapperRef = _ref2;
951
851
  },
952
852
  style: {
953
853
  width: sizeStyle.width
954
854
  },
955
855
  className: names
956
- }, /*#__PURE__*/_react["default"].createElement(SlateEditor, {
856
+ }, /*#__PURE__*/_react["default"].createElement(_slateReact.Editor, {
957
857
  plugins: this.plugins,
958
- toolbarRef: function toolbarRef(r) {
858
+ innerRef: function innerRef(r) {
959
859
  if (r) {
960
- _this3.toolbarRef = r;
860
+ _this3.slateEditor = r;
961
861
  }
962
862
  },
963
- autoFocus: autoFocus,
964
- actionsRef: this.props.onRef,
965
- onEditor: this.props.onEditor,
966
- value: value,
967
- focus: this.focus,
968
- onKeyDown: onKeyDown,
969
- onChange: this.onChange,
970
- getFocusedValue: this.getFocusedValue,
971
- onBlur: this.onBlur,
972
- onDrop: function onDrop(event, editor) {
973
- return _this3.onDropPaste(event, editor, true);
974
- },
975
- onPaste: function onPaste(event, editor) {
976
- return _this3.onDropPaste(event, editor);
977
- },
978
- onFocus: this.onFocus,
979
- onEditingDone: this.onEditingDone,
980
- onDone: this.onDone,
981
- focusedNode: focusedNode,
982
- normalize: this.normalize,
983
- readOnly: disabled,
984
- spellCheck: spellCheck,
985
- className: (0, _classnames["default"])((0, _defineProperty2["default"])({}, classes.noPadding, toolbarOpts && toolbarOpts.noBorder), classes.slateEditor),
986
- style: {
987
- minHeight: sizeStyle.minHeight,
988
- height: sizeStyle.height,
989
- maxHeight: sizeStyle.maxHeight
863
+ ref: function ref(r) {
864
+ return _this3.editor = r && _this3.props.editorRef(r);
990
865
  },
991
- pluginProps: pluginProps,
992
- toolbarOpts: toolbarOpts,
993
- placeholder: placeholder,
994
- renderPlaceholder: this.renderPlaceholder
995
- }));
996
- return /*#__PURE__*/_react["default"].createElement("div", {
997
- ref: function ref(_ref8) {
998
- return _this3.wrapperRef = _ref8;
999
- },
1000
- style: {
1001
- width: sizeStyle.width
1002
- },
1003
- className: names
1004
- }, /*#__PURE__*/_react["default"].createElement(_slateReact.Editor, {
1005
- plugins: this.plugins,
1006
866
  toolbarRef: function toolbarRef(r) {
1007
867
  if (r) {
1008
868
  _this3.toolbarRef = r;
@@ -1036,27 +896,33 @@ var EditorComponent = /*#__PURE__*/function (_React$Component) {
1036
896
  pluginProps: pluginProps,
1037
897
  toolbarOpts: toolbarOpts,
1038
898
  placeholder: placeholder,
1039
- renderPlaceholder: this.renderPlaceholder
899
+ renderPlaceholder: this.renderPlaceholder,
900
+ onDataChange: this.changeData
901
+ }), /*#__PURE__*/_react["default"].createElement(_configUi.AlertDialog, {
902
+ open: dialog.open,
903
+ title: dialog.title,
904
+ text: dialog.text,
905
+ onClose: dialog.onClose,
906
+ onConfirm: dialog.onConfirm
1040
907
  }));
1041
908
  }
1042
909
  }]);
1043
- return EditorComponent;
910
+ return Editor;
1044
911
  }(_react["default"].Component); // TODO color - hardcoded gray background and keypad colors will need to change too
1045
- exports.EditorComponent = EditorComponent;
1046
- (0, _defineProperty2["default"])(EditorComponent, "propTypes", {
912
+
913
+
914
+ exports.Editor = Editor;
915
+ (0, _defineProperty2["default"])(Editor, "propTypes", {
1047
916
  autoFocus: _propTypes["default"].bool,
917
+ editorRef: _propTypes["default"].func.isRequired,
1048
918
  error: _propTypes["default"].any,
1049
919
  onRef: _propTypes["default"].func.isRequired,
1050
920
  onChange: _propTypes["default"].func.isRequired,
1051
- onEditor: _propTypes["default"].func,
1052
921
  onFocus: _propTypes["default"].func,
1053
922
  onBlur: _propTypes["default"].func,
1054
923
  onKeyDown: _propTypes["default"].func,
1055
- value: _propTypes["default"].arrayOf(_propTypes["default"].shape({
1056
- type: _propTypes["default"].string,
1057
- children: _propTypes["default"].array,
1058
- data: _propTypes["default"].object
1059
- })),
924
+ focus: _propTypes["default"].func.isRequired,
925
+ value: _slatePropTypes["default"].value.isRequired,
1060
926
  imageSupport: _propTypes["default"].object,
1061
927
  mathMlOptions: _propTypes["default"].shape({
1062
928
  mmlOutput: _propTypes["default"].bool,
@@ -1111,7 +977,7 @@ exports.EditorComponent = EditorComponent;
1111
977
  maxImageWidth: _propTypes["default"].number,
1112
978
  maxImageHeight: _propTypes["default"].number
1113
979
  });
1114
- (0, _defineProperty2["default"])(EditorComponent, "defaultProps", {
980
+ (0, _defineProperty2["default"])(Editor, "defaultProps", {
1115
981
  disableUnderline: true,
1116
982
  onFocus: function onFocus() {},
1117
983
  onBlur: function onBlur() {},
@@ -1139,13 +1005,12 @@ var styles = {
1139
1005
  backgroundColor: _renderUi.color.background()
1140
1006
  },
1141
1007
  '& table:not([border="1"]) tr': {
1142
- borderTop: '1px solid #dfe2e5'
1143
- // TODO perhaps secondary color for background, for now disable
1008
+ borderTop: '1px solid #dfe2e5' // TODO perhaps secondary color for background, for now disable
1144
1009
  // '&:nth-child(2n)': {
1145
1010
  // backgroundColor: '#f6f8fa'
1146
1011
  // }
1147
- },
1148
1012
 
1013
+ },
1149
1014
  '& td, th': {
1150
1015
  padding: '.6em 1em',
1151
1016
  textAlign: 'center'
@@ -1161,6 +1026,8 @@ var styles = {
1161
1026
  padding: '0 !important'
1162
1027
  }
1163
1028
  };
1164
- var _default = (0, _styles.withStyles)(styles)(EditorComponent);
1029
+
1030
+ var _default = (0, _styles.withStyles)(styles)(Editor);
1031
+
1165
1032
  exports["default"] = _default;
1166
1033
  //# sourceMappingURL=editor.js.map