|
@@ -1,4 +1,10 @@
|
|
|
-import { Attrib, ShapeStyles, ShapeStylesStatus, ShapeType } from "../type";
|
|
|
+import {
|
|
|
+ Attrib,
|
|
|
+ ShapeStyles,
|
|
|
+ ShapeStylesStatus,
|
|
|
+ ShapeStylesStatusKey,
|
|
|
+ ShapeType,
|
|
|
+} from "../type";
|
|
|
import { Entity } from "../packages/entity";
|
|
|
import { Shape } from "konva/lib/Shape";
|
|
|
import { Group } from "konva/lib/Group";
|
|
@@ -8,7 +14,6 @@ import { KonvaEventObject } from "konva/lib/Node";
|
|
|
let mouseDisabled = false;
|
|
|
const dragShapes: (Shape | Group)[] = [];
|
|
|
export const disableMouse = () => {
|
|
|
- console.log("disable mose");
|
|
|
mouseDisabled = true;
|
|
|
dragShapes.forEach((shape) => shape.draggable(false));
|
|
|
};
|
|
@@ -42,9 +47,6 @@ export const openShapeMouseStyles = <T extends Shape | Group>(
|
|
|
if (styles.bus) {
|
|
|
styles.bus.on("statusChange", (status) => {
|
|
|
outStatus = status;
|
|
|
- if (status) {
|
|
|
- Object.assign(inStatus, status);
|
|
|
- }
|
|
|
mouseHandler();
|
|
|
});
|
|
|
}
|
|
@@ -55,79 +57,103 @@ export const openShapeMouseStyles = <T extends Shape | Group>(
|
|
|
active: false,
|
|
|
common: false,
|
|
|
};
|
|
|
- let prevApi = "common";
|
|
|
+ let prevApi: ShapeStylesStatusKey = "common";
|
|
|
+ let prevInApi: ShapeStylesStatusKey = "common";
|
|
|
+ let prevInType: "click" | "mouse";
|
|
|
+ const mouseHandler = (
|
|
|
+ ev?: KonvaEventObject<any>,
|
|
|
+ type?: "click" | "mouse"
|
|
|
+ ) => {
|
|
|
+ const inApi = getActiveKey(inStatus);
|
|
|
+ if (styles.bus && (prevInApi !== inApi || prevInType !== type)) {
|
|
|
+ const before = prevApi;
|
|
|
+ prevInApi = inApi;
|
|
|
+ styles.bus.emit("shapeStatusChange", {
|
|
|
+ current: inApi,
|
|
|
+ before: before,
|
|
|
+ type,
|
|
|
+ });
|
|
|
+ }
|
|
|
|
|
|
- const mouseHandler = (ev?: KonvaEventObject<any>) => {
|
|
|
- let api: string = outStatus && getActiveKey(outStatus);
|
|
|
- if (!api) {
|
|
|
- api = mouseDisabled ? "common" : getActiveKey(inStatus) || "common";
|
|
|
+ let api: ShapeStylesStatusKey = outStatus
|
|
|
+ ? getActiveKey(outStatus)
|
|
|
+ : mouseDisabled
|
|
|
+ ? null
|
|
|
+ : inApi;
|
|
|
+
|
|
|
+ const ls = outStatus ? [outStatus, inStatus] : [inStatus];
|
|
|
+ for (const l of ls) {
|
|
|
+ const showStatus = { ...l };
|
|
|
+ while (api && !(api in styles)) {
|
|
|
+ console.log(shape.id(), api, styles);
|
|
|
+ showStatus[api] = false;
|
|
|
+ api = getActiveKey(showStatus);
|
|
|
+ }
|
|
|
+ if (api) break;
|
|
|
}
|
|
|
+
|
|
|
+ // console.log(shape.id(), api, outStatus);
|
|
|
+ api = api || "common";
|
|
|
if (prevApi !== api) {
|
|
|
- styles[api] && styles[api](ev);
|
|
|
+ styles[api](ev);
|
|
|
prevApi = api;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- if (styles.hover) {
|
|
|
- useOn(`mouseenter.${namespace}`, (ev) => {
|
|
|
- inStatus.hover = true;
|
|
|
- mouseHandler(ev);
|
|
|
- });
|
|
|
- useOn(`mouseleave.${namespace}`, (ev) => {
|
|
|
- inStatus.hover = false;
|
|
|
- mouseHandler(ev);
|
|
|
- });
|
|
|
- }
|
|
|
+ useOn(`mouseenter.${namespace}`, (ev) => {
|
|
|
+ inStatus.hover = true;
|
|
|
+ mouseHandler(ev, "mouse");
|
|
|
+ });
|
|
|
+ useOn(`mouseleave.${namespace}`, (ev) => {
|
|
|
+ inStatus.hover = false;
|
|
|
+ mouseHandler(ev, "mouse");
|
|
|
+ });
|
|
|
|
|
|
let unCheckActive: () => void;
|
|
|
- if (styles.active) {
|
|
|
- let clickTime: number = 0;
|
|
|
- useOn(`click.${namespace} touchend.${namespace}`, (ev) => {
|
|
|
- if (!styles.active) return;
|
|
|
- inStatus.active = true;
|
|
|
- mouseHandler(ev);
|
|
|
- clickTime = Date.now();
|
|
|
- });
|
|
|
+ let clickTime: number = 0;
|
|
|
+ useOn(`click.${namespace} touchend.${namespace}`, (ev) => {
|
|
|
+ inStatus.active = true;
|
|
|
+ mouseHandler(ev, "click");
|
|
|
+ clickTime = Date.now();
|
|
|
+ });
|
|
|
|
|
|
- const e = `click.${namespace}${shape.id()} touchend.${namespace}${shape.id()}`;
|
|
|
- let count = 3;
|
|
|
- let stage;
|
|
|
- let tip = false;
|
|
|
- const interval = setInterval(() => {
|
|
|
- if (!(stage = shape.getStage())) {
|
|
|
- if (count < -5) {
|
|
|
- clearInterval(interval);
|
|
|
- console.log(shape.name(), shape.id());
|
|
|
- }
|
|
|
- if (--count < 0) {
|
|
|
- tip && console.error("可能发生资源泄露");
|
|
|
- tip = true;
|
|
|
- }
|
|
|
- return;
|
|
|
+ const e = `click.${namespace}${shape.id()} touchend.${namespace}${shape.id()}`;
|
|
|
+ let count = 3;
|
|
|
+ let stage;
|
|
|
+ let tip = false;
|
|
|
+ const interval = setInterval(() => {
|
|
|
+ if (!(stage = shape.getStage())) {
|
|
|
+ if (count < -5) {
|
|
|
+ clearInterval(interval);
|
|
|
}
|
|
|
- clearInterval(interval);
|
|
|
- stage.on(e, (evt) => {
|
|
|
- if (Date.now() - clickTime > 500 && evt.target !== shape) {
|
|
|
- inStatus.active = false;
|
|
|
- mouseHandler(evt);
|
|
|
- }
|
|
|
- });
|
|
|
- }, 100);
|
|
|
+ if (--count < 0) {
|
|
|
+ tip && console.error("可能发生资源泄露");
|
|
|
+ tip = true;
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ clearInterval(interval);
|
|
|
+ stage.on(e, (evt) => {
|
|
|
+ if (Date.now() - clickTime > 50 && evt.target !== shape) {
|
|
|
+ inStatus.active = false;
|
|
|
+ mouseHandler(evt, "click");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }, 100);
|
|
|
|
|
|
- unCheckActive = () => {
|
|
|
- stage && stage.off(e);
|
|
|
- clearInterval(interval);
|
|
|
- };
|
|
|
- }
|
|
|
+ unCheckActive = () => {
|
|
|
+ stage && stage.off(e);
|
|
|
+ clearInterval(interval);
|
|
|
+ };
|
|
|
|
|
|
useOn(`dragstart.${namespace}`, (ev) => {
|
|
|
inStatus.draging = true;
|
|
|
- mouseHandler(ev);
|
|
|
+ mouseHandler(ev, "mouse");
|
|
|
});
|
|
|
useOn(`dragend.${namespace}`, (ev) => {
|
|
|
setTimeout(() => {
|
|
|
inStatus.draging = false;
|
|
|
- mouseHandler(ev);
|
|
|
+ mouseHandler(ev, "mouse");
|
|
|
}, 16);
|
|
|
});
|
|
|
|