diff --git a/src/components/app-root.tsx b/src/components/app-root.tsx index 9fda8c8..35fe809 100644 --- a/src/components/app-root.tsx +++ b/src/components/app-root.tsx @@ -5,6 +5,7 @@ import { RmgPage, RmgErrorBoundary, RmgThemeProvider, RmgWindow } from '@railmap import { useTranslation } from 'react-i18next'; import { useRootDispatch, useRootSelector } from '../redux'; import { closePaletteAppClip, onPaletteAppClipEmit } from '../redux/runtime/runtime-slice'; +import { useWindowSize } from '../util/hook'; import { ToolsPanel } from './panel/tools'; import SvgWrapper from './svg-wrapper'; import { RmpDetails } from './panel/details-rmp'; @@ -24,6 +25,8 @@ export default function AppRoot() { } = useRootSelector(state => state.runtime); const [isDetailsOpen, setDetailsOpen] = React.useState(false); const [openExport, setOpenExport] = React.useState(false); + const size = useWindowSize(); + const svgHeight = (((size.height ?? 720) - 40) * 3) / 5; return ( @@ -31,7 +34,7 @@ export default function AppRoot() { - + setDetailsOpen(false)} /> diff --git a/src/components/svg-wrapper.tsx b/src/components/svg-wrapper.tsx index 3bf78af..b9133b1 100644 --- a/src/components/svg-wrapper.tsx +++ b/src/components/svg-wrapper.tsx @@ -26,9 +26,7 @@ export default function SvgWrapper() { const { selected, mode, active, svgViewBoxMin, svgViewBoxZoom } = useRootSelector(state => state.runtime); const size = useWindowSize(); const svgWidth = (size.width ?? 720) - 40; - const svgHeight = ((size.height ?? 720) * 3) / 5; - const canvasScale = 1; - const color = param.color ? param.color.value ?? param.color.defaultValue : ['', '', '#000000', '#FFF']; + const svgHeight = (((size.height ?? 720) - 40) * 3) / 5; const [offset, setOffset] = React.useState({ x: 0, y: 0 }); const [svgViewBoxMinTmp, setSvgViewBoxMinTmp] = React.useState({ x: 0, y: 0 }); // temp copy of svgViewBoxMin @@ -129,14 +127,15 @@ export default function SvgWrapper() { const newTransform = updateTransformString(s.attrs['transform'] ?? '', dx, dy); return { ...s, attrs: { ...s.attrs, transform: newTransform } }; } else { - const [keyX, keyY] = s.type === SvgsType.Circle ? ['cx', 'cy'] : ['x', 'y']; - const newX = !Number.isNaN(Number(s.attrs[keyX])) - ? String(roundToNearestN(Number(s.attrs[keyX]) + dx, 1)) - : s.attrs[keyX]; - const newY = !Number.isNaN(Number(s.attrs[keyY])) - ? String(roundToNearestN(Number(s.attrs[keyY]) + dy, 1)) - : s.attrs[keyY]; - return { ...s, attrs: { ...s.attrs, [keyX]: newX, [keyY]: newY } }; + const newX = + !Number.isNaN(Number(s.attrs.x)) || s.attrs.x === undefined + ? String(roundToNearestN(Number(s.attrs.x ?? 0) + dx, 1)) + : s.attrs.x; + const newY = + !Number.isNaN(Number(s.attrs.y)) || s.attrs.y === undefined + ? String(roundToNearestN(Number(s.attrs.y ?? 0) + dy, 1)) + : s.attrs.y; + return { ...s, attrs: { ...s.attrs, x: newX, y: newY } }; } } else { if (s.children && s.children.length > 0) { @@ -176,22 +175,17 @@ export default function SvgWrapper() { id="rmp-style-gen-svg" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" - height={svgHeight * canvasScale} + width={svgWidth} + height={svgHeight} viewBox={`${svgViewBoxMin.x} ${svgViewBoxMin.y} ${(svgWidth * svgViewBoxZoom) / 100} ${ (svgHeight * svgViewBoxZoom) / 100 }`} colorInterpolationFilters="sRGB" - style={{ - ['--rmg-svg-width' as any]: svgWidth + 'px', - ['--rmg-svg-height' as any]: svgHeight + 'px', - ['--rmg-theme-colour' as any]: color[2], - ['--rmg-theme-fg' as any]: color[3], - userSelect: 'none', - touchAction: 'none', - }} + style={{ position: 'absolute', left: 40, userSelect: 'none', touchAction: 'none' }} onPointerDown={handleBackgroundDown} onPointerMove={handleBackgroundMove} onPointerUp={handleBackgroundUp} + tabIndex={0} > diff --git a/src/components/svgs/createSvgs.tsx b/src/components/svgs/createSvgs.tsx index 4346516..c4419e8 100644 --- a/src/components/svgs/createSvgs.tsx +++ b/src/components/svgs/createSvgs.tsx @@ -82,9 +82,9 @@ export const CreateSvgs = (props: CreateSvgsProps) => { ); const Children = supportsChildren(type) && svgsElem.children - ? svgsElem.children.map(s => ( + ? svgsElem.children.map((s, i) => ( { 'style' in newAttrs && typeof newAttrs.style === 'object' ? { ...(newAttrs.style as object), cursor: 'move' } : { cursor: 'move' }; - return React.createElement( - type, - { - ...newAttrs, - id: id, - key: id, - onPointerDown, - onPointerMove, - onPointerUp, - style: newStyle, - }, - ...Children + return ( + + {React.createElement( + type, + { + ...newAttrs, + id: id, + key: id, + x: 0, + y: 0, + onPointerDown, + onPointerMove, + onPointerUp, + style: newStyle, + }, + ...Children + )} + ); };