mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-29 17:32:43 +00:00
Fix #540 by validating file names
This commit is contained in:
@@ -16,8 +16,18 @@ export function FileCreation({ model, id, method, onClose }: Props) {
|
|||||||
const { locale } = useLocale()
|
const { locale } = useLocale()
|
||||||
const { projects, project, updateFile } = useProject()
|
const { projects, project, updateFile } = useProject()
|
||||||
const [fileId, setFileId] = useState(id === 'pack_mcmeta' ? 'pack' : '')
|
const [fileId, setFileId] = useState(id === 'pack_mcmeta' ? 'pack' : '')
|
||||||
|
const [error, setError] = useState<string>()
|
||||||
|
|
||||||
|
const changeFileId = (str: string) => {
|
||||||
|
setError(undefined)
|
||||||
|
setFileId(str)
|
||||||
|
}
|
||||||
|
|
||||||
const doSave = () => {
|
const doSave = () => {
|
||||||
|
if (!fileId.match(/^([a-z0-9_.-]+:)?[a-z0-9/_.-]+$/)) {
|
||||||
|
setError('Invalid resource location')
|
||||||
|
return
|
||||||
|
}
|
||||||
Analytics.saveProjectFile(id, projects.length, project.files.length, method as any)
|
Analytics.saveProjectFile(id, projects.length, project.files.length, method as any)
|
||||||
updateFile(id, undefined, { type: id, id: fileId, data: DataModel.unwrapLists(model.data) })
|
updateFile(id, undefined, { type: id, id: fileId, data: DataModel.unwrapLists(model.data) })
|
||||||
onClose()
|
onClose()
|
||||||
@@ -25,7 +35,8 @@ export function FileCreation({ model, id, method, onClose }: Props) {
|
|||||||
|
|
||||||
return <Modal class="file-modal" onDismiss={onClose}>
|
return <Modal class="file-modal" onDismiss={onClose}>
|
||||||
<p>{locale('project.save_current_file')}</p>
|
<p>{locale('project.save_current_file')}</p>
|
||||||
<TextInput autofocus={id !== 'pack_mcmeta'} class="btn btn-input" value={fileId} onChange={setFileId} onEnter={doSave} onCancel={onClose} placeholder={locale('resource_location')} spellcheck={false} readOnly={id === 'pack_mcmeta'} />
|
<TextInput autofocus={id !== 'pack_mcmeta'} class="btn btn-input" value={fileId} onChange={changeFileId} onEnter={doSave} onCancel={onClose} placeholder={locale('resource_location')} spellcheck={false} readOnly={id === 'pack_mcmeta'} />
|
||||||
|
{error !== undefined && <span class="invalid">{error}</span>}
|
||||||
<Btn icon="file" label={locale('project.save')} onClick={doSave} />
|
<Btn icon="file" label={locale('project.save')} onClick={doSave} />
|
||||||
</Modal>
|
</Modal>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,18 @@ export function FileRenaming({ id, name, onClose }: Props) {
|
|||||||
const { locale } = useLocale()
|
const { locale } = useLocale()
|
||||||
const { projects, project, updateFile } = useProject()
|
const { projects, project, updateFile } = useProject()
|
||||||
const [fileId, setFileId] = useState(name)
|
const [fileId, setFileId] = useState(name)
|
||||||
|
const [error, setError] = useState<string>()
|
||||||
|
|
||||||
|
const changeFileId = (str: string) => {
|
||||||
|
setError(undefined)
|
||||||
|
setFileId(str)
|
||||||
|
}
|
||||||
|
|
||||||
const doSave = () => {
|
const doSave = () => {
|
||||||
|
if (!fileId.match(/^([a-z0-9_.-]+:)?[a-z0-9/_.-]+$/)) {
|
||||||
|
setError('Invalid resource location')
|
||||||
|
return
|
||||||
|
}
|
||||||
Analytics.renameProjectFile(id, projects.length, project.files.length, 'menu')
|
Analytics.renameProjectFile(id, projects.length, project.files.length, 'menu')
|
||||||
updateFile(id, name, { type: id, id: fileId })
|
updateFile(id, name, { type: id, id: fileId })
|
||||||
onClose()
|
onClose()
|
||||||
@@ -23,7 +33,8 @@ export function FileRenaming({ id, name, onClose }: Props) {
|
|||||||
|
|
||||||
return <Modal class="file-modal" onDismiss={onClose}>
|
return <Modal class="file-modal" onDismiss={onClose}>
|
||||||
<p>{locale('project.rename_file')}</p>
|
<p>{locale('project.rename_file')}</p>
|
||||||
<TextInput autofocus class="btn btn-input" value={fileId} onChange={setFileId} onEnter={doSave} placeholder={locale('resource_location')} spellcheck={false} />
|
<TextInput autofocus class="btn btn-input" value={fileId} onChange={changeFileId} onEnter={doSave} onCancel={onClose} placeholder={locale('resource_location')} spellcheck={false} />
|
||||||
|
{error !== undefined && <span class="invalid">{error}</span>}
|
||||||
<Btn icon="pencil" label={locale('project.rename')} onClick={doSave} />
|
<Btn icon="pencil" label={locale('project.rename')} onClick={doSave} />
|
||||||
</Modal>
|
</Modal>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1417,6 +1417,11 @@ main.has-project {
|
|||||||
pointer-events: all;
|
pointer-events: all;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.modal span.invalid {
|
||||||
|
color: var(--invalid-text);
|
||||||
|
font-size: 16px;
|
||||||
|
}
|
||||||
|
|
||||||
[data-modals] .tree {
|
[data-modals] .tree {
|
||||||
pointer-events: none;
|
pointer-events: none;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user