@desynova-digital/components 9.0.8 → 9.0.10

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,9 @@ 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
+ isFocused = _ref.isFocused,
64
+ props = _objectWithoutProperties(_ref, ['maxLength', 'defaultValue', 'type', 'showLengthCount', 'label', 'value', 'autoFocus', 'isFocused']);
63
65
 
64
66
  var _useState = (0, _react.useState)(true),
65
67
  _useState2 = _slicedToArray(_useState, 2),
@@ -71,7 +73,7 @@ var DraftInputText = function DraftInputText(_ref) {
71
73
  diff = _useState4[0],
72
74
  setDiff = _useState4[1];
73
75
 
74
- var _useState5 = (0, _react.useState)(false),
76
+ var _useState5 = (0, _react.useState)(autoFocus),
75
77
  _useState6 = _slicedToArray(_useState5, 2),
76
78
  focus = _useState6[0],
77
79
  setFocus = _useState6[1];
@@ -83,6 +85,8 @@ var DraftInputText = function DraftInputText(_ref) {
83
85
  editorState = _useState8[0],
84
86
  setEditorState = _useState8[1];
85
87
 
88
+ var editorRef = (0, _react.useRef)(null);
89
+
86
90
  var _useState9 = (0, _react.useState)(null),
87
91
  _useState10 = _slicedToArray(_useState9, 2),
88
92
  listScrollTop = _useState10[0],
@@ -129,16 +133,41 @@ var DraftInputText = function DraftInputText(_ref) {
129
133
  */
130
134
  var _getLengthOfSelectedText = function _getLengthOfSelectedText() {
131
135
  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();
136
+ var isCollapsed = currentSelection.isCollapsed();
137
+
138
+ var length = 0;
139
+
140
+ if (!isCollapsed) {
141
+ var currentContent = editorState.getCurrentContent();
142
+ var startKey = currentSelection.getStartKey();
143
+ var endKey = currentSelection.getEndKey();
144
+ var startBlock = currentContent.getBlockForKey(startKey);
145
+ var isStartAndEndBlockAreTheSame = startKey === endKey;
146
+ var startBlockTextLength = startBlock.getLength();
147
+ var startSelectedTextLength = startBlockTextLength - currentSelection.getStartOffset();
148
+ var endSelectedTextLength = currentSelection.getEndOffset();
149
+ var keyAfterEnd = currentContent.getKeyAfter(endKey);
150
+
151
+ if (isStartAndEndBlockAreTheSame) {
152
+ length += currentSelection.getEndOffset() - currentSelection.getStartOffset();
153
+ } else {
154
+ var currentKey = startKey;
155
+
156
+ while (currentKey && currentKey !== keyAfterEnd) {
157
+ if (currentKey === startKey) {
158
+ length += startSelectedTextLength + 1;
159
+ } else if (currentKey === endKey) {
160
+ length += endSelectedTextLength;
161
+ } else {
162
+ length += currentContent.getBlockForKey(currentKey).getLength() + 1;
163
+ }
164
+
165
+ currentKey = currentContent.getKeyAfter(currentKey);
166
+ }
167
+ }
168
+ }
138
169
 
139
- return selectedBlocks.reduce(function (length, block) {
140
- return length + block.getText().length;
141
- }, 0);
170
+ return length;
142
171
  };
143
172
 
144
173
  /**
@@ -214,7 +243,9 @@ var DraftInputText = function DraftInputText(_ref) {
214
243
  return match === '<em>' ? '<i>' : '</i>';
215
244
  });
216
245
  };
217
-
246
+ /**
247
+ * this useEffect is used to send value to parent component as soon as editor state gets updated
248
+ */
218
249
  (0, _react.useEffect)(function () {
219
250
  var html = (0, _draftConvert.convertToHTML)(editorState.getCurrentContent());
220
251
  html = removeOuterPTag(html);
@@ -228,18 +259,29 @@ var DraftInputText = function DraftInputText(_ref) {
228
259
  }
229
260
  }, [editorState]);
230
261
 
262
+ /**
263
+ * This useEffect handles our focus condition and also updates state when the props value changes
264
+ */
231
265
  (0, _react.useEffect)(function () {
232
266
  if (diff && value !== '' && value !== undefined) {
267
+ props.simpleEdit = true;
233
268
  setDiff(false);
234
269
  setFocus(true);
235
270
  var dummy = (0, _draftConvert.convertFromHTML)(value);
236
271
  var newState = _draftJs.EditorState.createWithContent(dummy);
237
- setEditorState(function () {
238
- return _draftJs.EditorState.moveFocusToEnd(newState);
239
- });
272
+ if (isFocused) {
273
+ setEditorState(function () {
274
+ return _draftJs.EditorState.moveFocusToEnd(newState);
275
+ });
276
+ } else {
277
+ setEditorState(newState);
278
+ }
240
279
  }
241
280
  }, [value]);
242
281
 
282
+ /**
283
+ * This useEffect is used to chnage our value when timeline is changed
284
+ */
243
285
  (0, _react.useEffect)(function () {
244
286
  if (listScrollTop !== null) {
245
287
  if (!props.simpleEdit) {
@@ -252,6 +294,16 @@ var DraftInputText = function DraftInputText(_ref) {
252
294
  }
253
295
  }, [listScrollTop]);
254
296
 
297
+ /**
298
+ * This useEffect is used to focus on Line1 when a new segment is created
299
+ */
300
+
301
+ (0, _react.useEffect)(function () {
302
+ if (autoFocus) {
303
+ editorRef.current.focus();
304
+ }
305
+ }, [autoFocus]);
306
+
255
307
  return _react2.default.createElement(
256
308
  DraftInputTextDiv,
257
309
  { theme: props.theme, inputWidth: props.inputWidth },
@@ -266,6 +318,7 @@ var DraftInputText = function DraftInputText(_ref) {
266
318
  showLengthCount: showLengthCount || false
267
319
  }),
268
320
  _react2.default.createElement(_draftJs.Editor, {
321
+ ref: editorRef,
269
322
  editorState: editorState,
270
323
  onChange: _handleChange,
271
324
  handleBeforeInput: _handleBeforeInput,
@@ -388,11 +441,15 @@ DraftInputText.propTypes = {
388
441
  inputWidth: _propTypes2.default.number,
389
442
  showLengthCount: _propTypes2.default.bool,
390
443
  simpleEdit: _propTypes2.default.bool,
444
+ autoFocus: _propTypes2.default.bool,
445
+ isFocused: _propTypes2.default.bool,
391
446
  theme: _propTypes2.default.oneOf(['light', 'dark', 'nexc']),
392
447
  fieldType: _propTypes2.default.oneOf(['inputField', 'textArea'])
393
448
  };
394
449
 
395
450
  DraftInputText.defaultProps = {
451
+ isFocused: false,
452
+ autoFocus: false,
396
453
  fieldType: 'textArea',
397
454
  readOnly: false,
398
455
  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.8",
3
+ "version": "9.0.10",
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.8",
10
+ "@desynova-digital/tokens": "9.0.10",
11
11
  "prop-types": "^15.7.2",
12
12
  "styled-components": "^4.3.2"
13
13
  },