Files
warren/frontend/stores/viewers/text.ts
2025-09-06 01:21:13 +02:00

72 lines
1.8 KiB
TypeScript

import { uploadToWarren } from '~/lib/api/warrens';
import type { DirectoryEntry } from '~/shared/types';
export const useTextEditor = defineStore('text-editor', {
state: () => ({
data: null as {
warrenId: string;
parentPath: string;
entry: DirectoryEntry;
content: string;
editedContent: string;
} | null,
saving: false as boolean,
}),
actions: {
async open(
warrenId: string,
parentPath: string,
entry: DirectoryEntry,
content: Blob
) {
const contentString = await content.text();
this.data = {
warrenId,
parentPath,
entry,
content: contentString,
editedContent: contentString,
};
},
async save(): Promise<boolean> {
if (this.saving || this.data == null) {
return false;
}
this.saving = true;
const dt = new DataTransfer();
dt.items.add(
new File([this.data.editedContent], this.data.entry.name)
);
const result = await uploadToWarren(
this.data.warrenId,
this.data.parentPath,
dt.files,
undefined
);
this.$patch({
data: {
content: this.data.editedContent,
},
saving: false,
});
return result.success;
},
discardEdits() {
if (this.data == null) {
return;
}
this.data.editedContent = this.data.content;
},
close() {
this.data = null;
},
},
});