Skip to content

fix: scroll to and focus target field when navigating from visual editing - #185

Merged
ascorbic merged 18 commits into
emdash-cms:mainfrom
ophirbucai:fix/visual-editing-field-navigation
Apr 11, 2026
Merged

ascorbic merged 18 commits into
emdash-cms:mainfrom
ophirbucai:fix/visual-editing-field-navigation

Conversation

@ophirbucai

@ophirbucai ophirbucai commented Apr 3, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

When navigating from visual editing with ?field=<name>, the admin content editor scrolls to and focuses the target field once content has loaded, then strips the param from the URL.

Also annotates the excerpt field in demos/simple with {...post.edit.excerpt} so clicking it in edit mode opens the admin editor navigated to that field.

Type of change

  • Bug fix

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm --silent lint:json | jq '.diagnostics | length' returns 0
  • pnpm test passes (core package: 2095 tests passing)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)

AI-generated code disclosure

  • This PR includes AI-generated code

Screenshots / test output

Verified manually:

  • Clicking a field annotation in edit mode opens admin with ?field=<name>
  • Admin scrolls to and focuses the correct field after content loads
  • ?field= param is stripped from the URL after scroll

- Add missing `{...post.edit.excerpt}` annotation across all templates and demos
- Parse `?field=` search param in admin content edit route
- Add `useElementReady` hook to poll for DOM elements after async render
- Scroll to and focus the target field when navigating from visual editing
- Strip `?field=` from URL after scroll to prevent re-scroll on back navigation
- Add `id` to all field renderers (image, select, boolean, portableText) for scroll targeting
Replace Google Fonts CDN links with @fontsource-variable/inter and
@fontsource/jetbrains-mono for privacy and offline reliability.
Add {...post.edit.content} to the article-content wrapper so the
Portable Text body participates in visual editing — clicking it in
edit mode now navigates to the admin editor with ?field=content,
triggering scroll-to-field.
…or rich text fields

- useElementReady now accepts any CSS selector via querySelector
  instead of getElementById, making it more reusable
- Scroll-to-field now focuses the .ProseMirror contenteditable div
  for portableText fields, falling back to the field container for
  all other field types
@changeset-bot

changeset-bot Bot commented Apr 3, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dfec3ff

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
@emdash-cms/admin Patch
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/plugin-ai-moderation Patch
@emdash-cms/plugin-atproto Patch
@emdash-cms/plugin-audit-log Patch
@emdash-cms/plugin-color Patch
@emdash-cms/plugin-embeds Patch
@emdash-cms/plugin-forms Patch
@emdash-cms/plugin-webhook-notifier Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Apr 3, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@ophirbucai

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Apr 3, 2026
- Remove selector from useElementReady dep array; use a ref so the
  effect fires once at mount rather than restarting when the selector
  string identity changes mid-poll
- Normalize validateSearch to parse search.field safely from unknown
  input, dropping the pass-through that bypassed runtime validation
- Drop redundant post.edit.content annotation on article-content
  wrapper — PortableText renders the inline editor automatically via
  getEditMeta; the wrapper annotation was suppressing it
- Remove dead ProseMirror focus path; inline editor handles rich text
The extracted hook had a placement bug (hasAttempted set before rAF fires)
and added indirection for a single call site. The inline effect with a
hasScrolled ref is simpler, correct, and easier to reason about locally.
@ophirbucai
ophirbucai marked this pull request as ready for review April 3, 2026 17:51
@ophirbucai ophirbucai changed the title fix: visual editing field navigation, scroll-to-field, and portableText annotation fix: scroll to and focus target field when navigating from visual editing Apr 3, 2026
…rom useSearch. remove redundant hasScrolled check, URL is already updated. noticed that we need to defer the call to the next render now, so added requestIdleCallback & cleanup. shimmed at top of root component file
@github-actions

github-actions Bot commented Apr 5, 2026

Copy link
Copy Markdown
Contributor

Overlapping PRs

This PR modifies files that are also changed by other open PRs:

This may cause merge conflicts or duplicated work. A maintainer will coordinate.

Copilot AI review requested due to automatic review settings April 11, 2026 09:25

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@pkg-pr-new

pkg-pr-new Bot commented Apr 11, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@185

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@185

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@185

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@185

emdash

npm i https://pkg.pr.new/emdash@185

create-emdash

npm i https://pkg.pr.new/create-emdash@185

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@185

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@185

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@185

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@185

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@185

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@185

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@185

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@185

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@185

commit: dfec3ff

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the admin content editor to support deep-linking to a specific field (via ?field=<name>) when navigating from visual editing, and expands demo/template field annotations so more fields can be targeted from the frontend.

Changes:

  • Adds ?field= search validation and an idle-time scroll/focus + URL cleanup effect to the admin content edit route.
  • Ensures more field widgets render predictable id="field-<name>" anchors for scroll targeting.
  • Annotates the excerpt field in blog templates/demos so visual editing clicks can navigate to that field in the admin.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
templates/blog/src/pages/posts/[slug].astro Adds {...post.edit.excerpt} to excerpt markup for visual editing navigation.
templates/blog-cloudflare/src/pages/posts/[slug].astro Same excerpt annotation for the Cloudflare blog template.
packages/admin/src/router.tsx Validates field search param and performs post-load scroll/focus + strips param; adds requestIdleCallback polyfill.
packages/admin/src/components/ContentEditor.tsx Adds id anchors for additional field kinds (Switch/Select/Image/PortableText wrapper) to enable scroll targeting.
demos/simple/src/pages/posts/[slug].astro Adds excerpt annotation for the simple demo.
demos/preview/src/pages/posts/[slug].astro Adds excerpt annotation for the preview demo.
demos/postgres/src/pages/posts/[slug].astro Adds excerpt annotation for the postgres demo.
demos/playground/src/pages/posts/[slug].astro Adds excerpt annotation for the playground demo.
demos/cloudflare/src/pages/posts/[slug].astro Adds excerpt annotation for the cloudflare demo.
.changeset/twenty-friends-sell.md Publishes a patch changeset for the admin fix.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +528 to +532
React.useEffect(() => {
if (typeof searchParams.field !== "string" || isLoading) return;

const timeoutId = requestIdleCallback(() => {
const el = document.getElementById(`field-${searchParams.field}`);

Copilot AI Apr 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scroll/focus effect can run before the editor is actually rendered: it only gates on isLoading, but this component can still be returning <LoadingScreen /> when manifest is not yet loaded. In that case the idle callback won’t find the field element and the effect won’t rerun (since manifest isn’t a dependency). Gate on !!manifest (and/or !!item) and include it in the effect deps so the behavior reliably triggers once the editor is mounted.

Copilot uses AI. Check for mistakes.
Comment on lines +534 to +535
el.scrollIntoView({ behavior: "smooth", block: "center" });
el.focus();

Copilot AI Apr 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

el.focus() may be a no-op for fields where the ID is attached to a non-focusable wrapper (e.g., portableText/image use a <div id=...>). Consider focusing the first focusable descendant (input/textarea/button/[contenteditable]/[tabindex]) and using focus({ preventScroll: true }) to avoid undoing scrollIntoView.

Suggested change
el.scrollIntoView({ behavior: "smooth", block: "center" });
el.focus();
const focusableSelector = [
"input",
"textarea",
"select",
"button",
"a[href]",
'[contenteditable="true"]',
'[tabindex]:not([tabindex="-1"])',
].join(", ");
const focusTarget =
el instanceof HTMLElement &&
(el.matches(focusableSelector)
? el
: el.querySelector<HTMLElement>(focusableSelector));
el.scrollIntoView({ behavior: "smooth", block: "center" });
focusTarget?.focus({ preventScroll: true });

Copilot uses AI. Check for mistakes.
Comment on lines +528 to +541
React.useEffect(() => {
if (typeof searchParams.field !== "string" || isLoading) return;

const timeoutId = requestIdleCallback(() => {
const el = document.getElementById(`field-${searchParams.field}`);
if (el) {
el.scrollIntoView({ behavior: "smooth", block: "center" });
el.focus();
const { field: _, ...preservedSearch } = searchParams;
void navigate({ search: preservedSearch as never, replace: true });
}
});
return () => cancelIdleCallback(timeoutId);
}, [searchParams, isLoading, navigate]);

Copilot AI Apr 11, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This new ?field= navigation behavior (idle scroll, focus, and URL param stripping) isn’t covered by existing admin router/component tests. Since packages/admin/tests/router.test.tsx exists, adding a test that navigates to /content/$collection/$id?field=..., stubs scrollIntoView/focus, and asserts the param is removed would help prevent regressions.

Copilot uses AI. Check for mistakes.
@ascorbic
ascorbic merged commit e3f7db8 into emdash-cms:main Apr 11, 2026
25 checks passed
This was referenced Apr 11, 2026
fmhall pushed a commit to fmhall/emdash that referenced this pull request Apr 13, 2026
…ting (emdash-cms#185)

* fix: visual editing field navigation and scroll-to-field in admin

- Add missing `{...post.edit.excerpt}` annotation across all templates and demos
- Parse `?field=` search param in admin content edit route
- Add `useElementReady` hook to poll for DOM elements after async render
- Scroll to and focus the target field when navigating from visual editing
- Strip `?field=` from URL after scroll to prevent re-scroll on back navigation
- Add `id` to all field renderers (image, select, boolean, portableText) for scroll targeting

* fix: self-host Inter and JetBrains Mono via fontsource

Replace Google Fonts CDN links with @fontsource-variable/inter and
@fontsource/jetbrains-mono for privacy and offline reliability.

* fix: annotate content field for visual editing in simple demo

Add {...post.edit.content} to the article-content wrapper so the
Portable Text body participates in visual editing — clicking it in
edit mode now navigates to the admin editor with ?field=content,
triggering scroll-to-field.

* fix: generalize useElementReady to CSS selectors, focus ProseMirror for rich text fields

- useElementReady now accepts any CSS selector via querySelector
  instead of getElementById, making it more reusable
- Scroll-to-field now focuses the .ProseMirror contenteditable div
  for portableText fields, falling back to the field container for
  all other field types

* chore: ignore CLAUDE.local.md and .playwright-mcp

* Revert "fix: self-host Inter and JetBrains Mono via fontsource"

This reverts commit 35b8ba8.

* refactor: use useStableCallback in useElementReady

* chore: ignore *.local.* files for personal cursor rules

* fix: stabilize useElementReady deps and normalize validateSearch

- Remove selector from useElementReady dep array; use a ref so the
  effect fires once at mount rather than restarting when the selector
  string identity changes mid-poll
- Normalize validateSearch to parse search.field safely from unknown
  input, dropping the pass-through that bypassed runtime validation
- Drop redundant post.edit.content annotation on article-content
  wrapper — PortableText renders the inline editor automatically via
  getEditMeta; the wrapper annotation was suppressing it
- Remove dead ProseMirror focus path; inline editor handles rich text

* refactor: replace useElementReady hook with inline effect

The extracted hook had a placement bug (hasAttempted set before rAF fires)
and added indirection for a single call site. The inline effect with a
hasScrolled ref is simpler, correct, and easier to reason about locally.

* revert: restore .gitignore to main

* fix: pass id directly to Select instead of wrapper div

* fix: pass id directly to Select instead of wrapper div

* fix: remove spread object from useEffect deps, use stable reference from useSearch. remove redundant hasScrolled check, URL is already updated. noticed that we need to defer the call to the next render now, so added requestIdleCallback & cleanup. shimmed at top of root component file

* chore: add changelist entry 🦋

---------

Co-authored-by: Matt Kane <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants