Skip to content

Commit c81e89c

Browse files
committed
Support images & files in synced blocks
1 parent 30bae7c commit c81e89c

File tree

9 files changed

+53
-24
lines changed

9 files changed

+53
-24
lines changed

bun.lockb

400 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
],
2121
"dependencies": {
2222
"@geist-ui/icons": "^1.0.2",
23-
"@gitbook/api": "^0.36.0",
23+
"@gitbook/api": "^0.38.0",
2424
"@radix-ui/react-checkbox": "^1.0.4",
2525
"@radix-ui/react-popover": "^1.0.7",
2626
"@sentry/nextjs": "^7.94.1",

src/components/DocumentView/BlockSyncedBlock.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { DocumentBlockSyncedBlock } from '@gitbook/api';
22

33
import { getSyncedBlock } from '@/lib/api';
4+
import { resolveContentRefWithFiles } from '@/lib/references';
45

56
import { BlockProps } from './Block';
67
import { Blocks } from './Blocks';
@@ -13,22 +14,35 @@ export async function BlockSyncedBlock(props: BlockProps<DocumentBlockSyncedBloc
1314
return null;
1415
}
1516

16-
const result = await getSyncedBlock(
17+
const syncedBlock = await getSyncedBlock(
1718
apiToken,
1819
block.data.ref.organization,
1920
block.data.ref.syncedBlock,
2021
);
2122

22-
if (!result) {
23+
if (!syncedBlock) {
2324
return null;
2425
}
2526

2627
return (
2728
<Blocks
28-
nodes={result.document.nodes}
29-
document={result.document}
29+
nodes={syncedBlock.document.nodes}
30+
document={syncedBlock.document}
3031
ancestorBlocks={[...ancestorBlocks, block]}
31-
context={context}
32+
context={{
33+
...context,
34+
resolveContentRef: async (ref, options) => {
35+
if (!syncedBlock?.files) {
36+
return context.resolveContentRef(ref, options);
37+
}
38+
const result = resolveContentRefWithFiles(syncedBlock.files, ref);
39+
console.log('RESOLVED', result);
40+
if (result !== undefined) {
41+
return result;
42+
}
43+
return context.resolveContentRef(ref, options);
44+
},
45+
}}
3246
style={style}
3347
/>
3448
);

src/components/DocumentView/Drawing.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function Drawing(props: BlockProps<DocumentBlockDrawing>) {
1919
sources={{
2020
light: {
2121
src: resolved.href,
22-
size: resolved.fileDimensions,
22+
size: resolved.file?.dimensions,
2323
},
2424
}}
2525
alt="Drawing"

src/components/DocumentView/File.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@ import { BlockProps } from './Block';
1212
export async function File(props: BlockProps<DocumentBlockFile>) {
1313
const { block, context, style } = props;
1414

15-
const file = context.content
16-
? await getRevisionFile(
17-
context.content.spaceId,
18-
context.content.revisionId,
19-
block.data.ref.file,
20-
)
21-
: null;
15+
const contentRef = await context.resolveContentRef(block.data.ref);
16+
const file = contentRef?.file;
17+
2218
if (!file) {
2319
return null;
2420
}

src/components/DocumentView/Images.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ async function ImageBlock(props: {
8989
sources={{
9090
light: {
9191
src: src.href,
92-
size: src.fileDimensions,
92+
size: src.file?.dimensions,
9393
},
9494
dark: darkSrc
9595
? {
9696
src: darkSrc.href,
97-
size: darkSrc.fileDimensions,
97+
size: darkSrc.file?.dimensions,
9898
}
9999
: null,
100100
}}

src/components/DocumentView/InlineImage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ export async function InlineImage(props: InlineProps<DocumentInlineImage>) {
3232
sources={{
3333
light: {
3434
src: src.href,
35-
size: src.fileDimensions,
35+
size: src.file?.dimensions,
3636
},
3737
dark: darkSrc
3838
? {
3939
src: darkSrc.href,
40-
size: darkSrc.fileDimensions,
40+
size: darkSrc.file?.dimensions,
4141
}
4242
: null,
4343
}}
@@ -55,7 +55,7 @@ async function getImageSizes(size: 'original' | 'line', src: ResolvedContentRef)
5555
switch (size) {
5656
case 'line': {
5757
const imageSize =
58-
src.fileDimensions ??
58+
src.file?.dimensions ??
5959
(await getImageSize(src.href, {
6060
dpr: 3,
6161
}));

src/components/DocumentView/Table/RecordCard.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function RecordCard(
5252
sources={{
5353
light: {
5454
src: cover.href,
55-
size: cover.fileDimensions,
55+
size: cover.file?.dimensions,
5656
},
5757
}}
5858
sizes={[

src/lib/references.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ContentRef, Revision, RevisionPageDocument, Space } from '@gitbook/api';
1+
import { ContentRef, Revision, RevisionFile, RevisionPageDocument, Space } from '@gitbook/api';
22
import assertNever from 'assert-never';
33

44
import {
@@ -24,8 +24,8 @@ export interface ResolvedContentRef {
2424
href: string;
2525
/** True if the content ref is active */
2626
active: boolean;
27-
/** Image size, if the reference is a image file */
28-
fileDimensions?: { width: number; height: number };
27+
/** File, if the reference is a file */
28+
file?: RevisionFile;
2929
}
3030

3131
export interface ContentRefContext extends PageHrefContext {
@@ -90,7 +90,7 @@ export async function resolveContentRef(
9090
href: file.downloadURL,
9191
text: file.name,
9292
active: false,
93-
fileDimensions: file.dimensions,
93+
file,
9494
};
9595
} else {
9696
return null;
@@ -251,3 +251,22 @@ async function resolveContentRefInSpace(spaceId: string, contentRef: ContentRef)
251251
baseUrl,
252252
});
253253
}
254+
255+
export function resolveContentRefWithFiles(
256+
files: RevisionFile[],
257+
contentRef: ContentRef,
258+
): ResolvedContentRef | null | undefined {
259+
if (contentRef.kind === 'file') {
260+
const file = files.find((file) => file.id === contentRef.file);
261+
if (file) {
262+
return {
263+
href: file.downloadURL,
264+
text: file.name,
265+
active: false,
266+
file,
267+
};
268+
}
269+
return null;
270+
}
271+
return undefined;
272+
}

0 commit comments

Comments
 (0)