Skip to content

Commit

Permalink
FIXED: keyboard position was lost if first item was selected and tab …
Browse files Browse the repository at this point in the history
…was changed

FIXED: scroll position wasn't reset to top when opening a new directory, fixes #52
UPDATED: version number
  • Loading branch information
warpdesign committed May 29, 2019
1 parent f23607f commit 2c90eb5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-explorer",
"version": "v2.0.0-beta.2",
"version": "v2.0.0-beta.3",
"description": "Plugin-based file explorer written with React",
"main": "build/main.js",
"build": {
Expand Down
11 changes: 4 additions & 7 deletions src/components/FileTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ export class FileTableClass extends React.Component<IProps, IState> {
}

public componentDidUpdate() {
const scrollTop = this.state.position === -1 && this.cache.scrollTop || undefined;
if (scrollTop > 0) {
const scrollTop = this.state.position === -1 ? this.cache.scrollTop : null;
if (scrollTop !== null && scrollTop > -1) {
this.tableRef.current.scrollToPosition(scrollTop);
}
}
Expand Down Expand Up @@ -215,8 +215,6 @@ export class FileTableClass extends React.Component<IProps, IState> {
// when cache is being (re)loaded, cache.files is empty:
// we don't want to show "empty folder" placeholder
// that case, only when cache is loaded and there are no files
const { viewState } = this.injected;

if (cache.cmd === 'cwd' || cache.history.length) {
this.updateNodes(files);
}
Expand Down Expand Up @@ -298,7 +296,8 @@ export class FileTableClass extends React.Component<IProps, IState> {
private updateState(nodes: ITableRow[], keepSelection = false) {
const cache = this.cache;
const newPath = nodes.length && nodes[0].nodeData.dir || '';
const position = keepSelection && cache.selectedId && this.getFilePosition(nodes, cache.selectedId) || -1;
const position = keepSelection && cache.selectedId ? this.getFilePosition(nodes, cache.selectedId) : -1;

// cancel inlineedit if there was one
this.clearEditElement();
this.setState({ nodes, selected: keepSelection ? this.state.selected : 0, position, path: newPath }, () => {
Expand Down Expand Up @@ -490,8 +489,6 @@ export class FileTableClass extends React.Component<IProps, IState> {
const fileCache = this.cache;
const { nodes, position } = this.state;

console.log('selected', nodes.filter((node, i) => i !== position && node.isSelected));

const selection = nodes.filter((node, i) => i !== position && node.isSelected).map((node) => node.nodeData) as File[];

if (position > -1) {
Expand Down
62 changes: 31 additions & 31 deletions src/state/fileState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,12 @@ export class FileState {
}

@action
updateSelection() {
const isSameDir = this.selected.length && this.selected[0].dir === this.path;
const newSelection = [];
updateSelection(isSameDir: boolean) {
if (isSameDir) {
// selected contains files that can be outdated:
const newSelection = [];
// cache.selected contains files that can be outdated:
// files may have been removed on reload
// we iterated through the previously selected files and
// create a new selection based on file ids that were and are still
// in the list of files
// so we filter the list and remove outdated files.
for (let selection of this.selected) {
// use inode/dev to retrieve files that were selected before reload:
// we cannot use fullname anymore since files may have been renamed
Expand All @@ -301,20 +298,22 @@ export class FileState {
if (this.selectedId && !this.files.find(file => file.id.dev === this.selectedId.dev && file.id.ino === this.selectedId.ino)) {
this.selectedId = null;
}

// Do not change the selectedId here, we want to keep it
// if (newSelection.length) {
// const selectedFile = newSelection[newSelection.length - 1];
// this.selected.replace(newSelection);
// this.selectedId = {
// ...selectedFile.id
// }
// } else {
// this.selected.clear();
// this.selectedId = null;
// }
if (newSelection.length) {
const selectedFile = newSelection[newSelection.length - 1];
this.selected.replace(newSelection);
this.selectedId = {
...selectedFile.id
}
} else {
this.selected.clear();
this.selectedId = null;
}
} else {
this.selected.clear();
this.selectedId = null;
this.scrollTop = 0;
}
}

Expand Down Expand Up @@ -395,9 +394,21 @@ export class FileState {

return this.api.cd(joint)
.then((path) => {
return this.list(path).then(() => {
this.updatePath(path, skipHistory);
this.cmd = '';
return this.list(path).then((files) => {
runInAction(() => {
const isSameDir = this.path === path;

this.files.replace(files);

this.updatePath(path, skipHistory);
this.cmd = '';

// update the cache's selection, keeping files that were previously selected
this.updateSelection(isSameDir);

this.setStatus('ok');
});

return path;
});
})
Expand All @@ -415,17 +426,6 @@ export class FileState {
@needsConnection
async list(path: string): Promise<File[]> {
return this.api.list(path)
.then((files: File[]) => {
runInAction(() => {
this.files.replace(files);
// update the cache's selection, keeping files that were previously selected
this.updateSelection();

this.setStatus('ok');
});

return files;
})
.catch(this.handleError)
}

Expand Down

0 comments on commit 2c90eb5

Please sign in to comment.