Compare commits

...

1 Commits

Author SHA1 Message Date
739a5c5132 named export files
Signed-off-by: Jess Frazelle <github@jessfraz.com>
2024-11-18 11:58:22 -08:00
3 changed files with 24 additions and 6 deletions

View File

@ -482,8 +482,9 @@ export const ModelingMachineProvider = ({
// Set the export intent.
engineCommandManager.exportInfo = {
intent: ExportIntent.Save,
// This never gets used its only for make.
name: '',
// If we have a name for the file, like if we are in the desktop
// app, we can use it for the name of the resulting export.
name: file?.name || '',
}
const format = {

View File

@ -1631,7 +1631,11 @@ export class EngineCommandManager extends EventTarget {
switch (this.exportInfo.intent) {
case ExportIntent.Save: {
exportSave(event.data, this.pendingExport.toastId).then(() => {
exportSave(
event.data,
this.exportInfo.name,
this.pendingExport.toastId
).then(() => {
this.pendingExport?.resolve(null)
}, this.pendingExport?.reject)
break

View File

@ -6,6 +6,7 @@ import JSZip from 'jszip'
import ModelingAppFile from './modelingAppFile'
import toast from 'react-hot-toast'
import { EXPORT_TOAST_MESSAGES } from './constants'
import { string } from 'yargs'
const save_ = async (file: ModelingAppFile, toastId: string) => {
try {
@ -68,21 +69,33 @@ const save_ = async (file: ModelingAppFile, toastId: string) => {
}
// Saves files locally from an export call.
export async function exportSave(data: ArrayBuffer, toastId: string) {
export async function exportSave(
data: ArrayBuffer,
filename: string,
toastId: string
) {
// This converts the ArrayBuffer to a Rust equivalent Vec<u8>.
let uintArray = new Uint8Array(data)
let files: ModelingAppFile[] = deserialize_files(uintArray)
if (filename === '') {
filename = 'output'
}
if (files.length > 1) {
let zip = new JSZip()
for (const file of files) {
zip.file(file.name, new Uint8Array(file.contents), { binary: true })
}
return zip.generateAsync({ type: 'array' }).then((contents) => {
return save_({ name: 'output.zip', contents }, toastId)
return save_({ name: filename + '.zip', contents }, toastId)
})
} else {
return save_(files[0], toastId)
const file = files[0]
return save_(
{ name: file.name.replace('output', filename), contents: file.contents },
toastId
)
}
}