@madebywild/sanity-link-field 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +150 -143
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +150 -142
- package/dist/index.js.map +1 -1
- package/package.json +9 -8
- package/src/input.tsx +33 -25
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: !0 });
|
|
3
|
-
var jsxRuntime = require("react/jsx-runtime"), sanityUtils = require("@madebywild/sanity-utils"), sanity = require("sanity"), asyncAutocomplete = require("@madebywild/sanity-utils/async-autocomplete"), ui = require("@sanity/ui"), changeCase = require("change-case"), React = require("react");
|
|
3
|
+
var jsxRuntime = require("react/jsx-runtime"), sanityUtils = require("@madebywild/sanity-utils"), sanity = require("sanity"), compilerRuntime = require("react/compiler-runtime"), asyncAutocomplete = require("@madebywild/sanity-utils/async-autocomplete"), ui = require("@sanity/ui"), changeCase = require("change-case"), React = require("react");
|
|
4
4
|
function _interopNamespaceCompat(e) {
|
|
5
5
|
if (e && typeof e == "object" && "default" in e) return e;
|
|
6
6
|
var n = /* @__PURE__ */ Object.create(null);
|
|
@@ -18,8 +18,11 @@ function _interopNamespaceCompat(e) {
|
|
|
18
18
|
}
|
|
19
19
|
var changeCase__namespace = /* @__PURE__ */ _interopNamespaceCompat(changeCase), React__namespace = /* @__PURE__ */ _interopNamespaceCompat(React);
|
|
20
20
|
function FieldInput(props) {
|
|
21
|
-
const options = props.schemaType.options;
|
|
22
|
-
|
|
21
|
+
const $ = compilerRuntime.c(5), options = props.schemaType.options;
|
|
22
|
+
let t0;
|
|
23
|
+
$[0] !== options?.noCustomText || $[1] !== props ? (t0 = props.members.map((member) => member.kind !== "field" || options?.noCustomText && member.name === "customText" ? null : /* @__PURE__ */ jsxRuntime.jsx(sanity.MemberField, { member, ...props }, member.key)), $[0] = options?.noCustomText, $[1] = props, $[2] = t0) : t0 = $[2];
|
|
24
|
+
let t1;
|
|
25
|
+
return $[3] !== t0 ? (t1 = /* @__PURE__ */ jsxRuntime.jsx(ui.Stack, { space: 4, children: t0 }), $[3] = t0, $[4] = t1) : t1 = $[4], t1;
|
|
23
26
|
}
|
|
24
27
|
const SectionsQuery = `
|
|
25
28
|
*[_id == $pageId && defined(pageBuilder.sectionsArray)][0]{
|
|
@@ -34,24 +37,27 @@ function InternalLinkInput(props) {
|
|
|
34
37
|
React__namespace.useEffect(() => {
|
|
35
38
|
!selectedLink?._ref && selectedSection !== void 0 && props.onChange(sanity.unset(["sectionTarget"]));
|
|
36
39
|
}, [selectedLink, selectedSection, props.onChange]);
|
|
37
|
-
const client = sanity.useClient({
|
|
40
|
+
const client = sanity.useClient({
|
|
41
|
+
apiVersion: "2025-10-21"
|
|
42
|
+
}), fetchSections = React__namespace.useCallback(async () => {
|
|
43
|
+
if (!selectedLink?._ref) return [];
|
|
44
|
+
try {
|
|
45
|
+
return await client.fetch(SectionsQuery, {
|
|
46
|
+
pageId: selectedLink._ref
|
|
47
|
+
});
|
|
48
|
+
} catch {
|
|
49
|
+
return [];
|
|
50
|
+
}
|
|
51
|
+
}, [client, selectedLink?._ref]);
|
|
38
52
|
return /* @__PURE__ */ jsxRuntime.jsxs(ui.Stack, { space: 2, children: [
|
|
39
53
|
linkFieldMember && /* @__PURE__ */ jsxRuntime.jsx(sanity.MemberField, { member: linkFieldMember, ...props }),
|
|
40
|
-
/* @__PURE__ */ jsxRuntime.jsx(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
renderValue: (value, opt) => opt?.label ? changeCase__namespace.capitalCase(opt.label) : value,
|
|
48
|
-
onChange: (value) => {
|
|
49
|
-
const next = value ? sanity.set(value, ["sectionTarget"]) : sanity.unset(["sectionTarget"]);
|
|
50
|
-
return props.onChange(next);
|
|
51
|
-
},
|
|
52
|
-
renderOption: ({ label, value }) => /* @__PURE__ */ jsxRuntime.jsx(ui.Card, { as: "button", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Box, { flex: 1, padding: 3, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: 2, children: changeCase__namespace.capitalCase(label ?? value) }) }) })
|
|
53
|
-
}
|
|
54
|
-
)
|
|
54
|
+
selectedLink?._ref && /* @__PURE__ */ jsxRuntime.jsx(asyncAutocomplete.AsyncAutocomplete, { placeholder: "Select section", noOptionsPlaceholder: "No sections found", listItems: fetchSections, value: selectedSection, renderValue: (value, opt) => opt?.label ? changeCase__namespace.capitalCase(opt.label) : value, onChange: (value_0) => {
|
|
55
|
+
const next = value_0 ? sanity.set(value_0, ["sectionTarget"]) : sanity.unset(["sectionTarget"]);
|
|
56
|
+
return props.onChange(next);
|
|
57
|
+
}, renderOption: ({
|
|
58
|
+
label,
|
|
59
|
+
value: value_1
|
|
60
|
+
}) => /* @__PURE__ */ jsxRuntime.jsx(ui.Card, { as: "button", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Box, { flex: 1, padding: 3, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: 2, children: changeCase__namespace.capitalCase(label ?? value_1) }) }) }) })
|
|
55
61
|
] });
|
|
56
62
|
}
|
|
57
63
|
function buildLinkPreview({
|
|
@@ -111,133 +117,134 @@ const typeName = "wild.link", LinkKind = {
|
|
|
111
117
|
}, visibleIfKind = sanityUtils.visibleIf("kind"), requiredIfKind = sanityUtils.requiredIf("kind"), wildSanityLinkFieldPlugin = sanity.definePlugin((config) => ({
|
|
112
118
|
name: "@madebywild/sanity-link-field",
|
|
113
119
|
schema: {
|
|
114
|
-
types: [
|
|
115
|
-
|
|
116
|
-
|
|
120
|
+
types: [sanity.defineType({
|
|
121
|
+
name: typeName,
|
|
122
|
+
type: "object",
|
|
123
|
+
title: "Link",
|
|
124
|
+
description: "Link to an internal page, external URL and more.",
|
|
125
|
+
icon: () => /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: "\u{1F517}" }),
|
|
126
|
+
components: {
|
|
127
|
+
input: (props) => /* @__PURE__ */ jsxRuntime.jsx(FieldInput, { ...props })
|
|
128
|
+
},
|
|
129
|
+
fields: [sanity.defineField({
|
|
130
|
+
name: "kind",
|
|
131
|
+
type: "string",
|
|
132
|
+
title: "Link Kind",
|
|
133
|
+
description: "Select the kind of link.",
|
|
134
|
+
validation: (R) => R.required().error("Select a link kind."),
|
|
135
|
+
options: {
|
|
136
|
+
layout: "radio",
|
|
137
|
+
direction: "horizontal",
|
|
138
|
+
list: [{
|
|
139
|
+
title: "\u{1F4C4} Internal",
|
|
140
|
+
value: LinkKind.internal
|
|
141
|
+
}, {
|
|
142
|
+
title: "\u{1F30D} External",
|
|
143
|
+
value: LinkKind.external
|
|
144
|
+
}, {
|
|
145
|
+
title: "\u{1F4E7} Email",
|
|
146
|
+
value: LinkKind.email
|
|
147
|
+
}, {
|
|
148
|
+
title: "\u260E\uFE0F Phone",
|
|
149
|
+
value: LinkKind.phone
|
|
150
|
+
}, {
|
|
151
|
+
title: "\u{1F4C3} File",
|
|
152
|
+
value: LinkKind.file
|
|
153
|
+
}]
|
|
154
|
+
}
|
|
155
|
+
}), sanity.defineField({
|
|
156
|
+
name: LinkKind.external,
|
|
157
|
+
type: "url",
|
|
158
|
+
title: "External Link",
|
|
159
|
+
...visibleIfKind(LinkKind.external),
|
|
160
|
+
...requiredIfKind(LinkKind.external)
|
|
161
|
+
}), sanity.defineField({
|
|
162
|
+
name: LinkKind.email,
|
|
163
|
+
type: "string",
|
|
164
|
+
title: "Email",
|
|
165
|
+
...visibleIfKind(LinkKind.email),
|
|
166
|
+
...requiredIfKind(LinkKind.email)
|
|
167
|
+
}), sanity.defineField({
|
|
168
|
+
name: LinkKind.phone,
|
|
169
|
+
type: "string",
|
|
170
|
+
title: "Phone",
|
|
171
|
+
...visibleIfKind(LinkKind.phone),
|
|
172
|
+
...requiredIfKind(LinkKind.phone)
|
|
173
|
+
}), sanity.defineField({
|
|
174
|
+
name: LinkKind.file,
|
|
175
|
+
type: "file",
|
|
176
|
+
title: "File",
|
|
177
|
+
...visibleIfKind(LinkKind.file),
|
|
178
|
+
...requiredIfKind(LinkKind.file)
|
|
179
|
+
}), sanity.defineField({
|
|
180
|
+
name: LinkKind.internal,
|
|
117
181
|
type: "object",
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
182
|
+
...visibleIfKind(LinkKind.internal),
|
|
183
|
+
...requiredIfKind(LinkKind.internal),
|
|
184
|
+
options: {
|
|
185
|
+
collapsed: !1,
|
|
186
|
+
collapsible: !1
|
|
187
|
+
},
|
|
121
188
|
components: {
|
|
122
|
-
|
|
189
|
+
field: (props) => /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: props.children }),
|
|
190
|
+
input: (props) => /* @__PURE__ */ jsxRuntime.jsx(InternalLinkInput, { ...props })
|
|
123
191
|
},
|
|
124
|
-
fields: [
|
|
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
|
-
fields: [
|
|
182
|
-
sanity.defineField({
|
|
183
|
-
type: "reference",
|
|
184
|
-
name: "link",
|
|
185
|
-
title: "Internal Link",
|
|
186
|
-
description: "Select a page and an optional section target.",
|
|
187
|
-
weak: config.weakReferences ?? !0,
|
|
188
|
-
to: config.internalLinkSchemaTypes,
|
|
189
|
-
options: {
|
|
190
|
-
disableNew: !0
|
|
191
|
-
}
|
|
192
|
-
}),
|
|
193
|
-
sanity.defineField({
|
|
194
|
-
type: "string",
|
|
195
|
-
name: "sectionTarget",
|
|
196
|
-
title: "Section Target"
|
|
197
|
-
})
|
|
198
|
-
]
|
|
199
|
-
}),
|
|
200
|
-
sanity.defineField({
|
|
201
|
-
name: "customText",
|
|
202
|
-
type: "string",
|
|
203
|
-
title: "Custom text",
|
|
204
|
-
description: "Will take precedence over inferred text."
|
|
205
|
-
}),
|
|
206
|
-
sanity.defineField({
|
|
207
|
-
name: "canDownload",
|
|
208
|
-
type: "boolean",
|
|
209
|
-
title: "Downloadable",
|
|
210
|
-
initialValue: !0,
|
|
211
|
-
description: "The file will be downloaded when the link is clicked.",
|
|
212
|
-
options: { layout: "switch" },
|
|
213
|
-
...visibleIfKind(LinkKind.file),
|
|
214
|
-
...requiredIfKind(LinkKind.file)
|
|
215
|
-
}),
|
|
216
|
-
sanity.defineField({
|
|
217
|
-
name: "openInNewTab",
|
|
218
|
-
type: "boolean",
|
|
219
|
-
title: "Open in new tab",
|
|
220
|
-
description: "Open the link in a new tab.",
|
|
221
|
-
initialValue: !1,
|
|
222
|
-
options: { layout: "switch" },
|
|
223
|
-
...visibleIfKind([LinkKind.external, LinkKind.internal])
|
|
224
|
-
})
|
|
225
|
-
],
|
|
226
|
-
preview: {
|
|
227
|
-
select: {
|
|
228
|
-
kind: "kind",
|
|
229
|
-
email: "email",
|
|
230
|
-
phone: "phone",
|
|
231
|
-
customText: "customText",
|
|
232
|
-
fileName: "file.asset.originalFilename",
|
|
233
|
-
externalUrl: "external",
|
|
234
|
-
internalUri: "internal.uri.current",
|
|
235
|
-
internalTitle: "internal.title"
|
|
236
|
-
},
|
|
237
|
-
prepare: (props) => buildLinkPreview(props)
|
|
238
|
-
}
|
|
239
|
-
})
|
|
240
|
-
]
|
|
192
|
+
fields: [sanity.defineField({
|
|
193
|
+
type: "reference",
|
|
194
|
+
name: "link",
|
|
195
|
+
title: "Internal Link",
|
|
196
|
+
description: "Select a page and an optional section target.",
|
|
197
|
+
weak: config.weakReferences ?? !0,
|
|
198
|
+
to: config.internalLinkSchemaTypes,
|
|
199
|
+
options: {
|
|
200
|
+
disableNew: !0
|
|
201
|
+
}
|
|
202
|
+
}), sanity.defineField({
|
|
203
|
+
type: "string",
|
|
204
|
+
name: "sectionTarget",
|
|
205
|
+
title: "Section Target"
|
|
206
|
+
})]
|
|
207
|
+
}), sanity.defineField({
|
|
208
|
+
name: "customText",
|
|
209
|
+
type: "string",
|
|
210
|
+
title: "Custom text",
|
|
211
|
+
description: "Will take precedence over inferred text."
|
|
212
|
+
}), sanity.defineField({
|
|
213
|
+
name: "canDownload",
|
|
214
|
+
type: "boolean",
|
|
215
|
+
title: "Downloadable",
|
|
216
|
+
initialValue: !0,
|
|
217
|
+
description: "The file will be downloaded when the link is clicked.",
|
|
218
|
+
options: {
|
|
219
|
+
layout: "switch"
|
|
220
|
+
},
|
|
221
|
+
...visibleIfKind(LinkKind.file),
|
|
222
|
+
...requiredIfKind(LinkKind.file)
|
|
223
|
+
}), sanity.defineField({
|
|
224
|
+
name: "openInNewTab",
|
|
225
|
+
type: "boolean",
|
|
226
|
+
title: "Open in new tab",
|
|
227
|
+
description: "Open the link in a new tab.",
|
|
228
|
+
initialValue: !1,
|
|
229
|
+
options: {
|
|
230
|
+
layout: "switch"
|
|
231
|
+
},
|
|
232
|
+
...visibleIfKind([LinkKind.external, LinkKind.internal])
|
|
233
|
+
})],
|
|
234
|
+
preview: {
|
|
235
|
+
select: {
|
|
236
|
+
kind: "kind",
|
|
237
|
+
email: "email",
|
|
238
|
+
phone: "phone",
|
|
239
|
+
customText: "customText",
|
|
240
|
+
fileName: "file.asset.originalFilename",
|
|
241
|
+
externalUrl: "external",
|
|
242
|
+
internalUri: "internal.uri.current",
|
|
243
|
+
internalTitle: "internal.title"
|
|
244
|
+
},
|
|
245
|
+
prepare: (props) => buildLinkPreview(props)
|
|
246
|
+
}
|
|
247
|
+
})]
|
|
241
248
|
}
|
|
242
249
|
}));
|
|
243
250
|
exports.LinkKind = LinkKind;
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/input.tsx","../src/preview.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { useFieldMember } from \"@madebywild/sanity-utils\";\nimport { AsyncAutocomplete } from \"@madebywild/sanity-utils/async-autocomplete\";\nimport { Box, Card, Stack, Text } from \"@sanity/ui\";\nimport * as changeCase from \"change-case\";\nimport * as React from \"react\";\nimport type { ObjectInputProps, ObjectMember, Reference } from \"sanity\";\nimport { MemberField, set, unset, useClient } from \"sanity\";\nimport type { FieldOptions } from \"./types\";\n\nfunction FieldInput(props: ObjectInputProps) {\n const options = props.schemaType.options as FieldOptions | undefined;\n\n return (\n <Stack space={4}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n if (options?.noCustomText && member.name === \"customText\") return null;\n return <MemberField key={member.key} member={member} {...props} />;\n })}\n </Stack>\n );\n}\n\nconst SectionsQuery = `\n *[_id == $pageId && defined(pageBuilder.sectionsArray)][0]{\n \"sections\": array::compact(pageBuilder.sectionsArray[]{\n \"value\": _key,\n \"label\": coalesce(sectionSettings.sectionTitle, _type),\n }),\n }.sections\n`;\n\nfunction InternalLinkInput(props: ObjectInputProps) {\n const linkFieldMember = useFieldMember(props.members, \"link\");\n\n const selectedLink = props.value?.link as Reference | undefined;\n const selectedSection = props.value?.sectionTarget as string | undefined;\n\n // Reset sectionTarget if link is removed.\n React.useEffect(() => {\n if (!selectedLink?._ref && selectedSection !== undefined) {\n props.onChange(unset([\"sectionTarget\"]));\n }\n }, [selectedLink, selectedSection, props.onChange]);\n\n const client = useClient({ apiVersion: \"2025-10-21\" });\n\n const fetchSections = React.useCallback(() => {\n if (!selectedLink?._ref) return [];\n return client.fetch(SectionsQuery, { pageId: selectedLink._ref }).catch(() => []);\n }, [client, selectedLink, SectionsQuery]);\n\n return (\n <Stack space={2}>\n {linkFieldMember && <MemberField member={linkFieldMember} {...props} />}\n <AsyncAutocomplete\n placeholder=\"Select section\"\n noOptionsPlaceholder=\"No sections found\"\n listItems={fetchSections}\n value={selectedSection}\n renderValue={(value, opt) => {\n return opt?.label ? changeCase.capitalCase(opt.label) : value;\n }}\n onChange={(value) => {\n const next = value ? set(value, [\"sectionTarget\"]) : unset([\"sectionTarget\"]);\n return props.onChange(next);\n }}\n renderOption={({ label, value }) => {\n return (\n <Card as=\"button\">\n <Box flex={1} padding={3}>\n <Text size={2}>{changeCase.capitalCase(label ?? value)}</Text>\n </Box>\n </Card>\n );\n }}\n />\n </Stack>\n );\n}\n\nexport { FieldInput, InternalLinkInput };\n","import type { LinkKind } from \"./types\";\n\n/** @public */\nexport function buildLinkPreview({\n kind,\n email,\n phone,\n fileName,\n customText,\n internalUri,\n externalUrl,\n internalTitle,\n}: {\n kind?: string;\n email?: string;\n phone?: string;\n fileName?: string;\n customText?: string;\n externalUrl?: string;\n internalUri?: string;\n internalTitle?: string;\n}) {\n switch (kind as keyof typeof LinkKind) {\n case \"internal\": {\n return {\n title: customText ?? internalTitle ?? \"Internal Link\",\n subtitle: internalUri,\n media: () => <>📄</>,\n };\n }\n case \"external\":\n return {\n title: customText ?? \"External Link\",\n subtitle: externalUrl,\n media: () => <>🌍</>,\n };\n case \"email\":\n return {\n title: customText ?? \"Email Link\",\n subtitle: email,\n media: () => <>📧</>,\n };\n case \"phone\":\n return {\n title: customText ?? \"Phone Link\",\n subtitle: phone,\n media: () => <>☎️</>,\n };\n case \"file\":\n return {\n title: customText ?? \"File Link\",\n subtitle: fileName,\n media: () => <>📃</>,\n };\n default:\n return {\n title: customText ?? \"Empty Link\",\n media: () => <>⛓️💥</>,\n };\n }\n}\n","import type { ListItem } from \"@madebywild/sanity-utils/async-autocomplete\";\nimport type { ObjectDefinition, ObjectOptions, ReferenceTo } from \"sanity\";\n\n/** @public */\nexport const typeName = \"wild.link\" as const;\n\n/** @public */\nexport const LinkKind = {\n internal: \"internal\",\n external: \"external\",\n email: \"email\",\n phone: \"phone\",\n file: \"file\",\n} as const;\n\n/** @public */\nexport type SectionQueryResult = ListItem[];\n\n/** @public */\nexport type PluginConfig = {\n weakReferences?: boolean;\n internalLinkSchemaTypes: ReferenceTo;\n};\n\n/** @public */\nexport type FieldOptions = ObjectOptions & {\n noCustomText?: boolean;\n};\n\n// Add the custom field definition to Sanity's intrinsic definitions\n// so that type checking works correctly when using this field type.\ndeclare module \"sanity\" {\n export interface IntrinsicDefinitions {\n [typeName]: Omit<ObjectDefinition, \"type\" | \"fields\" | \"options\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { requiredIf, visibleIf } from \"@madebywild/sanity-utils\";\nimport { defineField, definePlugin, defineType } from \"sanity\";\nimport { FieldInput, InternalLinkInput } from \"./input\";\nimport { buildLinkPreview } from \"./preview\";\nimport { type FieldOptions, LinkKind, type PluginConfig, type SectionQueryResult, typeName } from \"./types\";\n\nconst visibleIfKind = visibleIf(\"kind\");\nconst requiredIfKind = requiredIf(\"kind\");\n\n/** @public */\nconst wildSanityLinkFieldPlugin = definePlugin<PluginConfig>((config) => {\n return {\n name: \"@madebywild/sanity-link-field\",\n schema: {\n types: [\n defineType({\n name: typeName,\n type: \"object\",\n title: \"Link\",\n description: \"Link to an internal page, external URL and more.\",\n icon: () => <>🔗</>,\n components: {\n input: (props) => <FieldInput {...props} />,\n },\n fields: [\n defineField({\n name: \"kind\",\n type: \"string\",\n title: \"Link Kind\",\n description: \"Select the kind of link.\",\n validation: (R) => R.required().error(\"Select a link kind.\"),\n options: {\n layout: \"radio\",\n direction: \"horizontal\",\n list: [\n { title: \"📄 Internal\", value: LinkKind.internal },\n { title: \"🌍 External\", value: LinkKind.external },\n { title: \"📧 Email\", value: LinkKind.email },\n { title: \"☎️ Phone\", value: LinkKind.phone },\n { title: \"📃 File\", value: LinkKind.file },\n ],\n },\n }),\n defineField({\n name: LinkKind.external,\n type: \"url\",\n title: \"External Link\",\n ...visibleIfKind(LinkKind.external),\n ...requiredIfKind(LinkKind.external),\n }),\n defineField({\n name: LinkKind.email,\n type: \"string\",\n title: \"Email\",\n ...visibleIfKind(LinkKind.email),\n ...requiredIfKind(LinkKind.email),\n }),\n defineField({\n name: LinkKind.phone,\n type: \"string\",\n title: \"Phone\",\n ...visibleIfKind(LinkKind.phone),\n ...requiredIfKind(LinkKind.phone),\n }),\n defineField({\n name: LinkKind.file,\n type: \"file\",\n title: \"File\",\n ...visibleIfKind(LinkKind.file),\n ...requiredIfKind(LinkKind.file),\n }),\n defineField({\n name: LinkKind.internal,\n type: \"object\",\n ...visibleIfKind(LinkKind.internal),\n ...requiredIfKind(LinkKind.internal),\n options: { collapsed: false, collapsible: false },\n components: {\n field: (props) => <>{props.children}</>,\n input: (props) => <InternalLinkInput {...props} />,\n },\n fields: [\n defineField({\n type: \"reference\",\n name: \"link\",\n title: \"Internal Link\",\n description: \"Select a page and an optional section target.\",\n weak: config.weakReferences ?? true,\n to: config.internalLinkSchemaTypes,\n options: {\n disableNew: true,\n },\n }),\n defineField({\n type: \"string\",\n name: \"sectionTarget\",\n title: \"Section Target\",\n }),\n ],\n }),\n defineField({\n name: \"customText\",\n type: \"string\",\n title: \"Custom text\",\n description: \"Will take precedence over inferred text.\",\n }),\n defineField({\n name: \"canDownload\",\n type: \"boolean\",\n title: \"Downloadable\",\n initialValue: true,\n description: \"The file will be downloaded when the link is clicked.\",\n options: { layout: \"switch\" },\n ...visibleIfKind(LinkKind.file),\n ...requiredIfKind(LinkKind.file),\n }),\n defineField({\n name: \"openInNewTab\",\n type: \"boolean\",\n title: \"Open in new tab\",\n description: \"Open the link in a new tab.\",\n initialValue: false,\n options: { layout: \"switch\" },\n ...visibleIfKind([LinkKind.external, LinkKind.internal]),\n }),\n ],\n preview: {\n select: {\n kind: \"kind\",\n email: \"email\",\n phone: \"phone\",\n customText: \"customText\",\n fileName: \"file.asset.originalFilename\",\n externalUrl: \"external\",\n internalUri: \"internal.uri.current\",\n internalTitle: \"internal.title\",\n },\n prepare: (props) => {\n return buildLinkPreview(props);\n },\n },\n }),\n ],\n },\n };\n});\n\nexport {\n wildSanityLinkFieldPlugin,\n typeName,\n LinkKind,\n buildLinkPreview,\n type PluginConfig,\n type FieldOptions,\n type SectionQueryResult,\n};\n"],"names":["jsx","Stack","MemberField","useFieldMember","React","unset","useClient","jsxs","AsyncAutocomplete","changeCase","set","Card","Box","Text","Fragment","visibleIf","requiredIf","definePlugin","defineType","defineField"],"mappings":";;;;;;;;;;;;;;;;;;;AASA,SAAS,WAAW,OAAyB;AAC3C,QAAM,UAAU,MAAM,WAAW;AAEjC,SACEA,2BAAAA,IAACC,GAAAA,OAAA,EAAM,OAAO,GACX,UAAA,MAAM,QAAQ,IAAI,CAAC,WACd,OAAO,SAAS,WAChB,SAAS,gBAAgB,OAAO,SAAS,eAAqB,OAC3DD,2BAAAA,IAACE,OAAAA,aAAA,EAA6B,QAAiB,GAAG,MAAA,GAAhC,OAAO,GAAgC,CACjE,EAAA,CACH;AAEJ;AAEA,MAAM,gBAAgB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAStB,SAAS,kBAAkB,OAAyB;AAClD,QAAM,kBAAkBC,YAAAA,eAAe,MAAM,SAAS,MAAM,GAEtD,eAAe,MAAM,OAAO,MAC5B,kBAAkB,MAAM,OAAO;AAGrCC,mBAAM,UAAU,MAAM;AAChB,KAAC,cAAc,QAAQ,oBAAoB,UAC7C,MAAM,SAASC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,YACP,cAAc;AAAA,YACd,aAAa;AAAA,YACb,SAAS,EAAE,QAAQ,SAAA;AAAA,YACnB,GAAG,cAAc,SAAS,IAAI;AAAA,YAC9B,GAAG,eAAe,SAAS,IAAI;AAAA,UAAA,CAChC;AAAA,UACDA,mBAAY;AAAA,YACV,MAAM;AAAA,YACN,MAAM;AAAA,YACN,OAAO;AAAA,YACP,aAAa;AAAA,YACb,cAAc;AAAA,YACd,SAAS,EAAE,QAAQ,SAAA;AAAA,YACnB,GAAG,cAAc,CAAC,SAAS,UAAU,SAAS,QAAQ,CAAC;AAAA,UAAA,CACxD;AAAA,QAAA;AAAA,QAEH,SAAS;AAAA,UACP,QAAQ;AAAA,YACN,MAAM;AAAA,YACN,OAAO;AAAA,YACP,OAAO;AAAA,YACP,YAAY;AAAA,YACZ,UAAU;AAAA,YACV,aAAa;AAAA,YACb,aAAa;AAAA,YACb,eAAe;AAAA,UAAA;AAAA,UAEjB,SAAS,CAAC,UACD,iBAAiB,KAAK;AAAA,QAAA;AAAA,MAEjC,CACD;AAAA,IAAA;AAAA,EACH;AAEJ,EACD;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/input.tsx","../src/preview.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { useFieldMember } from \"@madebywild/sanity-utils\";\nimport { AsyncAutocomplete } from \"@madebywild/sanity-utils/async-autocomplete\";\nimport { Box, Card, Stack, Text } from \"@sanity/ui\";\nimport * as changeCase from \"change-case\";\nimport * as React from \"react\";\nimport type { ObjectInputProps, ObjectMember, Reference } from \"sanity\";\nimport { MemberField, set, unset, useClient } from \"sanity\";\nimport type { FieldOptions } from \"./types\";\n\nfunction FieldInput(props: ObjectInputProps) {\n const options = props.schemaType.options as FieldOptions | undefined;\n\n return (\n <Stack space={4}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n if (options?.noCustomText && member.name === \"customText\") return null;\n return <MemberField key={member.key} member={member} {...props} />;\n })}\n </Stack>\n );\n}\n\nconst SectionsQuery = `\n *[_id == $pageId && defined(pageBuilder.sectionsArray)][0]{\n \"sections\": array::compact(pageBuilder.sectionsArray[]{\n \"value\": _key,\n \"label\": coalesce(sectionSettings.sectionTitle, _type),\n }),\n }.sections\n`;\n\nfunction InternalLinkInput(props: ObjectInputProps) {\n const linkFieldMember = useFieldMember(props.members, \"link\");\n\n const selectedLink = props.value?.link as Reference | undefined;\n const selectedSection = props.value?.sectionTarget as string | undefined;\n\n // Reset sectionTarget if link is removed.\n React.useEffect(() => {\n if (!selectedLink?._ref && selectedSection !== undefined) {\n props.onChange(unset([\"sectionTarget\"]));\n }\n }, [selectedLink, selectedSection, props.onChange]);\n\n const client = useClient({ apiVersion: \"2025-10-21\" });\n\n const fetchSections = React.useCallback(async () => {\n if (!selectedLink?._ref) return [];\n\n try {\n const items = await client.fetch(SectionsQuery, { pageId: selectedLink._ref });\n return items;\n } catch {\n return [];\n }\n }, [client, selectedLink?._ref]);\n\n return (\n <Stack space={2}>\n {linkFieldMember && <MemberField member={linkFieldMember} {...props} />}\n {selectedLink?._ref && (\n <AsyncAutocomplete\n placeholder=\"Select section\"\n noOptionsPlaceholder=\"No sections found\"\n listItems={fetchSections}\n value={selectedSection}\n renderValue={(value, opt) => {\n return opt?.label ? changeCase.capitalCase(opt.label) : value;\n }}\n onChange={(value) => {\n const next = value ? set(value, [\"sectionTarget\"]) : unset([\"sectionTarget\"]);\n return props.onChange(next);\n }}\n renderOption={({ label, value }) => {\n return (\n <Card as=\"button\">\n <Box flex={1} padding={3}>\n <Text size={2}>{changeCase.capitalCase(label ?? value)}</Text>\n </Box>\n </Card>\n );\n }}\n />\n )}\n </Stack>\n );\n}\n\nexport { FieldInput, InternalLinkInput };\n","import type { LinkKind } from \"./types\";\n\n/** @public */\nexport function buildLinkPreview({\n kind,\n email,\n phone,\n fileName,\n customText,\n internalUri,\n externalUrl,\n internalTitle,\n}: {\n kind?: string;\n email?: string;\n phone?: string;\n fileName?: string;\n customText?: string;\n externalUrl?: string;\n internalUri?: string;\n internalTitle?: string;\n}) {\n switch (kind as keyof typeof LinkKind) {\n case \"internal\": {\n return {\n title: customText ?? internalTitle ?? \"Internal Link\",\n subtitle: internalUri,\n media: () => <>📄</>,\n };\n }\n case \"external\":\n return {\n title: customText ?? \"External Link\",\n subtitle: externalUrl,\n media: () => <>🌍</>,\n };\n case \"email\":\n return {\n title: customText ?? \"Email Link\",\n subtitle: email,\n media: () => <>📧</>,\n };\n case \"phone\":\n return {\n title: customText ?? \"Phone Link\",\n subtitle: phone,\n media: () => <>☎️</>,\n };\n case \"file\":\n return {\n title: customText ?? \"File Link\",\n subtitle: fileName,\n media: () => <>📃</>,\n };\n default:\n return {\n title: customText ?? \"Empty Link\",\n media: () => <>⛓️💥</>,\n };\n }\n}\n","import type { ListItem } from \"@madebywild/sanity-utils/async-autocomplete\";\nimport type { ObjectDefinition, ObjectOptions, ReferenceTo } from \"sanity\";\n\n/** @public */\nexport const typeName = \"wild.link\" as const;\n\n/** @public */\nexport const LinkKind = {\n internal: \"internal\",\n external: \"external\",\n email: \"email\",\n phone: \"phone\",\n file: \"file\",\n} as const;\n\n/** @public */\nexport type SectionQueryResult = ListItem[];\n\n/** @public */\nexport type PluginConfig = {\n weakReferences?: boolean;\n internalLinkSchemaTypes: ReferenceTo;\n};\n\n/** @public */\nexport type FieldOptions = ObjectOptions & {\n noCustomText?: boolean;\n};\n\n// Add the custom field definition to Sanity's intrinsic definitions\n// so that type checking works correctly when using this field type.\ndeclare module \"sanity\" {\n export interface IntrinsicDefinitions {\n [typeName]: Omit<ObjectDefinition, \"type\" | \"fields\" | \"options\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { requiredIf, visibleIf } from \"@madebywild/sanity-utils\";\nimport { defineField, definePlugin, defineType } from \"sanity\";\nimport { FieldInput, InternalLinkInput } from \"./input\";\nimport { buildLinkPreview } from \"./preview\";\nimport { type FieldOptions, LinkKind, type PluginConfig, type SectionQueryResult, typeName } from \"./types\";\n\nconst visibleIfKind = visibleIf(\"kind\");\nconst requiredIfKind = requiredIf(\"kind\");\n\n/** @public */\nconst wildSanityLinkFieldPlugin = definePlugin<PluginConfig>((config) => {\n return {\n name: \"@madebywild/sanity-link-field\",\n schema: {\n types: [\n defineType({\n name: typeName,\n type: \"object\",\n title: \"Link\",\n description: \"Link to an internal page, external URL and more.\",\n icon: () => <>🔗</>,\n components: {\n input: (props) => <FieldInput {...props} />,\n },\n fields: [\n defineField({\n name: \"kind\",\n type: \"string\",\n title: \"Link Kind\",\n description: \"Select the kind of link.\",\n validation: (R) => R.required().error(\"Select a link kind.\"),\n options: {\n layout: \"radio\",\n direction: \"horizontal\",\n list: [\n { title: \"📄 Internal\", value: LinkKind.internal },\n { title: \"🌍 External\", value: LinkKind.external },\n { title: \"📧 Email\", value: LinkKind.email },\n { title: \"☎️ Phone\", value: LinkKind.phone },\n { title: \"📃 File\", value: LinkKind.file },\n ],\n },\n }),\n defineField({\n name: LinkKind.external,\n type: \"url\",\n title: \"External Link\",\n ...visibleIfKind(LinkKind.external),\n ...requiredIfKind(LinkKind.external),\n }),\n defineField({\n name: LinkKind.email,\n type: \"string\",\n title: \"Email\",\n ...visibleIfKind(LinkKind.email),\n ...requiredIfKind(LinkKind.email),\n }),\n defineField({\n name: LinkKind.phone,\n type: \"string\",\n title: \"Phone\",\n ...visibleIfKind(LinkKind.phone),\n ...requiredIfKind(LinkKind.phone),\n }),\n defineField({\n name: LinkKind.file,\n type: \"file\",\n title: \"File\",\n ...visibleIfKind(LinkKind.file),\n ...requiredIfKind(LinkKind.file),\n }),\n defineField({\n name: LinkKind.internal,\n type: \"object\",\n ...visibleIfKind(LinkKind.internal),\n ...requiredIfKind(LinkKind.internal),\n options: { collapsed: false, collapsible: false },\n components: {\n field: (props) => <>{props.children}</>,\n input: (props) => <InternalLinkInput {...props} />,\n },\n fields: [\n defineField({\n type: \"reference\",\n name: \"link\",\n title: \"Internal Link\",\n description: \"Select a page and an optional section target.\",\n weak: config.weakReferences ?? true,\n to: config.internalLinkSchemaTypes,\n options: {\n disableNew: true,\n },\n }),\n defineField({\n type: \"string\",\n name: \"sectionTarget\",\n title: \"Section Target\",\n }),\n ],\n }),\n defineField({\n name: \"customText\",\n type: \"string\",\n title: \"Custom text\",\n description: \"Will take precedence over inferred text.\",\n }),\n defineField({\n name: \"canDownload\",\n type: \"boolean\",\n title: \"Downloadable\",\n initialValue: true,\n description: \"The file will be downloaded when the link is clicked.\",\n options: { layout: \"switch\" },\n ...visibleIfKind(LinkKind.file),\n ...requiredIfKind(LinkKind.file),\n }),\n defineField({\n name: \"openInNewTab\",\n type: \"boolean\",\n title: \"Open in new tab\",\n description: \"Open the link in a new tab.\",\n initialValue: false,\n options: { layout: \"switch\" },\n ...visibleIfKind([LinkKind.external, LinkKind.internal]),\n }),\n ],\n preview: {\n select: {\n kind: \"kind\",\n email: \"email\",\n phone: \"phone\",\n customText: \"customText\",\n fileName: \"file.asset.originalFilename\",\n externalUrl: \"external\",\n internalUri: \"internal.uri.current\",\n internalTitle: \"internal.title\",\n },\n prepare: (props) => {\n return buildLinkPreview(props);\n },\n },\n }),\n ],\n },\n };\n});\n\nexport {\n wildSanityLinkFieldPlugin,\n typeName,\n LinkKind,\n buildLinkPreview,\n type PluginConfig,\n type FieldOptions,\n type SectionQueryResult,\n};\n"],"names":["FieldInput","props","$","_c","options","schemaType","t0","noCustomText","members","map","member","kind","name","MemberField","key","t1","jsx","Stack","SectionsQuery","InternalLinkInput","linkFieldMember","useFieldMember","selectedLink","value","link","selectedSection","sectionTarget","React","useEffect","_ref","undefined","onChange","unset","client","useClient","apiVersion","fetchSections","useCallback","fetch","pageId","jsxs","AsyncAutocomplete","opt","label","changeCase","capitalCase","next","set","Card","Box","Text","buildLinkPreview","email","phone","fileName","customText","internalUri","externalUrl","internalTitle","title","subtitle","media","Fragment","typeName","LinkKind","internal","external","file","visibleIfKind","visibleIf","requiredIfKind","requiredIf","wildSanityLinkFieldPlugin","definePlugin","config","schema","types","defineType","type","description","icon","components","input","fields","defineField","validation","R","required","error","layout","direction","list","collapsed","collapsible","field","children","weak","weakReferences","to","internalLinkSchemaTypes","disableNew","initialValue","preview","select","prepare"],"mappings":";;;;;;;;;;;;;;;;;;;AASA,SAAAA,WAAAC,OAAA;AAAA,QAAAC,IAAAC,gBAAAA,EAAA,CAAA,GACEC,UAAgBH,MAAKI,WAAWD;AAAqC,MAAAE;AAAAJ,WAAAE,SAAAG,gBAAAL,SAAAD,SAIhEK,KAAAL,MAAKO,QAAQC,IAAKC,YACbA,OAAMC,SAAU,WAChBP,SAAOG,gBAAkBG,OAAME,SAAU,eAAqB,sCAC1DC,oBAAA,EAAqCH,QAAM,GAAMT,MAAAA,GAAhCS,OAAMI,GAA+B,CAC/D,GAACZ,EAAA,CAAA,IAAAE,SAAAG,cAAAL,OAAAD,OAAAC,OAAAI,MAAAA,KAAAJ,EAAA,CAAA;AAAA,MAAAa;AAAA,SAAAb,SAAAI,MALJS,KAAAC,+BAACC,GAAAA,OAAA,EAAa,OAAA,GACXX,UAAAA,GAAAA,CAKH,GAAQJ,OAAAI,IAAAJ,OAAAa,MAAAA,KAAAb,EAAA,CAAA,GANRa;AAMQ;AAIZ,MAAMG,gBAAgB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAStB,SAASC,kBAAkBlB,OAAyB;AAClD,QAAMmB,kBAAkBC,YAAAA,eAAepB,MAAMO,SAAS,MAAM,GAEtDc,eAAerB,MAAMsB,OAAOC,MAC5BC,kBAAkBxB,MAAMsB,OAAOG;AAGrCC,mBAAMC,UAAU,MAAM;AAChB,KAACN,cAAcO,QAAQJ,oBAAoBK,UAC7C7B,MAAM8B,SAASC,OAAAA,MAAM,CAAC,eAAe,CAAC,CAAC;AAAA,EAE3C,GAAG,CAACV,cAAcG,iBAAiBxB,MAAM8B,QAAQ,CAAC;AAElD,QAAME,SAASC,OAAAA,UAAU;AAAA,IAAEC,YAAY;AAAA,EAAA,CAAc,GAE/C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
|
2
2
|
import { useFieldMember, visibleIf, requiredIf } from "@madebywild/sanity-utils";
|
|
3
3
|
import { unset, useClient, MemberField, set, definePlugin, defineType, defineField } from "sanity";
|
|
4
|
+
import { c } from "react/compiler-runtime";
|
|
4
5
|
import { AsyncAutocomplete } from "@madebywild/sanity-utils/async-autocomplete";
|
|
5
6
|
import { Stack, Card, Box, Text } from "@sanity/ui";
|
|
6
7
|
import * as changeCase from "change-case";
|
|
7
8
|
import * as React from "react";
|
|
8
9
|
function FieldInput(props) {
|
|
9
|
-
const options = props.schemaType.options;
|
|
10
|
-
|
|
10
|
+
const $ = c(5), options = props.schemaType.options;
|
|
11
|
+
let t0;
|
|
12
|
+
$[0] !== options?.noCustomText || $[1] !== props ? (t0 = props.members.map((member) => member.kind !== "field" || options?.noCustomText && member.name === "customText" ? null : /* @__PURE__ */ jsx(MemberField, { member, ...props }, member.key)), $[0] = options?.noCustomText, $[1] = props, $[2] = t0) : t0 = $[2];
|
|
13
|
+
let t1;
|
|
14
|
+
return $[3] !== t0 ? (t1 = /* @__PURE__ */ jsx(Stack, { space: 4, children: t0 }), $[3] = t0, $[4] = t1) : t1 = $[4], t1;
|
|
11
15
|
}
|
|
12
16
|
const SectionsQuery = `
|
|
13
17
|
*[_id == $pageId && defined(pageBuilder.sectionsArray)][0]{
|
|
@@ -22,24 +26,27 @@ function InternalLinkInput(props) {
|
|
|
22
26
|
React.useEffect(() => {
|
|
23
27
|
!selectedLink?._ref && selectedSection !== void 0 && props.onChange(unset(["sectionTarget"]));
|
|
24
28
|
}, [selectedLink, selectedSection, props.onChange]);
|
|
25
|
-
const client = useClient({
|
|
29
|
+
const client = useClient({
|
|
30
|
+
apiVersion: "2025-10-21"
|
|
31
|
+
}), fetchSections = React.useCallback(async () => {
|
|
32
|
+
if (!selectedLink?._ref) return [];
|
|
33
|
+
try {
|
|
34
|
+
return await client.fetch(SectionsQuery, {
|
|
35
|
+
pageId: selectedLink._ref
|
|
36
|
+
});
|
|
37
|
+
} catch {
|
|
38
|
+
return [];
|
|
39
|
+
}
|
|
40
|
+
}, [client, selectedLink?._ref]);
|
|
26
41
|
return /* @__PURE__ */ jsxs(Stack, { space: 2, children: [
|
|
27
42
|
linkFieldMember && /* @__PURE__ */ jsx(MemberField, { member: linkFieldMember, ...props }),
|
|
28
|
-
/* @__PURE__ */ jsx(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
renderValue: (value, opt) => opt?.label ? changeCase.capitalCase(opt.label) : value,
|
|
36
|
-
onChange: (value) => {
|
|
37
|
-
const next = value ? set(value, ["sectionTarget"]) : unset(["sectionTarget"]);
|
|
38
|
-
return props.onChange(next);
|
|
39
|
-
},
|
|
40
|
-
renderOption: ({ label, value }) => /* @__PURE__ */ jsx(Card, { as: "button", children: /* @__PURE__ */ jsx(Box, { flex: 1, padding: 3, children: /* @__PURE__ */ jsx(Text, { size: 2, children: changeCase.capitalCase(label ?? value) }) }) })
|
|
41
|
-
}
|
|
42
|
-
)
|
|
43
|
+
selectedLink?._ref && /* @__PURE__ */ jsx(AsyncAutocomplete, { placeholder: "Select section", noOptionsPlaceholder: "No sections found", listItems: fetchSections, value: selectedSection, renderValue: (value, opt) => opt?.label ? changeCase.capitalCase(opt.label) : value, onChange: (value_0) => {
|
|
44
|
+
const next = value_0 ? set(value_0, ["sectionTarget"]) : unset(["sectionTarget"]);
|
|
45
|
+
return props.onChange(next);
|
|
46
|
+
}, renderOption: ({
|
|
47
|
+
label,
|
|
48
|
+
value: value_1
|
|
49
|
+
}) => /* @__PURE__ */ jsx(Card, { as: "button", children: /* @__PURE__ */ jsx(Box, { flex: 1, padding: 3, children: /* @__PURE__ */ jsx(Text, { size: 2, children: changeCase.capitalCase(label ?? value_1) }) }) }) })
|
|
43
50
|
] });
|
|
44
51
|
}
|
|
45
52
|
function buildLinkPreview({
|
|
@@ -99,133 +106,134 @@ const typeName = "wild.link", LinkKind = {
|
|
|
99
106
|
}, visibleIfKind = visibleIf("kind"), requiredIfKind = requiredIf("kind"), wildSanityLinkFieldPlugin = definePlugin((config) => ({
|
|
100
107
|
name: "@madebywild/sanity-link-field",
|
|
101
108
|
schema: {
|
|
102
|
-
types: [
|
|
103
|
-
|
|
104
|
-
|
|
109
|
+
types: [defineType({
|
|
110
|
+
name: typeName,
|
|
111
|
+
type: "object",
|
|
112
|
+
title: "Link",
|
|
113
|
+
description: "Link to an internal page, external URL and more.",
|
|
114
|
+
icon: () => /* @__PURE__ */ jsx(Fragment, { children: "\u{1F517}" }),
|
|
115
|
+
components: {
|
|
116
|
+
input: (props) => /* @__PURE__ */ jsx(FieldInput, { ...props })
|
|
117
|
+
},
|
|
118
|
+
fields: [defineField({
|
|
119
|
+
name: "kind",
|
|
120
|
+
type: "string",
|
|
121
|
+
title: "Link Kind",
|
|
122
|
+
description: "Select the kind of link.",
|
|
123
|
+
validation: (R) => R.required().error("Select a link kind."),
|
|
124
|
+
options: {
|
|
125
|
+
layout: "radio",
|
|
126
|
+
direction: "horizontal",
|
|
127
|
+
list: [{
|
|
128
|
+
title: "\u{1F4C4} Internal",
|
|
129
|
+
value: LinkKind.internal
|
|
130
|
+
}, {
|
|
131
|
+
title: "\u{1F30D} External",
|
|
132
|
+
value: LinkKind.external
|
|
133
|
+
}, {
|
|
134
|
+
title: "\u{1F4E7} Email",
|
|
135
|
+
value: LinkKind.email
|
|
136
|
+
}, {
|
|
137
|
+
title: "\u260E\uFE0F Phone",
|
|
138
|
+
value: LinkKind.phone
|
|
139
|
+
}, {
|
|
140
|
+
title: "\u{1F4C3} File",
|
|
141
|
+
value: LinkKind.file
|
|
142
|
+
}]
|
|
143
|
+
}
|
|
144
|
+
}), defineField({
|
|
145
|
+
name: LinkKind.external,
|
|
146
|
+
type: "url",
|
|
147
|
+
title: "External Link",
|
|
148
|
+
...visibleIfKind(LinkKind.external),
|
|
149
|
+
...requiredIfKind(LinkKind.external)
|
|
150
|
+
}), defineField({
|
|
151
|
+
name: LinkKind.email,
|
|
152
|
+
type: "string",
|
|
153
|
+
title: "Email",
|
|
154
|
+
...visibleIfKind(LinkKind.email),
|
|
155
|
+
...requiredIfKind(LinkKind.email)
|
|
156
|
+
}), defineField({
|
|
157
|
+
name: LinkKind.phone,
|
|
158
|
+
type: "string",
|
|
159
|
+
title: "Phone",
|
|
160
|
+
...visibleIfKind(LinkKind.phone),
|
|
161
|
+
...requiredIfKind(LinkKind.phone)
|
|
162
|
+
}), defineField({
|
|
163
|
+
name: LinkKind.file,
|
|
164
|
+
type: "file",
|
|
165
|
+
title: "File",
|
|
166
|
+
...visibleIfKind(LinkKind.file),
|
|
167
|
+
...requiredIfKind(LinkKind.file)
|
|
168
|
+
}), defineField({
|
|
169
|
+
name: LinkKind.internal,
|
|
105
170
|
type: "object",
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
171
|
+
...visibleIfKind(LinkKind.internal),
|
|
172
|
+
...requiredIfKind(LinkKind.internal),
|
|
173
|
+
options: {
|
|
174
|
+
collapsed: !1,
|
|
175
|
+
collapsible: !1
|
|
176
|
+
},
|
|
109
177
|
components: {
|
|
110
|
-
|
|
178
|
+
field: (props) => /* @__PURE__ */ jsx(Fragment, { children: props.children }),
|
|
179
|
+
input: (props) => /* @__PURE__ */ jsx(InternalLinkInput, { ...props })
|
|
111
180
|
},
|
|
112
|
-
fields: [
|
|
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
|
-
fields: [
|
|
170
|
-
defineField({
|
|
171
|
-
type: "reference",
|
|
172
|
-
name: "link",
|
|
173
|
-
title: "Internal Link",
|
|
174
|
-
description: "Select a page and an optional section target.",
|
|
175
|
-
weak: config.weakReferences ?? !0,
|
|
176
|
-
to: config.internalLinkSchemaTypes,
|
|
177
|
-
options: {
|
|
178
|
-
disableNew: !0
|
|
179
|
-
}
|
|
180
|
-
}),
|
|
181
|
-
defineField({
|
|
182
|
-
type: "string",
|
|
183
|
-
name: "sectionTarget",
|
|
184
|
-
title: "Section Target"
|
|
185
|
-
})
|
|
186
|
-
]
|
|
187
|
-
}),
|
|
188
|
-
defineField({
|
|
189
|
-
name: "customText",
|
|
190
|
-
type: "string",
|
|
191
|
-
title: "Custom text",
|
|
192
|
-
description: "Will take precedence over inferred text."
|
|
193
|
-
}),
|
|
194
|
-
defineField({
|
|
195
|
-
name: "canDownload",
|
|
196
|
-
type: "boolean",
|
|
197
|
-
title: "Downloadable",
|
|
198
|
-
initialValue: !0,
|
|
199
|
-
description: "The file will be downloaded when the link is clicked.",
|
|
200
|
-
options: { layout: "switch" },
|
|
201
|
-
...visibleIfKind(LinkKind.file),
|
|
202
|
-
...requiredIfKind(LinkKind.file)
|
|
203
|
-
}),
|
|
204
|
-
defineField({
|
|
205
|
-
name: "openInNewTab",
|
|
206
|
-
type: "boolean",
|
|
207
|
-
title: "Open in new tab",
|
|
208
|
-
description: "Open the link in a new tab.",
|
|
209
|
-
initialValue: !1,
|
|
210
|
-
options: { layout: "switch" },
|
|
211
|
-
...visibleIfKind([LinkKind.external, LinkKind.internal])
|
|
212
|
-
})
|
|
213
|
-
],
|
|
214
|
-
preview: {
|
|
215
|
-
select: {
|
|
216
|
-
kind: "kind",
|
|
217
|
-
email: "email",
|
|
218
|
-
phone: "phone",
|
|
219
|
-
customText: "customText",
|
|
220
|
-
fileName: "file.asset.originalFilename",
|
|
221
|
-
externalUrl: "external",
|
|
222
|
-
internalUri: "internal.uri.current",
|
|
223
|
-
internalTitle: "internal.title"
|
|
224
|
-
},
|
|
225
|
-
prepare: (props) => buildLinkPreview(props)
|
|
226
|
-
}
|
|
227
|
-
})
|
|
228
|
-
]
|
|
181
|
+
fields: [defineField({
|
|
182
|
+
type: "reference",
|
|
183
|
+
name: "link",
|
|
184
|
+
title: "Internal Link",
|
|
185
|
+
description: "Select a page and an optional section target.",
|
|
186
|
+
weak: config.weakReferences ?? !0,
|
|
187
|
+
to: config.internalLinkSchemaTypes,
|
|
188
|
+
options: {
|
|
189
|
+
disableNew: !0
|
|
190
|
+
}
|
|
191
|
+
}), defineField({
|
|
192
|
+
type: "string",
|
|
193
|
+
name: "sectionTarget",
|
|
194
|
+
title: "Section Target"
|
|
195
|
+
})]
|
|
196
|
+
}), defineField({
|
|
197
|
+
name: "customText",
|
|
198
|
+
type: "string",
|
|
199
|
+
title: "Custom text",
|
|
200
|
+
description: "Will take precedence over inferred text."
|
|
201
|
+
}), defineField({
|
|
202
|
+
name: "canDownload",
|
|
203
|
+
type: "boolean",
|
|
204
|
+
title: "Downloadable",
|
|
205
|
+
initialValue: !0,
|
|
206
|
+
description: "The file will be downloaded when the link is clicked.",
|
|
207
|
+
options: {
|
|
208
|
+
layout: "switch"
|
|
209
|
+
},
|
|
210
|
+
...visibleIfKind(LinkKind.file),
|
|
211
|
+
...requiredIfKind(LinkKind.file)
|
|
212
|
+
}), defineField({
|
|
213
|
+
name: "openInNewTab",
|
|
214
|
+
type: "boolean",
|
|
215
|
+
title: "Open in new tab",
|
|
216
|
+
description: "Open the link in a new tab.",
|
|
217
|
+
initialValue: !1,
|
|
218
|
+
options: {
|
|
219
|
+
layout: "switch"
|
|
220
|
+
},
|
|
221
|
+
...visibleIfKind([LinkKind.external, LinkKind.internal])
|
|
222
|
+
})],
|
|
223
|
+
preview: {
|
|
224
|
+
select: {
|
|
225
|
+
kind: "kind",
|
|
226
|
+
email: "email",
|
|
227
|
+
phone: "phone",
|
|
228
|
+
customText: "customText",
|
|
229
|
+
fileName: "file.asset.originalFilename",
|
|
230
|
+
externalUrl: "external",
|
|
231
|
+
internalUri: "internal.uri.current",
|
|
232
|
+
internalTitle: "internal.title"
|
|
233
|
+
},
|
|
234
|
+
prepare: (props) => buildLinkPreview(props)
|
|
235
|
+
}
|
|
236
|
+
})]
|
|
229
237
|
}
|
|
230
238
|
}));
|
|
231
239
|
export {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/input.tsx","../src/preview.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { useFieldMember } from \"@madebywild/sanity-utils\";\nimport { AsyncAutocomplete } from \"@madebywild/sanity-utils/async-autocomplete\";\nimport { Box, Card, Stack, Text } from \"@sanity/ui\";\nimport * as changeCase from \"change-case\";\nimport * as React from \"react\";\nimport type { ObjectInputProps, ObjectMember, Reference } from \"sanity\";\nimport { MemberField, set, unset, useClient } from \"sanity\";\nimport type { FieldOptions } from \"./types\";\n\nfunction FieldInput(props: ObjectInputProps) {\n const options = props.schemaType.options as FieldOptions | undefined;\n\n return (\n <Stack space={4}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n if (options?.noCustomText && member.name === \"customText\") return null;\n return <MemberField key={member.key} member={member} {...props} />;\n })}\n </Stack>\n );\n}\n\nconst SectionsQuery = `\n *[_id == $pageId && defined(pageBuilder.sectionsArray)][0]{\n \"sections\": array::compact(pageBuilder.sectionsArray[]{\n \"value\": _key,\n \"label\": coalesce(sectionSettings.sectionTitle, _type),\n }),\n }.sections\n`;\n\nfunction InternalLinkInput(props: ObjectInputProps) {\n const linkFieldMember = useFieldMember(props.members, \"link\");\n\n const selectedLink = props.value?.link as Reference | undefined;\n const selectedSection = props.value?.sectionTarget as string | undefined;\n\n // Reset sectionTarget if link is removed.\n React.useEffect(() => {\n if (!selectedLink?._ref && selectedSection !== undefined) {\n props.onChange(unset([\"sectionTarget\"]));\n }\n }, [selectedLink, selectedSection, props.onChange]);\n\n const client = useClient({ apiVersion: \"2025-10-21\" });\n\n const fetchSections = React.useCallback(() => {\n if (!selectedLink?._ref) return [];\n return client.fetch(SectionsQuery, { pageId: selectedLink._ref }).catch(() => []);\n }, [client, selectedLink, SectionsQuery]);\n\n return (\n <Stack space={2}>\n {linkFieldMember && <MemberField member={linkFieldMember} {...props} />}\n <AsyncAutocomplete\n placeholder=\"Select section\"\n noOptionsPlaceholder=\"No sections found\"\n listItems={fetchSections}\n value={selectedSection}\n renderValue={(value, opt) => {\n return opt?.label ? changeCase.capitalCase(opt.label) : value;\n }}\n onChange={(value) => {\n const next = value ? set(value, [\"sectionTarget\"]) : unset([\"sectionTarget\"]);\n return props.onChange(next);\n }}\n renderOption={({ label, value }) => {\n return (\n <Card as=\"button\">\n <Box flex={1} padding={3}>\n <Text size={2}>{changeCase.capitalCase(label ?? value)}</Text>\n </Box>\n </Card>\n );\n }}\n />\n </Stack>\n );\n}\n\nexport { FieldInput, InternalLinkInput };\n","import type { LinkKind } from \"./types\";\n\n/** @public */\nexport function buildLinkPreview({\n kind,\n email,\n phone,\n fileName,\n customText,\n internalUri,\n externalUrl,\n internalTitle,\n}: {\n kind?: string;\n email?: string;\n phone?: string;\n fileName?: string;\n customText?: string;\n externalUrl?: string;\n internalUri?: string;\n internalTitle?: string;\n}) {\n switch (kind as keyof typeof LinkKind) {\n case \"internal\": {\n return {\n title: customText ?? internalTitle ?? \"Internal Link\",\n subtitle: internalUri,\n media: () => <>📄</>,\n };\n }\n case \"external\":\n return {\n title: customText ?? \"External Link\",\n subtitle: externalUrl,\n media: () => <>🌍</>,\n };\n case \"email\":\n return {\n title: customText ?? \"Email Link\",\n subtitle: email,\n media: () => <>📧</>,\n };\n case \"phone\":\n return {\n title: customText ?? \"Phone Link\",\n subtitle: phone,\n media: () => <>☎️</>,\n };\n case \"file\":\n return {\n title: customText ?? \"File Link\",\n subtitle: fileName,\n media: () => <>📃</>,\n };\n default:\n return {\n title: customText ?? \"Empty Link\",\n media: () => <>⛓️💥</>,\n };\n }\n}\n","import type { ListItem } from \"@madebywild/sanity-utils/async-autocomplete\";\nimport type { ObjectDefinition, ObjectOptions, ReferenceTo } from \"sanity\";\n\n/** @public */\nexport const typeName = \"wild.link\" as const;\n\n/** @public */\nexport const LinkKind = {\n internal: \"internal\",\n external: \"external\",\n email: \"email\",\n phone: \"phone\",\n file: \"file\",\n} as const;\n\n/** @public */\nexport type SectionQueryResult = ListItem[];\n\n/** @public */\nexport type PluginConfig = {\n weakReferences?: boolean;\n internalLinkSchemaTypes: ReferenceTo;\n};\n\n/** @public */\nexport type FieldOptions = ObjectOptions & {\n noCustomText?: boolean;\n};\n\n// Add the custom field definition to Sanity's intrinsic definitions\n// so that type checking works correctly when using this field type.\ndeclare module \"sanity\" {\n export interface IntrinsicDefinitions {\n [typeName]: Omit<ObjectDefinition, \"type\" | \"fields\" | \"options\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { requiredIf, visibleIf } from \"@madebywild/sanity-utils\";\nimport { defineField, definePlugin, defineType } from \"sanity\";\nimport { FieldInput, InternalLinkInput } from \"./input\";\nimport { buildLinkPreview } from \"./preview\";\nimport { type FieldOptions, LinkKind, type PluginConfig, type SectionQueryResult, typeName } from \"./types\";\n\nconst visibleIfKind = visibleIf(\"kind\");\nconst requiredIfKind = requiredIf(\"kind\");\n\n/** @public */\nconst wildSanityLinkFieldPlugin = definePlugin<PluginConfig>((config) => {\n return {\n name: \"@madebywild/sanity-link-field\",\n schema: {\n types: [\n defineType({\n name: typeName,\n type: \"object\",\n title: \"Link\",\n description: \"Link to an internal page, external URL and more.\",\n icon: () => <>🔗</>,\n components: {\n input: (props) => <FieldInput {...props} />,\n },\n fields: [\n defineField({\n name: \"kind\",\n type: \"string\",\n title: \"Link Kind\",\n description: \"Select the kind of link.\",\n validation: (R) => R.required().error(\"Select a link kind.\"),\n options: {\n layout: \"radio\",\n direction: \"horizontal\",\n list: [\n { title: \"📄 Internal\", value: LinkKind.internal },\n { title: \"🌍 External\", value: LinkKind.external },\n { title: \"📧 Email\", value: LinkKind.email },\n { title: \"☎️ Phone\", value: LinkKind.phone },\n { title: \"📃 File\", value: LinkKind.file },\n ],\n },\n }),\n defineField({\n name: LinkKind.external,\n type: \"url\",\n title: \"External Link\",\n ...visibleIfKind(LinkKind.external),\n ...requiredIfKind(LinkKind.external),\n }),\n defineField({\n name: LinkKind.email,\n type: \"string\",\n title: \"Email\",\n ...visibleIfKind(LinkKind.email),\n ...requiredIfKind(LinkKind.email),\n }),\n defineField({\n name: LinkKind.phone,\n type: \"string\",\n title: \"Phone\",\n ...visibleIfKind(LinkKind.phone),\n ...requiredIfKind(LinkKind.phone),\n }),\n defineField({\n name: LinkKind.file,\n type: \"file\",\n title: \"File\",\n ...visibleIfKind(LinkKind.file),\n ...requiredIfKind(LinkKind.file),\n }),\n defineField({\n name: LinkKind.internal,\n type: \"object\",\n ...visibleIfKind(LinkKind.internal),\n ...requiredIfKind(LinkKind.internal),\n options: { collapsed: false, collapsible: false },\n components: {\n field: (props) => <>{props.children}</>,\n input: (props) => <InternalLinkInput {...props} />,\n },\n fields: [\n defineField({\n type: \"reference\",\n name: \"link\",\n title: \"Internal Link\",\n description: \"Select a page and an optional section target.\",\n weak: config.weakReferences ?? true,\n to: config.internalLinkSchemaTypes,\n options: {\n disableNew: true,\n },\n }),\n defineField({\n type: \"string\",\n name: \"sectionTarget\",\n title: \"Section Target\",\n }),\n ],\n }),\n defineField({\n name: \"customText\",\n type: \"string\",\n title: \"Custom text\",\n description: \"Will take precedence over inferred text.\",\n }),\n defineField({\n name: \"canDownload\",\n type: \"boolean\",\n title: \"Downloadable\",\n initialValue: true,\n description: \"The file will be downloaded when the link is clicked.\",\n options: { layout: \"switch\" },\n ...visibleIfKind(LinkKind.file),\n ...requiredIfKind(LinkKind.file),\n }),\n defineField({\n name: \"openInNewTab\",\n type: \"boolean\",\n title: \"Open in new tab\",\n description: \"Open the link in a new tab.\",\n initialValue: false,\n options: { layout: \"switch\" },\n ...visibleIfKind([LinkKind.external, LinkKind.internal]),\n }),\n ],\n preview: {\n select: {\n kind: \"kind\",\n email: \"email\",\n phone: \"phone\",\n customText: \"customText\",\n fileName: \"file.asset.originalFilename\",\n externalUrl: \"external\",\n internalUri: \"internal.uri.current\",\n internalTitle: \"internal.title\",\n },\n prepare: (props) => {\n return buildLinkPreview(props);\n },\n },\n }),\n ],\n },\n };\n});\n\nexport {\n wildSanityLinkFieldPlugin,\n typeName,\n LinkKind,\n buildLinkPreview,\n type PluginConfig,\n type FieldOptions,\n type SectionQueryResult,\n};\n"],"names":[],"mappings":";;;;;;;AASA,SAAS,WAAW,OAAyB;AAC3C,QAAM,UAAU,MAAM,WAAW;AAEjC,SACE,oBAAC,OAAA,EAAM,OAAO,GACX,UAAA,MAAM,QAAQ,IAAI,CAAC,WACd,OAAO,SAAS,WAChB,SAAS,gBAAgB,OAAO,SAAS,eAAqB,OAC3D,oBAAC,aAAA,EAA6B,QAAiB,GAAG,MAAA,GAAhC,OAAO,GAAgC,CACjE,EAAA,CACH;AAEJ;AAEA,MAAM,gBAAgB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAStB,SAAS,kBAAkB,OAAyB;AAClD,QAAM,kBAAkB,eAAe,MAAM,SAAS,MAAM,GAEtD,eAAe,MAAM,OAAO,MAC5B,kBAAkB,MAAM,OAAO;AAGrC,QAAM,UAAU,MAAM;AAChB,KAAC,cAAc,QAAQ,oBAAoB,UAC7C,MAAM,SAAS,MAAM,CAAC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Q,CAAC;AAAA,UAAA,CACxD;AAAA,QAAA;AAAA,QAEH,SAAS;AAAA,UACP,QAAQ;AAAA,YACN,MAAM;AAAA,YACN,OAAO;AAAA,YACP,OAAO;AAAA,YACP,YAAY;AAAA,YACZ,UAAU;AAAA,YACV,aAAa;AAAA,YACb,aAAa;AAAA,YACb,eAAe;AAAA,UAAA;AAAA,UAEjB,SAAS,CAAC,UACD,iBAAiB,KAAK;AAAA,QAAA;AAAA,MAEjC,CACD;AAAA,IAAA;AAAA,EACH;AAEJ,EACD;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/input.tsx","../src/preview.tsx","../src/types.tsx","../src/index.tsx"],"sourcesContent":["import { useFieldMember } from \"@madebywild/sanity-utils\";\nimport { AsyncAutocomplete } from \"@madebywild/sanity-utils/async-autocomplete\";\nimport { Box, Card, Stack, Text } from \"@sanity/ui\";\nimport * as changeCase from \"change-case\";\nimport * as React from \"react\";\nimport type { ObjectInputProps, ObjectMember, Reference } from \"sanity\";\nimport { MemberField, set, unset, useClient } from \"sanity\";\nimport type { FieldOptions } from \"./types\";\n\nfunction FieldInput(props: ObjectInputProps) {\n const options = props.schemaType.options as FieldOptions | undefined;\n\n return (\n <Stack space={4}>\n {props.members.map((member: ObjectMember) => {\n if (member.kind !== \"field\") return null;\n if (options?.noCustomText && member.name === \"customText\") return null;\n return <MemberField key={member.key} member={member} {...props} />;\n })}\n </Stack>\n );\n}\n\nconst SectionsQuery = `\n *[_id == $pageId && defined(pageBuilder.sectionsArray)][0]{\n \"sections\": array::compact(pageBuilder.sectionsArray[]{\n \"value\": _key,\n \"label\": coalesce(sectionSettings.sectionTitle, _type),\n }),\n }.sections\n`;\n\nfunction InternalLinkInput(props: ObjectInputProps) {\n const linkFieldMember = useFieldMember(props.members, \"link\");\n\n const selectedLink = props.value?.link as Reference | undefined;\n const selectedSection = props.value?.sectionTarget as string | undefined;\n\n // Reset sectionTarget if link is removed.\n React.useEffect(() => {\n if (!selectedLink?._ref && selectedSection !== undefined) {\n props.onChange(unset([\"sectionTarget\"]));\n }\n }, [selectedLink, selectedSection, props.onChange]);\n\n const client = useClient({ apiVersion: \"2025-10-21\" });\n\n const fetchSections = React.useCallback(async () => {\n if (!selectedLink?._ref) return [];\n\n try {\n const items = await client.fetch(SectionsQuery, { pageId: selectedLink._ref });\n return items;\n } catch {\n return [];\n }\n }, [client, selectedLink?._ref]);\n\n return (\n <Stack space={2}>\n {linkFieldMember && <MemberField member={linkFieldMember} {...props} />}\n {selectedLink?._ref && (\n <AsyncAutocomplete\n placeholder=\"Select section\"\n noOptionsPlaceholder=\"No sections found\"\n listItems={fetchSections}\n value={selectedSection}\n renderValue={(value, opt) => {\n return opt?.label ? changeCase.capitalCase(opt.label) : value;\n }}\n onChange={(value) => {\n const next = value ? set(value, [\"sectionTarget\"]) : unset([\"sectionTarget\"]);\n return props.onChange(next);\n }}\n renderOption={({ label, value }) => {\n return (\n <Card as=\"button\">\n <Box flex={1} padding={3}>\n <Text size={2}>{changeCase.capitalCase(label ?? value)}</Text>\n </Box>\n </Card>\n );\n }}\n />\n )}\n </Stack>\n );\n}\n\nexport { FieldInput, InternalLinkInput };\n","import type { LinkKind } from \"./types\";\n\n/** @public */\nexport function buildLinkPreview({\n kind,\n email,\n phone,\n fileName,\n customText,\n internalUri,\n externalUrl,\n internalTitle,\n}: {\n kind?: string;\n email?: string;\n phone?: string;\n fileName?: string;\n customText?: string;\n externalUrl?: string;\n internalUri?: string;\n internalTitle?: string;\n}) {\n switch (kind as keyof typeof LinkKind) {\n case \"internal\": {\n return {\n title: customText ?? internalTitle ?? \"Internal Link\",\n subtitle: internalUri,\n media: () => <>📄</>,\n };\n }\n case \"external\":\n return {\n title: customText ?? \"External Link\",\n subtitle: externalUrl,\n media: () => <>🌍</>,\n };\n case \"email\":\n return {\n title: customText ?? \"Email Link\",\n subtitle: email,\n media: () => <>📧</>,\n };\n case \"phone\":\n return {\n title: customText ?? \"Phone Link\",\n subtitle: phone,\n media: () => <>☎️</>,\n };\n case \"file\":\n return {\n title: customText ?? \"File Link\",\n subtitle: fileName,\n media: () => <>📃</>,\n };\n default:\n return {\n title: customText ?? \"Empty Link\",\n media: () => <>⛓️💥</>,\n };\n }\n}\n","import type { ListItem } from \"@madebywild/sanity-utils/async-autocomplete\";\nimport type { ObjectDefinition, ObjectOptions, ReferenceTo } from \"sanity\";\n\n/** @public */\nexport const typeName = \"wild.link\" as const;\n\n/** @public */\nexport const LinkKind = {\n internal: \"internal\",\n external: \"external\",\n email: \"email\",\n phone: \"phone\",\n file: \"file\",\n} as const;\n\n/** @public */\nexport type SectionQueryResult = ListItem[];\n\n/** @public */\nexport type PluginConfig = {\n weakReferences?: boolean;\n internalLinkSchemaTypes: ReferenceTo;\n};\n\n/** @public */\nexport type FieldOptions = ObjectOptions & {\n noCustomText?: boolean;\n};\n\n// Add the custom field definition to Sanity's intrinsic definitions\n// so that type checking works correctly when using this field type.\ndeclare module \"sanity\" {\n export interface IntrinsicDefinitions {\n [typeName]: Omit<ObjectDefinition, \"type\" | \"fields\" | \"options\"> & {\n type: typeof typeName;\n options?: FieldOptions;\n };\n }\n}\n","import { requiredIf, visibleIf } from \"@madebywild/sanity-utils\";\nimport { defineField, definePlugin, defineType } from \"sanity\";\nimport { FieldInput, InternalLinkInput } from \"./input\";\nimport { buildLinkPreview } from \"./preview\";\nimport { type FieldOptions, LinkKind, type PluginConfig, type SectionQueryResult, typeName } from \"./types\";\n\nconst visibleIfKind = visibleIf(\"kind\");\nconst requiredIfKind = requiredIf(\"kind\");\n\n/** @public */\nconst wildSanityLinkFieldPlugin = definePlugin<PluginConfig>((config) => {\n return {\n name: \"@madebywild/sanity-link-field\",\n schema: {\n types: [\n defineType({\n name: typeName,\n type: \"object\",\n title: \"Link\",\n description: \"Link to an internal page, external URL and more.\",\n icon: () => <>🔗</>,\n components: {\n input: (props) => <FieldInput {...props} />,\n },\n fields: [\n defineField({\n name: \"kind\",\n type: \"string\",\n title: \"Link Kind\",\n description: \"Select the kind of link.\",\n validation: (R) => R.required().error(\"Select a link kind.\"),\n options: {\n layout: \"radio\",\n direction: \"horizontal\",\n list: [\n { title: \"📄 Internal\", value: LinkKind.internal },\n { title: \"🌍 External\", value: LinkKind.external },\n { title: \"📧 Email\", value: LinkKind.email },\n { title: \"☎️ Phone\", value: LinkKind.phone },\n { title: \"📃 File\", value: LinkKind.file },\n ],\n },\n }),\n defineField({\n name: LinkKind.external,\n type: \"url\",\n title: \"External Link\",\n ...visibleIfKind(LinkKind.external),\n ...requiredIfKind(LinkKind.external),\n }),\n defineField({\n name: LinkKind.email,\n type: \"string\",\n title: \"Email\",\n ...visibleIfKind(LinkKind.email),\n ...requiredIfKind(LinkKind.email),\n }),\n defineField({\n name: LinkKind.phone,\n type: \"string\",\n title: \"Phone\",\n ...visibleIfKind(LinkKind.phone),\n ...requiredIfKind(LinkKind.phone),\n }),\n defineField({\n name: LinkKind.file,\n type: \"file\",\n title: \"File\",\n ...visibleIfKind(LinkKind.file),\n ...requiredIfKind(LinkKind.file),\n }),\n defineField({\n name: LinkKind.internal,\n type: \"object\",\n ...visibleIfKind(LinkKind.internal),\n ...requiredIfKind(LinkKind.internal),\n options: { collapsed: false, collapsible: false },\n components: {\n field: (props) => <>{props.children}</>,\n input: (props) => <InternalLinkInput {...props} />,\n },\n fields: [\n defineField({\n type: \"reference\",\n name: \"link\",\n title: \"Internal Link\",\n description: \"Select a page and an optional section target.\",\n weak: config.weakReferences ?? true,\n to: config.internalLinkSchemaTypes,\n options: {\n disableNew: true,\n },\n }),\n defineField({\n type: \"string\",\n name: \"sectionTarget\",\n title: \"Section Target\",\n }),\n ],\n }),\n defineField({\n name: \"customText\",\n type: \"string\",\n title: \"Custom text\",\n description: \"Will take precedence over inferred text.\",\n }),\n defineField({\n name: \"canDownload\",\n type: \"boolean\",\n title: \"Downloadable\",\n initialValue: true,\n description: \"The file will be downloaded when the link is clicked.\",\n options: { layout: \"switch\" },\n ...visibleIfKind(LinkKind.file),\n ...requiredIfKind(LinkKind.file),\n }),\n defineField({\n name: \"openInNewTab\",\n type: \"boolean\",\n title: \"Open in new tab\",\n description: \"Open the link in a new tab.\",\n initialValue: false,\n options: { layout: \"switch\" },\n ...visibleIfKind([LinkKind.external, LinkKind.internal]),\n }),\n ],\n preview: {\n select: {\n kind: \"kind\",\n email: \"email\",\n phone: \"phone\",\n customText: \"customText\",\n fileName: \"file.asset.originalFilename\",\n externalUrl: \"external\",\n internalUri: \"internal.uri.current\",\n internalTitle: \"internal.title\",\n },\n prepare: (props) => {\n return buildLinkPreview(props);\n },\n },\n }),\n ],\n },\n };\n});\n\nexport {\n wildSanityLinkFieldPlugin,\n typeName,\n LinkKind,\n buildLinkPreview,\n type PluginConfig,\n type FieldOptions,\n type SectionQueryResult,\n};\n"],"names":["FieldInput","props","$","_c","options","schemaType","t0","noCustomText","members","map","member","kind","name","key","t1","SectionsQuery","InternalLinkInput","linkFieldMember","useFieldMember","selectedLink","value","link","selectedSection","sectionTarget","React","useEffect","_ref","undefined","onChange","unset","client","useClient","apiVersion","fetchSections","useCallback","fetch","pageId","opt","label","changeCase","capitalCase","next","set","buildLinkPreview","email","phone","fileName","customText","internalUri","externalUrl","internalTitle","title","subtitle","media","typeName","LinkKind","internal","external","file","visibleIfKind","visibleIf","requiredIfKind","requiredIf","wildSanityLinkFieldPlugin","definePlugin","config","schema","types","defineType","type","description","icon","components","input","fields","defineField","validation","R","required","error","layout","direction","list","collapsed","collapsible","field","children","weak","weakReferences","to","internalLinkSchemaTypes","disableNew","initialValue","preview","select","prepare"],"mappings":";;;;;;;;AASA,SAAAA,WAAAC,OAAA;AAAA,QAAAC,IAAAC,EAAA,CAAA,GACEC,UAAgBH,MAAKI,WAAWD;AAAqC,MAAAE;AAAAJ,WAAAE,SAAAG,gBAAAL,SAAAD,SAIhEK,KAAAL,MAAKO,QAAQC,IAAKC,YACbA,OAAMC,SAAU,WAChBP,SAAOG,gBAAkBG,OAAME,SAAU,eAAqB,2BAC1D,aAAA,EAAqCF,QA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AAA,MAEJqC,SAAS;AAAA,QACPC,QAAQ;AAAA,UACNnF,MAAM;AAAA,UACNiC,OAAO;AAAA,UACPC,OAAO;AAAA,UACPE,YAAY;AAAA,UACZD,UAAU;AAAA,UACVG,aAAa;AAAA,UACbD,aAAa;AAAA,UACbE,eAAe;AAAA,QAAA;AAAA,QAEjB6C,SAAU9F,CAAAA,UACD0C,iBAAiB1C,KAAK;AAAA,MAAA;AAAA,IAEjC,CACD,CAAC;AAAA,EAAA;AAGR,EACD;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@madebywild/sanity-link-field",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"license": "UNLICENSED",
|
|
@@ -22,22 +22,23 @@
|
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"change-case": "^5.4.4",
|
|
25
|
-
"@madebywild/sanity-utils": "0.2.
|
|
25
|
+
"@madebywild/sanity-utils": "0.2.4"
|
|
26
26
|
},
|
|
27
27
|
"peerDependencies": {
|
|
28
28
|
"@sanity/ui": "^3.1",
|
|
29
|
-
"sanity": "^4.17",
|
|
30
29
|
"react": "^19",
|
|
31
|
-
"react-dom": "^19"
|
|
30
|
+
"react-dom": "^19",
|
|
31
|
+
"sanity": "^4.17"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"@sanity/pkg-utils": "^9.2",
|
|
35
|
-
"
|
|
35
|
+
"@types/react": "^19",
|
|
36
|
+
"@types/react-dom": "^19",
|
|
37
|
+
"babel-plugin-react-compiler": "1.0.0",
|
|
36
38
|
"react": "^19",
|
|
37
39
|
"react-dom": "^19",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"@types/react-dom": "^19"
|
|
40
|
+
"sanity": "^4.17",
|
|
41
|
+
"typescript": "^5"
|
|
41
42
|
},
|
|
42
43
|
"scripts": {
|
|
43
44
|
"build": "pkg-utils build --strict",
|
package/src/input.tsx
CHANGED
|
@@ -45,36 +45,44 @@ function InternalLinkInput(props: ObjectInputProps) {
|
|
|
45
45
|
|
|
46
46
|
const client = useClient({ apiVersion: "2025-10-21" });
|
|
47
47
|
|
|
48
|
-
const fetchSections = React.useCallback(() => {
|
|
48
|
+
const fetchSections = React.useCallback(async () => {
|
|
49
49
|
if (!selectedLink?._ref) return [];
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
const items = await client.fetch(SectionsQuery, { pageId: selectedLink._ref });
|
|
53
|
+
return items;
|
|
54
|
+
} catch {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
}, [client, selectedLink?._ref]);
|
|
52
58
|
|
|
53
59
|
return (
|
|
54
60
|
<Stack space={2}>
|
|
55
61
|
{linkFieldMember && <MemberField member={linkFieldMember} {...props} />}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
<
|
|
72
|
-
<
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
62
|
+
{selectedLink?._ref && (
|
|
63
|
+
<AsyncAutocomplete
|
|
64
|
+
placeholder="Select section"
|
|
65
|
+
noOptionsPlaceholder="No sections found"
|
|
66
|
+
listItems={fetchSections}
|
|
67
|
+
value={selectedSection}
|
|
68
|
+
renderValue={(value, opt) => {
|
|
69
|
+
return opt?.label ? changeCase.capitalCase(opt.label) : value;
|
|
70
|
+
}}
|
|
71
|
+
onChange={(value) => {
|
|
72
|
+
const next = value ? set(value, ["sectionTarget"]) : unset(["sectionTarget"]);
|
|
73
|
+
return props.onChange(next);
|
|
74
|
+
}}
|
|
75
|
+
renderOption={({ label, value }) => {
|
|
76
|
+
return (
|
|
77
|
+
<Card as="button">
|
|
78
|
+
<Box flex={1} padding={3}>
|
|
79
|
+
<Text size={2}>{changeCase.capitalCase(label ?? value)}</Text>
|
|
80
|
+
</Box>
|
|
81
|
+
</Card>
|
|
82
|
+
);
|
|
83
|
+
}}
|
|
84
|
+
/>
|
|
85
|
+
)}
|
|
78
86
|
</Stack>
|
|
79
87
|
);
|
|
80
88
|
}
|