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: added a way to exclude objects from saving in history #58

Merged
merged 3 commits into from
Jun 1, 2024
Merged
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
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.