redhotmagma-graphics-editor 1.41.0 → 1.42.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 (82) hide show
  1. package/App.js +90 -165
  2. package/CanvasAdapters/Mock/Canvas.js +71 -125
  3. package/CanvasAdapters/Mock/Objects/CanvasObject.js +23 -40
  4. package/CanvasAdapters/Mock/Objects/Group.js +15 -46
  5. package/CanvasAdapters/Mock/Objects/Image.js +15 -46
  6. package/CanvasAdapters/Mock/Objects/Text.js +15 -51
  7. package/CanvasAdapters/Mock/index.js +0 -1
  8. package/CanvasAdapters/PaperJs/Canvas.js +268 -398
  9. package/CanvasAdapters/PaperJs/CanvasRepository.js +6 -23
  10. package/CanvasAdapters/PaperJs/Modifiers/FontSizeMin.js +2 -5
  11. package/CanvasAdapters/PaperJs/Objects/CanvasObject.js +33 -65
  12. package/CanvasAdapters/PaperJs/Objects/Group.js +14 -48
  13. package/CanvasAdapters/PaperJs/Objects/Image.js +58 -114
  14. package/CanvasAdapters/PaperJs/Objects/Text.js +43 -99
  15. package/CanvasAdapters/PaperJs/Utils/HTML2Paper.js +57 -108
  16. package/CanvasAdapters/PaperJs/Utils/SVGfix.js +9 -30
  17. package/CanvasAdapters/PaperJs/Utils/StyleParams.js +10 -21
  18. package/CanvasAdapters/PaperJs/Utils/TextToSVGRepository.js +53 -86
  19. package/CanvasAdapters/PaperJs/Utils/TextToSVGUtils.js +4 -14
  20. package/CanvasAdapters/PaperJs/Utils/UTF8Base64.js +3 -24
  21. package/CanvasAdapters/PaperJs/Utils/UseColorLayer.js +58 -0
  22. package/CanvasAdapters/PaperJs/Utils/UseMask.js +183 -279
  23. package/CanvasAdapters/PaperJs/Utils/__tests__/SVGfix.test.js +0 -1
  24. package/CanvasAdapters/PaperJs/Utils/__tests__/TextToSVGUtils.test.js +0 -1
  25. package/CanvasAdapters/PaperJs/Utils/__tests__/updateFontStyle.test.js +0 -1
  26. package/CanvasAdapters/PaperJs/Utils/updateFontStyle.js +5 -11
  27. package/CanvasAdapters/PaperJs/index.js +0 -1
  28. package/CanvasInterface/Canvas.js +145 -235
  29. package/CanvasInterface/CanvasProvider.js +25 -75
  30. package/CanvasInterface/Objects/CanvasObject.js +68 -148
  31. package/CanvasInterface/Objects/index.js +0 -1
  32. package/CanvasInterface/Observable.js +10 -29
  33. package/CanvasInterface/canvasConnect.js +12 -44
  34. package/CanvasInterface/index.js +1 -7
  35. package/Components/Canvas/Canvas.js +14 -41
  36. package/Components/Canvas/CanvasContainer.js +5 -18
  37. package/Components/Canvas/styles.js +2 -3
  38. package/Components/DelayedContainer.js +14 -39
  39. package/Components/Editor/Editor.js +47 -102
  40. package/Components/Editor/EditorContainer.js +47 -131
  41. package/Components/Editor/styles.js +2 -3
  42. package/Components/InlineToolbox/InlineToolbox.js +49 -142
  43. package/Components/InlineToolbox/InlineToolboxButton.js +9 -19
  44. package/Components/InlineToolbox/styles.js +2 -3
  45. package/Components/ManipulableContainer.js +15 -41
  46. package/Components/ResizeDetect/ResizeDetect.js +14 -47
  47. package/Components/Rulers/Ruler.js +20 -68
  48. package/Components/Rulers/Rulers.js +14 -46
  49. package/Components/Rulers/styles.js +2 -3
  50. package/Components/SelectionToolbox/ColorPalette.js +25 -73
  51. package/Components/SelectionToolbox/SelectionToolbox.js +39 -101
  52. package/Components/SelectionToolbox/TextTools.js +81 -157
  53. package/Components/SelectionToolbox/styles.js +2 -3
  54. package/Components/StandardToolbox/StandardToolbox.js +81 -144
  55. package/Components/TextEditor/TextEditor.js +67 -182
  56. package/Components/TextEditor/TextEditorStateProvider.js +20 -56
  57. package/Components/Toolbox/ToolboxContainer.js +5 -18
  58. package/Components/Toolbox/ToolboxRow.js +4 -16
  59. package/Components/Toolbox/styles.js +2 -3
  60. package/Utils/Calc2D.js +3 -9
  61. package/Utils/DOM.js +22 -80
  62. package/Utils/Device.js +2 -6
  63. package/Utils/Image.js +17 -11
  64. package/Utils/Logger.js +8 -32
  65. package/Utils/Range.js +6 -33
  66. package/Utils/String.js +2 -8
  67. package/Utils/UnitConversion.js +3 -9
  68. package/Utils/__tests__/Calc2D.test.js +1 -3
  69. package/Utils/__tests__/DOM.test.js +17 -25
  70. package/Utils/__tests__/Range.test.js +0 -1
  71. package/_demos/UsingExternalControls/App.js +13 -48
  72. package/_demos/UsingRenderProps/App.js +15 -54
  73. package/index.js +2 -6
  74. package/package.json +5 -7
  75. package/src/App.js +35 -38
  76. package/src/CanvasAdapters/PaperJs/Canvas.ts +11 -0
  77. package/src/CanvasAdapters/PaperJs/Utils/UseColorLayer.js +56 -0
  78. package/src/Components/InlineToolbox/InlineToolbox.js +2 -2
  79. package/src/Components/SelectionToolbox/TextTools.js +1 -1
  80. package/src/_demos/UsingExternalControls/App.js +25 -28
  81. package/src/_demos/UsingRenderProps/App.js +16 -19
  82. package/src/index.js +2 -2
@@ -4,99 +4,61 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports["default"] = exports.TextEditor = void 0;
7
-
8
7
  var _react = _interopRequireDefault(require("react"));
9
-
10
8
  var _get = _interopRequireDefault(require("lodash/get"));
11
-
12
9
  var _DOM = require("../../Utils/DOM");
13
-
14
10
  var _Range = require("../../Utils/Range");
15
-
16
11
  var _TextEditorStateProvider = require("./TextEditorStateProvider");
17
-
18
12
  var _excluded = ["fontItem", "color"];
19
-
20
13
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
21
-
22
- function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
23
-
14
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
24
15
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
25
-
26
16
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
27
-
28
- function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
29
-
17
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
30
18
  function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
31
-
32
19
  function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
33
-
34
20
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
35
-
36
21
  function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
37
-
38
22
  function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
39
-
40
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
41
-
42
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
43
-
44
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
45
-
23
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
24
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
25
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
46
26
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
47
-
48
- function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
49
-
50
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
51
-
52
- function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
53
-
54
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
55
-
27
+ function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
28
+ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
29
+ function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
30
+ function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
56
31
  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
57
-
58
- function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
59
-
32
+ function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
60
33
  function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
61
-
62
34
  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; } }
63
-
64
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
65
-
66
- function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
67
-
68
- var TextEditor = /*#__PURE__*/function (_React$Component) {
35
+ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
36
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
37
+ function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
38
+ function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
39
+ var TextEditor = exports.TextEditor = /*#__PURE__*/function (_React$Component) {
69
40
  _inherits(TextEditor, _React$Component);
70
-
71
41
  var _super = _createSuper(TextEditor);
72
-
73
42
  function TextEditor() {
74
43
  var _this;
75
-
76
44
  _classCallCheck(this, TextEditor);
77
-
78
45
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
79
46
  args[_key] = arguments[_key];
80
47
  }
81
-
82
48
  _this = _super.call.apply(_super, [this].concat(args));
83
-
84
49
  _defineProperty(_assertThisInitialized(_this), "state", {
85
50
  contentHTML: ''
86
51
  });
87
-
88
52
  _defineProperty(_assertThisInitialized(_this), "selectionChangeListener", function () {
89
53
  var editorContent = _this.contentRef.current;
90
54
  var selection = window.getSelection();
91
55
  var node;
92
-
93
56
  if (selection.rangeCount > 0) {
94
57
  var range = selection.getRangeAt(0);
95
58
  var endContainer = range.endContainer;
96
59
  var parentNode = endContainer.parentNode;
97
60
  node = parentNode;
98
61
  }
99
-
100
62
  if (node && node.children && node.children.length > 0) {
101
63
  if (Object.values(node.children).filter(function (element) {
102
64
  return element.nodeName.toLowerCase() !== 'br';
@@ -104,33 +66,29 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
104
66
  return;
105
67
  }
106
68
  }
107
-
108
69
  if (editorContent.childElementCount === 0 && editorContent.innerText !== '') {
109
70
  _this.wrapSingleTextNodeWithStyle(editorContent);
110
- } // check if the target node is inside the editor
111
-
71
+ }
112
72
 
73
+ // check if the target node is inside the editor
113
74
  if (node && editorContent.contains(node)) {
114
75
  _this.updateCurrentFormatting(node);
115
76
  }
116
77
  });
117
-
118
78
  _defineProperty(_assertThisInitialized(_this), "updateCurrentFormatting", function () {
119
79
  var node = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.getStyleSourceNode();
120
-
121
80
  if (!node.innerText) {
122
81
  return;
123
82
  }
124
-
125
83
  var rootElement = _this.contentRef.current;
126
84
  var _this$props = _this.props,
127
- onUpdateCurrentStyle = _this$props.onUpdateCurrentStyle,
128
- textEditorState = _this$props.textEditorState; // get the formatting
85
+ onUpdateCurrentStyle = _this$props.onUpdateCurrentStyle,
86
+ textEditorState = _this$props.textEditorState;
129
87
 
130
- var currentFormatting = _objectSpread(_objectSpread({}, _this.getNodeFormatting(node)), _this.props.explicitStyleParams); // when there are 0 span's currentFormatting would take defaultcolor and would update currentStyle with default
88
+ // get the formatting
89
+ var currentFormatting = _objectSpread(_objectSpread({}, _this.getNodeFormatting(node)), _this.props.explicitStyleParams);
90
+ // when there are 0 span's currentFormatting would take defaultcolor and would update currentStyle with default
131
91
  // can't skip update as it might affect font family update so required to consider currentStyle color.
132
-
133
-
134
92
  if (rootElement.innerText.trim() === '') {
135
93
  var currentStyle = textEditorState.currentStyle;
136
94
  textEditorState && textEditorState.updateDefaultStyle && textEditorState.updateDefaultStyle({
@@ -138,91 +96,72 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
138
96
  });
139
97
  currentFormatting.color = currentStyle.color;
140
98
  }
141
-
142
99
  onUpdateCurrentStyle && onUpdateCurrentStyle(currentFormatting);
143
100
  textEditorState && textEditorState.updateCurrentStyle && textEditorState.updateCurrentStyle(currentFormatting);
144
101
  });
145
-
146
102
  _defineProperty(_assertThisInitialized(_this), "initTextEditorContent", function () {
147
103
  // init text editor content
148
104
  var _this$props2 = _this.props,
149
- content = _this$props2.content,
150
- autoFocusWhenMounted = _this$props2.autoFocusWhenMounted;
151
-
105
+ content = _this$props2.content,
106
+ autoFocusWhenMounted = _this$props2.autoFocusWhenMounted;
152
107
  var unwrappedContent = _this.unwrapContent(content);
153
-
154
108
  var input = _this.contentRef.current;
155
-
156
109
  if (input.innerHTML !== unwrappedContent) {
157
110
  input.innerHTML = unwrappedContent;
158
-
159
111
  if (!input.children.length && autoFocusWhenMounted) {
160
112
  _this.focusToEnd();
161
113
  }
162
114
  }
163
115
  });
164
-
165
116
  _defineProperty(_assertThisInitialized(_this), "focusToEnd", function () {
166
117
  var editorContent = _this.contentRef.current;
167
- editorContent.focus(); // set the cursor to the end of the text
168
-
118
+ editorContent.focus();
119
+ // set the cursor to the end of the text
169
120
  var selection = window.getSelection();
170
-
171
121
  if (selection.rangeCount > 0) {
172
122
  var range = selection.getRangeAt(0);
173
123
  var lastEndNode = (0, _DOM.getLastEndNode)(editorContent);
174
124
  range.setStart(lastEndNode, lastEndNode.textContent.length);
175
125
  }
176
126
  });
177
-
178
127
  _defineProperty(_assertThisInitialized(_this), "onKeyDown", function (evt) {
179
128
  var disableLinebreaks = _this.props.disableLinebreaks;
180
-
181
129
  if (disableLinebreaks && evt.keyCode === 13) {
182
130
  evt.preventDefault();
183
- } // prevent direct line-breaks
131
+ }
132
+ // prevent direct line-breaks
184
133
  // it inserts a <br> tag which can lead to wrong structure
185
-
186
-
187
134
  if (evt.keyCode === 13 && evt.shiftKey) {
188
135
  evt.preventDefault();
189
136
  } else if (evt.keyCode === 66 && evt.ctrlKey) {
190
137
  // bold
191
138
  _this.formatDoc('bold');
192
-
193
139
  evt.preventDefault();
194
140
  } else if (evt.keyCode === 73 && evt.ctrlKey) {
195
141
  // italic
196
142
  _this.formatDoc('italic');
197
-
198
143
  evt.preventDefault();
199
144
  } else if (evt.keyCode === 85 && evt.ctrlKey // ctrl + u // not supported yet
200
145
  ) {
201
146
  evt.preventDefault();
202
147
  }
203
-
204
148
  evt.stopPropagation();
205
149
  });
206
-
207
150
  _defineProperty(_assertThisInitialized(_this), "onPaste", function () {
208
151
  setTimeout(function () {
209
152
  var container = _this.contentRef.current;
210
153
  var defaultStyle = (0, _get["default"])(_this.props, 'textEditorState.defaultStyle', {});
211
154
  var disableLinebreaks = _this.props.disableLinebreaks;
212
155
  (0, _DOM.updatePastedTextNode)(container, disableLinebreaks);
213
-
214
156
  var divs = _toConsumableArray(container.childNodes);
215
-
216
157
  divs.forEach(function (div) {
217
158
  div.dataset && _this.setNodeData(div, 'color', defaultStyle.color.identifier);
218
-
219
159
  if (div.childElementCount === 0 && div.textContent.trim() !== '') {
220
160
  var spanEle = document.createElement('SPAN');
221
161
  spanEle.innerText = div.textContent;
222
162
  div.textContent = '';
223
163
  div.appendChild(spanEle);
224
164
  }
225
-
226
165
  if (div.childElementCount > 0) {
227
166
  div.firstChild.style.color = defaultStyle.color.value;
228
167
  div.firstChild.dataset && _this.setNodeData(div.firstChild, 'color', defaultStyle.color.identifier);
@@ -230,112 +169,92 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
230
169
  });
231
170
  });
232
171
  });
233
-
234
172
  _defineProperty(_assertThisInitialized(_this), "updateContent", function () {
235
173
  var htmlElement = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.contentRef.current;
236
-
237
174
  var wrappedContent = _this.wrapContentWithDefaultStyle(htmlElement);
238
-
239
175
  var value = wrappedContent.outerHTML;
240
-
241
176
  if (_this.state.contentHTML !== value) {
242
177
  _this.setState({
243
178
  contentHTML: value
244
179
  });
245
-
246
180
  var onChangeHTML = _this.props.onChangeHTML;
247
181
  onChangeHTML && onChangeHTML(wrappedContent);
248
182
  }
249
183
  });
250
-
251
184
  _defineProperty(_assertThisInitialized(_this), "contentRef", _this.props.rteRef || /*#__PURE__*/_react["default"].createRef());
252
-
253
185
  _defineProperty(_assertThisInitialized(_this), "getSelectedNodes", function () {
254
186
  var selectAllIfEmpty = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
255
187
  var forceSelectAll = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
256
188
  var updateSelection = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
257
-
258
189
  if (window.getSelection) {
259
190
  var editorContent = _this.contentRef.current;
260
191
  var sel = window.getSelection();
261
192
  var range = sel.rangeCount > 0 && sel.getRangeAt(0);
262
-
263
- if (selectAllIfEmpty && (sel.rangeCount === 0 || sel.isCollapsed || // selection length is 0
264
- forceSelectAll) || !editorContent.contains(range.commonAncestorContainer.parentNode) && range.commonAncestorContainer !== editorContent) {
193
+ if (selectAllIfEmpty && (sel.rangeCount === 0 || sel.isCollapsed ||
194
+ // selection length is 0
195
+ forceSelectAll) ||
196
+ // forced select all
197
+ // if the selection is out of range select all is the default
198
+ // important: IE11 does not check text nodes so the parentNode should be used
199
+ // so the commonAncestorContainer can be the editor container itself
200
+ !editorContent.contains(range.commonAncestorContainer.parentNode) && range.commonAncestorContainer !== editorContent) {
265
201
  var _range = (0, _Range.selectAll)(editorContent);
266
-
267
202
  if (updateSelection) {
268
203
  sel.removeAllRanges();
269
204
  sel.addRange(_range);
270
205
  }
271
-
272
206
  if (editorContent.textContent.length === 0 && _range.endOffset === 0) {
273
207
  return false;
274
208
  }
275
-
276
209
  return (0, _Range.getNodesInRange)(_range, false);
277
210
  }
278
-
279
211
  return range && (0, _Range.getNodesInRange)(range);
280
212
  }
281
-
282
213
  return false;
283
214
  });
284
-
285
215
  _defineProperty(_assertThisInitialized(_this), "applyOnSelectedNodes", function (fn) {
286
216
  var applyOnAll = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
287
-
288
217
  var nodes = _this.getSelectedNodes(true, applyOnAll);
289
-
290
218
  var results = [];
291
-
292
219
  if (nodes) {
293
220
  nodes.forEach(function (node) {
294
221
  return results.push(fn(node));
295
222
  });
296
223
  }
297
-
298
224
  _this.updateCurrentFormatting();
299
-
300
225
  return results;
301
226
  });
302
-
303
227
  _defineProperty(_assertThisInitialized(_this), "setNodeData", function (node, name, value) {
304
228
  node.dataset[name] = value;
305
229
  });
306
-
307
230
  _defineProperty(_assertThisInitialized(_this), "getNodeData", function (node, key) {
308
231
  if (!node || !key) {
309
232
  throw new Error('Empty node or data key!');
310
233
  }
311
-
312
234
  var data = node.dataset[key];
313
- var root = _this.contentRef.current; // if there is no data, try to get it from the parent (until we find it)
314
-
235
+ var root = _this.contentRef.current;
236
+ // if there is no data, try to get it from the parent (until we find it)
315
237
  if (!data && node !== root && root.contains(node)) {
316
238
  return _this.getNodeData(node.parentNode, key);
317
239
  }
318
-
319
240
  return data;
320
241
  });
321
-
242
+ // https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Editable_content#Example_A_simple_but_complete_rich_text_editor
322
243
  _defineProperty(_assertThisInitialized(_this), "formatDoc", function (sCmd, sValue) {
323
244
  var _this$props3 = _this.props,
324
- fullTextCommandList = _this$props3.fullTextCommandList,
325
- onChangeProp = _this$props3.onChangeProp;
245
+ fullTextCommandList = _this$props3.fullTextCommandList,
246
+ onChangeProp = _this$props3.onChangeProp;
326
247
  var isParagraphCommand = ['justifyLeft', 'justifyCenter', 'justifyRight'].indexOf(sCmd) !== -1;
327
248
  var value = sValue;
328
249
  var setData = false;
329
-
330
250
  if (_typeof(sValue) === 'object') {
331
251
  value = sValue.value;
332
252
  setData = true;
333
253
  }
334
-
335
254
  if (isParagraphCommand) {
336
255
  if (/^justify/.test(sCmd)) {
337
- var alignValue = sCmd.replace('justify', '').toLowerCase(); // set the explicit textAlign param
338
-
256
+ var alignValue = sCmd.replace('justify', '').toLowerCase();
257
+ // set the explicit textAlign param
339
258
  onChangeProp('textAlign', alignValue);
340
259
  return;
341
260
  } else if (document.activeElement !== _this.contentRef.current) {
@@ -344,78 +263,62 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
344
263
  }
345
264
  } else {
346
265
  var applyOnAll = fullTextCommandList instanceof Array && fullTextCommandList.indexOf(sCmd) > -1;
347
-
348
266
  switch (sCmd) {
349
267
  case 'fontSize':
350
268
  // change the explicit fontSize param instead of setting it on the node (needed for CI-223)
351
269
  onChangeProp('fontSize', value);
352
270
  return;
353
-
354
271
  case 'foreColor':
355
272
  _this.applyOnSelectedNodes(function (node) {
356
273
  var style = node.style;
357
274
  style.color = value;
358
275
  setData && _this.setNodeData(node, 'color', sValue.identifier);
359
276
  }, applyOnAll);
360
-
361
277
  return;
362
-
363
278
  case 'fontFamily':
364
279
  _this.applyOnSelectedNodes(function (_ref) {
365
280
  var style = _ref.style;
366
281
  return style.fontFamily = value;
367
282
  }, applyOnAll);
368
-
369
283
  return;
370
-
371
284
  case 'bold':
372
285
  {
373
286
  var isToggled = _this.applyOnSelectedNodes(function (_ref2) {
374
287
  var style = _ref2.style;
375
288
  return style.fontWeight === sCmd;
376
289
  }).indexOf(false) === -1;
377
-
378
290
  _this.applyOnSelectedNodes(function (_ref3) {
379
291
  var style = _ref3.style;
380
292
  return isToggled ? style.fontWeight = 'normal' : style.fontWeight = sCmd;
381
293
  }, applyOnAll);
382
-
383
294
  return;
384
295
  }
385
-
386
296
  case 'italic':
387
297
  {
388
298
  var _isToggled = _this.applyOnSelectedNodes(function (_ref4) {
389
299
  var style = _ref4.style;
390
300
  return style.fontStyle === sCmd;
391
301
  }).indexOf(false) === -1;
392
-
393
302
  _this.applyOnSelectedNodes(function (_ref5) {
394
303
  var style = _ref5.style;
395
304
  return _isToggled ? style.fontStyle = 'normal' : style.fontStyle = sCmd;
396
305
  }, applyOnAll);
397
-
398
306
  return;
399
307
  }
400
308
  }
401
309
  }
402
-
403
310
  document.execCommand(sCmd, false, value);
404
311
  });
405
-
406
312
  return _this;
407
313
  }
408
-
409
314
  _createClass(TextEditor, [{
410
315
  key: "componentDidMount",
411
316
  value: function componentDidMount() {
412
317
  var _this2 = this;
413
-
414
318
  this.initTextEditorContent();
415
319
  var editorContent = this.contentRef.current;
416
320
  this._observer = new MutationObserver(function () {
417
321
  (0, _DOM.fixContentEditableDOM)(editorContent);
418
-
419
322
  _this2.updateContent();
420
323
  });
421
324
  var config = {
@@ -424,23 +327,22 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
424
327
  characterData: true,
425
328
  subtree: true
426
329
  };
330
+ this._observer.observe(editorContent, config);
427
331
 
428
- this._observer.observe(editorContent, config); // setup content editable
429
-
430
-
332
+ // setup content editable
431
333
  document.execCommand('defaultParagraphSeparator', false, 'div');
432
334
  document.execCommand('styleWithCSS', false, true);
433
335
  editorContent.addEventListener('keydown', this.onKeyDown);
434
336
  editorContent.addEventListener('paste', this.onPaste);
435
- document.addEventListener('selectionchange', this.selectionChangeListener); // trigger content update to synchronize the Text element with the editor settings
337
+ document.addEventListener('selectionchange', this.selectionChangeListener);
436
338
 
339
+ // trigger content update to synchronize the Text element with the editor settings
437
340
  this.updateContent();
438
-
439
341
  if (this.props.autoFocusWhenMounted) {
440
342
  this.focusToEnd();
441
- } // set the formatDoc function in the editor state context
442
-
343
+ }
443
344
 
345
+ // set the formatDoc function in the editor state context
444
346
  if (this.props.textEditorState) {
445
347
  this.props.textEditorState.setFormatDocFunction(this.formatDoc);
446
348
  }
@@ -449,7 +351,6 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
449
351
  key: "componentWillUnmount",
450
352
  value: function componentWillUnmount() {
451
353
  this._observer.disconnect();
452
-
453
354
  var editorContent = this.contentRef.current;
454
355
  editorContent.removeEventListener('keydown', this.onKeyDown);
455
356
  document.removeEventListener('selectionchange', this.selectionChangeListener);
@@ -458,10 +359,9 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
458
359
  key: "componentDidUpdate",
459
360
  value: function componentDidUpdate(prevProps) {
460
361
  var _this$props4 = this.props,
461
- content = _this$props4.content,
462
- defaultStyle = _this$props4.textEditorState.defaultStyle,
463
- disableLinebreaks = _this$props4.disableLinebreaks;
464
-
362
+ content = _this$props4.content,
363
+ defaultStyle = _this$props4.textEditorState.defaultStyle,
364
+ disableLinebreaks = _this$props4.disableLinebreaks;
465
365
  if (content && content !== prevProps.content) {
466
366
  this.setState({
467
367
  contentHTML: content
@@ -469,11 +369,9 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
469
369
  this.initTextEditorContent();
470
370
  this.updateCurrentFormatting();
471
371
  }
472
-
473
372
  if (defaultStyle !== prevProps.textEditorState.defaultStyle) {
474
373
  this.updateContent();
475
374
  }
476
-
477
375
  if (disableLinebreaks && disableLinebreaks !== prevProps.disableLinebreaks) {
478
376
  (0, _DOM.removeLinebreaks)(this.contentRef.current);
479
377
  }
@@ -482,10 +380,9 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
482
380
  key: "wrapSingleTextNodeWithStyle",
483
381
  value: function wrapSingleTextNodeWithStyle(editorContent) {
484
382
  var _this$props$textEdito = this.props.textEditorState.currentStyle,
485
- fontItem = _this$props$textEdito.fontItem,
486
- color = _this$props$textEdito.color,
487
- assignableStyles = _objectWithoutProperties(_this$props$textEdito, _excluded);
488
-
383
+ fontItem = _this$props$textEdito.fontItem,
384
+ color = _this$props$textEdito.color,
385
+ assignableStyles = _objectWithoutProperties(_this$props$textEdito, _excluded);
489
386
  var sp = document.createElement('span');
490
387
  sp.textContent = editorContent.innerText;
491
388
  editorContent.innerText = '';
@@ -502,32 +399,27 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
502
399
  key: "getStyleSourceNode",
503
400
  value: function getStyleSourceNode() {
504
401
  var selection = window.getSelection();
505
-
506
402
  if (selection.rangeCount > 0) {
507
403
  var range = selection.getRangeAt(0);
508
404
  var endContainer = range.endContainer;
509
405
  var parentNode = endContainer.parentNode;
510
-
511
406
  if (this.contentRef.current.contains(parentNode)) {
512
407
  return parentNode;
513
408
  }
514
409
  }
515
-
516
410
  return this.contentRef.current;
517
411
  }
518
412
  }, {
519
413
  key: "getNodeFormatting",
520
414
  value: function getNodeFormatting(node) {
521
415
  var endNode = (0, _DOM.getFirstEndNode)(node).parentNode;
522
-
523
416
  var _window$getComputedSt = window.getComputedStyle(endNode),
524
- fontSize = _window$getComputedSt.fontSize,
525
- fontFamily = _window$getComputedSt.fontFamily,
526
- fontWeight = _window$getComputedSt.fontWeight,
527
- fontStyle = _window$getComputedSt.fontStyle,
528
- textAlign = _window$getComputedSt.textAlign,
529
- color = _window$getComputedSt.color;
530
-
417
+ fontSize = _window$getComputedSt.fontSize,
418
+ fontFamily = _window$getComputedSt.fontFamily,
419
+ fontWeight = _window$getComputedSt.fontWeight,
420
+ fontStyle = _window$getComputedSt.fontStyle,
421
+ textAlign = _window$getComputedSt.textAlign,
422
+ color = _window$getComputedSt.color;
531
423
  return {
532
424
  fontSize: fontSize,
533
425
  fontFamily: fontFamily,
@@ -564,11 +456,11 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
564
456
  key: "render",
565
457
  value: function render() {
566
458
  var _this$props5 = this.props,
567
- className = _this$props5.className,
568
- _this$props5$textEdit = _this$props5.textEditorState,
569
- defaultStyle = _this$props5$textEdit.defaultStyle,
570
- currentStyle = _this$props5$textEdit.currentStyle,
571
- elementProps = _this$props5.elementProps;
459
+ className = _this$props5.className,
460
+ _this$props5$textEdit = _this$props5.textEditorState,
461
+ defaultStyle = _this$props5$textEdit.defaultStyle,
462
+ currentStyle = _this$props5$textEdit.currentStyle,
463
+ elementProps = _this$props5.elementProps;
572
464
  var color = (0, _get["default"])(defaultStyle, 'color.value', '');
573
465
  var dataColor = (0, _get["default"])(defaultStyle, 'color.identifier', '');
574
466
  var textAlign = (0, _get["default"])(currentStyle, 'textAlign', 'left');
@@ -589,17 +481,10 @@ var TextEditor = /*#__PURE__*/function (_React$Component) {
589
481
  );
590
482
  }
591
483
  }]);
592
-
593
484
  return TextEditor;
594
485
  }(_react["default"].Component);
595
-
596
- exports.TextEditor = TextEditor;
597
-
598
486
  _defineProperty(TextEditor, "defaultProps", {
599
487
  autoFocusWhenMounted: true,
600
488
  disableLinebreaks: false
601
489
  });
602
-
603
- var _default = (0, _TextEditorStateProvider.withTextEditorState)(TextEditor);
604
-
605
- exports["default"] = _default;
490
+ var _default = exports["default"] = (0, _TextEditorStateProvider.withTextEditorState)(TextEditor);