<!-- Thank you for the PR! Please go through the checklist below and make sure you've completed all the steps. Please review the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository if you haven't already. The following items will ensure that your PR is handled as smoothly as possible: - PR Title must follow conventional commits format. For example, `feat: my new feature`, `fix(plugin-seo): my fix`. - Minimal description explained as if explained to someone not immediately familiar with the code. - Provide before/after screenshots or code diffs if applicable. - Link any related issues/discussions from GitHub or Discord. - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Fixes # --> ### What? This PR introduces support for copy + pasting complex fields such as Arrays and Blocks. These changes introduce a new `ClipboardAction` component that houses logic for copy + pasting to and from the clipboard to supported fields. I've scoped this PR to include only Blocks & Arrays, however the structure of the components introduced lend themselves to be easily extended to other field types. I've limited the scope because there may be design & functional blockers that make it unclear how to add actions to particular fields. Supported fields: - Arrays ([Demo](https://github.com/user-attachments/assets/523916f6-77d0-43e2-9a11-a6a9d8c1b71c)) - Array Rows ([Demo](https://github.com/user-attachments/assets/0cd01a1f-3e5e-4fea-ac83-8c0bba8d1aac)) - Blocks ([Demo](https://github.com/user-attachments/assets/4c55ac2b-55f4-4793-9b53-309b2e090dd9)) - Block Rows ([Demo](https://github.com/user-attachments/assets/1b4d2bea-981a-485b-a6c4-c59a77a50567)) Fields that may be supported in the future with minimal effort by adopting the changes introduced here: - Tabs - Groups - Collapsible - Relationships This PR also encompasses e2e tests that check both field and row-level copy/pasting. ### Why? To make it simpler and faster to copy complex fields over between documents and rows within those docs. ### How? Introduces a new `ClipboardAction` component with helper utilities to aid in copy/pasting and validating field data. Addresses #2977 & #10703 Notes: - There seems to be an issue with Blocks & Arrays that contain RichText fields where the RichText field dissappears from the dom upon replacing form state. These fields are resurfaced after either saving the data or dragging/dropping the row containing them. - Copying a Row and then pasting it at the field-level will overwrite the field to include only that one row. This is intended however can be changed if requested. - Clipboard permissions are required to use this feature. [See Clipboard API caniuse](https://caniuse.com/async-clipboard). #### TODO - [x] ~~I forgot BlockReferences~~ - [x] ~~Fix tests failing due to new buttons causing locator conflicts~~ - [x] ~~Ensure deeply nested structures work~~ - [x] ~~Add missing translations~~ - [x] ~~Implement local storage instead of clipboard api~~ - [x] ~~Improve tests~~ --------- Co-authored-by: Germán Jabloñski <43938777+GermanJablo@users.noreply.github.com>
527 lines
11 KiB
TypeScript
527 lines
11 KiB
TypeScript
import type { BlocksField, CollectionConfig } from 'payload'
|
|
|
|
import { slateEditor } from '@payloadcms/richtext-slate'
|
|
|
|
import { blockFieldsSlug, textFieldsSlug } from '../../slugs.js'
|
|
import { getBlocksFieldSeedData } from './shared.js'
|
|
|
|
export const getBlocksField = (prefix?: string): BlocksField => ({
|
|
name: 'blocks',
|
|
type: 'blocks',
|
|
blocks: [
|
|
{
|
|
slug: prefix ? `${prefix}Content` : 'content',
|
|
imageURL: '/api/uploads/file/payload480x320.jpg',
|
|
interfaceName: prefix ? `${prefix}ContentBlock` : 'ContentBlock',
|
|
admin: {
|
|
components: {
|
|
Label: './collections/Blocks/components/CustomBlockLabel.tsx',
|
|
},
|
|
},
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
required: true,
|
|
},
|
|
{
|
|
name: 'richText',
|
|
type: 'richText',
|
|
editor: slateEditor({}),
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: prefix ? `${prefix}NoBlockname` : 'noBlockname',
|
|
interfaceName: prefix ? `${prefix}NoBlockname` : 'NoBlockname',
|
|
admin: {
|
|
disableBlockName: true,
|
|
},
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: prefix ? `${prefix}Number` : 'number',
|
|
interfaceName: prefix ? `${prefix}NumberBlock` : 'NumberBlock',
|
|
fields: [
|
|
{
|
|
name: 'number',
|
|
type: 'number',
|
|
required: true,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: prefix ? `${prefix}SubBlocks` : 'subBlocks',
|
|
interfaceName: prefix ? `${prefix}SubBlocksBlock` : 'SubBlocksBlock',
|
|
fields: [
|
|
{
|
|
type: 'collapsible',
|
|
fields: [
|
|
{
|
|
name: 'subBlocks',
|
|
type: 'blocks',
|
|
blocks: [
|
|
{
|
|
slug: 'textRequired',
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
required: true,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: 'number',
|
|
interfaceName: 'NumberBlock',
|
|
fields: [
|
|
{
|
|
name: 'number',
|
|
type: 'number',
|
|
required: true,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
label: 'Collapsible within Block',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: prefix ? `${prefix}Tabs` : 'tabs',
|
|
interfaceName: prefix ? `${prefix}TabsBlock` : 'TabsBlock',
|
|
fields: [
|
|
{
|
|
type: 'tabs',
|
|
tabs: [
|
|
{
|
|
fields: [
|
|
{
|
|
type: 'collapsible',
|
|
fields: [
|
|
{
|
|
// collapsible
|
|
name: 'textInCollapsible',
|
|
type: 'text',
|
|
},
|
|
],
|
|
label: 'Collapsible within Block',
|
|
},
|
|
{
|
|
type: 'row',
|
|
fields: [
|
|
{
|
|
// collapsible
|
|
name: 'textInRow',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
label: 'Tab with Collapsible',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
defaultValue: getBlocksFieldSeedData(prefix),
|
|
required: true,
|
|
})
|
|
|
|
const BlockFields: CollectionConfig = {
|
|
slug: blockFieldsSlug,
|
|
fields: [
|
|
getBlocksField(),
|
|
{
|
|
...getBlocksField(),
|
|
name: 'duplicate',
|
|
},
|
|
{
|
|
...getBlocksField('localized'),
|
|
name: 'collapsedByDefaultBlocks',
|
|
admin: {
|
|
initCollapsed: true,
|
|
},
|
|
localized: true,
|
|
},
|
|
{
|
|
...getBlocksField('localized'),
|
|
name: 'disableSort',
|
|
admin: {
|
|
isSortable: false,
|
|
},
|
|
localized: true,
|
|
},
|
|
{
|
|
...getBlocksField('localized'),
|
|
name: 'localizedBlocks',
|
|
localized: true,
|
|
},
|
|
{
|
|
name: 'i18nBlocks',
|
|
type: 'blocks',
|
|
blocks: [
|
|
{
|
|
slug: 'textInI18nBlock',
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
],
|
|
graphQL: {
|
|
singularName: 'I18nText',
|
|
},
|
|
labels: {
|
|
plural: {
|
|
en: 'Texts en',
|
|
es: 'Texts es',
|
|
},
|
|
singular: {
|
|
en: 'Text en',
|
|
es: 'Text es',
|
|
},
|
|
},
|
|
},
|
|
],
|
|
label: {
|
|
en: 'Block en',
|
|
es: 'Block es',
|
|
},
|
|
labels: {
|
|
plural: {
|
|
en: 'Blocks en',
|
|
es: 'Blocks es',
|
|
},
|
|
singular: {
|
|
en: 'Block en',
|
|
es: 'Block es',
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: 'blocksWithLocalizedArray',
|
|
type: 'blocks',
|
|
blocks: [
|
|
{
|
|
slug: 'localizedArray',
|
|
fields: [
|
|
{
|
|
name: 'array',
|
|
type: 'array',
|
|
localized: true,
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: 'blocksWithSimilarConfigs',
|
|
type: 'blocks',
|
|
blocks: [
|
|
{
|
|
slug: 'block-a',
|
|
fields: [
|
|
{
|
|
name: 'items',
|
|
type: 'array',
|
|
fields: [
|
|
{
|
|
name: 'title',
|
|
type: 'text',
|
|
required: true,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: 'block-b',
|
|
fields: [
|
|
{
|
|
name: 'items',
|
|
type: 'array',
|
|
fields: [
|
|
{
|
|
name: 'title2',
|
|
type: 'text',
|
|
required: true,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: 'group-block',
|
|
fields: [
|
|
{
|
|
name: 'group',
|
|
type: 'group',
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: 'blocksWithSimilarGroup',
|
|
type: 'blocks',
|
|
admin: {
|
|
description:
|
|
'The purpose of this field is to test validateExistingBlockIsIdentical works with similar blocks with group fields',
|
|
},
|
|
blocks: [
|
|
{
|
|
slug: 'group-block',
|
|
fields: [
|
|
{
|
|
name: 'group',
|
|
type: 'group',
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: 'block-b',
|
|
fields: [
|
|
{
|
|
name: 'items',
|
|
type: 'array',
|
|
fields: [
|
|
{
|
|
name: 'title2',
|
|
type: 'text',
|
|
required: true,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: 'blocksWithMinRows',
|
|
type: 'blocks',
|
|
blocks: [
|
|
{
|
|
slug: 'blockWithMinRows',
|
|
fields: [
|
|
{
|
|
name: 'blockTitle',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
minRows: 2,
|
|
},
|
|
{
|
|
name: 'customBlocks',
|
|
type: 'blocks',
|
|
blocks: [
|
|
{
|
|
slug: 'block-1',
|
|
fields: [
|
|
{
|
|
name: 'block1Title',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: 'block-2',
|
|
fields: [
|
|
{
|
|
name: 'block2Title',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: 'ui',
|
|
type: 'ui',
|
|
admin: {
|
|
components: {
|
|
Field: '/collections/Blocks/components/AddCustomBlocks/index.js#AddCustomBlocks',
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: 'relationshipBlocks',
|
|
type: 'blocks',
|
|
blocks: [
|
|
{
|
|
slug: 'relationships',
|
|
fields: [
|
|
{
|
|
name: 'relationship',
|
|
type: 'relationship',
|
|
relationTo: textFieldsSlug,
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: 'blockWithLabels',
|
|
type: 'blocks',
|
|
labels: {
|
|
singular: ({ t }) => t('authentication:account'),
|
|
plural: ({ t }) => t('authentication:generate'),
|
|
},
|
|
blocks: [
|
|
{
|
|
labels: {
|
|
singular: ({ t }) => t('authentication:account'),
|
|
plural: ({ t }) => t('authentication:generate'),
|
|
},
|
|
slug: 'text',
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: 'deduplicatedBlocks',
|
|
type: 'blocks',
|
|
blockReferences: ['ConfigBlockTest'],
|
|
blocks: [],
|
|
},
|
|
{
|
|
name: 'deduplicatedBlocks2',
|
|
type: 'blocks',
|
|
blockReferences: ['ConfigBlockTest'],
|
|
blocks: [],
|
|
},
|
|
{
|
|
name: 'localizedReferencesLocalizedBlock',
|
|
type: 'blocks',
|
|
blockReferences: ['localizedTextReference'],
|
|
blocks: [],
|
|
localized: true,
|
|
},
|
|
{
|
|
name: 'localizedReferences',
|
|
type: 'blocks',
|
|
// Needs to be a separate block - otherwise this will break in postgres. This is unrelated to block references
|
|
// and an issue with all blocks.
|
|
blockReferences: ['localizedTextReference2'],
|
|
blocks: [],
|
|
},
|
|
{
|
|
name: 'groupedBlocks',
|
|
type: 'blocks',
|
|
admin: {
|
|
description: 'The purpose of this field is to test Block groups.',
|
|
},
|
|
blocks: [
|
|
{
|
|
slug: 'blockWithGroupOne',
|
|
admin: {
|
|
group: 'Group',
|
|
},
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: 'blockWithGroupTwo',
|
|
admin: {
|
|
group: 'Group',
|
|
},
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: 'blockWithLocalizedGroup',
|
|
admin: {
|
|
group: {
|
|
en: 'Group in en',
|
|
es: 'Group in es',
|
|
},
|
|
},
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
{
|
|
slug: 'blockWithoutGroup',
|
|
fields: [
|
|
{
|
|
name: 'text',
|
|
type: 'text',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
{
|
|
name: 'readOnly',
|
|
type: 'blocks',
|
|
admin: {
|
|
readOnly: true,
|
|
},
|
|
defaultValue: [
|
|
{
|
|
blockType: 'readOnlyBlock',
|
|
title: 'readOnly',
|
|
},
|
|
],
|
|
blocks: [
|
|
{
|
|
slug: 'readOnlyBlock',
|
|
fields: [
|
|
{
|
|
type: 'text',
|
|
name: 'title',
|
|
defaultValue: 'readOnly',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}
|
|
|
|
export default BlockFields
|