@@ -205,11 +205,14 @@ export const FileProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
205205 }
206206 } , [ spellCheckEnabled , editor ] )
207207
208- const [ debouncedEditor ] = useDebounce ( editor ?. state . doc . content , 4000 )
208+ const [ debouncedEditor ] = useDebounce ( editor ?. state . doc . content , 3000 )
209209
210210 useEffect ( ( ) => {
211211 if ( debouncedEditor && ! currentlyChangingFilePath ) {
212212 writeEditorContentToDisk ( editor , currentlyOpenFilePath )
213+ if ( editor && currentlyOpenFilePath ) {
214+ handleNewFileRenaming ( editor , currentlyOpenFilePath )
215+ }
213216 }
214217 } , [ debouncedEditor , currentlyOpenFilePath , editor , currentlyChangingFilePath ] )
215218
@@ -227,26 +230,29 @@ export const FileProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
227230 } )
228231 setNeedToWriteEditorContentToDisk ( false )
229232 }
233+ }
234+ }
230235
231- const fileInfo = vaultFilesFlattened . find ( ( f ) => f . path === filePath )
232- if (
233- fileInfo &&
234- fileInfo . name . startsWith ( 'Untitled' ) &&
235- new Date ( ) . getTime ( ) - fileInfo . dateCreated . getTime ( ) < 60000
236- ) {
237- const editorText = editor ?. getText ( )
238- if ( editorText ) {
239- const newProposedFileName = generateFileNameFromFileContent ( editorText )
240- if ( newProposedFileName ) {
241- const directoryToMakeFileIn = await window . path . dirname ( filePath )
242- const filesInDirectory = await getFilesInDirectory ( directoryToMakeFileIn , vaultFilesFlattened )
243- const fileName = getNextAvailableFileNameGivenBaseName (
244- filesInDirectory . map ( ( file ) => file . name ) ,
245- newProposedFileName ,
246- )
247- const newFilePath = await window . path . join ( directoryToMakeFileIn , fileName )
248- await renameFile ( filePath , newFilePath )
249- }
236+ const handleNewFileRenaming = async ( _editor : Editor , filePath : string ) => {
237+ const fileInfo = vaultFilesFlattened . find ( ( f ) => f . path === filePath )
238+ if (
239+ fileInfo &&
240+ fileInfo . name . startsWith ( 'Untitled' ) &&
241+ new Date ( ) . getTime ( ) - fileInfo . dateCreated . getTime ( ) < 60000
242+ ) {
243+ const editorText = _editor . getText ( )
244+ if ( editorText ) {
245+ const newProposedFileName = generateFileNameFromFileContent ( editorText )
246+ if ( newProposedFileName ) {
247+ const directoryToMakeFileIn = await window . path . dirname ( filePath )
248+ const filesInDirectory = await getFilesInDirectory ( directoryToMakeFileIn , vaultFilesFlattened )
249+ const fileName = getNextAvailableFileNameGivenBaseName (
250+ filesInDirectory . map ( ( file ) => file . name ) ,
251+ newProposedFileName ,
252+ )
253+ const newFilePath = await window . path . join ( directoryToMakeFileIn , fileName )
254+ await renameFile ( filePath , newFilePath )
255+ // setCurrentlyOpenFilePath(newFilePath)
250256 }
251257 }
252258 }
0 commit comments