Skip to content

Commit

Permalink
feat: 1. Add '⌘' undo|redo hotkeys to support mac keyboard; 2. Fix un…
Browse files Browse the repository at this point in the history
…do render logic issue (#303)
  • Loading branch information
jooyyy authored Jan 17, 2024
1 parent 436d61a commit 1de2e33
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/core/ServersPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ServersPlugin {
// 加载后钩子
this.editor.hooksEntity.hookImportAfter.callAsync(jsonFile, () => {
this.canvas.renderAll();
// this.editor.getPlugin('HistoryPlugin').history.clear();
});
});
});
Expand Down
9 changes: 1 addition & 8 deletions src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/core/plugin/GroupPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() },
Expand Down
31 changes: 23 additions & 8 deletions src/core/plugin/HistoryPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@ 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;
public editor: IEditor;
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;
Expand All @@ -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() {
Expand All @@ -58,8 +67,8 @@ class HistoryPlugin {

undo() {
if (this.history.canUndo.value) {
this.renderCanvas();
this.history.undo();
this.renderCanvas();
}
}

Expand All @@ -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;
6 changes: 0 additions & 6 deletions src/core/plugin/WorkspacePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 1de2e33

Please sign in to comment.