From ccacd5114c13e9f9e1cff6af418309e24d791c9c Mon Sep 17 00:00:00 2001 From: "congvc-bot[bot]" <3634010+congvc-bot[bot]@users.noreply.github.com> Date: Sun, 22 Nov 2026 00:19:05 +0700 Subject: [PATCH] feat(doop): make Hand (H) the default canvas tool (CHE-6941, ported for CHE-7328) Forward-port of fork PR 60 (main 323f90ff1a81, commit ecb983ba85) onto cheese/main. Esc and canvas switch return to Hand. cheese/main split drawingBrowser.test.ts, so the Hand test lives in drawingShapesBrowser.test.ts; fixtures and drawing tests that assume Select start from it explicitly. --- src/components/CanvasDrawing.tsx | 2 +- src/components/Stage.tsx | 6 ++--- src/lib/store.ts | 4 ++-- src/pages/CanvasPage.tsx | 2 +- tests/defaultTool.test.ts | 33 ++++++++++++++++++++++++++ tests/drawingMoveBrowser.test.ts | 2 ++ tests/drawingPage.ts | 4 +++- tests/drawingShapesBrowser.test.ts | 32 +++++++++++++++++++++++++ tests/fixtures/canvasClipboard.html | 1 + tests/fixtures/canvasPage.html | 1 + tests/fixtures/elementContextMenu.html | 1 + tests/fixtures/elementPanel.html | 1 + tests/fixtures/pages.html | 1 + tests/fixtures/rulers.html | 1 + tests/svgImportBrowser.test.ts | 2 ++ 15 files changed, 85 insertions(+), 8 deletions(-) create mode 100644 tests/defaultTool.test.ts diff --git a/src/components/CanvasDrawing.tsx b/src/components/CanvasDrawing.tsx index 46e093888..5443a849c 100644 --- a/src/components/CanvasDrawing.tsx +++ b/src/components/CanvasDrawing.tsx @@ -82,7 +82,7 @@ export function useCanvasDrawing(stage: RefObject) { if (current.typing) finish() else { cancel() - useStore.getState().setTool('select') + useStore.getState().setTool('hand') } } else if ( e.key === 'Enter' && diff --git a/src/components/Stage.tsx b/src/components/Stage.tsx index ef442492b..2b89e4787 100644 --- a/src/components/Stage.tsx +++ b/src/components/Stage.tsx @@ -816,9 +816,9 @@ export function Stage({ } }, []) - /* Bare shortcut keys pick a tool, Esc returns to Select — bare keys + /* Bare shortcut keys pick a tool, Esc returns to Hand, the resting tool — bare keys only (no modifiers), and never while typing in a field. Esc is the one - exception for canvas composers: it drops their draft and returns to Select. + exception for canvas composers: it drops their draft and returns to Hand. Other fields keep Escape for their own editing behavior. Z is the Zoom tool only while it is held: letting go returns to the tool before it. The key-up is read by `code` too, since ⌥ held for a zoom-out @@ -838,7 +838,7 @@ export function Stage({ if (e.metaKey || e.ctrlKey || e.altKey || e.shiftKey || e.repeat) return if (e.key === 'Escape') { if (isTypingTarget(e.target) && !e.target.closest('[data-comment-ui], [data-drawing-input]')) return - if (useStore.getState().tool !== 'select') useStore.getState().setTool('select') + if (useStore.getState().tool !== 'hand') useStore.getState().setTool('hand') return } if (isTypingTarget(e.target)) return diff --git a/src/lib/store.ts b/src/lib/store.ts index 581476db2..b01e544c7 100644 --- a/src/lib/store.ts +++ b/src/lib/store.ts @@ -332,7 +332,7 @@ export const useStore = create((set, get) => ({ following: null, selectedIds: [], selectedId: null, - tool: 'select', + tool: 'hand', panMode: false, fileDragging: false, inspectorOpen: false, @@ -679,7 +679,7 @@ export const useStore = create((set, get) => ({ const demoted = !roleAllows(role, 'edit') return { canvas: { ...s.canvas, role }, - tool: toolAllowed(s.tool, role) ? s.tool : 'select', + tool: toolAllowed(s.tool, role) ? s.tool : 'hand', /* an open Design panel or Inspector would keep offering edits */ ...(demoted ? { inspectorOpen: false, elementPanelOpen: false } : {}), } diff --git a/src/pages/CanvasPage.tsx b/src/pages/CanvasPage.tsx index c758a7ef8..b00e74d3f 100644 --- a/src/pages/CanvasPage.tsx +++ b/src/pages/CanvasPage.tsx @@ -374,7 +374,7 @@ export function CanvasPage({ useStore.getState().setPresences([]) useStore.getState().setCanvasNotFound(false) select(null) - useStore.getState().setTool('select') + useStore.getState().setTool('hand') clearHistory() } }, [canvasId, guest, select]) diff --git a/tests/defaultTool.test.ts b/tests/defaultTool.test.ts new file mode 100644 index 000000000..9aaee4837 --- /dev/null +++ b/tests/defaultTool.test.ts @@ -0,0 +1,33 @@ +import { beforeEach, describe, expect, it } from 'vitest' +import { useStore } from '../src/lib/store' +import type { Canvas } from '../shared/types' + +/** + * Hand is the resting tool: the app starts in it and role demotion falls back + * to it. (Esc and canvas switch are covered in the browser test.) + */ + +const canvas = (role: Canvas['role']): Canvas => + ({ id: 'c', name: 'c', frames: [], pages: [], createdAt: 0, updatedAt: 0, role }) as Canvas + +describe('default tool', () => { + beforeEach(() => useStore.setState(useStore.getInitialState(), true)) + + it('starts as Hand', () => { + expect(useStore.getState().tool).toBe('hand') + }) + + it('falls back to Hand when a role demotion removes the active tool', () => { + useStore.setState({ canvas: canvas('edit') }) + useStore.getState().setTool('rectangle') + useStore.getState().setRole('view') + expect(useStore.getState().tool).toBe('hand') + }) + + it('keeps Select when the role still allows it', () => { + useStore.setState({ canvas: canvas('edit') }) + useStore.getState().setTool('select') + useStore.getState().setRole('view') + expect(useStore.getState().tool).toBe('select') + }) +}) diff --git a/tests/drawingMoveBrowser.test.ts b/tests/drawingMoveBrowser.test.ts index 8fbe8fec4..fb9b8032f 100644 --- a/tests/drawingMoveBrowser.test.ts +++ b/tests/drawingMoveBrowser.test.ts @@ -23,6 +23,8 @@ describe.skipIf(!findBrowserPath())('drawing on the canvas', { timeout: 30_000 } await drag([250, 300], [330, 380]) const [original] = await waitForFrames(1) for (const reason of ['escape', 'pointer', 'page']) { + /* Esc returns to Hand (the resting tool); move needs Select again */ + await page.keyboard.press('v') await page.mouse.move(290, 340) await page.mouse.down() await page.mouse.move(700, 300, { steps: 4 }) diff --git a/tests/drawingPage.ts b/tests/drawingPage.ts index 0ea5cbc4b..2ca7b33b6 100644 --- a/tests/drawingPage.ts +++ b/tests/drawingPage.ts @@ -104,7 +104,7 @@ async function openPage(pages: TestPages): Promise { export function drawingPage(): void { const pages = pagePerTest() - beforeEach(async () => { + beforeEach(async (ctx) => { page = await openPage(pages) saved = [] nextId = 0 @@ -129,6 +129,8 @@ export function drawingPage(): void { ) await page.waitForSelector('[aria-label="Canvas tools"]') await page.evaluate(() => window.drawingHarness.viewport({ x: 40, y: 20, zoom: 2 })) + /* the app opens in Hand; every other test here starts from Select */ + if (!ctx.task.name.startsWith('opens in Hand')) await page.keyboard.press('v') }) afterAll(() => { diff --git a/tests/drawingShapesBrowser.test.ts b/tests/drawingShapesBrowser.test.ts index e3904a6b5..1345b3499 100644 --- a/tests/drawingShapesBrowser.test.ts +++ b/tests/drawingShapesBrowser.test.ts @@ -16,6 +16,38 @@ import type { Frame } from '../shared/types' describe.skipIf(!findBrowserPath())('drawing on the canvas', { timeout: 30_000 }, () => { drawingPage() + it('opens in Hand: a drag over a frame pans without selecting, and Esc returns to Hand from any tool', async () => { + await seedFrames([ + { + id: 'hand-frame', + updatedBy: 'User', + canvasId: 'c', + pageId: 'first', + name: 'Hand frame', + x: 100, + y: 100, + width: 300, + height: 200, + createdAt: 0, + updatedAt: 0, + html: '', + }, + ]) + expect(await page.$eval('[aria-label="Hand"]', (el) => el.getAttribute('aria-pressed'))).toBe('true') + const before = await page.evaluate(() => window.drawingHarness.camera()) + await drag([300, 300], [380, 340]) + const after = await page.evaluate(() => window.drawingHarness.camera()) + expect(after.x - before.x).toBeCloseTo(80) + expect(after.y - before.y).toBeCloseTo(40) + expect(await page.evaluate(() => window.drawingHarness.state().selectedIds)).toEqual([]) + for (const key of ['v', 'c', 'r'] as const) { + await page.keyboard.press(key) + expect(await page.evaluate(() => window.drawingHarness.state().tool)).not.toBe('hand') + await page.keyboard.press('Escape') + expect(await page.evaluate(() => window.drawingHarness.state().tool)).toBe('hand') + } + }) + it('draws a frame with reverse drag at a panned/zoomed viewport, then adds editable shapes inside it', async () => { await page.click('[aria-label="Frame"]') await drag([700, 500], [200, 200]) diff --git a/tests/fixtures/canvasClipboard.html b/tests/fixtures/canvasClipboard.html index 8ba273a88..7cef263a2 100644 --- a/tests/fixtures/canvasClipboard.html +++ b/tests/fixtures/canvasClipboard.html @@ -12,6 +12,7 @@ import { TooltipProvider } from '/src/components/ui/tooltip.tsx' import { pickedSelectors, useStore } from '/src/lib/store.ts' import { clearHistory, undo } from '/src/lib/history.ts' + useStore.setState({ tool: 'select' }) /* the app opens in Hand; these tests start from Select */ import '/src/styles.css' window.canvasClipboardHarness = { diff --git a/tests/fixtures/canvasPage.html b/tests/fixtures/canvasPage.html index 28d8dc434..1f1ff6a07 100644 --- a/tests/fixtures/canvasPage.html +++ b/tests/fixtures/canvasPage.html @@ -11,6 +11,7 @@ import { CanvasPage } from '/src/pages/CanvasPage.tsx' import { TooltipProvider } from '/src/components/ui/tooltip.tsx' import { useStore } from '/src/lib/store.ts' + useStore.setState({ tool: 'select' }) /* the app opens in Hand; these tests start from Select */ import '/src/styles.css' /* the canvas page as an editor has it open, with all its chrome; the diff --git a/tests/fixtures/elementContextMenu.html b/tests/fixtures/elementContextMenu.html index 695352ecf..b9e4c9a80 100644 --- a/tests/fixtures/elementContextMenu.html +++ b/tests/fixtures/elementContextMenu.html @@ -19,6 +19,7 @@ ContextMenuItem, } from '/src/components/ui/context-menu.tsx' import { useStore } from '/src/lib/store.ts' + useStore.setState({ tool: 'select' }) /* the app opens in Hand; these tests start from Select */ import { clearHistory, undo, redo } from '/src/lib/history.ts' import '/src/styles.css' diff --git a/tests/fixtures/elementPanel.html b/tests/fixtures/elementPanel.html index 7b703ffd0..4a270d9ba 100644 --- a/tests/fixtures/elementPanel.html +++ b/tests/fixtures/elementPanel.html @@ -15,6 +15,7 @@ import { layoutZoomFor } from '/src/lib/layoutZoom.ts' import { TooltipProvider } from '/src/components/ui/tooltip.tsx' import { pickedSelectors, useStore } from '/src/lib/store.ts' + useStore.setState({ tool: 'select' }) /* the app opens in Hand; these tests start from Select */ import { clearHistory, undo, redo } from '/src/lib/history.ts' import { editElementGeometry } from '/src/lib/frameBridge.ts' import { nudgeSelection } from '/src/lib/layerEdits.ts' diff --git a/tests/fixtures/pages.html b/tests/fixtures/pages.html index fba4af1fa..bee3b2094 100644 --- a/tests/fixtures/pages.html +++ b/tests/fixtures/pages.html @@ -19,6 +19,7 @@ import { createRoot } from 'react-dom/client' import { Stage } from '/src/components/Stage.tsx' import { useStore } from '/src/lib/store.ts' + useStore.setState({ tool: 'select' }) /* the app opens in Hand; these tests start from Select */ import { TooltipProvider } from '/src/components/ui/tooltip.tsx' import '/src/styles.css' window.seedPages = (canvas) => useStore.getState().setCanvas(canvas) diff --git a/tests/fixtures/rulers.html b/tests/fixtures/rulers.html index 1a989bd9c..3106dea11 100644 --- a/tests/fixtures/rulers.html +++ b/tests/fixtures/rulers.html @@ -11,6 +11,7 @@ import { createRoot } from 'react-dom/client' import { Stage } from '/src/components/Stage.tsx' import { useStore } from '/src/lib/store.ts' + useStore.setState({ tool: 'select' }) /* the app opens in Hand; these tests start from Select */ import { clearHistory, undo, redo } from '/src/lib/history.ts' import { TooltipProvider } from '/src/components/ui/tooltip.tsx' import '/src/styles.css' diff --git a/tests/svgImportBrowser.test.ts b/tests/svgImportBrowser.test.ts index 8192fee46..fc121f716 100644 --- a/tests/svgImportBrowser.test.ts +++ b/tests/svgImportBrowser.test.ts @@ -38,6 +38,8 @@ describe.skipIf(!findBrowserPath())('importing SVG files', { timeout: 30_000 }, await page.setViewport({ width: 1200, height: 800 }) await page.goto(fixtureUrl('drawing.html')) await page.waitForFunction(() => !!window.drawingHarness) + /* the app opens in Hand; these tests start from Select */ + await page.keyboard.press('v') }) afterAll(() => {