fix camerarevert and default to select tool

This commit is contained in:
Jeff Emmett 2024-11-27 13:46:41 +07:00
parent 949062941f
commit f7310919f8
2 changed files with 11 additions and 14 deletions

View File

@ -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,
},

View File

@ -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);
}
};
// 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]);