forked from proteye/keystone-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfields.ts
149 lines (132 loc) · 3.73 KB
/
fields.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { integer, text, timestamp, select, image, relationship } from '@keystone-6/core/fields'
import { document } from '@keystone-6/fields-document'
import slugify from 'slugify'
import { mainConfig } from './config'
import { componentBlocks } from './component-blocks'
export const slugField = text({
defaultValue: '',
isIndexed: 'unique',
db: { isNullable: false },
hooks: {
resolveInput: ({ resolvedData, fieldKey, item }) => {
const { slug } = resolvedData
const title = resolvedData.title ?? item?.title ?? resolvedData.name ?? item?.name ?? ''
if (slug === '') {
return slugify(title, { lower: true })
}
return resolvedData[fieldKey]
},
},
})
export const authorField = (ref: string) =>
relationship({
ref,
ui: {
displayMode: 'select',
labelField: 'name',
hideCreate: true,
createView: {
fieldMode: ({
session: {
data: { isAdmin },
},
}) => (isAdmin ? 'edit' : 'hidden'),
},
itemView: {
fieldMode: ({
session: {
data: { isAdmin },
},
}) => (isAdmin ? 'edit' : 'read'),
},
},
hooks: {
resolveInput: ({ resolvedData, fieldKey, operation, context }) => {
if (!context.session) {
return resolvedData[fieldKey]
}
const { id, isAdmin } = context.session.data
if (
!isAdmin ||
(operation === 'update' && resolvedData[fieldKey]?.disconnect) ||
(operation === 'create' && !resolvedData[fieldKey]?.connect)
) {
return { connect: { id } }
}
return resolvedData[fieldKey]
},
},
})
export const imageField = relationship({
ref: 'Image',
hooks: {
beforeOperation: async ({ resolvedData, listKey, context }) => {
const imageId = resolvedData?.image?.connect?.id
if (imageId) {
context.query.Image.updateOne({
where: { id: imageId },
data: {
type: listKey,
},
})
}
},
},
ui: {
displayMode: 'cards',
cardFields: ['image'],
inlineEdit: { fields: ['image'] },
linkToItem: true,
inlineConnect: false,
inlineCreate: { fields: ['image'] },
},
})
export const imageStorageField = image({ storage: mainConfig.storage.localImages })
export const imageAltField = text({ defaultValue: '', validation: { length: { max: 255 } } })
export const orderField = integer({ defaultValue: -1, isIndexed: true })
export const contentField = document({
formatting: true,
layouts: [
[1, 1],
[1, 1, 1],
[2, 1],
[1, 2],
[1, 2, 1],
],
links: true,
dividers: true,
ui: {
views: require.resolve('./component-blocks'),
},
componentBlocks,
})
export const statusField = select({
options: [
{ label: 'Published', value: 'published' },
{ label: 'Draft', value: 'draft' },
],
defaultValue: 'draft',
ui: {
displayMode: 'segmented-control',
},
})
export const viewsCountField = integer({
defaultValue: 0,
ui: { createView: { fieldMode: 'hidden' }, itemView: { fieldMode: 'read' } },
})
export const seoFields = {
seoTitle: text({ defaultValue: '', validation: { length: { max: 255 } } }),
seoDescription: text({ defaultValue: '', validation: { length: { max: 255 } } }),
seoKeywords: text({ defaultValue: '', validation: { length: { max: 255 } } }),
}
export const timestampFields = {
createdAt: timestamp({
defaultValue: { kind: 'now' },
ui: { createView: { fieldMode: 'hidden' }, itemView: { fieldMode: 'hidden' } },
}),
updatedAt: timestamp({
defaultValue: { kind: 'now' },
db: { updatedAt: true },
ui: { createView: { fieldMode: 'hidden' }, itemView: { fieldMode: 'hidden' } },
}),
}