import { uploadToWarren } from '~/lib/api/warrens'; import type { DirectoryEntry } from '~/shared/types'; export default 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 { 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; }, }, });