add removeRecursively

This commit is contained in:
marlon-erler
2024-07-22 15:06:01 +02:00
parent ae4339b4d2
commit a9181d8610
2 changed files with 155 additions and 84 deletions
+69 -52
View File
@@ -342,41 +342,13 @@
var PATH_COMPONENT_SEPARATOR = "\\";
var StorageModel = class _StorageModel {
storageEntryTree = {};
// basic
write = (pathComponents, value) => {
const pathString = _StorageModel.pathComponentsToString(
...pathComponents
);
localStorage.setItem(pathString, value);
this.updateTree(...pathComponents);
};
// read
read = (pathComponents) => {
const pathString = _StorageModel.pathComponentsToString(
...pathComponents
);
return localStorage.getItem(pathString);
};
remove = (pathComponents, shouldInitialize = true) => {
const pathString = _StorageModel.pathComponentsToString(
...pathComponents
);
localStorage.removeItem(pathString);
if (shouldInitialize == true) {
this.initializeTree();
}
};
removeRecursively = (pathComponentsOfEntityToDelete) => {
a: for (const key of Object.keys(localStorage)) {
const pathComponentsOfCurrentEntity = _StorageModel.stringToPathComponents(key);
for (let i = 0; i < pathComponentsOfEntityToDelete.length; i++) {
if (!pathComponentsOfCurrentEntity[i]) continue a;
if (pathComponentsOfCurrentEntity[i] != pathComponentsOfEntityToDelete[i])
continue a;
}
this.remove(pathComponentsOfCurrentEntity, false);
}
this.initializeTree();
};
list = (pathComponents) => {
let currentParent = this.storageEntryTree;
for (const component of pathComponents) {
@@ -386,13 +358,68 @@
}
return [...Object.keys(currentParent).sort(localeCompare)];
};
rename = (sourcePathComponents, destinationPathComponents) => {
// write
write = (pathComponents, value) => {
const pathString = _StorageModel.pathComponentsToString(
...pathComponents
);
localStorage.setItem(pathString, value);
this.updateTree(...pathComponents);
};
remove = (pathComponents, shouldInitialize = true) => {
const pathString = _StorageModel.pathComponentsToString(
...pathComponents
);
localStorage.removeItem(pathString);
if (shouldInitialize == true) {
this.initializeTree();
}
};
removeRecursively = (pathComponents) => {
loop_over_files: for (const key of Object.keys(localStorage)) {
const pathComponentsOfCurrentEntity = _StorageModel.stringToPathComponents(key);
loop_over_path_components: for (let i = 0; i < pathComponents.length; i++) {
if (!pathComponentsOfCurrentEntity[i]) continue loop_over_files;
if (pathComponentsOfCurrentEntity[i] != pathComponents[i])
continue loop_over_files;
}
this.remove(pathComponentsOfCurrentEntity, false);
}
this.initializeTree();
};
rename = (sourcePathComponents, destinationPathComponents, shouldInitialize = true) => {
const content = this.read(sourcePathComponents);
if (content == null) return false;
this.write(destinationPathComponents, content);
this.remove(sourcePathComponents);
if (shouldInitialize == true) {
this.initializeTree();
}
return true;
};
renameRecursively = (sourcePathComponents, destinationPathComponents) => {
loop_over_files: for (const key of Object.keys(localStorage)) {
const originalPathComponentsOfCurrentEntity = _StorageModel.stringToPathComponents(key);
loop_over_path_components: for (let i = 0; i < sourcePathComponents.length; i++) {
if (!originalPathComponentsOfCurrentEntity[i]) continue loop_over_files;
if (originalPathComponentsOfCurrentEntity[i] != sourcePathComponents[i])
continue loop_over_files;
}
const relativePathOfCurrentEntity = originalPathComponentsOfCurrentEntity.slice(
sourcePathComponents.length
);
const destinationPathComponentsOfCurrentEntity = [
...destinationPathComponents,
...relativePathOfCurrentEntity
];
this.rename(
originalPathComponentsOfCurrentEntity,
destinationPathComponentsOfCurrentEntity,
false
);
}
this.initializeTree();
};
// stringifiable
writeStringifiable = (pathComponents, value) => {
const valueString = stringify(value);
@@ -439,7 +466,7 @@
return pathComponents[pathComponents.length - 1] || "\\";
};
static pathComponentsToString = (...pathComponents) => {
return pathComponents.join(PATH_COMPONENT_SEPARATOR);
return pathComponents.filter((x) => x != "").join(PATH_COMPONENT_SEPARATOR);
};
static stringToPathComponents = (string) => {
return string.split(PATH_COMPONENT_SEPARATOR).filter((x) => x != "");
@@ -549,6 +576,14 @@
const boardNames = this.storageModel.list(boardDirPath);
return boardNames;
};
getBoardInfo = (boardId) => {
const boardInfoPath = this.getBoardInfoPath(boardId);
const boardInfoFileContentOrNull = this.storageModel.readStringifiable(
boardInfoPath,
BoardInfoFileContentReference
);
return boardInfoFileContentOrNull;
};
//tasks
createTask = (boardId, name) => {
const file = FileModel.createFile();
@@ -580,23 +615,9 @@
);
return taskFileContentOrNull;
};
// locations
getTaskLocation = (file) => {
const locationPath = this.getTaskLocationPath(file.id);
const locationOrNull = this.storageModel.readStringifiable(locationPath, TaskLocationReference);
return locationOrNull;
};
storeTaskLocation = (file, taskLocation) => {
const locationPath = this.getTaskLocationPath(file.id);
this.storageModel.writeStringifiable(locationPath, taskLocation);
};
updateTaskLocation = (file, newLocation) => {
const previousLocation = this.getTaskLocation(file);
if (previousLocation == null) return false;
const oldBoardName = previousLocation.boardId;
const sourcePath = this.getTaskPath(oldBoardName, file.id);
const newBoardName = newLocation.boardId;
const destinationPath = this.getTaskPath(newBoardName, file.id);
moveTask = (taskFileId, oldBoardId, newBoardId) => {
const sourcePath = this.getTaskPath(oldBoardId, taskFileId);
const destinationPath = this.getTaskPath(newBoardId, taskFileId);
return this.storageModel.rename(sourcePath, destinationPath);
};
// init
@@ -645,10 +666,6 @@
name: "",
boardId: ""
};
var TaskLocationReference = {
dataVersion: DATA_VERSION,
boardId: ""
};
// src/Model/Files/fileModel.ts
var FileModel = class _FileModel {
+86 -32
View File
@@ -12,7 +12,26 @@ export const PATH_COMPONENT_SEPARATOR = "\\";
export default class StorageModel {
storageEntryTree: StorageEntry = {};
// basic
// read
read = (pathComponents: string[]): string | null => {
const pathString: string = StorageModel.pathComponentsToString(
...pathComponents
);
return localStorage.getItem(pathString);
};
list = (pathComponents: string[]): string[] => {
let currentParent: StorageEntry = this.storageEntryTree;
for (const component of pathComponents) {
const nextParent: StorageEntry | undefined = currentParent[component];
if (nextParent == undefined) return [];
currentParent = nextParent;
}
return [...Object.keys(currentParent).sort(localeCompare)];
};
// write
write = (pathComponents: string[], value: string): void => {
const pathString: string = StorageModel.pathComponentsToString(
...pathComponents
@@ -21,13 +40,6 @@ export default class StorageModel {
this.updateTree(...pathComponents);
};
read = (pathComponents: string[]): string | null => {
const pathString: string = StorageModel.pathComponentsToString(
...pathComponents
);
return localStorage.getItem(pathString);
};
remove = (
pathComponents: string[],
shouldInitialize: boolean = true
@@ -42,43 +54,86 @@ export default class StorageModel {
}
};
removeRecursively = (pathComponentsOfEntityToDelete: string[]): void => {
a: for (const key of Object.keys(localStorage)) {
removeRecursively = (pathComponents: string[]): void => {
loop_over_files: for (const key of Object.keys(localStorage)) {
// get path of current entity
const pathComponentsOfCurrentEntity: string[] =
StorageModel.stringToPathComponents(key);
for (let i = 0; i < pathComponentsOfEntityToDelete.length; i++) {
if (!pathComponentsOfCurrentEntity[i]) continue a;
if (
pathComponentsOfCurrentEntity[i] != pathComponentsOfEntityToDelete[i]
)
continue a;
// exit if entity does not match
loop_over_path_components: for (
let i = 0;
i < pathComponents.length;
i++
) {
if (!pathComponentsOfCurrentEntity[i]) continue loop_over_files;
if (pathComponentsOfCurrentEntity[i] != pathComponents[i])
continue loop_over_files;
}
// remove
this.remove(pathComponentsOfCurrentEntity, false);
}
this.initializeTree();
};
list = (pathComponents: string[]): string[] => {
let currentParent: StorageEntry = this.storageEntryTree;
for (const component of pathComponents) {
const nextParent: StorageEntry | undefined = currentParent[component];
if (nextParent == undefined) return [];
currentParent = nextParent;
}
return [...Object.keys(currentParent).sort(localeCompare)];
};
rename = (sourcePathComponents: string[], destinationPathComponents: string[]): boolean => {
const content: string|null = this.read(sourcePathComponents);
rename = (
sourcePathComponents: string[],
destinationPathComponents: string[],
shouldInitialize: boolean = true
): boolean => {
const content: string | null = this.read(sourcePathComponents);
if (content == null) return false;
this.write(destinationPathComponents, content);
this.remove(sourcePathComponents);
return true;
if (shouldInitialize == true) {
this.initializeTree();
}
return true;
};
renameRecursively = (
sourcePathComponents: string[],
destinationPathComponents: string[]
): void => {
loop_over_files: for (const key of Object.keys(localStorage)) {
// get path of current entity
const originalPathComponentsOfCurrentEntity: string[] =
StorageModel.stringToPathComponents(key);
// exit if entity does not match
loop_over_path_components: for (
let i = 0;
i < sourcePathComponents.length;
i++
) {
if (!originalPathComponentsOfCurrentEntity[i]) continue loop_over_files;
if (originalPathComponentsOfCurrentEntity[i] != sourcePathComponents[i])
continue loop_over_files;
}
// assemble destinationPath
const relativePathOfCurrentEntity =
originalPathComponentsOfCurrentEntity.slice(
sourcePathComponents.length
);
const destinationPathComponentsOfCurrentEntity = [
...destinationPathComponents,
...relativePathOfCurrentEntity,
];
// rename
this.rename(
originalPathComponentsOfCurrentEntity,
destinationPathComponentsOfCurrentEntity,
false
);
}
this.initializeTree();
};
// stringifiable
writeStringifiable = (pathComponents: string[], value: ValidObject): void => {
@@ -99,7 +154,6 @@ export default class StorageModel {
return object;
};
// tree
initializeTree = (): void => {
console.log("initializing tree");
@@ -142,7 +196,7 @@ export default class StorageModel {
};
static pathComponentsToString = (...pathComponents: string[]): string => {
return pathComponents.join(PATH_COMPONENT_SEPARATOR);
return pathComponents.filter((x) => x != "").join(PATH_COMPONENT_SEPARATOR);
};
static stringToPathComponents = (string: string): string[] => {