Skip to content

Commit 558214f

Browse files
fix: uncaught error on drop without active pane
1 parent 14fb9b4 commit 558214f

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed

lib/main.js

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export default {
101101
: enableNotif ? atom.notifications.addSuccess(`New import added!`) : 0;
102102
},
103103

104-
popNotif(relativePath, editor) {
104+
insertStatement(relativePath, editor) {
105105

106106
const _path = editor.getPath().toString();
107107
const extname = path.extname(_path);
@@ -135,53 +135,49 @@ export default {
135135
: this.toImportPositionTop(importText, editor, importName);
136136
},
137137

138-
warnNotif(options, fromFileType, toFileType) {
139-
140-
const { isSameValid, notSupportedValid, invalidFileTrue } = options;
138+
notify(options, fromExtname, toExtname) {
139+
const { isSameValid, notSupportedValid, invalidFile, isDirectory } = options;
141140
const notif = atom.notifications;
142-
const append = fromFileType.toUpperCase().substring(1);
143-
144-
invalidFileTrue ? notif.addWarning(`Unable to import ${fromFileType} to ${toFileType}.`)
145-
: !invalidFileTrue && isSameValid ? notif.addInfo('Same file path.')
146-
: !invalidFileTrue && notSupportedValid ? notif.addWarning(`${append} files not supported.`)
147-
: !invalidFileTrue && !isSameValid && notSupportedValid === 0 ? notif.addWarning(`Same file path. ${append} files not supported.`)
148-
: !invalidFileTrue && !notSupportedValid && isSameValid === 0 ? notif.addError('Import failed.') : 0;
141+
const append = fromExtname.toUpperCase().substring(1);
142+
143+
invalidFile ? notif.addWarning(`Unable to import ${fromExtname} to ${toExtname}.`)
144+
: isDirectory && toExtname !== '' ? notif.addError('File not found.')
145+
: fromExtname === '' && toExtname === '' ? notif.addError('Active pane not found.')
146+
: !invalidFile && isSameValid ? notif.addInfo('Same file path.')
147+
: !invalidFile && notSupportedValid ? notif.addWarning(`${append} files not supported.`)
148+
: !invalidFile && !isSameValid && notSupportedValid === 0 ? notif.addWarning(`Same file path. ${append} files not supported.`)
149+
: !invalidFile && !notSupportedValid && isSameValid === 0 ? notif.addError('Import failed.') : 0;
149150
},
150151

151-
calculatePath(editor) {
152-
153-
if (editor.getPath() === undefined) return;
152+
setup(item, activePane) {
154153

155154
const enableNotif = !this.param.disableNotifs;
156-
const selectedSpan = atom.document.querySelector('.file.entry.list-item.selected > span');
157-
if (!selectedSpan)
158-
if (enableNotif) return atom.notifications.addError('File not found.');
159-
160-
const activePane = atom.workspace.getActiveTextEditor();
155+
const isFile = item.target.hasOwnProperty('file');
156+
const isDirectory = item.target.hasOwnProperty('directory');
161157
const validFileTypes = [ '.ts', '.tsx' ];
162-
const fromPath = selectedSpan.dataset.path.toString();
158+
const fromPath = isFile ? item.target.file.path : '';
163159
const toPath = activePane.getPath().toString();
164160
const relativePath = relative(toPath, fromPath);
165-
const fromFileType = path.extname(fromPath);
166-
const toFileType = path.extname(toPath);
167-
const from_valid = validFileTypes.some(el => fromFileType.includes(el));
168-
const to_valid = validFileTypes.some(el => toFileType.includes(el));
161+
const fromExtname = path.extname(fromPath);
162+
const toExtname = path.extname(toPath);
163+
const fromIsValid = validFileTypes.some(el => fromExtname.includes(el));
164+
const toIsValid = validFileTypes.some(el => toExtname.includes(el));
169165
const isSamePath = (fromPath === toPath);
170-
const isBothValid = (from_valid && to_valid);
171-
const isSameType = (fromFileType === toFileType);
166+
const isBothValid = (fromIsValid && toIsValid);
167+
const isSameType = (fromExtname === toExtname);
172168

173169
if (isSamePath && isBothValid && isSameType && enableNotif)
174-
this.warnNotif({ invalidFileTrue: false, isSameValid: true, notSupportedValid: 0 }, fromFileType, toFileType);
170+
this.notify({ invalidFile: false, isSameValid: true, notSupportedValid: 0, isDirectory }, fromExtname, toExtname);
175171
else if (!isSamePath && !isBothValid && isSameType && enableNotif)
176-
this.warnNotif({ invalidFileTrue: false, isSameValid: 0, notSupportedValid: true }, fromFileType, toFileType);
172+
this.notify({ invalidFile: false, isSameValid: 0, notSupportedValid: true, isDirectory }, fromExtname, toExtname);
177173
else if (isSamePath && !isBothValid && isSameType && enableNotif)
178-
this.warnNotif({ invalidFileTrue: false, isSameValid: false, notSupportedValid: 0 }, fromFileType, toFileType);
174+
this.notify({ invalidFile: false, isSameValid: false, notSupportedValid: 0, isDirectory }, fromExtname, toExtname);
179175
else if (!isSamePath && !isBothValid && !isSameType && enableNotif)
180-
this.warnNotif({ invalidFileTrue: false, isSameValid: 0, notSupportedValid: false }, fromFileType, toFileType);
176+
this.notify({ invalidFile: false, isSameValid: 0, notSupportedValid: false, isDirectory }, fromExtname, toExtname);
181177
else if (!isSamePath && isBothValid && !isSameType && enableNotif)
182-
this.warnNotif({ invalidFileTrue: true, isSameValid: 0, notSupportedValid: 0 }, fromFileType, toFileType);
178+
this.notify({ invalidFile: true, isSameValid: 0, notSupportedValid: 0, isDirectory }, fromExtname, toExtname);
183179
else if (!isSamePath && isBothValid && isSameType)
184-
this.popNotif(relativePath, editor)
180+
this.insertStatement(relativePath, activePane)
185181
},
186182

187183
activate(state) {
@@ -194,17 +190,18 @@ export default {
194190
const editorView = atom.views.getView(editor);
195191
const lines = editorView.querySelector('.lines');
196192
const leftDock = atom.workspace.paneContainers.left.refs.wrapperElement;
197-
const directories = atom.document.querySelector('[class*="tree-view-root"]');
198193

199-
let valid = false;;
200-
lines.addEventListener("dragenter", () => (valid = true), false);
201-
directories.addEventListener("dragleave", () => (valid = false), false);
194+
let valid = false;
195+
lines.addEventListener("dragenter", () => (valid = true), false);
196+
leftDock.addEventListener("dragenter", (e) => e.target.ondragenter = () => (valid = false), false);
202197

203198
this.subscriptions.add(lines, "drop", (_lines) => {
204199
_lines.preventDefault();
205200

206-
leftDock.ondragend = (_leftDock) => {
207-
valid ? this.calculatePath(editor) : 0;
201+
leftDock.ondragend = (item) => {
202+
const activePane = atom.workspace.getActiveTextEditor();
203+
204+
valid && activePane ? this.setup(item, activePane) : this.notify({ }, '', '');
208205
}
209206
});
210207

0 commit comments

Comments
 (0)