From c81e89c7f451de5c6251a985a000d61d3a8f3eda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Mon, 25 Mar 2024 17:34:15 +0100 Subject: [PATCH] Support images & files in synced blocks --- bun.lockb | Bin 468747 -> 469147 bytes package.json | 2 +- .../DocumentView/BlockSyncedBlock.tsx | 24 ++++++++++++---- src/components/DocumentView/Drawing.tsx | 2 +- src/components/DocumentView/File.tsx | 10 ++----- src/components/DocumentView/Images.tsx | 4 +-- src/components/DocumentView/InlineImage.tsx | 6 ++-- .../DocumentView/Table/RecordCard.tsx | 2 +- src/lib/references.ts | 27 +++++++++++++++--- 9 files changed, 53 insertions(+), 24 deletions(-) diff --git a/bun.lockb b/bun.lockb index 2d949319652fd5e48e534a65f9a6f8e2da097e1d..447149b32e4dee8b665494e91ae5f2bb39bca4b4 100755 GIT binary patch delta 2806 zcmY+`dr(wW90%}o*$b?wsDL7fyj&2)HUX6lUn?jesH-A^FVqo3^AS>xg4*tmn40An ztF7Y;YI>rLIHcF`i5i-YI%;W?O*sNn(>RS*W2Q6N_qY4)kIrzozx%nrbMC$8ckhL5 z*JAeGh}mHsv@54|?bD#MrI9PIhv)rh-=3veT=RB6>dZD`mu=enOKsPbU?uJScT3^| z_Ir-3ePRh~4^XO`Qm&)$C~Sk5G}k0OwwNnpvme$^H0qlx3p(a&$sTt>s^@Hhxp_Nxf2Q1PAvC*!u*hupt*w|G&~;aRsdarxBsYGiq(uz#&b3Ho> zB8-S8Q`UX2z1)C-~)d30QtQkV958|(DZi~o7w^7gIe^xc7J@h- z$VaLhtt)m@Y29IRnW(TD;y0)2!Nl&seoX913xzolOY24Jfw&m4OcX}ziFm2p6k0f~ z7vdGP2$-xz7Ck9|=iX27j%fS(dXV9W(yrA_lxqa@EQrkFWv=rD;+9>~EH3)W$HimhFVFqn1^HO1-(Z(@v2&~wI z`b(3CLYC_V^aV^D&qOQi8S6QL_9E;GEte*5%~nhzwa%lZAzq3uEuWT-xDe%(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(jLQ`tqBn4_JO-LCZ6V@)G(VWEh7WoQVrl-&^F^E| z7tsP}eu%g0l_xYm#|2q6v_M|E2k|g12qw=X#kfujrUk&_O(IQ~|4q`F~uLPc>ZD3*~%%E*#-hHrcnw@#8U`4de%!`6m&{CPV8aB&vI<^p_ zfnO5SX!pYo#k&(=1|rF!-?5U zTo3!6dm)Dw2djih{c~Zm_2bcJQ4*F!lWdL=uVuEFPD-8b8$iarS0J0 zWW@h)a53!x*bJ?N6K;SF&`M#l!W+>q!DQp@ge}5x6Z#eA)p9;Ntd~~j;Dnojsl;b! zDKL54r9DSWMJ#W-4aa&MrD9vq7ttDMX|N&OmF%7Uv~)_b>&=cWI@FC$P9u6#1&i`aO)khYS8b zOu{nsSv-g=UiVxcw&zZqoaED;=HPPJG2Dzi(T6a37j~iF#w_we2qWSORI!^ zLHn5Y2&|UY2a|`}jovW-dFp3k6|egP| zV_|dofitdZc|vqdG`_tV7o^sTvT{%_yJ)o=T^FslEbb+Pram2R9Qo8b(twFd6P*Ko K*SjNJ{`? { + if (!syncedBlock?.files) { + return context.resolveContentRef(ref, options); + } + const result = resolveContentRefWithFiles(syncedBlock.files, ref); + console.log('RESOLVED', result); + if (result !== undefined) { + return result; + } + return context.resolveContentRef(ref, options); + }, + }} style={style} /> ); diff --git a/src/components/DocumentView/Drawing.tsx b/src/components/DocumentView/Drawing.tsx index b4adf477a9..1b0d83d404 100644 --- a/src/components/DocumentView/Drawing.tsx +++ b/src/components/DocumentView/Drawing.tsx @@ -19,7 +19,7 @@ export async function Drawing(props: BlockProps) { sources={{ light: { src: resolved.href, - size: resolved.fileDimensions, + size: resolved.file?.dimensions, }, }} alt="Drawing" diff --git a/src/components/DocumentView/File.tsx b/src/components/DocumentView/File.tsx index f86d063af3..d0f623134d 100644 --- a/src/components/DocumentView/File.tsx +++ b/src/components/DocumentView/File.tsx @@ -12,13 +12,9 @@ import { BlockProps } from './Block'; export async function File(props: BlockProps) { const { block, context, style } = props; - const file = context.content - ? await getRevisionFile( - context.content.spaceId, - context.content.revisionId, - block.data.ref.file, - ) - : null; + const contentRef = await context.resolveContentRef(block.data.ref); + const file = contentRef?.file; + if (!file) { return null; } diff --git a/src/components/DocumentView/Images.tsx b/src/components/DocumentView/Images.tsx index 1bb7d67f32..a9595dd988 100644 --- a/src/components/DocumentView/Images.tsx +++ b/src/components/DocumentView/Images.tsx @@ -89,12 +89,12 @@ async function ImageBlock(props: { sources={{ light: { src: src.href, - size: src.fileDimensions, + size: src.file?.dimensions, }, dark: darkSrc ? { src: darkSrc.href, - size: darkSrc.fileDimensions, + size: darkSrc.file?.dimensions, } : null, }} diff --git a/src/components/DocumentView/InlineImage.tsx b/src/components/DocumentView/InlineImage.tsx index c3c35fbc45..799af9bcb3 100644 --- a/src/components/DocumentView/InlineImage.tsx +++ b/src/components/DocumentView/InlineImage.tsx @@ -32,12 +32,12 @@ export async function InlineImage(props: InlineProps) { sources={{ light: { src: src.href, - size: src.fileDimensions, + size: src.file?.dimensions, }, dark: darkSrc ? { src: darkSrc.href, - size: darkSrc.fileDimensions, + size: darkSrc.file?.dimensions, } : null, }} @@ -55,7 +55,7 @@ async function getImageSizes(size: 'original' | 'line', src: ResolvedContentRef) switch (size) { case 'line': { const imageSize = - src.fileDimensions ?? + src.file?.dimensions ?? (await getImageSize(src.href, { dpr: 3, })); diff --git a/src/components/DocumentView/Table/RecordCard.tsx b/src/components/DocumentView/Table/RecordCard.tsx index c79f772b83..7658891d60 100644 --- a/src/components/DocumentView/Table/RecordCard.tsx +++ b/src/components/DocumentView/Table/RecordCard.tsx @@ -52,7 +52,7 @@ export async function RecordCard( sources={{ light: { src: cover.href, - size: cover.fileDimensions, + size: cover.file?.dimensions, }, }} sizes={[ diff --git a/src/lib/references.ts b/src/lib/references.ts index 8c5a8bdaff..b7f6771f99 100644 --- a/src/lib/references.ts +++ b/src/lib/references.ts @@ -1,4 +1,4 @@ -import { ContentRef, Revision, RevisionPageDocument, Space } from '@gitbook/api'; +import { ContentRef, Revision, RevisionFile, RevisionPageDocument, Space } from '@gitbook/api'; import assertNever from 'assert-never'; import { @@ -24,8 +24,8 @@ export interface ResolvedContentRef { href: string; /** True if the content ref is active */ active: boolean; - /** Image size, if the reference is a image file */ - fileDimensions?: { width: number; height: number }; + /** File, if the reference is a file */ + file?: RevisionFile; } export interface ContentRefContext extends PageHrefContext { @@ -90,7 +90,7 @@ export async function resolveContentRef( href: file.downloadURL, text: file.name, active: false, - fileDimensions: file.dimensions, + file, }; } else { return null; @@ -251,3 +251,22 @@ async function resolveContentRefInSpace(spaceId: string, contentRef: ContentRef) baseUrl, }); } + +export function resolveContentRefWithFiles( + files: RevisionFile[], + contentRef: ContentRef, +): ResolvedContentRef | null | undefined { + if (contentRef.kind === 'file') { + const file = files.find((file) => file.id === contentRef.file); + if (file) { + return { + href: file.downloadURL, + text: file.name, + active: false, + file, + }; + } + return null; + } + return undefined; +}