Filter out unused assets. (#1502)
Prunes unused assets when exporting to a file. ### Change Type - [x] `patch` — Bug Fix ### Test Plan 1. Insert an image. 2. Delete the image. 3. Save to file. 4. The saved file should not have the asset record present. 5. The file size should also be quite small (around 2kb for a file with no shapes). ### Release Notes - Optimize file size of exported files.
This commit is contained in:
parent
a5e653b225
commit
0c9e8d323e
1 changed files with 15 additions and 5 deletions
|
@ -3,6 +3,7 @@ import {
|
||||||
Editor,
|
Editor,
|
||||||
fileToBase64,
|
fileToBase64,
|
||||||
TLAsset,
|
TLAsset,
|
||||||
|
TLAssetId,
|
||||||
TLInstanceId,
|
TLInstanceId,
|
||||||
TLRecord,
|
TLRecord,
|
||||||
TLStore,
|
TLStore,
|
||||||
|
@ -153,7 +154,9 @@ export function parseTldrawJsonFile({
|
||||||
|
|
||||||
/** @public */
|
/** @public */
|
||||||
export async function serializeTldrawJson(store: TLStore): Promise<string> {
|
export async function serializeTldrawJson(store: TLStore): Promise<string> {
|
||||||
const recordsToSave: TLRecord[] = []
|
const records: TLRecord[] = []
|
||||||
|
const usedAssets = new Set<TLAssetId | null>()
|
||||||
|
const assets: TLAsset[] = []
|
||||||
for (const record of store.allRecords()) {
|
for (const record of store.allRecords()) {
|
||||||
switch (record.typeName) {
|
switch (record.typeName) {
|
||||||
case 'asset':
|
case 'asset':
|
||||||
|
@ -171,22 +174,29 @@ export async function serializeTldrawJson(store: TLStore): Promise<string> {
|
||||||
assetSrcToSave = record.props.src
|
assetSrcToSave = record.props.src
|
||||||
}
|
}
|
||||||
|
|
||||||
recordsToSave.push({
|
assets.push({
|
||||||
...record,
|
...record,
|
||||||
props: {
|
props: {
|
||||||
...record.props,
|
...record.props,
|
||||||
src: assetSrcToSave,
|
src: assetSrcToSave,
|
||||||
},
|
},
|
||||||
} as TLAsset)
|
})
|
||||||
} else {
|
} else {
|
||||||
recordsToSave.push(record)
|
assets.push(record)
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
case 'shape':
|
||||||
|
if ('assetId' in record.props) {
|
||||||
|
usedAssets.add(record.props.assetId)
|
||||||
|
}
|
||||||
|
records.push(record)
|
||||||
|
break
|
||||||
default:
|
default:
|
||||||
recordsToSave.push(record)
|
records.push(record)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const recordsToSave = records.concat(assets.filter((a) => usedAssets.has(a.id)))
|
||||||
|
|
||||||
return JSON.stringify({
|
return JSON.stringify({
|
||||||
tldrawFileFormatVersion: LATEST_TLDRAW_FILE_FORMAT_VERSION,
|
tldrawFileFormatVersion: LATEST_TLDRAW_FILE_FORMAT_VERSION,
|
||||||
|
|
Loading…
Reference in a new issue