Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix(edgeless): let the canvas viewer's clicks find their link
Found by a fresh review of the viewer:

- the container took the pointer capture on every press, so a browser
  sent the click to the container and no link, page reference or card
  was ever found; the capture is now taken once a press becomes a drag;
- a link stored without a scheme (`example.com`) navigated as a path on
  the host's site; it is normalised before the safe-URL check, as the
  bookmark block opens it;
- a frozen view's editor host was a second tab stop inside the button
  and woke the editor's keyboard handler on focus; it is now inert;
- a drag pans in model units under a CSS-scaled host, as the editor does;
- a mount that throws before the host is attached disposes the workspace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
Mathieu Jolly and claude committed Nov 10, 2026
commit f9c39d2a040d53f008049809e4e0575e2a2b5f82
41 changes: 41 additions & 0 deletions packages/affine/all/src/__tests__/canvas-viewer.unit.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ function fixture(): Uint8Array {
},
note
);
// A link stored without a scheme, as an import or a connector writes it.
store.addBlock(
'affine:bookmark',
{
id: 'bm-1',
url: 'example.com',
title: 'Example',
description: 'A site',
},
note
);
store.addBlock(
'affine:frame',
{ title: new Text('A frame'), xywh: '[500,0,300,200]' },
Expand Down Expand Up @@ -177,13 +188,41 @@ describe('the canvas viewer', () => {
reference!.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(onOpenDoc).toHaveBeenCalledWith('p-2');

const card = container.querySelector('[data-block-id="bm-1"]');
expect(card).not.toBeNull();
card!.dispatchEvent(new MouseEvent('click', { bubbles: true }));
// Normalised as the bookmark block opens it, never a path on the site.
expect(onLinkClick).toHaveBeenCalledWith(
expect.stringMatching(/^https:\/\/example\.com\/?$/)
);
onLinkClick.mockClear();

const anchor = document.createElement('a');
anchor.href = 'javascript:alert(1)';
container.append(anchor);
anchor.dispatchEvent(new MouseEvent('click', { bubbles: true }));
expect(onLinkClick).not.toHaveBeenCalled();
});

test('a press takes no pointer capture until it becomes a drag', async () => {
// A captured press would send its click to the container, and a click on
// a link would no longer find the link (a real browser, not happy-dom).
const { container } = await mount();
const capture = vi.fn();
container.setPointerCapture = capture;
container.hasPointerCapture = () => false;
const at = { pointerId: 7, clientX: 10, clientY: 10, bubbles: true };
container.dispatchEvent(new PointerEvent('pointerdown', at));
container.dispatchEvent(new PointerEvent('pointerup', at));
expect(capture).not.toHaveBeenCalled();

container.dispatchEvent(new PointerEvent('pointerdown', at));
container.dispatchEvent(
new PointerEvent('pointermove', { ...at, clientX: 40 })
);
expect(capture).toHaveBeenCalledWith(7);
});

test('dispose empties the host and can run twice', async () => {
const { host, viewer } = await mount();
viewer.dispose();
Expand Down Expand Up @@ -262,6 +301,8 @@ describe('the canvas viewer on one frame', () => {
expect(onExpand).toHaveBeenCalledTimes(2);
expect(container.getAttribute('role')).toBe('button');
expect(container.getAttribute('aria-label')).toBe('The map');
// One button: nothing inside it takes focus or keys.
expect((editorHost as HTMLElement & { inert: boolean }).inert).toBe(true);
});

test('interactive on demand: it fills the host and zooms', async () => {
Expand Down
44 changes: 32 additions & 12 deletions packages/affine/all/src/canvas-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ViewportElementExtension } from '@labre/affine-shared/services';
import {
isSafeLinkUrl,
isTouchPadPinchEvent,
normalizeUrl,
} from '@labre/affine-shared/utils';
import { BlockStdScope, LifeCycleWatcher } from '@labre/std';
import { DisposableGroup } from '@labre/global/disposable';
Expand Down Expand Up @@ -298,10 +299,20 @@ export function mountCanvasViewer(
ViewportElementExtension('[data-labre-canvas-viewer]'),
CanvasViewerUnlock,
];
std = new BlockStdScope({ store, extensions });
const editorHost = std.render();
container.append(editorHost);
host.append(container);
try {
std = new BlockStdScope({ store, extensions });
const editorHost = std.render();
// A frozen view is one button: nothing inside it takes focus, keys or
// pointers (the editor host would otherwise be a second tab stop, and
// its keyboard handler would wake on focus).
if (!interactive) editorHost.inert = true;
container.append(editorHost);
host.append(container);
} catch (error) {
container.remove();
workspace.dispose();
throw error;
}

const gfx = () => std?.get(GfxControllerIdentifier);
const pointers = new Map<number, { x: number; y: number }>();
Expand All @@ -328,7 +339,6 @@ export function mountCanvasViewer(
e.stopPropagation();
if (pointers.size === 0) travelled = 0;
pointers.set(e.pointerId, local(e));
container.setPointerCapture?.(e.pointerId);
};

const onPointerMove = (e: PointerEvent) => {
Expand All @@ -340,12 +350,19 @@ export function mountCanvasViewer(
const delta = { x: next.x - last.x, y: next.y - last.y };
travelled += Math.abs(delta.x) + Math.abs(delta.y);
pointers.set(e.pointerId, next);
// Captured only once it is a drag: a captured press sends its click to
// the container, and a click on a link would no longer find the link.
if (
travelled > CLICK_SLOP_PX &&
!container.hasPointerCapture?.(e.pointerId)
) {
container.setPointerCapture?.(e.pointerId);
}

if (pointers.size === 1) {
viewport.applyDeltaCenter(
-delta.x / viewport.zoom,
-delta.y / viewport.zoom
);
// As the editor's pan: model units, under any CSS scale of the host.
const scale = viewport.zoom * viewport.viewScale;
viewport.applyDeltaCenter(-delta.x / scale, -delta.y / scale);
return;
}
const [other] = [...pointers].find(([id]) => id !== e.pointerId) ?? [];
Expand Down Expand Up @@ -376,9 +393,12 @@ export function mountCanvasViewer(
options.onOpenDoc?.(link.docId);
return;
}
if (!isSafeLinkUrl(link.url)) return;
if (options.onLinkClick) options.onLinkClick(link.url);
else window.location.assign(link.url);
// As the bookmark block opens its link: a stored `example.com` is
// `https://example.com`, never a path on the host's site.
const url = normalizeUrl(link.url);
if (!isSafeLinkUrl(url)) return;
if (options.onLinkClick) options.onLinkClick(url);
else window.location.assign(url);
};

// A frozen view is a picture with one gesture, like the surface reference
Expand Down
Loading