<template>
<v-stage :config="stageSize">
<v-layer>
<v-text
:config="{
text: 'undo',
x: 10,
y: 10
}"
@click="handleUndo"
/>
<v-text
:config="{
text: 'redo',
x: 50,
y: 10
}"
@click="handleRedo"
/>
<v-rect
:config="{
x: position.x,
y: position.y,
width: 50,
height: 50,
fill: 'black',
draggable: true
}"
@dragend="handleDragEnd"
/>
</v-layer>
</v-stage>
</template>
<script setup>
import { ref, reactive } from 'vue';
const stageSize = {
width: window.innerWidth,
height: window.innerHeight
};
const position = reactive({ x: 20, y: 20 });
const history = ref([{ x: 20, y: 20 }]);
const historyStep = ref(0);
const handleUndo = () => {
if (historyStep.value === 0) {
return;
}
historyStep.value -= 1;
const previous = history.value[historyStep.value];
position.x = previous.x;
position.y = previous.y;
};
const handleRedo = () => {
if (historyStep.value === history.value.length - 1) {
return;
}
historyStep.value += 1;
const next = history.value[historyStep.value];
position.x = next.x;
position.y = next.y;
};
const handleDragEnd = (e) => {
history.value = history.value.slice(0, historyStep.value + 1);
const pos = {
x: e.target.x(),
y: e.target.y()
};
history.value = history.value.concat([pos]);
historyStep.value += 1;
position.x = pos.x;
position.y = pos.y;
};
</script>