Browse Source

fix: context bar size

Peng Xiao 3 years ago
parent
commit
c69d2fa2e5

+ 2 - 17
tldraw/apps/tldraw-logseq/src/components/ContextBar/ContextBar.tsx

@@ -4,7 +4,6 @@ import {
   TLContextBarComponent,
   TLContextBarComponent,
   useApp,
   useApp,
   getContextBarTranslation,
   getContextBarTranslation,
-  useRendererContext,
 } from '@tldraw/react'
 } from '@tldraw/react'
 import { observer } from 'mobx-react-lite'
 import { observer } from 'mobx-react-lite'
 import type { TextShape, PolygonShape, Shape } from '~lib/shapes'
 import type { TextShape, PolygonShape, Shape } from '~lib/shapes'
@@ -12,11 +11,6 @@ import { NumberInput } from '~components/inputs/NumberInput'
 import { ColorInput } from '~components/inputs/ColorInput'
 import { ColorInput } from '~components/inputs/ColorInput'
 
 
 const _ContextBar: TLContextBarComponent<Shape> = ({ shapes, offsets }) => {
 const _ContextBar: TLContextBarComponent<Shape> = ({ shapes, offsets }) => {
-  const {
-    viewport: {
-      camera: { zoom },
-    },
-  } = useRendererContext()
   const app = useApp()
   const app = useApp()
   const rSize = React.useRef<[number, number] | null>(null)
   const rSize = React.useRef<[number, number] | null>(null)
   const rContextBar = React.useRef<HTMLDivElement>(null)
   const rContextBar = React.useRef<HTMLDivElement>(null)
@@ -64,18 +58,9 @@ const _ContextBar: TLContextBarComponent<Shape> = ({ shapes, offsets }) => {
     const elm = rContextBar.current
     const elm = rContextBar.current
     if (!elm) return
     if (!elm) return
     const size = rSize.current ?? [0, 0]
     const size = rSize.current ?? [0, 0]
-    const vpOffsets = {
-      left: offsets.left / zoom,
-      right: offsets.right / zoom,
-      top: offsets.top / zoom,
-      bottom: offsets.bottom / zoom,
-      width: offsets.width / zoom,
-      height: offsets.height / zoom,
-    }
-
-    const [x, y] = getContextBarTranslation(size, { ...vpOffsets, bottom: vpOffsets.bottom - 32 })
+    const [x, y] = getContextBarTranslation(size, { ...offsets, bottom: offsets.bottom - 32 })
     elm.style.setProperty('transform', `translateX(${x}px) translateY(${y}px)`)
     elm.style.setProperty('transform', `translateX(${x}px) translateY(${y}px)`)
-  }, [offsets, zoom])
+  }, [offsets])
 
 
   if (!app) return null
   if (!app) return null
 
 

+ 7 - 5
tldraw/packages/react/src/components/ui/SelectionDetail/SelectionDetail.tsx

@@ -9,13 +9,13 @@ import type { TLSelectionDetailProps } from '~types/component-props'
 import Vec from '@tldraw/vec'
 import Vec from '@tldraw/vec'
 
 
 export const SelectionDetail = observer(function SelectionDetail<S extends TLReactShape>({
 export const SelectionDetail = observer(function SelectionDetail<S extends TLReactShape>({
-  bounds,
+  scaledBounds,
   shapes,
   shapes,
   detail = 'size',
   detail = 'size',
   rotation = 0,
   rotation = 0,
 }: TLSelectionDetailProps<S>) {
 }: TLSelectionDetailProps<S>) {
   // This is the actual rotation of the bounding box, used to position the detail. Note that when rotating only one shape, the bounds rotation and the rotation shown in the detail will be the same; however, when rotating more than one shape, the bounding box will be axis-aligned, but the detail will show the angle that the bounds has been rotated by.
   // This is the actual rotation of the bounding box, used to position the detail. Note that when rotating only one shape, the bounds rotation and the rotation shown in the detail will be the same; however, when rotating more than one shape, the bounding box will be axis-aligned, but the detail will show the angle that the bounds has been rotated by.
-  const selectionRotation = shapes.length === 1 ? rotation : bounds.rotation ?? 0
+  const selectionRotation = shapes.length === 1 ? rotation : scaledBounds.rotation ?? 0
   const isFlipped = !(selectionRotation < TAU || selectionRotation > TAU * 3)
   const isFlipped = !(selectionRotation < TAU || selectionRotation > TAU * 3)
   const isLine = shapes.length === 1 && shapes[0].type === 'line'
   const isLine = shapes.length === 1 && shapes[0].type === 'line'
 
 
@@ -25,8 +25,10 @@ export const SelectionDetail = observer(function SelectionDetail<S extends TLRea
         className="tl-bounds-detail"
         className="tl-bounds-detail"
         style={{
         style={{
           transform: isFlipped
           transform: isFlipped
-            ? `rotate(${Math.PI + selectionRotation}rad) translateY(${bounds.height / 2 + 32}px)`
-            : `rotate(${selectionRotation}rad) translateY(${bounds.height / 2 + 24}px)`,
+            ? `rotate(${Math.PI + selectionRotation}rad) translateY(${
+                scaledBounds.height / 2 + 32
+              }px)`
+            : `rotate(${selectionRotation}rad) translateY(${scaledBounds.height / 2 + 24}px)`,
           padding: '2px 3px',
           padding: '2px 3px',
           borderRadius: '1px',
           borderRadius: '1px',
         }}
         }}
@@ -37,7 +39,7 @@ export const SelectionDetail = observer(function SelectionDetail<S extends TLRea
               shapes[0].props.handles!.end.point
               shapes[0].props.handles!.end.point
             ).toFixed()}`
             ).toFixed()}`
           : detail === 'size'
           : detail === 'size'
-          ? `${bounds.width.toFixed()} × ${bounds.height.toFixed()}`
+          ? `${scaledBounds.width.toFixed()} × ${scaledBounds.height.toFixed()}`
           : `∠${GeomUtils.radiansToDegrees(GeomUtils.clampRadians(rotation)).toFixed()}°`}
           : `∠${GeomUtils.radiansToDegrees(GeomUtils.clampRadians(rotation)).toFixed()}°`}
       </div>
       </div>
     </HTMLContainer>
     </HTMLContainer>

+ 2 - 1
tldraw/packages/react/src/hooks/useCounterScaledPosition.tsx

@@ -12,7 +12,8 @@ export function useCounterScaledPosition(
     const elm = ref.current!
     const elm = ref.current!
     elm.style.transform = `translate(
     elm.style.transform = `translate(
         calc(${bounds.minX}px - var(--tl-padding)),
         calc(${bounds.minX}px - var(--tl-padding)),
-        calc(${bounds.minY}px - var(--tl-padding)))`
+        calc(${bounds.minY}px - var(--tl-padding)))
+        scale(var(--tl-scale))`
   }, [bounds.minX, bounds.minY, rotation, bounds.rotation])
   }, [bounds.minX, bounds.minY, rotation, bounds.rotation])
 
 
   React.useLayoutEffect(() => {
   React.useLayoutEffect(() => {