Skip to content

Commit

Permalink
feat: 实现画布的回退和恢复功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan Heng committed Jul 28, 2023
1 parent 88c61bd commit 0b5c7fd
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
2 changes: 2 additions & 0 deletions main/src/view/canvas/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ onMounted(() => {
<template>
<div class="wrapper">
<div class="tools">
<button @click="app.undo()">回退</button>
<button @click="app.redo()">恢复</button>
<button @click="app.setTool(selectTool)">选择🖱️</button>
<!-- <button @click="app.setModel('drawing')">铅笔✏️</button> -->
<button @click="app.setTool(rectTool)">矩形⬜️</button>
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ export class App extends BaseService<EventArgs> {
aboutToChangePlugins?.forEach((plugin) => plugin?.dispose());
return this;
}

public dispose(): void {
this.currentTool = null;
this.disposePlugins(Array.from(this.installedPlugins.keys()));
this.canvas.dispose();
this.removeAllListeners();
}
}

export default App;
1 change: 1 addition & 0 deletions packages/plugin-history/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './add-object-cmd';
export * from './remove-object-cmd';
27 changes: 27 additions & 0 deletions packages/plugin-history/src/commands/remove-object-cmd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { App } from '@pictode/core';

import { Cmd } from '../types';

import { BaseCmd } from './base';

export class RemoveObjectCmd extends BaseCmd<Cmd.RemoveObjectCmd> {
constructor(app: App, options: Cmd.RemoveObjectCmd) {
super(app, options);
}

public execute(): void {
if (!this.app || !this.options?.object) {
return;
}
this.app.canvas.remove(this.options.object);
}

public undo(): void {
if (!this.app || !this.options?.object) {
return;
}
this.app.canvas.add(this.options.object);
}
}

export default RemoveObjectCmd;
29 changes: 23 additions & 6 deletions packages/plugin-history/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { App, BaseFabricObject, Plugin } from '@pictode/core';
import './methods';

import { BaseCmd } from './commands/base';
import { AddObjectCmd } from './commands';
import { AddObjectCmd, RemoveObjectCmd } from './commands';
import { History } from './history';
import { Cmd, Options } from './types';

Expand All @@ -19,20 +19,23 @@ export class HistoryPlugin implements Plugin {

constructor(options?: Options) {
this.history = new History(undefined, options);
(['onObjectAdded'] as (keyof this)[]).forEach((method) => {
method = method as keyof HistoryPlugin;
this[method] = (this[method] as Function).bind(this);
});
}

public install(app: App) {
this.app = app;
this.history.app = app;
this.app.canvas.on('object:added', ({ target }: { target: BaseFabricObject }) => {
if (this.app) {
this.history.execute(new AddObjectCmd(this.app, { object: target }));
}
});
this.app.canvas.on('object:added', this.onObjectAdded);
this.app.canvas.on('object:removed', this.onObjectRemove);
}

public dispose(): void {
this.history.dispose();
this.app?.canvas.off('object:added', this.onObjectAdded);
this.app?.canvas.off('object:added', this.onObjectRemove);
this.app?.emit('history:destroy', {
history: this,
});
Expand All @@ -49,6 +52,20 @@ export class HistoryPlugin implements Plugin {
public isEnabled(): boolean {
return this.history.enabled;
}

private onObjectAdded({ target }: { target: BaseFabricObject }) {
if (!this.app) {
return;
}
this.history.execute(new AddObjectCmd(this.app, { object: target }));
}

private onObjectRemove({ target }: { target: BaseFabricObject }) {
if (!this.app) {
return;
}
this.history.execute(new RemoveObjectCmd(this.app, { object: target }));
}
}

export default HistoryPlugin;
4 changes: 4 additions & 0 deletions packages/plugin-history/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export namespace Cmd {
export interface AddObjectOptions {
object: BaseFabricObject;
}

export interface RemoveObjectCmd {
object: BaseFabricObject;
}
}

export interface Options {
Expand Down

0 comments on commit 0b5c7fd

Please sign in to comment.