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