-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathDringPlugin.ts
130 lines (117 loc) · 3.42 KB
/
DringPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* @Author: 秦少卫
* @Date: 2023-05-19 08:31:34
* @LastEditors: 秦少卫
* @LastEditTime: 2024-04-10 17:33:11
* @Description: 拖拽插件
*/
import { IEditor, IPluginTempl } from '@kuaitu/core';
type IPlugin = Pick<DringPlugin, 'startDring' | 'endDring'>;
declare module '@kuaitu/core' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IEditor extends IPlugin {}
}
export class DringPlugin implements IPluginTempl {
defautOption = {};
static pluginName = 'DringPlugin';
static events = ['startDring', 'endDring'];
static apis = ['startDring', 'endDring'];
hotkeys: string[] = ['space'];
dragMode = false;
constructor(public canvas: fabric.Canvas, public editor: IEditor) {
this.dragMode = false;
this.init();
}
init() {
this._initDring();
}
startDring() {
this.dragMode = true;
this.canvas.setCursor('grab');
this.editor.emit('startDring');
this.canvas.renderAll();
}
endDring() {
this.dragMode = false;
this.canvas.setCursor('default');
this.canvas.isDragging = false;
this.editor.emit('endDring');
this.canvas.renderAll();
}
// 拖拽模式;
_initDring() {
const This = this;
this.canvas.on('mouse:down', function (this: ExtCanvas, opt) {
const evt = opt.e;
// evt.button === 1 为鼠标中键的判断
if (evt.altKey || This.dragMode || evt.button === 1) {
This.canvas.setCursor('grabbing');
This.canvas.discardActiveObject();
This._setDring();
this.selection = false;
this.isDragging = true;
this.lastPosX = evt.clientX;
this.lastPosY = evt.clientY;
this.requestRenderAll();
}
});
this.canvas.on('mouse:move', function (this: ExtCanvas, opt) {
This.dragMode && This.canvas.setCursor('grab');
if (this.isDragging) {
This.canvas.discardActiveObject();
This.canvas.setCursor('grabbing');
const { e } = opt;
if (!this.viewportTransform) return;
const vpt = this.viewportTransform;
vpt[4] += e.clientX - this.lastPosX;
vpt[5] += e.clientY - this.lastPosY;
this.lastPosX = e.clientX;
this.lastPosY = e.clientY;
this.requestRenderAll();
}
});
this.canvas.on('mouse:up', function (this: ExtCanvas) {
if (!this.viewportTransform) return;
this.setViewportTransform(this.viewportTransform);
this.isDragging = false;
this.selection = true;
this.getObjects().forEach((obj) => {
if (obj.id !== 'workspace' && obj.hasControls) {
obj.selectable = true;
}
});
This.dragMode && This.canvas.setCursor('grab');
this.requestRenderAll();
});
}
_setDring() {
this.canvas.selection = false;
this.canvas.getObjects().forEach((obj) => {
obj.selectable = false;
});
this.canvas.requestRenderAll();
}
destroy() {
console.log('pluginDestroy');
}
// 快捷键扩展回调
// eslint-disable-next-line @typescript-eslint/no-explicit-any
hotkeyEvent(eventName: string, e: KeyboardEvent) {
if (e.code === 'Space' && e.type === 'keydown') {
if (!this.dragMode) {
this.startDring();
}
}
if (e.code === 'Space' && e.type === 'keyup') {
this.endDring();
}
}
}
declare global {
export type ExtCanvas = fabric.Canvas & {
isDragging: boolean;
lastPosX: number;
lastPosY: number;
};
}
export default DringPlugin;