From 1b6d873cf9f91e943e162adf00a174c978690d59 Mon Sep 17 00:00:00 2001 From: Aki Hamano Date: Sat, 29 Nov 2025 11:56:17 +0900 Subject: [PATCH 1/4] Notes: Forward mouse wheel events from floating sidebar to editor canvas --- .../src/components/collab-sidebar/comments.js | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index e24727fd41bf5a..e99dadfb709464 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -23,12 +23,15 @@ import { FlexItem, privateApis as componentsPrivateApis, } from '@wordpress/components'; -import { useDebounce } from '@wordpress/compose'; +import { useDebounce, useRefEffect } from '@wordpress/compose'; import { published, moreVertical } from '@wordpress/icons'; import { __, _x, sprintf, _n } from '@wordpress/i18n'; import { useSelect, useDispatch } from '@wordpress/data'; -import { __unstableStripHTML as stripHTML } from '@wordpress/dom'; +import { + __unstableStripHTML as stripHTML, + getScrollContainer, +} from '@wordpress/dom'; import { store as blockEditorStore, privateApis as blockEditorPrivateApis, @@ -95,6 +98,17 @@ export function Comments( { const relatedBlockElement = useBlockElement( selectedBlockClientId ); + const editorCanvasElement = useMemo( () => { + if ( ! relatedBlockElement ) { + return null; + } + const editorBody = relatedBlockElement.closest( 'body' ); + if ( ! editorBody ) { + return null; + } + return getScrollContainer( editorBody ); + }, [ relatedBlockElement ] ); + const threads = useMemo( () => { const t = [ ...noteThreads ]; const orderedThreads = []; @@ -174,6 +188,34 @@ export function Comments( { setBlockRefs( ( prev ) => ( { ...prev, [ id ]: blockRef } ) ); }, [] ); + // Pass wheel events from the comments sidebar to the editor canvas. + // Only enable this for floating sidebar, as the regular sidebar can scroll itself. + const scrollToCanvasEffect = useRefEffect( + ( node ) => { + if ( ! isFloating || ! editorCanvasElement ) { + return; + } + + function onWheel( event ) { + const { deltaX, deltaY } = event; + editorCanvasElement.scrollBy( deltaX, deltaY ); + } + const options = { passive: true }; + node.addEventListener( 'wheel', onWheel, options ); + return () => { + node.removeEventListener( 'wheel', onWheel, options ); + }; + }, + [ editorCanvasElement, isFloating ] + ); + + // Apply the scroll effect to the sidebar ref. + useEffect( () => { + if ( commentSidebarRef?.current ) { + return scrollToCanvasEffect( commentSidebarRef.current ); + } + }, [ commentSidebarRef, scrollToCanvasEffect ] ); + // Recalculate floating comment thread offsets whenever the heights change. useEffect( () => { /** From 71e06953291762cb77d2060ed93ca6e222837be3 Mon Sep 17 00:00:00 2001 From: Aki Hamano Date: Mon, 1 Dec 2025 19:20:30 +0900 Subject: [PATCH 2/4] Attach events inside the effect --- .../src/components/collab-sidebar/comments.js | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index e99dadfb709464..dc3220ca9a5094 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -23,7 +23,7 @@ import { FlexItem, privateApis as componentsPrivateApis, } from '@wordpress/components'; -import { useDebounce, useRefEffect } from '@wordpress/compose'; +import { useDebounce } from '@wordpress/compose'; import { published, moreVertical } from '@wordpress/icons'; import { __, _x, sprintf, _n } from '@wordpress/i18n'; @@ -190,31 +190,27 @@ export function Comments( { // Pass wheel events from the comments sidebar to the editor canvas. // Only enable this for floating sidebar, as the regular sidebar can scroll itself. - const scrollToCanvasEffect = useRefEffect( - ( node ) => { - if ( ! isFloating || ! editorCanvasElement ) { - return; - } + useEffect( () => { + if ( ! isFloating || ! editorCanvasElement ) { + return; + } - function onWheel( event ) { - const { deltaX, deltaY } = event; - editorCanvasElement.scrollBy( deltaX, deltaY ); - } - const options = { passive: true }; - node.addEventListener( 'wheel', onWheel, options ); - return () => { - node.removeEventListener( 'wheel', onWheel, options ); - }; - }, - [ editorCanvasElement, isFloating ] - ); + const node = commentSidebarRef?.current; + if ( ! node ) { + return; + } - // Apply the scroll effect to the sidebar ref. - useEffect( () => { - if ( commentSidebarRef?.current ) { - return scrollToCanvasEffect( commentSidebarRef.current ); + function onWheel( event ) { + const { deltaX, deltaY } = event; + editorCanvasElement.scrollBy( deltaX, deltaY ); } - }, [ commentSidebarRef, scrollToCanvasEffect ] ); + const options = { passive: true }; + node.addEventListener( 'wheel', onWheel, options ); + + return () => { + node.removeEventListener( 'wheel', onWheel, options ); + }; + }, [ commentSidebarRef, editorCanvasElement, isFloating ] ); // Recalculate floating comment thread offsets whenever the heights change. useEffect( () => { From 3459efc4457651f0ac4096eb9d8af5293e138fec Mon Sep 17 00:00:00 2001 From: Aki Hamano Date: Fri, 24 Apr 2026 17:10:40 +0900 Subject: [PATCH 3/4] Collab sidebar: resolve canvas scroll container independently of block selection The floating comments sidebar forwards wheel events to the editor canvas, but previously the canvas was looked up via the selected block. When no block was selected, the lookup returned null and wheel forwarding stopped working. Resolve the canvas from the first block in the editor so scroll forwarding keeps working regardless of selection state. Co-Authored-By: Claude Opus 4.7 (1M context) --- packages/editor/src/components/collab-sidebar/comments.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/comments.js b/packages/editor/src/components/collab-sidebar/comments.js index ff7e7d271aab4b..2cad4ee1be1d1f 100644 --- a/packages/editor/src/components/collab-sidebar/comments.js +++ b/packages/editor/src/components/collab-sidebar/comments.js @@ -88,17 +88,18 @@ export function Comments( { }, [] ); const relatedBlockElement = useBlockElement( selectedBlockClientId ); + const firstBlockElement = useBlockElement( orderedBlockIds[ 0 ] ); const editorCanvasElement = useMemo( () => { - if ( ! relatedBlockElement ) { + if ( ! firstBlockElement ) { return null; } - const editorBody = relatedBlockElement.closest( 'body' ); + const editorBody = firstBlockElement.closest( 'body' ); if ( ! editorBody ) { return null; } return getScrollContainer( editorBody ); - }, [ relatedBlockElement ] ); + }, [ firstBlockElement ] ); const threads = useMemo( () => { const t = [ ...noteThreads ]; From 6c09550ca8f1296ef1fa91b0361d7c402ed7e5f6 Mon Sep 17 00:00:00 2001 From: Aki Hamano Date: Mon, 4 May 2026 18:35:22 +0900 Subject: [PATCH 4/4] Notes: Defer wheel forwarding to inner scrollers, normalize delta to pixels Lets a scrollable element inside the floating sidebar (e.g. a tall thread) consume the wheel until it hits its edge, then resumes canvas scroll. Also normalizes `WheelEvent.deltaY` by `deltaMode` so Firefox's line/page-mode events scroll the canvas at native speeds instead of crawling a few pixels per notch. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../src/components/collab-sidebar/hooks.js | 54 +++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/components/collab-sidebar/hooks.js b/packages/editor/src/components/collab-sidebar/hooks.js index b5fd9f04eea325..cde9348d4f445b 100644 --- a/packages/editor/src/components/collab-sidebar/hooks.js +++ b/packages/editor/src/components/collab-sidebar/hooks.js @@ -319,6 +319,47 @@ export function useEnableFloatingSidebar( enabled = false ) { }, [ enabled, registry ] ); } +// Return true if a vertical scroll container inside `panel` should +// consume the wheel before we forward it to the canvas. The edge check +// lets the canvas resume scrolling once the inner scroller reaches its +// top/bottom. +function isWheelAbsorbedInside( panel, event ) { + const { deltaY, target } = event; + const inner = getScrollContainer( target, 'vertical' ); + if ( ! inner || ! panel.contains( inner ) ) { + return false; + } + const maxScrollTop = inner.scrollHeight - inner.clientHeight; + const isScrollingDown = deltaY > 0; + const canScrollDown = inner.scrollTop < maxScrollTop; + const canScrollUp = inner.scrollTop > 0; + return isScrollingDown ? canScrollDown : canScrollUp; +} + +// Normalize `WheelEvent.deltaY` to CSS pixels by branching on `deltaMode`. +// Firefox on Windows/Linux can report lines/pages instead of pixels, +// which would otherwise crawl when forwarded to `scrollBy`. +function getWheelDeltaYInPixels( event, canvas, view ) { + const { deltaY, deltaMode } = event; + if ( deltaMode === 1 ) { + // DOM_DELTA_LINE: scale by the canvas's line height, + // falling back to `font-size × 1.2` when it resolves to `normal`. + const canvasStyle = view.getComputedStyle( canvas ); + const parsedLineHeight = parseFloat( canvasStyle.lineHeight ); + const parsedFontSize = parseFloat( canvasStyle.fontSize ); + const lineHeight = Number.isFinite( parsedLineHeight ) + ? parsedLineHeight + : parsedFontSize * 1.2; + return deltaY * lineHeight; + } + if ( deltaMode === 2 ) { + // DOM_DELTA_PAGE: scale by the canvas height. + return deltaY * canvas.clientHeight; + } + // DOM_DELTA_PIXEL: already in pixels, so use as-is. + return deltaY; +} + export function useFloatingBoard( { threads, selectedNoteId, @@ -371,10 +412,17 @@ export function useFloatingBoard( { const listenerOptions = { passive: true, capture: true }; view?.addEventListener( 'scroll', applyScroll, listenerOptions ); - // Wheel events over the floating sidebar should scroll the canvas, - // since the sidebar itself isn't a scroll container in floating mode. + // Wheel events over the floating sidebar should scroll the canvas. const onWheel = ( event ) => { - canvas?.scrollBy( event.deltaX, event.deltaY ); + if ( ! canvas || isWheelAbsorbedInside( panel, event ) ) { + return; + } + const deltaYInPixels = getWheelDeltaYInPixels( + event, + canvas, + panel.ownerDocument.defaultView + ); + canvas.scrollBy( 0, deltaYInPixels ); }; panel.addEventListener( 'wheel', onWheel, { passive: true } );