|
|
@@ -1,7 +1,12 @@
|
|
|
import { Viewer } from "../viewer.ts";
|
|
|
import { computed, Ref, ref, watch, watchEffect } from "vue";
|
|
|
import { dragListener, scaleListener } from "../../utils/event.ts";
|
|
|
-import { globalWatch, installGlobalVar, useStage } from "./use-global-vars.ts";
|
|
|
+import {
|
|
|
+ globalWatch,
|
|
|
+ globalWatchEffect,
|
|
|
+ installGlobalVar,
|
|
|
+ useStage,
|
|
|
+} from "./use-global-vars.ts";
|
|
|
import { useCan } from "./use-status";
|
|
|
import { frameEebounce, mergeFuns } from "../../utils/shared.ts";
|
|
|
import { Transform } from "konva/lib/Util";
|
|
|
@@ -40,28 +45,38 @@ export const useViewer = installGlobalVar(() => {
|
|
|
}),
|
|
|
scaleListener(dom, (info) => {
|
|
|
if (can.viewMode || disabled.value) {
|
|
|
-
|
|
|
viewer.scalePixel(info.center, info.scale);
|
|
|
}
|
|
|
}),
|
|
|
- watchEffect(
|
|
|
- () => {
|
|
|
- size.value && viewer.setSize(size.value);
|
|
|
- },
|
|
|
- { flush: "sync" }
|
|
|
- )
|
|
|
);
|
|
|
|
|
|
- viewer.bus.on("transformChange", (newTransform) => {
|
|
|
- transform.value = newTransform;
|
|
|
- });
|
|
|
- viewer.bus.on("viewSizeChange", () => {
|
|
|
- sizeMat.value = viewer.sizeMat;
|
|
|
- });
|
|
|
- transform.value = viewer.transform;
|
|
|
- sizeMat.value = viewer.sizeMat;
|
|
|
return onDestroy;
|
|
|
};
|
|
|
+ const stopSizeWatch = globalWatchEffect(
|
|
|
+ () => {
|
|
|
+ size.value && viewer.setSize(size.value);
|
|
|
+ },
|
|
|
+ { flush: "sync" },
|
|
|
+ );
|
|
|
+ viewer.bus.on("transformChange", (newTransform) => {
|
|
|
+ transform.value = newTransform;
|
|
|
+ });
|
|
|
+ viewer.bus.on("viewSizeChange", () => {
|
|
|
+ sizeMat.value = viewer.sizeMat;
|
|
|
+ });
|
|
|
+ transform.value = viewer.transform;
|
|
|
+ sizeMat.value = viewer.sizeMat;
|
|
|
+
|
|
|
+ const stopModeWatch = globalWatch(
|
|
|
+ () => can.viewMouseReact,
|
|
|
+ (can, _, onCleanup) => {
|
|
|
+ if (can) {
|
|
|
+ const dom = stage.value!.getNode().container();
|
|
|
+ onCleanup(init(dom));
|
|
|
+ }
|
|
|
+ },
|
|
|
+ { immediate: true },
|
|
|
+ );
|
|
|
|
|
|
return {
|
|
|
var: {
|
|
|
@@ -70,16 +85,7 @@ export const useViewer = installGlobalVar(() => {
|
|
|
sizeMat,
|
|
|
disabled,
|
|
|
},
|
|
|
- onDestroy: globalWatch(
|
|
|
- () => can.viewMouseReact,
|
|
|
- (can, _, onCleanup) => {
|
|
|
- if (can) {
|
|
|
- const dom = stage.value!.getNode().container();
|
|
|
- onCleanup(init(dom));
|
|
|
- }
|
|
|
- },
|
|
|
- { immediate: true }
|
|
|
- ),
|
|
|
+ onDestroy: mergeFuns(stopSizeWatch, stopModeWatch),
|
|
|
};
|
|
|
}, Symbol("viewer"));
|
|
|
|
|
|
@@ -103,23 +109,23 @@ export const useViewerInvertTransformConfig = () => {
|
|
|
return computed(() => transform.value.decompose());
|
|
|
};
|
|
|
|
|
|
-export const useViewerDebounce = installGlobalVar(() => ref(true))
|
|
|
+export const useViewerDebounce = installGlobalVar(() => ref(true));
|
|
|
|
|
|
export const useFixedScale = installGlobalVar(() => {
|
|
|
const inv = useViewerInvertTransformConfig();
|
|
|
- const debounce = useViewerDebounce()
|
|
|
+ const debounce = useViewerDebounce();
|
|
|
const scale = ref(inv.value.scaleX) as Ref<number> & { debounce: boolean };
|
|
|
|
|
|
const update = () => {
|
|
|
const newScale = inv.value.scaleX;
|
|
|
scale.value = newScale;
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
const debounceUpdate = frameEebounce(update);
|
|
|
const stopWatch = globalWatch(
|
|
|
() => [inv.value, debounce.value],
|
|
|
() => (debounce.value ? debounceUpdate() : update()),
|
|
|
- { flush: "sync" }
|
|
|
+ { flush: "sync" },
|
|
|
);
|
|
|
|
|
|
return {
|
|
|
@@ -136,13 +142,13 @@ export const useUnitTransform = installGlobalVar(() => {
|
|
|
getPixel(real: number) {
|
|
|
return lineLen(
|
|
|
invTransform.value.point({ x: real, y: 0 }),
|
|
|
- invTransform.value.point({ x: 0, y: 0 })
|
|
|
+ invTransform.value.point({ x: 0, y: 0 }),
|
|
|
);
|
|
|
},
|
|
|
getReal(pixel: number) {
|
|
|
return lineLen(
|
|
|
transform.value.point({ x: pixel, y: 0 }),
|
|
|
- transform.value.point({ x: 0, y: 0 })
|
|
|
+ transform.value.point({ x: 0, y: 0 }),
|
|
|
);
|
|
|
},
|
|
|
};
|
|
|
@@ -231,7 +237,7 @@ export const useGetViewBoxPositionPixel = () => {
|
|
|
return getFixPosition(
|
|
|
fixPosition,
|
|
|
selfSize,
|
|
|
- size.value || { width: 100, height: 100 }
|
|
|
+ size.value || { width: 100, height: 100 },
|
|
|
);
|
|
|
}
|
|
|
};
|