From 47b97d0dbb649237cb3415a22604677995ba9b72 Mon Sep 17 00:00:00 2001 From: kitsuyui Date: Sat, 4 May 2024 18:01:06 +0900 Subject: [PATCH 1/3] refurb: treemap - Remove unused codes --- packages/treemap/src/treemap.tsx | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/packages/treemap/src/treemap.tsx b/packages/treemap/src/treemap.tsx index 84521ab7..d2d2eefa 100644 --- a/packages/treemap/src/treemap.tsx +++ b/packages/treemap/src/treemap.tsx @@ -68,19 +68,6 @@ const previousBoustrophedonDirection = (rect1: Rect, rect2: Rect): Direction => return nextBoustrophedonDirection(rect2, rect1) } -const invertDirection = (direction: Direction): Direction => { - switch (direction) { - case 'right': - return 'left' - case 'down': - return 'up' - case 'left': - return 'right' - case 'up': - return 'down' - } -} - const rectsToRectInfos = (rects: Rect[]): RectInfo[] => { const rectInfos: RectInfo[] = rects.map((rect, index) => ({ ...rect, @@ -111,6 +98,9 @@ const rectsToRectInfos = (rects: Rect[]): RectInfo[] => { } +const DEFAULT_ASPECT_RATIO = 16 / 9 + + export const Treemap = (props: { weightedItems: WeightedItem[] verticalFirst?: boolean @@ -120,7 +110,7 @@ export const Treemap = (props: { const [ref, { width, height }] = useMeasure() const { weightedItems } = props const verticalFirst = props.verticalFirst ?? true - const aspectRatio = props.aspectRatio ?? 16 / 9 + const aspectRatio = props.aspectRatio ?? DEFAULT_ASPECT_RATIO const boustrophedon = props.boustrophedon ?? false const [inAreas, setInAreas] = useState([]) const [dividing, setDividing] = useState< From a51ffee41261413cbd789cb4510462133718aa54 Mon Sep 17 00:00:00 2001 From: kitsuyui Date: Sat, 4 May 2024 18:05:36 +0900 Subject: [PATCH 2/3] Add key --- packages/treemap/src/treemap.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/treemap/src/treemap.tsx b/packages/treemap/src/treemap.tsx index d2d2eefa..cdaa639c 100644 --- a/packages/treemap/src/treemap.tsx +++ b/packages/treemap/src/treemap.tsx @@ -186,6 +186,7 @@ const TreemapByRect = (props: { items: RectItem[] }) => { const { x, y, w, h, index } = rect return (
Date: Sat, 4 May 2024 18:09:23 +0900 Subject: [PATCH 3/3] Don't use async import --- packages/treemap/src/treemap.tsx | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/packages/treemap/src/treemap.tsx b/packages/treemap/src/treemap.tsx index cdaa639c..dd0ee631 100644 --- a/packages/treemap/src/treemap.tsx +++ b/packages/treemap/src/treemap.tsx @@ -1,5 +1,6 @@ import React, { useEffect, useState } from 'react' import { useMeasure } from 'react-use' +import { dividing } from '@kitsuyui/rectangle-dividing' interface Rect { x: number @@ -113,26 +114,11 @@ export const Treemap = (props: { const aspectRatio = props.aspectRatio ?? DEFAULT_ASPECT_RATIO const boustrophedon = props.boustrophedon ?? false const [inAreas, setInAreas] = useState([]) - const [dividing, setDividing] = useState< - typeof import('@kitsuyui/rectangle-dividing') | null - >(null) useEffect(() => { - ; (async () => { - if (dividing) return - const d = await import('@kitsuyui/rectangle-dividing') - setDividing(d) - })() - }) - - useEffect(() => { - if (!dividing) { - setInAreas([]) - return - } const rect: Rect = { x: 0, y: 0, w: width, h: height } const weights = new Float32Array(weightedItems.map(({ weight }) => weight)) - const ia: Rect[] = dividing.dividing( + const ia: Rect[] = dividing( rect, weights, aspectRatio, @@ -149,7 +135,6 @@ export const Treemap = (props: { aspectRatio, verticalFirst, boustrophedon, - dividing, ]) const rectInfos = rectsToRectInfos(inAreas)