@deephaven/file-explorer 0.5.1 → 0.5.2-allpackages.16

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.
@@ -1,29 +1,7 @@
1
- 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); }
2
-
3
1
  function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
4
2
 
5
3
  function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
6
4
 
7
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
8
-
9
- 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); } }
10
-
11
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
12
-
13
- 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); }
14
-
15
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
16
-
17
- 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); }; }
18
-
19
- function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
20
-
21
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
22
-
23
- 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; } }
24
-
25
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
26
-
27
5
  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; }
28
6
 
29
7
  import React, { PureComponent } from 'react';
@@ -33,496 +11,455 @@ import { Modal, ModalBody, ModalFooter, ModalHeader } from 'reactstrap';
33
11
  import { BasicModal } from '@deephaven/components';
34
12
  import { Pending, PromiseUtils, ValidationError } from '@deephaven/utils';
35
13
  import Log from '@deephaven/log';
36
- import FileExplorer from './FileExplorer';
37
- import FileUtils from './FileUtils';
14
+ import FileExplorer from "./FileExplorer.js";
15
+ import FileUtils from "./FileUtils.js";
38
16
  import "./NewItemModal.css";
39
- import FileExistsError from './FileExistsError';
40
- import FileNotFoundError from './FileNotFoundError';
17
+ import FileExistsError from "./FileExistsError.js";
18
+ import FileNotFoundError from "./FileNotFoundError.js";
41
19
  var log = Log.module('NewItemModal');
42
20
 
43
- var NewItemModal = /*#__PURE__*/function (_PureComponent) {
44
- _inherits(NewItemModal, _PureComponent);
45
-
46
- var _super = _createSuper(NewItemModal);
21
+ class NewItemModal extends PureComponent {
22
+ static getValidationMessage(err) {
23
+ if (err instanceof FileExistsError && err.info.type === 'directory') {
24
+ return 'Error: Cannot overwrite existing directory';
25
+ }
47
26
 
48
- function NewItemModal(props) {
49
- var _this;
27
+ return "".concat(err);
28
+ }
50
29
 
51
- _classCallCheck(this, NewItemModal);
30
+ static handleError(err) {
31
+ if (!PromiseUtils.isCanceled(err)) {
32
+ log.error(err);
33
+ }
34
+ }
52
35
 
53
- _this = _super.call(this, props);
36
+ constructor(props) {
37
+ super(props);
54
38
 
55
- _defineProperty(_assertThisInitialized(_this), "inputRef", /*#__PURE__*/React.createRef());
39
+ _defineProperty(this, "inputRef", /*#__PURE__*/React.createRef());
56
40
 
57
- _defineProperty(_assertThisInitialized(_this), "cancelableValidatePromise", void 0);
41
+ _defineProperty(this, "cancelableValidatePromise", void 0);
58
42
 
59
- _defineProperty(_assertThisInitialized(_this), "cancelableExistingItemPromise", void 0);
43
+ _defineProperty(this, "cancelableExistingItemPromise", void 0);
60
44
 
61
- _defineProperty(_assertThisInitialized(_this), "pending", new Pending());
45
+ _defineProperty(this, "pending", new Pending());
62
46
 
63
- _defineProperty(_assertThisInitialized(_this), "pathMap", new Map());
47
+ _defineProperty(this, "pathMap", new Map());
64
48
 
65
- _this.handleModalSubmit = _this.handleModalSubmit.bind(_assertThisInitialized(_this));
66
- _this.handleModalOpened = _this.handleModalOpened.bind(_assertThisInitialized(_this));
67
- _this.handleSelect = _this.handleSelect.bind(_assertThisInitialized(_this));
68
- _this.handleChange = _this.handleChange.bind(_assertThisInitialized(_this));
69
- _this.handleKeyDown = _this.handleKeyDown.bind(_assertThisInitialized(_this));
70
- _this.handleValidationError = _this.handleValidationError.bind(_assertThisInitialized(_this));
71
- _this.handleOverwriteCancel = _this.handleOverwriteCancel.bind(_assertThisInitialized(_this));
72
- _this.handleOverwriteConfirm = _this.handleOverwriteConfirm.bind(_assertThisInitialized(_this));
73
- _this.handleExtensionChangeCancel = _this.handleExtensionChangeCancel.bind(_assertThisInitialized(_this));
74
- _this.handleExtensionChangeConfirm = _this.handleExtensionChangeConfirm.bind(_assertThisInitialized(_this));
75
- var defaultValue = props.defaultValue;
49
+ this.handleModalSubmit = this.handleModalSubmit.bind(this);
50
+ this.handleModalOpened = this.handleModalOpened.bind(this);
51
+ this.handleSelect = this.handleSelect.bind(this);
52
+ this.handleChange = this.handleChange.bind(this);
53
+ this.handleKeyDown = this.handleKeyDown.bind(this);
54
+ this.handleValidationError = this.handleValidationError.bind(this);
55
+ this.handleOverwriteCancel = this.handleOverwriteCancel.bind(this);
56
+ this.handleOverwriteConfirm = this.handleOverwriteConfirm.bind(this);
57
+ this.handleExtensionChangeCancel = this.handleExtensionChangeCancel.bind(this);
58
+ this.handleExtensionChangeConfirm = this.handleExtensionChangeConfirm.bind(this);
59
+ var {
60
+ defaultValue
61
+ } = props;
76
62
  var path = FileUtils.hasPath(defaultValue) ? FileUtils.getPath(defaultValue) : '/';
77
- _this.state = {
63
+ this.state = {
78
64
  isSubmitting: false,
79
- path: path,
65
+ path,
80
66
  prevExtension: FileUtils.getExtension(defaultValue),
81
67
  showExtensionChangeModal: false,
82
68
  showOverwriteModal: false,
83
69
  value: FileUtils.getBaseName(defaultValue)
84
70
  };
85
- return _this;
86
71
  }
87
72
 
88
- _createClass(NewItemModal, [{
89
- key: "componentDidUpdate",
90
- value: function componentDidUpdate(prevProps, prevState) {
91
- var isOpen = this.props.isOpen;
92
- var prevIsOpen = prevProps.isOpen;
93
- var _this$state = this.state,
94
- value = _this$state.value,
95
- path = _this$state.path;
96
-
97
- if (!prevIsOpen && isOpen) {
98
- this.resetValue();
99
- }
73
+ componentDidUpdate(prevProps, prevState) {
74
+ var {
75
+ isOpen
76
+ } = this.props;
77
+ var {
78
+ isOpen: prevIsOpen
79
+ } = prevProps;
80
+ var {
81
+ value,
82
+ path
83
+ } = this.state;
84
+
85
+ if (!prevIsOpen && isOpen) {
86
+ this.resetValue();
87
+ }
100
88
 
101
- if (path !== prevState.path || value !== prevState.value) {
102
- this.updateValidationStatus(path, value);
103
- }
89
+ if (path !== prevState.path || value !== prevState.value) {
90
+ this.updateValidationStatus(path, value);
104
91
  }
105
- }, {
106
- key: "componentWillUnmount",
107
- value: function componentWillUnmount() {
108
- this.pending.cancel();
92
+ }
109
93
 
110
- if (this.cancelableValidatePromise) {
111
- this.cancelableValidatePromise.cancel();
112
- }
94
+ componentWillUnmount() {
95
+ this.pending.cancel();
113
96
 
114
- if (this.cancelableExistingItemPromise) {
115
- this.cancelableExistingItemPromise.cancel();
116
- }
97
+ if (this.cancelableValidatePromise) {
98
+ this.cancelableValidatePromise.cancel();
117
99
  }
118
- }, {
119
- key: "resetValue",
120
- value: function resetValue() {
121
- var _ref = this.props,
122
- defaultValue = _ref.defaultValue;
123
- var path = FileUtils.hasPath(defaultValue) ? FileUtils.getPath(defaultValue) : '/';
124
- this.setState({
125
- path: path,
126
- value: FileUtils.getBaseName(defaultValue),
127
- validationError: undefined,
128
- prevExtension: FileUtils.getExtension(defaultValue),
129
- isSubmitting: false
130
- });
100
+
101
+ if (this.cancelableExistingItemPromise) {
102
+ this.cancelableExistingItemPromise.cancel();
131
103
  }
132
- }, {
133
- key: "getValidationPromise",
134
- value: function getValidationPromise(path, name) {
135
- var checkExisting = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
104
+ }
136
105
 
137
- if (this.cancelableValidatePromise) {
138
- this.cancelableValidatePromise.cancel();
139
- }
106
+ resetValue() {
107
+ var {
108
+ defaultValue
109
+ } = this.props;
110
+ var path = FileUtils.hasPath(defaultValue) ? FileUtils.getPath(defaultValue) : '/';
111
+ this.setState({
112
+ path,
113
+ value: FileUtils.getBaseName(defaultValue),
114
+ validationError: undefined,
115
+ prevExtension: FileUtils.getExtension(defaultValue),
116
+ isSubmitting: false
117
+ });
118
+ }
119
+
120
+ getValidationPromise(path, name) {
121
+ var checkExisting = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
140
122
 
141
- this.cancelableValidatePromise = PromiseUtils.makeCancelable(this.validateName(path, name, checkExisting));
142
- return this.cancelableValidatePromise;
123
+ if (this.cancelableValidatePromise) {
124
+ this.cancelableValidatePromise.cancel();
143
125
  }
144
- }, {
145
- key: "validateName",
146
- value: function () {
147
- var _validateName = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(path, name, checkExisting) {
148
- var _this$props, defaultValue, storage, value, existingFile;
149
-
150
- return regeneratorRuntime.wrap(function _callee$(_context) {
151
- while (1) {
152
- switch (_context.prev = _context.next) {
153
- case 0:
154
- FileUtils.validateName(name);
155
- _this$props = this.props, defaultValue = _this$props.defaultValue, storage = _this$props.storage;
156
-
157
- if (!checkExisting) {
158
- _context.next = 16;
159
- break;
160
- }
161
-
162
- value = "".concat(path).concat(name);
163
-
164
- if (!(value !== defaultValue)) {
165
- _context.next = 16;
166
- break;
167
- }
168
-
169
- _context.prev = 5;
170
- _context.next = 8;
171
- return storage.info(value);
172
-
173
- case 8:
174
- existingFile = _context.sent;
175
- throw new FileExistsError(existingFile);
176
-
177
- case 12:
178
- _context.prev = 12;
179
- _context.t0 = _context["catch"](5);
180
-
181
- if (_context.t0 instanceof FileNotFoundError) {
182
- _context.next = 16;
183
- break;
184
- }
185
-
186
- throw _context.t0;
187
-
188
- case 16:
189
- return _context.abrupt("return", name);
190
-
191
- case 17:
192
- case "end":
193
- return _context.stop();
194
- }
195
- }
196
- }, _callee, this, [[5, 12]]);
197
- }));
198
126
 
199
- function validateName(_x, _x2, _x3) {
200
- return _validateName.apply(this, arguments);
201
- }
127
+ this.cancelableValidatePromise = PromiseUtils.makeCancelable(this.validateName(path, name, checkExisting));
128
+ return this.cancelableValidatePromise;
129
+ }
202
130
 
203
- return validateName;
204
- }()
205
- }, {
206
- key: "updateValidationStatus",
207
- value: function updateValidationStatus(path, newName) {
208
- var _this2 = this;
209
-
210
- this.getValidationPromise(path, newName).then(function () {
211
- _this2.setState({
212
- validationError: undefined
213
- });
214
- })["catch"](this.handleValidationError)["catch"](NewItemModal.handleError);
215
- }
216
- }, {
217
- key: "handleModalOpened",
218
- value: function handleModalOpened() {
219
- this.focusRenameInput();
220
- }
221
- }, {
222
- key: "handleModalSubmit",
223
- value: function handleModalSubmit() {
224
- this.submitModal();
225
- }
226
- }, {
227
- key: "handleChange",
228
- value: function handleChange(event) {
229
- var value = event.target.value;
230
- this.setState({
231
- value: value
232
- });
233
- }
234
- }, {
235
- key: "handleSelect",
236
- value: function handleSelect(item) {
237
- var _this3 = this;
238
-
239
- log.debug('handleSelect', item);
240
-
241
- if (item.type === 'directory') {
242
- this.setState({
243
- path: FileUtils.makePath(item.filename)
244
- });
245
- } else {
246
- // Use selected item name and folder and focus the input
247
- var value = item.basename;
248
- var path = FileUtils.getPath(item.filename);
249
- this.setState({
250
- value: value,
251
- path: path
252
- }, function () {
253
- _this3.focusRenameInput();
254
- });
255
- }
256
- } // eslint-disable-next-line class-methods-use-this
257
-
258
- }, {
259
- key: "handleKeyDown",
260
- value: function handleKeyDown(e) {
261
- var key = e.key;
262
- e.stopPropagation();
263
-
264
- switch (key) {
265
- case 'Enter':
266
- {
267
- // Prevent form submit event
268
- e.preventDefault();
269
- this.submitModal();
270
- break;
271
- }
131
+ validateName(path, name, checkExisting) {
132
+ var _this = this;
272
133
 
273
- case 'Escape':
274
- {
275
- e.preventDefault();
276
- var onCancel = this.props.onCancel;
277
- onCancel();
278
- break;
279
- }
134
+ return _asyncToGenerator(function* () {
135
+ FileUtils.validateName(name);
136
+ var {
137
+ defaultValue,
138
+ storage
139
+ } = _this.props;
280
140
 
281
- default:
282
- }
283
- }
284
- }, {
285
- key: "handleValidationError",
286
- value: function handleValidationError(err) {
287
- if (!(err instanceof ValidationError)) {
288
- throw err;
141
+ if (checkExisting) {
142
+ var value = "".concat(path).concat(name);
143
+
144
+ if (value !== defaultValue) {
145
+ try {
146
+ var existingFile = yield storage.info(value);
147
+ throw new FileExistsError(existingFile);
148
+ } catch (e) {
149
+ if (!(e instanceof FileNotFoundError)) {
150
+ throw e;
151
+ } // There is no existing file, ignore
152
+
153
+ }
154
+ }
289
155
  }
290
156
 
291
- log.debug('Validation error', err);
157
+ return name;
158
+ })();
159
+ }
160
+
161
+ updateValidationStatus(path, newName) {
162
+ this.getValidationPromise(path, newName).then(() => {
292
163
  this.setState({
293
- validationError: err
164
+ validationError: undefined
294
165
  });
295
- }
296
- }, {
297
- key: "handleOverwriteCancel",
298
- value: function handleOverwriteCancel() {
166
+ }).catch(this.handleValidationError).catch(NewItemModal.handleError);
167
+ }
168
+
169
+ handleModalOpened() {
170
+ this.focusRenameInput();
171
+ }
172
+
173
+ handleModalSubmit() {
174
+ this.submitModal();
175
+ }
176
+
177
+ handleChange(event) {
178
+ var {
179
+ value
180
+ } = event.target;
181
+ this.setState({
182
+ value
183
+ });
184
+ }
185
+
186
+ handleSelect(item) {
187
+ log.debug('handleSelect', item);
188
+
189
+ if (item.type === 'directory') {
299
190
  this.setState({
300
- showOverwriteModal: false,
301
- isSubmitting: false
191
+ path: FileUtils.makePath(item.filename)
302
192
  });
303
- }
304
- }, {
305
- key: "handleOverwriteConfirm",
306
- value: function handleOverwriteConfirm() {
193
+ } else {
194
+ // Use selected item name and folder and focus the input
195
+ var value = item.basename;
196
+ var path = FileUtils.getPath(item.filename);
307
197
  this.setState({
308
- showOverwriteModal: false
309
- });
310
- var onSubmit = this.props.onSubmit;
311
- var _this$state2 = this.state,
312
- path = _this$state2.path,
313
- value = _this$state2.value;
314
- log.debug('handleOverwriteConfirm', path, value);
315
- onSubmit("".concat(path).concat(value));
316
- }
317
- }, {
318
- key: "handleExtensionChangeCancel",
319
- value: function handleExtensionChangeCancel() {
320
- log.debug('handleExtensionChangeCancel');
321
- this.setState(function (_ref2) {
322
- var value = _ref2.value,
323
- prevExtension = _ref2.prevExtension;
324
- var newValue = FileUtils.replaceExtension(value, prevExtension);
325
- return {
326
- showExtensionChangeModal: false,
327
- value: newValue
328
- };
198
+ value,
199
+ path
200
+ }, () => {
201
+ this.focusRenameInput();
329
202
  });
330
- this.submitModal(true);
331
203
  }
332
- }, {
333
- key: "handleExtensionChangeConfirm",
334
- value: function handleExtensionChangeConfirm() {
335
- log.debug('handleExtensionChangeConfirm');
336
- this.setState({
337
- showExtensionChangeModal: false
338
- });
339
- this.submitModal(true);
204
+ } // eslint-disable-next-line class-methods-use-this
205
+
206
+
207
+ handleKeyDown(e) {
208
+ var {
209
+ key
210
+ } = e;
211
+ e.stopPropagation();
212
+
213
+ switch (key) {
214
+ case 'Enter':
215
+ {
216
+ // Prevent form submit event
217
+ e.preventDefault();
218
+ this.submitModal();
219
+ break;
220
+ }
221
+
222
+ case 'Escape':
223
+ {
224
+ e.preventDefault();
225
+ var {
226
+ onCancel
227
+ } = this.props;
228
+ onCancel();
229
+ break;
230
+ }
231
+
232
+ default:
340
233
  }
341
- /**
342
- * Focus rename input and select name part for files, select all text for folders
343
- */
344
-
345
- }, {
346
- key: "focusRenameInput",
347
- value: function focusRenameInput() {
348
- var input = this.inputRef.current;
349
-
350
- if (input) {
351
- var type = this.props.type;
352
- var value = input.value;
353
- var selectionEnd = type === 'directory' ? value.length : value.lastIndexOf('.');
354
- input.focus();
355
- input.setSelectionRange(0, selectionEnd > 0 ? selectionEnd : value.length);
356
- }
234
+ }
235
+
236
+ handleValidationError(err) {
237
+ if (!(err instanceof ValidationError)) {
238
+ throw err;
357
239
  }
358
- }, {
359
- key: "submitModal",
360
- value: function submitModal() {
361
- var _this4 = this;
362
-
363
- var skipExtensionCheck = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
364
- this.setState(function (_ref3) {
365
- var prevExtension = _ref3.prevExtension,
366
- value = _ref3.value,
367
- path = _ref3.path;
368
- var _this4$props = _this4.props,
369
- notifyOnExtensionChange = _this4$props.notifyOnExtensionChange,
370
- type = _this4$props.type;
371
- log.debug('submitModal', prevExtension, value);
372
- var newExtension = FileUtils.getExtension(value);
373
-
374
- if (notifyOnExtensionChange && !skipExtensionCheck && prevExtension !== null && prevExtension !== newExtension) {
375
- return {
376
- isSubmitting: false,
377
- showExtensionChangeModal: true,
378
- newExtension: newExtension
379
- };
380
- }
381
240
 
382
- _this4.getValidationPromise(path, value, true).then(function (newItemName) {
383
- var onSubmit = _this4.props.onSubmit;
384
- onSubmit("".concat(path).concat(value));
385
- })["catch"](function (e) {
386
- // Don't allow using existing names for folders
387
- // For files, prompt if they want to overwrite existing file
388
- if (e instanceof FileExistsError) {
389
- if (type !== 'directory' && e.info.type !== 'directory') {
390
- _this4.setState({
391
- showOverwriteModal: true
392
- });
393
-
394
- return;
395
- }
396
- }
241
+ log.debug('Validation error', err);
242
+ this.setState({
243
+ validationError: err
244
+ });
245
+ }
397
246
 
398
- throw e;
399
- })["catch"](function (e) {
400
- if (PromiseUtils.isCanceled(e)) {
401
- _this4.setState({
402
- isSubmitting: false
403
- });
404
- }
247
+ handleOverwriteCancel() {
248
+ this.setState({
249
+ showOverwriteModal: false,
250
+ isSubmitting: false
251
+ });
252
+ }
405
253
 
406
- throw e;
407
- })["catch"](_this4.handleValidationError)["catch"](NewItemModal.handleError);
254
+ handleOverwriteConfirm() {
255
+ this.setState({
256
+ showOverwriteModal: false
257
+ });
258
+ var {
259
+ onSubmit
260
+ } = this.props;
261
+ var {
262
+ path,
263
+ value
264
+ } = this.state;
265
+ log.debug('handleOverwriteConfirm', path, value);
266
+ onSubmit("".concat(path).concat(value));
267
+ }
408
268
 
269
+ handleExtensionChangeCancel() {
270
+ log.debug('handleExtensionChangeCancel');
271
+ this.setState((_ref) => {
272
+ var {
273
+ value,
274
+ prevExtension
275
+ } = _ref;
276
+ var newValue = FileUtils.replaceExtension(value, prevExtension);
277
+ return {
278
+ showExtensionChangeModal: false,
279
+ value: newValue
280
+ };
281
+ });
282
+ this.submitModal(true);
283
+ }
284
+
285
+ handleExtensionChangeConfirm() {
286
+ log.debug('handleExtensionChangeConfirm');
287
+ this.setState({
288
+ showExtensionChangeModal: false
289
+ });
290
+ this.submitModal(true);
291
+ }
292
+ /**
293
+ * Focus rename input and select name part for files, select all text for folders
294
+ */
295
+
296
+
297
+ focusRenameInput() {
298
+ var input = this.inputRef.current;
299
+
300
+ if (input) {
301
+ var {
302
+ type
303
+ } = this.props;
304
+ var {
305
+ value
306
+ } = input;
307
+ var selectionEnd = type === 'directory' ? value.length : value.lastIndexOf('.');
308
+ input.focus();
309
+ input.setSelectionRange(0, selectionEnd > 0 ? selectionEnd : value.length);
310
+ }
311
+ }
312
+
313
+ submitModal() {
314
+ var skipExtensionCheck = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
315
+ this.setState((_ref2) => {
316
+ var {
317
+ prevExtension,
318
+ value,
319
+ path
320
+ } = _ref2;
321
+ var {
322
+ notifyOnExtensionChange,
323
+ type
324
+ } = this.props;
325
+ log.debug('submitModal', prevExtension, value);
326
+ var newExtension = FileUtils.getExtension(value);
327
+
328
+ if (notifyOnExtensionChange && !skipExtensionCheck && prevExtension !== null && prevExtension !== newExtension) {
409
329
  return {
410
- isSubmitting: true,
411
- showExtensionChangeModal: false,
412
- newExtension: undefined
330
+ isSubmitting: false,
331
+ showExtensionChangeModal: true,
332
+ newExtension
413
333
  };
414
- });
415
- }
416
- }, {
417
- key: "render",
418
- value: function render() {
419
- var _this$props2 = this.props,
420
- storage = _this$props2.storage,
421
- isOpen = _this$props2.isOpen,
422
- onCancel = _this$props2.onCancel,
423
- placeholder = _this$props2.placeholder,
424
- title = _this$props2.title,
425
- type = _this$props2.type;
426
- var _this$state3 = this.state,
427
- isSubmitting = _this$state3.isSubmitting,
428
- path = _this$state3.path,
429
- showExtensionChangeModal = _this$state3.showExtensionChangeModal,
430
- showOverwriteModal = _this$state3.showOverwriteModal,
431
- validationError = _this$state3.validationError,
432
- value = _this$state3.value,
433
- prevExtension = _this$state3.prevExtension,
434
- newExtension = _this$state3.newExtension;
435
- var isDirectory = type === 'directory';
436
- var prevExtensionText = FileUtils.fileExtensionToString(prevExtension);
437
- var newExtensionText = FileUtils.fileExtensionToString(newExtension);
438
- var submitBtnLabel = isDirectory ? 'Create' : 'Save';
439
- var nameInputLabel = isDirectory ? 'New folder name' : 'Save file as';
440
- return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Modal, {
441
- isOpen: isOpen,
442
- toggle: onCancel,
443
- onOpened: this.handleModalOpened,
444
- className: "modal-dialog-centered new-file-modal theme-bg-dark"
445
- }, /*#__PURE__*/React.createElement(ModalHeader, {
446
- toggle: onCancel
447
- }, title), /*#__PURE__*/React.createElement(ModalBody, null, /*#__PURE__*/React.createElement("form", null, /*#__PURE__*/React.createElement("div", {
448
- className: "d-flex flex-column new-file-modal-content"
449
- }, /*#__PURE__*/React.createElement("div", {
450
- className: "flex-grow-0 mb-4"
451
- }, /*#__PURE__*/React.createElement("label", {
452
- htmlFor: "file-name-input"
453
- }, nameInputLabel), /*#__PURE__*/React.createElement("input", {
454
- id: "file-name-input",
455
- type: "text",
456
- autoComplete: "off",
457
- autoCapitalize: "none",
458
- autoCorrect: "off",
459
- spellCheck: "false",
460
- className: classNames('form-control', {
461
- 'is-invalid': validationError
462
- }),
463
- value: value,
464
- placeholder: placeholder,
465
- ref: this.inputRef,
466
- onKeyDown: this.handleKeyDown,
467
- onChange: this.handleChange
468
- }), validationError && /*#__PURE__*/React.createElement("div", {
469
- className: "invalid-feedback"
470
- }, NewItemModal.getValidationMessage(validationError))), /*#__PURE__*/React.createElement("div", {
471
- className: "flex-grow-0"
472
- }, /*#__PURE__*/React.createElement("label", null, "Directory: ", /*#__PURE__*/React.createElement("span", {
473
- className: "new-item-parentId"
474
- }, path))), /*#__PURE__*/React.createElement("div", {
475
- className: "flex-grow-1 file-explorer-container"
476
- }, /*#__PURE__*/React.createElement(FileExplorer, {
477
- onSelect: this.handleSelect,
478
- storage: storage
479
- }))))), /*#__PURE__*/React.createElement(ModalFooter, null, /*#__PURE__*/React.createElement("button", {
480
- className: "btn btn-outline-primary",
481
- onClick: onCancel,
482
- type: "button"
483
- }, "Cancel"), /*#__PURE__*/React.createElement("button", {
484
- className: "btn btn-primary",
485
- disabled: isSubmitting,
486
- onClick: this.handleModalSubmit,
487
- type: "button"
488
- }, submitBtnLabel))), /*#__PURE__*/React.createElement(BasicModal, {
489
- isOpen: showOverwriteModal,
490
- headerText: "Confirm overwrite",
491
- bodyText: "File with this name already exists, are you sure you want to overwrite it?",
492
- onCancel: this.handleOverwriteCancel,
493
- onConfirm: this.handleOverwriteConfirm,
494
- cancelButtonText: "Cancel",
495
- confirmButtonText: "Overwrite"
496
- }), /*#__PURE__*/React.createElement(BasicModal, {
497
- isOpen: showExtensionChangeModal,
498
- headerText: "Confirm extension change",
499
- bodyText: "Are you sure you want to change extension from \"".concat(prevExtensionText, "\" to \"").concat(newExtensionText, "\"?"),
500
- onCancel: this.handleExtensionChangeCancel,
501
- onConfirm: this.handleExtensionChangeConfirm,
502
- cancelButtonText: "Keep \"".concat(prevExtensionText, "\""),
503
- confirmButtonText: "Use \"".concat(newExtensionText, "\"")
504
- }));
505
- }
506
- }], [{
507
- key: "getValidationMessage",
508
- value: function getValidationMessage(err) {
509
- if (err instanceof FileExistsError && err.info.type === 'directory') {
510
- return 'Error: Cannot overwrite existing directory';
511
334
  }
512
335
 
513
- return "".concat(err);
514
- }
515
- }, {
516
- key: "handleError",
517
- value: function handleError(err) {
518
- if (!PromiseUtils.isCanceled(err)) {
519
- log.error(err);
520
- }
521
- }
522
- }]);
336
+ this.getValidationPromise(path, value, true).then(newItemName => {
337
+ var {
338
+ onSubmit
339
+ } = this.props;
340
+ onSubmit("".concat(path).concat(value));
341
+ }).catch(e => {
342
+ // Don't allow using existing names for folders
343
+ // For files, prompt if they want to overwrite existing file
344
+ if (e instanceof FileExistsError) {
345
+ if (type !== 'directory' && e.info.type !== 'directory') {
346
+ this.setState({
347
+ showOverwriteModal: true
348
+ });
349
+ return;
350
+ }
351
+ }
523
352
 
524
- return NewItemModal;
525
- }(PureComponent);
353
+ throw e;
354
+ }).catch(e => {
355
+ if (PromiseUtils.isCanceled(e)) {
356
+ this.setState({
357
+ isSubmitting: false
358
+ });
359
+ }
360
+
361
+ throw e;
362
+ }).catch(this.handleValidationError).catch(NewItemModal.handleError);
363
+ return {
364
+ isSubmitting: true,
365
+ showExtensionChangeModal: false,
366
+ newExtension: undefined
367
+ };
368
+ });
369
+ }
370
+
371
+ render() {
372
+ var {
373
+ storage,
374
+ isOpen,
375
+ onCancel,
376
+ placeholder,
377
+ title,
378
+ type
379
+ } = this.props;
380
+ var {
381
+ isSubmitting,
382
+ path,
383
+ showExtensionChangeModal,
384
+ showOverwriteModal,
385
+ validationError,
386
+ value,
387
+ prevExtension,
388
+ newExtension
389
+ } = this.state;
390
+ var isDirectory = type === 'directory';
391
+ var prevExtensionText = FileUtils.fileExtensionToString(prevExtension);
392
+ var newExtensionText = FileUtils.fileExtensionToString(newExtension);
393
+ var submitBtnLabel = isDirectory ? 'Create' : 'Save';
394
+ var nameInputLabel = isDirectory ? 'New folder name' : 'Save file as';
395
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Modal, {
396
+ isOpen: isOpen,
397
+ toggle: onCancel,
398
+ onOpened: this.handleModalOpened,
399
+ className: "modal-dialog-centered new-file-modal theme-bg-dark"
400
+ }, /*#__PURE__*/React.createElement(ModalHeader, {
401
+ toggle: onCancel
402
+ }, title), /*#__PURE__*/React.createElement(ModalBody, null, /*#__PURE__*/React.createElement("form", null, /*#__PURE__*/React.createElement("div", {
403
+ className: "d-flex flex-column new-file-modal-content"
404
+ }, /*#__PURE__*/React.createElement("div", {
405
+ className: "flex-grow-0 mb-4"
406
+ }, /*#__PURE__*/React.createElement("label", {
407
+ htmlFor: "file-name-input"
408
+ }, nameInputLabel), /*#__PURE__*/React.createElement("input", {
409
+ id: "file-name-input",
410
+ type: "text",
411
+ autoComplete: "off",
412
+ autoCapitalize: "none",
413
+ autoCorrect: "off",
414
+ spellCheck: "false",
415
+ className: classNames('form-control', {
416
+ 'is-invalid': validationError
417
+ }),
418
+ value: value,
419
+ placeholder: placeholder,
420
+ ref: this.inputRef,
421
+ onKeyDown: this.handleKeyDown,
422
+ onChange: this.handleChange
423
+ }), validationError && /*#__PURE__*/React.createElement("div", {
424
+ className: "invalid-feedback"
425
+ }, NewItemModal.getValidationMessage(validationError))), /*#__PURE__*/React.createElement("div", {
426
+ className: "flex-grow-0"
427
+ }, /*#__PURE__*/React.createElement("label", null, "Directory: ", /*#__PURE__*/React.createElement("span", {
428
+ className: "new-item-parentId"
429
+ }, path))), /*#__PURE__*/React.createElement("div", {
430
+ className: "flex-grow-1 file-explorer-container"
431
+ }, /*#__PURE__*/React.createElement(FileExplorer, {
432
+ onSelect: this.handleSelect,
433
+ storage: storage
434
+ }))))), /*#__PURE__*/React.createElement(ModalFooter, null, /*#__PURE__*/React.createElement("button", {
435
+ className: "btn btn-outline-primary",
436
+ onClick: onCancel,
437
+ type: "button"
438
+ }, "Cancel"), /*#__PURE__*/React.createElement("button", {
439
+ className: "btn btn-primary",
440
+ disabled: isSubmitting,
441
+ onClick: this.handleModalSubmit,
442
+ type: "button"
443
+ }, submitBtnLabel))), /*#__PURE__*/React.createElement(BasicModal, {
444
+ isOpen: showOverwriteModal,
445
+ headerText: "Confirm overwrite",
446
+ bodyText: "File with this name already exists, are you sure you want to overwrite it?",
447
+ onCancel: this.handleOverwriteCancel,
448
+ onConfirm: this.handleOverwriteConfirm,
449
+ cancelButtonText: "Cancel",
450
+ confirmButtonText: "Overwrite"
451
+ }), /*#__PURE__*/React.createElement(BasicModal, {
452
+ isOpen: showExtensionChangeModal,
453
+ headerText: "Confirm extension change",
454
+ bodyText: "Are you sure you want to change extension from \"".concat(prevExtensionText, "\" to \"").concat(newExtensionText, "\"?"),
455
+ onCancel: this.handleExtensionChangeCancel,
456
+ onConfirm: this.handleExtensionChangeConfirm,
457
+ cancelButtonText: "Keep \"".concat(prevExtensionText, "\""),
458
+ confirmButtonText: "Use \"".concat(newExtensionText, "\"")
459
+ }));
460
+ }
461
+
462
+ }
526
463
 
527
464
  _defineProperty(NewItemModal, "propTypes", {
528
465
  isOpen: PropTypes.bool,
@@ -541,12 +478,8 @@ _defineProperty(NewItemModal, "defaultProps", {
541
478
  defaultValue: '/',
542
479
  notifyOnExtensionChange: false,
543
480
  placeholder: '',
544
- onSubmit: function onSubmit(name) {
545
- return undefined;
546
- },
547
- onCancel: function onCancel() {
548
- return undefined;
549
- }
481
+ onSubmit: name => undefined,
482
+ onCancel: () => undefined
550
483
  });
551
484
 
552
485
  export default NewItemModal;