|
@@ -4,11 +4,14 @@ import {
|
|
|
EmitFn,
|
|
|
isRef,
|
|
|
markRaw,
|
|
|
+ nextTick,
|
|
|
+ onUnmounted,
|
|
|
reactive,
|
|
|
Ref,
|
|
|
ref,
|
|
|
shallowReactive,
|
|
|
shallowRef,
|
|
|
+ watch,
|
|
|
watchEffect,
|
|
|
} from "vue";
|
|
|
import { useAutomaticData } from "./use-automatic-data";
|
|
@@ -38,6 +41,8 @@ import { mergeDescribes, PropertyKeys } from "../propertys";
|
|
|
import { useStore } from "../store";
|
|
|
import { globalWatch, useStage } from "./use-global-vars";
|
|
|
import { useAlignmentShape } from "./use-alignment";
|
|
|
+import { useViewer, useViewerTransform } from "./use-viewer";
|
|
|
+import { usePause } from "./use-pause";
|
|
|
|
|
|
type Emit<T> = EmitFn<{
|
|
|
updateShape: (value: T) => void;
|
|
@@ -303,3 +308,52 @@ export const useComponentsAttach = <T>(
|
|
|
cleanup: mergeFuns(cleanups),
|
|
|
};
|
|
|
};
|
|
|
+
|
|
|
+export const useOnComponentBoundChange = () => {
|
|
|
+ const getComponentData = useGetComponentData();
|
|
|
+ const transform = useViewerTransform()
|
|
|
+ const quitHooks = [] as Array<() => void>;
|
|
|
+ const destory = () => mergeFuns(quitHooks)();
|
|
|
+
|
|
|
+ const on = <T extends EntityShape>(
|
|
|
+ shape: Ref<T | undefined> | T | undefined,
|
|
|
+ callback: (shape: T, type: 'transform' | 'data') => void
|
|
|
+ ) => {
|
|
|
+ const $shape = computed(() => (shape = isRef(shape) ? shape.value : shape));
|
|
|
+ let repShape: T | undefined;
|
|
|
+ const item = getComponentData($shape);
|
|
|
+ const update = (type?: 'transform' | 'data') => {
|
|
|
+ $shape.value && !api.isPause && callback(repShape || $shape.value, type || 'data');
|
|
|
+ };
|
|
|
+ const sync = () => update()
|
|
|
+ const shapeListener = (shape: T) => {
|
|
|
+ repShape = shape.repShape as T || shape;
|
|
|
+ repShape.on("transform", sync);
|
|
|
+ shape.on("bound-change", sync);
|
|
|
+ return () => {
|
|
|
+ repShape!.off("transform", sync);
|
|
|
+ shape.off("bound-change", sync);
|
|
|
+ };
|
|
|
+ };
|
|
|
+
|
|
|
+ const onDestroy = mergeFuns([
|
|
|
+ watch(item, () => nextTick(() => update('data')), { deep: true }),
|
|
|
+ watch(transform, () => update('transform')),
|
|
|
+ watch($shape, (shape, _, onCleanup) => {
|
|
|
+ if (!shape) return;
|
|
|
+ onCleanup(shapeListener(shape));
|
|
|
+ }, {immediate: true}),
|
|
|
+ ])
|
|
|
+ quitHooks.push(onDestroy);
|
|
|
+
|
|
|
+ return () => {
|
|
|
+ const ndx = quitHooks.indexOf(onDestroy)
|
|
|
+ ~ndx && quitHooks.splice(ndx, 1)
|
|
|
+ onDestroy
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ const api = usePause({ destory, on });
|
|
|
+ onUnmounted(destory)
|
|
|
+ return api
|
|
|
+};
|