|
@@ -6,6 +6,7 @@ import {
|
|
|
nextTick,
|
|
|
onUnmounted,
|
|
|
reactive,
|
|
|
+ Ref,
|
|
|
ref,
|
|
|
shallowRef,
|
|
|
watch,
|
|
@@ -67,19 +68,19 @@ export const installGlobalVar = <T>(
|
|
|
export type InstanceProps = {
|
|
|
id?: string;
|
|
|
data: DrawData;
|
|
|
- handlerResource(file: File): Promise<string>
|
|
|
-}
|
|
|
+ handlerResource(file: File): Promise<string>;
|
|
|
+};
|
|
|
export const useInstanceProps = installGlobalVar(() => {
|
|
|
- const props = ref<InstanceProps>()
|
|
|
+ const props = ref<InstanceProps>();
|
|
|
return {
|
|
|
set(val: InstanceProps) {
|
|
|
- props.value = val
|
|
|
+ props.value = val;
|
|
|
},
|
|
|
get() {
|
|
|
- return props.value!
|
|
|
- }
|
|
|
- }
|
|
|
-}, Symbol('instanceId'))
|
|
|
+ return props.value!;
|
|
|
+ },
|
|
|
+ };
|
|
|
+}, Symbol("instanceId"));
|
|
|
|
|
|
export const stackVar = <T>(init?: T) => {
|
|
|
const stack = reactive([init]) as T[];
|
|
@@ -125,21 +126,21 @@ export const globalWatch = <T>(
|
|
|
};
|
|
|
|
|
|
export const useStage = installGlobalVar(
|
|
|
- () => shallowRef<DC<Stage> | undefined>(),
|
|
|
+ () => shallowRef<DC<Stage> | undefined>(),
|
|
|
Symbol("stage")
|
|
|
);
|
|
|
export const useMode = installGlobalVar(() => {
|
|
|
- const stack = stackVar(new Set([Mode.write]))
|
|
|
+ const stack = stackVar(new Set([Mode.write]));
|
|
|
const modeStack = {
|
|
|
...stack,
|
|
|
get value() {
|
|
|
- return stack.value
|
|
|
+ return stack.value;
|
|
|
},
|
|
|
set value(val: Set<Mode>) {
|
|
|
- stack.value = val
|
|
|
+ stack.value = val;
|
|
|
},
|
|
|
push(...modes: Mode[]) {
|
|
|
- return stack.push(new Set(modes))
|
|
|
+ return stack.push(new Set(modes));
|
|
|
},
|
|
|
include(...modes: Mode[]) {
|
|
|
return modes.every((m) => modeStack.value.has(m));
|
|
@@ -149,44 +150,49 @@ export const useMode = installGlobalVar(() => {
|
|
|
},
|
|
|
del(...modes: Mode[]) {
|
|
|
modes.forEach((mode) => modeStack.value.delete(mode));
|
|
|
- }
|
|
|
- }
|
|
|
- if (import.meta.env.DEV) {
|
|
|
- watchEffect(() => {
|
|
|
- console.error([...modeStack.value.values()].join(','))
|
|
|
- }, { flush: 'sync' })
|
|
|
- }
|
|
|
+ },
|
|
|
+ };
|
|
|
+ // if (import.meta.env.DEV) {
|
|
|
+ // watchEffect(
|
|
|
+ // () => {
|
|
|
+ // console.error([...modeStack.value.values()].join(","));
|
|
|
+ // },
|
|
|
+ // { flush: "sync" }
|
|
|
+ // );
|
|
|
+ // }
|
|
|
return modeStack;
|
|
|
}, Symbol("mode"));
|
|
|
export const useCan = installGlobalVar(() => {
|
|
|
const mode = useMode();
|
|
|
const stage = useStage();
|
|
|
- const key = useDownKeys()
|
|
|
- const loaded = computed(() => !!stage.value?.getStage())
|
|
|
+ const key = useDownKeys();
|
|
|
+ const loaded = computed(() => !!stage.value?.getStage());
|
|
|
|
|
|
// 鼠标是否可用
|
|
|
- const mouse = computed(() => loaded.value && !mode.include(Mode.static))
|
|
|
+ const mouse = computed(() => loaded.value && !mode.include(Mode.static));
|
|
|
|
|
|
// 可以进入拖拽模式
|
|
|
const dragMode = computed(() => {
|
|
|
- if (!mouse.value || mode.include(Mode.readonly) || key.has(' ')) return false;
|
|
|
- return mode.include(Mode.draw) || mode.include(Mode.update)
|
|
|
- })
|
|
|
+ if (!mouse.value || mode.include(Mode.readonly) || key.has(" "))
|
|
|
+ return false;
|
|
|
+ return mode.include(Mode.draw) || mode.include(Mode.update);
|
|
|
+ });
|
|
|
|
|
|
// 是否在视图模式
|
|
|
const viewMode = computed(() => {
|
|
|
- return mouse.value && (!mode.include(Mode.draging) || key.has(' '))
|
|
|
- })
|
|
|
+ return mouse.value && (!mode.include(Mode.draging) || key.has(" "));
|
|
|
+ });
|
|
|
|
|
|
// shape是否可以对鼠标做出反应
|
|
|
- const mouseReact = computed(() => mouse.value && (mode.include(Mode.write) || mode.include(Mode.update)))
|
|
|
+ const mouseReact = computed(
|
|
|
+ () => mouse.value && (mode.include(Mode.write) || mode.include(Mode.update))
|
|
|
+ );
|
|
|
|
|
|
// 可以进入编辑模式
|
|
|
- const editMode = computed(() => mouse.value && mode.include(Mode.write))
|
|
|
+ const editMode = computed(() => mouse.value && mode.include(Mode.write));
|
|
|
|
|
|
// 可以进入绘制模式
|
|
|
- const drawMode = computed(() => mouse.value && mode.include(Mode.write))
|
|
|
-
|
|
|
+ const drawMode = computed(() => mouse.value && mode.include(Mode.write));
|
|
|
|
|
|
return reactive({
|
|
|
viewMouseReact: mouse,
|
|
@@ -200,7 +206,33 @@ export const useCan = installGlobalVar(() => {
|
|
|
|
|
|
export const usePointerPos = installGlobalVar(() => {
|
|
|
const stage = useStage();
|
|
|
- const pos = ref<Pos | null>(null);
|
|
|
+ const pos = ref(null) as Ref<Pos | null> & { replay: () => void };
|
|
|
+ let lastClient: { clientX: number; clientY: number } | null = null;
|
|
|
+ let replayIng = false;
|
|
|
+
|
|
|
+ const replay = () => {
|
|
|
+ const $stage = stage.value?.getNode();
|
|
|
+ if (!$stage || !lastClient) return;
|
|
|
+ replayIng = true;
|
|
|
+ const dom = $stage.container().querySelector("canvas") as HTMLCanvasElement;
|
|
|
+ const moveConf = {
|
|
|
+ bubbles: true, // 事件是否能够冒泡
|
|
|
+ cancelable: true, // 事件是否可以被取消
|
|
|
+ isPrimary: true,
|
|
|
+ pointerId: 1,
|
|
|
+ };
|
|
|
+ dom.dispatchEvent(
|
|
|
+ new PointerEvent("pointermove", {
|
|
|
+ ...moveConf,
|
|
|
+ clientX: -9999999,
|
|
|
+ clientY: -9999999,
|
|
|
+ })
|
|
|
+ );
|
|
|
+ dom.dispatchEvent(
|
|
|
+ new PointerEvent("pointermove", { ...lastClient, ...moveConf })
|
|
|
+ );
|
|
|
+ replayIng = false;
|
|
|
+ };
|
|
|
|
|
|
watchEffect((onCleanup) => {
|
|
|
const $stage = stage.value?.getNode();
|
|
@@ -209,15 +241,22 @@ export const usePointerPos = installGlobalVar(() => {
|
|
|
const mount = $stage.container().parentElement!;
|
|
|
pos.value = $stage.pointerPos;
|
|
|
onCleanup(
|
|
|
- listener(mount, "pointermove", () => {
|
|
|
+ listener(mount, "pointermove", (ev) => {
|
|
|
pos.value = $stage.pointerPos;
|
|
|
+ if (pos.value && !replayIng) {
|
|
|
+ lastClient = {
|
|
|
+ clientX: ev.clientX,
|
|
|
+ clientY: ev.clientY,
|
|
|
+ };
|
|
|
+ }
|
|
|
})
|
|
|
);
|
|
|
});
|
|
|
+
|
|
|
+ pos.replay = replay;
|
|
|
return pos;
|
|
|
}, Symbol("pointerPos"));
|
|
|
|
|
|
-
|
|
|
export const useDownKeys = installGlobalVar(() => {
|
|
|
const keys = reactive(new Set<string>());
|
|
|
const cleanup = mergeFuns(
|
|
@@ -244,8 +283,7 @@ export const useTransformIngShapes = installGlobalVar(
|
|
|
Symbol("transformIngShapes")
|
|
|
);
|
|
|
|
|
|
-
|
|
|
export const useCursor = installGlobalVar(
|
|
|
- () => stackVar('default'),
|
|
|
- Symbol('cursor')
|
|
|
-)
|
|
|
+ () => stackVar("default"),
|
|
|
+ Symbol("cursor")
|
|
|
+);
|