add default value to file name, extension file

This commit is contained in:
Judicael 2022-08-30 17:06:29 +03:00
parent 0f79d4ea68
commit f03a79c5a9
3 changed files with 10 additions and 6 deletions

View file

@ -16,7 +16,7 @@ export const FilenameDialog = ({ isOpen, onClose }: FilenameDialogProps) => {
const app = useTldrawApp() const app = useTldrawApp()
const container = useContainer() const container = useContainer()
const intl = useIntl() const intl = useIntl()
const [filename, setFilename] = React.useState('') const [filename, setFilename] = React.useState(app.document.name)
const handleChange = React.useCallback((event: React.ChangeEvent<HTMLInputElement>) => { const handleChange = React.useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
const value = event.target.value.trimStart() const value = event.target.value.trimStart()
@ -63,7 +63,9 @@ export const FilenameDialog = ({ isOpen, onClose }: FilenameDialogProps) => {
<Button <Button
css={{ backgroundColor: '#2F80ED', color: 'White' }} css={{ backgroundColor: '#2F80ED', color: 'White' }}
onClick={() => { onClick={() => {
app.saveProjectAs(filename) // Remove the file extension if the user entered it
const name = filename.trim().replace(/\.tldr$/, '')
app.saveProjectAs(name)
onClose() onClose()
}} }}
> >

View file

@ -17,6 +17,8 @@ export const LABEL_POINT = [0.5, 0.5]
export const PI2 = Math.PI * 2 export const PI2 = Math.PI * 2
export const FILE_EXTENSION = '.tldr'
export const EASINGS: Record<Easing, (t: number) => number> = { export const EASINGS: Record<Easing, (t: number) => number> = {
linear: (t) => t, linear: (t) => t,
easeInQuad: (t) => t * t, easeInQuad: (t) => t * t,

View file

@ -1,7 +1,7 @@
import { fileOpen, fileSave, supported } from 'browser-fs-access' import { fileOpen, fileSave, supported } from 'browser-fs-access'
import type { FileSystemHandle } from 'browser-fs-access' import type { FileSystemHandle } from 'browser-fs-access'
import { get as getFromIdb, set as setToIdb } from 'idb-keyval' import { get as getFromIdb, set as setToIdb } from 'idb-keyval'
import { IMAGE_EXTENSIONS, VIDEO_EXTENSIONS } from '~constants' import { FILE_EXTENSION, IMAGE_EXTENSIONS, VIDEO_EXTENSIONS } from '~constants'
import type { TDDocument, TDFile } from '~types' import type { TDDocument, TDFile } from '~types'
const options = { mode: 'readwrite' as const } const options = { mode: 'readwrite' as const }
@ -54,9 +54,9 @@ export async function saveToFileSystem(
const newFileHandle = await fileSave( const newFileHandle = await fileSave(
blob, blob,
{ {
fileName: `${filename}.tldr`, fileName: `${filename}${FILE_EXTENSION}`,
description: 'Tldraw File', description: 'Tldraw File',
extensions: [`.tldr`], extensions: [`${FILE_EXTENSION}`],
}, },
fileHandle fileHandle
) )
@ -74,7 +74,7 @@ export async function openFromFileSystem(): Promise<null | {
// Get the blob // Get the blob
const blob = await fileOpen({ const blob = await fileOpen({
description: 'Tldraw File', description: 'Tldraw File',
extensions: [`.tldr`], extensions: [`${FILE_EXTENSION}`],
multiple: false, multiple: false,
}) })