-
Notifications
You must be signed in to change notification settings - Fork 3
/
linkField.tsx
267 lines (247 loc) Β· 8.15 KB
/
linkField.tsx
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import {defineField, definePlugin, defineType, type ObjectInputProps} from 'sanity'
import {CustomLinkInput} from './components/CustomLinkInput'
import {LinkInput} from './components/LinkInput'
import {LinkTypeInput} from './components/LinkTypeInput'
import {isCustomLink} from './helpers/typeGuards'
import type {LinkFieldPluginOptions, LinkValue} from './types'
/**
* A plugin that adds a custom Link field for creating internal and external links,
* as well as `mailto` and `tel`-links, all using the same intuitive UI.
*
* @param options - Options for the plugin. See {@link LinkFieldPluginOptions}
*
* @example Minimal example
* ```ts
* // sanity.config.ts
* import { defineConfig } from 'sanity'
* import { linkField } from 'sanity-plugin-link-field'
*
* export default defineConfig((
* // ...
* plugins: [
* linkField()
* ]
* })
*
* // mySchema.ts
* import { defineField, defineType } from 'sanity';
*
* export const mySchema = defineType({
* // ...
* fields: [
* // ...
* defineField({
* name: 'link',
* title: 'Link',
* type: 'link'
* }),
* ]
*});
* ```
*/
export const linkField = definePlugin<LinkFieldPluginOptions | void>((opts) => {
const {
linkableSchemaTypes = ['page'],
weakReferences = false,
referenceFilterOptions,
descriptions = {
internal: 'Link to another page or document on the website.',
external: 'Link to an absolute URL to a page on another website.',
email: 'Link to send an e-mail to the given address.',
phone: 'Link to call the given phone number.',
advanced: 'Optional. Add anchor links and custom parameters.',
parameters: 'Optional. Add custom parameters to the URL, such as UTM tags.',
anchor: 'Optional. Add an anchor to link to a specific section on the page.',
},
enableLinkParameters = true,
enableAnchorLinks = true,
customLinkTypes = [],
} = opts || {}
const linkType = {
name: 'link',
title: 'Link',
type: 'object',
fieldsets: [
{
name: 'advanced',
title: 'Advanced',
description: descriptions.advanced,
options: {
collapsible: true,
collapsed: true,
},
},
],
fields: [
defineField({
name: 'text',
type: 'string',
description: descriptions.text,
}),
defineField({
name: 'type',
type: 'string',
initialValue: 'internal',
validation: (Rule) => Rule.required(),
components: {
input: (props) => LinkTypeInput({customLinkTypes, linkableSchemaTypes, ...props}),
},
}),
// Internal
defineField({
name: 'internalLink',
type: 'reference',
to: linkableSchemaTypes.map((type) => ({
type,
})),
weak: weakReferences,
options: {
disableNew: true,
...referenceFilterOptions,
},
description: descriptions?.internal,
hidden: ({parent}) => !!parent?.type && parent?.type !== 'internal',
}),
// External
defineField({
name: 'url',
type: 'url',
description: descriptions?.external,
validation: (rule) =>
rule.uri({
allowRelative: true,
scheme: ['https', 'http'],
}),
hidden: ({parent}) => parent?.type !== 'external',
}),
// E-mail
defineField({
name: 'email',
type: 'email',
description: descriptions?.email,
hidden: ({parent}) => parent?.type !== 'email',
}),
// Phone
defineField({
name: 'phone',
type: 'string',
description: descriptions?.phone,
validation: (rule) =>
rule.custom((value, context) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (!value || (context.parent as any)?.type !== 'phone') {
return true
}
return (
(new RegExp(/^\+?[0-9\s-]*$/).test(value) &&
!value.startsWith('-') &&
!value.endsWith('-')) ||
'Must be a valid phone number'
)
}),
hidden: ({parent}) => parent?.type !== 'phone',
}),
// Custom
defineField({
name: 'value',
type: 'string',
description: descriptions?.external,
hidden: ({parent}) => !parent || !isCustomLink(parent as LinkValue),
components: {
input: (props) => CustomLinkInput({customLinkTypes, ...props}),
},
}),
// New tab
defineField({
title: 'Open in new window',
name: 'blank',
type: 'boolean',
initialValue: false,
description: descriptions.blank,
hidden: ({parent}) => parent?.type === 'email' || parent?.type === 'phone',
}),
// Parameters
...(enableLinkParameters || enableAnchorLinks
? [
...(enableLinkParameters
? [
defineField({
title: 'Parameters',
name: 'parameters',
type: 'string',
description: descriptions.parameters,
validation: (rule) =>
rule.custom((value, context) => {
if (
!value ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.parent as any)?.type === 'email' ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.parent as any)?.type === 'phone'
) {
return true
}
if (value.indexOf('?') !== 0) {
return 'Must start with ?; eg. ?utm_source=example.com&utm_medium=referral'
}
if (value.length === 1) {
return 'Must contain at least one parameter'
}
return true
}),
hidden: ({parent}) => parent?.type === 'email' || parent?.type === 'phone',
fieldset: 'advanced',
}),
]
: []),
// Anchor
...(enableAnchorLinks
? [
defineField({
title: 'Anchor',
name: 'anchor',
type: 'string',
description: descriptions.anchor,
validation: (rule) =>
rule.custom((value, context) => {
if (
!value ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.parent as any)?.type === 'email' ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(context.parent as any)?.type === 'phone'
) {
return true
}
if (value.indexOf('#') !== 0) {
return 'Must start with #; eg. #page-section-1'
}
if (value.length === 1) {
return 'Must contain at least one character'
}
return (
new RegExp(/^([-?/:@._~!$&'()*+,;=a-zA-Z0-9]|%[0-9a-fA-F]{2})*$/).test(
value.replace(/^#/, ''),
) || 'Invalid URL fragment'
)
}),
hidden: ({parent}) => parent?.type === 'email' || parent?.type === 'phone',
fieldset: 'advanced',
}),
]
: []),
]
: []),
],
components: {
input: (props: ObjectInputProps) =>
LinkInput({customLinkTypes, ...props, value: props.value as LinkValue}),
},
}
return {
name: 'link-field',
schema: {
types: [defineType(linkType)],
},
}
})