From 205b28dcfe23fb05de82444f1b4c8d1923c79d3a Mon Sep 17 00:00:00 2001 From: jon-dez <48642222+jon-dez@users.noreply.github.com> Date: Wed, 18 Sep 2024 07:51:35 -0400 Subject: [PATCH] - This is a hot fix that reintroduces backwards compatibility with images previously included in drawings, that 1.7.0 ended up breaking --- manifest.json | 2 +- src/tldraw/asset-store.ts | 28 +++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/manifest.json b/manifest.json index 2d60118..65d3216 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "tldraw", "name": "Tldraw", - "version": "1.7.0", + "version": "1.7.1", "minAppVersion": "0.15.0", "description": "Integrates Tldraw into Obsidian, allowing users to draw and edit content on a virtual whiteboard.", "author": "Sam Alhaqab", diff --git a/src/tldraw/asset-store.ts b/src/tldraw/asset-store.ts index d3f976d..ead9108 100644 --- a/src/tldraw/asset-store.ts +++ b/src/tldraw/asset-store.ts @@ -3,6 +3,8 @@ import TldrawPlugin from "src/main"; import { createAttachmentFilepath } from "src/utils/utils"; import { FileHelpers, TLAsset, TLAssetContext, TLAssetStore } from "tldraw"; +const blockRefAsset = 'obsidian.blockref.'; + /** * Replaces the default tldraw asset store with one that saves assets to the attachment folder. * @@ -36,8 +38,8 @@ export class ObsidianTLAssetStore implements TLAssetStore { async upload(asset: TLAsset, file: File): Promise { if (!this.storeAsset) throw new Error('storeAsset callback was not provided.'); - const id = window.crypto.randomUUID(); - const objectName = `${id}-${file.name}`.replace(/\W/g, '-') + const blockRefId = window.crypto.randomUUID(); + const objectName = `${blockRefId}-${file.name}`.replace(/\W/g, '-') const ext = file.type.split('/').at(1); const { @@ -48,27 +50,31 @@ export class ObsidianTLAssetStore implements TLAssetStore { const tFile = await this.plugin.app.vault.createBinary(`${folder}/${filename}`, await file.arrayBuffer() ); - await this.storeAsset(id, tFile); + await this.storeAsset(blockRefId, tFile); const assetDataUri = await FileHelpers.blobToDataUrl(file); - this.resolvedCache.set(id, assetDataUri); - return `asset:${id}`; + this.resolvedCache.set(blockRefId, assetDataUri); + return `asset:${blockRefAsset}${blockRefId}`; } async resolve(asset: TLAsset, ctx: TLAssetContext): Promise { const assetId = asset.props.src?.split(':').at(1); if (!assetId) return null; - const assetUri = this.resolvedCache.get(assetId); + if(!assetId.startsWith(blockRefAsset)) return asset.props.src; + + const blockRefId = assetId.slice(blockRefAsset.length); + + const assetUri = this.resolvedCache.get(blockRefId); if (assetUri) return assetUri; const blocks = this.plugin.app.metadataCache.getFileCache(this.tldrawFile)?.blocks; if (!blocks) return null; - const assetBlock = blocks[assetId]; + const assetBlock = blocks[blockRefId]; if (!assetBlock) { - new Notice(`Asset block not found: ${assetId}`); + new Notice(`Asset block not found: ${blockRefId}`); return null; } @@ -78,21 +84,21 @@ export class ObsidianTLAssetStore implements TLAssetStore { const link = assetBlockContents.match(insideBrackets)?.at(1); if (!link) { - new Notice(`Asset block does not reference a link: ${assetId}`); + new Notice(`Asset block does not reference a link: ${blockRefId}`); return null; } const assetFile = this.plugin.app.metadataCache.getFirstLinkpathDest(link, this.tldrawFile.path); if (!assetFile) { - new Notice(`Asset block link did not reference a known file: ${assetId} (${link})`); + new Notice(`Asset block link did not reference a known file: ${blockRefId} (${link})`); return null; } const assetData = await this.plugin.app.vault.readBinary(assetFile); const assetFileBlob = new Blob([assetData]); const assetDataUri = await FileHelpers.blobToDataUrl(assetFileBlob); - this.resolvedCache.set(assetId, assetDataUri) + this.resolvedCache.set(blockRefId, assetDataUri) return assetDataUri; } }