Skip to content

Commit a8e6980

Browse files
committed
refactor(monkey): simplify newPage structure and add throttle utility
1 parent 7db3a80 commit a8e6980

17 files changed

Lines changed: 364 additions & 533 deletions

File tree

‎src/monkey/copymanga-enhance/scripts/detail/newPage/component/App/style.css‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
:root {
22
--header-height: 30px;
33
--body-height: calc(100dvh - var(--header-height));
4-
--image-group-padding: calc(var(--body-height) / 6 * 5);
54
}
65

76
#app {

‎src/monkey/copymanga-enhance/scripts/detail/newPage/component/AppBody/index.ts‎

Lines changed: 16 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ import GM_addStyle from "../../../../../../polyfill/GM_addStyle";
22
import cc from 'classcat'
33
import { ACTION_GRID_MAP, DirectionMode } from "../../constant";
44
import useDirectionMode from "../../hooks/useDirectionMode";
5-
import { defineComponent, onMounted, unref, h, ref, watch } from "../../vue";
5+
import { defineComponent, onMounted, unref, h } from "../../vue";
66
import css from './style.css'
7+
import WhitePage from '../WhitePage';
78
import useMouseGrid from "../../hooks/useMouseGrid";
8-
import ImageGroup from "../ImageGroup";
9-
import useTtbColumn from "../../hooks/useTtbColumn";
109
import useImageList from "../../hooks/useImageList";
11-
import useImageWidth from "../../hooks/useImageWidth";
12-
import debounce from "../../../../utils/debounce";
13-
import { useWindowSize } from "../../hooks/useWindowSize";
1410

1511
export default defineComponent({
1612
setup () {
@@ -20,31 +16,6 @@ export default defineComponent({
2016
const [imagesRef] = useImageList()
2117
const [directionModeRef] = useDirectionMode()
2218
const [mouseGridRef] = useMouseGrid()
23-
const [imageWidthRef] = useImageWidth()
24-
const [ttbColumnRef] = useTtbColumn()
25-
const { width: windowWidthRef } = useWindowSize()
26-
27-
const imageGroupHeightRef = ref(0)
28-
const imageGroupRef = ref<InstanceType<typeof ImageGroup> | null>(null)
29-
const refreshImageGroupHeight = debounce(() => {
30-
const list = Array.from<HTMLElement>(unref(imageGroupRef)?.$el.children ?? [])
31-
const height = list.reduce((acc, child) => acc + child.clientHeight, 0)
32-
imageGroupHeightRef.value = height
33-
}, 50)
34-
watch(
35-
[
36-
imagesRef,
37-
directionModeRef,
38-
ttbColumnRef,
39-
imageGroupRef,
40-
imageWidthRef,
41-
windowWidthRef,
42-
], () => {
43-
if (DirectionMode.TTB !== unref(directionModeRef)) return
44-
refreshImageGroupHeight()
45-
}
46-
)
47-
4819
return () => h(
4920
'div',
5021
{
@@ -60,60 +31,26 @@ export default defineComponent({
6031
{
6132
class: cc([
6233
'direction-wrapper',
63-
'max-h-(--body-height)',
64-
'flex flex-row flex-wrap',
65-
'overflow-x-hidden overflow-y-auto',
34+
'w-dvw',
35+
'flex flex-wrap justify-center',
36+
'snap-mandatory ltr:snap-y rtl:snap-y',
6637
]),
6738
},
68-
[
69-
h(
70-
ImageGroup,
71-
{
72-
class: cc([
73-
'ltr:hidden rtl:hidden',
74-
'overflow-hidden',
75-
{
76-
'hidden': unref(ttbColumnRef) === 1 || unref(ttbColumnRef) === 2,
77-
'basic-[33%]': unref(ttbColumnRef) === 3,
78-
}
79-
]),
80-
images: unref(imagesRef),
81-
style: `height: ${unref(imageGroupHeightRef)}px`,
82-
},
83-
),
84-
h(
85-
ImageGroup,
86-
{
87-
class: cc([
88-
'ltr:hidden rtl:hidden',
89-
'overflow-hidden',
90-
{
91-
'hidden': unref(ttbColumnRef) === 1,
92-
'basic-[50%]': unref(ttbColumnRef) === 2,
93-
'basic-[33%]': unref(ttbColumnRef) === 3,
94-
},
95-
]),
96-
images: unref(imagesRef),
97-
style: `height: ${unref(imageGroupHeightRef)}px`,
98-
},
99-
),
100-
h(
101-
ImageGroup,
39+
unref(imagesRef)
40+
.map(({ component, props }) => h(
41+
'div',
10242
{
10343
class: cc([
104-
{
105-
'w-dvw': DirectionMode.TTB !== unref(directionModeRef),
106-
'flex': unref(ttbColumnRef) === 1,
107-
'basic-[50%]': DirectionMode.TTB === unref(directionModeRef) && unref(ttbColumnRef) === 2,
108-
'basic-[33%]': DirectionMode.TTB === unref(directionModeRef) && unref(ttbColumnRef) === 3,
109-
},
44+
'wrapper',
45+
'ltr:h-(--body-height) rtl:h-(--body-height)',
46+
'ltr:snap-center rtl:snap-center',
47+
'flex',
48+
{ 'hidden': unref(directionModeRef) === DirectionMode.TTB && component === WhitePage },
11049
]),
111-
images: unref(imagesRef),
112-
ref: imageGroupRef,
50+
...Object.fromEntries(Object.entries(props).filter(([k]) => k.startsWith('data-')))
11351
},
114-
),
115-
],
116-
)
52+
[h(component, { ...props })],
53+
))),
11754
],
11855
)
11956
},

‎src/monkey/copymanga-enhance/scripts/detail/newPage/component/AppBody/style.css‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
.direction-wrapper > *:nth-child(1) {
2-
--image-group-padding-1: calc(var(--image-group-padding) * 2);
3-
padding-top: var(--image-group-padding-1);
1+
.direction-wrapper {
2+
max-height: var(--body-height);
3+
overflow-y: scroll;
44
}
55

6-
.direction-wrapper > *:nth-child(2) {
7-
--image-group-padding-2: calc(var(--image-group-padding) * 1);
8-
padding-top: var(--image-group-padding-2);
6+
.wrapper {
7+
flex-basis: 100%;
8+
justify-content: center;
99
}
1010

1111
/* #region 单列时 */

‎src/monkey/copymanga-enhance/scripts/detail/newPage/component/AppHeader/index.ts‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import useWhitePage from "../../hooks/useWhitePage";
99
import { defineComponent, onMounted, unref, computed, h, Fragment } from "../../vue";
1010
import css from './style.css'
1111
import useImageWidth from "../../hooks/useImageWidth";
12-
import useTtbColumn from "../../hooks/useTtbColumn";
1312

1413
const ImageWidths = [
1514
100, 90, 80, 70, 60, 50, 40, 30, 20,
@@ -40,27 +39,6 @@ export default defineComponent({
4039

4140
const [directionModeRef, setDirectionMode] = useDirectionMode()
4241
const [imageWidthRef, setImageWidth] = useImageWidth()
43-
const [ttbColumnRef, setTtbColumn] = useTtbColumn()
44-
const toggleTtbColumn = () => {
45-
const imageWidth = unref(imageWidthRef)
46-
const ttbColumn = unref(ttbColumnRef)
47-
if (ttbColumn === 1) {
48-
setTtbColumn(2)
49-
if (ImageWidths.indexOf(imageWidth) !== ImageWidths.length - 1) {
50-
setImageWidth(ImageWidths[ImageWidths.indexOf(imageWidth) + 1])
51-
}
52-
} else if (ttbColumn === 2) {
53-
setTtbColumn(3)
54-
if (ImageWidths.indexOf(imageWidth) !== ImageWidths.length - 1) {
55-
setImageWidth(ImageWidths[ImageWidths.indexOf(imageWidth) + 1])
56-
}
57-
} else {
58-
setTtbColumn(1)
59-
if (ImageWidths.indexOf(imageWidth) - 2 >= 0) {
60-
setImageWidth(ImageWidths[ImageWidths.indexOf(imageWidth) - 2])
61-
}
62-
}
63-
}
6442

6543
return () => h('div',
6644
{
@@ -135,9 +113,6 @@ export default defineComponent({
135113
return h('option', { value: v, selected: unref(imageWidthRef) === v }, `${v}%`)
136114
})) :
137115
h(Fragment),
138-
[DirectionMode.TTB].includes(unref(directionModeRef)) ?
139-
h('div', { class: 'text-white cursor-pointer', onClick: () => toggleTtbColumn() }, unref(ttbColumnRef) === 1 ? '默认模式' : unref(ttbColumnRef) === 2 ? '提前预览' : '超前预览') :
140-
h(Fragment),
141116
],
142117
),
143118
],

‎src/monkey/copymanga-enhance/scripts/detail/newPage/component/ImageGroup/index.ts‎

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
import { defineComponent } from "../../vue";
1+
import cc from "classcat";
2+
import { defineComponent, h } from "../../vue";
23

34
export default defineComponent({
4-
setup () {
5-
return {}
5+
props: {
6+
class: {
7+
type: String,
8+
default: () => undefined,
9+
},
10+
},
11+
setup (props) {
12+
return () => h(
13+
'div',
14+
{
15+
...props,
16+
class: cc([
17+
'white-page portrait size-px',
18+
props.class,
19+
]),
20+
},
21+
)
622
},
7-
template: '<div class="white-page portrait size-px ttb:hidden"></div>',
823
})

‎src/monkey/copymanga-enhance/scripts/detail/newPage/hooks/useImageList.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
import flow from "../../../../../utils/flow"
2-
import { ImageItem } from "../component/ImageGroup"
3-
import WhitePage from "../component/WhitePage"
2+
import WhitePage from '../component/WhitePage'
43
import { DirectionMode, PageType } from "../constant"
5-
import { onMounted, readonly, ref, unref, watch } from "../vue"
4+
import { defineComponent, onMounted, readonly, ref, unref, watch } from "../vue"
65
import useDirectionMode from "./useDirectionMode"
76
import useImageInfoMap from "./useImageInfoMap"
87
import useRawImageList from "./useRawImageList"
98
import useWhitePage from "./useWhitePage"
109
import Image from "../component/Image"
1110

11+
type ImageItem = {
12+
component: ReturnType<typeof defineComponent> | string;
13+
props: { key: string; pageType: PageType; } & Record<string, any>;
14+
}
15+
1216
export default function useImageList () {
13-
const imageListRef = useRawImageList()
17+
const rawImageListRef = useRawImageList()
1418
const infoMapRef = useImageInfoMap()
1519
const [whitePageRef] = useWhitePage()
1620
const [directionModeRef] = useDirectionMode()
17-
1821
onMounted(() => refresh())
1922
watch(
2023
[
2124
whitePageRef,
2225
directionModeRef,
23-
imageListRef,
26+
rawImageListRef,
2427
infoMapRef,
2528
],
2629
() => refresh()
@@ -141,11 +144,10 @@ export default function useImageList () {
141144
})
142145
})
143146
}
144-
145147
const imagesRef = ref<ImageItem[]>([])
146148
function refresh () {
147149
const list = flow(
148-
unref(imageListRef),
150+
unref(rawImageListRef),
149151
parseImages,
150152
addFirstWhitePage,
151153
injectWhitePages,
@@ -154,5 +156,5 @@ export default function useImageList () {
154156
imagesRef.value = list
155157
}
156158

157-
return [readonly(imagesRef)] as const
159+
return [readonly(imagesRef), refresh] as const
158160
}

‎src/monkey/copymanga-enhance/scripts/detail/newPage/hooks/useMouseWatcher.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import throttle from "../../../utils/throttle";
12
import { ACTION_GRID_MAP, GRID_COLUMN, GRID_ROW } from "../constant";
23
import { onMounted, unref } from "../vue";
34
import useMouseGrid from "./useMouseGrid";
@@ -22,12 +23,12 @@ export default function useMouseWatcher () {
2223
return row * COUNT_COLUMN + col + 1
2324
}
2425

25-
const handleGridMove = (info: { x: number, y: number }) => {
26+
const handleGridMove = throttle((info: { x: number, y: number }) => {
2627
const gridIndex = getGridIndex(info.x, info.y)
2728
const currentGridIndex = unref(mouseGridRef)
2829
if (gridIndex === currentGridIndex) return
2930
setMouseGrid(gridIndex)
30-
}
31+
}, 25)
3132

3233
const handleGridClick = (info: { x: number, y: number }) => {
3334
const gridIndex = getGridIndex(info.x, info.y)

‎src/monkey/copymanga-enhance/scripts/detail/newPage/hooks/useTtbColumn.ts‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)