mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-04-25 00:26:30 +00:00
fix: remove excessive commenting
This commit is contained in:
parent
f02e10c9ac
commit
4665fa67fa
@ -290,7 +290,6 @@ function FileContextMenu({
|
||||
const depth = useMemo(() => fullPath.split('/').length, [fullPath]);
|
||||
const fileName = useMemo(() => path.basename(fullPath), [fullPath]);
|
||||
|
||||
// Add this to determine if the path is a file or folder
|
||||
const isFolder = useMemo(() => {
|
||||
const files = workbenchStore.files.get();
|
||||
const fileEntry = files[fullPath];
|
||||
@ -298,7 +297,6 @@ function FileContextMenu({
|
||||
return !fileEntry || fileEntry.type === 'folder';
|
||||
}, [fullPath]);
|
||||
|
||||
// Get the parent directory for files
|
||||
const targetPath = useMemo(() => {
|
||||
return isFolder ? fullPath : path.dirname(fullPath);
|
||||
}, [fullPath, isFolder]);
|
||||
@ -354,10 +352,7 @@ function FileContextMenu({
|
||||
);
|
||||
|
||||
const handleCreateFile = async (fileName: string) => {
|
||||
// Use targetPath instead of fullPath
|
||||
const newFilePath = path.join(targetPath, fileName);
|
||||
|
||||
// Change from createNewFile to createFile
|
||||
const success = await workbenchStore.createFile(newFilePath, '');
|
||||
|
||||
if (success) {
|
||||
@ -370,10 +365,7 @@ function FileContextMenu({
|
||||
};
|
||||
|
||||
const handleCreateFolder = async (folderName: string) => {
|
||||
// Use targetPath instead of fullPath
|
||||
const newFolderPath = path.join(targetPath, folderName);
|
||||
|
||||
// Change from createNewFolder to createFolder
|
||||
const success = await workbenchStore.createFolder(newFolderPath);
|
||||
|
||||
if (success) {
|
||||
@ -385,10 +377,8 @@ function FileContextMenu({
|
||||
setIsCreatingFolder(false);
|
||||
};
|
||||
|
||||
// Add delete handler function
|
||||
const handleDelete = async () => {
|
||||
try {
|
||||
// Confirm deletion with the user
|
||||
if (!confirm(`Are you sure you want to delete ${isFolder ? 'folder' : 'file'}: ${fileName}?`)) {
|
||||
return;
|
||||
}
|
||||
|
@ -182,29 +182,23 @@ export class FilesStore {
|
||||
|
||||
const currentFiles = this.files.get();
|
||||
|
||||
// Process each deleted path
|
||||
for (const deletedPath of this.#deletedPaths) {
|
||||
// Remove the path itself
|
||||
if (currentFiles[deletedPath]) {
|
||||
this.files.setKey(deletedPath, undefined);
|
||||
|
||||
// Adjust file count if it was a file
|
||||
if (currentFiles[deletedPath]?.type === 'file') {
|
||||
this.#size--;
|
||||
}
|
||||
}
|
||||
|
||||
// Also remove any files/folders inside deleted folders
|
||||
for (const [path, dirent] of Object.entries(currentFiles)) {
|
||||
if (path.startsWith(deletedPath + '/')) {
|
||||
this.files.setKey(path, undefined);
|
||||
|
||||
// Adjust file count if it was a file
|
||||
if (dirent?.type === 'file') {
|
||||
this.#size--;
|
||||
}
|
||||
|
||||
// Remove from modified files tracking if present
|
||||
if (dirent?.type === 'file' && this.#modifiedFiles.has(path)) {
|
||||
this.#modifiedFiles.delete(path);
|
||||
}
|
||||
@ -225,7 +219,6 @@ export class FilesStore {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Also skip if this is a file/folder inside a deleted folder
|
||||
let isInDeletedFolder = false;
|
||||
|
||||
for (const deletedPath of this.#deletedPaths) {
|
||||
@ -280,11 +273,9 @@ export class FilesStore {
|
||||
}
|
||||
}
|
||||
|
||||
// Check if we already have this file with content
|
||||
const existingFile = this.files.get()[sanitizedPath];
|
||||
|
||||
if (existingFile?.type === 'file' && existingFile.isBinary && existingFile.content && !content) {
|
||||
// Keep existing binary content if new content is empty
|
||||
content = existingFile.content;
|
||||
}
|
||||
|
||||
@ -327,34 +318,27 @@ export class FilesStore {
|
||||
throw new Error(`EINVAL: invalid file path, create '${relativePath}'`);
|
||||
}
|
||||
|
||||
// Create parent directories if they don't exist
|
||||
const dirPath = path.dirname(relativePath);
|
||||
|
||||
if (dirPath !== '.') {
|
||||
await webcontainer.fs.mkdir(dirPath, { recursive: true });
|
||||
}
|
||||
|
||||
// Detect binary content
|
||||
const isBinary = content instanceof Uint8Array;
|
||||
|
||||
if (isBinary) {
|
||||
await webcontainer.fs.writeFile(relativePath, Buffer.from(content));
|
||||
|
||||
// Store Base64 encoded data instead of an empty string
|
||||
const base64Content = Buffer.from(content).toString('base64');
|
||||
this.files.setKey(filePath, { type: 'file', content: base64Content, isBinary: true });
|
||||
|
||||
// Store the base64 content as the original content for tracking modifications
|
||||
this.#modifiedFiles.set(filePath, base64Content);
|
||||
} else {
|
||||
// Ensure we write at least a space character for empty files to ensure they're tracked
|
||||
const contentToWrite = (content as string).length === 0 ? ' ' : content;
|
||||
await webcontainer.fs.writeFile(relativePath, contentToWrite);
|
||||
|
||||
// But store the actual empty string in our file map if that's what was requested
|
||||
this.files.setKey(filePath, { type: 'file', content: content as string, isBinary: false });
|
||||
|
||||
// Store the text content as the original content
|
||||
this.#modifiedFiles.set(filePath, content as string);
|
||||
}
|
||||
|
||||
@ -379,7 +363,6 @@ export class FilesStore {
|
||||
|
||||
await webcontainer.fs.mkdir(relativePath, { recursive: true });
|
||||
|
||||
// Immediately update the folder in our store without waiting for the watcher
|
||||
this.files.setKey(folderPath, { type: 'folder' });
|
||||
|
||||
logger.info(`Folder created: ${folderPath}`);
|
||||
@ -403,19 +386,15 @@ export class FilesStore {
|
||||
|
||||
await webcontainer.fs.rm(relativePath);
|
||||
|
||||
// Add to deleted paths set
|
||||
this.#deletedPaths.add(filePath);
|
||||
|
||||
// Immediately update our store without waiting for the watcher
|
||||
this.files.setKey(filePath, undefined);
|
||||
this.#size--;
|
||||
|
||||
// Remove from modified files tracking if present
|
||||
if (this.#modifiedFiles.has(filePath)) {
|
||||
this.#modifiedFiles.delete(filePath);
|
||||
}
|
||||
|
||||
// Persist the deleted paths to localStorage for extra durability
|
||||
this.#persistDeletedPaths();
|
||||
|
||||
logger.info(`File deleted: ${filePath}`);
|
||||
@ -439,35 +418,28 @@ export class FilesStore {
|
||||
|
||||
await webcontainer.fs.rm(relativePath, { recursive: true });
|
||||
|
||||
// Add to deleted paths set
|
||||
this.#deletedPaths.add(folderPath);
|
||||
|
||||
// Immediately update our store without waiting for the watcher
|
||||
this.files.setKey(folderPath, undefined);
|
||||
|
||||
// Also remove all files and subfolders from our store
|
||||
const allFiles = this.files.get();
|
||||
|
||||
for (const [path, dirent] of Object.entries(allFiles)) {
|
||||
if (path.startsWith(folderPath + '/')) {
|
||||
this.files.setKey(path, undefined);
|
||||
|
||||
// Also add these paths to the deleted paths set
|
||||
this.#deletedPaths.add(path);
|
||||
|
||||
// Decrement file count for each file (not folder) removed
|
||||
if (dirent?.type === 'file') {
|
||||
this.#size--;
|
||||
}
|
||||
|
||||
// Remove from modified files tracking if present
|
||||
if (dirent?.type === 'file' && this.#modifiedFiles.has(path)) {
|
||||
this.#modifiedFiles.delete(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Persist the deleted paths to localStorage for extra durability
|
||||
this.#persistDeletedPaths();
|
||||
|
||||
logger.info(`Folder deleted: ${folderPath}`);
|
||||
@ -479,7 +451,7 @@ export class FilesStore {
|
||||
}
|
||||
}
|
||||
|
||||
// Add a method to persist deleted paths to localStorage
|
||||
// method to persist deleted paths to localStorage
|
||||
#persistDeletedPaths() {
|
||||
try {
|
||||
if (typeof localStorage !== 'undefined') {
|
||||
|
@ -262,7 +262,6 @@ export class WorkbenchStore {
|
||||
const success = await this.#filesStore.createFile(filePath, content);
|
||||
|
||||
if (success) {
|
||||
// If the file is created successfully, select it in the editor
|
||||
this.setSelectedFile(filePath);
|
||||
|
||||
/*
|
||||
@ -294,15 +293,12 @@ export class WorkbenchStore {
|
||||
|
||||
async deleteFile(filePath: string) {
|
||||
try {
|
||||
// Check if the file is currently open in the editor
|
||||
const currentDocument = this.currentDocument.get();
|
||||
const isCurrentFile = currentDocument?.filePath === filePath;
|
||||
|
||||
// Delete the file
|
||||
const success = await this.#filesStore.deleteFile(filePath);
|
||||
|
||||
if (success) {
|
||||
// Remove from unsaved files if present
|
||||
const newUnsavedFiles = new Set(this.unsavedFiles.get());
|
||||
|
||||
if (newUnsavedFiles.has(filePath)) {
|
||||
@ -310,9 +306,7 @@ export class WorkbenchStore {
|
||||
this.unsavedFiles.set(newUnsavedFiles);
|
||||
}
|
||||
|
||||
// If this was the current file, select another file
|
||||
if (isCurrentFile) {
|
||||
// Find another file to select
|
||||
const files = this.files.get();
|
||||
let nextFile: string | undefined = undefined;
|
||||
|
||||
@ -336,15 +330,12 @@ export class WorkbenchStore {
|
||||
|
||||
async deleteFolder(folderPath: string) {
|
||||
try {
|
||||
// Check if any file in this folder is currently open
|
||||
const currentDocument = this.currentDocument.get();
|
||||
const isInCurrentFolder = currentDocument?.filePath?.startsWith(folderPath + '/');
|
||||
|
||||
// Delete the folder
|
||||
const success = await this.#filesStore.deleteFolder(folderPath);
|
||||
|
||||
if (success) {
|
||||
// Remove any files in this folder from unsaved files
|
||||
const unsavedFiles = this.unsavedFiles.get();
|
||||
const newUnsavedFiles = new Set<string>();
|
||||
|
||||
@ -358,9 +349,7 @@ export class WorkbenchStore {
|
||||
this.unsavedFiles.set(newUnsavedFiles);
|
||||
}
|
||||
|
||||
// If current file was in this folder, select another file
|
||||
if (isInCurrentFolder) {
|
||||
// Find another file to select
|
||||
const files = this.files.get();
|
||||
let nextFile: string | undefined = undefined;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user