@hprint/core 0.0.1-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ContextMenu.d.ts +25 -0
- package/dist/ContextMenu.d.ts.map +1 -0
- package/dist/Editor.d.ts +35 -0
- package/dist/Editor.d.ts.map +1 -0
- package/dist/Instance.d.ts +77 -0
- package/dist/Instance.d.ts.map +1 -0
- package/dist/ServersPlugin.d.ts +71 -0
- package/dist/ServersPlugin.d.ts.map +1 -0
- package/dist/index.css +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +569 -0
- package/dist/index.mjs +22130 -0
- package/dist/interface/Editor.d.ts +35 -0
- package/dist/interface/Editor.d.ts.map +1 -0
- package/dist/objects/CustomRect.d.ts +3 -0
- package/dist/objects/CustomRect.d.ts.map +1 -0
- package/dist/objects/CustomTextbox.d.ts +3 -0
- package/dist/objects/CustomTextbox.d.ts.map +1 -0
- package/dist/plugin.d.ts +30 -0
- package/dist/plugin.d.ts.map +1 -0
- package/dist/utils/fabric-history.d.ts +2 -0
- package/dist/utils/fabric-history.d.ts.map +1 -0
- package/dist/utils/utils.d.ts +61 -0
- package/dist/utils/utils.d.ts.map +1 -0
- package/package.json +46 -0
- package/src/ContextMenu.js +277 -0
- package/src/Editor.ts +215 -0
- package/src/Instance.ts +79 -0
- package/src/ServersPlugin.ts +387 -0
- package/src/index.ts +11 -0
- package/src/interface/Editor.ts +56 -0
- package/src/objects/CustomRect.js +21 -0
- package/src/objects/CustomTextbox.js +165 -0
- package/src/plugin.ts +88 -0
- package/src/styles/contextMenu.css +60 -0
- package/src/utils/fabric-history.js +232 -0
- package/src/utils/utils.ts +165 -0
- package/tsconfig.json +10 -0
- package/vite.config.ts +29 -0
package/src/Editor.ts
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
import hotkeys from 'hotkeys-js';
|
|
3
|
+
import ContextMenu from './ContextMenu.js';
|
|
4
|
+
import ServersPlugin from './ServersPlugin';
|
|
5
|
+
import { AsyncSeriesHook } from 'tapable';
|
|
6
|
+
import { fabric } from '@hprint/shared';
|
|
7
|
+
import type {
|
|
8
|
+
IPluginMenu,
|
|
9
|
+
IPluginClass,
|
|
10
|
+
IPluginOption,
|
|
11
|
+
IEditorHooksType,
|
|
12
|
+
IPluginTempl,
|
|
13
|
+
} from '@hprint/core';
|
|
14
|
+
|
|
15
|
+
import Utils from './utils/utils';
|
|
16
|
+
import { LengthConvert } from '@hprint/shared';
|
|
17
|
+
|
|
18
|
+
class Editor extends EventEmitter {
|
|
19
|
+
private canvas: fabric.Canvas | null = null;
|
|
20
|
+
contextMenu: ContextMenu | null = null;
|
|
21
|
+
[key: string]: any;
|
|
22
|
+
private pluginMap: {
|
|
23
|
+
[propName: string]: IPluginTempl;
|
|
24
|
+
} = {};
|
|
25
|
+
// 自定义事件
|
|
26
|
+
private customEvents: string[] = [];
|
|
27
|
+
// 自定义API
|
|
28
|
+
private customApis: string[] = [];
|
|
29
|
+
// 生命周期函数名
|
|
30
|
+
private hooks: IEditorHooksType[] = [
|
|
31
|
+
'hookImportBefore',
|
|
32
|
+
'hookImportAfter',
|
|
33
|
+
'hookSaveBefore',
|
|
34
|
+
'hookSaveAfter',
|
|
35
|
+
'hookTransform',
|
|
36
|
+
];
|
|
37
|
+
public hooksEntity: {
|
|
38
|
+
[propName: string]: AsyncSeriesHook<any, any>;
|
|
39
|
+
} = {};
|
|
40
|
+
|
|
41
|
+
init(canvas: fabric.Canvas) {
|
|
42
|
+
this.canvas = canvas;
|
|
43
|
+
this._initContextMenu();
|
|
44
|
+
this._bindContextMenu();
|
|
45
|
+
this._initActionHooks();
|
|
46
|
+
this._initServersPlugin();
|
|
47
|
+
|
|
48
|
+
this.Utils = Utils;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
get fabricCanvas() {
|
|
53
|
+
return this.canvas;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 引入组件
|
|
57
|
+
use(plugin: IPluginTempl, options?: IPluginOption) {
|
|
58
|
+
if (this._checkPlugin(plugin) && this.canvas) {
|
|
59
|
+
this._saveCustomAttr(plugin);
|
|
60
|
+
const pluginRunTime = new (plugin as IPluginClass)(
|
|
61
|
+
this.canvas,
|
|
62
|
+
this,
|
|
63
|
+
options || {}
|
|
64
|
+
);
|
|
65
|
+
// 添加插件名称
|
|
66
|
+
pluginRunTime.pluginName = plugin.pluginName;
|
|
67
|
+
this.pluginMap[plugin.pluginName] = pluginRunTime;
|
|
68
|
+
this._bindingHooks(pluginRunTime);
|
|
69
|
+
this._bindingHotkeys(pluginRunTime);
|
|
70
|
+
this._bindingApis(pluginRunTime);
|
|
71
|
+
}
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
destory() {
|
|
76
|
+
this.canvas = null;
|
|
77
|
+
this.contextMenu = null;
|
|
78
|
+
this.pluginMap = {};
|
|
79
|
+
this.customEvents = [];
|
|
80
|
+
this.customApis = [];
|
|
81
|
+
this.hooksEntity = {};
|
|
82
|
+
}
|
|
83
|
+
// 获取插件
|
|
84
|
+
getPlugin(name: string) {
|
|
85
|
+
if (this.pluginMap[name]) {
|
|
86
|
+
return this.pluginMap[name];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 检查组件
|
|
91
|
+
private _checkPlugin(plugin: IPluginTempl) {
|
|
92
|
+
const { pluginName, events = [], apis = [] } = plugin;
|
|
93
|
+
//名称检查
|
|
94
|
+
if (this.pluginMap[pluginName]) {
|
|
95
|
+
throw new Error(pluginName + '插件重复初始化');
|
|
96
|
+
}
|
|
97
|
+
events.forEach((eventName: string) => {
|
|
98
|
+
if (this.customEvents.find((info) => info === eventName)) {
|
|
99
|
+
throw new Error(pluginName + '插件中' + eventName + '重复');
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
apis.forEach((apiName: string) => {
|
|
104
|
+
if (this.customApis.find((info) => info === apiName)) {
|
|
105
|
+
throw new Error(pluginName + '插件中' + apiName + '重复');
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 绑定hooks方法
|
|
112
|
+
private _bindingHooks(plugin: IPluginTempl) {
|
|
113
|
+
this.hooks.forEach((hookName) => {
|
|
114
|
+
const hook = plugin[hookName];
|
|
115
|
+
if (hook) {
|
|
116
|
+
this.hooksEntity[hookName].tapPromise(
|
|
117
|
+
plugin.pluginName + hookName,
|
|
118
|
+
function () {
|
|
119
|
+
// console.log(hookName, ...arguments);
|
|
120
|
+
// eslint-disable-next-line prefer-rest-params
|
|
121
|
+
const result = hook.apply(plugin, [...arguments]);
|
|
122
|
+
// hook 兼容非 Promise 返回值
|
|
123
|
+
const promise: Promise<any> =
|
|
124
|
+
(result as any) instanceof Promise
|
|
125
|
+
? (result as Promise<any>)
|
|
126
|
+
: Promise.resolve(result as any);
|
|
127
|
+
return promise as Promise<void>;
|
|
128
|
+
}
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// 绑定快捷键
|
|
135
|
+
private _bindingHotkeys(plugin: IPluginTempl) {
|
|
136
|
+
plugin?.hotkeys?.forEach((keyName: string) => {
|
|
137
|
+
// 支持 keyup
|
|
138
|
+
hotkeys(keyName, { keyup: true }, (e) => {
|
|
139
|
+
plugin.hotkeyEvent && plugin.hotkeyEvent(keyName, e);
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 保存组件自定义事件与API
|
|
145
|
+
private _saveCustomAttr(plugin: IPluginTempl) {
|
|
146
|
+
const { events = [], apis = [] } = plugin;
|
|
147
|
+
this.customApis = this.customApis.concat(apis);
|
|
148
|
+
this.customEvents = this.customEvents.concat(events);
|
|
149
|
+
}
|
|
150
|
+
// 代理API事件
|
|
151
|
+
private _bindingApis(pluginRunTime: IPluginTempl) {
|
|
152
|
+
const { apis = [] } = (pluginRunTime.constructor as any) || {};
|
|
153
|
+
apis.forEach((apiName: string) => {
|
|
154
|
+
this[apiName] = function () {
|
|
155
|
+
// eslint-disable-next-line prefer-rest-params
|
|
156
|
+
return pluginRunTime[apiName].apply(pluginRunTime, [
|
|
157
|
+
...arguments,
|
|
158
|
+
]);
|
|
159
|
+
};
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// 右键菜单
|
|
164
|
+
private _bindContextMenu() {
|
|
165
|
+
this.canvas &&
|
|
166
|
+
this.canvas.on('mouse:down', (opt) => {
|
|
167
|
+
if (opt.button === 3) {
|
|
168
|
+
let menu: IPluginMenu[] = [];
|
|
169
|
+
Object.keys(this.pluginMap).forEach((pluginName) => {
|
|
170
|
+
const pluginRunTime = this.pluginMap[pluginName];
|
|
171
|
+
const pluginMenu =
|
|
172
|
+
pluginRunTime.contextMenu &&
|
|
173
|
+
pluginRunTime.contextMenu();
|
|
174
|
+
if (pluginMenu) {
|
|
175
|
+
menu = menu.concat(pluginMenu);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
this._renderMenu(opt, menu);
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// 渲染右键菜单
|
|
184
|
+
private _renderMenu(opt: { e: MouseEvent }, menu: IPluginMenu[]) {
|
|
185
|
+
if (menu.length !== 0 && this.contextMenu) {
|
|
186
|
+
this.contextMenu.hideAll();
|
|
187
|
+
this.contextMenu.setData(menu);
|
|
188
|
+
this.contextMenu.show(opt.e.clientX, opt.e.clientY);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// 生命周期事件
|
|
193
|
+
_initActionHooks() {
|
|
194
|
+
this.hooks.forEach((hookName) => {
|
|
195
|
+
this.hooksEntity[hookName] = new AsyncSeriesHook(['data']);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
_initContextMenu() {
|
|
200
|
+
this.contextMenu = new ContextMenu(this.canvas!.wrapperEl, []);
|
|
201
|
+
this.contextMenu.install();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
_initServersPlugin() {
|
|
205
|
+
this.use(ServersPlugin);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// 解决 listener 为 undefined 的时候卸载错误
|
|
209
|
+
off(eventName: string, listener: any): this {
|
|
210
|
+
// noinspection TypeScriptValidateTypes
|
|
211
|
+
return listener ? super.off(eventName, listener) : this;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export default Editor;
|
package/src/Instance.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import Editor from './Editor';
|
|
2
|
+
import UnitPlugin from '../../plugins/src/plugins/UnitPlugin';
|
|
3
|
+
import DringPlugin from '../../plugins/src/plugins/DringPlugin';
|
|
4
|
+
import AlignGuidLinePlugin from '../../plugins/src/plugins/AlignGuidLinePlugin';
|
|
5
|
+
import ControlsPlugin from '../../plugins/src/plugins/ControlsPlugin';
|
|
6
|
+
import ControlsRotatePlugin from '../../plugins/src/plugins/ControlsRotatePlugin';
|
|
7
|
+
import CenterAlignPlugin from '../../plugins/src/plugins/CenterAlignPlugin';
|
|
8
|
+
import LayerPlugin from '../../plugins/src/plugins/LayerPlugin';
|
|
9
|
+
import CopyPlugin from '../../plugins/src/plugins/CopyPlugin';
|
|
10
|
+
import MoveHotKeyPlugin from '../../plugins/src/plugins/MoveHotKeyPlugin';
|
|
11
|
+
import DeleteHotKeyPlugin from '../../plugins/src/plugins/DeleteHotKeyPlugin';
|
|
12
|
+
import GroupPlugin from '../../plugins/src/plugins/GroupPlugin';
|
|
13
|
+
import DrawLinePlugin from '../../plugins/src/plugins/DrawLinePlugin';
|
|
14
|
+
import GroupTextEditorPlugin from '../../plugins/src/plugins/GroupTextEditorPlugin';
|
|
15
|
+
import GroupAlignPlugin from '../../plugins/src/plugins/GroupAlignPlugin';
|
|
16
|
+
import WorkspacePlugin from '../../plugins/src/plugins/WorkspacePlugin';
|
|
17
|
+
import MaskPlugin from '../../plugins/src/plugins/MaskPlugin';
|
|
18
|
+
import HistoryPlugin from '../../plugins/src/plugins/HistoryPlugin';
|
|
19
|
+
import FlipPlugin from '../../plugins/src/plugins/FlipPlugin';
|
|
20
|
+
import RulerPlugin from '../../plugins/src/plugins/RulerPlugin';
|
|
21
|
+
import MaterialPlugin from '../../plugins/src/plugins/MaterialPlugin';
|
|
22
|
+
import WaterMarkPlugin from '../../plugins/src/plugins/WaterMarkPlugin';
|
|
23
|
+
import FontPlugin from '../../plugins/src/plugins/FontPlugin';
|
|
24
|
+
import PolygonModifyPlugin from '../../plugins/src/plugins/PolygonModifyPlugin';
|
|
25
|
+
import DrawPolygonPlugin from '../../plugins/src/plugins/DrawPolygonPlugin';
|
|
26
|
+
import FreeDrawPlugin from '../../plugins/src/plugins/FreeDrawPlugin';
|
|
27
|
+
import PathTextPlugin from '../../plugins/src/plugins/PathTextPlugin';
|
|
28
|
+
import PsdPlugin from '../../plugins/src/plugins/PsdPlugin';
|
|
29
|
+
import SimpleClipImagePlugin from '../../plugins/src/plugins/SimpleClipImagePlugin';
|
|
30
|
+
import BarCodePlugin from '../../plugins/src/plugins/BarCodePlugin';
|
|
31
|
+
import QrCodePlugin from '../../plugins/src/plugins/QrCodePlugin';
|
|
32
|
+
import ImageStroke from '../../plugins/src/plugins/ImageStroke';
|
|
33
|
+
import ResizePlugin from '../../plugins/src/plugins/ResizePlugin';
|
|
34
|
+
import LockPlugin from '../../plugins/src/plugins/LockPlugin';
|
|
35
|
+
import AddBaseTypePlugin from '../../plugins/src/plugins/AddBaseTypePlugin';
|
|
36
|
+
import PrintPlugin from '../../plugins/src/plugins/PrintPlugin';
|
|
37
|
+
|
|
38
|
+
const AllEditor = {
|
|
39
|
+
Editor,
|
|
40
|
+
UnitPlugin,
|
|
41
|
+
DringPlugin,
|
|
42
|
+
AlignGuidLinePlugin,
|
|
43
|
+
ControlsPlugin,
|
|
44
|
+
ControlsRotatePlugin,
|
|
45
|
+
CenterAlignPlugin,
|
|
46
|
+
LayerPlugin,
|
|
47
|
+
CopyPlugin,
|
|
48
|
+
MoveHotKeyPlugin,
|
|
49
|
+
DeleteHotKeyPlugin,
|
|
50
|
+
GroupPlugin,
|
|
51
|
+
DrawLinePlugin,
|
|
52
|
+
GroupTextEditorPlugin,
|
|
53
|
+
GroupAlignPlugin,
|
|
54
|
+
WorkspacePlugin,
|
|
55
|
+
MaskPlugin,
|
|
56
|
+
HistoryPlugin,
|
|
57
|
+
FlipPlugin,
|
|
58
|
+
RulerPlugin,
|
|
59
|
+
MaterialPlugin,
|
|
60
|
+
WaterMarkPlugin,
|
|
61
|
+
FontPlugin,
|
|
62
|
+
PolygonModifyPlugin,
|
|
63
|
+
DrawPolygonPlugin,
|
|
64
|
+
FreeDrawPlugin,
|
|
65
|
+
PathTextPlugin,
|
|
66
|
+
PsdPlugin,
|
|
67
|
+
SimpleClipImagePlugin,
|
|
68
|
+
BarCodePlugin,
|
|
69
|
+
QrCodePlugin,
|
|
70
|
+
ImageStroke,
|
|
71
|
+
ResizePlugin,
|
|
72
|
+
LockPlugin,
|
|
73
|
+
AddBaseTypePlugin,
|
|
74
|
+
PrintPlugin,
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
declare type HPrintEditor = typeof AllEditor;
|
|
78
|
+
|
|
79
|
+
export default HPrintEditor;
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
import { v4 as uuid } from 'uuid';
|
|
2
|
+
import { selectFiles, clipboardText, downFile } from './utils/utils';
|
|
3
|
+
import { fabric, StaticCanvas } from 'fabric';
|
|
4
|
+
import type { IEditor, IPluginTempl } from '@hprint/core';
|
|
5
|
+
import { SelectEvent, SelectMode } from '../../plugins/src/types/eventType';
|
|
6
|
+
|
|
7
|
+
type IPlugin = Pick<
|
|
8
|
+
ServersPlugin,
|
|
9
|
+
| 'insert'
|
|
10
|
+
| 'loadJSON'
|
|
11
|
+
| 'getJson'
|
|
12
|
+
| 'dragAddItem'
|
|
13
|
+
| 'clipboard'
|
|
14
|
+
| 'clipboardBase64'
|
|
15
|
+
| 'saveJson'
|
|
16
|
+
| 'saveSvg'
|
|
17
|
+
| 'getBase64'
|
|
18
|
+
| 'saveImg'
|
|
19
|
+
| 'clear'
|
|
20
|
+
| 'preview'
|
|
21
|
+
| 'staticPreview'
|
|
22
|
+
| 'getSelectMode'
|
|
23
|
+
| 'getExtensionKey'
|
|
24
|
+
>;
|
|
25
|
+
|
|
26
|
+
declare module '@hprint/core' {
|
|
27
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
28
|
+
interface IEditor extends IPlugin {}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function transformText(objects: any) {
|
|
32
|
+
if (!objects) return;
|
|
33
|
+
objects.forEach((item: any) => {
|
|
34
|
+
if (item.objects) {
|
|
35
|
+
transformText(item.objects);
|
|
36
|
+
} else {
|
|
37
|
+
item.type === 'text' && (item.type = 'textbox');
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class ServersPlugin implements IPluginTempl {
|
|
43
|
+
public selectedMode: SelectMode;
|
|
44
|
+
static pluginName = 'ServersPlugin';
|
|
45
|
+
static apis = [
|
|
46
|
+
'insert',
|
|
47
|
+
'loadJSON',
|
|
48
|
+
'getJson',
|
|
49
|
+
'dragAddItem',
|
|
50
|
+
'clipboard',
|
|
51
|
+
'clipboardBase64',
|
|
52
|
+
'saveJson',
|
|
53
|
+
'saveSvg',
|
|
54
|
+
'saveImg',
|
|
55
|
+
'getBase64',
|
|
56
|
+
'clear',
|
|
57
|
+
'preview',
|
|
58
|
+
'staticPreview',
|
|
59
|
+
'getSelectMode',
|
|
60
|
+
'getExtensionKey',
|
|
61
|
+
];
|
|
62
|
+
static events = [SelectMode.ONE, SelectMode.MULTI, SelectEvent.CANCEL];
|
|
63
|
+
// public hotkeys: string[] = ['left', 'right', 'down', 'up'];
|
|
64
|
+
constructor(
|
|
65
|
+
public canvas: fabric.Canvas,
|
|
66
|
+
public editor: IEditor
|
|
67
|
+
) {
|
|
68
|
+
this.selectedMode = SelectMode.EMPTY;
|
|
69
|
+
this._initSelectEvent();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
private _initSelectEvent() {
|
|
73
|
+
this.canvas.on('selection:created', () => this._emitSelectEvent());
|
|
74
|
+
this.canvas.on('selection:updated', () => this._emitSelectEvent());
|
|
75
|
+
this.canvas.on('selection:cleared', () => this._emitSelectEvent());
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private _emitSelectEvent() {
|
|
79
|
+
if (!this.canvas) {
|
|
80
|
+
throw TypeError('还未初始化');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const actives = this.canvas
|
|
84
|
+
.getActiveObjects()
|
|
85
|
+
.filter((item) => !(item instanceof fabric.GuideLine)); // 过滤掉辅助线
|
|
86
|
+
if (actives && actives.length === 1) {
|
|
87
|
+
this.selectedMode = SelectMode.ONE;
|
|
88
|
+
this.editor.emit(SelectEvent.ONE, actives);
|
|
89
|
+
} else if (actives && actives.length > 1) {
|
|
90
|
+
this.selectedMode = SelectMode.MULTI;
|
|
91
|
+
this.editor.emit(SelectEvent.MULTI, actives);
|
|
92
|
+
} else {
|
|
93
|
+
this.editor.emit(SelectEvent.CANCEL);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
getSelectMode() {
|
|
98
|
+
return String(this.selectedMode);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
insert(callback?: () => void) {
|
|
102
|
+
selectFiles({ accept: '.json' }).then((files) => {
|
|
103
|
+
if (files && files.length > 0) {
|
|
104
|
+
const file = files[0];
|
|
105
|
+
const reader = new FileReader();
|
|
106
|
+
reader.readAsText(file, 'UTF-8');
|
|
107
|
+
reader.onload = () => {
|
|
108
|
+
this.loadJSON(reader.result as string, callback);
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 设置path属性
|
|
115
|
+
renderITextPath(textPaths: Record<'id' | 'path', any>[]) {
|
|
116
|
+
textPaths.forEach((item) => {
|
|
117
|
+
const object = this.canvas
|
|
118
|
+
.getObjects()
|
|
119
|
+
.find((o) => o.id === item.id);
|
|
120
|
+
if (object) {
|
|
121
|
+
fabric.Path.fromObject(item.path, (e) => {
|
|
122
|
+
object.set('path', e);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async loadJSON(jsonFile: string | object, callback?: () => void) {
|
|
129
|
+
// 确保元素存在id
|
|
130
|
+
const temp =
|
|
131
|
+
typeof jsonFile === 'string' ? JSON.parse(jsonFile) : jsonFile;
|
|
132
|
+
const textPaths: Record<'id' | 'path', any>[] = [];
|
|
133
|
+
temp.objects.forEach((item: any) => {
|
|
134
|
+
!item.id && (item.id = uuid());
|
|
135
|
+
// 收集所有路径文本元素i-text,并设置path为null
|
|
136
|
+
if (item.type === 'i-text' && item.path) {
|
|
137
|
+
textPaths.push({ id: item.id, path: item.path });
|
|
138
|
+
item.path = null;
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// hookTransform遍历
|
|
143
|
+
const tempTransform = await this._transform(temp);
|
|
144
|
+
|
|
145
|
+
jsonFile = JSON.stringify(tempTransform);
|
|
146
|
+
// 加载前钩子
|
|
147
|
+
this.editor.hooksEntity.hookImportBefore.callAsync(jsonFile, () => {
|
|
148
|
+
this.canvas.loadFromJSON(jsonFile, () => {
|
|
149
|
+
// 把i-text对应的path加上
|
|
150
|
+
this.renderITextPath(textPaths);
|
|
151
|
+
this.canvas.renderAll();
|
|
152
|
+
// 加载后钩子
|
|
153
|
+
this.editor.hooksEntity.hookImportAfter.callAsync(
|
|
154
|
+
jsonFile,
|
|
155
|
+
() => {
|
|
156
|
+
// 修复导入带水印的json无法清除问题 #359
|
|
157
|
+
this.editor?.updateDrawStatus &&
|
|
158
|
+
typeof this.editor.updateDrawStatus ===
|
|
159
|
+
'function' &&
|
|
160
|
+
this.editor.updateDrawStatus(
|
|
161
|
+
!!temp['overlayImage']
|
|
162
|
+
);
|
|
163
|
+
this.canvas.renderAll();
|
|
164
|
+
callback && callback();
|
|
165
|
+
this.editor.emit('loadJson');
|
|
166
|
+
}
|
|
167
|
+
);
|
|
168
|
+
});
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async _transform(json: any) {
|
|
173
|
+
await this.promiseCallAsync(json);
|
|
174
|
+
if (json.objects) {
|
|
175
|
+
const all = json.objects.map((item: any) => {
|
|
176
|
+
return this._transform(item);
|
|
177
|
+
});
|
|
178
|
+
await Promise.all(all);
|
|
179
|
+
}
|
|
180
|
+
return json;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
promiseCallAsync(item: any) {
|
|
184
|
+
return new Promise((resolve) => {
|
|
185
|
+
this.editor.hooksEntity.hookTransform.callAsync(item, () => {
|
|
186
|
+
resolve(item);
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
getJson() {
|
|
192
|
+
const keys = this.getExtensionKey();
|
|
193
|
+
return this.canvas.toJSON(keys);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
getExtensionKey() {
|
|
197
|
+
return [
|
|
198
|
+
'id',
|
|
199
|
+
'gradientAngle',
|
|
200
|
+
'selectable',
|
|
201
|
+
'hasControls',
|
|
202
|
+
'linkData',
|
|
203
|
+
'editable',
|
|
204
|
+
'extensionType',
|
|
205
|
+
'extension',
|
|
206
|
+
'verticalAlign',
|
|
207
|
+
'roundValue',
|
|
208
|
+
'getBase64',
|
|
209
|
+
];
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* @description: 拖拽添加到画布
|
|
214
|
+
* @param {Event} event
|
|
215
|
+
* @param {Object} item
|
|
216
|
+
*/
|
|
217
|
+
dragAddItem(item: fabric.Object, event?: DragEvent) {
|
|
218
|
+
if (event) {
|
|
219
|
+
const { left, top } = this.canvas
|
|
220
|
+
.getSelectionElement()
|
|
221
|
+
.getBoundingClientRect();
|
|
222
|
+
if (event.x < left || event.y < top || item.width === undefined)
|
|
223
|
+
return;
|
|
224
|
+
|
|
225
|
+
const point = {
|
|
226
|
+
x: event.x - left,
|
|
227
|
+
y: event.y - top,
|
|
228
|
+
};
|
|
229
|
+
const pointerVpt = this.canvas.restorePointerVpt(point);
|
|
230
|
+
item.left = pointerVpt.x - item.width / 2;
|
|
231
|
+
item.top = pointerVpt.y;
|
|
232
|
+
}
|
|
233
|
+
const { width } = this._getSaveOption();
|
|
234
|
+
width && item.scaleToWidth(width / 2);
|
|
235
|
+
this.canvas.add(item);
|
|
236
|
+
this.canvas.setActiveObject(item);
|
|
237
|
+
|
|
238
|
+
!event && this.editor.position('center');
|
|
239
|
+
this.canvas.requestRenderAll();
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
clipboard() {
|
|
243
|
+
const jsonStr = this.getJson();
|
|
244
|
+
return clipboardText(JSON.stringify(jsonStr, null, '\t'));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async clipboardBase64() {
|
|
248
|
+
const dataUrl = await this.preview();
|
|
249
|
+
return clipboardText(dataUrl);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async saveJson() {
|
|
253
|
+
const dataUrl = this.getJson();
|
|
254
|
+
// 把文本text转为textgroup,让导入可以编辑
|
|
255
|
+
await transformText(dataUrl.objects);
|
|
256
|
+
const fileStr = `data:text/json;charset=utf-8,${encodeURIComponent(
|
|
257
|
+
JSON.stringify(dataUrl, null, '\t')
|
|
258
|
+
)}`;
|
|
259
|
+
downFile(fileStr, 'json');
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
saveSvg() {
|
|
263
|
+
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
264
|
+
const { fontOption, svgOption } = this._getSaveSvgOption();
|
|
265
|
+
fabric.fontPaths = {
|
|
266
|
+
...fontOption,
|
|
267
|
+
};
|
|
268
|
+
const dataUrl = this.canvas.toSVG(svgOption);
|
|
269
|
+
const fileStr = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(dataUrl)}`;
|
|
270
|
+
this.editor.hooksEntity.hookSaveAfter.callAsync(fileStr, () => {
|
|
271
|
+
downFile(fileStr, 'svg');
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
saveImg() {
|
|
277
|
+
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
278
|
+
const option = this._getSaveOption();
|
|
279
|
+
this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
|
|
280
|
+
const dataUrl = this.canvas.toDataURL(option);
|
|
281
|
+
this.editor.hooksEntity.hookSaveAfter.callAsync(dataUrl, () => {
|
|
282
|
+
downFile(dataUrl, 'png');
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
getBase64() {
|
|
288
|
+
return new Promise<string>((resolve) => {
|
|
289
|
+
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
290
|
+
const option = this._getSaveOption();
|
|
291
|
+
this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
|
|
292
|
+
const dataUrl = this.canvas.toDataURL(option);
|
|
293
|
+
this.editor.hooksEntity.hookSaveAfter.callAsync(dataUrl, () =>
|
|
294
|
+
resolve(dataUrl)
|
|
295
|
+
);
|
|
296
|
+
});
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
preview() {
|
|
301
|
+
return new Promise<string>((resolve) => {
|
|
302
|
+
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
|
|
303
|
+
const option = this._getSaveOption();
|
|
304
|
+
this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
|
|
305
|
+
this.canvas.renderAll();
|
|
306
|
+
const dataUrl = this.canvas.toDataURL(option);
|
|
307
|
+
this.editor.hooksEntity.hookSaveAfter.callAsync(dataUrl, () => {
|
|
308
|
+
resolve(dataUrl);
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
staticPreview() {
|
|
315
|
+
return new Promise<string>((resolve) => {});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
_getSaveSvgOption() {
|
|
319
|
+
const workspace = this.canvas
|
|
320
|
+
.getObjects()
|
|
321
|
+
.find((item) => item.id === 'workspace');
|
|
322
|
+
let fontFamilyArry = this.canvas
|
|
323
|
+
.getObjects()
|
|
324
|
+
.filter((item) => item.type == 'textbox')
|
|
325
|
+
.map((item) => item.fontFamily);
|
|
326
|
+
fontFamilyArry = Array.from(new Set(fontFamilyArry));
|
|
327
|
+
|
|
328
|
+
const fontList = this.editor.getPlugin('FontPlugin').cacheList;
|
|
329
|
+
|
|
330
|
+
const fontEntry = {};
|
|
331
|
+
for (const font of fontFamilyArry) {
|
|
332
|
+
const item = fontList.find((item) => item.name === font);
|
|
333
|
+
fontEntry[font] = item.file;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
console.log('_getSaveSvgOption', fontEntry);
|
|
337
|
+
const { left, top, width, height } = workspace as fabric.Object;
|
|
338
|
+
return {
|
|
339
|
+
fontOption: fontEntry,
|
|
340
|
+
svgOption: {
|
|
341
|
+
width,
|
|
342
|
+
height,
|
|
343
|
+
viewBox: {
|
|
344
|
+
x: left,
|
|
345
|
+
y: top,
|
|
346
|
+
width,
|
|
347
|
+
height,
|
|
348
|
+
},
|
|
349
|
+
},
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
_getSaveOption() {
|
|
354
|
+
const workspace = this.canvas
|
|
355
|
+
.getObjects()
|
|
356
|
+
.find((item: fabric.Object) => item.id === 'workspace');
|
|
357
|
+
const { left, top, width, height } = workspace as fabric.Object;
|
|
358
|
+
const option = {
|
|
359
|
+
name: 'New Image',
|
|
360
|
+
format: 'jpeg',
|
|
361
|
+
quality: 1,
|
|
362
|
+
multiplier: 5,
|
|
363
|
+
width,
|
|
364
|
+
height,
|
|
365
|
+
left,
|
|
366
|
+
top,
|
|
367
|
+
};
|
|
368
|
+
return option;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
clear() {
|
|
372
|
+
this.canvas.getObjects().forEach((obj) => {
|
|
373
|
+
if (obj.id !== 'workspace') {
|
|
374
|
+
this.canvas.remove(obj);
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
this.editor?.setWorkspaseBg('#fff');
|
|
378
|
+
this.canvas.discardActiveObject();
|
|
379
|
+
this.canvas.renderAll();
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
destroy() {
|
|
383
|
+
console.log('pluginDestroy');
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
export default ServersPlugin;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import Editor from './Editor';
|
|
2
|
+
import Utils from './utils/utils';
|
|
3
|
+
import CustomRect from './objects/CustomRect';
|
|
4
|
+
import CustomTextbox from './objects/CustomTextbox';
|
|
5
|
+
import { fabric } from 'fabric';
|
|
6
|
+
import type { Canvas, Point, IEvent } from 'fabric/fabric-impl';
|
|
7
|
+
|
|
8
|
+
export { Utils, CustomRect, CustomTextbox, fabric, Canvas, Point, IEvent };
|
|
9
|
+
export default Editor;
|
|
10
|
+
|
|
11
|
+
export * from './interface/Editor';
|