Skip to content

Commit 32df04f

Browse files
authored
Enhance Tldraw component to track last actions in history (#383)
- Added lastActions state to DiscourseContextType for tracking recent changes. - Implemented a mechanism to push new actions to lastActions on app change events, maintaining a maximum of 10 entries. - Updated error handling to include lastActions context for better debugging.
1 parent aaeb69a commit 32df04f

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ import { TLDRAW_DATA_ATTRIBUTE } from "./tldrawStyles";
8888
import { AUTO_CANVAS_RELATIONS_KEY } from "~/data/userSettings";
8989
import { getSetting } from "~/utils/extensionSettings";
9090
import { isPluginTimerReady, waitForPluginTimer } from "~/utils/pluginTimer";
91+
import { HistoryEntry } from "@tldraw/store";
92+
import { TLRecord } from "@tldraw/tlschema";
9193

9294
declare global {
9395
interface Window {
@@ -100,12 +102,14 @@ export type DiscourseContextType = {
100102
// { [Relation.Label] => DiscourseRelation[] }
101103
relations: Record<string, DiscourseRelation[]>;
102104
lastAppEvent: string;
105+
lastActions: HistoryEntry<TLRecord>[];
103106
};
104107

105108
export const discourseContext: DiscourseContextType = {
106109
nodes: {},
107110
relations: {},
108111
lastAppEvent: "",
112+
lastActions: [],
109113
};
110114

111115
const DEFAULT_WIDTH = 160;
@@ -121,6 +125,9 @@ const TldrawCanvas = ({ title }: { title: string }) => {
121125
const appRef = useRef<TldrawApp | null>(null);
122126
const lastInsertRef = useRef<VecModel>();
123127
const containerRef = useRef<HTMLDivElement>(null);
128+
const lastActionsRef = useRef<HistoryEntry<TLRecord>[]>(
129+
discourseContext.lastActions,
130+
);
124131

125132
const [maximized, setMaximized] = useState(false);
126133
const [isConvertToDialogOpen, setConvertToDialogOpen] = useState(false);
@@ -499,6 +506,7 @@ const TldrawCanvas = ({ title }: { title: string }) => {
499506
type: "Tldraw Error",
500507
context: {
501508
title: title,
509+
lastActions: lastActionsRef.current,
502510
},
503511
}).catch(() => {});
504512

@@ -600,6 +608,12 @@ const TldrawCanvas = ({ title }: { title: string }) => {
600608

601609
appRef.current = app;
602610

611+
app.on("change", (entry) => {
612+
lastActionsRef.current.push(entry);
613+
if (lastActionsRef.current.length > 10)
614+
lastActionsRef.current.shift();
615+
});
616+
603617
app.on("event", (event) => {
604618
const e = event as TLPointerEventInfo;
605619

0 commit comments

Comments
 (0)