Skip to content

Commit

Permalink
Merge from jon-dez/main
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-dez committed Sep 18, 2024
2 parents b2e0b64 + 205b28d commit e7bf19d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
28 changes: 17 additions & 11 deletions src/tldraw/asset-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -36,8 +38,8 @@ export class ObsidianTLAssetStore implements TLAssetStore {
async upload(asset: TLAsset, file: File): Promise<string> {
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 {
Expand All @@ -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<null | string> {
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;
}

Expand All @@ -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;
}
}

0 comments on commit e7bf19d

Please sign in to comment.