Skip to content

Commit 4b4a78b

Browse files
authored
fix(files): keep Markdown tables readable while scrolling (#10041)
* fix(files): keep Markdown tables readable while scrolling * fix(files): track Markdown overlays during table scrolling * fix(files): contain wrapped inline code in WebKit * fix(files): preserve short Markdown table layout * fix(files): clean up Markdown fixtures after API failures
1 parent 13949ec commit 4b4a78b

15 files changed

Lines changed: 883 additions & 55 deletions

File tree

‎.github/ci-suites.json‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
{
4444
"group": "project-files",
4545
"task": "ci:e2e-project-files"
46+
},
47+
{
48+
"group": "markdown-viewer",
49+
"task": "ci:e2e-markdown-viewer"
4650
}
4751
],
4852
"production": [

‎.github/scripts/ci-plan.gql‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ query($base: String!, $head: String!) {
55
"ci:e2e-scim", "ci:e2e-cli", "ci:e2e-stop-after", "ci:e2e-desktop-inbox",
66
"ci:e2e-assistant-actions", "ci:e2e-organization-search",
77
"ci:e2e-docs-platform",
8-
"ci:e2e-browser-extension", "ci:e2e-project-files", "ci:e2e-mobile", "ci:e2e-seo"
8+
"ci:e2e-browser-extension", "ci:e2e-project-files", "ci:e2e-mobile", "ci:e2e-seo",
9+
"ci:e2e-markdown-viewer"
910
]) {
1011
items {
1112
name

‎.github/scripts/http-e2e.sh‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# CI groups, which reuse the production build from `e2e-build`.
44
# The job extracts it into apps/sim/.next before running these suites.
55
#
6-
# Usage: http-e2e.sh <scim|cli|stop-after|desktop-inbox|assistant-actions|organization-search|browser-extension|browser-extension-native|browser-extension-navigation|browser-extension-composer|browser-extension-access|project-files|mobile|seo> (run from apps/sim)
6+
# Usage: http-e2e.sh <scim|cli|stop-after|desktop-inbox|assistant-actions|organization-search|browser-extension|browser-extension-native|browser-extension-navigation|browser-extension-composer|browser-extension-access|project-files|markdown-viewer|mobile|seo> (run from apps/sim)
77
#
88
# The job provides DATABASE_URL, BETTER_AUTH_SECRET and ENCRYPTION_KEY; each group sets the rest of
99
# its app's environment here. Reports and server logs land in $RUNNER_TEMP/e2e.
@@ -20,7 +20,7 @@
2020
# telemetry flush runs detached and still writes .next/dev).
2121
set -euo pipefail
2222

23-
group=${1:?usage: http-e2e.sh <scim|cli|stop-after|desktop-inbox|assistant-actions|organization-search|browser-extension|browser-extension-native|browser-extension-navigation|browser-extension-composer|browser-extension-access|project-files|mobile|seo>}
23+
group=${1:?usage: http-e2e.sh <scim|cli|stop-after|desktop-inbox|assistant-actions|organization-search|browser-extension|browser-extension-native|browser-extension-navigation|browser-extension-composer|browser-extension-access|project-files|markdown-viewer|mobile|seo>}
2424
report_dir="$RUNNER_TEMP/e2e"
2525
ready_timeout_seconds=600
2626
mkdir -p "$report_dir"
@@ -315,6 +315,22 @@ JS
315315
bun --no-env-file scripts/test-project-files-e2e.ts
316316
;;
317317

318+
markdown-viewer)
319+
app_server=start
320+
export NEXT_PUBLIC_FORCE_HOSTED=false
321+
export INTERNAL_API_SECRET=markdown-viewer-http-ci-local-secret-at-least-32-characters
322+
export NEXT_PUBLIC_SOCKET_URL=http://127.0.0.1:3027 SOCKET_SERVER_URL=http://127.0.0.1:3027
323+
start_browser_install markdown-viewer --only-shell chromium webkit
324+
start_app markdown-viewer 3026 'Markdown viewer' record-http-status
325+
start_realtime markdown-viewer 3027
326+
wait_for_browsers
327+
MARKDOWN_VIEWER_E2E_BASE_URL="$NEXT_PUBLIC_APP_URL" \
328+
MARKDOWN_VIEWER_E2E_DATABASE_URL="$DATABASE_URL" \
329+
MARKDOWN_VIEWER_E2E_AUTH_SECRET="$BETTER_AUTH_SECRET" \
330+
MARKDOWN_VIEWER_E2E_REPORT_PATH="$report_dir/markdown-viewer-ui-report.json" \
331+
bun run test:markdown-viewer:e2e
332+
;;
333+
318334
# MOBILE_E2E_BROWSER (chromium or webkit) narrows the job to one browser; unset runs both.
319335
mobile)
320336
app_server=start

‎.github/workflows/checks.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ jobs:
312312
${{ runner.temp }}/e2e/**/*.json
313313
${{ runner.temp }}/e2e/browser-extension*/**/*.png
314314
${{ runner.temp }}/e2e/*-ui-report.json.artifacts/*.png
315+
${{ runner.temp }}/e2e/markdown-viewer-ui-report.json.artifacts/fixture.md
315316
if-no-files-found: ignore
316317
retention-days: 7
317318

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/document-table.css‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
* Canonical table chrome for the file viewer. Both surfaces that render a table for a file — the
33
* rich markdown editor (`.rich-markdown-prose table`) and the tabular previews CSV/XLSX render
44
* through `DataTable` (`.document-table`) — share this one definition so a table looks the same
5-
* whichever file it came from. Chrome is shared; *sizing* is not — prose fits the document width
6-
* while a preview sizes to its data and scrolls (see the two rules below). Editor-only concerns
7-
* (prose block margin, fixed layout for column resizing, cell paragraph reset) stay in
5+
* whichever file it came from. Both size to their content inside a scrolling viewport.
6+
* Editor-only concerns (scroll wrappers, prose block margin, cell paragraph reset) stay in
87
* rich-markdown-editor.css.
98
*/
109

@@ -21,10 +20,6 @@
2120
overflow: hidden;
2221
}
2322

24-
.rich-markdown-prose table {
25-
width: 100%;
26-
}
27-
2823
/* A preview table is data, not prose. A CSV can carry dozens of columns, so sizing the table to the
2924
frame (`width: 100%`) divides that frame between them and — with `overflow-wrap: anywhere` able to
3025
break every column down to one character — renders each header a vertical column of letters.

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/collaboration/caret-presence.ts‎

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,36 @@ export function caretColorSlot(color: unknown): number {
5858
*/
5959
const CARET_ACTIVE_CLASS = 'collaboration-carets__caret--active'
6060

61-
/** The class that flips the label to the caret's left near the editor's right edge. */
61+
/** The class that flips the label to the caret's left near the viewport's right edge. */
6262
const CARET_FLIP_CLASS = 'collaboration-carets__caret--flip'
6363

6464
/** Per-caret fade timers, keyed by the (reused) caret DOM node. */
6565
const caretFadeTimers = new WeakMap<HTMLElement, ReturnType<typeof setTimeout>>()
6666

67+
function positionCaretLabel(caret: HTMLElement, editorBounds?: DOMRect) {
68+
const label = caret.querySelector<HTMLElement>('.collaboration-carets__label')
69+
const bounds =
70+
caret.closest('.tableWrapper')?.getBoundingClientRect() ??
71+
editorBounds ??
72+
caret.closest('.ProseMirror')?.getBoundingClientRect()
73+
if (!label || !bounds) return
74+
const anchor = caret.getBoundingClientRect().left
75+
const outside = anchor < bounds.left || anchor > bounds.right
76+
caret.toggleAttribute('data-caret-outside', outside)
77+
if (outside) return
78+
const available = Math.max(0, bounds.width - 2)
79+
caret.style.setProperty('--caret-label-max-width', `${available}px`)
80+
const width = label.getBoundingClientRect().width
81+
const flip = anchor - 1 + width > bounds.right - 1
82+
caret.classList.toggle(CARET_FLIP_CLASS, flip)
83+
const left = flip ? anchor + 1 - width : anchor - 1
84+
const bounded = Math.min(Math.max(left, bounds.left + 1), bounds.right - 1 - width)
85+
caret.style.setProperty('--caret-label-offset', `${bounded - left}px`)
86+
}
87+
6788
/**
6889
* Show a peer's name label, (re)start its fade timer, and flip it to the caret's
69-
* left when it would run off the editor's right edge.
90+
* left when it would run off its visible viewport.
7091
*
7192
* yCursorPlugin renders each caret as a keyed widget decoration, so ProseMirror
7293
* REUSES the same DOM node as the caret moves (verified: the render function is
@@ -75,7 +96,7 @@ const caretFadeTimers = new WeakMap<HTMLElement, ReturnType<typeof setTimeout>>(
7596
* (unlike `update`) fires only on a real state change, not the 15s heartbeat, so
7697
* an idle peer's label correctly stays hidden.
7798
*/
78-
export function activateCaretLabel(caret: HTMLElement, editorRight?: number) {
99+
export function activateCaretLabel(caret: HTMLElement, editorBounds?: DOMRect) {
79100
caret.classList.add(CARET_ACTIVE_CLASS)
80101
const existing = caretFadeTimers.get(caret)
81102
if (existing) clearTimeout(existing)
@@ -87,14 +108,7 @@ export function activateCaretLabel(caret: HTMLElement, editorRight?: number) {
87108
}, CARET_LABEL_HOLD_MS)
88109
)
89110

90-
if (editorRight === undefined) return
91-
const label = caret.querySelector<HTMLElement>('.collaboration-carets__label')
92-
if (!label) return
93-
// Measure the default (rightward) position, then flip left only if it overflows.
94-
caret.classList.remove(CARET_FLIP_CLASS)
95-
if (label.getBoundingClientRect().right > editorRight) {
96-
caret.classList.add(CARET_FLIP_CLASS)
97-
}
111+
positionCaretLabel(caret, editorBounds)
98112
}
99113

100114
/**
@@ -144,33 +158,50 @@ export function createCaretActivityExtension(awareness: Awareness): Extension {
144158
new Plugin({
145159
key: new PluginKey('collaborationCaretActivity'),
146160
view: (editorView) => {
147-
// Coalesce bursts of awareness changes into a single rAF per frame, however many
148-
// peers moved: accumulate the changed client ids, then re-activate each matching
149-
// (reused) caret node once. The shared editorRight is read once up front; each moving
150-
// caret then does one getBoundingClientRect for its edge-flip measure (bounded by the
151-
// small number of concurrently-moving peers, not the awareness event rate).
161+
// Layout changes reposition idle names without restarting their activity timers.
152162
let raf = 0
163+
let repositionAll = false
153164
const pending = new Set<number>()
154165
const flush = () => {
155166
raf = 0
156-
const editorRight = editorView.dom.getBoundingClientRect().right
157-
for (const id of pending) {
158-
const caret = editorView.dom.querySelector<HTMLElement>(
159-
`.collaboration-carets__caret[data-caret-client-id="${id}"]`
160-
)
161-
if (caret) activateCaretLabel(caret, editorRight)
167+
const bounds = editorView.dom.getBoundingClientRect()
168+
const carets = repositionAll
169+
? editorView.dom.querySelectorAll<HTMLElement>('.collaboration-carets__caret')
170+
: [...pending].flatMap((id) => {
171+
const caret = editorView.dom.querySelector<HTMLElement>(
172+
`.collaboration-carets__caret[data-caret-client-id="${id}"]`
173+
)
174+
return caret ? [caret] : []
175+
})
176+
for (const caret of carets) {
177+
const id = caret.dataset.caretClientId
178+
if (id !== undefined && pending.has(Number(id))) activateCaretLabel(caret, bounds)
179+
else positionCaretLabel(caret, bounds)
162180
}
181+
repositionAll = false
163182
pending.clear()
164183
}
184+
const onViewportChange = () => {
185+
repositionAll = true
186+
if (!raf) raf = requestAnimationFrame(flush)
187+
}
165188
const onChange = ({ added, updated }: { added: number[]; updated: number[] }) => {
166189
for (const id of added) pending.add(id)
167190
for (const id of updated) pending.add(id)
168191
if (pending.size > 0 && !raf) raf = requestAnimationFrame(flush)
169192
}
170193
awareness.on('change', onChange)
194+
editorView.dom.addEventListener('scroll', onViewportChange, true)
195+
window.addEventListener('resize', onViewportChange)
196+
const observer =
197+
typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(onViewportChange)
198+
observer?.observe(editorView.dom)
171199
return {
172200
destroy: () => {
173201
awareness.off('change', onChange)
202+
editorView.dom.removeEventListener('scroll', onViewportChange, true)
203+
window.removeEventListener('resize', onViewportChange)
204+
observer?.disconnect()
174205
if (raf) cancelAnimationFrame(raf)
175206
// Clear any pending fade timers for this editor's carets so they don't fire on
176207
// detached nodes after unmount (harmless no-op, but a genuine leaked timer).

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/bubble-menu-chrome.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import { cn, MENU_STYLES } from '@sim/emcn'
44
export const BUBBLE_MENU_CLASS = cn(
55
MENU_STYLES.surface,
66
MENU_STYLES.padding,
7-
'scrollbar-none fade-in-0 z-[var(--z-popover)] flex max-w-[calc(100%_-_1rem)] animate-in items-center gap-0.5 overflow-x-auto duration-150 ease-out motion-reduce:animate-none'
7+
'scrollbar-none fade-in-0 absolute top-0 left-0 z-[var(--z-popover)] flex w-max max-w-[calc(100%_-_1rem)] animate-in items-center gap-0.5 overflow-x-auto duration-150 ease-out motion-reduce:animate-none'
88
)

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/bubble-menu.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,11 @@ export function EditorBubbleMenu({
307307
editor.commands.focus()
308308
}
309309

310-
const { resolveAnchor, appendTo } = useBubbleMenuFloating(editor, scrollContainerRef)
310+
const { resolveAnchor, appendTo, menuRef } = useBubbleMenuFloating(
311+
editor,
312+
scrollContainerRef,
313+
bubbleMenuKey
314+
)
311315
const canFocus = useCallback(
312316
() => hasFormattableSelection(editor, editor.state.selection.from, editor.state.selection.to),
313317
[editor]
@@ -334,6 +338,7 @@ export function EditorBubbleMenu({
334338

335339
return (
336340
<BubbleMenu
341+
ref={menuRef}
337342
editor={editor}
338343
pluginKey={bubbleMenuKey}
339344
getReferencedVirtualElement={resolveAnchor}

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/table-menu.tsx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ export function TableBubbleMenu({ editor, scrollContainerRef }: TableBubbleMenuP
4545
}),
4646
})
4747

48-
const { resolveAnchor, appendTo } = useBubbleMenuFloating(editor, scrollContainerRef)
48+
const { resolveAnchor, appendTo, menuRef } = useBubbleMenuFloating(
49+
editor,
50+
scrollContainerRef,
51+
menuKey
52+
)
4953
const canFocus = useCallback(
5054
() =>
5155
shouldShowTableMenu({ editor }) &&
@@ -62,6 +66,7 @@ export function TableBubbleMenu({ editor, scrollContainerRef }: TableBubbleMenuP
6266

6367
return (
6468
<BubbleMenu
69+
ref={menuRef}
6570
editor={editor}
6671
pluginKey={menuKey}
6772
getReferencedVirtualElement={resolveAnchor}

‎apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/rich-markdown-editor/menus/use-bubble-menu-floating.ts‎

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { useCallback } from 'react'
1+
import { useCallback, useEffect, useRef } from 'react'
2+
import { autoUpdate } from '@floating-ui/dom'
23
import { posToDOMRect } from '@tiptap/core'
3-
import { AllSelection, type Selection } from '@tiptap/pm/state'
4+
import { AllSelection, type PluginKey, type Selection } from '@tiptap/pm/state'
45
import type { Editor } from '@tiptap/react'
56

67
/**
@@ -35,21 +36,42 @@ function selectionVirtualElement(editor: Editor) {
3536
* Wires a BubbleMenu's Floating UI concerns — the selection anchor and the portal target — so the
3637
* text and table toolbars share one source of truth and can't drift.
3738
*
38-
* The toolbar is appended into the editor's scroll container (which must be a positioning context)
39-
* and left at TipTap's default `absolute` strategy, so it becomes an absolutely-positioned child of
40-
* the scrolled content and tracks the selection through native scrolling — no scroll listener, so no
41-
* lag or "chase" as the pane scrolls. TipTap's default `flip` still parks it above or below the
42-
* selection at the pane edges, and the container's overflow clips it once the selection scrolls out.
39+
* The toolbar stays inside the editor's scroll container. Its controls can resize after a selection
40+
* changes, and a table can scroll independently of the document, so track both through Floating UI.
4341
*/
4442
export function useBubbleMenuFloating(
4543
editor: Editor,
46-
scrollContainerRef: React.RefObject<HTMLDivElement | null>
44+
scrollContainerRef: React.RefObject<HTMLDivElement | null>,
45+
pluginKey: PluginKey
4746
) {
47+
const menuRef = useRef<HTMLDivElement>(null)
4848
const resolveAnchor = useCallback(() => selectionVirtualElement(editor), [editor])
4949
const appendTo = useCallback(
5050
() => scrollContainerRef.current ?? document.body,
5151
[scrollContainerRef]
5252
)
5353

54-
return { resolveAnchor, appendTo }
54+
useEffect(() => {
55+
const menu = menuRef.current
56+
if (!menu) return
57+
let stop: (() => void) | undefined
58+
const trackSelection = () => {
59+
stop?.()
60+
const { node } = editor.view.domAtPos(editor.state.selection.from)
61+
const reference = node instanceof Element ? node : (node.parentElement ?? editor.view.dom)
62+
stop = autoUpdate(reference, menu, () => {
63+
if (!editor.isDestroyed && menu.isConnected) {
64+
editor.commands.setMeta(pluginKey, 'updatePosition')
65+
}
66+
})
67+
}
68+
trackSelection()
69+
editor.on('selectionUpdate', trackSelection)
70+
return () => {
71+
stop?.()
72+
editor.off('selectionUpdate', trackSelection)
73+
}
74+
}, [editor, pluginKey])
75+
76+
return { resolveAnchor, appendTo, menuRef }
5577
}

0 commit comments

Comments
 (0)