Skip to content

Commit

Permalink
refactor: 重构选择事件监听hook (#365)
Browse files Browse the repository at this point in the history
  • Loading branch information
wuchenguang1998 authored May 7, 2024
1 parent 4d1d18b commit 0aa242e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 47 deletions.
52 changes: 5 additions & 47 deletions src/hooks/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
* @version:
* @Author: June
* @Date: 2023-04-23 21:10:05
* @LastEditors: 秦少卫
* @LastEditTime: 2024-04-10 14:42:34
* @LastEditors: wuchenguang1998
* @LastEditTime: 2024-05-04 15:37:06
*/
import Editor, { EventType } from '@kuaitu/core';
const { SelectEvent, SelectMode } = EventType;
import Editor from '@kuaitu/core';
interface Selector {
mSelectMode: SelectMode;
mSelectOneType: string | undefined;
Expand All @@ -17,54 +16,13 @@ interface Selector {
}

export default function useSelect() {
const state = reactive<Selector>({
mSelectMode: SelectMode.EMPTY,
mSelectOneType: '',
mSelectId: '', // 选择id
mSelectIds: [], // 选择id
mSelectActive: [],
});

const fabric = inject('fabric');
const canvasEditor = inject('canvasEditor') as Editor;

const selectOne = (e: [fabric.Object]) => {
state.mSelectMode = SelectMode.ONE;
if (e[0]) {
state.mSelectId = e[0].id;
state.mSelectOneType = e[0].type;
state.mSelectIds = e.map((item) => item.id);
}
};

const selectMulti = (e: fabric.Object[]) => {
state.mSelectMode = SelectMode.MULTI;
state.mSelectId = '';
state.mSelectIds = e.map((item) => item.id);
};

const selectCancel = () => {
state.mSelectId = '';
state.mSelectIds = [];
state.mSelectMode = SelectMode.EMPTY;
state.mSelectOneType = '';
};

onMounted(() => {
canvasEditor.on(SelectEvent.ONE, selectOne);
canvasEditor.on(SelectEvent.MULTI, selectMulti);
canvasEditor.on(SelectEvent.CANCEL, selectCancel);
});

onBeforeMount(() => {
canvasEditor.off(SelectEvent.ONE, selectOne);
canvasEditor.off(SelectEvent.MULTI, selectMulti);
canvasEditor.off(SelectEvent.CANCEL, selectCancel);
});
const mixinState = inject('mixinState') as Selector;

return {
fabric,
canvasEditor,
mixinState: state,
mixinState,
};
}
65 changes: 65 additions & 0 deletions src/hooks/useSelectListen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* @Descripttion: useSelectListen
* @version:
* @Author: wuchenguang1998
* @Date: 2024-05-04 14:36:49
* @LastEditors: wuchenguang1998
* @LastEditTime: 2024-05-04 15:27:11
*/
import Editor, { EventType } from '@kuaitu/core';
const { SelectEvent, SelectMode } = EventType;
interface Selector {
mSelectMode: SelectMode;
mSelectOneType: string | undefined;
mSelectId: string | undefined;
mSelectIds: (string | undefined)[];
mSelectActive: unknown[];
}

export default function useSelectListen(canvasEditor: Editor) {
const state = reactive<Selector>({
mSelectMode: SelectMode.EMPTY,
mSelectOneType: '',
mSelectId: '', // 选择id
mSelectIds: [], // 选择id
mSelectActive: [],
});

const selectOne = (e: [fabric.Object]) => {
state.mSelectMode = SelectMode.ONE;
if (e[0]) {
state.mSelectId = e[0].id;
state.mSelectOneType = e[0].type;
state.mSelectIds = e.map((item) => item.id);
}
};

const selectMulti = (e: fabric.Object[]) => {
state.mSelectMode = SelectMode.MULTI;
state.mSelectId = '';
state.mSelectIds = e.map((item) => item.id);
};

const selectCancel = () => {
state.mSelectId = '';
state.mSelectIds = [];
state.mSelectMode = SelectMode.EMPTY;
state.mSelectOneType = '';
};

onMounted(() => {
canvasEditor.on(SelectEvent.ONE, selectOne);
canvasEditor.on(SelectEvent.MULTI, selectMulti);
canvasEditor.on(SelectEvent.CANCEL, selectCancel);
});

onBeforeMount(() => {
canvasEditor.off(SelectEvent.ONE, selectOne);
canvasEditor.off(SelectEvent.MULTI, selectMulti);
canvasEditor.off(SelectEvent.CANCEL, selectCancel);
});

return {
mixinState: state,
};
}
6 changes: 6 additions & 0 deletions src/views/home/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ import attribute from '@/components/attribute.vue';
// 功能组件
import { fabric } from 'fabric';
// hooks
import useSelectListen from '@/hooks/useSelectListen';
const APIHOST = import.meta.env.APP_APIHOST;
import Editor, {
Expand Down Expand Up @@ -282,8 +285,11 @@ const switchAttrBar = () => {
state.attrBarShow = !state.attrBarShow;
};
const { mixinState } = useSelectListen(canvasEditor);
provide('fabric', fabric);
provide('canvasEditor', canvasEditor);
provide('mixinState', mixinState);
</script>
<style lang="less" scoped>
.logo {
Expand Down

0 comments on commit 0aa242e

Please sign in to comment.