Repository navigation
feat(blocks,admin): portable_text and block_list Block Kit fields + Duplicate on plugin block cards - #2674
feat(blocks,admin): portable_text and block_list Block Kit fields + Duplicate on plugin block cards#2674simone-coelho wants to merge 3 commits into
Conversation
Scope checkThis PR changes 1,160 lines across 10 files. Large PRs are harder to review and more likely to be closed without review. If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs. See CONTRIBUTING.md for contribution guidelines. |
|
All contributors have signed the CLA ✍️ ✅ |
🦋 Changeset detectedLatest commit: b173aaa The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
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 |
There was a problem hiding this comment.
Pull request overview
Adds two new structured Block Kit field types (portable_text and block_list) that plugin blocks can use to store rich Portable Text arrays and nested registered-block content (editable via the existing admin editor UI), and adds a Duplicate action for plugin block cards.
Changes:
- Extend
@emdash-cms/blocksElement/validation/builder APIs to supportportable_textandblock_list, including repeater sub-field support. - Add admin UI rendering for
portable_text/block_listinside the plugin block modal (including block catalog integration, duplication/removal, reorder, and sibling-list moves). - Add focused admin and blocks tests plus a changeset for the feature release.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/blocks/tests/validation.test.ts | Adds validation coverage for portable_text, block_list, and repeater sub-field usage + invalid cases. |
| packages/blocks/src/validation.ts | Registers new element types and adds validation rules for portable_text and block_list. |
| packages/blocks/src/types.ts | Extends the public Block Kit Element union and repeater sub-field union with new structured element interfaces. |
| packages/blocks/src/render-element.tsx | Treats portable_text/block_list as admin-only elements (runtime render returns null). |
| packages/blocks/src/index.ts | Exports the new element types from the package entrypoint. |
| packages/blocks/src/builders.ts | Adds builder helpers portableText() and blockList() and exports them via elements. |
| packages/admin/tests/editor/block-kit-structured-fields.test.tsx | Adds focused UI/behavior tests for the two structured fields and plugin-block duplication. |
| packages/admin/src/components/PortableTextEditor.tsx | Implements the structured field UI components, sibling-list move context, and integrates them into the plugin block modal. |
| packages/admin/src/components/editor/PluginBlockNode.tsx | Adds Duplicate button to plugin block cards in the editor. |
| .changeset/structured-block-kit-fields.md | Documents the feature set and bumps @emdash-cms/blocks and @emdash-cms/admin as minor. |
Suppressed comments (2)
packages/admin/src/components/PortableTextEditor.tsx:2548
- When inserting/updating a
block_listitem,{ _key: ..., ...values }allowsvalues._keyto overwrite the generated/preserved key (same for_typeif it ever appears). Spreadingvaluesfirst and then setting_key/_typelast prevents accidental overrides.
if (child.index === null) {
emit([...items, { _key: newBlockKey(), ...values, _type: child.def.type }]);
} else {
const index = child.index;
emit(
packages/admin/src/components/PortableTextEditor.tsx:1971
- The dialog footer buttons use hard-coded English strings (
Cancel,Save,Insert). Admin UI strings should be wrapped for translation via Lingui.
<Button type="button" variant="ghost" onClick={onClose}>
Cancel
</Button>
<Button type="submit" disabled={!canSubmit}>
{isEditing ? "Save" : "Insert"}
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| for (let i = 0; i < value.initial_value.length; i++) { | ||
| if (!isRecord(value.initial_value[i])) { | ||
| errors.push({ | ||
| path: `${path}.initial_value[${i}]`, | ||
| message: "Each initial_value entry must be a Portable Text block object", | ||
| }); | ||
| } | ||
| } |
| const catalog = React.useContext(PluginBlockCatalogContext); | ||
| const siblingsContext = React.useContext(BlockListSiblingsContext); | ||
| const scope = React.useContext(BlockListScopeContext); | ||
| const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 4 } })); |
| ref={inputRef} | ||
| type="url" | ||
| className="w-full" | ||
| placeholder={block?.placeholder || "Enter URL..."} |
| </form> | ||
| )} | ||
| </div> | ||
| <div className="flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2"> |
| const initialValue = React.useRef<PortableTextBlock[]>( | ||
| Array.isArray(value) ? (value as PortableTextBlock[]) : [], | ||
| ).current; | ||
| return ( | ||
| <div> | ||
| <label className="text-sm font-medium mb-1.5 block">{field.label}</label> | ||
| <PortableTextEditor | ||
| value={initialValue} | ||
| onChange={(next) => onChange(field.action_id, next)} | ||
| placeholder={field.placeholder} | ||
| className="rounded-md border border-kumo-line" | ||
| /> | ||
| </div> | ||
| ); |
There was a problem hiding this comment.
This PR adds genuinely useful Block Kit fields (portable_text, block_list) and a Duplicate action for plugin block cards. The approach fits EmDash well: it reuses the existing PortableTextEditor and Block Kit form machinery, keeps the runtime renderer returning null (so stored values round-trip unchanged), and is additive with no existing behavior change. The code is mostly clean and localized.
One readiness concern that isn’t a code bug but affects merge readiness: AGENTS.md requires a maintainer-approved Discussion for new features. The linked Discussion #2673 was opened alongside this PR and approval is still pending. That should be resolved before merge.
I reviewed statically (did not run tests or lint, per instructions). I found a few real issues worth fixing:
- Canonical docs are missing the new element types. The changeset is good, but
docs/src/content/docs/plugins/creating-plugins/block-kit.mdxandskills/creating-plugins/references/block-kit.mdstill omitportable_textandblock_list(and several existing elements). Per.changeset/README.md, useful explanations/examples should not live only in the changeset. block_listdrag-and-drop lacks keyboard support. The siblingBlockKitRepeateruses bothPointerSensorandKeyboardSensor;BlockKitBlockListFieldonly uses the pointer sensor, so keyboard users can’t reorder items.- Moving an item to a sibling list can violate
max_items. Thereceivecallback appends unconditionally while the Add button correctly stops at the limit. - The move/type selects are unlabeled. They need an accessible name for screen-reader users.
- The duplicate-plugin-block test should verify distinct
_keyvalues, since duplicate keys would break Portable Text identity downstream.
None of these appear to risk data loss or a broken build, so I’m leaving this as comment rather than request_changes.
| { type: "portable_text", action_id: "content", label: "Content" } | ||
| ``` | ||
|
|
||
| #### `block_list` |
There was a problem hiding this comment.
[needs fixing] The changeset describes portable_text and block_list well, but the canonical reference docs still omit them. docs/src/content/docs/plugins/creating-plugins/block-kit.mdx and skills/creating-plugins/references/block-kit.md only list the original element types, so plugin authors have no discoverable documentation for the new fields. Per .changeset/README.md, useful explanations and examples should not live only in the changeset/CHANGELOG — add them to the canonical docs too. I’d expand the Element Types tables in both files to include portable_text, block_list, and the existing missing entries (repeater, media_picker, etc.) with short descriptions and examples.
| #### `block_list` | |
| #### `block_list` | |
| An ordered list of registered plugin blocks... |
(Apply the doc expansion in docs/src/content/docs/plugins/creating-plugins/block-kit.mdx and skills/creating-plugins/references/block-kit.md.)
| const catalog = React.useContext(PluginBlockCatalogContext); | ||
| const siblingsContext = React.useContext(BlockListSiblingsContext); | ||
| const scope = React.useContext(BlockListScopeContext); | ||
| const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 4 } })); |
There was a problem hiding this comment.
[needs fixing] BlockKitBlockListField only wires up a PointerSensor for drag-and-drop. The sibling BlockKitRepeater uses both pointer and keyboard sensors, so keyboard users currently cannot reorder block_list items. Add the keyboard sensor for parity and accessibility.
| const sensors = useSensors(useSensor(PointerSensor, { activationConstraint: { distance: 4 } })); | |
| const sensors = useSensors( | |
| useSensor(PointerSensor, { activationConstraint: { distance: 4 } }), | |
| useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates }), | |
| ); |
| receive: (incoming) => | ||
| emitRef.current([...itemsRef.current, { ...incoming, _key: newBlockKey() }]), |
There was a problem hiding this comment.
[needs fixing] The sibling-list receive callback appends a moved item without checking whether this list has already reached field.max_items. That makes "Move to…" bypass the documented bound while the Add button correctly stays disabled. Guard the append against the list’s own limit.
| receive: (incoming) => | |
| emitRef.current([...itemsRef.current, { ...incoming, _key: newBlockKey() }]), | |
| return registerSibling({ | |
| id: listId, | |
| label: listLabel, | |
| receive: (incoming) => { | |
| if (maxItems !== undefined && itemsRef.current.length >= maxItems) return; | |
| emitRef.current([...itemsRef.current, { ...incoming, _key: newBlockKey() }]); | |
| }, | |
| }); |
| </div> | ||
|
|
||
| {pickingType && ( | ||
| <Select |
There was a problem hiding this comment.
[suggestion] The block-type picker <Select> has no accessible name. Its purpose is conveyed only by the placeholder text, which may not be exposed to assistive tech as a label. Add an explicit aria-label (or wrap it in a visible <label>).
| <Select | |
| <Select | |
| aria-label={t`Choose block type`} | |
| value="" |
| {summary && <span className="ms-2 text-kumo-subtle">{summary}</span>} | ||
| </span> | ||
| {moveTargets.length > 0 && ( | ||
| <Select |
There was a problem hiding this comment.
[suggestion] The "Move to…" <Select> also has no accessible name. Add an aria-label so screen-reader users know which sibling list the control will move the current item into.
| <Select | |
| <Select | |
| aria-label={t`Move item to another list`} | |
| value="" |
| const blocks = onChange.mock.calls.at(-1)![0] as Array<Record<string, unknown>>; | ||
| const copies = blocks.filter((b) => b._type === "acme.heading"); | ||
| expect(copies[0]!.text).toBe("Duplicate me"); | ||
| expect(copies[1]!.text).toBe("Duplicate me"); |
There was a problem hiding this comment.
[suggestion] The test verifies that two acme.heading blocks exist with identical text, but it does not assert that the duplicate received a fresh _key. Duplicate Portable Text keys would break identity downstream and should be caught if the duplicate action ever regresses. Add a _key uniqueness assertion.
| expect(copies[1]!.text).toBe("Duplicate me"); | |
| expect(copies[0]!.text).toBe("Duplicate me"); | |
| expect(copies[1]!.text).toBe("Duplicate me"); | |
| expect(copies[0]!._key).not.toBe(copies[1]!._key); |
|
Added one more piece to the same feature (c96e80c): an optional |
Overlapping PRsThis PR modifies files that are also changed by other open PRs:
This may cause merge conflicts or duplicated work. A maintainer will coordinate. |
|
Second follow-up commit (0ca416a), both found by driving the real dialog against an imported page rather than in unit tests:
Focused tests cover both (10 in the structured-fields file); the full admin suite is green (124 files / 1528 tests). |
|
This PR has been inactive for 14 days. It will be closed automatically in 7 days if there is no further activity. If you're still working on this, please push an update or leave a comment. |
…lock_list — plus Duplicate on plugin block cards portable_text mounts the standard Portable Text editor inside the plugin block dialog and round-trips full Portable Text arrays (blocks, spans, marks, markDefs, keys) without ever flattening to a string. block_list holds an ordered list of registered plugin blocks with add (from the same registered catalog the document editor offers), per-block Block Kit edit, duplicate, delete, drag reordering, and move-to-sibling-list when several lists share one form (including lists nested in repeater rows). Plugin block cards gain a Duplicate action inserting an identical copy after the original. Both new elements validate like their peers, are accepted as repeater sub-fields, and return null from the runtime renderElement like repeater.
… not offered on document-level insert surfaces A block with hidden: true stays fully registered (existing blocks render and edit unchanged, block_list items resolve their defs) but is skipped by the slash menu and by a block_list picker that declares no allowed_types. An explicit allowed_types naming the type still offers it, so plugins can ship child-only blocks reachable exclusively inside the parents that declare them.
…eds a late value until the author types; repeater handlers read the latest rows so a block moved between two rows' block lists survives; the visible-block helper lives in the editor module Found by driving the real dialog: a child block's dialog seeds its form values in an effect after first render, so the uncontrolled nested Portable Text editor mounted empty and one Save replaced the stored paragraphs with only the newly typed text. The repeater's sub-field change handler built the next rows from its render closure, so the two writes of a move (target row, then source row) let the second erase the first and the moved block vanished. Both are pinned by focused tests; visiblePluginBlocks moves into PortableTextEditor.tsx so suites that mock PluginBlockNode with partial factories keep importing.
0ca416a to
b173aaa
Compare
|
Rebased onto current
Typecheck, lint and format checks pass across the repo. Tests pass for |
|
I have read the CLA Document and I hereby sign the CLA |
|
This PR is waiting on its author because it has merge conflicts with |
|
Closing after 21 days without activity while waiting on its author because it has merge conflicts with |
What does this PR do?
Adds two structured Block Kit elements —
portable_textandblock_list— and a Duplicate action on plugin block cards, so plugin blocks can carry rich text and nested registered-block content that stays fully editable in the admin editor.portable_text: the plugin block dialog mounts the existingPortableTextEditorfor a Portable Text block array. Structure round-trips intact (blocks, spans, marks, markDefs, keys) and is never flattened to a string. The previous best option — a multilinetext_inputover stored Portable Text — showed an empty box and replaced the array with a plain string on save.block_list: an ordered list of registered plugin blocks ({ _type, _key, ...fields }items). Authors add from the same registered catalog the document editor offers, edit through the chosen block's own Block Kit form, duplicate, delete, drag to reorder, and move an item to a sibling list when one form holds several lists (including lists nested inrepeaterrows — e.g. one list per column).allowed_types/min_items/max_itemsbound the list.Both elements are additive
Elementunion members with validation mirroring their peers, are accepted asrepeatersub-fields, and returnnullfrom the runtimerenderElementexactly likerepeater. No existing block or stored value changes behavior.Discussion: #2673 (opened alongside this PR — maintainer approval pending; happy to adjust the design there).
Closes #
Type of change
Checklist
pnpm typecheckpasses (@emdash-cms/blocksand@emdash-cms/admin;registry-verificationneeds built siblings and is untouched)pnpm lintpasses (lint:quickclean)pnpm testpasses (blocks 120/120; full admin browser suite 124 files / 1523 tests; new focused suitetests/editor/block-kit-structured-fields.test.tsxcovers all three behaviors)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI-generated code disclosure
Screenshots / test output