Skip to content

Commit 902ee22

Browse files
authored
ENG-1496 Canvas editor/store instance stale (#872)
* working fix for Canvas editor/store instance stale * comment * ENG-13-- Update pnpm-lock and pnpm-workspace to include patch for @tldraw/state@2.4.6 (#873)
1 parent cf4b58c commit 902ee22

6 files changed

Lines changed: 331 additions & 124 deletions

File tree

apps/roam/src/components/canvas/Tldraw.tsx

Lines changed: 17 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ import {
5252
StateNode,
5353
DefaultSpinner,
5454
Box,
55-
MigrationSequence,
56-
TLAnyBindingUtilConstructor,
57-
TLAnyShapeUtilConstructor,
5855
} from "tldraw";
5956
import "tldraw/tldraw.css";
6057
import tldrawStyles from "./tldrawStyles";
@@ -71,7 +68,6 @@ import {
7168
BaseDiscourseNodeUtil,
7269
DiscourseNodeShape,
7370
createNodeShapeTools,
74-
createNodeShapeUtils,
7571
} from "./DiscourseNodeUtil";
7672
import { useRoamStore } from "./useRoamStore";
7773
import {
@@ -87,21 +83,12 @@ import isLiveBlock from "roamjs-components/queries/isLiveBlock";
8783
import openBlockInSidebar from "roamjs-components/writes/openBlockInSidebar";
8884
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
8985
import renderToast from "roamjs-components/components/Toast";
90-
import {
91-
createAllReferencedNodeUtils,
92-
createAllRelationShapeUtils,
93-
} from "./DiscourseRelationShape/DiscourseRelationUtil";
9486
import {
9587
AddReferencedNodeType,
9688
createAllReferencedNodeTools,
9789
createAllRelationShapeTools,
9890
} from "./DiscourseRelationShape/DiscourseRelationTool";
99-
import {
100-
createAllReferencedNodeBindings,
101-
createAllRelationBindings,
102-
} from "./DiscourseRelationShape/DiscourseRelationBindings";
10391
import ConvertToDialog from "./ConvertToDialog";
104-
import { createMigrations } from "./DiscourseRelationShape/discourseRelationMigrations";
10592
import ToastListener, { dispatchToastEvent } from "./ToastListener";
10693
import { CanvasDrawerPanel } from "./CanvasDrawer";
10794
import { ClipboardPanel, ClipboardProvider } from "./Clipboard";
@@ -121,6 +108,10 @@ import {
121108
getEffectiveCanvasSyncMode,
122109
setCanvasSyncMode,
123110
} from "./canvasSyncMode";
111+
import {
112+
CanvasStoreAdapterArgs,
113+
useCanvasStoreAdapterArgs,
114+
} from "./useCanvasStoreAdapterArgs";
124115
import posthog from "posthog-js";
125116

126117
declare global {
@@ -269,15 +260,6 @@ const TldrawCanvas = ({ title }: { title: string }) => {
269260
);
270261
};
271262

272-
type CanvasStoreAdapterArgs = {
273-
pageUid: string;
274-
migrations: MigrationSequence[];
275-
customShapeUtils: readonly TLAnyShapeUtilConstructor[];
276-
customBindingUtils: readonly TLAnyBindingUtilConstructor[];
277-
customShapeTypes: string[];
278-
customBindingTypes: string[];
279-
};
280-
281263
type CanvasStoreAdapterResult = {
282264
store: TLStore | TLStoreWithStatus | null;
283265
needsUpgrade: boolean;
@@ -715,17 +697,19 @@ const TldrawCanvasShared = ({
715697
onCanvasSyncModeChange,
716698
});
717699

718-
// UTILS
719-
const discourseNodeUtils = createNodeShapeUtils(allNodes);
720-
const discourseRelationUtils = createAllRelationShapeUtils(allRelationIds);
721-
const referencedNodeUtils = createAllReferencedNodeUtils(
700+
const storeAdapterArgs = useCanvasStoreAdapterArgs({
701+
pageUid,
702+
isCloudflareSync,
703+
allNodes,
704+
allRelationIds,
722705
allAddReferencedNodeByAction,
723-
);
724-
const customShapeUtils = [
725-
...discourseNodeUtils,
726-
...discourseRelationUtils,
727-
...referencedNodeUtils,
728-
];
706+
});
707+
const {
708+
customShapeUtils,
709+
customBindingUtils,
710+
customShapeTypes,
711+
customBindingTypes,
712+
} = storeAdapterArgs;
729713

730714
// TOOLS
731715
const discourseGraphTool = class DiscourseGraphTool extends StateNode {
@@ -744,19 +728,6 @@ const TldrawCanvasShared = ({
744728
...referencedNodeTools,
745729
];
746730

747-
// BINDINGS
748-
const relationBindings = createAllRelationBindings(allRelationIds);
749-
const referencedNodeBindings = createAllReferencedNodeBindings(
750-
allAddReferencedNodeByAction,
751-
);
752-
const customBindingUtils = [...relationBindings, ...referencedNodeBindings];
753-
const customShapeTypes = customShapeUtils
754-
.map((s) => (s as unknown as { type?: string }).type)
755-
.filter((t): t is string => !!t);
756-
const customBindingTypes = customBindingUtils
757-
.map((b) => (b as unknown as { type?: string }).type)
758-
.filter((t): t is string => !!t);
759-
760731
// UI OVERRIDES
761732
const uiOverrides = createUiOverrides({
762733
allNodes,
@@ -774,26 +745,8 @@ const TldrawCanvasShared = ({
774745
inSidebar: !!containerRef.current?.closest(".rm-sidebar-outline"),
775746
});
776747
}, [pageUid]);
777-
const arrowShapeMigrations = useMemo(
778-
() =>
779-
createMigrations({
780-
allRelationIds,
781-
allAddReferencedNodeActions,
782-
allNodeTypes: allNodes.map((node) => node.type),
783-
}),
784-
[allRelationIds, allAddReferencedNodeActions, allNodes],
785-
);
786-
787-
const migrations = [arrowShapeMigrations];
788748
const { store, needsUpgrade, performUpgrade, error, isLoading } =
789-
useStoreAdapter({
790-
migrations,
791-
customShapeUtils,
792-
customBindingUtils,
793-
customShapeTypes,
794-
customBindingTypes,
795-
pageUid,
796-
});
749+
useStoreAdapter(storeAdapterArgs);
797750

798751
// ASSETS
799752
const assetLoading = usePreloadAssets(defaultEditorAssetUrls);

apps/roam/src/components/canvas/uiOverrides.tsx

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/naming-convention */
2-
import React from "react";
2+
import React, { ReactElement } from "react";
33
import {
44
TLImageShape,
55
TLShape,
@@ -16,10 +16,10 @@ import {
1616
TldrawUiMenuItem,
1717
DefaultMainMenu,
1818
TldrawUiMenuGroup,
19-
// TldrawUiDropdownMenuItem,
20-
// TldrawUiButton,
21-
// TldrawUiButtonLabel,
22-
// TldrawUiIcon,
19+
TldrawUiDropdownMenuItem,
20+
TldrawUiButton,
21+
TldrawUiButtonLabel,
22+
TldrawUiIcon,
2323
useActions,
2424
DefaultContextMenu,
2525
DefaultContextMenuContent,
@@ -52,39 +52,39 @@ import { CustomDefaultToolbar } from "./CustomDefaultToolbar";
5252
import { renderModifyNodeDialog } from "~/components/ModifyNodeDialog";
5353
import { CanvasSyncMode } from "./canvasSyncMode";
5454

55-
// const SyncModeMenuSwitchItem = ({
56-
// checked,
57-
// disabled,
58-
// label,
59-
// onToggle,
60-
// }: {
61-
// checked: boolean;
62-
// disabled?: boolean;
63-
// label: string;
64-
// onToggle: () => void;
65-
// }): React.ReactElement => {
66-
// return (
67-
// <TldrawUiDropdownMenuItem>
68-
// <TldrawUiButton
69-
// type="menu"
70-
// title={label}
71-
// disabled={disabled}
72-
// onClick={onToggle}
73-
// >
74-
// <TldrawUiButtonLabel>{label}</TldrawUiButtonLabel>
75-
// <span
76-
// style={{
77-
// marginLeft: "auto",
78-
// display: "inline-flex",
79-
// alignItems: "center",
80-
// }}
81-
// >
82-
// <TldrawUiIcon icon={checked ? "toggle-on" : "toggle-off"} small />
83-
// </span>
84-
// </TldrawUiButton>
85-
// </TldrawUiDropdownMenuItem>
86-
// );
87-
// };
55+
const SyncModeMenuSwitchItem = ({
56+
checked,
57+
disabled,
58+
label,
59+
onToggle,
60+
}: {
61+
checked: boolean;
62+
disabled?: boolean;
63+
label: string;
64+
onToggle: () => void;
65+
}): ReactElement => {
66+
return (
67+
<TldrawUiDropdownMenuItem>
68+
<TldrawUiButton
69+
type="menu"
70+
title={label}
71+
disabled={disabled}
72+
onClick={onToggle}
73+
>
74+
<TldrawUiButtonLabel>{label}</TldrawUiButtonLabel>
75+
<span
76+
style={{
77+
marginLeft: "auto",
78+
display: "inline-flex",
79+
alignItems: "center",
80+
}}
81+
>
82+
<TldrawUiIcon icon={checked ? "toggle-on" : "toggle-off"} small />
83+
</span>
84+
</TldrawUiButton>
85+
</TldrawUiDropdownMenuItem>
86+
);
87+
};
8888

8989
export const getOnSelectForShape = ({
9090
shape,
@@ -224,8 +224,8 @@ export const createUiComponents = ({
224224
allNodes,
225225
allAddReferencedNodeActions,
226226
allRelationNames,
227-
// canvasSyncMode,
228-
// onCanvasSyncModeChange,
227+
canvasSyncMode,
228+
onCanvasSyncModeChange,
229229
}: {
230230
allNodes: DiscourseNode[];
231231
allRelationNames: string[];
@@ -277,21 +277,21 @@ export const createUiComponents = ({
277277
</TldrawUiMenuSubmenu>
278278
);
279279
};
280-
// const onToggleSyncMode = (): void => {
281-
// const nextMode: CanvasSyncMode =
282-
// canvasSyncMode === "sync" ? "local" : "sync";
283-
// onCanvasSyncModeChange(nextMode);
284-
// };
280+
const onToggleSyncMode = (): void => {
281+
const nextMode: CanvasSyncMode =
282+
canvasSyncMode === "sync" ? "local" : "sync";
283+
onCanvasSyncModeChange(nextMode);
284+
};
285285

286286
return (
287287
<DefaultMainMenu>
288-
{/* <TldrawUiMenuGroup id="sync-mode"> */}
289-
{/* <SyncModeMenuSwitchItem
288+
<TldrawUiMenuGroup id="sync-mode">
289+
<SyncModeMenuSwitchItem
290290
label="(Beta) Use cloud canvas"
291291
checked={canvasSyncMode === "sync"}
292292
onToggle={onToggleSyncMode}
293-
/> */}
294-
{/* </TldrawUiMenuGroup> */}
293+
/>
294+
</TldrawUiMenuGroup>
295295
<EditSubmenu />
296296
<CustomViewMenu /> {/* Replaced <ViewSubmenu /> */}
297297
<ExportFileContentSubMenu />

0 commit comments

Comments
 (0)