Skip to content

Commit 11c3a65

Browse files
authored
feat(richtext-*): allow omitting the root editor property (#6660)
No need to add lexical/slate to the bundle if someone decides not to make use of richText fields within payload at all
1 parent 8dd5e4d commit 11c3a65

File tree

11 files changed

+32
-4
lines changed

11 files changed

+32
-4
lines changed

packages/graphql/src/schema/buildObjectType.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import {
4141
GraphQLUnionType,
4242
} from 'graphql'
4343
import { DateTimeResolver, EmailAddressResolver } from 'graphql-scalars'
44+
import { MissingEditorProp } from 'payload/errors'
4445
import { tabHasName } from 'payload/types'
4546
import { createDataloaderCacheKey, toWords } from 'payload/utilities'
4647

@@ -476,6 +477,10 @@ function buildObjectType({
476477
async resolve(parent, args, context: Context) {
477478
let depth = config.defaultDepth
478479
if (typeof args.depth !== 'undefined') depth = args.depth
480+
if (!field?.editor) {
481+
throw new MissingEditorProp(field) // while we allow disabling editor functionality, you should not have any richText fields defined if you do not have an editor
482+
}
483+
479484
if (typeof field?.editor === 'function') {
480485
throw new Error('Attempted to access unsanitized rich text editor.')
481486
}

packages/payload/src/config/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export default joi.object({
101101
defaultMaxTextLength: joi.number(),
102102
editor: joi
103103
.object()
104-
.required()
104+
.optional()
105105
.keys({
106106
CellComponent: componentSchema.optional(),
107107
FieldComponent: componentSchema.optional(),

packages/payload/src/config/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ export type Config = {
615615
*/
616616
defaultMaxTextLength?: number
617617
/** Default richtext editor to use for richText fields */
618-
editor: RichTextAdapterProvider<any, any, any>
618+
editor?: RichTextAdapterProvider<any, any, any>
619619
/**
620620
* Email Adapter
621621
*
@@ -747,7 +747,7 @@ export type SanitizedConfig = Omit<
747747
> & {
748748
collections: SanitizedCollectionConfig[]
749749
/** Default richtext editor to use for richText fields */
750-
editor: RichTextAdapter<any, any, any>
750+
editor?: RichTextAdapter<any, any, any>
751751
endpoints: Endpoint[]
752752
globals: SanitizedGlobalConfig[]
753753
i18n: Required<I18nOptions>

packages/payload/src/errors/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export { InvalidFieldName } from './InvalidFieldName.js'
1212
export { InvalidFieldRelationship } from './InvalidFieldRelationship.js'
1313
export { LockedAuth } from './LockedAuth.js'
1414
export { MissingCollectionLabel } from './MissingCollectionLabel.js'
15+
export { MissingEditorProp } from './MissingEditorProp.js'
1516
export { MissingFieldInputOptions } from './MissingFieldInputOptions.js'
1617
export { MissingFieldType } from './MissingFieldType.js'
1718
export { MissingFile } from './MissingFile.js'

packages/payload/src/exports/errors.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ export {
22
APIError,
33
AuthenticationError,
44
DuplicateCollection,
5+
DuplicateFieldName,
56
DuplicateGlobal,
67
ErrorDeletingFile,
8+
FileRetrievalError,
79
FileUploadError,
810
Forbidden,
911
InvalidConfiguration,
1012
InvalidFieldName,
1113
InvalidFieldRelationship,
1214
LockedAuth,
1315
MissingCollectionLabel,
16+
MissingEditorProp,
1417
MissingFieldInputOptions,
1518
MissingFieldType,
1619
MissingFile,

packages/payload/src/fields/config/sanitize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export const sanitizeFields = async ({
158158
// config.editor should be sanitized at this point
159159
field.editor = _config.editor
160160
} else {
161-
throw new MissingEditorProp(field)
161+
throw new MissingEditorProp(field) // while we allow disabling editor functionality, you should not have any richText fields defined if you do not have an editor
162162
}
163163
}
164164

packages/payload/src/fields/hooks/afterRead/promise.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { SanitizedGlobalConfig } from '../../../globals/config/types.js'
55
import type { PayloadRequestWithData, RequestContext } from '../../../types/index.js'
66
import type { Field, TabAsField } from '../../config/types.js'
77

8+
import { MissingEditorProp } from '../../../errors/index.js'
89
import { fieldAffectsData, tabHasName } from '../../config/types.js'
910
import getValueWithDefault from '../../getDefaultValue.js'
1011
import { relationshipPopulationPromise } from './relationshipPopulationPromise.js'
@@ -143,6 +144,9 @@ export const promise = async ({
143144
}
144145

145146
case 'richText': {
147+
if (!field?.editor) {
148+
throw new MissingEditorProp(field) // while we allow disabling editor functionality, you should not have any richText fields defined if you do not have an editor
149+
}
146150
if (typeof field?.editor === 'function') {
147151
throw new Error('Attempted to access unsanitized rich text editor.')
148152
}

packages/payload/src/fields/validations.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ export const richText: Validate<object, unknown, unknown, RichTextField> = async
268268
value,
269269
options,
270270
) => {
271+
if (!options?.editor) {
272+
throw new Error('richText field has no editor property.')
273+
}
271274
if (typeof options?.editor === 'function') {
272275
throw new Error('Attempted to access unsanitized rich text editor.')
273276
}

packages/payload/src/utilities/configToJSONSchema.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import type { SanitizedConfig } from '../config/types.js'
88
import type { Field, FieldAffectingData, Option } from '../fields/config/types.js'
99
import type { SanitizedGlobalConfig } from '../globals/config/types.js'
1010

11+
import { MissingEditorProp } from '../errors/MissingEditorProp.js'
1112
import { fieldAffectsData, tabHasName } from '../fields/config/types.js'
1213
import { deepCopyObject } from './deepCopyObject.js'
1314
import { toWords } from './formatLabels.js'
@@ -195,6 +196,9 @@ export function fieldsToJSONSchema(
195196
}
196197

197198
case 'richText': {
199+
if (!field?.editor) {
200+
throw new MissingEditorProp(field) // while we allow disabling editor functionality, you should not have any richText fields defined if you do not have an editor
201+
}
198202
if (typeof field.editor === 'function') {
199203
throw new Error('Attempted to access unsanitized rich text editor.')
200204
}

packages/ui/src/providers/ComponentMap/buildComponentMap/fields.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
SanitizedConfig,
1111
} from 'payload/types'
1212

13+
import { MissingEditorProp } from 'payload/errors'
1314
import { fieldAffectsData, fieldIsPresentationalOnly } from 'payload/types'
1415
import React, { Fragment } from 'react'
1516

@@ -566,6 +567,9 @@ export const mapFields = (args: {
566567
style: field.admin?.style,
567568
width: field.admin?.width,
568569
}
570+
if (!field?.editor) {
571+
throw new MissingEditorProp(field) // while we allow disabling editor functionality, you should not have any richText fields defined if you do not have an editor
572+
}
569573
if (typeof field?.editor === 'function') {
570574
throw new Error('Attempted to access unsanitized rich text editor.')
571575
}

0 commit comments

Comments
 (0)