-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
170 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import JSZip from "jszip"; | ||
|
||
import { ProjectModel } from "./data"; | ||
import { DB, DbProject } from "./db"; | ||
|
||
class ExportError extends Error { | ||
constructor(message: string) { | ||
super("export error: " + message); | ||
} | ||
} | ||
|
||
export const exportProject = async (db: DB, project: string, options: { includeAssets: boolean } = { includeAssets: true }) => { | ||
const projectContent: ProjectModel & { id: unknown, source: unknown } | undefined = await db.loadProject(project); | ||
if (projectContent === undefined) { | ||
throw new ExportError("failed to load project for export"); | ||
} | ||
|
||
// Manually get rid of the database-specific properties. | ||
delete projectContent.id; | ||
delete projectContent.source; | ||
|
||
const assetBlobs: { [assetHash: string]: Blob } = {}; | ||
if (options.includeAssets) { | ||
for (const assetInfo of Object.values(projectContent.assets)) { | ||
const blob = await db.loadAsset(assetInfo.hash); | ||
if (blob === undefined) { | ||
console.warn("export warning: ignoring missing blob for asset with hash '" + assetInfo.hash + "'"); | ||
} else { | ||
assetBlobs[assetInfo.hash] = blob; | ||
} | ||
} | ||
} | ||
|
||
const projectContentString = JSON.stringify(projectContent); | ||
|
||
// build the zip file | ||
const zip = new JSZip(); | ||
// TODO: add an index.html file linking to the tour builder | ||
zip.file("tourforge.json", projectContentString); | ||
for (const [assetHash, assetBlob] of Object.entries(assetBlobs)) { | ||
// TODO: maybe give the asset an extension here based on the blob's media type | ||
zip.file(assetHash, assetBlob); | ||
} | ||
const zipBlob = await zip.generateAsync({ type: "blob" }); | ||
|
||
// download the zip file | ||
const zipUrl = URL.createObjectURL(zipBlob); | ||
const a = document.createElement("a"); | ||
a.href = zipUrl; | ||
a.download = projectContent.title + ".tourforge.zip"; | ||
document.body.appendChild(a); | ||
a.click(); | ||
document.body.removeChild(a); | ||
URL.revokeObjectURL(zipUrl); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,6 @@ | |
|
||
.BottomButtons { | ||
display: grid; | ||
grid-template-columns: 1fr; | ||
grid-template-columns: 1fr 1fr; | ||
gap: 8px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters