Skip to content

Commit ecbe191

Browse files
authored
feat(files): inline rich markdown editor (#5133)
* feat(files): inline rich markdown editor Replace the raw/preview split for markdown files with a Linear-style inline WYSIWYG editor (TipTap/ProseMirror): bubble + slash menus, code-block language picker with Prism highlighting and line-wrap, resizable images (HTML <img>), GFM tables, and frontmatter held byte-exact out of band. A round-trip preflight gate (decided once per open) falls back to the raw Monaco editor for any file that can't be edited losslessly, so the rich editor never silently corrupts a file. * fix(files): chain autosave unmount flush after in-flight save The unmount flush no longer fires a concurrent PUT alongside an in-flight save; it awaits the in-flight save and then writes the latest content sequentially, so an out-of-order completion can't clobber newer edits with a stale snapshot (addresses Cursor Bugbot). * fix(files): read pasted images from clipboard items, not just files Some browsers expose a pasted or copied image only via DataTransfer.items (with an empty files list), so screenshot paste was silently ignored. extractImageFiles now falls back to items; moved to a testable module with unit tests (addresses Cursor Bugbot). * fix(files): destroy round-trip probe editor on serialization error Wrap the probe serialize() in try/finally so the throwaway Editor is always destroyed even if setContent/getMarkdown throws (addresses Greptile). Adds a test proving PipeSafeTable escapes only interior cell pipes, not structural delimiters. * fix(resource): hold breadcrumb nav latch across the route swap scheduleClose fired on the pointer/focus exit that immediately follows a click-to-navigate and was clearing the reopen latch before the route swapped, letting the popover flash back open. The latch is now released by a short timer instead (addresses Cursor Bugbot). * chore(files): drop platform references and non-essential inline comments * fix(files): scope inline markdown editor to the files view The mothership preview was routing streaming markdown through the inline editor path: it showed Monaco during streaming (previewMode fell back to 'editor') and lost the streamed content on the TextEditor→MarkdownFileEditor swap (the TextEditor unmounted before it could reconcile + autosave). The inline rich editor is now opt-in via a FileViewer prop that only the files view sets, so the mothership keeps its raw/preview streaming editor and persists as before. * fix(mothership): use the inline markdown editor in the chat resource view Idle markdown in the chat resource view now renders the single-surface inline editor (no raw/split/preview pencil toggle), matching the files view. While the agent streams, FileViewer forces the rendered preview instead of Monaco, and the streamed file persists via the agent's server write + the existing content-query invalidation on tool completion — so the idle editor refetches the persisted content. * refactor(files): collapse the duplicate raw-editor fallback branch in the markdown gate * fix(mothership): swap to the inline editor once a file preview finishes streaming The preview session keeps status='complete' and previewText after streaming ends, so streamingContent stayed defined and the file stuck on the read-only rendered preview. Treat content as streaming only while status==='streaming'; once complete the EmbeddedFile sees no streamingContent and mounts the editable inline editor (which refetches the persisted content). The synthetic streaming-file stays a pure preview. * Revert "fix(mothership): swap to the inline editor once a file preview finishes streaming" This reverts commit 25b12e4. * Revert "fix(mothership): use the inline markdown editor in the chat resource view" This reverts commit 9430aa7. * feat(files): rich markdown editor across files + chat, read-only for unsafe, robust load/save - chat resource view streams into the rich editor (streamdown while streaming → editable on completion); agent persists server-side, editor never saves mid-stream - round-trip-unsafe / >128KB markdown renders read-only in the rich editor (no Monaco, no corruption) - markdown always uses the rich editor (dropped the inline-markdown opt-in flag) - editor loads content as TipTap's initial content keyed by file id — strict-mode/SSR-safe, no content-sync effect - fix autosave "Saving…" status suppression under React strict mode - lock the streamed-file persistence handoff with a state-machine lifecycle test * chore(files): remove dead code (unused FileViewer logger + EmbeddedWorkflowActions router) * fix(files): derive markdown round-trip verdict from live content, not a locked stale snapshot The gate locked isRoundTripSafe on the first post-stream snapshot, which is often the empty create_file buffer before the agent's server write lands — wrongly leaving an unsafe document editable. Derive the verdict from the current content (memoized on the bytes) so canEdit tracks the real payload. * test(files): guard the rich editor dirty signal — open is never dirty, edits emit * fix(files): lock the markdown round-trip verdict on opened content, never strand dirty edits The round-trip-safety verdict now gates editability only at open time — computed once, on the exact content the editor mounts with, and locked for its lifetime. A dirty document is round-trip-safe by construction (the editor only emits safe markdown), so the verdict must never flip off mid-edit: doing so disabled autosave, ⌘S, the toolbar Save and the unmount flush, stranding unsaved edits. Locking on the opened (reconciled) content also fixes the stale post-stream empty-buffer snapshot, and lets the redundant MarkdownFileEditor gate (plus its duplicate content fetch) be deleted. * improvement(file-viewer): reuse shared copy hook, lazy frontmatter split - code-block: replace hand-rolled copy-with-timeout with shared useCopyToClipboard - rich-markdown-editor: compute frontmatter split once via lazy ref, drop redundant frontmatterRef - round-trip-safety: correct stale comments (read-only, not raw editor fallback) * feat(file-viewer): linked images, typed-link input rule, drag-to-reorder, churn fixes - image: round-trip linked images/badges via an href attr + custom markdown tokenizer; make the image a drag handle so it can be grabbed and reordered - link-input-rule: convert typed [text](url) to a link on the closing paren (normalized href) - markdown-paste: render pasted markdown as rich content, guarded against code blocks - round-trip-safety: behavioral link-count check replaces the static linked-image rejection - extensions: trim the table serializer's blank lines to stop interior-table whitespace churn * improvement(file-viewer): Backspace at start of a heading reverts it to a paragraph Notion-style: ProseMirror's default joins or no-ops at a heading boundary, stranding the heading style. A second Backspace then merges as usual. * fix(file-viewer): don't upload pasted/dropped images into a read-only editor handlePaste/handleDrop ran the workspace image upload without checking editability, so a read-only doc (canEdit=false or a round-trip-unsafe file) could still trigger an upload. Guard both on view.editable. * fix(file-viewer): sanitize linked-image href; drop global leading-newline strip - image: run the linked-image (badge) anchor target through normalizeLinkHref so a javascript:/data: href in a file can't execute on click; the markdown still preserves the raw target (file content unchanged) - markdown-fidelity: the table serializer now trims its own surrounding blank lines, so the global leading-newline strip in postProcessSerializedMarkdown is redundant — removing it stops clobbering content that legitimately begins with whitespace * feat(file-viewer): stream agent output directly into the rich editor; add more code languages - rich-markdown-editor: the TipTap editor is now the only markdown surface. Agent output streams into it read-only (synced per chunk, autoscrolled), then the same instance hands off to an editable editor on settle — no separate streamdown preview, so no stream→edit flash. The round-trip verdict + frontmatter lock when the content settles. - code-block/code-highlight/detect-language: register Go, Rust, Java, C, C++, C#, Ruby, PHP grammars and add detectors, so those blocks highlight and the picker offers them. - css: style h5/h6 in the prose stylesheet. * fix(sidebar): hydrate collapse state before paint to stop refresh flash The collapsed sidebar swaps entire subtrees (collapsed flyout vs expanded lists), but isCollapsed only resolved after the first paint via auto rehydration, so a collapsed reload rendered the expanded tree into the 51px rail and then reflowed — the misplaced/flashing content on refresh. Adopt zustand's documented SSR pattern: skipHydration on the persist config (first render keeps the default false, matching SSR HTML) and flush persist.rehydrate() from a useLayoutEffect so the correct structure commits in the same pre-paint frame. Removes the old race where onRehydrateStorage lifted the data-sidebar-collapsed mask before React committed the rail. * refactor(file-viewer): audit fixes — stale docs, DRY settle-lock, language detection - rich-markdown-editor: rewrite the now-stale single-surface docstring (no PreviewPanel); extract a shared lockSettled() helper used by both the mount and stream-settle paths; guard the settle re-seed so it only setContent's when the body actually changed (no redundant doc rebuild) - detect-language: stop misreading generics (List<String>) as HTML markup; detect Go type/struct - code-block: export LANGUAGE_OPTIONS + add a test asserting every picker language has a registered Prism grammar (prevents picker/highlighter drift) * refactor(file-viewer): remove dead markdown-preview renderer now superseded by the rich editor Markdown files route exclusively to RichMarkdownEditor on both the read-only and editable paths, so PreviewPanel's markdown branch and its Streamdown-based renderer were unreachable. Delete MarkdownPreview and its renderers, callout/ frontmatter/checkbox machinery, and the now-unused remark/rehype/prism/streamdown imports; drop the dead toggleMarkdownCheckbox/onCheckboxToggle plumbing in text-editor. Keep the html/csv/svg/mermaid branches intact. * refactor(file-viewer): drop dead streamingMode/append path, align naming, cover autosave The streaming engine only ever runs in 'replace' mode (the only runtime callers pass it); the 'append' branch of resolveStreamingEditorContent was unreachable. Remove streamingMode + the StreamingMode type and thread it out of the 6 components that forwarded it — nextContent is now simply the streamed snapshot, behavior-identical on the live path. Rename for codebase semantics: the boolean prop streaming -> isStreaming, EditorKeymap -> RichMarkdownKeymap, the highlight PluginKey KEY -> HIGHLIGHT_PLUGIN_KEY. Add a defensive isEditable guard to the markdown paste handler (parity with the image handler; read-only must never mutate). Add a dependency-free useAutosave test suite (debounce, min-display window, no-data-loss when an edit lands mid-save, error/no-retry, Cmd+S flush, streaming-disabled lock, unmount flush). * fix(file-viewer): re-lock round-trip verdict + frontmatter on each stream settle LoadedRichMarkdownEditor stays mounted across multiple agent edits to the same file within a chat (previewContextKey is the chat id), but the settle effect only locked settledRef when it was null — so a second stream into the same instance kept editability and frontmatter tied to the first settled snapshot. A repeat edit that is round-trip-unsafe would stay editable, and saves would re-attach the stale frontmatter. Track wasStreaming and re-derive the verdict + frontmatter on every stream->settle transition (user edits never re-derive, preserving the don't-strand-edits rule). Verified red/green in the e2e streaming harness. * test(file-viewer): lock link href sanitization for dangerous schemes from file content Greptile flagged a possible javascript: link XSS. Verified TipTap 3.26.1 already neutralizes javascript:/data:/vbscript: (and mixed-case/whitespace variants) from file-loaded markdown to an empty href. Add a committed regression test that asserts this against the real headless editor, so a future TipTap bump can't silently reintroduce the issue. * perf(file-viewer): cap the round-trip probe at 24KB and coalesce streaming syncs @tiptap/markdown's parse is superlinear (~O(n2)) in document size — measured ~170ms at 11KB, ~875ms at 23KB, multiple seconds past ~35KB — and it runs synchronously at mount inside the round-trip-safety probe (twice) and the editor's own setContent. The 128KB cap allowed multi-second main-thread freezes; lower it to 24KB so the worst-case mount stays near a second while still covering the vast majority of real markdown files (larger files open read-only). Separately, coalesce streaming chunk-syncs to one re-parse per animation frame so a fast-streaming agent doesn't re-parse the whole accumulating doc per token. Typing latency was measured to be already excellent (sub-ms median, no change needed); the only hot cost was the mount parse. * perf(file-viewer): chunked markdown parsing to remove the O(n2) mount cost @tiptap/markdown's whole-document setContent(md,'markdown') is superlinear in size, freezing the main thread at mount for large files (~2.5s at 34KB, ~11s at 65KB) and forcing a restrictive read-only cap. Parse block-by-block instead: a conservative blank-line/fence-aware splitter (merges list/quote runs and indented continuations so ambiguous structures stay atomic; reference-link/footnote/raw-HTML docs fall back to a whole parse), each block parsed with the editor's own lexer via one reused headless parser, assembled into a doc. This is linear and byte-identical to the one-shot parse — measured ~15ms vs multiple seconds at 124KB+ — so the editor mount, streaming sync, and round-trip probe are all linear, and the editable-size cap goes 24KB -> 256KB (covers the p99 of real files). Fidelity + idempotency are pinned by unit tests, a 400-document property/fuzz test, and adversarial edge cases (nested/loose lists, blockquotes, setext, indented code, lazy continuation, HTML, reference links). * fix(sidebar): render collapse state from a cookie so SSR matches The server couldn't read localStorage, so a collapsed user's first paint rendered the *expanded* tree at 51px — prefetched chat/workflow lists, pinned-chat pin icons, and loading skeletons all crammed into the rail and then reflowed once the store hydrated. Mirror the collapse state into a sidebar_collapsed cookie (the shadcn/ui sidebar pattern), read it in the workspace server layout, and seed the sidebar's first render with it: structure is now correct on the server, so the first paint is the real rail with no skeleton/pin/shift. The store remains the post-hydration source of truth; the blocking script honors the cookie for width when localStorage is absent so width and structure agree. * refactor(sidebar): make the cookie the single source of truth for collapse Consolidates the collapse machinery onto one source of truth instead of layering the cookie on top of the legacy localStorage + CSS-mask system: - Collapse persists only in the sidebar_collapsed cookie; the store seeds isCollapsed from it and drops it from localStorage (partialize + merge), removing the dual-write and the cross-tab desync it caused. - Retire the redundant html[data-sidebar-collapsed] attribute + CSS mask now that the server emits the correct data-collapsed structure; also delete the dead sidebar-collapse-show/-remove/-btn rules. - Blocking script reads the cookie for collapse (width stays in localStorage) and seeds the cookie once from the legacy flag so existing collapsed users keep their preference. - Keep skipHydration + a pre-paint rehydrate for width only — the documented zustand SSR pattern, so _hasHydrated is deterministically false during SSR. Width stays in localStorage; each field now has exactly one home. * refactor(file-viewer): simplify + cleanup chunked-parse (linear merge, parse-once seed) From the /simplify + /cleanup passes: - splitMarkdownBlocks: build continuation runs and join each once instead of concatenating onto the growing previous block per group, which was O(n2) for a pathological single long loose list (now linear: 208KB loose list splits in ~3ms). - rich-markdown-editor: seed the editor's initial content via a lazy useState initializer instead of useRef(parseMarkdownToDoc(...)), whose argument re-parsed the whole document on every render (i.e. every keystroke). Parses exactly once at mount. - Document that the indent-merge rule is load-bearing for nested fenced code, and tighten the verbose inline comment blocks. * refactor(sidebar): drop orphaned sidebar-collapse-btn class Its CSS rule was removed with the data-sidebar-collapsed mask; the button's collapse behavior is fully driven by the React isCollapsed ternary, leaving the class name pointing at nothing. * test(file-viewer): consolidate split test files into one per module Match the dir's one-test-per-module convention: fold the markdown-parse property/fuzz suite into markdown-parse.test.ts and the editability corpus into round-trip-safety.test.ts (both already tested the same module from a separate-concern file). No coverage change — same assertions, fewer files (12 -> 10). * fix(file-viewer): make all editor controls respect read-only permissions Every interactive control that calls updateAttributes/dispatches a command mutates the doc even when read-only (ProseMirror commands run regardless of editable), so gate them on editor.isEditable: - bubble menu: the Cmd/Ctrl+K shortcut and shouldShow now bail when not editable, so a read-only doc can't open the link bar and setLink into it (Cursor finding). - code block: the language picker renders as a static label when read-only (its onSelect mutates); copy + view-only wrap stay. - image: no drag-to-reorder (draggable=false, no drag handle) and no resize handle when read-only; the image still renders and follows its link. - links: a plain click now follows the link in read-only (reader) mode, while edit mode still requires a modifier so a plain click can place the cursor (Cursor finding). Verified with new read-only permission e2e tests. * fix(sidebar): honor collapsed cookie even when localStorage is corrupt The blocking script read the collapse cookie inside the same try as JSON.parse(localStorage); invalid persisted JSON fell through to the 248px fallback and ignored a collapsed cookie, painting an expanded-width rail on first load. Read collapse from the cookie first and parse the persisted width in its own try so the two are independent. * docs(sidebar): convert inline comments to TSDoc * fix(file-viewer): resolve in-app workspace image URLs in the rich editor The removed MarkdownPreview rewrote /workspace/{id}/files/{fileId} image src to the serving endpoint /api/files/view/{fileId}; without it, in-app image URLs 404 in the rich editor (Cursor finding). Re-add the rewrite as a display-only transform on the rendered <img src> — the node's stored src attribute keeps the original path so markdown round-trips unchanged. Absolute/non-workspace URLs pass through. Unit tested. * fix(files): restore same-page anchor links in the rich markdown editor Headings rendered by the TipTap editor had no slug ids (the old MarkdownPreview got them from rehype-slug), so in-document table-of-contents links like [section](#section) had no targets. Resolve the slug to its heading on click (GitHub-style, duplicate-disambiguated) and scroll to it, with zero per-keystroke cost. * feat(files): render mermaid diagrams in the rich markdown editor A code block renders as a Mermaid diagram when it is fenced ```mermaid or auto-detected (an untagged fence whose first line opens with a diagram keyword, the Linear/GitHub heuristic). Detection is display-only — the node stays an ordinary code block and the markdown round-trips unchanged. - Source while the caret is inside the block, diagram on blur; a Show source / Show diagram control plus copy, matching the code block's hover chrome. - Clicking the diagram selects the node (same ring as an image), not flips source. - Theme-aware (light/dark) via next-themes; the diagram frame shares the code block's chrome (one CSS source of truth). - Extracted MermaidDiagram into a shared module so the editor reuses it without pulling preview-panel's heavy deps; rendered SVGs are memoized so toggling the source view and back is instant. Covered by mermaid-diagram unit tests and the editor e2e harness. * fix(files): harden the markdown editor (CRLF chunking, href allowlist, image escaping) Final-audit follow-ups: - splitMarkdownBlocks normalizes CRLF/CR first — a closing fence ending in \r no longer fails to match, which had collapsed Windows-authored files with fenced code into one block and defeated the linear chunker (perf regression). - normalizeLinkHref rejects file://, blob:, and other non-network schemes (script/data schemes already rejected); network scheme:// (http/ftp/…) and bare host:port still pass. - Image markdown serialization escapes alt/title delimiters and angle-brackets a src with spaces/parens, so they round-trip losslessly; linked-image anchors open in a new tab (target=_blank). - Markdown paste routes through the chunker so a large pasted blob can't freeze the main thread. * test(files): cover the code-highlight incremental re-tokenization gate Export and unit-test changeTouchesCodeBlock: prose-only edits map decorations (false), edits inside a code block or a setNodeMarkup language change re-tokenize (true) — the perf-correctness path that keeps highlighting off the keystroke path. * fix(files): keep relative links relative, navigate in-app links within the SPA - normalizeLinkHref no longer prefixes `./`/`../` relative paths into `https://./…` (they round-trip and resolve correctly). - Following a same-origin in-app link (e.g. /workspace/…) routes through the Next router (same tab) instead of always opening a new tab; modifier-click and external URLs still open a new tab. * fix(files): linked images don't open a tab on a plain click in the editor The linked-image anchor's native navigation was firing on a plain click in edit mode (where handleClick intentionally returns false for caret placement). Prevent the anchor's default so the editor's handleClick — gated on editable/modifier, matching text links via openOnClick:false — is the sole navigator. * fix(sidebar): match the collapse cookie value strictly (not a substring) A substring search for 'sidebar_collapsed=1' also matched 'sidebar_collapsed=10', desyncing the pre-paint sidebar rail and client store from the strict server read. Parse the cookie value and compare it to '1' exactly, in both the pre-paint inline script and readCollapsedCookie. Added a store test. * fix(sidebar): reconcile migrated-legacy collapse before paint A user whose collapse lived only in localStorage has no sidebar_collapsed cookie at SSR (initialCollapsed=false), but the pre-paint script migrates them to a cookie. The store's persist.rehydrate() is async (flips _hasHydrated after paint), so the first paint showed expanded labels in the collapsed 51px rail. Reconcile to the cookie synchronously in a useLayoutEffect (first render still matches the server, so no hydration mismatch) — no narrow-rail flash.
1 parent 7349bf4 commit ecbe191

56 files changed

Lines changed: 5131 additions & 1298 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

apps/sim/app/_styles/globals.css

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,38 +66,11 @@
6666
opacity: 0;
6767
}
6868

69-
html[data-sidebar-collapsed] .sidebar-container span,
70-
html[data-sidebar-collapsed] .sidebar-container .text-small {
71-
opacity: 0;
72-
}
73-
7469
.sidebar-container .sidebar-collapse-hide {
7570
transition: opacity 60ms ease;
7671
}
7772

78-
.sidebar-container .sidebar-collapse-show {
79-
opacity: 0;
80-
pointer-events: none;
81-
transition: opacity 120ms ease-out;
82-
}
83-
84-
.sidebar-container[data-collapsed] .sidebar-collapse-hide,
85-
html[data-sidebar-collapsed] .sidebar-container .sidebar-collapse-hide {
86-
opacity: 0;
87-
}
88-
89-
.sidebar-container[data-collapsed] .sidebar-collapse-show,
90-
html[data-sidebar-collapsed] .sidebar-container .sidebar-collapse-show {
91-
opacity: 1;
92-
pointer-events: auto;
93-
}
94-
95-
html[data-sidebar-collapsed] .sidebar-container .sidebar-collapse-remove {
96-
display: none;
97-
}
98-
99-
html[data-sidebar-collapsed] .sidebar-container .sidebar-collapse-btn {
100-
width: 0;
73+
.sidebar-container[data-collapsed] .sidebar-collapse-hide {
10174
opacity: 0;
10275
}
10376

apps/sim/app/layout.tsx

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,26 +78,36 @@ export default function RootLayout({ children }: { children: React.ReactNode })
7878
// window yields a width >= MIN instead of a sub-minimum sliver.
7979
var defaultSidebarWidth = 248;
8080
try {
81-
var stored = localStorage.getItem('sidebar-state');
82-
if (stored) {
83-
var parsed = JSON.parse(stored);
84-
var state = parsed && parsed.state;
85-
var isCollapsed = state && state.isCollapsed;
86-
87-
if (isCollapsed) {
88-
document.documentElement.style.setProperty('--sidebar-width', '51px');
89-
document.documentElement.setAttribute('data-sidebar-collapsed', '');
90-
} else {
91-
var width = state && state.sidebarWidth;
92-
var maxSidebarWidth = Math.max(248, window.innerWidth * 0.3);
93-
var finalWidth =
94-
typeof width === 'number' && isFinite(width)
95-
? Math.min(Math.max(width, 248), maxSidebarWidth)
96-
: defaultSidebarWidth;
97-
document.documentElement.style.setProperty('--sidebar-width', finalWidth + 'px');
98-
}
81+
// Collapse comes from the cookie (independent of localStorage
82+
// parsing); the persisted width is read defensively below. Match the
83+
// value strictly so 'sidebar_collapsed=10' isn't read as collapsed.
84+
var cookieMatch = document.cookie.match(/(?:^|;\s*)sidebar_collapsed=([^;]*)/);
85+
var hasCookie = cookieMatch !== null;
86+
var collapsed = cookieMatch !== null && cookieMatch[1] === '1';
87+
88+
var state = null;
89+
try {
90+
var stored = localStorage.getItem('sidebar-state');
91+
state = stored ? JSON.parse(stored).state : null;
92+
} catch (e) {}
93+
94+
// One-time migration: seed the cookie from the legacy localStorage
95+
// flag for users who collapsed before the cookie existed.
96+
if (!hasCookie && state && typeof state.isCollapsed === 'boolean') {
97+
collapsed = state.isCollapsed;
98+
document.cookie = 'sidebar_collapsed=' + (collapsed ? '1' : '0') + '; path=/; max-age=31536000; samesite=lax';
99+
}
100+
101+
if (collapsed) {
102+
document.documentElement.style.setProperty('--sidebar-width', '51px');
99103
} else {
100-
document.documentElement.style.setProperty('--sidebar-width', defaultSidebarWidth + 'px');
104+
var width = state && state.sidebarWidth;
105+
var maxSidebarWidth = Math.max(248, window.innerWidth * 0.3);
106+
var finalWidth =
107+
typeof width === 'number' && isFinite(width)
108+
? Math.min(Math.max(width, 248), maxSidebarWidth)
109+
: defaultSidebarWidth;
110+
document.documentElement.style.setProperty('--sidebar-width', finalWidth + 'px');
101111
}
102112
} catch (e) {
103113
document.documentElement.style.setProperty('--sidebar-width', defaultSidebarWidth + 'px');

apps/sim/app/workspace/[workspaceId]/components/resource/components/resource-header/resource-header.tsx

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,14 @@ interface BreadcrumbLocationPopoverProps {
371371
veilBoundaryRef: React.RefObject<HTMLDivElement | null>
372372
}
373373

374+
/**
375+
* Grace period before a hover-out dismisses the path popover. Covers the gap
376+
* the pointer crosses between the trigger and the popover content (and brief
377+
* jitter at their edges); re-entering either within this window cancels the
378+
* close. Standard hover-intent close delay — not tied to any navigation timing.
379+
*/
380+
const POPOVER_CLOSE_DELAY_MS = 120
381+
374382
function BreadcrumbLocationPopover({
375383
icon: Icon,
376384
breadcrumbs,
@@ -381,22 +389,44 @@ function BreadcrumbLocationPopover({
381389
const closeTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)
382390
const rootBreadcrumb = breadcrumbs[0]
383391

384-
const openPopover = () => {
392+
const cancelScheduledClose = () => {
385393
if (closeTimeoutRef.current) {
386394
clearTimeout(closeTimeoutRef.current)
387395
closeTimeoutRef.current = null
388396
}
397+
}
398+
399+
/**
400+
* Hover-intent open. Driven only by pointer-/keyboard-enter — never by
401+
* pointer movement. This is what makes the popover dismiss cleanly on a
402+
* click-to-navigate: a stationary click fires no enter event, so once
403+
* {@link navigateAndClose} sets `open` false nothing re-opens it before the
404+
* route swaps. (A move-driven open would re-fire under the resting cursor and
405+
* flash the popover/veil back in mid-navigation.)
406+
*/
407+
const openPopover = () => {
408+
cancelScheduledClose()
389409
setOpen(true)
390410
}
391411

392412
const scheduleClose = () => {
393-
if (closeTimeoutRef.current) {
394-
clearTimeout(closeTimeoutRef.current)
395-
}
413+
cancelScheduledClose()
396414
closeTimeoutRef.current = setTimeout(() => {
397415
setOpen(false)
398416
closeTimeoutRef.current = null
399-
}, 120)
417+
}, POPOVER_CLOSE_DELAY_MS)
418+
}
419+
420+
/**
421+
* Closes the popover up front, then runs the crumb's handler. Closing first
422+
* lets the veil fade and the popover play its exit animation instead of
423+
* snapping away when navigation unmounts the header.
424+
*/
425+
const navigateAndClose = (onClick?: () => void) => {
426+
if (!onClick) return
427+
cancelScheduledClose()
428+
setOpen(false)
429+
onClick()
400430
}
401431

402432
useEffect(() => {
@@ -413,15 +443,11 @@ function BreadcrumbLocationPopover({
413443
<button
414444
type='button'
415445
aria-label={rootBreadcrumb?.label ?? 'Path'}
416-
onClick={rootBreadcrumb?.onClick}
446+
onClick={() => navigateAndClose(rootBreadcrumb?.onClick)}
417447
onFocus={openPopover}
418448
onBlur={scheduleClose}
419449
onMouseEnter={openPopover}
420450
onMouseLeave={scheduleClose}
421-
onMouseMove={openPopover}
422-
onPointerEnter={openPopover}
423-
onPointerLeave={scheduleClose}
424-
onPointerMove={openPopover}
425451
className={cn(
426452
chipVariants({ flush: true }),
427453
'max-w-none gap-1.5 px-2 transition-colors',
@@ -457,10 +483,6 @@ function BreadcrumbLocationPopover({
457483
)}
458484
onMouseEnter={openPopover}
459485
onMouseLeave={scheduleClose}
460-
onMouseMove={openPopover}
461-
onPointerEnter={openPopover}
462-
onPointerLeave={scheduleClose}
463-
onPointerMove={openPopover}
464486
>
465487
<PopoverSection className='px-1.5 py-0.5 text-[var(--text-muted)] text-xs'>
466488
<span className='inline-flex items-center gap-1'>
@@ -474,7 +496,7 @@ function BreadcrumbLocationPopover({
474496
key={`${crumb.label}-${index}`}
475497
icon={crumb.icon || (index === 0 ? Icon : undefined)}
476498
label={crumb.label}
477-
onClick={crumb.onClick}
499+
onClick={crumb.onClick ? () => navigateAndClose(crumb.onClick) : undefined}
478500
active={index === breadcrumbs.length - 1}
479501
/>
480502
))}

apps/sim/app/workspace/[workspaceId]/components/workspace-chrome/workspace-chrome.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const SLIDE_TRANSITION =
1515

1616
interface WorkspaceChromeProps {
1717
children: React.ReactNode
18+
/** Cookie-derived collapse state from the server layout; seeds the sidebar's first render. */
19+
initialSidebarCollapsed?: boolean
1820
}
1921

2022
function isFullscreenPath(pathname: string | null): boolean {
@@ -41,7 +43,7 @@ function isFullscreenPath(pathname: string | null): boolean {
4143
* On a direct load of a fullscreen route the wrapper mounts already collapsed,
4244
* so no slide plays (CSS transitions don't run on mount).
4345
*/
44-
export function WorkspaceChrome({ children }: WorkspaceChromeProps) {
46+
export function WorkspaceChrome({ children, initialSidebarCollapsed }: WorkspaceChromeProps) {
4547
const pathname = usePathname()
4648
const isFullscreen = isFullscreenPath(pathname)
4749

@@ -103,7 +105,7 @@ export function WorkspaceChrome({ children }: WorkspaceChromeProps) {
103105
isFullscreen && '-translate-x-full'
104106
)}
105107
>
106-
<Sidebar />
108+
<Sidebar initialCollapsed={initialSidebarCollapsed} />
107109
</div>
108110
</div>
109111
<div

apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/file-viewer.tsx

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
'use client'
22

33
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'
4-
import { createLogger } from '@sim/logger'
54
import { Music } from 'lucide-react'
65
import dynamic from 'next/dynamic'
76
import type { WorkspaceFileRecord } from '@/lib/uploads/contexts/workspace'
87
import { getFileExtension } from '@/lib/uploads/utils/file-utils'
98
import { useWorkspaceFileBinary, useWorkspaceFileContent } from '@/hooks/queries/workspace-files'
10-
import { resolveFileCategory } from './file-category'
11-
import type { StreamingMode } from './text-editor-state'
12-
import { useDocPreviewBinary } from './use-doc-preview-binary'
13-
14-
export type { StreamingMode } from './text-editor-state'
15-
169
import { CsvTablePreview } from './csv-table-preview'
1710
import { DocxPreview } from './docx-preview'
11+
import { resolveFileCategory } from './file-category'
1812
import { ImagePreview } from './image-preview'
1913
import type { PdfDocumentSource } from './pdf-viewer'
2014
import { PptxPreview } from './pptx-preview'
@@ -27,13 +21,17 @@ import {
2721
resolvePreviewError,
2822
} from './preview-shared'
2923
import { TextEditor } from './text-editor'
24+
import { useDocPreviewBinary } from './use-doc-preview-binary'
3025
import { XlsxPreview } from './xlsx-preview'
3126

3227
const PdfViewerCore = dynamic(() => import('./pdf-viewer').then((m) => m.PdfViewerCore), {
3328
ssr: false,
3429
})
3530

36-
const logger = createLogger('FileViewer')
31+
const RichMarkdownEditor = dynamic(
32+
() => import('./rich-markdown-editor/rich-markdown-editor').then((m) => m.RichMarkdownEditor),
33+
{ ssr: false, loading: () => <PreviewLoadingFrame className='flex flex-1 flex-col' /> }
34+
)
3735

3836
/**
3937
* CSVs at or below this size load fully into the editor (editable, with an inline preview).
@@ -50,6 +48,15 @@ export function isPreviewable(file: { type: string; name: string }): boolean {
5048
return resolvePreviewType(file.type, file.name) !== null
5149
}
5250

51+
/**
52+
* Markdown files render in the inline rich editor ({@link RichMarkdownEditor}) rather than
53+
* the raw Monaco editor. Toolbars use this to hide the raw/split/preview mode controls,
54+
* which don't apply to the single-surface editor.
55+
*/
56+
export function isMarkdownFile(file: { type: string; name: string }): boolean {
57+
return resolvePreviewType(file.type, file.name) === 'markdown'
58+
}
59+
5360
/**
5461
* A CSV larger than {@link CSV_INLINE_EDIT_MAX_BYTES} is shown as a streamed, read-only preview —
5562
* the editor would OOM loading the whole file. The viewer renders {@link CsvTablePreview} for it,
@@ -84,7 +91,6 @@ interface FileViewerProps {
8491
onSaveStatusChange?: (status: 'idle' | 'saving' | 'saved' | 'error') => void
8592
saveRef?: React.MutableRefObject<(() => Promise<void>) | null>
8693
streamingContent?: string
87-
streamingMode?: StreamingMode
8894
disableStreamingAutoScroll?: boolean
8995
previewContextKey?: string
9096
}
@@ -100,7 +106,6 @@ export function FileViewer({
100106
onSaveStatusChange,
101107
saveRef,
102108
streamingContent,
103-
streamingMode,
104109
disableStreamingAutoScroll = false,
105110
previewContextKey,
106111
}: FileViewerProps) {
@@ -114,6 +119,14 @@ export function FileViewer({
114119
if (isCsvStreamOnly(file)) {
115120
return <UnsupportedPreview file={file} />
116121
}
122+
// Markdown renders through the inline rich editor (non-editable) so the public share
123+
// surface matches the in-app reading experience; canEdit={false} disables autosave,
124+
// the bubble menu, and every other editing affordance.
125+
if (isMarkdownFile(file)) {
126+
return (
127+
<RichMarkdownEditor key={file.id} file={file} workspaceId={workspaceId} canEdit={false} />
128+
)
129+
}
117130
return <ReadOnlyTextPreview file={file} workspaceId={workspaceId} />
118131
}
119132
// A large CSV can't be loaded whole into the editor (the browser OOMs on the full text).
@@ -122,6 +135,24 @@ export function FileViewer({
122135
return <CsvTablePreview key={file.id} file={file} workspaceId={workspaceId} />
123136
}
124137

138+
if (isMarkdownFile(file)) {
139+
return (
140+
<RichMarkdownEditor
141+
key={file.id}
142+
file={file}
143+
workspaceId={workspaceId}
144+
canEdit={canEdit}
145+
autoFocus={autoFocus}
146+
onDirtyChange={onDirtyChange}
147+
onSaveStatusChange={onSaveStatusChange}
148+
saveRef={saveRef}
149+
streamingContent={streamingContent}
150+
disableStreamingAutoScroll={disableStreamingAutoScroll}
151+
previewContextKey={previewContextKey}
152+
/>
153+
)
154+
}
155+
125156
return (
126157
<TextEditor
127158
file={file}
@@ -133,7 +164,6 @@ export function FileViewer({
133164
onSaveStatusChange={onSaveStatusChange}
134165
saveRef={saveRef}
135166
streamingContent={streamingContent}
136-
streamingMode={streamingMode}
137167
disableStreamingAutoScroll={disableStreamingAutoScroll}
138168
previewContextKey={previewContextKey}
139169
/>
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
export { resolveFileCategory } from './file-category'
22
export type { PreviewMode } from './file-viewer'
3-
export { FileViewer, isCsvStreamOnly, isPreviewable, isTextEditable } from './file-viewer'
3+
export {
4+
FileViewer,
5+
isCsvStreamOnly,
6+
isMarkdownFile,
7+
isPreviewable,
8+
isTextEditable,
9+
} from './file-viewer'
410
export { PreviewPanel, RICH_PREVIEWABLE_EXTENSIONS, resolvePreviewType } from './preview-panel'

0 commit comments

Comments
 (0)