-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #522 from VictoriaShyika/274-default_file
Added default file if folder empty
- Loading branch information
Showing
4 changed files
with
63 additions
and
15 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
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
16 changes: 16 additions & 0 deletions
16
src/components/main/actionsPanel/workspaceTreeView/helpers/createDefaultFile.ts
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,16 @@ | ||
import { RootNodeUid } from "@_constants/index"; | ||
import { TFileHandlerCollection, getIndexHtmlContent } from "@_node/index"; | ||
|
||
export const createDefaultFile = async ( | ||
fileHandlers: TFileHandlerCollection, | ||
) => { | ||
const indexHtmlContent = getIndexHtmlContent(); | ||
const newFile = await ( | ||
fileHandlers[RootNodeUid] as FileSystemDirectoryHandle | ||
).getFileHandle("index.html", { | ||
create: true, | ||
}); | ||
const writableStream = await newFile.createWritable(); | ||
await writableStream.write(indexHtmlContent); | ||
await writableStream.close(); | ||
}; |
26 changes: 26 additions & 0 deletions
26
src/components/main/actionsPanel/workspaceTreeView/hooks/useDefaultFile.tsx
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,26 @@ | ||
import { RootNodeUid } from "@_constants/main"; | ||
import { MainContext } from "@_redux/main"; | ||
import { setShowActionsPanel } from "@_redux/main/processor"; | ||
import { useAppState } from "@_redux/useAppState"; | ||
import { useContext, useEffect } from "react"; | ||
import { useDispatch } from "react-redux"; | ||
import { createDefaultFile } from "../helpers/createDefaultFile"; | ||
|
||
export const useDefaultFileCreate = () => { | ||
const { fileTree } = useAppState(); | ||
const { fileHandlers, reloadCurrentProject, currentProjectFileHandle } = | ||
useContext(MainContext); | ||
|
||
const dispatch = useDispatch(); | ||
|
||
useEffect(() => { | ||
if ( | ||
fileTree[RootNodeUid]?.children?.length === 0 && | ||
fileHandlers[RootNodeUid] | ||
) { | ||
createDefaultFile(fileHandlers); | ||
reloadCurrentProject(fileTree, currentProjectFileHandle); | ||
dispatch(setShowActionsPanel(true)); | ||
} | ||
}, [fileTree[RootNodeUid]?.children, fileHandlers[RootNodeUid]]); | ||
}; |