Skip to content

Commit

Permalink
Added rename action, refactor of how paths are handled by FileSystemPath
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorthiz committed Oct 19, 2024
1 parent 0619be4 commit 67680e8
Show file tree
Hide file tree
Showing 25 changed files with 311 additions and 108 deletions.
23 changes: 23 additions & 0 deletions src/Components/FileTree/Actions/FileTreeMoveAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {FileSystemPath} from "../FileSystemsTypes";
import {useFileSystems} from "../Store/FileSystemsStore";
import {useAppStore} from "../../../Stores/AppStore";

const updateCurrentlyLoadedFilesIfNeeded = (currentPath: FileSystemPath, newPath: FileSystemPath) => {
const fileSystemAppStore = useAppStore();
if (fileSystemAppStore.selectedKsyInfo.isPartOf(currentPath)) {
const newCurrentlyLoadedFilePath = fileSystemAppStore.selectedKsyInfo.replacePathPart(currentPath, newPath);
fileSystemAppStore.updateSelectedKsyFile(newCurrentlyLoadedFilePath);
}

if (fileSystemAppStore.selectedBinaryInfo.isPartOf(currentPath)) {
const newCurrentlyLoadedFilePath = fileSystemAppStore.selectedBinaryInfo.replacePathPart(currentPath, newPath);
fileSystemAppStore.updateSelectedKsyFile(newCurrentlyLoadedFilePath);
}
};

export const FileTreeMoveAction = async (oldPath: FileSystemPath, newPath: FileSystemPath) => {
const fileSystemsStore = useFileSystems();
await fileSystemsStore.move(oldPath, newPath);
updateCurrentlyLoadedFilesIfNeeded(oldPath, newPath);
fileSystemsStore.selectPath(newPath)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {MenuItem} from "@imengyu/vue3-context-menu/lib/ContextMenuDefine";
import {h} from "vue";
import {useFileSystems} from "../../Store/FileSystemsStore";
import {FolderIcon} from "@heroicons/vue/16/solid";
import {FileSystemPath} from "../../FileSystemsTypes";

export const FileTreeCtxActionCloseDirectory = (item: TreeNodeDisplay): MenuItem => {
const action = () => {
const fileSystemsStore = useFileSystems();
fileSystemsStore.closePath(item.fullPathWithStore);
fileSystemsStore.closePath(FileSystemPath.fromFullPathWithStore(item.fullPathWithStore));
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {h} from "vue";
import {useFileSystems} from "../../Store/FileSystemsStore";
import {FILE_SYSTEM_TYPE_KAITAI} from "../../FileSystems/KaitaiFileSystem";
import {FolderPlusIcon} from "@heroicons/vue/16/solid";
import {FileSystemPath} from "../../FileSystemsTypes";

export const FileTreeCtxActionCreateDirectory = (item: TreeNodeDisplay): MenuItem => {
const action = () => {
Expand All @@ -12,10 +13,9 @@ export const FileTreeCtxActionCreateDirectory = (item: TreeNodeDisplay): MenuIte
const fullPathToNewFolder = item.fullPath
? `${item.fullPath}/${newFolderName}`
: newFolderName;
const fullPathToNewFolderWithStore = `${item.storeId}:${fullPathToNewFolder}`;
fileStore.createDirectory(item.storeId, fullPathToNewFolder);
fileStore.openPath(item.fullPathWithStore);
fileStore.selectPath(fullPathToNewFolderWithStore);
fileStore.openPath(FileSystemPath.fromFullPathWithStore(item.fullPathWithStore));
fileStore.selectPath(FileSystemPath.of(item.storeId, fullPathToNewFolder));
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import {FILE_SYSTEM_TYPE_KAITAI} from "../../FileSystems/KaitaiFileSystem";
import {createNewKsyAction} from "../../../../GlobalActions/CreateNewKsyAction";
import {useTextModalInputStore} from "../../../Modals/TextInputModal/TextInputModalStore";
import {DocumentPlusIcon} from "@heroicons/vue/16/solid";
import {FileSystemPath} from "../../FileSystemsTypes";

export const FileTreeCtxActionCreateKsy = (item: TreeNodeDisplay): MenuItem => {
const action = () => {
const store = useTextModalInputStore();
store.open({
title: "Add new KSY",
onAccept: (fileName) => {
createNewKsyAction(item.storeId, item.fullPath, fileName);
createNewKsyAction(FileSystemPath.fromFullPathWithStore(item.fullPathWithStore), fileName);
},
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {h} from "vue";
import {useFileSystems} from "../../Store/FileSystemsStore";
import {FILE_SYSTEM_TYPE_KAITAI} from "../../FileSystems/KaitaiFileSystem";
import {TrashIcon} from "@heroicons/vue/16/solid";
import {FileSystemPath} from "../../FileSystemsTypes";

export const FileTreeCtxActionDelete = (item: TreeNodeDisplay): MenuItem => {
const action = async () => {
const fileSystemStore = useFileSystems();
fileSystemStore.deletePath(item.storeId, item.fullPath);
fileSystemStore.deletePath(FileSystemPath.fromFullPathWithStore(item.fullPathWithStore));
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {MenuItem} from "@imengyu/vue3-context-menu/lib/ContextMenuDefine";
import {h} from "vue";
import {FileActionsWrapper} from "../../../../Utils/Files/FileActionsWrapper";
import {useFileSystems} from "../../Store/FileSystemsStore";
import {BoltIcon, CloudArrowDownIcon} from "@heroicons/vue/16/solid";
import {CloudArrowDownIcon} from "@heroicons/vue/16/solid";

export const FileTreeCtxActionDownload = (item: TreeNodeDisplay): MenuItem => {
const action = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,32 @@ export const mapFileTreeDisplayNodeToYaml = async (item: TreeNodeDisplay): Promi


export const FileTreeCtxActionGenerateParser = (item: TreeNodeDisplay): MenuItem => {

const toSimpleSnakeCase = (name: string) => {
return name.replace(/\W+/g, " ")
.split(/ |\B(?=[A-Z])/)
.map(word => word.toLowerCase())
.join("_");
};

const createOnlyMainFileTab = (compiled: CompilationTarget, language: SupportedLanguage) => {
const [name, content] = Object.entries(compiled.result)[0];
const record = Object.entries(compiled.result)
.find(([name, content]) => {
const nameInSnakeCase = toSimpleSnakeCase(name);
return nameInSnakeCase.indexOf(compiled.mainClassId) !== -1;
});
if (!record) {
console.error("[GenerateOnlyMainFile] Could not find main file using schema!");
return;
}
const [name, content] = record;
CurrentGoldenLayout.addDynamicCodeTab(name, content, language.monacoEditorLangCode);
return true;
};

const createTabsForAllGeneratedFiles = (compiled: CompilationTarget, language: SupportedLanguage) => {
Object.entries(compiled.result).reverse().forEach(([name, content]) => {
Object.entries(compiled.result).forEach(([name, content]) => {
CurrentGoldenLayout.addDynamicCodeTab(name, content, language.monacoEditorLangCode);
});
return true;
};

const generateParserForLanguage = async (language: SupportedLanguage) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import {MenuItem} from "@imengyu/vue3-context-menu/lib/ContextMenuDefine";
import {h} from "vue";
import {useAppStore} from "../../../../Stores/AppStore";
import {DocumentIcon} from "@heroicons/vue/16/solid";
import {FileSystemPath} from "../../FileSystemsTypes";

export const FileTreeCtxActionOpenBinFile = (item: TreeNodeDisplay): MenuItem => {
const action = () => {
const appStore = useAppStore();
appStore.updateSelectedBinaryFile({
storeId: item.storeId,
filePath: item.fullPath
});
appStore.updateSelectedBinaryFile(FileSystemPath.fromFullPathWithStore(item.fullPathWithStore));
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import {MenuItem} from "@imengyu/vue3-context-menu/lib/ContextMenuDefine";
import {h} from "vue";
import {useFileSystems} from "../../Store/FileSystemsStore";
import {FolderIcon} from "@heroicons/vue/16/solid";
import {FileSystemPath} from "../../FileSystemsTypes";

export const FileTreeCtxActionOpenDirectory = (item: TreeNodeDisplay): MenuItem => {
const action = () => {
const fileSystemsStore = useFileSystems();
fileSystemsStore.openPath(item.fullPathWithStore);
fileSystemsStore.openPath(FileSystemPath.fromFullPathWithStore(item.fullPathWithStore));
};

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import {MenuItem} from "@imengyu/vue3-context-menu/lib/ContextMenuDefine";
import {h} from "vue";
import {useAppStore} from "../../../../Stores/AppStore";
import {DocumentTextIcon} from "@heroicons/vue/16/solid";
import {FileSystemPath} from "../../FileSystemsTypes";

export const FileTreeCtxActionOpenKsy = (item: TreeNodeDisplay): MenuItem => {
const action = async () => {
const appStore = useAppStore();
appStore.updateSelectedKsyFile({
storeId: item.storeId,
filePath: item.fullPath
});
appStore.updateSelectedKsyFile(FileSystemPath.fromFullPathWithStore(item.fullPathWithStore));
};

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {TreeNodeDisplay} from "../../FileSystemVisitors/FileSystemFileTreeMapper";
import {MenuItem} from "@imengyu/vue3-context-menu/lib/ContextMenuDefine";
import {h} from "vue";
import {PencilIcon} from "@heroicons/vue/16/solid";
import {useTextModalInputStore} from "../../../Modals/TextInputModal/TextInputModalStore";
import {useAppStore} from "../../../../Stores/AppStore";
import {FileSystemPath} from "../../FileSystemsTypes";
import {FILE_SYSTEM_TYPE_KAITAI} from "../../FileSystems/KaitaiFileSystem";
import {FileTreeMoveAction} from "../../Actions/FileTreeMoveAction";

export const FileTreeCtxActionRename = (item: TreeNodeDisplay): MenuItem => {


const action = async () => {
const modalInputStore = useTextModalInputStore();
modalInputStore.open({
title: "Rename",
initValue: item.fileName,
onAccept: async (newName) => {
const newFullPathString = item.fullPath.replace(item.fileName, newName);
const oldPath = FileSystemPath.fromFullPathWithStore(item.fullPathWithStore);
const newPath = FileSystemPath.of(item.storeId, newFullPathString);

FileTreeMoveAction(oldPath, newPath);
},
});
};

return {
label: "Rename",
onClick: action,
// hidden: [TreeNodeDisplayType.KSY_FILE].indexOf(item.type) === -1,
customClass: "context-menu-item",
disabled: item.storeId === FILE_SYSTEM_TYPE_KAITAI || item.fullPath === "",
icon: () => h(PencilIcon),
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,29 @@ import {FileTreeCtxActionOpenDirectory} from "./Actions/FileTreeCtxActionOpenDir
import {FileTreeCtxActionCloseDirectory} from "./Actions/FileTreeCtxActionCloseDirectory";
import {FileTreeCtxActionCreateDirectory} from "./Actions/FileTreeCtxActionCreateDirectory";
import {FileTreeCtxActionCreateKsy} from "./Actions/FileTreeCtxActionCreateKsy";
import {FileTreeCtxActionRename} from "./Actions/FileTreeCtxActionRename";

export const prepareContextMenuOptions = (e: MouseEvent, item: TreeNodeDisplay): MenuOptions => {

return {
x: e.x,
y: e.y,
customClass: "menu-trick",
theme: "flat dark",
clickCloseOnOutside: true,
items: [
// {
// label: "Restore old files",
// onClick: () => RestoreOldFileTreeAction(),
// customClass: "context-menu-item",
// hidden: item.storeId === FILE_SYSTEM_TYPE_KAITAI || item.fullPath !== ""
// },
FileTreeCtxActionOpenKsy(item),
FileTreeCtxActionOpenBinFile(item),
FileTreeCtxActionOpenDirectory(item),
FileTreeCtxActionCloseDirectory(item),
FileTreeCtxActionCreateDirectory(item),
FileTreeCtxActionCreateKsy(item),
FileTreeCtxActionRename(item),
FileTreeCtxActionClone(item),
FileTreeCtxActionGenerateParser(item),
FileTreeCtxActionDownload(item),
Expand Down
7 changes: 6 additions & 1 deletion src/Components/FileTree/FileSystems/KaitaiFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const FILE_SYSTEM_TYPE_KAITAI = "kaitai";

const initKaitaiFs = () => {
const root = <FileSystemItem>{fsType: FILE_SYSTEM_TYPE_KAITAI, children: {}, fn: "kaitai.io", type: ITEM_MODE_DIRECTORY};
(kaitaiFsFiles || []).forEach(fn => FsItemHelper.createFileOrDirectoryFromPathInRoot(root, fn));
(kaitaiFsFiles || []).forEach(path => FsItemHelper.createFileOrDirectoryFromPathInRoot(root, path));
return root;
};

Expand Down Expand Up @@ -40,6 +40,11 @@ export class KaitaiFileSystem implements FileSystem {
async delete(filePath: string): Promise<void> {
return Promise.reject("KaitaiFileSystem.delete is not supported!");
}

move(oldPath: string, newPath: string): Promise<void> {
return Promise.reject("KaitaiFileSystem.move is not supported!");

}
}


20 changes: 19 additions & 1 deletion src/Components/FileTree/FileSystems/LocalStorageFileSystem.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {FsItemHelper} from "../Utils/FsItemHelper";
import {FileSystem, FileSystemItem, ITEM_MODE_DIRECTORY} from "../FileSystemsTypes";
import {FileSystem, FileSystemItem, ITEM_MODE_DIRECTORY, ITEM_MODE_FILE} from "../FileSystemsTypes";
import {LocalForageForLocalStorageWrapper} from "../Utils/LocalForageForLocalStorageWrapper";
import {toRaw} from "vue";

Expand Down Expand Up @@ -77,4 +77,22 @@ export class LocalStorageFileSystem implements FileSystem {
LocalForageForLocalStorageWrapper.deleteFile(LocalStorageFileSystem.fileLocalForageKey(fileName));
});
}

async move(oldPath: string, newPath: string): Promise<void> {
const oldPathInfo = FsItemHelper.getInfoAboutPath(this.root, oldPath);
FsItemHelper.createFileOrDirectoryFromPathInRoot(this.root, newPath, oldPathInfo.node.type === ITEM_MODE_DIRECTORY);
const newPathInfo = FsItemHelper.getInfoAboutPath(this.root, newPath);
delete oldPathInfo.parentNode.children[oldPathInfo.nodeName];
newPathInfo.parentNode.children[newPathInfo.nodeName] = oldPathInfo.node;
if(oldPathInfo.node.type === ITEM_MODE_FILE) {
const oldFileForageKey = LocalStorageFileSystem.fileLocalForageKey(oldPathInfo.nodeName)
const newFileForageKey = LocalStorageFileSystem.fileLocalForageKey(newPathInfo.nodeName)
const content = await LocalForageForLocalStorageWrapper.getFile(oldFileForageKey)
await LocalForageForLocalStorageWrapper.saveFile(newFileForageKey, content)
}
await this.updateTreeInDatabase();
return Promise.resolve(undefined);
}


}
39 changes: 39 additions & 0 deletions src/Components/FileTree/FileSystemsTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,43 @@ export interface FileSystemItem {
children?: { [key: string]: FileSystemItem; };
}

export class FileSystemPath {

constructor(public storeId: string, public path: string) {
}

public static of(storeId: string, path: string) {
return new FileSystemPath(storeId, path);
}

public static fromFullPathWithStore(fullPathWithStore: string) {
const [storeId, path] = fullPathWithStore.split(":");
return new FileSystemPath(storeId, path);
}

public isInTheSameStore(other: FileSystemPath) {
return this.storeId === other.storeId
}

public isTheSame(other: FileSystemPath) {
return this.storeId === other.storeId &&
this.path === other.path;
}

public isPartOf(other: FileSystemPath) {
return this.storeId === other.storeId &&
this.path.startsWith(other.path);
}

public replacePathPart(oldFragment: FileSystemPath, newFragment: FileSystemPath): FileSystemPath {
const newPath = this.path.replace(oldFragment.path, newFragment.path);
return FileSystemPath.of(this.storeId, newPath);
}
public toString() {
return `${this.storeId}:${this.path}`;
}
}

export interface FileSystem {
storeId: string;

Expand All @@ -22,4 +59,6 @@ export interface FileSystem {
createDirectory(filePath: string): Promise<void>;

delete(filePath: string): Promise<void>;

move(oldPath: string, newPath: string): Promise<void>;
}
Loading

0 comments on commit 67680e8

Please sign in to comment.