visual-editing: "Edit" on a block inside a rich text field focuses the whole field, not the block

Open
#188 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
38/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
bun, typescript

Research direction

Start with withVisualEditingPath.ts, markerExtractor.ts, and expandAndFocus.ts, then trace how block identity passes through the bridge message and veFocus URL. Run the named Vitest suites, especially findFieldElement.int.spec.ts, to cover marker extraction, routing, expansion, focus, and stale IDs. Done means the acceptance criteria pass for Live Preview and standalone URL routing, with field focus preserved when no block marker is present.

Written by the indexing model from the issue text.

Description

What happens

A rich text field is one edit target, and every Lexical block inside it shares the field's marker. "Edit" on the third block of a long article opens the right tab, focuses the editor, scrolls the field so its middle sits in the centre of the screen and plays the reveal over the whole field. The clicked block is not scrolled to, not expanded if it is collapsed and not highlighted, and since Lexical blocks open expanded by default, the editor scrolls past every open block to find it.

Expected

A renderer can mark a block node inside a rich text field, and "Edit" on it lands on that block: the admin scrolls the block into view, expands it if it is collapsed, focuses its first field and plays the reveal on the block. Without a block marker, everything behaves as today.

Reproduction

apps/cms installs the plugin with enrichment: "explicit" (plugins/index.ts#L404-L424), reads posts with context: { visualEditing: draft } (getPostBySlug.ts#L21), and a post's Content is a rich text field with four Lexical blocks (Posts/index.ts#L122-L140).

  1. In apps/cms, fill .env from .env.example and run bun run dev (port 3333)
  2. Open a post, give Content enough paragraphs to run several screens, insert a Code block near the end and save
  3. Open Live Preview and switch the floating toggle to Always
  4. Hover the Code block: the outline and the Edit badge cover the whole article, because the renderer marks only the wrapper (RichText/index.tsx#L111-L119) and the block converters have nothing to mark a block with (RichText/index.tsx#L67-L78)
  5. Click Edit: Content scrolls so its middle is centred, the caret lands in the editor, the reveal plays over the whole field, and the Code block stays off screen

The steps are read from the config and the source, not clicked through in this repo; the same behaviour was seen on 1.1.0 in a separate Payload 3 project.

Root cause

Fix

Carry an optional block id from the renderer to the admin, and use it after the field is found.

  • src/internal/shared.ts: VeIdentity gains blockId?: string; spreadVeIdentity, toMetaInfo and fromStegaPayload copy it, which carries it through the click handler and the target registry with no change of their own
  • src/constants.ts: DATA_VE_BLOCK_ID_ATTR = "data-ve-block-id", and blockId?: string on VeOpenFieldMessage
  • src/client/withVisualEditingPath.ts: withVisualEditingPath(value: unknown, options?: { blockId?: string }). A renderer marks a block from the Lexical JSON in its converter: withVisualEditingPath(content, { blockId: node.fields.id })
  • src/client/overlay/markerExtractor.ts reads the attribute; createOverlay.ts compares blockId as well as path before skipping a re-upsert (createOverlay.ts#L42-L52)
  • src/client/buildAdminEditUrl.ts: append veBlock=<id>
  • src/admin/VisualEditingBridgeProvider.tsx: read and strip veBlock next to veFocus, set it in navigateAndFocus, and pass blockId from the URL or the message through focusFieldPath into expandAndFocus
  • src/admin/expandAndFocus.ts: expandAndFocus(chain, fieldId, options?: { blockId?: string }). After the field element is found, wait (same STEP_BUDGET_MS) for the block inside it, click its toggle if it is collapsed, then run focusAndSweep on the block, focusing the first input inside .collapsible__content rather than the block-name input in the header. If the block does not turn up (deleted since the preview rendered, an inline block, changed markup), fall back to today's field focus
  • README.md: a "Blocks inside rich text" subsection under "Editable richText and upload fields"

The admin selectors, read from Payload 3.90.1 and Lexical 0.50.0, the versions bun.lock locks:

Existing users are unaffected: every new parameter is optional, and without blockId the attributes, the message, the URL and the admin behaviour are the same as 1.1.0. A newer frontend with an older admin, or the reverse, falls back to field focus. Both halves ship in this one package, so one minor bump covers them.

Acceptance criteria

  • withVisualEditingPath(content, { blockId: "abc" }) returns today's attributes plus data-ve-block-id="abc"; without options the result equals 1.1.0's
  • In apps/cms with the Code block converter marked, hovering the Code block outlines only the block, and hovering a paragraph still outlines the whole Content field
  • "Edit" on a Code block two or more screens below the top of Content scrolls that block into view and plays the reveal on it, from Live Preview (message route) and from a standalone frontend tab (veFocus and veBlock URL route)
  • With the block collapsed in the admin beforehand, the same click expands it
  • The caret lands in the block's first field, not in the block-name input or the editor root
  • With an id that no longer exists in the document, the click focuses the field as today, with no console error
  • Every existing test passes unchanged

Test plan

  • Unit tests in packages/payload-plugin-visual-editing/tests (vitest with jsdom, bun run test): extend withVisualEditingPath.int.spec.ts, markerExtractor.int.spec.ts, buildAdminEditUrl.int.spec.ts, overlayClick.int.spec.ts and bridgeRouting.int.spec.tsx for blockId and veBlock
  • In findFieldElement.int.spec.ts, which already imports expandAndFocus, add a fixture of Payload's block markup: two blocks, the second collapsed, a block-name input in its header and an input in its content. Assert the toggle is clicked, scrollIntoView runs on the decorator element, focus lands in the content input, and a stale id falls back to the field
  • Manual in apps/cms: mark the four block converters (pass content into createJsxConverters), then run the reproduction from Live Preview and from a standalone frontend tab, which apps/cms routes through the URL (VisualEditingEditRouter/index.tsx#L9-L31), once with the block expanded and once collapsed

Seen on 1.1.0, unchanged on main at b645ef0c.

The long-form brief carries the end-to-end trace, a code sketch of the admin step, the alternatives considered (an id in the path, a server-side block marker, the Lexical editor instance) and the edge cases, nested blocks and locales among them.

Dominant language
TypeScript
Stars
34
Forks
5
Avg merge
2d 4h
Merged PRs (30d)
41

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from focusreactive/payload-plugins

All issues in focusreactive/payload-plugins

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.