@desynova-digital/components 9.0.7 → 9.0.9

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.
@@ -59,7 +59,8 @@ var DraftInputText = function DraftInputText(_ref) {
59
59
  showLengthCount = _ref.showLengthCount,
60
60
  label = _ref.label,
61
61
  value = _ref.value,
62
- props = _objectWithoutProperties(_ref, ['maxLength', 'defaultValue', 'type', 'showLengthCount', 'label', 'value']);
62
+ autoFocus = _ref.autoFocus,
63
+ props = _objectWithoutProperties(_ref, ['maxLength', 'defaultValue', 'type', 'showLengthCount', 'label', 'value', 'autoFocus']);
63
64
 
64
65
  var _useState = (0, _react.useState)(true),
65
66
  _useState2 = _slicedToArray(_useState, 2),
@@ -71,7 +72,7 @@ var DraftInputText = function DraftInputText(_ref) {
71
72
  diff = _useState4[0],
72
73
  setDiff = _useState4[1];
73
74
 
74
- var _useState5 = (0, _react.useState)(false),
75
+ var _useState5 = (0, _react.useState)(autoFocus),
75
76
  _useState6 = _slicedToArray(_useState5, 2),
76
77
  focus = _useState6[0],
77
78
  setFocus = _useState6[1];
@@ -83,6 +84,8 @@ var DraftInputText = function DraftInputText(_ref) {
83
84
  editorState = _useState8[0],
84
85
  setEditorState = _useState8[1];
85
86
 
87
+ var editorRef = (0, _react.useRef)(null);
88
+
86
89
  var _useState9 = (0, _react.useState)(null),
87
90
  _useState10 = _slicedToArray(_useState9, 2),
88
91
  listScrollTop = _useState10[0],
@@ -129,16 +132,41 @@ var DraftInputText = function DraftInputText(_ref) {
129
132
  */
130
133
  var _getLengthOfSelectedText = function _getLengthOfSelectedText() {
131
134
  var currentSelection = editorState.getSelection();
132
- if (currentSelection.isCollapsed()) return 0;
133
-
134
- var selectedBlocks = DraftJS.SelectionState.createEmpty(currentSelection.getStartKey()).merge({
135
- focusKey: currentSelection.getEndKey(),
136
- focusOffset: currentSelection.getEndOffset()
137
- }).getBlocksAsArray();
135
+ var isCollapsed = currentSelection.isCollapsed();
136
+
137
+ var length = 0;
138
+
139
+ if (!isCollapsed) {
140
+ var currentContent = editorState.getCurrentContent();
141
+ var startKey = currentSelection.getStartKey();
142
+ var endKey = currentSelection.getEndKey();
143
+ var startBlock = currentContent.getBlockForKey(startKey);
144
+ var isStartAndEndBlockAreTheSame = startKey === endKey;
145
+ var startBlockTextLength = startBlock.getLength();
146
+ var startSelectedTextLength = startBlockTextLength - currentSelection.getStartOffset();
147
+ var endSelectedTextLength = currentSelection.getEndOffset();
148
+ var keyAfterEnd = currentContent.getKeyAfter(endKey);
149
+
150
+ if (isStartAndEndBlockAreTheSame) {
151
+ length += currentSelection.getEndOffset() - currentSelection.getStartOffset();
152
+ } else {
153
+ var currentKey = startKey;
154
+
155
+ while (currentKey && currentKey !== keyAfterEnd) {
156
+ if (currentKey === startKey) {
157
+ length += startSelectedTextLength + 1;
158
+ } else if (currentKey === endKey) {
159
+ length += endSelectedTextLength;
160
+ } else {
161
+ length += currentContent.getBlockForKey(currentKey).getLength() + 1;
162
+ }
163
+
164
+ currentKey = currentContent.getKeyAfter(currentKey);
165
+ }
166
+ }
167
+ }
138
168
 
139
- return selectedBlocks.reduce(function (length, block) {
140
- return length + block.getText().length;
141
- }, 0);
169
+ return length;
142
170
  };
143
171
 
144
172
  /**
@@ -209,9 +237,16 @@ var DraftInputText = function DraftInputText(_ref) {
209
237
  return match ? match[1] : htmlString;
210
238
  };
211
239
 
240
+ var changeEmToI = function changeEmToI(str) {
241
+ return str.replace(/<[/]?em>/g, function (match) {
242
+ return match === '<em>' ? '<i>' : '</i>';
243
+ });
244
+ };
245
+
212
246
  (0, _react.useEffect)(function () {
213
247
  var html = (0, _draftConvert.convertToHTML)(editorState.getCurrentContent());
214
248
  html = removeOuterPTag(html);
249
+ html = changeEmToI(html);
215
250
  var obj = {
216
251
  size: contentLength,
217
252
  html: html
@@ -223,6 +258,7 @@ var DraftInputText = function DraftInputText(_ref) {
223
258
 
224
259
  (0, _react.useEffect)(function () {
225
260
  if (diff && value !== '' && value !== undefined) {
261
+ props.simpleEdit = true;
226
262
  setDiff(false);
227
263
  setFocus(true);
228
264
  var dummy = (0, _draftConvert.convertFromHTML)(value);
@@ -245,6 +281,11 @@ var DraftInputText = function DraftInputText(_ref) {
245
281
  }
246
282
  }, [listScrollTop]);
247
283
 
284
+ (0, _react.useEffect)(function () {
285
+ if (autoFocus) {
286
+ editorRef.current.focus();
287
+ }
288
+ }, [autoFocus]);
248
289
  return _react2.default.createElement(
249
290
  DraftInputTextDiv,
250
291
  { theme: props.theme, inputWidth: props.inputWidth },
@@ -259,6 +300,7 @@ var DraftInputText = function DraftInputText(_ref) {
259
300
  showLengthCount: showLengthCount || false
260
301
  }),
261
302
  _react2.default.createElement(_draftJs.Editor, {
303
+ ref: editorRef,
262
304
  editorState: editorState,
263
305
  onChange: _handleChange,
264
306
  handleBeforeInput: _handleBeforeInput,
@@ -381,11 +423,13 @@ DraftInputText.propTypes = {
381
423
  inputWidth: _propTypes2.default.number,
382
424
  showLengthCount: _propTypes2.default.bool,
383
425
  simpleEdit: _propTypes2.default.bool,
426
+ autoFocus: _propTypes2.default.bool,
384
427
  theme: _propTypes2.default.oneOf(['light', 'dark', 'nexc']),
385
428
  fieldType: _propTypes2.default.oneOf(['inputField', 'textArea'])
386
429
  };
387
430
 
388
431
  DraftInputText.defaultProps = {
432
+ autoFocus: false,
389
433
  fieldType: 'textArea',
390
434
  readOnly: false,
391
435
  code: false,
@@ -154,7 +154,8 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
154
154
  type: 'text',
155
155
  value: 'hello there i am using',
156
156
  label: 'Label and default value',
157
- required: true
157
+ required: true,
158
+ autoFocus: false
158
159
  })
159
160
  ),
160
161
  _react2.default.createElement(
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@desynova-digital/components",
3
- "version": "9.0.7",
3
+ "version": "9.0.9",
4
4
  "description": "Components for Desynova Digital",
5
5
  "main": "index.js",
6
6
  "author": "desynova-digital",
7
7
  "license": "MIT",
8
8
  "repository": "desynova-digital",
9
9
  "dependencies": {
10
- "@desynova-digital/tokens": "9.0.7",
10
+ "@desynova-digital/tokens": "9.0.9",
11
11
  "prop-types": "^15.7.2",
12
12
  "styled-components": "^4.3.2"
13
13
  },