forked from ikuaitu/vue-fabric-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGroupTextEditorPlugin.ts
189 lines (168 loc) · 5.54 KB
/
GroupTextEditorPlugin.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* @Author: 秦少卫
* @Date: 2023-06-22 16:11:40
* @LastEditors: 秦少卫
* @LastEditTime: 2024-07-25 16:49:18
* @Description: 组内文字编辑
*/
import { fabric } from 'fabric';
import { isGroup } from '../utils/utils';
import { v4 as uuid } from 'uuid';
import { pick } from 'lodash-es';
import type { IEditor, IPluginTempl } from '@kuaitu/core';
class GroupTextEditorPlugin implements IPluginTempl {
static pluginName = 'GroupTextEditorPlugin';
isDown = false;
constructor(public canvas: fabric.Canvas, public editor: IEditor) {
this._init();
}
// 组内文本输入
_init() {
this.canvas.on('mouse:down', (opt) => {
this.isDown = true;
// 重置选中controls
if (
opt.target &&
!opt.target.lockMovementX &&
!opt.target.lockMovementY &&
!opt.target.lockRotation &&
!opt.target.lockScalingX &&
!opt.target.lockScalingY
) {
opt.target.hasControls = true;
}
});
this.canvas.on('mouse:up', () => {
this.isDown = false;
});
this.canvas.on('mouse:dblclick', (opt) => {
if (isGroup(opt.target)) {
const selectedObject = this._getGroupObj(opt) as fabric.IText;
if (!selectedObject) return;
selectedObject.selectable = true;
// 由于组内的元素,双击以后会导致controls偏移,因此隐藏他
if (selectedObject.hasControls) {
selectedObject.hasControls = false;
}
if (this.isText(selectedObject)) {
this._bedingTextEditingEvent(selectedObject, opt.target);
return;
}
this.canvas.setActiveObject(selectedObject);
this.canvas.renderAll();
}
});
}
// 获取点击区域内的组内文字元素
_getGroupTextObj(opt: fabric.IEvent<MouseEvent>) {
const pointer = this.canvas.getPointer(opt.e, true);
if (!isGroup(opt.target)) return false;
const clickObj = this.canvas._searchPossibleTargets(opt.target._objects, pointer);
if (clickObj && this.isText(clickObj)) {
return clickObj;
}
return false;
}
_getGroupObj(opt: fabric.IEvent<MouseEvent>) {
const pointer = this.canvas.getPointer(opt.e, true);
if (!isGroup(opt.target)) return false;
const clickObj = this.canvas._searchPossibleTargets(opt.target._objects, pointer);
return clickObj;
}
// 通过组合重新组装来编辑文字,可能会耗性能。
_bedingTextEditingEvent(textObject: fabric.IText, groupObj: fabric.Group) {
const textObjectJSON = textObject.toObject();
const groupMatrix: number[] = groupObj.calcTransformMatrix();
const a: number = groupMatrix[0];
const b: number = groupMatrix[1];
const c: number = groupMatrix[2];
const d: number = groupMatrix[3];
const e: number = groupMatrix[4];
const f: number = groupMatrix[5];
const newX = a * (textObject.left ?? 0) + c * (textObject.top ?? 0) + e;
const newY = b * (textObject.left ?? 0) + d * (textObject.top ?? 0) + f;
const tempText = new (textObject.constructor as typeof fabric.IText)(textObject.text ?? '', {
...textObjectJSON,
scaleX: textObjectJSON.scaleX * a,
scaleY: textObjectJSON.scaleY * a,
textAlign: textObject.textAlign,
left: newX,
top: newY,
styles: textObject.styles,
groupCopyed: textObject.group,
});
tempText.id = uuid();
textObject.visible = false;
groupObj.addWithUpdate();
tempText.visible = true;
tempText.selectable = true;
tempText.hasControls = false;
tempText.editable = true;
this.canvas.add(tempText);
this.canvas.setActiveObject(tempText);
tempText.enterEditing();
tempText.selectAll();
tempText.on('editing:exited', () => {
const attrs = tempText.toObject();
// 进入编辑模式时触发
textObject.set({
...pick(attrs, [
'fill',
'fontSize',
'fontStyle',
'fontFamily',
'lineHeight',
'backgroundColor',
]),
text: tempText.text,
visible: true,
});
groupObj.addWithUpdate();
tempText.visible = false;
this.canvas.remove(tempText);
this.canvas.setActiveObject(groupObj);
});
}
// 绑定编辑取消事件
_bedingEditingEvent(textObject: fabric.IText, opt: fabric.IEvent<MouseEvent>) {
if (!opt.target) return;
const left = opt.target.left;
const top = opt.target.top;
const ids = this._unGroup() || [];
const resetGroup = () => {
const groupArr = this.canvas.getObjects().filter((item) => item.id && ids.includes(item.id));
// 删除元素
groupArr.forEach((item) => this.canvas.remove(item));
// 生成新组
const group = new fabric.Group([...groupArr]);
group.set('left', left);
group.set('top', top);
group.set('id', uuid());
textObject.off('editing:exited', resetGroup);
this.canvas.add(group);
this.canvas.discardActiveObject().renderAll();
};
// 绑定取消事件
textObject.on('editing:exited', resetGroup);
}
// 拆分组合并返回ID
_unGroup() {
const ids: string[] = [];
const activeObj = this.canvas.getActiveObject() as fabric.Group;
if (!activeObj) return;
activeObj.getObjects().forEach((item) => {
const id = uuid();
ids.push(id);
item.set('id', id);
});
activeObj.toActiveSelection();
return ids;
}
isText(obj: fabric.Object) {
return obj.type && ['i-text', 'text', 'textbox'].includes(obj.type);
}
destroy() {
console.log('pluginDestroy');
}
}
export default GroupTextEditorPlugin;