diff --git a/src/components/SvgShape.vue b/src/components/SvgShape.vue new file mode 100644 index 0000000..7d8c926 --- /dev/null +++ b/src/components/SvgShape.vue @@ -0,0 +1,28 @@ + + + + + \ No newline at end of file diff --git a/src/components/VueDraw.vue b/src/components/VueDraw.vue index 34a9fb9..3307f81 100644 --- a/src/components/VueDraw.vue +++ b/src/components/VueDraw.vue @@ -2,19 +2,23 @@ import { useElementBounding, usePointerSwipe } from '@vueuse/core' import { computed, ref } from 'vue'; import type { Crop, Settings, Shape, Tool } from '../types' +import { getArrowId } from '@/utils/getArrowId'; +import SvgShape from './SvgShape.vue'; const emit = defineEmits<{ - (e: 'crop', crop: Crop | undefined): void + (e: 'crop', crop: Crop | undefined): void }>() -const settings = defineModel("settings", { default: () => ({ - tool: "line", - thickness: 5, - color: "red" -})}) +const settings = defineModel("settings", { + default: () => ({ + tool: "line", + thickness: 5, + color: "#c82d2d" + }) +}) -const history = defineModel("history", { default: []}) +const history = defineModel("history", { default: [] }) -const crop = defineModel("crop", { default: undefined}) +const crop = defineModel("crop", { default: undefined }) const container = ref() const svgRef = ref() @@ -53,7 +57,7 @@ const allShapes = computed(() => activeShape.value ? [ ] : history.value) -const { posStart, posEnd, distanceX, distanceY, isSwiping } = usePointerSwipe(svgRef, { +const { posStart, posEnd, distanceX, distanceY, isSwiping } = usePointerSwipe(svgRef, { threshold: 0, onSwipe(e) { if (settings.value.tool === 'crop') { @@ -75,7 +79,7 @@ const { posStart, posEnd, distanceX, distanceY, isSwiping } = usePointerSwipe(sv else { if (activeShape.value) { history.value.push(activeShape.value) - activeShape.value === undefined + activeShape.value = undefined } } } @@ -91,57 +95,38 @@ function setTool(tool: Tool) { settings.value.tool = tool } -function arrowId(shape: Shape) { - return `arrow-${shape.color.replace(/[^a-z0-9]/gi, '')}` +function undo() { + if (history.value.length) { + history.value = history.value.slice(0, -1) + } } -const arrowMarkers = computed(() => [...new Set( +const arrowMarkers = computed(() => allShapes.value .filter(shape => shape.type === 'arrow') - .filter((shape, index, self) => self.findIndex(s => arrowId(s) === arrowId(shape)) === index) // Unique matches only + .filter((shape, index, self) => self.findIndex(s => getArrowId(s) === getArrowId(shape)) === index) // Unique matches only .map(shape => ({ - id: arrowId(shape), + id: getArrowId(shape), color: shape.color - }) - ) -)]) + })) +) + @@ -162,16 +148,11 @@ const arrowMarkers = computed(() => [...new Set( position: relative; cursor: crosshair; } + svg { z-index: 100; } -rect, line { - stroke-linecap: round; - stroke-linejoin: round; - fill-opacity: 0; -} - .overlay { opacity: 0.5; fill: #020202; @@ -189,6 +170,6 @@ rect, line { stroke-linejoin: miter; stroke-opacity: 1; stroke-dasharray: 4, 8; - stroke-dashoffset:0; + stroke-dashoffset: 0; } diff --git a/src/utils/getArrowId.ts b/src/utils/getArrowId.ts new file mode 100644 index 0000000..3734372 --- /dev/null +++ b/src/utils/getArrowId.ts @@ -0,0 +1,5 @@ +import type { Shape } from "@/types"; + +export function getArrowId(shape: Shape) { + return `arrow-${shape.color.replace(/[^a-z0-9]/gi, '')}` +} \ No newline at end of file