From a70cbfa4d379788efa9e001115cbbfb56c424ea0 Mon Sep 17 00:00:00 2001 From: vec Date: Fri, 19 Jan 2024 03:17:03 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20=E5=A4=A7=E5=B9=85=E6=B6=88=E9=99=A4?= =?UTF-8?q?=E6=8A=96=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/components/Gallery/index.tsx | 2 +- front/src/components/Waterfall/index.tsx | 48 +++++++++++++----------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/front/src/components/Gallery/index.tsx b/front/src/components/Gallery/index.tsx index 1b0f97b..441176d 100644 --- a/front/src/components/Gallery/index.tsx +++ b/front/src/components/Gallery/index.tsx @@ -87,7 +87,7 @@ function computeGalleryWidthWithAutoColumnCount( const getLayoutConfigure = (gallery: Gallery): WaterfallLayoutConfigure => { const viewport_width = getViewportWidth() - if (viewport_width > 1200) { + if (viewport_width > 1900) { const box_width = 232 const column_gutter = 27 const padding_horizontal = 54 diff --git a/front/src/components/Waterfall/index.tsx b/front/src/components/Waterfall/index.tsx index c6219df..0489f5d 100644 --- a/front/src/components/Waterfall/index.tsx +++ b/front/src/components/Waterfall/index.tsx @@ -14,6 +14,7 @@ type Pos = { top: string left: string zIndex: number + col_count: number } type PhotoID = number type PosMap = Record @@ -80,13 +81,13 @@ export default (props: Props) => { top: `calc(${pos.top})`, left: `calc(${pos.left})`, zIndex: `calc(${pos.zIndex})`, - opacity: 1, + opacity: pos.col_count >= layout_configure.column_count ? 0 : 1, // transition: pos && 'left 382ms, top 382ms' } } else { return { opacity: 0 } } - }, [pos_map]) + }, [layout_configure.column_count, pos_map]) return (
{ - if (idx === min_height_index) { + return columns.map((col, col_count) => { + if (col_count === min_col) { const dim = dim_map[photo.id] if (dim) { const [ width, height ] = dim() - return [...col, { id: photo.id, width, height, photo_idx }] + return [...col, { id: photo.id, width, height, photo_idx}] } else { return col } @@ -571,12 +573,17 @@ function useLayout({ }, createPlainColumns(column_count)) }, []) - const [pos_map, refreshPosMap] = useSafeState({}) - const computePosMap = useCallback((waterfall_columns: Columns) => { + const [columns, refreshColumns] = useSafeState([]) + + const waterfall_height = useMemo( + () => computeWaterfallHeight(columns) + , [columns]) + + const pos_map = useMemo(() => { const init_pos: PosMap = {} - return waterfall_columns.reduce((column_pos_init, column, x) => { - const left = `(${box_width}px * ${x} + ${column_gutter}px * ${x})` + return columns.reduce((column_pos_init, column, col_count) => { + const left = `(${box_width}px * ${col_count} + ${column_gutter}px * ${col_count})` return column.reduce((pos_info, heightInfo, y) => { const h = computeColumnHeight(column.slice(0, y)) @@ -586,17 +593,15 @@ function useLayout({ top: `${h}px`, left, zIndex: h, + col_count, } } }, column_pos_init) }, init_pos) - }, [box_width, column_gutter]) - - const [ waterfall_height, refreshWaterfallHeight ] = useSafeState(0) - - const [columns, refreshColumns] = useSafeState([]) + }, [box_width, column_gutter, columns]) - const cacheID = (...vals: number[]) => vals.join('-') + const cacheID = (...vals: Array) => vals.join('-') + const genCacheID = useCallback(() => cacheID(box_type, box_width, column_count, column_gutter), [box_type, box_width, column_count, column_gutter]) const columns_cache = useRef>() @@ -604,14 +609,12 @@ function useLayout({ const applyNewLayout = useCallback((new_cols: Columns) => { prev_columns.current = { - id: cacheID(box_width, column_count, column_gutter), + id: genCacheID(), cols: new_cols } refreshColumns(new_cols) - refreshPosMap( computePosMap(new_cols) ) - refreshWaterfallHeight( computeWaterfallHeight(new_cols) ) refresh_signal.trigger() - }, [box_width, column_count, column_gutter, computePosMap, refreshColumns, refreshPosMap, refreshWaterfallHeight, refresh_signal]) + }, [genCacheID, refreshColumns, refresh_signal]) const refreshLayout = useCallback(() => { if (prev_columns.current === undefined) { @@ -657,7 +660,8 @@ function useLayout({ refreshLayout() }, [box_width, column_count, column_gutter, refreshLayout, refresh_signal]) - if (prev_columns.current?.id !== cacheID(box_width, column_count, column_gutter)) { + if (prev_columns.current?.id !== genCacheID()) { + // console.log(cacheID(box_type, box_width, column_count, column_gutter)) // refreshLayout() }