Skip to content

Commit

Permalink
feat: 添加插件api提示
Browse files Browse the repository at this point in the history
  • Loading branch information
liumingye committed Aug 10, 2024
1 parent 55662c2 commit 4642a57
Show file tree
Hide file tree
Showing 40 changed files with 334 additions and 139 deletions.
17 changes: 12 additions & 5 deletions packages/core/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import hotkeys from 'hotkeys-js';
import ContextMenu from './ContextMenu.js';
import ServersPlugin from './ServersPlugin';
import { AsyncSeriesHook } from 'tapable';
import type {
IPluginMenu,
IPluginClass,
IPluginOption,
IEditorHooksType,
IPluginTempl,
} from '@kuaitu/core';

import Utils from './utils/utils';

Expand Down Expand Up @@ -44,10 +51,10 @@ class Editor extends EventEmitter {
}

// 引入组件
use(plugin: IPluginClass, options?: IPluginOption) {
use(plugin: IPluginTempl, options?: IPluginOption) {
if (this._checkPlugin(plugin) && this.canvas) {
this._saveCustomAttr(plugin);
const pluginRunTime = new plugin(this.canvas, this, options || {}) as IPluginClass;
const pluginRunTime = new (plugin as IPluginClass)(this.canvas, this, options || {});
// 添加插件名称
pluginRunTime.pluginName = plugin.pluginName;
this.pluginMap[plugin.pluginName] = pluginRunTime;
Expand All @@ -73,7 +80,7 @@ class Editor extends EventEmitter {
}

// 检查组件
private _checkPlugin(plugin: IPluginClass) {
private _checkPlugin(plugin: IPluginTempl) {
const { pluginName, events = [], apis = [] } = plugin;
//名称检查
if (this.pluginMap[pluginName]) {
Expand Down Expand Up @@ -103,7 +110,7 @@ class Editor extends EventEmitter {
// eslint-disable-next-line prefer-rest-params
const result = hook.apply(plugin, [...arguments]);
// hook 兼容非 Promise 返回值
return result instanceof Promise ? result : Promise.resolve(result);
return (result as any) instanceof Promise ? result : Promise.resolve(result);
});
}
});
Expand All @@ -120,7 +127,7 @@ class Editor extends EventEmitter {
}

// 保存组件自定义事件与API
private _saveCustomAttr(plugin: IPluginClass) {
private _saveCustomAttr(plugin: IPluginTempl) {
const { events = [], apis = [] } = plugin;
this.customApis = this.customApis.concat(apis);
this.customEvents = this.customEvents.concat(events);
Expand Down
33 changes: 25 additions & 8 deletions packages/core/ServersPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,31 @@
import { v4 as uuid } from 'uuid';
import { selectFiles, clipboardText, downFile } from './utils/utils';
import { fabric } from 'fabric';
import Editor from './Editor';
type IEditor = Editor;
import type { IEditor, IPluginTempl } from '@kuaitu/core';
import { SelectEvent, SelectMode } from './eventType';

type IPlugin = Pick<
ServersPlugin,
| 'insert'
| 'loadJSON'
| 'getJson'
| 'dragAddItem'
| 'clipboard'
| 'clipboardBase64'
| 'saveJson'
| 'saveSvg'
| 'saveImg'
| 'clear'
| 'preview'
| 'getSelectMode'
| 'getExtensionKey'
>;

declare module '@kuaitu/core' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IEditor extends IPlugin {}
}

function transformText(objects: any) {
if (!objects) return;
objects.forEach((item: any) => {
Expand All @@ -23,9 +44,7 @@ function transformText(objects: any) {
});
}

class ServersPlugin {
public canvas: fabric.Canvas;
public editor: IEditor;
class ServersPlugin implements IPluginTempl {
public selectedMode: SelectMode;
static pluginName = 'ServersPlugin';
static apis = [
Expand All @@ -45,9 +64,7 @@ class ServersPlugin {
];
static events = [SelectMode.ONE, SelectMode.MULTI, SelectEvent.CANCEL];
// public hotkeys: string[] = ['left', 'right', 'down', 'up'];
constructor(canvas: fabric.Canvas, editor: IEditor) {
this.canvas = canvas;
this.editor = editor;
constructor(public canvas: fabric.Canvas, public editor: IEditor) {
this.selectedMode = SelectMode.EMPTY;
this._initSelectEvent();
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export { default as AddBaseTypePlugin } from './plugin/AddBaseTypePlugin';
import EventType from './eventType';
import Utils from './utils/utils';
import CustomRect from './objects/CustomRect';
// import { extend } from 'dayjs';

export { EventType, Utils, CustomRect };
export default Editor;

export * from './interface/Editor';
48 changes: 48 additions & 0 deletions packages/core/interface/Editor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type Editor from '@kuaitu/core';

// IEditor类型包含插件实例,Editor不包含插件实例
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface IEditor extends Editor {}

// 生命周期事件类型
export type IEditorHooksType =
| 'hookImportBefore'
| 'hookImportAfter'
| 'hookSaveBefore'
| 'hookSaveAfter'
| 'hookTransform';

// 插件实例
export declare class IPluginTempl {
constructor(canvas: fabric.Canvas, editor: IEditor, options?: IPluginOption);
static pluginName: string;
static events: string[];
static apis: string[];
hotkeyEvent?: (name: string, e: KeyboardEvent) => void;
hookImportBefore?: (...args: unknown[]) => Promise<unknown>;
hookImportAfter?: (...args: unknown[]) => Promise<unknown>;
hookSaveBefore?: (...args: unknown[]) => Promise<unknown>;
hookSaveAfter?: (...args: unknown[]) => Promise<unknown>;
hookTransform?: (...args: unknown[]) => Promise<unknown>;
[propName: string]: any;
canvas?: fabric.Canvas;
editor?: IEditor;
}

export declare interface IPluginOption {
[propName: string]: unknown | undefined;
}

declare class IPluginClass2 extends IPluginTempl {
constructor();
}
// 插件class
export declare interface IPluginClass {
new (canvas: fabric.Canvas, editor: Editor, options?: IPluginOption): IPluginClass2;
}

export declare interface IPluginMenu {
text: string;
command?: () => void;
child?: IPluginMenu[];
}
12 changes: 10 additions & 2 deletions packages/core/plugin/AddBaseTypePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
*/

import { fabric } from 'fabric';
import type Editor from '../Editor';
import type { IEditor, IPluginTempl } from '@kuaitu/core';
import { v4 as uuid } from 'uuid';

type IPlugin = Pick<AddBaseTypePlugin, 'addBaseType' | 'createImgByElement'>;

declare module '@kuaitu/core' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IEditor extends IPlugin {}
}

export default class AddBaseTypePlugin implements IPluginTempl {
static pluginName = 'AddBaseTypePlugin';
static apis = ['addBaseType', 'createImgByElement'];
constructor(public canvas: fabric.Canvas, public editor: Editor) {
constructor(public canvas: fabric.Canvas, public editor: IEditor) {
this.editor = editor;
this.canvas = canvas;
}
Expand Down Expand Up @@ -61,6 +68,7 @@ export default class AddBaseTypePlugin implements IPluginTempl {

_toScale(item: fabric.Object) {
const { width } = this.editor.getWorkspase();
if (width === undefined) return;
item.scaleToWidth(width / 2);
}

Expand Down
5 changes: 1 addition & 4 deletions packages/core/plugin/AlignGuidLinePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
* @LastEditTime: 2024-04-10 17:32:48
* @Description: 辅助线功能
*/

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

import { fabric } from 'fabric';
import { IEditor, IPluginTempl } from '@kuaitu/core';

declare interface VerticalLine {
x: number;
Expand Down
9 changes: 7 additions & 2 deletions packages/core/plugin/BarCodePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
*/

import { fabric } from 'fabric';
import Editor from '../Editor';
import JsBarcode from 'jsbarcode';
import type { IEditor, IPluginTempl } from '@kuaitu/core';

type IEditor = Editor;
type IPlugin = Pick<BarCodePlugin, 'addBarcode' | 'setBarcode' | 'getBarcodeTypes'>;

declare module '@kuaitu/core' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IEditor extends IPlugin {}
}

// 条形码生成参数
// https://github.com/lindell/JsBarcode/wiki/Options
Expand Down
10 changes: 8 additions & 2 deletions packages/core/plugin/CenterAlignPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
*/

import { fabric } from 'fabric';
import Editor from '../Editor';
type IEditor = Editor;
import type { IEditor, IPluginTempl } from '@kuaitu/core';

type IPlugin = Pick<CenterAlignPlugin, 'centerH' | 'center' | 'position' | 'centerV'>;

declare module '@kuaitu/core' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IEditor extends IPlugin {}
}

class CenterAlignPlugin implements IPluginTempl {
static pluginName = 'CenterAlignPlugin';
Expand Down
5 changes: 1 addition & 4 deletions packages/core/plugin/ControlsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
* @LastEditTime: 2024-07-16 14:59:25
* @Description: 控制条插件
*/

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

import { fabric } from 'fabric';
import verticalImg from '../assets/middlecontrol.svg?url';
// import verticalImg from './middlecontrol.svg';
import horizontalImg from '../assets/middlecontrolhoz.svg?url';
import edgeImg from '../assets/edgecontrol.svg?url';
import rotateImg from '../assets/rotateicon.svg?url';
import type { IEditor, IPluginTempl } from '@kuaitu/core';

/**
* 实际场景: 在进行某个对象缩放的时候,由于fabricjs默认精度使用的是toFixed(2)。
Expand Down
4 changes: 1 addition & 3 deletions packages/core/plugin/ControlsRotatePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
* @LastEditTime: 2024-04-10 17:32:56
* @Description: 控制条插件
*/

import Editor from '../Editor';
type IEditor = Editor;
import type { IEditor, IPluginTempl } from '@kuaitu/core';

// 定义旋转光标样式,根据转动角度设定光标旋转
function rotateIcon(angle: number) {
Expand Down
10 changes: 8 additions & 2 deletions packages/core/plugin/CopyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
*/

import { fabric } from 'fabric';
import Editor from '../Editor';
type IEditor = Editor;
import { v4 as uuid } from 'uuid';
import { getImgStr } from '../utils/utils';
import type { IEditor, IPluginTempl } from '@kuaitu/core';

type IPlugin = Pick<CopyPlugin, 'clone'>;

declare module '@kuaitu/core' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IEditor extends IPlugin {}
}

class CopyPlugin implements IPluginTempl {
static pluginName = 'CopyPlugin';
Expand Down
11 changes: 8 additions & 3 deletions packages/core/plugin/DeleteHotKeyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
*/

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

type IPlugin = Pick<DeleteHotKeyPlugin, 'del'>;

declare module '@kuaitu/core' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IEditor extends IPlugin {}
}

class DeleteHotKeyPlugin implements IPluginTempl {
static pluginName = 'DeleteHotKeyPlugin';
Expand Down
10 changes: 8 additions & 2 deletions packages/core/plugin/DrawLinePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ import { v4 as uuid } from 'uuid';
import { fabric } from 'fabric';
import Arrow from '../objects/Arrow';
import ThinTailArrow from '../objects/ThinTailArrow';
import Editor from '../Editor';
type IEditor = Editor;
import type { IEditor, IPluginTempl } from '@kuaitu/core';

type IPlugin = Pick<DrawLinePlugin, 'setLineType' | 'setMode'>;

declare module '@kuaitu/core' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IEditor extends IPlugin {}
}

class DrawLinePlugin implements IPluginTempl {
static pluginName = 'DrawLinePlugin';
Expand Down
9 changes: 7 additions & 2 deletions packages/core/plugin/DrawPolygonPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { fabric } from 'fabric';
import Editor from '../Editor';
import { v4 as uuid } from 'uuid';
import { shiftAngle } from '../utils/utils';
import type { IEditor, IPluginTempl } from '@kuaitu/core';

type IEditor = Editor;
type IPlugin = Pick<DrawPolygonPlugin, 'beginDrawPolygon' | 'endDrawPolygon' | 'discardPolygon'>;

declare module '@kuaitu/core' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IEditor extends IPlugin {}
}

type LineCoords = [fabric.Point, fabric.Point];
type OffListener = (ev: fabric.IEvent) => void;
Expand Down
10 changes: 8 additions & 2 deletions packages/core/plugin/DringPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
* @Description: 拖拽插件
*/

import Editor from '../Editor';
type IEditor = Editor;
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 = {};
Expand Down
13 changes: 9 additions & 4 deletions packages/core/plugin/FlipPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { fabric } from 'fabric';
import type Editor from '../Editor';
import { SelectMode } from '../eventType';
// import { Ref } from 'vue';
import type { IEditor, IPluginTempl } from '@kuaitu/core';

type IPlugin = Pick<FlipPlugin, 'flip'>;

declare module '@kuaitu/core' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface IEditor extends IPlugin {}
}

// import { t } from '@/language/index';
const t = (key: string) => key;
// import event from '@/utils/event/notifier';

export default class FlipPlugin implements IPluginTempl {
static pluginName = 'FlipPlugin';
static apis = ['flip'];
constructor(public canvas: fabric.Canvas, public editor: Editor) {}
constructor(public canvas: fabric.Canvas, public editor: IEditor) {}

flip(type: 'X' | 'Y') {
const activeObject = this.canvas.getActiveObject();
Expand Down
Loading

0 comments on commit 4642a57

Please sign in to comment.