@desynova-digital/components 9.0.8 → 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
  /**
@@ -230,6 +258,7 @@ var DraftInputText = function DraftInputText(_ref) {
230
258
 
231
259
  (0, _react.useEffect)(function () {
232
260
  if (diff && value !== '' && value !== undefined) {
261
+ props.simpleEdit = true;
233
262
  setDiff(false);
234
263
  setFocus(true);
235
264
  var dummy = (0, _draftConvert.convertFromHTML)(value);
@@ -252,6 +281,11 @@ var DraftInputText = function DraftInputText(_ref) {
252
281
  }
253
282
  }, [listScrollTop]);
254
283
 
284
+ (0, _react.useEffect)(function () {
285
+ if (autoFocus) {
286
+ editorRef.current.focus();
287
+ }
288
+ }, [autoFocus]);
255
289
  return _react2.default.createElement(
256
290
  DraftInputTextDiv,
257
291
  { theme: props.theme, inputWidth: props.inputWidth },
@@ -266,6 +300,7 @@ var DraftInputText = function DraftInputText(_ref) {
266
300
  showLengthCount: showLengthCount || false
267
301
  }),
268
302
  _react2.default.createElement(_draftJs.Editor, {
303
+ ref: editorRef,
269
304
  editorState: editorState,
270
305
  onChange: _handleChange,
271
306
  handleBeforeInput: _handleBeforeInput,
@@ -388,11 +423,13 @@ DraftInputText.propTypes = {
388
423
  inputWidth: _propTypes2.default.number,
389
424
  showLengthCount: _propTypes2.default.bool,
390
425
  simpleEdit: _propTypes2.default.bool,
426
+ autoFocus: _propTypes2.default.bool,
391
427
  theme: _propTypes2.default.oneOf(['light', 'dark', 'nexc']),
392
428
  fieldType: _propTypes2.default.oneOf(['inputField', 'textArea'])
393
429
  };
394
430
 
395
431
  DraftInputText.defaultProps = {
432
+ autoFocus: false,
396
433
  fieldType: 'textArea',
397
434
  readOnly: false,
398
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.8",
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.8",
10
+ "@desynova-digital/tokens": "9.0.9",
11
11
  "prop-types": "^15.7.2",
12
12
  "styled-components": "^4.3.2"
13
13
  },