Skip to content
Merged
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ layers of that frame, so agents read and edit them like any other markup.
- **Zoom** — 8% to 1600%. Wheel pans, ⌘/Ctrl + wheel or a pinch zooms; a 1px pixel grid fades in from
300%. The zoom menu has 50/100/200% presets and the rulers toggle.
- **Panels** — the right side panel has **Agents**, **Comments**, **Activity** and **Memory** tabs;
Layers and the side panel can be resized, and ⌘. hides all UI.
Layers and the side panel can be resized. Drag the divider between Pages and Layers to change
their split; arrow keys move it, Home and End reach its limits, and Doop remembers it per canvas.
Pages starts compact until you resize it. ⌘. hides all UI.
- **Themes** — Light, Dark or System under Settings → Appearance.
- **Phones** — the canvas works on a phone: one-finger pan, pinch zoom, tap to comment, and bottom
sheets for canvas actions, Layers and the Inspector.
Expand Down
2 changes: 1 addition & 1 deletion src/components/LayersPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ export function LayersPanel({
<>
{/* Figma's order: the pages at the top, the frames of the active page
under them, the search at the bottom within thumb's reach */}
<PagesPanel />
{pageCanvas && <PagesPanel key={pageCanvas.id} canvasId={pageCanvas.id} />}
<div className={cn('mt-1.5 flex h-6 flex-none items-center justify-between pr-2.5 pl-3.5', sectionHeading)}>
<span data-slot="frame-section-heading" className={sectionHeading}>
{t('panels.layersTitle')}
Expand Down
99 changes: 94 additions & 5 deletions src/components/PagesPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import { ApiError } from '../lib/api'
import { Button } from './ui/button'
import { PageIcon, PlusIcon } from './ui/icons'
import { cn } from '../lib/utils'
import {
clampPagesPx,
pagesHeightCss,
pagesMaxPx,
pagesMinPx,
pagesPctOf,
pagesPxAfterKey,
usePagesPct,
} from '../lib/pagesSplit'
import { ContextMenu, ContextMenuTrigger, ContextMenuContent, ContextMenuItem } from './ui/context-menu'
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from './ui/select'
import { useT } from '@/lib/i18n'
Expand All @@ -19,7 +28,8 @@ import { useT } from '@/lib/i18n'
export const sectionHeading = 'text-[11px] font-normal text-ink-faint'
const sectionBtn = 'size-5 rounded-[5px] text-ink-faint hover:bg-paper-deep hover:text-ink'

export function PagesPanel({ fill = false }: { fill?: boolean }) {
export function PagesPanel({ canvasId }: { canvasId: string }) {
const [pagesPct, setPagesPct] = usePagesPct(canvasId)
const canvas = useStore((s) => s.canvas)
const activePageId = useStore((s) => s.activePageId)
const selectedIds = useStore((s) => s.selectedIds)
Expand All @@ -41,6 +51,9 @@ export function PagesPanel({ fill = false }: { fill?: boolean }) {
if (!canvas) return null
const pages = canvasPages(canvas)
const movableIds = canvas.frames.filter((f) => selectedIds.includes(f.id) && !f.locked).map((f) => f.id)
/* the picker is there to move the selection somewhere else, so it needs a second page */
const showMovePicker = !readOnly && selectedIds.length > 0 && pages.length > 1
const minPx = pagesMinPx(showMovePicker)

async function moveSelection(pageId: string) {
if (!canvas || busy || !movableIds.length) return
Expand Down Expand Up @@ -312,20 +325,26 @@ export function PagesPanel({ fill = false }: { fill?: boolean }) {
)

return (
<div className={cn('flex flex-none flex-col', fill && 'min-h-0 flex-1')}>
<div
data-slot="pages-panel"
className="relative flex flex-none flex-col border-b border-line-soft"
style={
pagesPct === null ? { maxHeight: pagesHeightCss(100, minPx) } : { height: pagesHeightCss(pagesPct, minPx) }
}
>
<div className={cn('mt-1.5 flex h-6 flex-none items-center justify-between pr-2.5 pl-3.5', sectionHeading)}>
<span data-slot="page-section-heading" className={sectionHeading}>
{t('panels.pagesTitle')}
</span>
{!readOnly && addPage}
</div>
<div className={cn('overflow-y-auto px-2 pb-1', fill ? 'min-h-0 flex-1' : 'max-h-[30vh]')}>{list}</div>
{!readOnly && selectedIds.length > 0 && pages.length > 1 && (
<div className={cn('min-h-0 overflow-y-auto px-2 pb-1', pagesPct === null && 'max-h-[30vh]')}>{list}</div>
{showMovePicker && (
/* the canvas shortcuts listen on window: ⌫ would delete the frames
being moved and typed letters would switch tools, so the picker's
keys stop here; React bubbles the portalled list's keys through this
div too, before the native event reaches window */
<div className="px-2 pb-2" onKeyDown={(e) => e.stopPropagation()}>
<div className="flex-none px-2 pb-2" onKeyDown={(e) => e.stopPropagation()}>
<Select value="" disabled={busy || !movableIds.length} onValueChange={(id) => void moveSelection(id)}>
<SelectTrigger size="sm" className="w-full" aria-label={t('panels.pagesMoveSelection')}>
<SelectValue placeholder={t('panels.pagesMoveSelectionPlaceholder')} />
Expand All @@ -342,6 +361,76 @@ export function PagesPanel({ fill = false }: { fill?: boolean }) {
</Select>
</div>
)}
<PagesSplitHandle minPx={minPx} onChange={setPagesPct} />
</div>
)
}

/** The Pages section's bottom edge: dragged, or moved with the arrow keys, Home and End. Each step measures the
* section and its rail again, so it starts from what is on screen. The separator's value and range are those
* measures too: the section also changes height when its content or the window does, not only on a drag. The
* browser releases the pointer's capture when the drag ends, which resets the drag. */
function PagesSplitHandle({ minPx, onChange }: { minPx: number; onChange: (pct: number) => void }) {
const t = useT()
const handle = useRef<HTMLDivElement>(null)
const drag = useRef<{ y: number; pagesPx: number; railPx: number } | null>(null)
const [sizes, setSizes] = useState({ pagesPx: 0, railPx: 0 })
useEffect(() => {
const el = handle.current
if (!el) return
const measure = () => {
const next = measurePages(el)
setSizes((prev) => (prev.pagesPx === next.pagesPx && prev.railPx === next.railPx ? prev : next))
}
const observer = new ResizeObserver(measure)
observer.observe(el.closest('[data-slot="pages-panel"]')!)
observer.observe(el.closest('[data-slot="panel"]')!)
return () => observer.disconnect()
}, [])
return (
<div
ref={handle}
role="separator"
tabIndex={0}
aria-orientation="horizontal"
aria-label={t('panels.pagesResize')}
aria-valuemin={minPx}
aria-valuemax={pagesMaxPx(sizes.railPx, minPx)}
aria-valuenow={Math.round(sizes.pagesPx)}
data-slot="pages-split"
Comment thread
baekjosh marked this conversation as resolved.
Resolved
className="absolute inset-x-2 top-full h-1.5 cursor-row-resize touch-none rounded-full focus-visible:bg-ink/20 focus-visible:outline-none"
onPointerDown={(e) => {
if (e.button !== 0) return
e.preventDefault()
e.stopPropagation()
drag.current = { y: e.clientY, ...measurePages(e.currentTarget) }
e.currentTarget.setPointerCapture(e.pointerId)
}}
onPointerMove={(e) => {
const start = drag.current
if (!start) return
e.stopPropagation()
const px = clampPagesPx(start.pagesPx + e.clientY - start.y, start.railPx, minPx)
onChange(pagesPctOf(px, start.railPx))
}}
onLostPointerCapture={() => {
drag.current = null
}}
onKeyDown={(e) => {
const { pagesPx, railPx } = measurePages(e.currentTarget)
const px = pagesPxAfterKey(e.key, pagesPx, railPx, minPx)
if (px === undefined) return
e.preventDefault()
e.stopPropagation()
onChange(pagesPctOf(px, railPx))
}}
/>
)
}

/** The Pages section and the rail it sits in, both in px. */
function measurePages(handle: HTMLElement) {
const pages = handle.closest<HTMLElement>('[data-slot="pages-panel"]')!
const rail = handle.closest<HTMLElement>('[data-slot="panel"]')!
return { pagesPx: pages.getBoundingClientRect().height, railPx: rail.getBoundingClientRect().height }
}
1 change: 1 addition & 0 deletions src/lib/i18n/en/panels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const panels = {
pagesMoveFailed: 'Could not move all selected frames to the page',
pagesSaveFailed: 'Could not save pages',
pagesDuplicateFailed: 'Could not duplicate the page',
pagesResize: 'Resize pages',

memoryIntro: 'Memory is how this canvas remembers your taste — and how every agent designs with it.',
memoryReferences: 'References',
Expand Down
1 change: 1 addition & 0 deletions src/lib/i18n/pt-BR/panels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const panels: Catalog['panels'] = {
pagesMoveFailed: 'Não foi possível mover todos os frames selecionados para a página',
pagesSaveFailed: 'Não foi possível salvar as páginas',
pagesDuplicateFailed: 'Não foi possível duplicar a página',
pagesResize: 'Redimensionar páginas',

memoryIntro: 'A Memória é como este canvas lembra do seu gosto — e como cada agente cria designs com ele.',
memoryReferences: 'Referências',
Expand Down
88 changes: 88 additions & 0 deletions src/lib/pagesSplit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* The split between the Pages list and the Layers tree in the canvas rail.
A canvas starts with a content-sized Pages section. After a resize its
height is a share of the rail, clamped to keep both lists usable. */
import { useEffect, useState } from 'react'

/** the least the Pages section gets with no move picker: its heading, one page row,
* the list's bottom padding and the 1px divider (30 + 27 + 4 + 1px);
* PagesPanel's classes set those sizes */
export const PAGES_BASE_MIN_PX = 62
/** what the move picker adds while it is shown (its 32px trigger and 8px of padding) */
export const PAGES_MOVE_PICKER_PX = 40
/** Room for the rail header, Layers heading, one row and the search footer. */
export const LAYERS_MIN_PX = 144
/** how far one arrow press moves the split */
export const PAGES_KEY_STEP_PX = 10

const STORAGE_KEY = 'doop:layers-pages-split'

/** the least the Pages section gets: one page row, plus the move picker while it is shown */
export function pagesMinPx(withMovePicker: boolean): number {
return withMovePicker ? PAGES_BASE_MIN_PX + PAGES_MOVE_PICKER_PX : PAGES_BASE_MIN_PX
}

/** the most the Pages section gets: the room the Layers tree leaves, never below `minPx` */
export function pagesMaxPx(railPx: number, minPx: number): number {
return Math.max(minPx, railPx - LAYERS_MIN_PX)
}

/** a Pages height, held between `minPx` and the room the Layers tree keeps */
export function clampPagesPx(px: number, railPx: number, minPx: number): number {
return Math.min(pagesMaxPx(railPx, minPx), Math.max(minPx, px))
}

/** a Pages height as a percentage of the rail */
export function pagesPctOf(px: number, railPx: number): number {
return (px / railPx) * 100
}

/** The same height bounds as `clampPagesPx`, applied during layout. */
export function pagesHeightCss(pct: number, minPx: number): string {
return `clamp(${minPx}px, ${pct}%, calc(100% - ${LAYERS_MIN_PX}px))`
}

/** the Pages height an arrow key, Home or End asks for; undefined for any other key.
* Home and End clamp infinity, so they stop at the same limits as a drag. */
export function pagesPxAfterKey(key: string, pagesPx: number, railPx: number, minPx: number): number | undefined {
switch (key) {
case 'ArrowUp':
return clampPagesPx(pagesPx - PAGES_KEY_STEP_PX, railPx, minPx)
case 'ArrowDown':
return clampPagesPx(pagesPx + PAGES_KEY_STEP_PX, railPx, minPx)
case 'Home':
return clampPagesPx(-Infinity, railPx, minPx)
case 'End':
return clampPagesPx(Infinity, railPx, minPx)
default:
return undefined
}
}

/** a stored split, or null when it is missing or not a percentage of the rail */
export function readPagesPct(raw: string | null): number | null {
if (raw === null) return null
const pct = Number(raw)
return Number.isFinite(pct) && pct > 0 && pct < 100 ? pct : null
}

/** The split as last set for this canvas. The caller is keyed by canvas ID. */
export function usePagesPct(canvasId: string): [number | null, (pct: number) => void] {
const key = `${STORAGE_KEY}:${canvasId}`
const [pct, setPct] = useState<number | null>(() => readStoredPct(key))
useEffect(() => {
try {
if (pct !== null) localStorage.setItem(key, String(pct))
} catch {
/* the split still works when it cannot be saved */
}
}, [key, pct])
return [pct, setPct]
}

function readStoredPct(key: string): number | null {
try {
return readPagesPct(localStorage.getItem(key))
} catch {
return null
}
}
3 changes: 2 additions & 1 deletion tests/canvasPageFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ export async function showCanvasPage(
await page.goto(fixtureUrl(`canvasPage.html${search}`))
await page.waitForFunction(() => !!window.canvasPageHarness && !!document.querySelector('.stage'))
await page.evaluate((frames, canvas) => window.canvasPageHarness.seed(frames, canvas), frames, canvas)
await page.waitForSelector('[data-frame-title]')
/* an empty canvas has no frame title to wait for: its Layers rail is what shows */
await page.waitForSelector(frames.length ? '[data-frame-title]' : '[data-slot="pages-panel"]')
}
8 changes: 5 additions & 3 deletions tests/fixtures/canvasPage.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@
import { useStore } from '/src/lib/store.ts'
import '/src/styles.css'

const canvasId = new URLSearchParams(location.search).get('fixtureCanvasId') ?? 'c'

/* the canvas page as an editor has it open, with all its chrome; the
test hands it the canvas the room would have sent */
window.canvasPageHarness = {
seed: (frames, canvas) =>
useStore
.getState()
.setCanvas({ id: 'c', name: 'Canvas', frames, role: 'edit', createdAt: 0, updatedAt: 0, ...canvas }),
setPages: (pages) => useStore.getState().setPages(pages, 'c'),
.setCanvas({ id: canvasId, name: 'Canvas', frames, role: 'edit', createdAt: 0, updatedAt: 0, ...canvas }),
setPages: (pages) => useStore.getState().setPages(pages, canvasId),
}
createRoot(document.getElementById('root')).render(
React.createElement(TooltipProvider, null, React.createElement(CanvasPage, { canvasId: 'c' })),
React.createElement(TooltipProvider, null, React.createElement(CanvasPage, { canvasId })),
)
</script>
</body>
Expand Down
8 changes: 7 additions & 1 deletion tests/pagesRename.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ describe('PagesPanel rename', () => {
container = document.createElement('div')
document.body.appendChild(container)
root = createRoot(container)
act(() => root.render(<PagesPanel />))
act(() =>
root.render(
<div data-slot="panel">
<PagesPanel canvasId="c1" />
</div>,
),
)
})

afterEach(() => {
Expand Down
70 changes: 70 additions & 0 deletions tests/pagesSplit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { describe, expect, it } from 'vitest'
import {
LAYERS_MIN_PX,
PAGES_BASE_MIN_PX,
PAGES_MOVE_PICKER_PX,
clampPagesPx,
pagesMaxPx,
pagesMinPx,
pagesPxAfterKey,
readPagesPct,
} from '../src/lib/pagesSplit'

describe('pagesMinPx', () => {
it('reserves room for the move picker only while it is shown', () => {
expect(pagesMinPx(false)).toBe(PAGES_BASE_MIN_PX)
expect(pagesMinPx(true)).toBe(PAGES_BASE_MIN_PX + PAGES_MOVE_PICKER_PX)
})
})

describe('clampPagesPx', () => {
const min = pagesMinPx(false)

it('holds the Pages section between its minimum and the room the Layers tree keeps', () => {
expect(clampPagesPx(-500, 800, min)).toBe(min)
expect(clampPagesPx(10_000, 800, min)).toBe(800 - LAYERS_MIN_PX)
expect(clampPagesPx(300, 800, min)).toBe(300)
})

it('keeps the Pages minimum in a rail too short to hold both lists', () => {
expect(clampPagesPx(400, 200, min)).toBe(min)
expect(pagesMaxPx(200, min)).toBe(min)
})

it('lets a section without the picker go lower than one with it', () => {
expect(clampPagesPx(0, 800, pagesMinPx(false))).toBeLessThan(clampPagesPx(0, 800, pagesMinPx(true)))
})
})

describe('pagesPxAfterKey', () => {
const min = pagesMinPx(true)

it('moves the split one step per arrow press and stops at its limits', () => {
expect(pagesPxAfterKey('ArrowDown', 300, 800, min)).toBe(310)
expect(pagesPxAfterKey('ArrowUp', 300, 800, min)).toBe(290)
expect(pagesPxAfterKey('ArrowUp', min, 800, min)).toBe(min)
})

it('takes Home and End to the two limits', () => {
expect(pagesPxAfterKey('Home', 300, 800, min)).toBe(min)
expect(pagesPxAfterKey('End', 300, 800, min)).toBe(800 - LAYERS_MIN_PX)
})

it('leaves every other key to the page', () => {
expect(pagesPxAfterKey('a', 300, 800, min)).toBeUndefined()
})
})

describe('readPagesPct', () => {
it('keeps a stored split', () => {
expect(readPagesPct('37.5')).toBe(37.5)
})

it('drops anything that is not a split of the rail, so a bad value falls back to the default', () => {
expect(readPagesPct(null)).toBeNull()
expect(readPagesPct('')).toBeNull()
expect(readPagesPct('tall')).toBeNull()
expect(readPagesPct('0')).toBeNull()
expect(readPagesPct('100')).toBeNull()
})
})
Loading
Loading