Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/CanvasDrawing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export function useCanvasDrawing(stage: RefObject<HTMLDivElement>) {
if (current.typing) finish()
else {
cancel()
useStore.getState().setTool('select')
useStore.getState().setTool('hand')
}
} else if (
e.key === 'Enter' &&
Expand Down
6 changes: 3 additions & 3 deletions src/components/Stage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export const useStore = create<State>((set, get) => ({
following: null,
selectedIds: [],
selectedId: null,
tool: 'select',
tool: 'hand',
panMode: false,
fileDragging: false,
inspectorOpen: false,
Expand Down Expand Up @@ -679,7 +679,7 @@ export const useStore = create<State>((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 } : {}),
}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/CanvasPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
33 changes: 33 additions & 0 deletions tests/defaultTool.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
2 changes: 2 additions & 0 deletions tests/drawingMoveBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
4 changes: 3 additions & 1 deletion tests/drawingPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async function openPage(pages: TestPages): Promise<Page> {
export function drawingPage(): void {
const pages = pagePerTest()

beforeEach(async () => {
beforeEach(async (ctx) => {
page = await openPage(pages)
saved = []
nextId = 0
Expand All @@ -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(() => {
Expand Down
32 changes: 32 additions & 0 deletions tests/drawingShapesBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<body></body>',
},
])
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])
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/canvasClipboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/canvasPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/elementContextMenu.html
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/elementPanel.html
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/pages.html
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/rulers.html
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 2 additions & 0 deletions tests/svgImportBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
Loading