From 1de2e33eded4a935762e41aaebe96660267e3156 Mon Sep 17 00:00:00 2001 From: "jooyyy.eth" Date: Wed, 17 Jan 2024 21:57:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=201.=20Add=20'=E2=8C=98'=20undo|redo=20ho?= =?UTF-8?q?tkeys=20to=20support=20mac=20keyboard;=202.=20Fix=20undo=20rend?= =?UTF-8?q?er=20logic=20issue=20(#303)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/ServersPlugin.ts | 1 + src/core/core.ts | 9 +-------- src/core/plugin/GroupPlugin.ts | 1 - src/core/plugin/HistoryPlugin.ts | 31 ++++++++++++++++++++++-------- src/core/plugin/WorkspacePlugin.ts | 6 ------ 5 files changed, 25 insertions(+), 23 deletions(-) diff --git a/src/core/ServersPlugin.ts b/src/core/ServersPlugin.ts index 97f8f764..b045573d 100644 --- a/src/core/ServersPlugin.ts +++ b/src/core/ServersPlugin.ts @@ -74,6 +74,7 @@ class ServersPlugin { // 加载后钩子 this.editor.hooksEntity.hookImportAfter.callAsync(jsonFile, () => { this.canvas.renderAll(); + // this.editor.getPlugin('HistoryPlugin').history.clear(); }); }); }); diff --git a/src/core/core.ts b/src/core/core.ts index deb66df5..6a450efc 100644 --- a/src/core/core.ts +++ b/src/core/core.ts @@ -25,14 +25,7 @@ class Editor extends EventEmitter { private hooksEntity: { [propName: string]: AsyncSeriesHook; } = {}; - // constructor(canvas: fabric.Canvas) { - // super(); - // this.canvas = canvas; - // this._initContextMenu(); - // this._bindContextMenu(); - // this._initActionHooks(); - // this._initServersPlugin(); - // } + init(canvas: fabric.Canvas) { this.canvas = canvas; this._initContextMenu(); diff --git a/src/core/plugin/GroupPlugin.ts b/src/core/plugin/GroupPlugin.ts index 58c5d584..fbffa7c5 100644 --- a/src/core/plugin/GroupPlugin.ts +++ b/src/core/plugin/GroupPlugin.ts @@ -52,7 +52,6 @@ class GroupPlugin { contextMenu() { const activeObject = this.canvas.getActiveObject(); - console.log(activeObject, '111'); if (activeObject && activeObject.type === 'group') { return [ { text: '拆分组合', hotkey: 'Ctrl+V', disabled: false, onclick: () => this.unGroup() }, diff --git a/src/core/plugin/HistoryPlugin.ts b/src/core/plugin/HistoryPlugin.ts index 94f64625..efa0d896 100644 --- a/src/core/plugin/HistoryPlugin.ts +++ b/src/core/plugin/HistoryPlugin.ts @@ -12,7 +12,6 @@ import Editor from '../core'; import { ref } from 'vue'; import { useRefHistory } from '@vueuse/core'; type IEditor = Editor; -// import { v4 as uuid } from 'uuid'; class HistoryPlugin { public canvas: fabric.Canvas; @@ -20,7 +19,7 @@ class HistoryPlugin { static pluginName = 'HistoryPlugin'; static apis = ['undo', 'redo', 'getHistory']; static events = ['historyInitSuccess']; - public hotkeys: string[] = ['ctrl+z']; + public hotkeys: string[] = ['ctrl+z', 'ctrl+shift+z', '⌘+z', '⌘+shift+z']; history: any; constructor(canvas: fabric.Canvas, editor: IEditor) { this.canvas = canvas; @@ -38,6 +37,16 @@ class HistoryPlugin { 'object:modified': (event) => this._save(event), 'selection:updated': (event) => this._save(event), }); + window.addEventListener('beforeunload', function (e) { + if (history.length > 0) { + (e || window.event).returnValue = '确认离开'; + } + }); + } + + // 导入模板之后,清理 History 缓存 + hookImportAfter() { + this.history.clear(); } getHistory() { @@ -58,8 +67,8 @@ class HistoryPlugin { undo() { if (this.history.canUndo.value) { - this.renderCanvas(); this.history.undo(); + this.renderCanvas(); } } @@ -79,13 +88,19 @@ class HistoryPlugin { // 快捷键扩展回调 hotkeyEvent(eventName: string, e: any) { - if (eventName === 'ctrl+z' && e.type === 'keydown') { - this.undo(); + if (e.type === 'keydown') { + switch (eventName) { + case 'ctrl+z': + case '⌘+z': + this.undo(); + break; + case 'ctrl+shift+z': + case '⌘+shift+z': + this.redo(); + break; + } } } - destroy() { - console.log('pluginDestroy'); - } } export default HistoryPlugin; diff --git a/src/core/plugin/WorkspacePlugin.ts b/src/core/plugin/WorkspacePlugin.ts index c7149022..1cff831a 100644 --- a/src/core/plugin/WorkspacePlugin.ts +++ b/src/core/plugin/WorkspacePlugin.ts @@ -43,12 +43,6 @@ class WorkspacePlugin { this._bindWheel(); } - // hookImportBefore() { - // return new Promise((resolve, reject) => { - // resolve(); - // }); - // } - hookImportAfter() { return new Promise((resolve) => { const workspace = this.canvas.getObjects().find((item) => item.id === 'workspace');