diff --git a/package.json b/package.json index 2b72a10..9f8d8fd 100644 --- a/package.json +++ b/package.json @@ -88,6 +88,7 @@ "@types/file-saver": "^2.0.1", "@types/node": "^14.6.2", "@types/react-color": "^3.0.4", + "@types/react-grid-layout": "^1.1.0", "@types/redux-logger": "^3.0.8", "@typescript-eslint/eslint-plugin": "4.1.1", "@typescript-eslint/parser": "4.1.1", diff --git a/src/pages/editor/preview.js b/src/pages/editor/preview.js deleted file mode 100644 index ebbb69e..0000000 --- a/src/pages/editor/preview.js +++ /dev/null @@ -1,79 +0,0 @@ -import React, { memo, useEffect, useState } from 'react'; -import GridLayout from 'react-grid-layout'; -import DynamicEngine from 'components/DynamicEngine'; -import req from '@/utils/req'; -import styles from './index.less'; - -const isMac = navigator.platform.indexOf('Mac') === 0; - -const pcStyle = { - width: isMac ? 395 : 412, - margin: '20px auto', - border: '10px solid #000', - borderRadius: '20px', - height: '684px', - overflow: 'auto', -}; - -const PreviewPage = memo(props => { - const [pointData, setPointData] = useState(() => { - let pointDataStr = localStorage.getItem('pointData'); - let points; - - try { - points = JSON.parse(pointDataStr) || []; - } catch (err) { - points = []; - } - return points.map(item => ({ - ...item, - point: { ...item.point, isDraggable: false, isResizable: false }, - })); - }); - - const vw = window.innerWidth; - - useEffect(() => { - const { tid } = props.location.query; - let timer = null; - req - .get('/visible/preview/get', { params: { tid } }) - .then(res => { - setPointData( - res.map(item => ({ - ...item, - point: { ...item.point, isDraggable: false, isResizable: false }, - })), - ); - }) - .catch(err => { - timer = setTimeout(() => { - window.close(); - }, 3000); - }); - return () => { - window.clearTimeout(timer); - }; - }, []); - - return ( -
800 ? pcStyle : {}}> - 800 ? 375 : vw} - margin={[0, 0]} - id="xx" - > - {pointData.map(value => ( -
- -
- ))} -
-
- ); -}); - -export default PreviewPage; diff --git a/src/pages/editor/preview.tsx b/src/pages/editor/preview.tsx new file mode 100644 index 0000000..26e72a1 --- /dev/null +++ b/src/pages/editor/preview.tsx @@ -0,0 +1,87 @@ +import React, { memo, useEffect, useMemo, useRef, useState } from 'react'; +import GridLayout from 'react-grid-layout'; +import DynamicEngine from 'components/DynamicEngine'; +import req from '@/utils/req'; +import styles from './index.less'; +import { useGetScrollBarWidth } from '@/utils/tool'; +import { LocationDescriptorObject } from 'history-with-query'; + +const isMac = navigator.platform.indexOf('Mac') === 0; + +interface PreviewPageProps { + location: LocationDescriptorObject; +} +interface PointDataItem { + id: string; + item: Record; + point: Record; +} + +const PreviewPage = memo((props: PreviewPageProps) => { + const [pointData, setPointData] = useState(() => { + let pointDataStr = localStorage.getItem('pointData'); + let points; + + try { + points = JSON.parse(pointDataStr!) || []; + } catch (err) { + points = []; + } + return points.map((item: PointDataItem) => ({ + ...item, + point: { ...item.point, isDraggable: false, isResizable: false }, + })); + }); + + const vw = window.innerWidth; + + useEffect(() => { + const tid = props.location.query?.tid; + req + .get('/visible/preview/get', { params: { tid } }) + .then(res => { + setPointData( + res.map(item => ({ + ...item, + point: { ...item.point, isDraggable: false, isResizable: false }, + })), + ); + }) + .catch(err => { + console.error(err); + }); + }, [props.location.query]); + + const ref = useRef(null); + const width = useGetScrollBarWidth(ref); + const pcStyle = useMemo(() => { + return { + width: isMac ? 395 : 375 + width, + margin: '20px auto', + border: '10px solid #000', + borderRadius: '20px', + height: '684px', + overflow: 'auto', + }; + }, [width]); + + return ( +
800 ? pcStyle : {}}> + 800 ? 375 : vw} + margin={[0, 0]} + > + {pointData.map((value: PointDataItem) => ( +
+ +
+ ))} +
+
+ ); +}); + +export default PreviewPage; diff --git a/src/utils/tool.ts b/src/utils/tool.ts index 1652f36..f4711cb 100644 --- a/src/utils/tool.ts +++ b/src/utils/tool.ts @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react'; +import { RefObject, useEffect, useLayoutEffect, useState } from 'react'; import { RGBColor } from 'react-color'; // 生成uuid @@ -52,3 +52,15 @@ export function useGetRect() { }, []); return rect; } + +export function useGetScrollBarWidth(ref: RefObject) { + const [width, setWidth] = useState(0); + useLayoutEffect(() => { + if (ref.current) { + const diff = ref.current.offsetWidth - ref.current.clientWidth; + console.log(diff); + setWidth(diff); + } + }, [ref]); + return width; +} diff --git a/yarn.lock b/yarn.lock index a908038..2a92633 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2217,6 +2217,13 @@ dependencies: "@types/react" "*" +"@types/react-grid-layout@^1.1.0": + version "1.1.0" + resolved "https://registry.yarnpkg.com/@types/react-grid-layout/-/react-grid-layout-1.1.0.tgz#6592d391d64b9efebd5588a6a8fbbb9040c30998" + integrity sha512-lnt4YEXRHWimtbrghVSq+Ug7ByQl9cROM/xJewjLNeuqrT4tnKGe4tXX/3+/xYANG96L57F4+G0qf8jCgvDxWA== + dependencies: + "@types/react" "*" + "@types/react-helmet@^6.1.0": version "6.1.0" resolved "https://registry.yarnpkg.com/@types/react-helmet/-/react-helmet-6.1.0.tgz#af586ed685f4905e2adc7462d1d65ace52beee7a"