feat: highlight when editing relationship

This commit is contained in:
Huy Bui
2024-07-27 13:18:03 +07:00
parent 9a8fdbce84
commit 3aeb42a918
3 changed files with 52 additions and 10 deletions

View File

@@ -1,15 +1,42 @@
import { useRef } from "react"; import { useEffect, useState, useRef } from "react";
import { Cardinality, ObjectType, Tab } from "../../data/constants"; import { Cardinality, ObjectType, Tab } from "../../data/constants";
import { RELATIONSHIP_EDITING } from "../../data/customEvents";
import { calcPath } from "../../utils/calcPath"; import { calcPath } from "../../utils/calcPath";
import { useDiagram, useSettings, useLayout, useSelect } from "../../hooks"; import { useDiagram, useSettings, useLayout, useSelect } from "../../hooks";
import { cn } from "../../utils/cn";
export default function Relationship({ data }) { export default function Relationship({ data }) {
const [editing, setEditing] = useState(false);
const { settings } = useSettings(); const { settings } = useSettings();
const { tables } = useDiagram(); const { tables } = useDiagram();
const { layout } = useLayout(); const { layout } = useLayout();
const { selectedElement, setSelectedElement } = useSelect(); const { selectedElement, setSelectedElement } = useSelect();
const pathRef = useRef(); const pathRef = useRef();
useEffect(() => {
const handleEditing = (event) => {
setEditing(event.detail.id === data.id);
};
document.addEventListener(RELATIONSHIP_EDITING, handleEditing);
return () => {
document.removeEventListener(RELATIONSHIP_EDITING, handleEditing);
};
}, []);
useEffect(() => {
const handleClickAway = (event) => {
if (pathRef.current && !pathRef.current.contains(event.target)) {
setEditing(false);
}
};
document.addEventListener("mousedown", handleClickAway);
return () => {
document.removeEventListener("mousedown", handleClickAway);
};
}, [pathRef]);
let cardinalityStart = "1"; let cardinalityStart = "1";
let cardinalityEnd = "1"; let cardinalityEnd = "1";
@@ -50,6 +77,7 @@ export default function Relationship({ data }) {
} }
const edit = () => { const edit = () => {
setEditing(true);
if (!layout.sidebar) { if (!layout.sidebar) {
setSelectedElement((prev) => ({ setSelectedElement((prev) => ({
...prev, ...prev,
@@ -72,8 +100,15 @@ export default function Relationship({ data }) {
} }
}; };
const editingPathClass = cn("group-hover:stroke-sky-700", {
"stroke-sky-700": editing,
});
const editingCircleClass = cn("group-hover:fill-sky-700", {
"fill-sky-700": editing,
});
return ( return (
<g className="select-none group" onDoubleClick={edit}> <g className="select-none group cursor-pointer" onDoubleClick={edit}>
<path <path
ref={pathRef} ref={pathRef}
d={calcPath( d={calcPath(
@@ -91,10 +126,9 @@ export default function Relationship({ data }) {
settings.tableWidth, settings.tableWidth,
)} )}
stroke="gray" stroke="gray"
className="group-hover:stroke-sky-700" className={editingPathClass}
fill="none" fill="none"
strokeWidth={2} strokeWidth={2}
cursor="pointer"
/> />
{pathRef.current && settings.showCardinality && ( {pathRef.current && settings.showCardinality && (
<> <>
@@ -103,7 +137,7 @@ export default function Relationship({ data }) {
cy={cardinalityStartY} cy={cardinalityStartY}
r="12" r="12"
fill="grey" fill="grey"
className="group-hover:fill-sky-700" className={editingCircleClass}
/> />
<text <text
x={cardinalityStartX} x={cardinalityStartX}
@@ -120,7 +154,7 @@ export default function Relationship({ data }) {
cy={cardinalityEndY} cy={cardinalityEndY}
r="12" r="12"
fill="grey" fill="grey"
className="group-hover:fill-sky-700" className={editingCircleClass}
/> />
<text <text
x={cardinalityEndX} x={cardinalityEndX}

View File

@@ -4,6 +4,7 @@ import Empty from "../Empty";
import SearchBar from "./SearchBar"; import SearchBar from "./SearchBar";
import RelationshipInfo from "./RelationshipInfo"; import RelationshipInfo from "./RelationshipInfo";
import { ObjectType } from "../../../data/constants"; import { ObjectType } from "../../../data/constants";
import { RELATIONSHIP_EDITING } from "../../../data/customEvents";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
export default function RelationshipsTab() { export default function RelationshipsTab() {
@@ -23,14 +24,20 @@ export default function RelationshipsTab() {
} }
keepDOM keepDOM
lazyRender lazyRender
onChange={(k) => onChange={(k) => {
const newId = parseInt(k);
const event = new CustomEvent(RELATIONSHIP_EDITING, {
detail: { id: newId },
});
document.dispatchEvent(event);
setSelectedElement((prev) => ({ setSelectedElement((prev) => ({
...prev, ...prev,
open: true, open: true,
id: parseInt(k), id: newId,
element: ObjectType.RELATIONSHIP, element: ObjectType.RELATIONSHIP,
})) }));
} }}
accordion accordion
> >
{relationships.length <= 0 ? ( {relationships.length <= 0 ? (

1
src/data/customEvents.js Normal file
View File

@@ -0,0 +1 @@
export const RELATIONSHIP_EDITING = "relationship-editing";