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 PATH_COMPONENT_SEPARATOR = "\\";
var StorageModel = class _StorageModel { var StorageModel = class _StorageModel {
storageEntryTree = {}; storageEntryTree = {};
// basic // read
write = (pathComponents, value) => {
const pathString = _StorageModel.pathComponentsToString(
...pathComponents
);
localStorage.setItem(pathString, value);
this.updateTree(...pathComponents);
};
read = (pathComponents) => { read = (pathComponents) => {
const pathString = _StorageModel.pathComponentsToString( const pathString = _StorageModel.pathComponentsToString(
...pathComponents ...pathComponents
); );
return localStorage.getItem(pathString); 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) => { list = (pathComponents) => {
let currentParent = this.storageEntryTree; let currentParent = this.storageEntryTree;
for (const component of pathComponents) { for (const component of pathComponents) {
@@ -386,13 +358,68 @@
} }
return [...Object.keys(currentParent).sort(localeCompare)]; 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); const content = this.read(sourcePathComponents);
if (content == null) return false; if (content == null) return false;
this.write(destinationPathComponents, content); this.write(destinationPathComponents, content);
this.remove(sourcePathComponents); this.remove(sourcePathComponents);
if (shouldInitialize == true) {
this.initializeTree();
}
return true; 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 // stringifiable
writeStringifiable = (pathComponents, value) => { writeStringifiable = (pathComponents, value) => {
const valueString = stringify(value); const valueString = stringify(value);
@@ -439,7 +466,7 @@
return pathComponents[pathComponents.length - 1] || "\\"; return pathComponents[pathComponents.length - 1] || "\\";
}; };
static pathComponentsToString = (...pathComponents) => { static pathComponentsToString = (...pathComponents) => {
return pathComponents.join(PATH_COMPONENT_SEPARATOR); return pathComponents.filter((x) => x != "").join(PATH_COMPONENT_SEPARATOR);
}; };
static stringToPathComponents = (string) => { static stringToPathComponents = (string) => {
return string.split(PATH_COMPONENT_SEPARATOR).filter((x) => x != ""); return string.split(PATH_COMPONENT_SEPARATOR).filter((x) => x != "");
@@ -549,6 +576,14 @@
const boardNames = this.storageModel.list(boardDirPath); const boardNames = this.storageModel.list(boardDirPath);
return boardNames; return boardNames;
}; };
getBoardInfo = (boardId) => {
const boardInfoPath = this.getBoardInfoPath(boardId);
const boardInfoFileContentOrNull = this.storageModel.readStringifiable(
boardInfoPath,
BoardInfoFileContentReference
);
return boardInfoFileContentOrNull;
};
//tasks //tasks
createTask = (boardId, name) => { createTask = (boardId, name) => {
const file = FileModel.createFile(); const file = FileModel.createFile();
@@ -580,23 +615,9 @@
); );
return taskFileContentOrNull; return taskFileContentOrNull;
}; };
// locations moveTask = (taskFileId, oldBoardId, newBoardId) => {
getTaskLocation = (file) => { const sourcePath = this.getTaskPath(oldBoardId, taskFileId);
const locationPath = this.getTaskLocationPath(file.id); const destinationPath = this.getTaskPath(newBoardId, taskFileId);
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);
return this.storageModel.rename(sourcePath, destinationPath); return this.storageModel.rename(sourcePath, destinationPath);
}; };
// init // init
@@ -645,10 +666,6 @@
name: "", name: "",
boardId: "" boardId: ""
}; };
var TaskLocationReference = {
dataVersion: DATA_VERSION,
boardId: ""
};
// src/Model/Files/fileModel.ts // src/Model/Files/fileModel.ts
var FileModel = class _FileModel { var FileModel = class _FileModel {
+85 -31
View File
@@ -12,7 +12,26 @@ export const PATH_COMPONENT_SEPARATOR = "\\";
export default class StorageModel { export default class StorageModel {
storageEntryTree: StorageEntry = {}; 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 => { write = (pathComponents: string[], value: string): void => {
const pathString: string = StorageModel.pathComponentsToString( const pathString: string = StorageModel.pathComponentsToString(
...pathComponents ...pathComponents
@@ -21,13 +40,6 @@ export default class StorageModel {
this.updateTree(...pathComponents); this.updateTree(...pathComponents);
}; };
read = (pathComponents: string[]): string | null => {
const pathString: string = StorageModel.pathComponentsToString(
...pathComponents
);
return localStorage.getItem(pathString);
};
remove = ( remove = (
pathComponents: string[], pathComponents: string[],
shouldInitialize: boolean = true shouldInitialize: boolean = true
@@ -42,43 +54,86 @@ export default class StorageModel {
} }
}; };
removeRecursively = (pathComponentsOfEntityToDelete: string[]): void => { removeRecursively = (pathComponents: string[]): void => {
a: for (const key of Object.keys(localStorage)) { loop_over_files: for (const key of Object.keys(localStorage)) {
// get path of current entity
const pathComponentsOfCurrentEntity: string[] = const pathComponentsOfCurrentEntity: string[] =
StorageModel.stringToPathComponents(key); StorageModel.stringToPathComponents(key);
for (let i = 0; i < pathComponentsOfEntityToDelete.length; i++) { // exit if entity does not match
if (!pathComponentsOfCurrentEntity[i]) continue a; loop_over_path_components: for (
if ( let i = 0;
pathComponentsOfCurrentEntity[i] != pathComponentsOfEntityToDelete[i] i < pathComponents.length;
) i++
continue a; ) {
if (!pathComponentsOfCurrentEntity[i]) continue loop_over_files;
if (pathComponentsOfCurrentEntity[i] != pathComponents[i])
continue loop_over_files;
} }
// remove
this.remove(pathComponentsOfCurrentEntity, false); this.remove(pathComponentsOfCurrentEntity, false);
} }
this.initializeTree(); this.initializeTree();
}; };
list = (pathComponents: string[]): string[] => { rename = (
let currentParent: StorageEntry = this.storageEntryTree; sourcePathComponents: string[],
for (const component of pathComponents) { destinationPathComponents: string[],
const nextParent: StorageEntry | undefined = currentParent[component]; shouldInitialize: boolean = true
if (nextParent == undefined) return []; ): boolean => {
currentParent = nextParent;
}
return [...Object.keys(currentParent).sort(localeCompare)];
};
rename = (sourcePathComponents: string[], destinationPathComponents: string[]): boolean => {
const content: string | null = this.read(sourcePathComponents); const content: string | null = this.read(sourcePathComponents);
if (content == null) return false; if (content == null) return false;
this.write(destinationPathComponents, content); this.write(destinationPathComponents, content);
this.remove(sourcePathComponents); 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 // stringifiable
writeStringifiable = (pathComponents: string[], value: ValidObject): void => { writeStringifiable = (pathComponents: string[], value: ValidObject): void => {
@@ -99,7 +154,6 @@ export default class StorageModel {
return object; return object;
}; };
// tree // tree
initializeTree = (): void => { initializeTree = (): void => {
console.log("initializing tree"); console.log("initializing tree");
@@ -142,7 +196,7 @@ export default class StorageModel {
}; };
static pathComponentsToString = (...pathComponents: string[]): string => { 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[] => { static stringToPathComponents = (string: string): string[] => {