Files
payloadcms/packages/ui/src/elements/BulkUpload/FormsManager/reducer.ts
Alessio Gravili 59414bd8f1 feat(richtext-lexical): support copy & pasting and drag & dopping files/images into the editor (#13868)
This PR adds support for inserting images into the rich text editor via
both **copy & paste** and **drag & drop**, whether from local files or
image DOM nodes.

It leverages the bulk uploads UI to provide a smooth workflow for:
- Selecting the target collection
- Filling in any required fields defined on the uploads collection
- Uploading multiple images at once

This significantly improves the UX for adding images to rich text, and
also works seamlessly when pasting images from external editors like
Google Docs or Microsoft Word.

Test pre-release: `3.57.0-internal.801ab5a`

## Showcase - drag & drop images from computer


https://github.com/user-attachments/assets/c558c034-d2e4-40d8-9035-c0681389fb7b

## Showcase - copy & paste images from computer


https://github.com/user-attachments/assets/f36faf94-5274-4151-b141-00aff2b0efa4

## Showcase - copy & paste image DOM nodes


https://github.com/user-attachments/assets/2839ed0f-3f28-4e8d-8b47-01d0cb947edc

---
- To see the specific tasks where the Asana app for GitHub is being
used, see below:
  - https://app.asana.com/0/0/1211217132290841
2025-09-24 15:04:46 +00:00

145 lines
3.5 KiB
TypeScript

import type { FormState, UploadEdits } from 'payload'
import { v4 as uuidv4 } from 'uuid'
import type { InitialForms } from './index.js'
export type State = {
activeIndex: number
forms: {
errorCount: number
formID: string
formState: FormState
uploadEdits?: UploadEdits
}[]
totalErrorCount: number
}
type Action =
| {
count: number
index: number
type: 'UPDATE_ERROR_COUNT'
}
| {
errorCount: number
formState: FormState
index: number
type: 'UPDATE_FORM'
updatedFields?: Record<string, unknown>
uploadEdits?: UploadEdits
}
| {
forms: InitialForms
type: 'ADD_FORMS'
}
| {
index: number
type: 'REMOVE_FORM'
}
| {
index: number
type: 'SET_ACTIVE_INDEX'
}
| {
state: Partial<State>
type: 'REPLACE'
}
export function formsManagementReducer(state: State, action: Action): State {
switch (action.type) {
case 'ADD_FORMS': {
const newForms: State['forms'] = []
for (let i = 0; i < action.forms.length; i++) {
newForms[i] = {
errorCount: 0,
formID: action.forms[i].formID ?? (crypto.randomUUID ? crypto.randomUUID() : uuidv4()),
formState: {
...(action.forms[i].initialState || {}),
file: {
initialValue: action.forms[i].file,
valid: true,
value: action.forms[i].file,
},
},
uploadEdits: {},
}
}
return {
...state,
activeIndex: 0,
forms: [...newForms, ...state.forms],
}
}
case 'REMOVE_FORM': {
const remainingFormStates = [...state.forms]
const [removedForm] = remainingFormStates.splice(action.index, 1)
const affectedByShift = state.activeIndex >= action.index
const nextIndex =
state.activeIndex === action.index
? action.index
: affectedByShift
? state.activeIndex - 1
: state.activeIndex
const boundedActiveIndex = Math.min(remainingFormStates.length - 1, nextIndex)
return {
...state,
activeIndex: affectedByShift ? boundedActiveIndex : state.activeIndex,
forms: remainingFormStates,
totalErrorCount: state.totalErrorCount - removedForm.errorCount,
}
}
case 'REPLACE': {
return {
...state,
...action.state,
}
}
case 'SET_ACTIVE_INDEX': {
return {
...state,
activeIndex: action.index,
}
}
case 'UPDATE_ERROR_COUNT': {
const forms = [...state.forms]
forms[action.index].errorCount = action.count
return {
...state,
forms,
totalErrorCount: state.forms.reduce((acc, form) => acc + form.errorCount, 0),
}
}
case 'UPDATE_FORM': {
const updatedForms = [...state.forms]
updatedForms[action.index].errorCount = action.errorCount
// Merge the existing formState with the new formState
updatedForms[action.index] = {
...updatedForms[action.index],
formState: {
...updatedForms[action.index].formState,
...action.formState,
},
uploadEdits: {
...updatedForms[action.index].uploadEdits,
...action.uploadEdits,
},
}
return {
...state,
forms: updatedForms,
totalErrorCount: state.forms.reduce((acc, form) => acc + form.errorCount, 0),
}
}
default: {
return state
}
}
}