2024-07-21 17:55:53 +02:00
|
|
|
import * as React from "bloatless-react";
|
|
|
|
|
|
2024-07-25 16:02:49 +02:00
|
|
|
import StorageModel, {
|
2026-04-01 08:29:39 +00:00
|
|
|
PATH_COMPONENT_SEPARATOR,
|
2024-07-25 16:02:49 +02:00
|
|
|
} from "../../Model/Global/storageModel";
|
2024-07-21 17:55:53 +02:00
|
|
|
|
2026-04-12 19:20:37 +00:00
|
|
|
import CoreViewModel, { Context } from "./coreViewModel";
|
|
|
|
|
import { CommonKeys } from "../../View/keystrokes";
|
|
|
|
|
|
|
|
|
|
export default class StorageViewModel extends Context {
|
|
|
|
|
contextDebugDescription = "storage";
|
2024-07-21 20:21:32 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// state
|
|
|
|
|
isShowingStorageModal: React.State<boolean> = new React.State(false);
|
|
|
|
|
selectedPath: React.State<string> = new React.State(
|
|
|
|
|
PATH_COMPONENT_SEPARATOR,
|
|
|
|
|
);
|
|
|
|
|
didMakeChanges: React.State<boolean> = new React.State(false);
|
2024-07-21 17:55:53 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
selectedFileName: React.State<string>;
|
|
|
|
|
selectedFileContent: React.State<string>;
|
2024-07-21 20:21:32 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
lastDeletedItemPath: React.State<string> = new React.State("");
|
2024-07-21 20:21:32 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// methods
|
|
|
|
|
getSelectedItemContent = (): string => {
|
|
|
|
|
const path = StorageModel.stringToPathComponents(
|
|
|
|
|
this.selectedPath.value,
|
|
|
|
|
);
|
2026-04-09 16:48:22 +00:00
|
|
|
const content = this.coreViewModel.storageModel.read(path);
|
2026-04-01 08:29:39 +00:00
|
|
|
return (
|
2026-04-09 16:48:22 +00:00
|
|
|
(content ?? this.coreViewModel.translations.storage.notAFile) ||
|
|
|
|
|
this.coreViewModel.translations.storage.contentEmpty
|
2026-04-01 08:29:39 +00:00
|
|
|
);
|
|
|
|
|
};
|
2024-07-21 20:21:32 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
deleteSelectedItem = (): void => {
|
|
|
|
|
const path = StorageModel.stringToPathComponents(
|
|
|
|
|
this.selectedPath.value,
|
|
|
|
|
);
|
|
|
|
|
this.lastDeletedItemPath.value = this.selectedPath.value;
|
2024-07-25 16:02:49 +02:00
|
|
|
|
2026-04-09 16:48:22 +00:00
|
|
|
this.coreViewModel.storageModel.removeRecursively(path);
|
2026-04-01 08:29:39 +00:00
|
|
|
this.didMakeChanges.value = true;
|
|
|
|
|
};
|
2024-07-21 20:21:32 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
removeJunk = (): void => {
|
2026-04-09 16:48:22 +00:00
|
|
|
this.coreViewModel.storageModel.removeJunk();
|
2026-04-01 08:29:39 +00:00
|
|
|
this.selectedPath.value = PATH_COMPONENT_SEPARATOR;
|
|
|
|
|
};
|
2024-07-28 16:08:29 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// view
|
|
|
|
|
showStorageModal = (): void => {
|
2026-04-12 19:20:37 +00:00
|
|
|
this.coreViewModel.context = this;
|
2026-04-01 08:29:39 +00:00
|
|
|
this.isShowingStorageModal.value = true;
|
|
|
|
|
};
|
2024-07-21 17:55:53 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
hideStorageModal = (): void => {
|
2026-04-12 19:20:37 +00:00
|
|
|
this.coreViewModel.closeContext(this.contextId);
|
2026-04-01 08:29:39 +00:00
|
|
|
if (this.didMakeChanges.value == true) {
|
|
|
|
|
window.location.reload();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.isShowingStorageModal.value = false;
|
|
|
|
|
};
|
2024-07-21 17:55:53 +02:00
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
// init
|
2026-04-13 10:58:21 +00:00
|
|
|
constructor(public readonly coreViewModel: CoreViewModel) {
|
2026-04-12 19:20:37 +00:00
|
|
|
super();
|
|
|
|
|
|
2026-04-01 08:29:39 +00:00
|
|
|
this.selectedFileName = React.createProxyState(
|
|
|
|
|
[this.selectedPath],
|
|
|
|
|
() => StorageModel.getFileNameFromString(this.selectedPath.value),
|
|
|
|
|
);
|
|
|
|
|
this.selectedFileContent = React.createProxyState(
|
|
|
|
|
[this.selectedPath],
|
|
|
|
|
() => this.getSelectedItemContent(),
|
|
|
|
|
);
|
2026-04-12 19:20:37 +00:00
|
|
|
|
|
|
|
|
// keystrokes
|
|
|
|
|
this.registerKeyStroke(CommonKeys.CloseOrCancel, this.hideStorageModal);
|
2026-04-01 08:29:39 +00:00
|
|
|
}
|
2024-07-21 17:55:53 +02:00
|
|
|
}
|