文件管理器记录历史路径

4.0
vapao 2023-04-22 23:22:11 +08:00
parent c9f4d7d912
commit 192aa3137c
1 changed files with 18 additions and 14 deletions

View File

@ -18,13 +18,14 @@ import { AuthButton, Action } from 'components';
import { http, uniqueId, X_TOKEN } from 'libs'; import { http, uniqueId, X_TOKEN } from 'libs';
import lds from 'lodash'; import lds from 'lodash';
import styles from './index.module.less' import styles from './index.module.less'
import moment from 'moment';
class FileManager extends React.Component { class FileManager extends React.Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.input = null; this.input = null;
this.input2 = null this.pwdHistoryCaches = new Map()
this.state = { this.state = {
fetching: false, fetching: false,
showDot: false, showDot: false,
@ -43,8 +44,9 @@ class FileManager extends React.Component {
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
if (this.props.id !== prevProps.id) { if (this.props.id !== prevProps.id) {
this.fetchFiles() let pwd = this.pwdHistoryCaches.get(this.props.id) || []
this.setState({objects: []}) this.setState({objects: [], pwd})
this.fetchFiles(pwd)
} }
} }
@ -72,6 +74,7 @@ class FileManager extends React.Component {
}, { }, {
title: '修改时间', title: '修改时间',
dataIndex: 'date', dataIndex: 'date',
sorter: (a, b) => moment(a.date).unix() - moment(b.date).unix(),
width: 190 width: 190
}, { }, {
title: '属性', title: '属性',
@ -97,13 +100,14 @@ class FileManager extends React.Component {
}; };
fetchFiles = (pwd) => { fetchFiles = (pwd) => {
this.setState({fetching: true}); this.setState({ fetching: true });
pwd = pwd || this.state.pwd; pwd = pwd || this.state.pwd;
const path = '/' + pwd.join('/'); const path = '/' + pwd.join('/');
return http.get('/api/file/', {params: {id: this.props.id, path}}) return http.get('/api/file/', {params: {id: this.props.id, path}})
.then(res => { .then(res => {
const objects = lds.orderBy(res, [this._kindSort, 'name'], ['desc', 'asc']); const objects = lds.orderBy(res, [this._kindSort, 'name'], ['desc', 'asc']);
this.setState({objects, pwd}) this.setState({objects, pwd})
this.pwdHistoryCaches.set(this.props.id, pwd)
this.state.inputPath !== null && this.setState({inputPath: path}) this.state.inputPath !== null && this.setState({inputPath: path})
}) })
.finally(() => this.setState({fetching: false})) .finally(() => this.setState({fetching: false}))
@ -123,19 +127,19 @@ class FileManager extends React.Component {
this.fetchFiles(pwd) this.fetchFiles(pwd)
}; };
handleInputEnter = () => { handleInputEdit = () => {
if (this.state.inputPath === null) { let inputPath = '/' + this.state.pwd.join('/')
if (this.state.pwd.length > 0) { this.setState({inputPath})
this.setState({inputPath: `/${this.state.pwd.join('/')}/`})
} else {
this.setState({inputPath: '/'})
} }
setTimeout(() => this.input2.focus(), 100)
} else { handleInputEnter = () => {
if (this.state.inputPath) {
let pwdStr = this.state.inputPath.replace(/^\/+/, '') let pwdStr = this.state.inputPath.replace(/^\/+/, '')
pwdStr = pwdStr.replace(/\/+$/, '') pwdStr = pwdStr.replace(/\/+$/, '')
this.fetchFiles(pwdStr.split('/')) this.fetchFiles(pwdStr.split('/'))
.then(() => this.setState({inputPath: null})) .then(() => this.setState({inputPath: null}))
} else {
this.setState({inputPath: null})
} }
} }
@ -220,7 +224,7 @@ class FileManager extends React.Component {
<input style={{display: 'none'}} type="file" ref={ref => this.input = ref}/> <input style={{display: 'none'}} type="file" ref={ref => this.input = ref}/>
<div className={styles.drawerHeader}> <div className={styles.drawerHeader}>
{this.state.inputPath !== null ? ( {this.state.inputPath !== null ? (
<Input ref={ref => this.input2 = ref} size="small" className={styles.input} <Input size="small" className={styles.input}
suffix={<div style={{color: '#999', fontSize: 12}}>回车确认</div>} suffix={<div style={{color: '#999', fontSize: 12}}>回车确认</div>}
value={this.state.inputPath} onChange={e => this.setState({inputPath: e.target.value})} value={this.state.inputPath} onChange={e => this.setState({inputPath: e.target.value})}
onBlur={this.handleInputEnter} onBlur={this.handleInputEnter}
@ -235,7 +239,7 @@ class FileManager extends React.Component {
<span>{item}</span> <span>{item}</span>
</Breadcrumb.Item> </Breadcrumb.Item>
))} ))}
<Breadcrumb.Item onClick={this.handleInputEnter}> <Breadcrumb.Item onClick={this.handleInputEdit}>
<EditOutlined className={styles.edit}/> <EditOutlined className={styles.edit}/>
</Breadcrumb.Item> </Breadcrumb.Item>
</Breadcrumb> </Breadcrumb>