Skip to content

Commit 7d2e6d9

Browse files
committed
refactor(lock): 增加lock插件
1 parent 4270c23 commit 7d2e6d9

File tree

5 files changed

+99
-44
lines changed

5 files changed

+99
-44
lines changed

packages/core/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* @Author: 秦少卫
33
* @Date: 2023-02-03 23:29:34
4-
* @LastEditors: June [email protected]
5-
* @LastEditTime: 2024-06-19 11:04:37
4+
* @LastEditors: 秦少卫
5+
* @LastEditTime: 2024-07-06 12:10:52
66
* @Description: 核心入口文件
77
*/
88
import Editor from './Editor';
@@ -36,6 +36,7 @@ export { default as BarCodePlugin } from './plugin/BarCodePlugin';
3636
export { default as QrCodePlugin } from './plugin/QrCodePlugin';
3737
export { default as ImageStroke } from './plugin/ImageStroke';
3838
export { default as ResizePlugin } from './plugin/ResizePlugin';
39+
export { default as LockPlugin } from './plugin/LockPlugin';
3940
import EventType from './eventType';
4041
import Utils from './utils/utils';
4142

packages/core/plugin/LockPlugin.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* @Author: 秦少卫
3+
* @Date: 2024-07-06 12:10:36
4+
* @LastEditors: 秦少卫
5+
* @LastEditTime: 2024-07-06 12:10:38
6+
* @Description: file content
7+
*/
8+
/*
9+
* @Author: 秦少卫
10+
* @Date: 2024-07-04 14:27:05
11+
* @LastEditors: 秦少卫
12+
* @LastEditTime: 2024-07-04 23:06:33
13+
* @Description: file content
14+
*/
15+
import { fabric } from 'fabric';
16+
import type Editor from '../Editor';
17+
import { SelectMode } from '../eventType';
18+
19+
export default class LockPlugin implements IPluginTempl {
20+
static pluginName = 'LockPlugin';
21+
static apis = ['lock', 'unLock'];
22+
lockAttrs: string[];
23+
constructor(public canvas: fabric.Canvas, public editor: Editor) {
24+
this.lockAttrs = [
25+
'lockMovementX',
26+
'lockMovementY',
27+
'lockRotation',
28+
'lockScalingX',
29+
'lockScalingY',
30+
];
31+
}
32+
33+
hookImportAfter() {
34+
this.canvas.forEachObject((obj) => {
35+
if (obj.hasControls === false && obj.selectable === false) {
36+
this.canvas.setActiveObject(obj);
37+
this.lock();
38+
}
39+
});
40+
return Promise.resolve();
41+
}
42+
43+
lock() {
44+
const activeObject = this.canvas.getActiveObject();
45+
if (activeObject) {
46+
activeObject.hasControls = false;
47+
activeObject.selectable = false;
48+
activeObject.evented = false;
49+
// 修改默认属性
50+
this.lockAttrs.forEach((key) => {
51+
activeObject[key] = true;
52+
});
53+
this.canvas.discardActiveObject().renderAll();
54+
}
55+
}
56+
57+
unLock() {
58+
const activeObject = this.canvas.getActiveObject();
59+
if (activeObject) {
60+
activeObject.hasControls = true;
61+
activeObject.selectable = true;
62+
activeObject.evented = true;
63+
// 修改默认属性
64+
this.lockAttrs.forEach((key) => {
65+
activeObject[key] = false;
66+
});
67+
this.canvas.discardActiveObject().renderAll();
68+
}
69+
}
70+
71+
contextMenu() {
72+
const selectedMode = this.editor.getSelectMode();
73+
const activeObject = this.canvas.getActiveObject();
74+
if (selectedMode === SelectMode.ONE && activeObject) {
75+
if (activeObject.selectable) {
76+
return [{ text: '锁定', hotkey: '', onclick: () => this.lock() }];
77+
} else {
78+
return [{ text: '解锁', hotkey: '', onclick: () => this.unLock() }];
79+
}
80+
}
81+
}
82+
83+
destroy() {
84+
console.log('pluginDestroy');
85+
}
86+
}

src/components/lock.vue

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: 秦少卫
33
* @Date: 2022-09-03 19:16:55
44
* @LastEditors: 秦少卫
5-
* @LastEditTime: 2024-05-21 09:21:51
5+
* @LastEditTime: 2024-07-06 12:20:00
66
* @Description: 锁定元素
77
-->
88

@@ -18,48 +18,13 @@ import useSelect from '@/hooks/select';
1818
import { onBeforeUnmount, onMounted } from 'vue';
1919
2020
const { mixinState, canvasEditor } = useSelect();
21-
const lockAttrs = [
22-
'lockMovementX',
23-
'lockMovementY',
24-
'lockRotation',
25-
'lockScalingX',
26-
'lockScalingY',
27-
];
2821
const isLock = ref(false);
29-
const lock = () => {
30-
// 修改自定义属性
31-
mixinState.mSelectActive.hasControls = false;
32-
// 修改默认属性
33-
lockAttrs.forEach((key) => {
34-
mixinState.mSelectActive[key] = true;
35-
});
36-
37-
mixinState.mSelectActive.selectable = false;
38-
39-
isLock.value = true;
40-
canvasEditor.canvas.renderAll();
41-
};
42-
const unLock = () => {
43-
// 修改自定义属性
44-
mixinState.mSelectActive.hasControls = true;
45-
// 修改默认属性
46-
lockAttrs.forEach((key) => {
47-
mixinState.mSelectActive[key] = false;
48-
});
49-
mixinState.mSelectActive.selectable = true;
50-
51-
isLock.value = false;
52-
canvasEditor.canvas.renderAll();
53-
};
54-
5522
const doLock = (isLock) => {
56-
isLock ? lock() : unLock();
23+
isLock ? canvasEditor.lock() : canvasEditor.unLock();
5724
};
5825
5926
const handleSelected = (items) => {
60-
isLock.value = !items[0].hasControls;
61-
// eslint-disable-next-line prefer-destructuring
62-
mixinState.mSelectActive = items[0];
27+
isLock.value = !items[0].selectable;
6328
};
6429
6530
onMounted(() => {

src/hooks/useSelectListen.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* @version:
44
* @Author: wuchenguang1998
55
* @Date: 2024-05-04 14:36:49
6-
* @LastEditors: wuchenguang1998
7-
* @LastEditTime: 2024-05-04 15:27:11
6+
* @LastEditors: 秦少卫
7+
* @LastEditTime: 2024-07-06 12:15:19
88
*/
99
import Editor, { EventType } from '@kuaitu/core';
1010
import { get } from 'lodash-es';
@@ -30,6 +30,7 @@ export default function useSelectListen(canvasEditor: Editor) {
3030

3131
const selectOne = (e: [fabric.Object]) => {
3232
state.mSelectMode = SelectMode.ONE;
33+
state.mSelectActive = e[0];
3334
if (e[0] && get(e[0], 'clip')) {
3435
selectCancel();
3536
// state.mSelectId = get(e[0], 'targetId');

src/views/home/index.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!--
22
* @Author: 秦少卫
33
* @Date: 2024-05-17 15:30:21
4-
* @LastEditors: June [email protected]
5-
* @LastEditTime: 2024-06-19 11:05:24
4+
* @LastEditors: 秦少卫
5+
* @LastEditTime: 2024-07-06 12:11:08
66
* @Description: file content
77
-->
88
<template>
@@ -272,6 +272,7 @@ import Editor, {
272272
QrCodePlugin,
273273
ImageStroke,
274274
ResizePlugin,
275+
LockPlugin,
275276
} from '@kuaitu/core';
276277
import Edit from '@/components/edit.vue';
277278
import ClipImage from '@/components/clipImage.vue';
@@ -387,6 +388,7 @@ onMounted(() => {
387388
canvasEditor.use(PsdPlugin);
388389
canvasEditor.use(ImageStroke);
389390
canvasEditor.use(ResizePlugin);
391+
canvasEditor.use(LockPlugin);
390392
391393
state.show = true;
392394
// 默认打开标尺

0 commit comments

Comments
 (0)