测回重做bug

This commit is contained in:
yyy 2025-02-28 10:19:15 +08:00
parent 9af2e7382c
commit 9e50046f72
2 changed files with 92 additions and 116 deletions

View File

@ -770,32 +770,31 @@ const deactivatedHandle = () => {}
//
const addEditHistory = () => {
// currentIndex
if (state.currentIndex !== state.allHistoryList.length - 1) {
state.allHistoryList = state.allHistoryList.splice(0, state.currentIndex)
state.currentIndex = state.allHistoryList.length
state.allHistoryList.push({
allMapPointInfo: JSON.parse(JSON.stringify(state.allMapPointInfo)),
mapRouteList: JSON.parse(JSON.stringify(state.mapRouteList))
})
} else {
state.allHistoryList.push({
allMapPointInfo: JSON.parse(JSON.stringify(state.allMapPointInfo)),
mapRouteList: JSON.parse(JSON.stringify(state.mapRouteList))
})
state.currentIndex++
if (state.currentIndex < state.allHistoryList.length - 1) {
state.allHistoryList.splice(state.currentIndex + 1)
}
state.allHistoryList.push({
allMapPointInfo: JSON.parse(JSON.stringify(state.allMapPointInfo)),
mapRouteList: JSON.parse(JSON.stringify(state.mapRouteList))
})
state.currentIndex = state.allHistoryList.length - 1
interfaceRefreshed.value = true
}
//
const backPreviousStep = () => {
console.log(state.currentIndex)
console.log(state.allMapPointInfo)
console.log(state.allHistoryList)
if (state.currentIndex > 0) {
state.currentIndex--
state.allMapPointInfo = state.allHistoryList[state.currentIndex]?.allMapPointInfo || []
state.mapRouteList = state.allHistoryList[state.currentIndex]?.mapRouteList || []
state.allMapPointInfo.splice(
0,
state.allMapPointInfo.length,
...JSON.parse(JSON.stringify(state.allHistoryList[state.currentIndex]?.allMapPointInfo))
)
state.mapRouteList.splice(
0,
state.mapRouteList.length,
...JSON.parse(JSON.stringify(state.allHistoryList[state.currentIndex]?.mapRouteList))
)
} else {
message.warning('没了老铁')
}
@ -804,8 +803,16 @@ const backPreviousStep = () => {
const backNextStep = () => {
if (state.currentIndex < state.allHistoryList.length - 1) {
state.currentIndex++
state.allMapPointInfo = state.allHistoryList[state.currentIndex]?.allMapPointInfo || []
state.mapRouteList = state.allHistoryList[state.currentIndex]?.mapRouteList || []
state.allMapPointInfo.splice(
0,
state.allMapPointInfo.length,
...JSON.parse(JSON.stringify(state.allHistoryList[state.currentIndex]?.allMapPointInfo))
)
state.mapRouteList.splice(
0,
state.mapRouteList.length,
...JSON.parse(JSON.stringify(state.allHistoryList[state.currentIndex]?.mapRouteList))
)
} else {
message.warning('没了老铁')
}

View File

@ -1,110 +1,79 @@
<template>
<div>
<!-- 选择单向或双向箭头 -->
<select v-model="isDoubleArrow">
<option value="false">单向箭头</option>
<option value="true">双向箭头</option>
</select>
<svg width="500" height="500">
<!-- 定义箭头样式 -->
<defs>
<!-- 正向箭头 -->
<marker
id="forward-arrow"
viewBox="0 0 10 10"
refX="10"
refY="5"
markerWidth="8"
markerHeight="8"
orient="auto"
>
<path d="M 0 0 L 10 5 L 0 10 z" fill="black" />
</marker>
<!-- 反向箭头 -->
<marker
id="backward-arrow"
viewBox="0 0 10 10"
refX="0"
refY="5"
markerWidth="8"
markerHeight="8"
orient="auto"
>
<path d="M 10 0 L 0 5 L 10 10 z" fill="black" />
</marker>
</defs>
<!-- 带有中间箭头的直线 -->
<line :x1="startX" :y1="startY" :x2="endX" :y2="endY" stroke="black" stroke-width="2" />
<path
:d="getLineMidArrowPath()"
stroke="none"
fill="black"
:marker-start="isDoubleArrow === 'true' ? 'url(#backward-arrow)' : ''"
:marker-end="isDoubleArrow === 'true' ? 'url(#forward-arrow)' : 'url(#forward-arrow)'"
/>
<!-- 带有中间箭头的贝塞尔曲线 -->
<path :d="bezierPath" stroke="black" stroke-width="2" fill="none" />
<path
:d="getBezierMidArrowPath()"
stroke="none"
fill="black"
:marker-start="isDoubleArrow === 'true' ? 'url(#backward-arrow)' : ''"
:marker-end="isDoubleArrow === 'true' ? 'url(#forward-arrow)' : 'url(#forward-arrow)'"
/>
</svg>
<ul>
<li v-for="(point, index) in points" :key="index">
点位 {{ index + 1 }}: ({{ point.x }}, {{ point.y }})
<button @click="movePoint(index)">移动</button>
<button @click="deletePoint(index)">删除</button>
</li>
</ul>
<button @click="addPoint">新增点位</button>
<button @click="undo" :disabled="historyIndex === 0">撤回</button>
<button @click="redo" :disabled="historyIndex === history.length - 1">重做</button>
</div>
</template>
<script setup>
import { ref } from 'vue'
import { reactive, ref } from 'vue'
// 线
const startX = ref(50)
const startY = ref(50)
const endX = ref(200)
const endY = ref(50)
//
const points = reactive([
{ x: 10, y: 20 },
{ x: 30, y: 40 }
])
// 线
const bezierPath = ref('M 50 200 C 100 250 200 250 200 200')
//
const history = reactive([[...points]])
//
const historyIndex = ref(0)
//
const isDoubleArrow = ref('false')
// 线
const getLineMidArrowPath = () => {
const midX = (startX.value + endX.value) / 2
const midY = (startY.value + endY.value) / 2
let dx = endX.value - startX.value
let dy = endY.value - startY.value
let length = Math.sqrt(dx * dx + dy * dy)
if (length === 0) {
return `M ${midX} ${midY} L ${midX} ${midY}`
}
const unitDx = dx / length
const unitDy = dy / length
const arrowLength = 1
const startXArrow = midX - unitDx * arrowLength
const startYArrow = midY - unitDy * arrowLength
const endXArrow = midX
const endYArrow = midY
return `M ${startXArrow} ${startYArrow} L ${endXArrow} ${endYArrow}`
//
const addPoint = () => {
const newPoint = { x: Math.random() * 100, y: Math.random() * 100 }
points.push(newPoint)
updateHistory()
}
// 线
const getBezierMidArrowPath = () => {
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
path.setAttribute('d', bezierPath.value)
const length = path.getTotalLength()
const midPoint = path.getPointAtLength(length / 2)
const prevPoint = path.getPointAtLength(length / 2 - 1)
//
const movePoint = (index) => {
points[index].x = Math.random() * 100
points[index].y = Math.random() * 100
updateHistory()
}
return `M ${prevPoint.x} ${prevPoint.y} L ${midPoint.x} ${midPoint.y}`
//
const deletePoint = (index) => {
points.splice(index, 1)
updateHistory()
}
//
const updateHistory = () => {
//
if (historyIndex.value < history.length - 1) {
history.splice(historyIndex.value + 1)
}
//
history.push([...points])
//
historyIndex.value++
}
//
const undo = () => {
if (historyIndex.value > 0) {
historyIndex.value--
//
points.splice(0, points.length, ...history[historyIndex.value])
}
}
//
const redo = () => {
if (historyIndex.value < history.length - 1) {
historyIndex.value++
//
points.splice(0, points.length, ...history[historyIndex.value])
}
}
</script>