Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin): adding support for the middle mouse button event to drag the workspace #525

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export { default as ImageStroke } from './plugin/ImageStroke';
export { default as ResizePlugin } from './plugin/ResizePlugin';
export { default as LockPlugin } from './plugin/LockPlugin';
export { default as AddBaseTypePlugin } from './plugin/AddBaseTypePlugin';
export { default as MiddleMousePlugin } from './plugin/MiddleMousePlugin';
import EventType from './eventType';
import Utils from './utils/utils';
import CustomRect from './objects/CustomRect';
Expand Down
3 changes: 2 additions & 1 deletion packages/core/plugin/DringPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export class DringPlugin implements IPluginTempl {
const This = this;
this.canvas.on('mouse:down', function (this: ExtCanvas, opt) {
const evt = opt.e;
if (evt.altKey || This.dragMode) {
// evt.button === 1 为鼠标中键的判断
if (evt.altKey || This.dragMode || evt.button === 1) {
This.canvas.setCursor('grabbing');
This.canvas.discardActiveObject();
This._setDring();
Expand Down
49 changes: 49 additions & 0 deletions packages/core/plugin/MiddleMousePlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* @Author: George
* @Date: 2024-10-29 11:11:11
* @LastEditors: George
* @LastEditTime: 2024-10-29 11:11:11
* @Description: 鼠标中键点击事件插件
*/

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

class MiddleMousePlugin implements IPluginTempl {
static pluginName = 'MiddleMousePlugin';
workspaceEl!: HTMLElement;

constructor(public canvas: fabric.Canvas, public editor: IEditor) {
this.init();
}

private init() {
const workspaceEl = document.querySelector('#workspace') as HTMLElement;
if (!workspaceEl) {
throw new Error('element #workspace is missing, plz check!');
}
this.workspaceEl = workspaceEl;
this.initListener();
}

private handleMouseUp = (e: MouseEvent) => e.button === 1 && this.canvas.fire('mouse:up', { e });

private handleMouseDown = (e: MouseEvent) =>
e.button === 1 && this.canvas.fire('mouse:down', { e });

/**
* @desc 初始化鼠标中键监听事件
*/
private initListener() {
this.workspaceEl.addEventListener('mouseup', this.handleMouseUp);
this.workspaceEl.addEventListener('mousedown', this.handleMouseDown);
}

destroy() {
this.workspaceEl.removeEventListener('mouseup', this.handleMouseUp);
this.workspaceEl.removeEventListener('mousedown', this.handleMouseDown);
console.log('pluginDestroy');
}
}

export default MiddleMousePlugin;
2 changes: 2 additions & 0 deletions src/views/home/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ import Editor, {
LockPlugin,
AddBaseTypePlugin,
MaskPlugin,
MiddleMousePlugin,
} from '@kuaitu/core';
import Edit from '@/components/edit.vue';
import ClipImage from '@/components/clipImage.vue';
Expand Down Expand Up @@ -392,6 +393,7 @@ onMounted(() => {
.use(ResizePlugin)
.use(LockPlugin)
.use(AddBaseTypePlugin)
.use(MiddleMousePlugin)
.use(MaskPlugin);

state.show = true;
Expand Down
Loading