Skip to content

Commit 087afdd

Browse files
authored
ENG-1348 Update render block to the new Render React Components API (#871)
* Update roamjs-components to version 0.87.0 and refactor component rendering in ResultsView and Discourse settings. Replace direct API calls with RenderRoamBlock for improved rendering consistency across components. * Enhance exportUtils by adding BlockViewType to pullBlockToTreeNode function for improved block handling. * format
1 parent 9d33cb5 commit 087afdd

9 files changed

Lines changed: 67 additions & 151 deletions

File tree

apps/roam/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"react-draggable": "4.4.5",
7272
"react-in-viewport": "1.0.0-alpha.20",
7373
"react-vertical-timeline-component": "3.5.2",
74-
"roamjs-components": "0.86.4",
74+
"roamjs-components": "0.87.0",
7575
"tldraw": "2.4.6",
7676
"use-sync-external-store": "1.5.0",
7777
"xregexp": "^5.0.0",

apps/roam/src/components/results-view/Kanban.tsx

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,20 @@ import getSubTree from "roamjs-components/util/getSubTree";
2222
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
2323
import { Sorts } from "~/utils/parseResultSettings";
2424
import getRoamUrl from "roamjs-components/dom/getRoamUrl";
25+
import { RenderRoamBlock } from "~/utils/roamReactComponents";
2526

2627
const zPriority = z.record(z.number().min(0).max(1));
2728

2829
type Reprioritize = (args: { uid: string; x: number; y: number }) => void;
2930

3031
const BlockEmbed = ({ uid, viewValue }: { uid: string; viewValue: string }) => {
3132
const title = getPageTitleByPageUid(uid);
32-
const contentRef = useRef(null);
3333
const open =
34-
viewValue === "open" ? true : viewValue === "closed" ? false : null;
35-
useEffect(() => {
36-
const el = contentRef.current;
37-
if (el) {
38-
window.roamAlphaAPI.ui.components.renderBlock({
39-
uid,
40-
el,
41-
// @ts-expect-error - add to roamjs-components
42-
// eslint-disable-next-line @typescript-eslint/naming-convention
43-
"open?": open,
44-
});
45-
}
46-
}, [uid, open, contentRef]);
34+
viewValue === "open" ? true : viewValue === "closed" ? false : undefined;
4735
return (
48-
<div ref={contentRef} className={title ? "page-embed" : "block-embed"} />
36+
<div className={title ? "page-embed" : "block-embed"}>
37+
<RenderRoamBlock uid={uid} open={open} />
38+
</div>
4939
);
5040
};
5141

apps/roam/src/components/results-view/ResultsTable.tsx

Lines changed: 20 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -25,50 +25,21 @@ import { CONTEXT_OVERLAY_SUGGESTION } from "~/utils/predefinedSelections";
2525
import { USE_REIFIED_RELATIONS } from "~/data/userSettings";
2626
import { getSetting } from "~/utils/extensionSettings";
2727
import { strictQueryForReifiedBlocks } from "~/utils/createReifiedBlock";
28-
import internalError from "~/utils/internalError";
28+
import {
29+
RenderRoamBlock,
30+
RenderRoamPage,
31+
RenderRoamBlockString,
32+
} from "~/utils/roamReactComponents";
2933

3034
const EXTRA_ROW_TYPES = ["context", "discourse"] as const;
3135
type ExtraRowType = (typeof EXTRA_ROW_TYPES)[number] | null;
3236

3337
const ExtraContextRow = ({ uid }: { uid: string }) => {
34-
const containerRef = useRef<HTMLDivElement>(null);
35-
36-
useEffect(() => {
37-
if (!containerRef.current) return;
38-
if (getPageTitleByPageUid(uid)) {
39-
window.roamAlphaAPI.ui.components
40-
.renderPage({
41-
uid,
42-
el: containerRef.current,
43-
// eslint-disable-next-line @typescript-eslint/naming-convention
44-
"hide-mentions?": true,
45-
})
46-
.catch((error) => {
47-
internalError({
48-
error,
49-
type: "Results Table: Extra Context Row",
50-
context: { uid },
51-
});
52-
});
53-
} else {
54-
window.roamAlphaAPI.ui.components
55-
.renderBlock({
56-
uid,
57-
el: containerRef.current,
58-
// eslint-disable-next-line @typescript-eslint/naming-convention
59-
"zoom-path?": true,
60-
})
61-
.catch((error) => {
62-
internalError({
63-
error,
64-
type: "Results Table: Extra Context Row",
65-
context: { uid },
66-
});
67-
});
68-
}
69-
}, [containerRef, uid]);
70-
71-
return <div ref={containerRef} />;
38+
return getPageTitleByPageUid(uid) ? (
39+
<RenderRoamPage uid={uid} hideMentions />
40+
) : (
41+
<RenderRoamBlock uid={uid} zoomPath />
42+
);
7243
};
7344

7445
const dragImage = document.createElement("img");
@@ -181,30 +152,12 @@ export const CellEmbed = ({
181152
viewValue?: string;
182153
}) => {
183154
const title = getPageTitleByPageUid(uid);
184-
const contentRef = useRef(null);
185-
useEffect(() => {
186-
const el = contentRef.current;
187-
const open =
188-
viewValue === "open" ? true : viewValue === "closed" ? false : undefined;
189-
if (el) {
190-
window.roamAlphaAPI.ui.components
191-
.renderBlock({
192-
uid,
193-
el,
194-
// eslint-disable-next-line @typescript-eslint/naming-convention
195-
"open?": open,
196-
})
197-
.catch((error) => {
198-
internalError({
199-
error,
200-
type: "Results Table: Cell Embed",
201-
context: { uid },
202-
});
203-
});
204-
}
205-
}, [contentRef, uid, viewValue]);
155+
const open =
156+
viewValue === "open" ? true : viewValue === "closed" ? false : undefined;
206157
return (
207-
<div ref={contentRef} className={title ? "page-embed" : "block-embed"} />
158+
<div className={title ? "page-embed" : "block-embed"}>
159+
<RenderRoamBlock uid={uid} open={open} />
160+
</div>
208161
);
209162
};
210163

@@ -215,29 +168,14 @@ export const CellRender = ({
215168
content: string;
216169
uid: string;
217170
}) => {
218-
const contentRef = useRef<HTMLSpanElement>(null);
219171
const isPage = !!getPageTitleByPageUid(uid);
220172
const displayString = isPage ? `[[${content}]]` : content;
221173

222-
useEffect(() => {
223-
const el = contentRef.current;
224-
if (el && displayString) {
225-
window.roamAlphaAPI.ui.components
226-
.renderString({
227-
el,
228-
string: displayString,
229-
})
230-
.catch((error) => {
231-
internalError({
232-
error,
233-
type: "Results Table: Cell Render",
234-
context: { displayString },
235-
});
236-
});
237-
}
238-
}, [displayString]);
239-
240-
return <span ref={contentRef} className="roamjs-query-link-cell" />;
174+
return (
175+
<span className="roamjs-query-link-cell">
176+
<RenderRoamBlockString string={displayString} />
177+
</span>
178+
);
241179
};
242180

243181
type ResultRowProps = {

apps/roam/src/components/results-view/Timeline.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useRef, useEffect } from "react";
1+
import React, { useMemo } from "react";
22
import {
33
VerticalTimeline,
44
VerticalTimelineElement,
@@ -7,6 +7,7 @@ import "react-vertical-timeline-component/style.min.css";
77
import { Icon } from "@blueprintjs/core";
88
import PageLink from "roamjs-components/components/PageLink";
99
import { Result } from "roamjs-components/types/query-builder";
10+
import { RenderRoamBlock } from "~/utils/roamReactComponents";
1011

1112
type TimelineProps = { timelineElements: Result[] };
1213

@@ -45,14 +46,6 @@ const TimelineElement = ({
4546
color: string;
4647
t: Result & { date: Date };
4748
}) => {
48-
const containerRef = useRef(null);
49-
useEffect(() => {
50-
if (!containerRef.current) return;
51-
window.roamAlphaAPI.ui.components.renderBlock({
52-
uid: t.uid,
53-
el: containerRef.current,
54-
});
55-
}, [t.uid, containerRef]);
5649
return (
5750
<VerticalTimelineElement
5851
contentStyle={{
@@ -79,7 +72,9 @@ const TimelineElement = ({
7972
<h4 className="vertical-timeline-element-title">
8073
<PageLink uid={t.uid}>{t.text}</PageLink>
8174
</h4>
82-
<p className="vertical-timeline-element-body" ref={containerRef} />
75+
<div className="vertical-timeline-element-body">
76+
<RenderRoamBlock uid={t.uid} />
77+
</div>
8378
</VerticalTimelineElement>
8479
);
8580
};

apps/roam/src/components/settings/DiscourseNodeSuggestiveRules.tsx

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useMemo, useEffect, useRef } from "react";
1+
import React, { useState, useMemo } from "react";
22
import { Button, Intent } from "@blueprintjs/core";
33
import DualWriteBlocksPanel from "./components/EphemeralBlocksPanel";
44
import getSubTree from "roamjs-components/util/getSubTree";
@@ -11,27 +11,10 @@ import {
1111
DiscourseNodeFlagPanel,
1212
DiscourseNodeTextPanel,
1313
} from "./components/BlockPropSettingPanels";
14+
import { RenderRoamBlock } from "~/utils/roamReactComponents";
1415

1516
const TEMPLATE_SETTING_KEYS = ["template"];
1617

17-
const BlockRenderer = ({ uid }: { uid: string }) => {
18-
const containerRef = useRef<HTMLDivElement>(null);
19-
20-
useEffect(() => {
21-
const container = containerRef.current;
22-
if (container) {
23-
container.innerHTML = "";
24-
25-
window.roamAlphaAPI.ui.components.renderBlock({
26-
uid: uid,
27-
el: container,
28-
});
29-
}
30-
}, [uid]);
31-
32-
return <div ref={containerRef} className="my-2 rounded border p-2" />;
33-
};
34-
3518
const DiscourseNodeSuggestiveRules = ({
3619
node,
3720
parentUid,
@@ -107,7 +90,9 @@ const DiscourseNodeSuggestiveRules = ({
10790
{blockUidToRender && (
10891
<div>
10992
<div className="mb-1 text-sm text-gray-600">Preview:</div>
110-
<BlockRenderer uid={blockUidToRender} />
93+
<div className="my-2 rounded border p-2">
94+
<RenderRoamBlock uid={blockUidToRender} />
95+
</div>
11196
</div>
11297
)}
11398

apps/roam/src/components/settings/DiscourseRelationConfigPanel.tsx

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
setGlobalSetting,
5656
getGlobalSettings,
5757
} from "~/components/settings/utils/accessors";
58+
import { RenderRoamBlock } from "~/utils/roamReactComponents";
5859

5960
const DEFAULT_SELECTED_RELATION = {
6061
display: "none",
@@ -64,21 +65,6 @@ const DEFAULT_SELECTED_RELATION = {
6465
id: "",
6566
};
6667

67-
const RelationEditPreview = ({ previewUid }: { previewUid: string }) => {
68-
const containerRef = useRef<HTMLDivElement>(null);
69-
useEffect(() => {
70-
const el = containerRef.current;
71-
if (el)
72-
window.roamAlphaAPI.ui.components.renderBlock({
73-
el,
74-
uid: previewUid,
75-
});
76-
}, [previewUid, containerRef]);
77-
return (
78-
<div ref={containerRef} className={"roamjs-discourse-editor-preview"}></div>
79-
);
80-
};
81-
8268
const edgeDisplayByUid = (uid: string) =>
8369
uid === "*"
8470
? "Any"
@@ -857,7 +843,11 @@ export const RelationEditPanel = ({
857843
}
858844
}}
859845
/>
860-
{isPreview && <RelationEditPreview previewUid={previewUid} />}
846+
{isPreview && (
847+
<div className={"roamjs-discourse-editor-preview"}>
848+
<RenderRoamBlock uid={previewUid} />
849+
</div>
850+
)}
861851
<Menu
862852
style={{
863853
position: "absolute",

apps/roam/src/utils/exportUtils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { Result } from "roamjs-components/types/query-builder";
2-
import { PullBlock, TreeNode, ViewType } from "roamjs-components/types";
2+
import {
3+
BlockViewType,
4+
PullBlock,
5+
TreeNode,
6+
ViewType,
7+
} from "roamjs-components/types";
38
import type { DiscourseNode } from "./getDiscourseNodes";
49
import matchDiscourseNode from "./matchDiscourseNode";
510

@@ -126,6 +131,8 @@ export const pullBlockToTreeNode = (
126131
uid: n[":block/uid"] || "",
127132
heading: n[":block/heading"] || 0,
128133
viewType: (n[":children/view-type"] || v).slice(1) as ViewType,
134+
blockViewType: (n[":block/view-type"]?.slice(1) ??
135+
"outline") as BlockViewType,
129136
editTime: new Date(n[":edit/time"] || 0),
130137
props: { imageResize: {}, iframe: {} },
131138
textAlign: n[":block/text-align"] || "left",
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Single access point for Roam's render React components (Block, Page, BlockString).
3+
* Use these instead of reading window.roamAlphaAPI.ui.react in each component.
4+
*/
5+
const {
6+
Block: RenderRoamBlock,
7+
Page: RenderRoamPage,
8+
BlockString: RenderRoamBlockString,
9+
} = window.roamAlphaAPI.ui.react;
10+
11+
export { RenderRoamBlock, RenderRoamPage, RenderRoamBlockString };

pnpm-lock.yaml

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)