Skip to content

Commit

Permalink
save history when pixel set
Browse files Browse the repository at this point in the history
  • Loading branch information
chetbox committed Dec 27, 2023
1 parent 899473d commit bf1db58
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions hosting/src/components/Canvas.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { Component, Prop, Vue } from 'vue-property-decorator';
import { Color, colorClass } from '../model/colors';
import { toGrid, emptyGrid } from '../model/canvas';
import database from '../database';
import firebase from 'firebase/app';
@Component
export default class Canvas extends Vue {
Expand All @@ -39,11 +40,22 @@ export default class Canvas extends Vue {
public setPixelColor(row: number, column: number, color: Color) {
const xOffsets = row.toString(2).padStart(this.data.canvasDepth, '0');
const yOffsets = column.toString(2).padStart(this.data.canvasDepth, '0');
let pixelRef = this.placeRef.child('canvas');
const canvasRef = this.placeRef.child('canvas');
let pixelRef = canvasRef;
for (let i = 0; i < this.data.canvasDepth; i++) {
pixelRef = pixelRef.child(xOffsets[i] + yOffsets[i]);
}
pixelRef.set(color);
// Save history
const historyRef = canvasRef.child('history');
const historyItemRef = historyRef.push();
historyItemRef.set({
x: row,
y: column,
value: color,
timestamp: firebase.database.ServerValue.TIMESTAMP,
});
}
protected mounted() {
Expand All @@ -54,7 +66,7 @@ export default class Canvas extends Vue {
this.placeRef.off('value', this.onCanvasUpdated);
}
private onCanvasUpdated(snapshot: firebase.default.database.DataSnapshot) {
private onCanvasUpdated(snapshot: firebase.database.DataSnapshot) {
if (!snapshot.exists()) {
console.warn('Canvas does not exist', this.canvasId);
return;
Expand Down

0 comments on commit bf1db58

Please sign in to comment.