Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
"build-module/bindings/**"
],
"dependencies": {
"@floating-ui/react-dom": "2.0.8",
"@wordpress/a11y": "file:../a11y",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/base-styles": "file:../base-styles",
Expand Down
6 changes: 4 additions & 2 deletions packages/editor/src/components/collab-sidebar/board-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export function createBoardStore() {

registerThread( id, blockEl, floatingEl ) {
blockRefs.set( id, blockEl );

const prev = floatingRefs.get( id );
if ( prev && prev !== floatingEl ) {
observer.unobserve( prev );
Expand All @@ -56,7 +55,6 @@ export function createBoardStore() {
idByElement.set( floatingEl, id );
observer.observe( floatingEl );
}

emit();
},

Expand All @@ -79,5 +77,9 @@ export function createBoardStore() {
)
);
},

getFirstBlockElement() {
return blockRefs.values().next().value ?? null;
},
};
}
39 changes: 24 additions & 15 deletions packages/editor/src/components/collab-sidebar/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { unlock } from '../../lock-unlock';
import CommentAuthorInfo from './comment-author-info';
import CommentForm from './comment-form';
import { focusCommentThread, getCommentExcerpt } from './utils';
import { useFloatingBoard, useFloatingThread } from './hooks';
import { useFloatingBoard } from './hooks';
import { FloatingContainer } from './floating-container';
import { AddComment } from './add-comment';
import { store as editorStore } from '../../store';
Expand Down Expand Up @@ -174,13 +174,13 @@ export function Comments( {
}
}, [ noteFocused, selectedNote, selectNote, commentSidebarRef ] );

const { boardOffsets, registerThread, unregisterThread } = useFloatingBoard(
{
const { notePositions, registerThread, unregisterThread } =
useFloatingBoard( {
threads,
selectedNoteId: selectedNote,
isFloating,
}
);
commentSidebarRef,
} );

const handleThreadNavigation = ( event, thread, isSelected ) => {
if ( event.defaultPrevented ) {
Expand Down Expand Up @@ -278,8 +278,7 @@ export function Comments( {
floating={
isFloating
? {
calculatedOffset:
boardOffsets[ thread.id ] ?? 0,
y: notePositions[ thread.id ],
registerThread,
unregisterThread,
}
Expand Down Expand Up @@ -318,14 +317,22 @@ function Thread( {
toggleBlockHighlight,
50
);
const { y, refs } = useFloatingThread( {
thread,
calculatedOffset: floating?.calculatedOffset ?? 0,
registerThread: floating?.registerThread,
unregisterThread: floating?.unregisterThread,
} );
const floatingRef = useRef( null );
const isKeyboardTabbingRef = useRef( false );

const registerThread = floating?.registerThread;
const unregisterThread = floating?.unregisterThread;

// Register block + floating elements with the board.
// The board's ResizeObserver and autoUpdate track changes automatically.
useEffect( () => {
const floatingEl = floatingRef.current;
if ( floatingEl && registerThread ) {
registerThread( thread.id, relatedBlockElement, floatingEl );
}
return () => unregisterThread?.( thread.id );
}, [ relatedBlockElement, thread.id, registerThread, unregisterThread ] );

const onMouseEnter = () => {
debouncedToggleBlockHighlight( thread.blockClientId, true );
};
Expand Down Expand Up @@ -414,14 +421,16 @@ function Thread( {
<AddComment
onSubmit={ onAddReply }
commentSidebarRef={ commentSidebarRef }
floating={ { y, refs } }
floating={ { y: floating.y, ref: floatingRef } }
/>
);
}

return (
<FloatingContainer
floating={ isFloating ? { y, refs } : undefined }
floating={
isFloating ? { y: floating.y, ref: floatingRef } : undefined
}
className={ clsx( 'editor-collab-sidebar-panel__thread', {
'is-selected': isSelected,
} ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function FloatingContainer( {
return (
<VStack
className={ clsx( className, { 'is-floating': isFloating } ) }
ref={ isFloating ? floating.refs.setFloating : undefined }
ref={ isFloating ? floating.ref : undefined }
style={ isFloating ? { top: floating.y, ...style } : style }
{ ...props }
>
Expand Down
116 changes: 45 additions & 71 deletions packages/editor/src/components/collab-sidebar/hooks.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/**
* External dependencies
*/
import {
useFloating,
offset as offsetMiddleware,
autoUpdate,
} from '@floating-ui/react-dom';

/**
* WordPress dependencies
*/
Expand All @@ -24,6 +15,7 @@ import {
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { store as noticesStore } from '@wordpress/notices';
import { getScrollContainer } from '@wordpress/dom';
import { decodeEntities } from '@wordpress/html-entities';
import { store as interfaceStore } from '@wordpress/interface';

Expand All @@ -34,9 +26,9 @@ import { store as editorStore } from '../../store';
import { FLOATING_NOTES_SIDEBAR } from './constants';
import { unlock } from '../../lock-unlock';
import { createBoardStore } from './board-store';
import { calculateAllOffsets } from './utils';
import { calculateNotePositions } from './utils';

const { useBlockElement, cleanEmptyObject } = unlock( blockEditorPrivateApis );
const { cleanEmptyObject } = unlock( blockEditorPrivateApis );

export function useBlockComments( postId ) {
const queryArgs = {
Expand Down Expand Up @@ -333,92 +325,74 @@ export function useEnableFloatingSidebar( enabled = false ) {
}, [ enabled, registry ] );
}

export function useFloatingBoard( { threads, selectedNoteId, isFloating } ) {
const [ boardOffsets, setBoardOffsets ] = useState( {} );
export function useFloatingBoard( {
threads,
selectedNoteId,
isFloating,
commentSidebarRef,
} ) {
const [ notePositions, setNotePositions ] = useState( {} );
const [ store ] = useState( createBoardStore );
const { setCanvasMinHeight } = unlock( useDispatch( editorStore ) );

const heights = useSyncExternalStore( store.subscribe, store.getSnapshot );

// Recalc is deferred to a rAF; the cleanup cancels the pending frame
// when deps change, so back-to-back updates collapse into one paint.
// Notes are positioned in canvas content-space; CSS inherits
// `--canvas-scroll` to translate each thread in sync with the canvas.
useEffect( () => {
if ( ! isFloating ) {
if ( ! isFloating || ! commentSidebarRef?.current ) {
return;
}

const panel = commentSidebarRef.current;
const blockEl = store.getFirstBlockElement();
// Climb to the block-list root so nested scroll containers
// (e.g. a Group with overflow:auto) don't shadow the canvas.
const rootEl = blockEl?.closest( '.is-root-container' ) ?? blockEl;
const canvas = rootEl ? getScrollContainer( rootEl ) : null;

const applyScroll = () => {
panel.style.setProperty(
'--canvas-scroll',
`${ -( canvas?.scrollTop ?? 0 ) }px`
);
};

// Recalc is deferred to a rAF; back-to-back updates collapse into one paint.
const rafId = window.requestAnimationFrame( () => {
const { offsets, minHeight } = calculateAllOffsets( {
const result = calculateNotePositions( {
threads,
selectedNoteId,
blockRects: store.getBlockRects(),
heights,
scrollTop: canvas?.scrollTop ?? 0,
} );
setBoardOffsets( offsets );
setCanvasMinHeight( minHeight );

setNotePositions( result.positions );
applyScroll();
} );

return () => window.cancelAnimationFrame( rafId );
// Root scrolling elements (documentElement/body) don't fire scroll
// on themselves; capture on the window catches them in either canvas.
const view = canvas?.ownerDocument?.defaultView;
const listenerOptions = { passive: true, capture: true };
view?.addEventListener( 'scroll', applyScroll, listenerOptions );

return () => {
window.cancelAnimationFrame( rafId );
view?.removeEventListener( 'scroll', applyScroll, listenerOptions );
};
}, [
commentSidebarRef,
heights,
isFloating,
selectedNoteId,
setCanvasMinHeight,
store,
threads,
] );

return {
boardOffsets,
notePositions,
registerThread: store.registerThread,
unregisterThread: store.unregisterThread,
};
}

export function useFloatingThread( {
thread,
calculatedOffset,
registerThread,
unregisterThread,
} ) {
const blockElement = useBlockElement( thread.blockClientId );

// Use floating-ui to track the block element's position with the calculated offset.
const { y, refs } = useFloating( {
placement: 'right-start',
middleware: [
offsetMiddleware( {
crossAxis: calculatedOffset || -16,
} ),
],
whileElementsMounted: autoUpdate,
} );

// Set the floating-ui reference element.
useEffect( () => {
if ( blockElement ) {
refs.setReference( blockElement );
}
}, [ blockElement, refs ] );

// Register block + floating elements with the board.
// The board's ResizeObserver tracks height changes automatically.
useEffect( () => {
const floatingEl = refs.floating?.current;
if ( floatingEl && registerThread ) {
registerThread( thread.id, blockElement, floatingEl );
}
return () => unregisterThread?.( thread.id );
}, [
blockElement,
thread.id,
refs.floating,
registerThread,
unregisterThread,
] );

return {
y,
refs,
};
}
5 changes: 5 additions & 0 deletions packages/editor/src/components/collab-sidebar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
}

.editor-collab-sidebar-panel {
position: relative;
padding: $grid-unit-20 $grid-unit-20 $grid-unit-30;
height: 100%;
overflow: hidden;
Expand Down Expand Up @@ -46,6 +47,10 @@
right: $grid-unit-20;
position: absolute;
margin-top: $grid-unit-20;
// Inherits `--canvas-scroll` from the panel (set by `useFloatingBoard`)
// so each thread tracks the canvas scroll.
transform: translateY(var(--canvas-scroll, 0));
will-change: transform;
}
}

Expand Down
Loading
Loading