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.
Add undo and redo to annotations. (#4482)
* Add undo & redo for annotations. (#4370, #4371) * Scale the drawing when saving history. (#4370) * Scale once and disable reset button. (#4370, #4371, #4453) * Replace switch with if-else. (#4370, #4371) * Prevent decimals from zoom or DPI scaling. (#4370, #4371) * Use more CSS and less svg files. (#4370, #4371)
- Loading branch information
1 parent
b4734d2
commit 8b6bd09
Showing
6 changed files
with
262 additions
and
29 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,118 @@ | ||
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.scale(devicePixelRatio, devicePixelRatio); | ||
copyContext.drawImage( | ||
canvas, | ||
0, 0, canvas.width, canvas.height, | ||
0, 0, area.width, area.height); | ||
return copy; | ||
} | ||
|
||
copy.width = area.width * devicePixelRatio; | ||
copy.height = area.height * devicePixelRatio; | ||
const copyContext = copy.getContext("2d"); | ||
copyContext.scale(devicePixelRatio, devicePixelRatio); | ||
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.