Skip to content

AI-native design editor. Open-source Figma alternative.

fix: zoom in place, shape cursor names, and align context menu items (#818)

Danila Poyarkov8528a917c0b9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b4f7dfeb8..4b64169f8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -25,6 +25,7 @@
### Changed
+- Show Flatten, Outline text, and Outline stroke in the canvas context menu without icons, like every other item there.
- Keep an AI chat working on the page where it started when you switch to another page, instead of sending its next edits to whichever page is on screen. When the AI switches pages itself, your view follows.
- `openpencil://` and web `?node=` links select the layer on another page when the current page has none, switching to that page.
- Generate Tailwind JSX with the same class mapping as Tailwind HTML export, so both describe a design the same way, and write opaque colors as hex in HTML, CSS, and Tailwind output. `openpencil export -f jsx --style tailwind` now exports a whole page when no `--node` is given.
@@ -32,6 +33,8 @@
### Fixed
+- Keep the view centered on what you were looking at when zooming to 100% or another fixed level, instead of jumping elsewhere whenever the zoom changes.
+- Draw collaborators' names on their cursors with proper letter spacing and fallback fonts, and end long names with an ellipsis.
- Keep line breaks in multi-line text when exporting OpenPencil JSX, so `get_jsx` output and `openpencil export -f jsx` render back to the same text instead of joining the lines with spaces.
- Announce unavailable commands in the command palette as disabled to screen readers.
- Keep the ordering keys a `.fig` gave its layers when saving one again, instead of renumbering every sibling, and give every layer on a canvas its own key. Shared styles, variables and the canvas's own layers were numbered in separate passes that each restarted, so Figma saw siblings claiming the same position and ordered them arbitrarily.
diff --git a/packages/core/src/canvas/pen-overlay.ts b/packages/core/src/canvas/pen-overlay.ts
index 71bd83a46..73d82ba30 100644
--- a/packages/core/src/canvas/pen-overlay.ts
+++ b/packages/core/src/canvas/pen-overlay.ts
@@ -207,6 +207,8 @@ export function drawRemoteCursors(
const LABEL_FONT_SIZE = 10
const LABEL_OFFSET_X = 12
const LABEL_OFFSET_Y = 20
+ /** Longer names end with an ellipsis. */
+ const LABEL_MAX_WIDTH = 160
for (const cursor of cursors) {
const screenX = cursor.x * r.zoom + r.panX
@@ -267,33 +269,35 @@ export function drawRemoteCursors(
canvas.drawPath(immutablePath, r.auxFill)
immutablePath.delete()
- if (cursor.name) {
- const font = r.labelFont
- if (font) {
- font.setSize(LABEL_FONT_SIZE)
- const labelX = screenX + LABEL_OFFSET_X
- const labelY = screenY + LABEL_OFFSET_Y
- const glyphIds = font.getGlyphIDs(cursor.name)
- const widths = font.getGlyphWidths(glyphIds)
- let textWidth = 0
- for (const w of widths) textWidth += w
-
- r.auxFill.setColor(r.ck.Color4f(cr, g, b, 1))
- const bgRect = r.ck.RRectXY(
- r.ck.XYWHRect(
- labelX - LABEL_PADDING_X,
- labelY - LABEL_FONT_SIZE - LABEL_PADDING_Y + 2,
- textWidth + LABEL_PADDING_X * 2,
- LABEL_FONT_SIZE + LABEL_PADDING_Y * 2
- ),
- 4,
- 4
- )
- canvas.drawRRect(bgRect, r.auxFill)
-
- r.auxFill.setColor(r.ck.Color4f(1, 1, 1, 1))
- canvas.drawText(cursor.name, labelX, labelY, r.auxFill, font)
- }
+ const provider = r.fontProvider
+ if (cursor.name && provider) {
+ const pillX = screenX + LABEL_OFFSET_X - LABEL_PADDING_X
+ const pillY = screenY + LABEL_OFFSET_Y - LABEL_FONT_SIZE - LABEL_PADDING_Y + 2
+ const pillHeight = LABEL_FONT_SIZE + LABEL_PADDING_Y * 2
+ // Shaped like other canvas labels: kerning, fallback fonts, and RTL names.
+ r.labelParagraphCache.use(
+ r.ck,
+ provider,
+ cursor.name,
+ LABEL_FONT_SIZE,
+ LABEL_MAX_WIDTH,
+ r.ck.WHITE,
+ r.fontGeneration,
+ ({ paragraph, metrics }) => {
+ r.auxFill.setColor(r.ck.Color4f(cr, g, b, 1))
+ const pill = r.ck.RRectXY(
+ r.ck.XYWHRect(pillX, pillY, metrics.width + LABEL_PADDING_X * 2, pillHeight),
+ 4,
+ 4
+ )
+ canvas.drawRRect(pill, r.auxFill)
+ canvas.drawParagraph(
+ paragraph,
+ pillX + LABEL_PADDING_X,
+ pillY + (pillHeight - metrics.height) / 2
+ )
+ }
+ )
}
}
}
diff --git a/packages/core/src/editor/viewport.ts b/packages/core/src/editor/viewport.ts
index 2d5537ba0..193deacd7 100644
--- a/packages/core/src/editor/viewport.ts
+++ b/packages/core/src/editor/viewport.ts
@@ -89,8 +89,9 @@ export function createViewportActions(ctx: EditorContext) {
const previous = currentViewport()
ctx.state.zoom = Math.max(0.02, Math.min(256, level))
- ctx.state.panX = viewW / 2 - centerX
- ctx.state.panY = viewH / 2 - centerY
+ // Keep the world point at the center of the viewport where it was.
+ ctx.state.panX = viewW / 2 - centerX * ctx.state.zoom
+ ctx.state.panY = viewH / 2 - centerY * ctx.state.zoom
ctx.requestRepaint()
emitViewportChanged(previous)
}
diff --git a/packages/core/tests/editor/viewport.test.ts b/packages/core/tests/editor/viewport.test.ts
new file mode 100644
index 000000000..74e3b2dd9
--- /dev/null
+++ b/packages/core/tests/editor/viewport.test.ts
@@ -0,0 +1,26 @@
+import { describe, expect, test } from 'bun:test'
+
+import { createEditor } from '@open-pencil/core/editor'
+
+const VIEWPORT = { width: 800, height: 600 }
+
+/** The world point at the center of the viewport. */
+function center(editor: ReturnType<typeof createEditor>) {
+ const { panX, panY, zoom } = editor.state
+ return { x: (VIEWPORT.width / 2 - panX) / zoom, y: (VIEWPORT.height / 2 - panY) / zoom }
+}
+
+describe('zoomToLevel', () => {
+ test.each([0.5, 1, 4])('keeps the centered point in place when zooming from 2 to %p', (level) => {
+ const editor = createEditor({ getViewportSize: () => VIEWPORT })
+ editor.state.zoom = 2
+ editor.state.panX = -300
+ editor.state.panY = 120
+ const before = center(editor)
+
+ editor.zoomToLevel(level)
+
+ expect(editor.state.zoom).toBe(level)
+ expect(center(editor)).toEqual(before)
+ })
+})
diff --git a/src/components/canvas/CanvasMenu.vue b/src/components/canvas/CanvasMenu.vue
index 03781b0b3..f5c3e5563 100644
--- a/src/components/canvas/CanvasMenu.vue
+++ b/src/components/canvas/CanvasMenu.vue
@@ -8,15 +8,8 @@ import {
ContextMenuSubContent,
ContextMenuPortal
} from 'reka-ui'
-import { computed, type Component } from 'vue'
+import { computed } from 'vue'
import IconChevronRight from '~icons/lucide/chevron-right'
-import IconCombine from '~icons/lucide/combine'
-import IconCopyMinus from '~icons/lucide/copy-minus'
-import IconCopyX from '~icons/lucide/copy-x'
-import IconListCollapse from '~icons/lucide/list-collapse'
-import IconSpline from '~icons/lucide/spline'
-import IconSquaresIntersect from '~icons/lucide/squares-intersect'
-import IconTypeOutline from '~icons/lucide/type-outline'
import {
vTestId,
@@ -59,7 +52,7 @@ function removeSelectedGuide() {
const menuCls = useMenuUI({
content:
- 'min-w-56 shadow-[0_8px_30px_rgb(0_0_0/0.4)] animate-in fade-in zoom-in-95 motion-reduce:animate-none',
+ 'min-w-56 max-w-80 shadow-[0_8px_30px_rgb(0_0_0/0.4)] animate-in fade-in zoom-in-95 motion-reduce:animate-none',
separator: 'my-1'
})
const componentMenu = menu({ tone: 'component' })
@@ -72,24 +65,9 @@ const cls = {
sep: menuCls.separator
}
-const booleanCommandIcons = {
- 'selection.booleanUnion': IconCombine,
- 'selection.booleanSubtract': IconCopyMinus,
- 'selection.booleanIntersect': IconSquaresIntersect,
- 'selection.booleanExclude': IconCopyX,
- 'selection.flatten': IconListCollapse,
- 'selection.outlineText': IconTypeOutline,
- 'selection.outlineStroke': IconSpline
-} satisfies Partial<Record<EditorCommandId, Component>>
-
function contextCommandTestId(id: EditorCommandId | undefined): string | undefined {
return id ? editorCommandMetadata(id).contextTestId : undefined
}
-
-function contextCommandIcon(id: EditorCommandId | undefined): Component | undefined {
- if (!id) return undefined
- return (booleanCommandIcons as Partial<Record<EditorCommandId, Component>>)[id]
-}
</script>
<template>
@@ -173,14 +151,7 @@ function contextCommandIcon(id: EditorCommandId | undefined): Component | undefi
@select="!sub.separator && sub.action?.()"
>
<template v-if="!sub.separator">
- <span class="flex min-w-0 flex-1 items-center gap-2">
- <component
- :is="contextCommandIcon(sub.id)"
- v-if="contextCommandIcon(sub.id)"
- class="size-3.5 shrink-0 text-muted"
- />
- <span class="truncate">{{ sub.label }}</span>
- </span>
+ <span class="min-w-0 flex-1 truncate">{{ sub.label }}</span>
<AppShortcutText v-if="sub.shortcut">{{ sub.shortcut }}</AppShortcutText>
</template>
</ContextMenuItem>
@@ -194,14 +165,7 @@ function contextCommandIcon(id: EditorCommandId | undefined): Component | undefi
:disabled="item.disabled"
@select="item.action?.()"
>
- <span class="flex min-w-0 flex-1 items-center gap-2">
- <component
- :is="contextCommandIcon(item.id)"
- v-if="contextCommandIcon(item.id)"
- class="size-3.5 shrink-0 text-muted"
- />
- <span class="truncate">{{ item.label }}</span>
- </span>
+ <span class="min-w-0 flex-1 truncate">{{ item.label }}</span>
<span
v-if="item.shortcut"
class="text-[11px]"
diff --git a/tests/e2e/canvas/cursor-labels.spec.ts b/tests/e2e/canvas/cursor-labels.spec.ts
new file mode 100644
index 000000000..d3800d9ab
--- /dev/null
+++ b/tests/e2e/canvas/cursor-labels.spec.ts
@@ -0,0 +1,26 @@
+import { expect, test, useEditorSetupWithClear } from '#tests/e2e/fixtures'
+
+test.use({ viewport: { width: 900, height: 700 } })
+
+const editor = useEditorSetupWithClear('/?test&no-chrome&no-rulers')
+
+test('collaborator cursor names are shaped and long names end with an ellipsis', async () => {
+ await editor.page.evaluate(() => {
+ const store = window.openPencil?.getStore?.()
+ if (!store) throw new Error('OpenPencil store not initialized')
+ const red = { r: 0.92, g: 0.34, b: 0.29, a: 1 }
+ const blue = { r: 0.2, g: 0.55, b: 0.95, a: 1 }
+ store.state.panX = 0
+ store.state.panY = 0
+ store.state.zoom = 1
+ store.state.remoteCursors = [
+ { name: 'Orbit', color: blue, x: 80, y: 80 },
+ { name: 'Ava Yoder', color: red, x: 80, y: 160 },
+ { name: 'A collaborator with a very long display name', color: blue, x: 80, y: 240 }
+ ]
+ store.requestRender()
+ })
+ await editor.canvas.waitForRender()
+ editor.canvas.assertNoErrors()
+ expect(await editor.canvas.screenshotCanvasRegion(400, 320)).toMatchSnapshot('cursor-labels.png')
+})
diff --git a/tests/e2e/canvas/cursor-labels.spec.ts-snapshots/cursor-labels-openpencil-darwin.png b/tests/e2e/canvas/cursor-labels.spec.ts-snapshots/cursor-labels-openpencil-darwin.png
new file mode 100644
index 000000000..57f3ef595
Binary files /dev/null and b/tests/e2e/canvas/cursor-labels.spec.ts-snapshots/cursor-labels-openpencil-darwin.png differ