|
@@ -1,5 +1,5 @@
|
|
|
<template>
|
|
<template>
|
|
|
- <div class="tel-view-tem" :style="{ width: `${width}px`, height: `${height}px`, background: bgColor, position: 'relative', userSelect: 'none' }" @mousedown="onMouseDown">
|
|
|
|
|
|
|
+ <div class="tel-view-tem" :style="containerStyle" @mousedown="onMouseDown">
|
|
|
<!-- 拖动选区样式 -->
|
|
<!-- 拖动选区样式 -->
|
|
|
<div v-if="isDragging" class="drag-area" :style="dragAreaStyle"></div>
|
|
<div v-if="isDragging" class="drag-area" :style="dragAreaStyle"></div>
|
|
|
<slot></slot>
|
|
<slot></slot>
|
|
@@ -7,14 +7,15 @@
|
|
|
</template>
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
<script setup lang="ts">
|
|
|
-import { ref, computed } from 'vue';
|
|
|
|
|
|
|
+import { ref, computed, watch } from 'vue';
|
|
|
import { propTypes } from '@/utils/propTypes';
|
|
import { propTypes } from '@/utils/propTypes';
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
const props = defineProps({
|
|
|
width: propTypes.number.def(750),
|
|
width: propTypes.number.def(750),
|
|
|
- height: propTypes.number.def(1000),
|
|
|
|
|
|
|
+ minHeight: propTypes.number.def(1000),
|
|
|
bgColor: propTypes.string.def('#fff'),
|
|
bgColor: propTypes.string.def('#fff'),
|
|
|
- enableDraw: propTypes.bool.def(true) // 新增开关属性
|
|
|
|
|
|
|
+ enableDraw: propTypes.bool.def(true),
|
|
|
|
|
+ bgSrc: propTypes.string.def(''),
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
const emit = defineEmits(['selectArea']);
|
|
const emit = defineEmits(['selectArea']);
|
|
@@ -23,7 +24,48 @@ const isDragging = ref(false);
|
|
|
const startPoint = ref({ x: 0, y: 0 });
|
|
const startPoint = ref({ x: 0, y: 0 });
|
|
|
const endPoint = ref({ x: 0, y: 0 });
|
|
const endPoint = ref({ x: 0, y: 0 });
|
|
|
|
|
|
|
|
-const dragAreaStyle = computed(() => {
|
|
|
|
|
|
|
+const imgHeight = ref(props.minHeight);
|
|
|
|
|
+const imgWidth = ref(props.width);
|
|
|
|
|
+
|
|
|
|
|
+watch(() => props.bgSrc, (val) => {
|
|
|
|
|
+ if (val) {
|
|
|
|
|
+ const img = new window.Image();
|
|
|
|
|
+ img.src = val;
|
|
|
|
|
+ img.onload = () => {
|
|
|
|
|
+ imgHeight.value = img.height;
|
|
|
|
|
+ imgWidth.value = img.width;
|
|
|
|
|
+ };
|
|
|
|
|
+ } else {
|
|
|
|
|
+ imgHeight.value = props.minHeight;
|
|
|
|
|
+ imgWidth.value = props.width;
|
|
|
|
|
+ }
|
|
|
|
|
+}, { immediate: true });
|
|
|
|
|
+
|
|
|
|
|
+const containerStyle: any = computed(() => {
|
|
|
|
|
+ if (props.bgSrc) {
|
|
|
|
|
+ return {
|
|
|
|
|
+ width: props.width ? `${props.width}px` : '100%',
|
|
|
|
|
+ height: `${imgHeight.value}px`,
|
|
|
|
|
+ backgroundImage: `url(${props.bgSrc})`,
|
|
|
|
|
+ backgroundSize: 'contain',
|
|
|
|
|
+ backgroundRepeat: 'no-repeat',
|
|
|
|
|
+ backgroundPosition: 'center',
|
|
|
|
|
+ position: 'relative',
|
|
|
|
|
+ userSelect: 'none',
|
|
|
|
|
+ minHeight: `${imgHeight.value}px`,
|
|
|
|
|
+ backgroundColor: props.bgColor,
|
|
|
|
|
+ };
|
|
|
|
|
+ }
|
|
|
|
|
+ return {
|
|
|
|
|
+ width: `${props.width}px`,
|
|
|
|
|
+ minHeight: `${props.minHeight}px`,
|
|
|
|
|
+ background: props.bgColor,
|
|
|
|
|
+ position: 'relative',
|
|
|
|
|
+ userSelect: 'none',
|
|
|
|
|
+ };
|
|
|
|
|
+});
|
|
|
|
|
+
|
|
|
|
|
+const dragAreaStyle: any = computed(() => {
|
|
|
const x = Math.min(startPoint.value.x, endPoint.value.x);
|
|
const x = Math.min(startPoint.value.x, endPoint.value.x);
|
|
|
const y = Math.min(startPoint.value.y, endPoint.value.y);
|
|
const y = Math.min(startPoint.value.y, endPoint.value.y);
|
|
|
const w = Math.abs(endPoint.value.x - startPoint.value.x);
|
|
const w = Math.abs(endPoint.value.x - startPoint.value.x);
|
|
@@ -42,7 +84,7 @@ const dragAreaStyle = computed(() => {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
function onMouseDown(e: MouseEvent) {
|
|
function onMouseDown(e: MouseEvent) {
|
|
|
- if (!props.enableDraw) return; // 根据开关属性决定是否允许绘制
|
|
|
|
|
|
|
+ if (!props.enableDraw) return;
|
|
|
if (e.button !== 0) return;
|
|
if (e.button !== 0) return;
|
|
|
const rect = (e.target as HTMLElement).getBoundingClientRect();
|
|
const rect = (e.target as HTMLElement).getBoundingClientRect();
|
|
|
startPoint.value = {
|
|
startPoint.value = {
|
|
@@ -91,6 +133,7 @@ function onMouseUp() {
|
|
|
position: relative;
|
|
position: relative;
|
|
|
}
|
|
}
|
|
|
.drag-area {
|
|
.drag-area {
|
|
|
|
|
+ z-index: 100;
|
|
|
transition: none;
|
|
transition: none;
|
|
|
}
|
|
}
|
|
|
</style>
|
|
</style>
|