-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
173 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { useState } from 'react'; | ||
|
||
import type { InputProps } from 'antd'; | ||
import { Input, Spin } from 'antd'; | ||
|
||
import { error, uuid, validateFileName } from '@/utils'; | ||
|
||
import { createDirectory, createFile, FileType } from '@/filehandle/utils/fileManager'; | ||
|
||
import { ExtendFileInfo } from './FileSideMenu'; | ||
|
||
function AddFileInput({ | ||
file, | ||
names, | ||
replace, | ||
removeInput | ||
}: { | ||
file: ExtendFileInfo; | ||
names: string[]; | ||
replace(info: ExtendFileInfo, id: string): any; | ||
removeInput(id: string): any; | ||
}) { | ||
const DEFAULT_NAME = 'default'; | ||
|
||
const isAddFile = file.type === FileType.FILE; | ||
|
||
const [inputStatus, setInputStatus] = useState<{ | ||
name: string; | ||
status: InputProps['status']; | ||
}>({ name: '', status: '' }); | ||
|
||
const [loading, setLoading] = useState(false); | ||
|
||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const value = e.target.value.trim(); | ||
|
||
if (!validateFileName(value) || names.some((n) => n === value)) { | ||
return setInputStatus({ name: value, status: 'error' }); | ||
} | ||
|
||
setInputStatus({ name: value, status: '' }); | ||
}; | ||
|
||
const handleKeyUp = (e: React.KeyboardEvent<HTMLInputElement>) => { | ||
if (e.key.toLocaleLowerCase() !== 'enter') { | ||
return void 0; | ||
} | ||
|
||
handleAdd(); | ||
}; | ||
|
||
const handleAdd = async () => { | ||
const reg = new RegExp(`(?<=default)\\d*(?=${isAddFile ? '.md' : ''})`); | ||
|
||
let name = inputStatus.name; | ||
if (inputStatus.name === '' || inputStatus.status !== '') { | ||
const max = names | ||
.map((n) => n.match(reg)) | ||
.filter(Boolean) | ||
.map(Number) | ||
.sort((a, b) => a - b) | ||
.pop(); | ||
|
||
if (typeof max === 'number') name = DEFAULT_NAME + (max + 1); | ||
else name = DEFAULT_NAME; | ||
} | ||
|
||
if (isAddFile) name += '.md'; | ||
|
||
if (file.handle.kind === 'directory') { | ||
try { | ||
setLoading(true); | ||
|
||
const handle = await (isAddFile ? createFile : createDirectory)(file.handle, name); | ||
|
||
replace( | ||
{ | ||
id: uuid(), | ||
name, | ||
type: file.type, | ||
handle, | ||
icon: '', | ||
parent: file.parent | ||
}, | ||
file.id | ||
); | ||
} catch (err) { | ||
error((err as Error).message); | ||
removeInput(file.id); | ||
} finally { | ||
setLoading(false); | ||
} | ||
} else { | ||
removeInput(file.id); | ||
} | ||
}; | ||
|
||
return ( | ||
<Spin spinning={loading}> | ||
<Input | ||
name="add-mdfile-input" | ||
style={{ marginLeft: 8 }} | ||
autoFocus | ||
size="small" | ||
value={inputStatus.name} | ||
status={inputStatus.status} | ||
suffix={isAddFile ? '.md' : void 0} | ||
onChange={handleInputChange} | ||
onContextMenuCapture={(e) => { | ||
e.stopPropagation(); | ||
e.preventDefault(); | ||
}} | ||
onKeyUp={handleKeyUp} | ||
onBlur={handleAdd} | ||
/> | ||
</Spin> | ||
); | ||
} | ||
|
||
export default AddFileInput; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.