Skip to content

Commit

Permalink
Check stateId before editing tldraw store (#142)
Browse files Browse the repository at this point in the history
* if localStateId = propStateId don't update store

* 1.10.9

* match nanoid dependancy

* splice refactor
  • Loading branch information
mdroidian authored Aug 15, 2023
1 parent e2fe5c7 commit d53af7c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
11 changes: 5 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "query-builder",
"version": "1.10.8",
"version": "1.10.9",
"description": "Introduces new user interfaces for building queries in Roam",
"main": "./build/main.js",
"author": {
Expand Down Expand Up @@ -30,6 +30,7 @@
"@tldraw/tldraw": "^2.0.0-alpha.12",
"contrast-color": "^1.0.1",
"cytoscape-navigator": "^2.0.1",
"nanoid": "2.0.4",
"react-charts": "^3.0.0-beta.48",
"react-draggable": "^4.4.5",
"react-in-viewport": "^1.0.0-alpha.20",
Expand Down
9 changes: 8 additions & 1 deletion src/components/TldrawCanvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ import getDiscourseContextResults from "../utils/getDiscourseContextResults";
import { StoreSnapshot } from "@tldraw/tlstore";
import setInputSetting from "roamjs-components/util/setInputSetting";
import ContrastColor from "contrast-color";
import nanoid from "nanoid";

declare global {
interface Window {
Expand Down Expand Up @@ -1208,6 +1209,7 @@ const TldrawCanvas = ({ title }: Props) => {
}, [tree, pageUid]);
const containerRef = useRef<HTMLDivElement>(null);
const [maximized, setMaximized] = useState(false);
const localStateIds: string[] = [];
const store = useMemo(() => {
const _store = customTldrawConfig.createStore({
initialData: initialState.data,
Expand All @@ -1232,19 +1234,22 @@ const TldrawCanvas = ({ title }: Props) => {
typeof props["roamjs-query-builder"] === "object"
? props["roamjs-query-builder"]
: {};
// we need this bc Roam doesn't update edit/user or edit/time when we just edit block/props
await setInputSetting({
blockUid: pageUid,
key: "timestamp",
value: new Date().valueOf().toString(),
});
const newstateId = nanoid();
localStateIds.push(newstateId);
localStateIds.splice(0, localStateIds.length - 25);
window.roamAlphaAPI.updateBlock({
block: {
uid: pageUid,
props: {
...props,
["roamjs-query-builder"]: {
...rjsqb,
stateId: newstateId,
tldraw: state,
},
},
Expand All @@ -1264,6 +1269,8 @@ const TldrawCanvas = ({ title }: Props) => {
(after?.[":block/props"] || {}) as json
) as Record<string, json>;
const rjsqb = props["roamjs-query-builder"] as Record<string, unknown>;
const propsStateId = rjsqb?.stateId as string;
if (localStateIds.some((s) => s === propsStateId)) return;
const newState = rjsqb?.tldraw as Parameters<
typeof store.deserialize
>[0];
Expand Down

0 comments on commit d53af7c

Please sign in to comment.