Skip to content

Commit

Permalink
Merge pull request #58 from Shivamc489/master
Browse files Browse the repository at this point in the history
feat: added a way to exclude objects from saving in history
  • Loading branch information
alimozdemir committed Jun 1, 2024
2 parents 568c821 + 4760f95 commit 0aee731
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ canvas.undo();
canvas.redo();
```

## Excluding Objects
Objects can be excluded from history by setting the excludeFromExport property to true on the object. This prevents any history records for actions involving this object.

```javascript
const text = new fabric.Text('Hello', {
excludeFromExport: true // This object will not be recorded in history.
});
canvas.add(text);
```
Caution: doing this will also exclude this object from exporting to JSON/Object.

# Example (only for demo purposes)

```html
Expand Down
21 changes: 11 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ fabric.Canvas.prototype._historyNext = function () {
*/
fabric.Canvas.prototype._historyEvents = function () {
return {
'object:added': this._historySaveAction,
'object:removed': this._historySaveAction,
'object:modified': this._historySaveAction,
'object:skewing': this._historySaveAction,
'object:added': (e) => this._historySaveAction(e),
'object:removed': (e) => this._historySaveAction(e),
'object:modified': (e) => this._historySaveAction(e),
'object:skewing': (e) => this._historySaveAction(e),
};
};

Expand All @@ -61,13 +61,14 @@ fabric.Canvas.prototype._historyDispose = function () {
/**
* It pushes the state of the canvas into history stack
*/
fabric.Canvas.prototype._historySaveAction = function () {
fabric.Canvas.prototype._historySaveAction = function (e) {
if (this.historyProcessing) return;

const json = this.historyNextState;
this.historyUndo.push(json);
this.historyNextState = this._historyNext();
this.fire('history:append', { json: json });
if (!e || (e.target && !e.target.excludeFromExport)) {
const json = this._historyNext();
this.historyUndo.push(json);
this.historyNextState = this._historyNext();
this.fire('history:append', { json: json });
}
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0aee731

Please sign in to comment.