From e6ddce8be74a830a7ac3aec2d7a111b4161bcc82 Mon Sep 17 00:00:00 2001 From: Jeff Emmett <46964190+Jeff-Emmett@users.noreply.github.com> Date: Wed, 27 Nov 2024 13:46:41 +0700 Subject: [PATCH] fix camerarevert and default to select tool --- src/components/Board.tsx | 6 +++--- src/hooks/useCameraControls.ts | 19 ++++++++----------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/components/Board.tsx b/src/components/Board.tsx index e514871..b584ae8 100644 --- a/src/components/Board.tsx +++ b/src/components/Board.tsx @@ -95,7 +95,7 @@ export function Board() { onSelect: () => { if (editor.getSelectedShapeIds().length > 0) { zoomToSelection(editor); - editor.setCurrentTool('hand'); + editor.setCurrentTool('select'); } }, readonlyOk: true, @@ -111,7 +111,7 @@ export function Board() { url.searchParams.set('y', camera.y.toString()); url.searchParams.set('zoom', camera.z.toString()); navigator.clipboard.writeText(url.toString()); - editor.setCurrentTool('hand'); + editor.setCurrentTool('select'); }, readonlyOk: true, }, @@ -121,7 +121,7 @@ export function Board() { kbd: 'b', onSelect: () => { revertCamera(); - editor.setCurrentTool('hand'); + editor.setCurrentTool('select'); }, readonlyOk: true, }, diff --git a/src/hooks/useCameraControls.ts b/src/hooks/useCameraControls.ts index 48aacba..6a0a688 100644 --- a/src/hooks/useCameraControls.ts +++ b/src/hooks/useCameraControls.ts @@ -14,18 +14,14 @@ let cameraHistory: CameraState[] = []; // Improved camera change tracking with debouncing const trackCameraChange = (editor: Editor) => { - // Only track if not in animation - if (editor.getCameraState() === 'moving') return; - const currentCamera = editor.getCamera(); const lastPosition = cameraHistory[cameraHistory.length - 1]; - // Enhanced threshold check for meaningful changes + // Store any viewport change that's not from a revert operation if (!lastPosition || - (Math.abs(lastPosition.x - currentCamera.x) > 1 || - Math.abs(lastPosition.y - currentCamera.y) > 1 || - Math.abs(lastPosition.z - currentCamera.z) > 0.1)) { - + currentCamera.x !== lastPosition.x || + currentCamera.y !== lastPosition.y || + currentCamera.z !== lastPosition.z) { cameraHistory.push({ ...currentCamera }); if (cameraHistory.length > MAX_HISTORY) { cameraHistory.shift(); @@ -77,15 +73,16 @@ export function useCameraControls(editor: Editor | null) { if (!editor) return; const handler = () => { - if (editor.getCameraState() !== 'moving') { - trackCameraChange(editor); - } + trackCameraChange(editor); }; + // Track both viewport changes and user interaction end editor.on('viewportChange' as keyof TLEventMap, handler); + editor.on('userChangeEnd' as keyof TLEventMap, handler); return () => { editor.off('viewportChange' as keyof TLEventMap, handler); + editor.off('userChangeEnd' as keyof TLEventMap, handler); }; }, [editor]);