Skip to content

Commit 9c7a8a8

Browse files
herzadinataBOCOVO
authored andcommitted
fix(dblm-ext): unfocus table when click outside
1 parent e977f20 commit 9c7a8a8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

packages/json-table-schema-visualizer/src/components/DiagramViewer/DiagramWrapper.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { useScrollDirectionContext } from "@/hooks/scrollDirection";
1717
import { ScrollDirection } from "@/types/scrollDirection";
1818
import eventEmitter from "@/events-emitter";
1919
import { tableCoordsStore } from "@/stores/tableCoords";
20+
import { useTablesInfo } from "@/hooks/table";
2021

2122
interface DiagramWrapperProps {
2223
children: ReactNode;
@@ -31,6 +32,7 @@ const DiagramWrapper = ({ children }: DiagramWrapperProps) => {
3132
useCursorChanger("grabbing");
3233
const themeColors = useThemeColors();
3334
const stageRef = useRef<null | CoreStage>(null);
35+
const { hoveredTableName, setHoveredTableName } = useTablesInfo();
3436

3537
// repositioning the stage only once
3638
const { scale: defaultStageScale, position: defaultStagePosition } =
@@ -83,6 +85,31 @@ const DiagramWrapper = ({ children }: DiagramWrapperProps) => {
8385
stageStateStore.set({ scale: newScale, position: newPos });
8486
};
8587

88+
const nodeBelongsToTable = (node: any): boolean => {
89+
let currentNode = node;
90+
while (currentNode != null) {
91+
if (
92+
typeof currentNode.name === "function" &&
93+
typeof currentNode.name() === "string"
94+
) {
95+
const names = (currentNode.name() as string).split(/\s+/);
96+
if (names.some((n) => n.startsWith("table-"))) {
97+
return true;
98+
}
99+
}
100+
currentNode = currentNode.getParent?.() ?? null;
101+
}
102+
return false;
103+
};
104+
105+
const handleStagePointerDown = (
106+
e: KonvaEventObject<MouseEvent | TouchEvent>,
107+
) => {
108+
if (hoveredTableName == null) return;
109+
if (nodeBelongsToTable(e.target)) return;
110+
setHoveredTableName(null);
111+
};
112+
86113
const fitToView = () => {
87114
if (stageRef.current != null) {
88115
const stage = stageRef.current;
@@ -183,6 +210,8 @@ const DiagramWrapper = ({ children }: DiagramWrapperProps) => {
183210
onDragStart={onGrabbing}
184211
onDragEnd={onGrabRelease}
185212
onWheel={handleZooming}
213+
onMouseDown={handleStagePointerDown}
214+
onTouchStart={handleStagePointerDown}
186215
width={windowWidth}
187216
height={windowHeight}
188217
style={{ width: "fit-content", backgroundColor: themeColors.bg }}

0 commit comments

Comments
 (0)