Skip to content

Commit 7a81c32

Browse files
committed
fix: override unocss default style max-h-full (#58)
close #58
1 parent 8cbf2b5 commit 7a81c32

File tree

4 files changed

+157
-154
lines changed

4 files changed

+157
-154
lines changed

src/views/snapshot/MiniHoverImg.vue

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,24 @@
11
<script lang="ts" setup>
2-
const props = defineProps<{
3-
imgRef: HTMLElement;
4-
imgHover: boolean;
5-
screenshotUrl: string;
6-
hoverPosition: {
7-
ox: number;
8-
oy: number;
9-
};
10-
hoverPositionStyle?: {
11-
left: string;
12-
top: string;
13-
};
14-
boxHoverPosition?: {
15-
left: number;
16-
right: number;
17-
top: number;
18-
bottom: number;
19-
};
20-
boxHoverPerPosition?: {
21-
left: string;
22-
right: string;
23-
top: string;
24-
bottom: string;
25-
};
26-
}>();
2+
import { useSharedSnapshotHoverImg, useSnapshotStore } from './snapshot';
273
28-
const imgBounding = useElementBounding(computed(() => props.imgRef));
4+
const { screenshotUrl } = useSnapshotStore();
5+
const {
6+
imgHover,
7+
imgBounding,
8+
hoverPositionStyle,
9+
boxHoverPosition,
10+
hoverPosition,
11+
boxHoverPerPosition,
12+
} = useSharedSnapshotHoverImg();
2913
</script>
3014

3115
<template>
3216
<!-- Teleport 需要单独一个组件否则 hmr 错误 -->
33-
<Teleport to="body">
17+
<Teleport v-if="screenshotUrl" to="body">
3418
<!-- 渲染在外部防止被遮挡 -->
3519
<div
3620
v-show="imgHover"
21+
class="MiniHoverImg"
3722
:style="{
3823
left: imgBounding.right.value + 4 + 'px',
3924
top: imgBounding.top.value + 'px',
@@ -51,12 +36,12 @@ const imgBounding = useElementBounding(computed(() => props.imgRef));
5136
>
5237
<img
5338
:src="screenshotUrl"
39+
:style="hoverPositionStyle"
5440
object-contain
5541
absolute
5642
left-0
5743
top-0
58-
:style="hoverPositionStyle"
59-
w-1000px
44+
class="max-w-none!"
6045
/>
6146
<div
6247
absolute
Lines changed: 6 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,21 @@
11
<script setup lang="ts">
22
import dayjs from 'dayjs';
33
import MiniHoverImg from './MiniHoverImg.vue';
4-
import { toFixedNumber } from '@/utils/others';
5-
import type { ShallowRef } from 'vue';
6-
import { useSnapshotStore } from './snapshot';
4+
import { useSharedSnapshotHoverImg, useSnapshotStore } from './snapshot';
75
8-
const { updatePosition, focusNode, screenshotUrl } = useSnapshotStore();
9-
10-
const snapshot = useSnapshotStore().snapshot as ShallowRef<Snapshot>;
11-
12-
const imgRef = shallowRef<HTMLImageElement>();
13-
const imgLoadTime = shallowRef(false);
14-
15-
const clickImg = (ev: MouseEvent) => {
16-
const img = imgRef.value;
17-
if (!img) {
18-
return;
19-
}
20-
21-
const imgRect = img.getBoundingClientRect();
22-
23-
const innerHeight = (imgRect.width / img.naturalWidth) * img.naturalHeight;
24-
const offsetTop = (imgRect.height - innerHeight) / 2;
25-
26-
const x = ((ev.clientX - imgRect.left) / imgRect.width) * img.naturalWidth;
27-
const y =
28-
((ev.clientY - imgRect.top - offsetTop) / innerHeight) * img.naturalHeight;
29-
30-
updatePosition({ x, y });
31-
};
32-
33-
const percent = (n: number) => {
34-
return `${n * 100}%`;
35-
};
36-
37-
const imgSize = useElementSize(imgRef);
38-
39-
const positionStyle = computed(() => {
40-
const img = imgRef.value;
41-
42-
const attr = focusNode.value?.attr;
43-
if (!focusNode.value || !img || !attr || !imgLoadTime.value) {
44-
return ``;
45-
}
46-
const imgWidth = imgSize.width.value;
47-
const imgHeight = imgSize.height.value;
48-
const innerHeight = (imgWidth / img.naturalWidth) * img.naturalHeight;
49-
return {
50-
left: `calc(${percent(attr.left / img.naturalWidth)} - 2px)`,
51-
width: `calc(${percent(
52-
(attr.right - attr.left) / img.naturalWidth,
53-
)} + 2px)`,
54-
55-
top: `calc(${percent(
56-
((attr.top / img.naturalHeight) * innerHeight +
57-
(imgHeight - innerHeight) / 2) /
58-
imgHeight,
59-
)} - 2px)`,
60-
height: `calc(${percent(
61-
(((attr.bottom - attr.top) / img.naturalHeight) * innerHeight) /
62-
imgHeight,
63-
)} + 2px)`,
64-
};
65-
});
66-
const imgHover = shallowRef(false);
67-
const hoverPosition = shallowRef({ ox: 0, oy: 0 });
68-
const boxHoverPosition = computed(() => {
69-
const attr = focusNode.value?.attr;
70-
if (!attr) {
71-
return;
72-
}
73-
const { ox, oy } = hoverPosition.value;
74-
return {
75-
left: ox - attr.left,
76-
right: attr.right - ox,
77-
top: oy - attr.top,
78-
bottom: attr.bottom - oy,
79-
};
80-
});
81-
const boxHoverPerPosition = computed(() => {
82-
const attr = focusNode.value?.attr;
83-
if (!attr || !boxHoverPosition.value) {
84-
return;
85-
}
86-
if (attr.width <= 0 || attr.height <= 0) {
87-
return;
88-
}
89-
const { bottom, left, right, top } = boxHoverPosition.value;
90-
return {
91-
left: toFixedNumber(left / (right + left), 3),
92-
right: toFixedNumber(right / (right + left), 3),
93-
top: toFixedNumber(top / (top + bottom), 3),
94-
bottom: toFixedNumber(bottom / (top + bottom), 3),
95-
};
96-
});
97-
const hoverPositionStyle = shallowRef({ left: '0', top: '0' });
98-
99-
const imgMove = (ev: MouseEvent) => {
100-
const img = imgRef.value;
101-
if (!img) return;
102-
const imgRect = img.getBoundingClientRect();
103-
104-
const innerHeight = (imgRect.width / img.naturalWidth) * img.naturalHeight;
105-
const offsetTop = (imgRect.height - innerHeight) / 2;
106-
107-
const ox = ((ev.clientX - imgRect.left) / imgRect.width) * img.naturalWidth;
108-
const oy =
109-
((ev.clientY - imgRect.top - offsetTop) / innerHeight) * img.naturalHeight;
110-
hoverPosition.value = { ox, oy };
111-
hoverPositionStyle.value = {
112-
left: (-(ox - 0.1 * img.naturalWidth) / img.naturalWidth) * 1000 + 'px',
113-
top: (-(oy - 0.1 * img.naturalWidth) / img.naturalWidth) * 1000 + 'px',
114-
};
115-
};
6+
const { screenshotUrl, snapshot } = useSnapshotStore();
7+
const { clickImg, imgHover, imgMove, imgLoadTime, positionStyle, imgRef } =
8+
useSharedSnapshotHoverImg();
1169
</script>
11710

11811
<template>
11912
<div
120-
v-if="screenshotUrl"
13+
v-if="snapshot && screenshotUrl"
12114
flex
12215
flex-col
12316
relative
12417
h-full
12518
p-2px
126-
box-border
12719
overflow-hidden
12820
>
12921
<img
@@ -169,15 +61,6 @@ const imgMove = (ev: MouseEvent) => {
16961
{{ dayjs(snapshot.id).format('YYYY-MM-DD HH:mm:ss') }}
17062
</div>
17163
</div>
172-
<MiniHoverImg
173-
v-if="imgRef"
174-
:imgHover="imgHover"
175-
:imgRef="imgRef"
176-
:boxHoverPerPosition="boxHoverPerPosition"
177-
:boxHoverPosition="boxHoverPosition"
178-
:hoverPositionStyle="hoverPositionStyle"
179-
:hoverPosition="hoverPosition"
180-
:screenshotUrl="screenshotUrl"
181-
/>
64+
<MiniHoverImg v-if="imgRef" />
18265
</div>
18366
</template>

src/views/snapshot/snapshot.ts

Lines changed: 136 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
} from '@/utils/export';
66
import { gmOk } from '@/utils/gm';
77
import { findNodesByXy, getAppInfo, listToTree } from '@/utils/node';
8-
import { toInteger } from '@/utils/others';
8+
import { toFixedNumber, toInteger } from '@/utils/others';
99
import type { ResolvedSelector } from '@/utils/selector';
1010
import { screenshotStorage, snapshotStorage } from '@/utils/snapshot';
1111
import { useTask } from '@/utils/task';
@@ -206,3 +206,138 @@ export const useSnapshotStore = createSharedComposable(() => {
206206
showTrack,
207207
};
208208
});
209+
210+
export const useSharedSnapshotHoverImg = createSharedComposable(() => {
211+
const { updatePosition, focusNode } = useSnapshotStore();
212+
213+
const imgRef = shallowRef<HTMLImageElement>();
214+
const imgBounding = useElementBounding(imgRef);
215+
const imgLoadTime = shallowRef(false);
216+
217+
const clickImg = (ev: MouseEvent) => {
218+
const img = imgRef.value;
219+
if (!img) {
220+
return;
221+
}
222+
223+
const imgRect = img.getBoundingClientRect();
224+
225+
const innerHeight = (imgRect.width / img.naturalWidth) * img.naturalHeight;
226+
const offsetTop = (imgRect.height - innerHeight) / 2;
227+
228+
const x = ((ev.clientX - imgRect.left) / imgRect.width) * img.naturalWidth;
229+
const y =
230+
((ev.clientY - imgRect.top - offsetTop) / innerHeight) *
231+
img.naturalHeight;
232+
233+
updatePosition({ x, y });
234+
};
235+
236+
const percent = (n: number) => {
237+
return `${n * 100}%`;
238+
};
239+
240+
const imgSize = useElementSize(imgRef);
241+
242+
const positionStyle = computed(() => {
243+
const img = imgRef.value;
244+
245+
const attr = focusNode.value?.attr;
246+
if (!focusNode.value || !img || !attr || !imgLoadTime.value) {
247+
return ``;
248+
}
249+
const imgWidth = imgSize.width.value;
250+
const imgHeight = imgSize.height.value;
251+
const innerHeight = (imgWidth / img.naturalWidth) * img.naturalHeight;
252+
return {
253+
left: `calc(${percent(attr.left / img.naturalWidth)} - 2px)`,
254+
width: `calc(${percent(
255+
(attr.right - attr.left) / img.naturalWidth,
256+
)} + 2px)`,
257+
258+
top: `calc(${percent(
259+
((attr.top / img.naturalHeight) * innerHeight +
260+
(imgHeight - innerHeight) / 2) /
261+
imgHeight,
262+
)} - 2px)`,
263+
height: `calc(${percent(
264+
(((attr.bottom - attr.top) / img.naturalHeight) * innerHeight) /
265+
imgHeight,
266+
)} + 2px)`,
267+
};
268+
});
269+
const imgHover = shallowRef(false);
270+
const hoverPosition = shallowRef({ ox: 0, oy: 0 });
271+
const boxHoverPosition = computed(() => {
272+
const attr = focusNode.value?.attr;
273+
if (!attr) {
274+
return;
275+
}
276+
const { ox, oy } = hoverPosition.value;
277+
return {
278+
left: ox - attr.left,
279+
right: attr.right - ox,
280+
top: oy - attr.top,
281+
bottom: attr.bottom - oy,
282+
};
283+
});
284+
const boxHoverPerPosition = computed(() => {
285+
const attr = focusNode.value?.attr;
286+
if (!attr || !boxHoverPosition.value) {
287+
return;
288+
}
289+
if (attr.width <= 0 || attr.height <= 0) {
290+
return;
291+
}
292+
const { bottom, left, right, top } = boxHoverPosition.value;
293+
return {
294+
left: toFixedNumber(left / (right + left), 3),
295+
right: toFixedNumber(right / (right + left), 3),
296+
top: toFixedNumber(top / (top + bottom), 3),
297+
bottom: toFixedNumber(bottom / (top + bottom), 3),
298+
};
299+
});
300+
const hoverBgImgWidth = 1000;
301+
const hoverPositionStyle = shallowRef({
302+
left: '0',
303+
top: '0',
304+
width: hoverBgImgWidth + 'px',
305+
});
306+
307+
const imgMove = (ev: MouseEvent) => {
308+
const img = imgRef.value;
309+
if (!img) return;
310+
const imgRect = img.getBoundingClientRect();
311+
312+
const innerHeight = (imgRect.width / img.naturalWidth) * img.naturalHeight;
313+
const offsetTop = (imgRect.height - innerHeight) / 2;
314+
315+
const ox = ((ev.clientX - imgRect.left) / imgRect.width) * img.naturalWidth;
316+
const oy =
317+
((ev.clientY - imgRect.top - offsetTop) / innerHeight) *
318+
img.naturalHeight;
319+
hoverPosition.value = { ox, oy };
320+
hoverPositionStyle.value = {
321+
left:
322+
(-(ox - 0.1 * img.naturalWidth) / img.naturalWidth) * hoverBgImgWidth +
323+
'px',
324+
top:
325+
(-(oy - 0.1 * img.naturalWidth) / img.naturalWidth) * hoverBgImgWidth +
326+
'px',
327+
width: hoverBgImgWidth + 'px',
328+
};
329+
};
330+
return {
331+
imgRef,
332+
imgBounding,
333+
clickImg,
334+
positionStyle,
335+
imgHover,
336+
imgMove,
337+
hoverPositionStyle,
338+
boxHoverPerPosition,
339+
imgLoadTime,
340+
hoverPosition,
341+
boxHoverPosition,
342+
};
343+
});

uno.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ import {
66
} from 'unocss';
77

88
export default defineConfig({
9-
presets: [presetWind4({ preflight: 'on-demand' }), presetAttributify()],
9+
presets: [presetWind4(), presetAttributify()],
1010
transformers: [transformerAttributifyJsx()],
1111
});

0 commit comments

Comments
 (0)