This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
10 changed files
with
268 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
const { Selection } = require("../../../shared/selection"); | ||
|
||
exports.EditorHistory = class { | ||
constructor(devicePixelRatio) { | ||
this.beforeEdits = []; | ||
this.afterEdits = []; | ||
this.devicePixelRatio = devicePixelRatio; | ||
} | ||
|
||
push(canvas, area, recordType) { | ||
const record = new EditRecord( | ||
canvas, | ||
area, | ||
this.devicePixelRatio, | ||
recordType | ||
); | ||
this.beforeEdits.push(record); | ||
this.afterEdits = []; | ||
} | ||
|
||
pushDiff(canvas, area) { | ||
this.push(canvas, area, RecordType.DIFF); | ||
} | ||
|
||
pushFrame(canvas, area) { | ||
this.push(canvas, area, RecordType.FRAME); | ||
} | ||
|
||
canUndo() { | ||
return !!this.beforeEdits.length; | ||
} | ||
|
||
undo(canvasBeforeUndo) { | ||
if (!this.canUndo()) { | ||
return null; | ||
} | ||
|
||
return this._replay(canvasBeforeUndo, this.beforeEdits, this.afterEdits); | ||
} | ||
|
||
canRedo() { | ||
return !!this.afterEdits.length; | ||
} | ||
|
||
redo(canvasBeforeRedo) { | ||
if (!this.canRedo()) { | ||
return null; | ||
} | ||
|
||
return this._replay(canvasBeforeRedo, this.afterEdits, this.beforeEdits); | ||
} | ||
|
||
_replay(canvasBeforeChange, from, to) { | ||
const fromRecord = from.pop(); | ||
|
||
let area = fromRecord.area; | ||
if (fromRecord.recordType === RecordType.FRAME) { | ||
area = new Selection( | ||
0, 0, | ||
parseInt(canvasBeforeChange.style.width, 10), | ||
parseInt(canvasBeforeChange.style.height, 10) | ||
); | ||
} | ||
|
||
const toRecord = new EditRecord( | ||
canvasBeforeChange, | ||
area, | ||
this.devicePixelRatio, | ||
fromRecord.recordType | ||
); | ||
|
||
to.push(toRecord); | ||
|
||
return fromRecord; | ||
} | ||
}; | ||
|
||
class EditRecord { | ||
constructor(canvas, area, devicePixelRatio, recordType) { | ||
this.area = area; | ||
this.recordType = recordType; | ||
this.canvas = this.captureCanvas(canvas, area, devicePixelRatio, recordType); | ||
} | ||
|
||
captureCanvas(canvas, area, devicePixelRatio, recordType) { | ||
const copy = document.createElement("canvas"); | ||
|
||
if (recordType === RecordType.FRAME) { | ||
copy.width = canvas.width; | ||
copy.height = canvas.height; | ||
const copyContext = copy.getContext("2d"); | ||
copyContext.drawImage(canvas, 0, 0); | ||
return copy; | ||
} | ||
|
||
copy.width = area.width; | ||
copy.height = area.height; | ||
const copyContext = copy.getContext("2d"); | ||
copyContext.drawImage( | ||
canvas, | ||
area.left * devicePixelRatio, | ||
area.top * devicePixelRatio, | ||
area.width * devicePixelRatio, | ||
area.height * devicePixelRatio, | ||
0, 0, area.width, area.height | ||
); | ||
|
||
return copy; | ||
} | ||
} | ||
|
||
const RecordType = { DIFF: 0, FRAME: 1 }; | ||
exports.RecordType = RecordType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.