Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(md): keep comment cards aligned during document reflow (#7200)
gbirmana1a996aba752
diff --git a/apps/web/src/features/block-md/comments/commentLayout.test.tsx b/apps/web/src/features/block-md/comments/commentLayout.test.tsxnew file mode 100644index 0000000000..81023fd4fa--- /dev/null+++ b/apps/web/src/features/block-md/comments/commentLayout.test.tsx@@ -0,0 +1,149 @@+import { cleanup, render } from '@solidjs/testing-library';+import { createEditor } from 'lexical';+import { createStore } from 'solid-js/store';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';+import { createCommentLayout } from './commentLayout';+import type { MarkStore } from './commentType';++const listeners = vi.hoisted(() => ({+ layoutShift: () => {},+ editorResize: () => {},+}));++vi.mock('@core/component/LexicalMarkdown/plugins', () => ({+ autoRegister: () => {},+ registerInternalLayoutShiftListener: (+ _editor: unknown,+ callback: () => void+ ) => {+ listeners.layoutShift = callback;+ return () => {};+ },+ registerEditorWidthObserver: (_editor: unknown, callback: () => void) => {+ listeners.editorResize = callback;+ return () => {};+ },+}));++vi.mock('@solid-primitives/resize-observer', () => ({+ createElementSize: () => ({ width: 1200, height: 10000 }),+}));++let state: ReturnType<typeof createFixture>['state'];+vi.mock('../context/markdown-document-context', () => ({+ useMarkdownDocument: () => ({ state }),+}));++class ResizeObserverStub implements ResizeObserver {+ static instances: ResizeObserverStub[] = [];+ targets = new Set<Element>();++ constructor(private callback: ResizeObserverCallback) {+ ResizeObserverStub.instances.push(this);+ }++ observe(target: Element) {+ this.targets.add(target);+ }++ unobserve(target: Element) {+ this.targets.delete(target);+ }++ disconnect() {+ this.targets.clear();+ }++ notify() {+ this.callback([], this);+ }+}++function createFixture() {+ const notebook = document.createElement('div');+ const margin = document.createElement('div');+ const mark = document.createElement('mark');+ let scrollTop = 5500;+ let anchorTop = 6000;+ vi.spyOn(margin, 'getBoundingClientRect').mockImplementation(+ () => new DOMRect(800, -scrollTop, 320, 10000)+ );+ vi.spyOn(mark, 'getBoundingClientRect').mockImplementation(+ () => new DOMRect(0, anchorTop - scrollTop, 100, 20)+ );+ const marks: MarkStore = {+ anchor: {+ id: 'anchor',+ markNodes: { node: mark },+ existsOnServer: true,+ owner: 'user',+ isDraft: false,+ },+ };+ const [fixtureState] = createStore({+ editor: {+ md: { notebook, commentMargin: margin, editor: createEditor() },+ },+ comments: { marks, activeMarkIds: [] as string[] },+ });+ return {+ state: fixtureState,+ margin,+ mark,+ // A resizing embed above the viewport triggers scroll anchoring, keeping+ // the highlighted text at the same screen Y despite its new document Y.+ resizeAboveAnchor(delta: number) {+ anchorTop += delta;+ scrollTop += delta;+ },+ };+}++beforeEach(() => {+ vi.useFakeTimers();+ ResizeObserverStub.instances = [];+ vi.stubGlobal('ResizeObserver', ResizeObserverStub);+});++afterEach(() => {+ cleanup();+ vi.useRealTimers();+ vi.restoreAllMocks();+ vi.unstubAllGlobals();+});++describe('comment layout during document reflow', () => {+ it.each(['margin resize', 'editor resize', 'internal layout shift'])(+ 'keeps comments beside their text through consecutive %s events',+ (event) => {+ const fixture = createFixture();+ state = fixture.state;+ let layout!: ReturnType<typeof createCommentLayout>;+ render(() => {+ layout = createCommentLayout();+ return null;+ });+ const notify = () => {+ if (event === 'editor resize') return listeners.editorResize();+ if (event === 'internal layout shift') return listeners.layoutShift();+ ResizeObserverStub.instances+ .find((observer) => observer.targets.has(fixture.margin))!+ .notify();+ };+ const cardTop = () =>+ fixture.margin.getBoundingClientRect().top ++ layout.threadPositions.anchor!.layout.calculatedYPos;+ const textTop = fixture.mark.getBoundingClientRect().top;+ expect(cardTop()).toBe(textTop);++ // Later events are inside the old 60ms throttle window. Each must+ // correct the position before paint, without waiting for a timer.+ for (const delta of [342, 200, -542]) {+ fixture.resizeAboveAnchor(delta);+ notify();+ expect(fixture.mark.getBoundingClientRect().top).toBe(textTop);+ expect(cardTop()).toBe(textTop);+ }+ }+ );+});diff --git a/apps/web/src/features/block-md/comments/commentLayout.ts b/apps/web/src/features/block-md/comments/commentLayout.tsindex d5de59b3eb..7d3b2cbc34 100644--- a/apps/web/src/features/block-md/comments/commentLayout.ts+++ b/apps/web/src/features/block-md/comments/commentLayout.ts@@ -90,20 +90,22 @@ export function createCommentLayout() {// The margin is display:none until the document has a comment; re-measure// once it is laid out.- const marginResizeObserver = new ResizeObserver(() => throttledUpdate());+ // ResizeObserver runs before paint. Keep geometry changes synchronous:+ // scroll anchoring can hold the text still while a resizing embed moves+ // the margin, so throttling here paints cards at stale document offsets.+ const marginResizeObserver = new ResizeObserver(updateMarkPositions);marginResizeObserver.observe(commentMargin);onCleanup(() => marginResizeObserver.disconnect());- // Throttle updates on comment positions. Update on (1) editor updates, (2)- // internal layout shifts causes by non-updating element height changed to- // embeds and media and (3) editor width changes.+ // Coalesce editor updates, but measure layout shifts and editor resizes+ // immediately so the margin and its text anchors move in the same frame.if (md.editor) {autoRegister(md.editor.registerUpdateListener(() => {throttledUpdate();}),- registerInternalLayoutShiftListener(md.editor, throttledUpdate),- registerEditorWidthObserver(md.editor, throttledUpdate)+ registerInternalLayoutShiftListener(md.editor, updateMarkPositions),+ registerEditorWidthObserver(md.editor, updateMarkPositions));}});diff --git a/docs/AGENT_GUIDE/documents.md b/docs/AGENT_GUIDE/documents.mdindex ef95048b00..490db147eb 100644--- a/docs/AGENT_GUIDE/documents.md+++ b/docs/AGENT_GUIDE/documents.md@@ -519,6 +519,12 @@ keeps at least 16px ofbottom clearance above the drawer's curve, including while the keyboard is open,and accounts for the home-indicator safe area when the keyboard is closed.+When checking desktop margin placement, scroll a long document while an embed+or image above the highlighted text changes height. Scroll anchoring may keep+the text at the same screen position; its comment card should stay aligned+through the resize, without briefly jumping upward or downward. Repeat with+several rapid height changes, including while scrolling has paused.+Also verify anchored comments in Drive's detail pane: open a document withexisting text anchors, then click a numbered comment badge to expand it. Thedocument should stay visible and the thread should open; loading the document