forked from ikuaitu/vue-fabric-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayerPlugin.ts
113 lines (100 loc) · 2.96 KB
/
LayerPlugin.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
/*
* @Author: 秦少卫
* @Date: 2023-06-15 23:23:18
* @LastEditors: 秦少卫
* @LastEditTime: 2024-07-06 23:53:42
* @Description: 图层调整插件
*/
import { fabric } from 'fabric';
import type { IEditor, IPluginTempl } from '@kuaitu/core';
type IPlugin = Pick<LayerPlugin, 'up' | 'down' | 'toFront' | 'toBack'>;
declare module '@kuaitu/core' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IEditor extends IPlugin {}
}
class LayerPlugin implements IPluginTempl {
static pluginName = 'LayerPlugin';
static apis = ['up', 'down', 'toFront', 'toBack'];
constructor(public canvas: fabric.Canvas, public editor: IEditor) {}
_getWorkspace() {
return this.canvas.getObjects().find((item) => item.id === 'workspace');
}
_workspaceSendToBack() {
const workspace = this._getWorkspace();
workspace && workspace.sendToBack();
}
up() {
const actives = this.canvas.getActiveObjects();
if (actives && actives.length === 1) {
const activeObject = this.canvas.getActiveObjects()[0];
activeObject && activeObject.bringForward();
this.canvas.renderAll();
this._workspaceSendToBack();
}
}
down() {
const actives = this.canvas.getActiveObjects();
if (actives && actives.length === 1) {
const activeObject = this.canvas.getActiveObjects()[0];
activeObject && activeObject.sendBackwards();
this.canvas.renderAll();
this._workspaceSendToBack();
}
}
toFront() {
const actives = this.canvas.getActiveObjects();
if (actives && actives.length === 1) {
const activeObject = this.canvas.getActiveObjects()[0];
activeObject && activeObject.bringToFront();
this.canvas.renderAll();
this._workspaceSendToBack();
}
}
toBack() {
const actives = this.canvas.getActiveObjects();
if (actives && actives.length === 1) {
const activeObject = this.canvas.getActiveObjects()[0];
activeObject && activeObject.sendToBack();
this.canvas.renderAll();
this._workspaceSendToBack();
}
}
contextMenu() {
const activeObject = this.canvas.getActiveObject();
if (activeObject) {
return [
{
text: '图层管理',
hotkey: '❯',
subitems: [
{
text: '上一个',
hotkey: '',
onclick: () => this.up(),
},
{
text: '下一个',
hotkey: '',
onclick: () => this.down(),
},
{
text: '置顶',
hotkey: '',
onclick: () => this.toFront(),
},
{
text: '置底',
hotkey: '',
onclick: () => this.toBack(),
},
],
},
];
// return [{ text: '复制', hotkey: 'Ctrl+V', disabled: false, onclick: () => this.clone() }];
}
}
destroy() {
console.log('pluginDestroy');
}
}
export default LayerPlugin;