|
1 | 1 | import { Button, Card, NonIdealState, Spinner } from '@blueprintjs/core'
|
2 | 2 |
|
3 | 3 | import clsx from 'clsx'
|
| 4 | +import { clamp } from 'lodash-es' |
4 | 5 | import { useCallback, useEffect, useState } from 'react'
|
5 | 6 | import { createPortal } from 'react-dom'
|
6 | 7 | import { Rnd, RndResizeCallback } from 'react-rnd'
|
| 8 | +import { useWindowSize } from 'react-use' |
7 | 9 |
|
8 | 10 | import { sendMessage, useMessage } from '../../../utils/messenger'
|
9 | 11 | import { useLazyStorage } from '../../../utils/useLazyStorage'
|
@@ -57,15 +59,15 @@ export function FloatingMap() {
|
57 | 59 | (savedValue, defaultValue) => ({ ...defaultValue, ...savedValue }),
|
58 | 60 | )
|
59 | 61 |
|
| 62 | + const { width: windowWidth, height: windowHeight } = useWindowSize() |
| 63 | + |
60 | 64 | useEffect(() => {
|
61 |
| - if (config.show) { |
62 |
| - setConfig({ |
63 |
| - ...config, |
64 |
| - x: Math.max(0, Math.min(config.x, window.innerWidth - config.width)), |
65 |
| - y: Math.max(0, Math.min(config.y, window.innerHeight - config.height)), |
66 |
| - }) |
67 |
| - } |
68 |
| - }, [config.show]) |
| 65 | + setConfig((cfg) => ({ |
| 66 | + ...cfg, |
| 67 | + x: clamp(cfg.x, 0, windowWidth - cfg.width), |
| 68 | + y: clamp(cfg.y, 0, windowHeight - cfg.height), |
| 69 | + })) |
| 70 | + }, [windowWidth, windowHeight]) |
69 | 71 |
|
70 | 72 | const [iframeWindow, setIframeWindow] = useState<Window | null | undefined>()
|
71 | 73 | const [mapStatus, setMapStatus] = useState(MapStatus.Loading)
|
@@ -162,6 +164,7 @@ export function FloatingMap() {
|
162 | 164 | lockAspectRatio={ASPECT_RATIO}
|
163 | 165 | lockAspectRatioExtraHeight={HEADER_HEIGHT}
|
164 | 166 | default={config}
|
| 167 | + position={config} |
165 | 168 | onDragStart={onDragStartHandler}
|
166 | 169 | onDragStop={onDragStopHandler}
|
167 | 170 | onResizeStart={onResizeStartHandler}
|
|
0 commit comments