|
@@ -1,6 +1,8 @@
|
|
|
-import { Graph } from "@antv/x6";
|
|
|
-import { useState } from "react";
|
|
|
+import { Cell, EventArgs, Graph } from "@antv/x6";
|
|
|
+import { useEffect, useRef, useState } from "react";
|
|
|
import { message } from "antd";
|
|
|
+import brushImg from "@/assets/image/brush.svg"
|
|
|
+import { cellStyle } from '@/types'
|
|
|
|
|
|
interface PageSettings {
|
|
|
// 背景颜色
|
|
@@ -34,11 +36,12 @@ export default function appModel() {
|
|
|
// 格式刷启用
|
|
|
const [enableFormatBrush, setEnableFormatBrush] = useState(false);
|
|
|
// 格式刷样式
|
|
|
- const [formatBrushStyle, setFormatBrusStyle] = useState();
|
|
|
+ const formatBrushStyle = useRef<cellStyle>();
|
|
|
// 左侧面板激活
|
|
|
const [leftPanelActiveKey, setLeftPanelActiveKey] = useState("1");
|
|
|
// 右侧面板tab activeKey
|
|
|
const [rightPanelTabActiveKey, setRightPanelTabActiveKey] = useState("1");
|
|
|
+ const graphRef = useRef<Graph>();
|
|
|
const [pageState, setPageState] = useState<PageSettings>({
|
|
|
backgroundColor: "transparent",
|
|
|
width: 0,
|
|
@@ -72,7 +75,6 @@ export default function appModel() {
|
|
|
}
|
|
|
// 处理页面设置
|
|
|
const onChangePageSettings = (key: keyof PageSettings, value: any) => {
|
|
|
- console.log(key, value)
|
|
|
setPageState((state) => {
|
|
|
return {
|
|
|
...state,
|
|
@@ -95,21 +97,69 @@ export default function appModel() {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ useEffect(() => {
|
|
|
+ const graphRoot = document.querySelector(
|
|
|
+ "#graph-container"
|
|
|
+ ) as HTMLDivElement;
|
|
|
+ const pageRoot = document.querySelector(
|
|
|
+ "#flow_canvas_container"
|
|
|
+ ) as HTMLDivElement;
|
|
|
+
|
|
|
+ if(enableFormatBrush) {
|
|
|
+ graphRoot && (graphRoot.style.cursor = `url(${brushImg}), auto`);
|
|
|
+ pageRoot && (pageRoot.style.cursor = `url(${brushImg}), auto`);
|
|
|
+ } else {
|
|
|
+ graphRoot && (graphRoot.style.cursor = "default");
|
|
|
+ pageRoot && (pageRoot.style.cursor = "default");
|
|
|
+ }
|
|
|
+ }, [enableFormatBrush])
|
|
|
+
|
|
|
+ const handleClick = (args: EventArgs & { cell: Cell }) => {
|
|
|
+ // 取消格式刷
|
|
|
+ if(!args?.cell || args?.cell?.data?.isPage) {
|
|
|
+ formatBrushStyle.current = undefined;
|
|
|
+ setEnableFormatBrush(false);
|
|
|
+ graphRef.current?.off("cell:click", handleClick);
|
|
|
+ graphRef.current?.off("blank:click", handleClick);
|
|
|
+ } else {
|
|
|
+ if(args.cell.data?.lock) return;
|
|
|
+ const data = args.cell.data;
|
|
|
+ args.cell.setData({
|
|
|
+ text: formatBrushStyle.current?.text || data?.text,
|
|
|
+ fill: formatBrushStyle.current?.fill || data?.fill,
|
|
|
+ stroke: formatBrushStyle.current?.stroke || data?.stroke,
|
|
|
+ opacity: formatBrushStyle.current?.opacity || data?.opacity
|
|
|
+ })
|
|
|
+ console.log(args.cell.data, formatBrushStyle.current)
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
// 开启格式刷
|
|
|
const toggleFormatBrush = (graph: Graph) => {
|
|
|
+ graphRef.current = graph;
|
|
|
const cell = graph?.getSelectedCells()?.find(item => item.isNode());
|
|
|
setEnableFormatBrush((state) => {
|
|
|
- if(state) {
|
|
|
+ if(!state) {
|
|
|
const data = cell?.getData();
|
|
|
- setFormatBrusStyle(data);
|
|
|
+ formatBrushStyle.current = data;
|
|
|
message.info('格式刷已开启');
|
|
|
+ graph.on("cell:click", handleClick);
|
|
|
+ graph.on("blank:click", handleClick);
|
|
|
} else {
|
|
|
- setFormatBrusStyle(undefined);
|
|
|
+ formatBrushStyle.current = undefined;
|
|
|
+ graph.off("cell:click", handleClick);
|
|
|
+ graph.off("blank:click", handleClick);
|
|
|
}
|
|
|
return !state;
|
|
|
- })
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
+ document.addEventListener("keydown", (e) => {
|
|
|
+ if (e.key === "Escape") {
|
|
|
+ setEnableFormatBrush(false);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
return {
|
|
|
showRightPanel,
|
|
|
toggleRightPanel,
|