|
@@ -4,6 +4,7 @@ import { Shape } from "konva/lib/Shape";
|
|
|
import {
|
|
|
globalWatch,
|
|
|
installGlobalVar,
|
|
|
+ usePointerIntersection,
|
|
|
usePointerIntersections,
|
|
|
usePointerPos,
|
|
|
useStage,
|
|
@@ -12,20 +13,19 @@ import {
|
|
|
import { useCan, useOperMode } from "./use-status";
|
|
|
import { Stage } from "konva/lib/Stage";
|
|
|
import { listener } from "../../utils/event.ts";
|
|
|
-import { inRevise, mergeFuns } from "../../utils/shared.ts";
|
|
|
+import { asyncTimeout, inRevise, mergeFuns } from "../../utils/shared.ts";
|
|
|
import { ComponentValue, DrawItem, ShapeType } from "../components";
|
|
|
import { shapeTreeContain, shapeTreeContains } from "../../utils/shape.ts";
|
|
|
import {
|
|
|
usePointerIsTransformerInner,
|
|
|
useTransformer,
|
|
|
} from "./use-transformer.ts";
|
|
|
-import { useAniamtion } from "./use-animation.ts";
|
|
|
import { KonvaEventObject } from "konva/lib/Node";
|
|
|
import { useStore } from "../store/index.ts";
|
|
|
import { Group } from "konva/lib/Group";
|
|
|
import { usePause } from "./use-pause.ts";
|
|
|
import { lineLen, Pos } from "@/utils/math.ts";
|
|
|
-import { Util } from "konva/lib/Util";
|
|
|
+import { useFormalLayer } from "./use-layer.ts";
|
|
|
|
|
|
const stageHoverMap = new WeakMap<
|
|
|
Stage,
|
|
@@ -76,8 +76,51 @@ export const getHoverShape = (stage: Stage) => {
|
|
|
return [hover, stop] as const;
|
|
|
};
|
|
|
|
|
|
+export const useShapeClick = (
|
|
|
+ shape: Ref<DC<Shape> | undefined>,
|
|
|
+ fn: () => void
|
|
|
+) => {
|
|
|
+ const init = (shape: Shape) => {
|
|
|
+ let downTime: number;
|
|
|
+ let move = false;
|
|
|
+ const downHandler = (ev: KonvaEventObject<any>) => {
|
|
|
+ ev.cancelBubble = true
|
|
|
+ downTime = Date.now();
|
|
|
+ move = false;
|
|
|
+ };
|
|
|
+ const moveHandler = (ev: KonvaEventObject<any>) => {
|
|
|
+ ev.cancelBubble = true
|
|
|
+ downTime = Date.now();
|
|
|
+ move = true;
|
|
|
+ };
|
|
|
+ const upHandler = (ev: KonvaEventObject<any>) => {
|
|
|
+ ev.cancelBubble = true
|
|
|
+ if (Date.now() - downTime < 500 && !move) {
|
|
|
+ fn();
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ shape.on("pointerdown.click", downHandler);
|
|
|
+ shape.on("pointermove.click", moveHandler);
|
|
|
+ shape.on("pointerup.click", upHandler);
|
|
|
+ return () => {
|
|
|
+ shape.off("pointerdown.click", downHandler);
|
|
|
+ shape.off("pointermove.click", moveHandler);
|
|
|
+ shape.off("pointerup.click", upHandler);
|
|
|
+ };
|
|
|
+ };
|
|
|
+ watch(shape, (shape, _, onCleanup) => {
|
|
|
+ const $shape = shape?.getNode();
|
|
|
+ $shape && onCleanup(init($shape));
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
export const useShapeIsHover = (shape: Ref<DC<EntityShape> | undefined>) => {
|
|
|
const stage = useStage();
|
|
|
+ const store = useStore();
|
|
|
+ const format = useFormalLayer();
|
|
|
+ const pos = usePointerPos();
|
|
|
+
|
|
|
const isHover = ref(false);
|
|
|
const stop = watch(
|
|
|
() => ({ stage: stage.value?.getNode(), shape: shape.value?.getNode() }),
|
|
@@ -86,15 +129,36 @@ export const useShapeIsHover = (shape: Ref<DC<EntityShape> | undefined>) => {
|
|
|
isHover.value = false;
|
|
|
return;
|
|
|
}
|
|
|
+ const forciblyCheck = async () => {
|
|
|
+ await asyncTimeout(6)
|
|
|
+ if (!pos.value || !format.value) {
|
|
|
+ isHover.value = false;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ isHover.value =
|
|
|
+ format.value.getIntersection(pos.value) === toRaw(shape);
|
|
|
+ };
|
|
|
+ store.bus.on("addItemAfter", forciblyCheck);
|
|
|
+ store.bus.on("setItemAfter", forciblyCheck);
|
|
|
+ store.bus.on("delItemAfter", forciblyCheck);
|
|
|
|
|
|
const [hoverShape, stopHoverListener] = getHoverShape(stage);
|
|
|
-
|
|
|
watchEffect(() => {
|
|
|
isHover.value = !!(
|
|
|
hoverShape.value && shapeTreeContain([shape], toRaw(hoverShape.value))
|
|
|
);
|
|
|
});
|
|
|
- onCleanup(stopHoverListener);
|
|
|
+ onCleanup(
|
|
|
+ mergeFuns([
|
|
|
+ stopHoverListener,
|
|
|
+ () => {
|
|
|
+ isHover.value = false;
|
|
|
+ store.bus.off("addItemAfter", forciblyCheck);
|
|
|
+ store.bus.off("setItemAfter", forciblyCheck);
|
|
|
+ store.bus.off("delItemAfter", forciblyCheck);
|
|
|
+ },
|
|
|
+ ])
|
|
|
+ );
|
|
|
},
|
|
|
{ immediate: true }
|
|
|
);
|