Skip to content

Commit b2a17ec

Browse files
authored
[ENG-1108] Canvas tool lock (#851)
* wip * lint * update * address PR comments * new behavior * address PR comment * lint * address PR comments * lint * address PR comments * further cleanup * another fix
1 parent 98eb74e commit b2a17ec

6 files changed

Lines changed: 23 additions & 17 deletions

File tree

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,6 @@ const ClipboardPageSection = ({
672672
},
673673
};
674674
editor.createShape<DiscourseNodeShape>(shape);
675-
editor.setCurrentTool("select");
676675
},
677676
[editor, extensionAPI, showNodesOnCanvas],
678677
);

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ export const createNodeShapeTools = (
110110
return class DiscourseNodeTool extends StateNode {
111111
static id = n.type;
112112
static initial = "idle";
113+
static isLockable = true;
113114
shapeType = n.type;
114115

115116
override onEnter = () => {
@@ -130,7 +131,6 @@ export const createNodeShapeTools = (
130131
props: { fontFamily: "sans", size: "s" },
131132
});
132133
this.editor.setEditingShape(shapeId);
133-
this.editor.setCurrentTool("select");
134134
};
135135
};
136136
});
@@ -519,6 +519,19 @@ export class BaseDiscourseNodeUtil extends BaseBoxShapeUtil<DiscourseNodeShape>
519519
this.updateProps(shape.id, shape.type, { h, w, imageUrl });
520520
};
521521

522+
const wasToolLocked = this.editor.getInstanceState().isToolLocked;
523+
524+
const restoreToolState = () => {
525+
if (wasToolLocked) {
526+
this.editor.updateInstanceState({ isToolLocked: true });
527+
this.editor.setCurrentTool(shape.type);
528+
} else {
529+
this.editor.setCurrentTool("select");
530+
}
531+
editor.setEditingShape(null);
532+
dialogRenderedRef.current = false;
533+
};
534+
522535
renderModifyNodeDialog({
523536
mode: isCreating ? "create" : "edit",
524537
nodeType: shape.type,
@@ -570,13 +583,14 @@ export class BaseDiscourseNodeUtil extends BaseBoxShapeUtil<DiscourseNodeShape>
570583
});
571584
}
572585
}
573-
574-
editor.setEditingShape(null);
575-
dialogRenderedRef.current = false;
576586
},
577587
onClose: () => {
578-
editor.setEditingShape(null);
579-
dialogRenderedRef.current = false;
588+
if (isCreating) {
589+
restoreToolState();
590+
} else {
591+
editor.setEditingShape(null);
592+
dialogRenderedRef.current = false;
593+
}
580594
},
581595
});
582596

apps/roam/src/components/canvas/DiscourseRelationShape/DiscourseRelationTool.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const createAllReferencedNodeTools = (
2828
class ReferencedNodeTool extends StateNode {
2929
static override initial = "idle";
3030
static override id = action;
31+
static override isLockable = true;
3132
static override children = (): TLStateNodeConstructor[] => [
3233
this.Idle,
3334
this.Pointing,
@@ -277,10 +278,6 @@ export const createAllReferencedNodeTools = (
277278
this.editor.setCursor({ type: "cross" });
278279
};
279280

280-
override onCancel = () => {
281-
this.editor.setCurrentTool("select");
282-
};
283-
284281
override onKeyUp: TLEventHandlers["onKeyUp"] = (info) => {
285282
if (info.key === "Enter") {
286283
if (this.editor.getInstanceState().isReadonly) return null;
@@ -315,6 +312,7 @@ export const createAllRelationShapeTools = (
315312
class RelationShapeTool extends StateNode {
316313
static override initial = "idle";
317314
static override id = name;
315+
static override isLockable = true;
318316
static override children = (): TLStateNodeConstructor[] => [
319317
this.Idle,
320318
this.Pointing,
@@ -573,10 +571,6 @@ export const createAllRelationShapeTools = (
573571
this.editor.setCursor({ type: "cross", rotation: 0 });
574572
};
575573

576-
override onCancel = () => {
577-
this.editor.setCurrentTool("select");
578-
};
579-
580574
override onKeyUp: TLEventHandlers["onKeyUp"] = (info) => {
581575
if (info.key === "Enter") {
582576
if (this.editor.getInstanceState().isReadonly) return null;

apps/roam/src/components/canvas/DiscourseRelationShape/DiscourseRelationUtil.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,6 @@ export class BaseDiscourseRelationUtil extends ShapeUtil<DiscourseRelationShape>
10861086
static override props = arrowShapeProps;
10871087

10881088
cancelAndWarn = (title: string) => {
1089-
this.editor.setCurrentTool("select");
10901089
dispatchToastEvent({
10911090
id: `tldraw-cancel-and-warn-${title}`,
10921091
title,

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ const DiscourseGraphPanel = ({
190190
props: { fontFamily: "sans", size: "s" },
191191
});
192192
editor.setEditingShape(shapeId);
193-
editor.setCurrentTool("select");
194193
} else {
195194
// For relations, just activate the tool
196195
editor.setCurrentTool(current.item.id);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,7 @@ const TldrawCanvasShared = ({
693693
const discourseGraphTool = class DiscourseGraphTool extends StateNode {
694694
static override id = "discourse-tool";
695695
static override initial = "idle";
696+
static override isLockable = true;
696697
};
697698
const discourseNodeTools = createNodeShapeTools(allNodes);
698699
const discourseRelationTools = createAllRelationShapeTools(allRelationNames);

0 commit comments

Comments
 (0)