Skip to content

Commit

Permalink
refactor(事件机制): 迁移到Core
Browse files Browse the repository at this point in the history
  • Loading branch information
nihaojob committed Apr 10, 2024
1 parent a00d9e0 commit 7b6fcd0
Show file tree
Hide file tree
Showing 27 changed files with 65 additions and 117 deletions.
16 changes: 9 additions & 7 deletions packages/core/core.ts → packages/core/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Editor extends EventEmitter {
'hookSaveAfter',
];
public hooksEntity: {
[propName: string]: AsyncSeriesHook;
[propName: string]: AsyncSeriesHook<any, any>;
} = {};

init(canvas: fabric.Canvas) {
Expand All @@ -35,10 +35,10 @@ class Editor extends EventEmitter {
}

// 引入组件
use(plugin: IPluginClass, options: IPluginOption) {
use(plugin: IPluginClass, options?: IPluginOption) {
if (this._checkPlugin(plugin)) {
this._saveCustomAttr(plugin);
const pluginRunTime = new plugin(this.canvas, this, options);
const pluginRunTime = new plugin(this.canvas, this, options || {}) as IPluginClass;
this.pluginMap[plugin.pluginName] = pluginRunTime;
this._bindingHooks(pluginRunTime);
this._bindingHotkeys(pluginRunTime);
Expand Down Expand Up @@ -91,7 +91,9 @@ class Editor extends EventEmitter {
private _bindingHotkeys(plugin: IPluginTempl) {
plugin?.hotkeys?.forEach((keyName: string) => {
// 支持 keyup
hotkeys(keyName, { keyup: true }, (e) => plugin.hotkeyEvent(keyName, e));
hotkeys(keyName, { keyup: true }, (e) => {
plugin.hotkeyEvent && plugin.hotkeyEvent(keyName, e);
});
});
}

Expand All @@ -103,7 +105,7 @@ class Editor extends EventEmitter {
}
// 代理API事件
private _bindingApis(pluginRunTime: IPluginTempl) {
const { apis = [] } = pluginRunTime.constructor;
const { apis = [] } = (pluginRunTime.constructor as any) || {};
apis.forEach((apiName: string) => {
this[apiName] = function () {
// eslint-disable-next-line prefer-rest-params
Expand Down Expand Up @@ -151,11 +153,11 @@ class Editor extends EventEmitter {
}

_initServersPlugin() {
this.use(ServersPlugin, {});
this.use(ServersPlugin);
}

// 解决 listener 为 undefined 的时候卸载错误
off<K>(eventName: string, listener: any): this {
off(eventName: string, listener: any): this {
// noinspection TypeScriptValidateTypes
return listener ? super.off(eventName, listener) : this;
}
Expand Down
7 changes: 7 additions & 0 deletions packages/core/Instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* @Author: 秦少卫
* @Date: 2024-04-10 15:38:47
* @LastEditors: 秦少卫
* @LastEditTime: 2024-04-10 15:38:48
* @Description: 类型文件
*/
9 changes: 4 additions & 5 deletions packages/core/ServersPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
* @Author: 秦少卫
* @Date: 2023-06-20 12:52:09
* @LastEditors: 秦少卫
* @LastEditTime: 2024-04-10 15:13:17
* @LastEditTime: 2024-04-10 17:32:45
* @Description: 内部插件
*/
import { v4 as uuid } from 'uuid';
import { selectFiles, clipboardText } from '@/utils/utils';
// import { clipboardText } from '@/utils/utils.ts';
import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;
// import { v4 as uuid } from 'uuid';
import { SelectEvent, SelectMode } from './eventType';
Expand Down Expand Up @@ -103,7 +102,7 @@ class ServersPlugin {
});
}

insertSvgFile(jsonFile: string, callback: () => void) {
insertSvgFile(jsonFile: string, callback?: () => void) {
// 加载前钩子
this.editor.hooksEntity.hookImportBefore.callAsync(jsonFile, () => {
this.canvas.loadFromJSON(jsonFile, () => {
Expand Down Expand Up @@ -180,7 +179,7 @@ class ServersPlugin {
}

preview() {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
this.editor.hooksEntity.hookSaveBefore.callAsync('', () => {
const option = this._getSaveOption();
this.canvas.setViewportTransform([1, 0, 0, 1, 0, 0]);
Expand Down
4 changes: 2 additions & 2 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
* @Author: 秦少卫
* @Date: 2023-02-03 23:29:34
* @LastEditors: 秦少卫
* @LastEditTime: 2024-04-10 14:01:02
* @LastEditTime: 2024-04-10 15:50:30
* @Description: 核心入口文件
*/
import Editor from './core';
import Editor from './Editor';
import DringPlugin from './plugin/DringPlugin';
import AlignGuidLinePlugin from './plugin/AlignGuidLinePlugin';
import ControlsPlugin from './plugin/ControlsPlugin';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Editor from './core';
import Editor from './Editor';
type IEditor = Editor;

class EditorWorkspacePlugin {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/AlignGuidLinePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* @Author: 秦少卫
* @Date: 2023-05-21 08:55:25
* @LastEditors: 秦少卫
* @LastEditTime: 2023-06-09 13:17:27
* @LastEditTime: 2024-04-10 17:32:48
* @Description: 辅助线功能
*/

import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;

import { fabric } from 'fabric';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/CenterAlignPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: 秦少卫
* @Date: 2023-06-15 22:49:42
* @LastEditors: 秦少卫
* @LastEditTime: 2023-06-27 23:10:47
* @LastEditTime: 2024-04-10 17:32:51
* @Description: 居中对齐插件
*/

import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;

class CenterAlignPlugin {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/ControlsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* @Author: 秦少卫
* @Date: 2023-06-13 23:00:43
* @LastEditors: 秦少卫
* @LastEditTime: 2024-04-10 11:29:46
* @LastEditTime: 2024-04-10 17:32:54
* @Description: 控制条插件
*/

import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;

import { fabric } from 'fabric';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/ControlsRotatePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* @Author: 秦少卫
* @Date: 2023-06-13 23:07:04
* @LastEditors: 秦少卫
* @LastEditTime: 2023-06-13 23:10:52
* @LastEditTime: 2024-04-10 17:32:56
* @Description: 控制条插件
*/

import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;

// 定义旋转光标样式,根据转动角度设定光标旋转
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/CopyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: 秦少卫
* @Date: 2023-06-20 12:38:37
* @LastEditors: 秦少卫
* @LastEditTime: 2023-06-20 13:34:21
* @LastEditTime: 2024-04-10 17:33:00
* @Description: 复制插件
*/

import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;
import { v4 as uuid } from 'uuid';

Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/DeleteHotKeyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: 秦少卫
* @Date: 2023-06-20 12:57:35
* @LastEditors: 秦少卫
* @LastEditTime: 2023-06-27 23:10:02
* @LastEditTime: 2024-04-10 17:33:02
* @Description: 删除快捷键
*/

import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;
// import { v4 as uuid } from 'uuid';

Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/DownFontPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* @Author: 秦少卫
* @Date: 2023-06-27 22:58:57
* @LastEditors: 秦少卫
* @LastEditTime: 2024-03-17 21:35:50
* @LastEditTime: 2024-04-10 17:33:08
* @Description: 下载字体插件
*/

import { downFontByJSON } from '../utils';
import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;

class DownFontPlugin {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/DrawLinePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
* @Author: 秦少卫
* @Date: 2023-06-21 22:09:36
* @LastEditors: 秦少卫
* @LastEditTime: 2024-03-16 08:19:12
* @LastEditTime: 2024-04-10 17:33:05
* @Description: file content
*/

import { v4 as uuid } from 'uuid';
import { fabric } from 'fabric';
import Arrow from '../objects/Arrow';
import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;

class DrawLinePlugin {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/DringPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* @Author: 秦少卫
* @Date: 2023-05-19 08:31:34
* @LastEditors: 秦少卫
* @LastEditTime: 2023-06-27 23:20:25
* @LastEditTime: 2024-04-10 17:33:11
* @Description: 拖拽插件
*/

import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;

declare type ExtCanvas = fabric.Canvas & {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/plugin/FlipPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fabric } from 'fabric';
import type Editor from '../core';
import type Editor from '../Editor';
import { SelectMode } from '../eventType';
// import { Ref } from 'vue';

Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/GroupAlignPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: 秦少卫
* @Date: 2023-06-22 16:19:46
* @LastEditors: 秦少卫
* @LastEditTime: 2024-02-20 13:41:29
* @LastEditTime: 2024-04-10 17:33:17
* @Description: 组对齐插件
*/

import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;

class GroupAlignPlugin {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/GroupPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: 秦少卫
* @Date: 2023-06-20 13:21:10
* @LastEditors: 秦少卫
* @LastEditTime: 2023-06-20 13:42:32
* @LastEditTime: 2024-04-10 17:33:19
* @Description: 组合拆分组合插件
*/

import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
import { v4 as uuid } from 'uuid';
type IEditor = Editor;

Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/GroupTextEditorPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: 秦少卫
* @Date: 2023-06-22 16:11:40
* @LastEditors: 秦少卫
* @LastEditTime: 2023-08-07 23:24:36
* @LastEditTime: 2024-04-10 17:33:22
* @Description: 组内文字编辑
*/

import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
import { v4 as uuid } from 'uuid';
type IEditor = Editor;

Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/HistoryPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* @Author: 秦少卫
* @Date: 2023-06-20 13:06:31
* @LastEditors: 秦少卫
* @LastEditTime: 2024-02-20 13:33:29
* @LastEditTime: 2024-04-10 17:33:25
* @Description: 历史记录插件
*/
import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
import 'fabric-history';

type IEditor = Editor;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/LayerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: 秦少卫
* @Date: 2023-06-15 23:23:18
* @LastEditors: 秦少卫
* @LastEditTime: 2023-06-27 23:07:57
* @LastEditTime: 2024-04-10 17:33:28
* @Description: 图层调整插件
*/

import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;

class LayerPlugin {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/MaterialPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: 秦少卫
* @Date: 2023-08-04 21:13:16
* @LastEditors: 秦少卫
* @LastEditTime: 2023-08-07 23:04:58
* @LastEditTime: 2024-04-10 17:33:31
* @Description: 素材插件
*/

import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;
import axios from 'axios';

Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/MoveHotKeyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: 秦少卫
* @Date: 2023-06-20 12:52:09
* @LastEditors: 秦少卫
* @LastEditTime: 2023-06-20 13:06:21
* @LastEditTime: 2024-04-10 17:32:31
* @Description: 移动快捷键
*/

import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
type IEditor = Editor;
// import { v4 as uuid } from 'uuid';

Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/RulerPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: 秦少卫
* @Date: 2023-07-04 23:45:49
* @LastEditors: 秦少卫
* @LastEditTime: 2024-03-16 08:19:20
* @LastEditTime: 2024-04-10 17:33:54
* @Description: 标尺插件
*/

import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
// import { throttle } from 'lodash-es';
type IEditor = Editor;

Expand Down
4 changes: 2 additions & 2 deletions packages/core/plugin/WorkspacePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @Author: 秦少卫
* @Date: 2023-06-27 12:26:41
* @LastEditors: 秦少卫
* @LastEditTime: 2024-04-10 11:47:47
* @LastEditTime: 2024-04-10 17:33:58
* @Description: 画布区域插件
*/

import { fabric } from 'fabric';
import Editor from '../core';
import Editor from '../Editor';
import { throttle } from 'lodash-es';
type IEditor = Editor;

Expand Down
Loading

0 comments on commit 7b6fcd0

Please sign in to comment.