Browse Source

fix: onResetBounds should take zoom into consider

Peng Xiao 3 years ago
parent
commit
7ffdb48cc5

+ 1 - 1
tldraw/apps/tldraw-logseq/src/components/ContextBar/contextBarActionFactory.tsx

@@ -112,7 +112,7 @@ const ResetBoundsAction = observer(() => {
       type="button"
       onClick={() => {
         shapes.forEach(s => {
-          s.onResetBounds()
+          s.onResetBounds({ zoom: app.viewport.camera.zoom })
         })
         app.persist()
       }}

+ 3 - 4
tldraw/apps/tldraw-logseq/src/lib/shapes/HTMLShape.tsx

@@ -62,11 +62,10 @@ export class HTMLShape extends TLBoxShape<HTMLShapeProps> {
   onResetBounds = (info?: TLResetBoundsInfo) => {
     if (this.htmlAnchorRef.current) {
       const rect = this.htmlAnchorRef.current.getBoundingClientRect()
+      const [w, h] = Vec.div([rect.width, rect.height], info?.zoom ?? 1)
+      const clamp = (v: number) => Math.max(Math.min(v || 400, 1400), 10)
       this.update({
-        size: [
-          Math.max(Math.min(rect.width || 400, 800), 10),
-          Math.max(Math.min(rect.height || 400, 800), 10),
-        ],
+        size: [clamp(w), clamp(h)],
       })
     }
     return this

+ 4 - 1
tldraw/packages/core/src/lib/shapes/TLShape/TLShape.tsx

@@ -57,7 +57,10 @@ export interface TLResizeInfo {
   transformOrigin: number[]
 }
 
-export interface TLResetBoundsInfo {}
+export interface TLResetBoundsInfo {
+  zoom: number
+  asset?: TLAsset
+}
 
 export interface TLHandleChangeInfo {
   id: string

+ 4 - 2
tldraw/packages/core/src/lib/tools/TLSelectTool/states/HoveringSelectionHandleState.ts

@@ -77,7 +77,9 @@ export class HoveringSelectionHandleState<
           break
         }
         case TLTargetType.Selection: {
-          selectedShape.onResetBounds?.({})
+          selectedShape.onResetBounds?.({
+            zoom: this.app.viewport.camera.zoom,
+          })
           if (this.app.selectedShapesArray.length === 1) {
             this.tool.transition('editingShape', {
               type: TLTargetType.Shape,
@@ -91,7 +93,7 @@ export class HoveringSelectionHandleState<
       const asset = selectedShape.props.assetId
         ? this.app.assets[selectedShape.props.assetId]
         : undefined
-      selectedShape.onResetBounds({ asset })
+      selectedShape.onResetBounds({ asset, zoom: this.app.viewport.camera.zoom })
       this.tool.transition('idle')
     }
   }