bsmnt 0.11.0 → 0.12.0
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/package.json +1 -1
- package/src/modules/features/cms/sanity/files/components/ui/sanity-image/index.tsx +36 -4
- package/src/modules/features/cms/sanity/files/lib/integrations/sanity/components/rich-text.tsx +7 -1
- package/src/modules/features/cms/sanity-pagebuilder/files/components/sanity/rich-text.tsx +7 -1
- package/src/modules/features/cms/sanity-pagebuilder/files/components/ui/sanity-image/index.tsx +36 -4
- package/src/modules/features/cms/sanity-pagebuilder/files/lib/integrations/sanity/components/page-builder-input.tsx +26 -7
- package/src/modules/features/cms/sanity-pagebuilder/files/lib/integrations/sanity/components/page-builder-insert-menu.tsx +242 -0
- package/src/modules/features/cms/sanity-pagebuilder/files/lib/integrations/sanity/components/use-studio-preview-urls.ts +62 -0
- package/src/modules/features/cms/sanity-pagebuilder/files/lib/integrations/sanity/page-builder-config.ts +72 -34
- package/src/modules/features/cms/sanity-pagebuilder/files/lib/integrations/sanity/schemas/components/index.ts +1 -0
- package/src/modules/features/cms/sanity-pagebuilder/files/lib/integrations/sanity/schemas/components/studio-preview.ts +37 -0
- package/src/modules/features/cms/sanity-pagebuilder/files/lib/integrations/sanity/schemas/index.ts +3 -0
- package/src/modules/features/cms/sanity-pagebuilder/files/lib/integrations/sanity/singletons.ts +2 -0
- package/src/modules/features/cms/sanity-pagebuilder/files/lib/integrations/sanity/structure.ts +5 -0
- package/src/modules/features/cms/sanity-pagebuilder-cache/files/components/sanity/rich-text.tsx +7 -1
- package/src/modules/features/cms/sanity-pagebuilder-cache/files/components/ui/sanity-image/index.tsx +36 -4
- package/src/modules/features/cms/sanity-pagebuilder-cache/files/lib/integrations/sanity/components/page-builder-input.tsx +26 -7
- package/src/modules/features/cms/sanity-pagebuilder-cache/files/lib/integrations/sanity/components/page-builder-insert-menu.tsx +242 -0
- package/src/modules/features/cms/sanity-pagebuilder-cache/files/lib/integrations/sanity/components/use-studio-preview-urls.ts +62 -0
- package/src/modules/features/cms/sanity-pagebuilder-cache/files/lib/integrations/sanity/page-builder-config.ts +72 -34
- package/src/modules/features/cms/sanity-pagebuilder-cache/files/lib/integrations/sanity/schemas/components/index.ts +1 -0
- package/src/modules/features/cms/sanity-pagebuilder-cache/files/lib/integrations/sanity/schemas/components/studio-preview.ts +37 -0
- package/src/modules/features/cms/sanity-pagebuilder-cache/files/lib/integrations/sanity/schemas/index.ts +3 -0
- package/src/modules/features/cms/sanity-pagebuilder-cache/files/lib/integrations/sanity/singletons.ts +2 -0
- package/src/modules/features/cms/sanity-pagebuilder-cache/files/lib/integrations/sanity/structure.ts +5 -0
- package/src/templates/next-default/.biome/plugins/no-fill-without-sizes.grit +11 -0
- package/src/templates/next-default/biome.json +2 -1
- package/src/templates/next-default/components/ui/image/index.tsx +20 -12
- package/src/templates/next-default/next.config.ts +10 -4
- package/src/templates/next-experiments/.biome/plugins/no-fill-without-sizes.grit +11 -0
- package/src/templates/next-experiments/biome.json +2 -1
- package/src/templates/next-experiments/components/ui/image/index.tsx +20 -12
- package/src/templates/next-experiments/next.config.ts +10 -4
- package/src/templates/next-pagebuilder/.biome/plugins/no-fill-without-sizes.grit +11 -0
- package/src/templates/next-pagebuilder/biome.json +2 -1
- package/src/templates/next-pagebuilder/components/page-builder/components/article-content/index.tsx +5 -0
- package/src/templates/next-pagebuilder/components/page-builder/components/article-content/related-post-item.tsx +1 -0
- package/src/templates/next-pagebuilder/components/ui/image/index.tsx +20 -12
- package/src/templates/next-pagebuilder/next.config.ts +4 -3
- package/src/templates/next-pagebuilder-cache/.biome/plugins/no-fill-without-sizes.grit +11 -0
- package/src/templates/next-pagebuilder-cache/biome.json +2 -1
- package/src/templates/next-pagebuilder-cache/components/page-builder/components/article-content/index.tsx +5 -0
- package/src/templates/next-pagebuilder-cache/components/page-builder/components/article-content/related-post-item.tsx +1 -0
- package/src/templates/next-pagebuilder-cache/components/ui/image/index.tsx +21 -12
- package/src/templates/next-pagebuilder-cache/lib/utils/image-sizes.ts +72 -0
- package/src/templates/next-pagebuilder-cache/next.config.ts +4 -3
- package/src/templates/next-webgl/.biome/plugins/no-fill-without-sizes.grit +11 -0
- package/src/templates/next-webgl/biome.json +2 -1
- package/src/templates/next-webgl/components/ui/image/index.tsx +20 -12
- package/src/templates/next-webgl/next.config.ts +10 -4
package/package.json
CHANGED
|
@@ -2,7 +2,8 @@ import { getImageDimensions } from "@sanity/asset-utils";
|
|
|
2
2
|
import { Image, type ImageProps } from "@/components/ui/image";
|
|
3
3
|
import { urlForImage } from "@/lib/integrations/sanity/utils/image";
|
|
4
4
|
|
|
5
|
-
interface
|
|
5
|
+
interface SanityImageBaseProps
|
|
6
|
+
extends Omit<ImageProps, "src" | "aspectRatio" | "sizes" | "width" | "height"> {
|
|
6
7
|
image: {
|
|
7
8
|
asset?: {
|
|
8
9
|
_ref: string;
|
|
@@ -15,6 +16,38 @@ interface SanityImageProps extends Omit<ImageProps, "src" | "aspectRatio"> {
|
|
|
15
16
|
maxWidth?: number;
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
type SanityImageProps = SanityImageBaseProps &
|
|
20
|
+
(
|
|
21
|
+
| { sizes: string; width?: never; height?: never }
|
|
22
|
+
| { width: number; height: number; sizes?: string }
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const MAX_SOURCE_WIDTH = 3840;
|
|
26
|
+
|
|
27
|
+
const isGifRef = (ref: string) => ref.endsWith("-gif");
|
|
28
|
+
|
|
29
|
+
const getImageSrc = (
|
|
30
|
+
image: SanityImageProps["image"],
|
|
31
|
+
maxWidth: number,
|
|
32
|
+
croppedWidth: number,
|
|
33
|
+
) => {
|
|
34
|
+
const builder = urlForImage(image);
|
|
35
|
+
const ref = image.asset?._ref ?? "";
|
|
36
|
+
|
|
37
|
+
if (ref.endsWith("-svg")) return builder.url();
|
|
38
|
+
|
|
39
|
+
const requestWidth = Math.min(maxWidth, MAX_SOURCE_WIDTH, croppedWidth);
|
|
40
|
+
const { left, right, top, bottom } = image.crop ?? {};
|
|
41
|
+
const hasCrop = Boolean(left || right || top || bottom);
|
|
42
|
+
const isGif = isGifRef(ref);
|
|
43
|
+
|
|
44
|
+
if (!(isGif || hasCrop) && requestWidth >= croppedWidth) {
|
|
45
|
+
return builder.url();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return builder.width(requestWidth).url();
|
|
49
|
+
};
|
|
50
|
+
|
|
18
51
|
export function SanityImage({
|
|
19
52
|
image,
|
|
20
53
|
maxWidth = 1920,
|
|
@@ -39,11 +72,10 @@ export function SanityImage({
|
|
|
39
72
|
|
|
40
73
|
return (
|
|
41
74
|
<Image
|
|
42
|
-
src={
|
|
43
|
-
.width(Math.min(maxWidth, sourceWidth))
|
|
44
|
-
.url()}
|
|
75
|
+
src={getImageSrc(image, maxWidth, sourceWidth)}
|
|
45
76
|
alt={alt || image.alt || ""}
|
|
46
77
|
aspectRatio={sourceWidth / sourceHeight}
|
|
78
|
+
unoptimized={isGifRef(image.asset._ref)}
|
|
47
79
|
{...imageProps}
|
|
48
80
|
{...props}
|
|
49
81
|
/>
|
package/src/modules/features/cms/sanity/files/lib/integrations/sanity/components/rich-text.tsx
CHANGED
|
@@ -37,7 +37,13 @@ export function RichText({ content }: RichTextProps) {
|
|
|
37
37
|
value={content}
|
|
38
38
|
components={{
|
|
39
39
|
types: {
|
|
40
|
-
image: ({ value }) =>
|
|
40
|
+
image: ({ value }) => (
|
|
41
|
+
<SanityImage
|
|
42
|
+
image={value}
|
|
43
|
+
maxWidth={1920}
|
|
44
|
+
sizes="(min-width: 768px) 768px, 100vw"
|
|
45
|
+
/>
|
|
46
|
+
),
|
|
41
47
|
},
|
|
42
48
|
marks: {
|
|
43
49
|
link: ({ children, value }) => {
|
|
@@ -42,7 +42,13 @@ export const RichText = ({ content }: RichTextProps) => {
|
|
|
42
42
|
value={content}
|
|
43
43
|
components={{
|
|
44
44
|
types: {
|
|
45
|
-
image: ({ value }) =>
|
|
45
|
+
image: ({ value }) => (
|
|
46
|
+
<SanityImage
|
|
47
|
+
image={value}
|
|
48
|
+
maxWidth={1920}
|
|
49
|
+
sizes="(min-width: 768px) 768px, 100vw"
|
|
50
|
+
/>
|
|
51
|
+
),
|
|
46
52
|
table: ({ value }) => {
|
|
47
53
|
const rows = value?.rows as
|
|
48
54
|
| Array<{ _key?: string; cells?: string[] }>
|
package/src/modules/features/cms/sanity-pagebuilder/files/components/ui/sanity-image/index.tsx
CHANGED
|
@@ -2,7 +2,8 @@ import { getImageDimensions } from "@sanity/asset-utils";
|
|
|
2
2
|
import { Image, type ImageProps } from "@/components/ui/image";
|
|
3
3
|
import { urlForImage } from "@/lib/integrations/sanity/utils/image";
|
|
4
4
|
|
|
5
|
-
interface
|
|
5
|
+
interface SanityImageBaseProps
|
|
6
|
+
extends Omit<ImageProps, "src" | "aspectRatio" | "sizes" | "width" | "height"> {
|
|
6
7
|
image: {
|
|
7
8
|
asset?: {
|
|
8
9
|
_ref: string;
|
|
@@ -15,6 +16,38 @@ interface SanityImageProps extends Omit<ImageProps, "src" | "aspectRatio"> {
|
|
|
15
16
|
maxWidth?: number;
|
|
16
17
|
}
|
|
17
18
|
|
|
19
|
+
type SanityImageProps = SanityImageBaseProps &
|
|
20
|
+
(
|
|
21
|
+
| { sizes: string; width?: never; height?: never }
|
|
22
|
+
| { width: number; height: number; sizes?: string }
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const MAX_SOURCE_WIDTH = 3840;
|
|
26
|
+
|
|
27
|
+
const isGifRef = (ref: string) => ref.endsWith("-gif");
|
|
28
|
+
|
|
29
|
+
const getImageSrc = (
|
|
30
|
+
image: SanityImageProps["image"],
|
|
31
|
+
maxWidth: number,
|
|
32
|
+
croppedWidth: number,
|
|
33
|
+
) => {
|
|
34
|
+
const builder = urlForImage(image);
|
|
35
|
+
const ref = image.asset?._ref ?? "";
|
|
36
|
+
|
|
37
|
+
if (ref.endsWith("-svg")) return builder.url();
|
|
38
|
+
|
|
39
|
+
const requestWidth = Math.min(maxWidth, MAX_SOURCE_WIDTH, croppedWidth);
|
|
40
|
+
const { left, right, top, bottom } = image.crop ?? {};
|
|
41
|
+
const hasCrop = Boolean(left || right || top || bottom);
|
|
42
|
+
const isGif = isGifRef(ref);
|
|
43
|
+
|
|
44
|
+
if (!(isGif || hasCrop) && requestWidth >= croppedWidth) {
|
|
45
|
+
return builder.url();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return builder.width(requestWidth).url();
|
|
49
|
+
};
|
|
50
|
+
|
|
18
51
|
export function SanityImage({
|
|
19
52
|
image,
|
|
20
53
|
maxWidth = 1920,
|
|
@@ -39,11 +72,10 @@ export function SanityImage({
|
|
|
39
72
|
|
|
40
73
|
return (
|
|
41
74
|
<Image
|
|
42
|
-
src={
|
|
43
|
-
.width(Math.min(maxWidth, sourceWidth))
|
|
44
|
-
.url()}
|
|
75
|
+
src={getImageSrc(image, maxWidth, sourceWidth)}
|
|
45
76
|
alt={alt || image.alt || ""}
|
|
46
77
|
aspectRatio={sourceWidth / sourceHeight}
|
|
78
|
+
unoptimized={isGifRef(image.asset._ref)}
|
|
47
79
|
{...imageProps}
|
|
48
80
|
{...props}
|
|
49
81
|
/>
|
|
@@ -1,25 +1,43 @@
|
|
|
1
1
|
import { type ArrayOfObjectsInputProps, useFormValue } from "sanity";
|
|
2
|
+
import { PageBuilderInsertMenu } from "@/lib/integrations/sanity/components/page-builder-insert-menu";
|
|
3
|
+
import { useStudioPreviewUrls } from "@/lib/integrations/sanity/components/use-studio-preview-urls";
|
|
2
4
|
import {
|
|
5
|
+
blockPreviewKey,
|
|
3
6
|
getVisiblePageBuilderInsertMenuGroups,
|
|
4
|
-
|
|
7
|
+
getVisiblePageBuilderMemberNames,
|
|
5
8
|
type PageBuilderGroupName,
|
|
9
|
+
pageBuilderReferenceMembers,
|
|
6
10
|
} from "@/lib/integrations/sanity/page-builder-config";
|
|
7
11
|
|
|
12
|
+
const BLOCK_PREVIEW_KEYS = pageBuilderReferenceMembers.map((member) =>
|
|
13
|
+
blockPreviewKey(member.name),
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
function useInsertMenuViews() {
|
|
17
|
+
const previewUrls = useStudioPreviewUrls(BLOCK_PREVIEW_KEYS);
|
|
18
|
+
|
|
19
|
+
return [
|
|
20
|
+
{
|
|
21
|
+
name: "grid" as const,
|
|
22
|
+
previewImageUrl: (schemaTypeName: string) =>
|
|
23
|
+
previewUrls?.[blockPreviewKey(schemaTypeName)],
|
|
24
|
+
},
|
|
25
|
+
{ name: "list" as const },
|
|
26
|
+
];
|
|
27
|
+
}
|
|
28
|
+
|
|
8
29
|
export function PageBuilderInput(props: ArrayOfObjectsInputProps) {
|
|
9
30
|
const pageTypeValue = useFormValue(["type"]);
|
|
10
31
|
const pageType =
|
|
11
32
|
typeof pageTypeValue === "string"
|
|
12
33
|
? (pageTypeValue as PageBuilderGroupName)
|
|
13
34
|
: undefined;
|
|
14
|
-
|
|
15
|
-
const visibleMemberNames =
|
|
16
|
-
getVisiblePageBuilderReferenceMembers(pageType).map(
|
|
17
|
-
(member) => member.name,
|
|
18
|
-
),
|
|
19
|
-
);
|
|
35
|
+
const views = useInsertMenuViews();
|
|
36
|
+
const visibleMemberNames = getVisiblePageBuilderMemberNames(pageType);
|
|
20
37
|
|
|
21
38
|
return props.renderDefault({
|
|
22
39
|
...props,
|
|
40
|
+
arrayFunctions: PageBuilderInsertMenu,
|
|
23
41
|
schemaType: {
|
|
24
42
|
...props.schemaType,
|
|
25
43
|
of: props.schemaType.of.filter(
|
|
@@ -32,6 +50,7 @@ export function PageBuilderInput(props: ArrayOfObjectsInputProps) {
|
|
|
32
50
|
insertMenu: {
|
|
33
51
|
...props.schemaType.options?.insertMenu,
|
|
34
52
|
groups: getVisiblePageBuilderInsertMenuGroups(pageType),
|
|
53
|
+
views,
|
|
35
54
|
},
|
|
36
55
|
},
|
|
37
56
|
},
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import { AddIcon, SearchIcon } from "@sanity/icons";
|
|
2
|
+
import {
|
|
3
|
+
Box,
|
|
4
|
+
Button,
|
|
5
|
+
Card,
|
|
6
|
+
Dialog,
|
|
7
|
+
Flex,
|
|
8
|
+
Grid,
|
|
9
|
+
Stack,
|
|
10
|
+
Text,
|
|
11
|
+
TextInput,
|
|
12
|
+
} from "@sanity/ui";
|
|
13
|
+
import { useMemo, useState } from "react";
|
|
14
|
+
import type { ArrayInputFunctionsProps, ArraySchemaType } from "sanity";
|
|
15
|
+
|
|
16
|
+
type ArrayItem = { _key: string };
|
|
17
|
+
type MemberSchemaType = ArraySchemaType["of"][number];
|
|
18
|
+
|
|
19
|
+
type CreateItem = (memberType: MemberSchemaType) => ArrayItem;
|
|
20
|
+
|
|
21
|
+
type InsertMenuSection = {
|
|
22
|
+
name: string;
|
|
23
|
+
title: string;
|
|
24
|
+
members: MemberSchemaType[];
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const SCROLL_HEIGHT = "min(60vh, 34rem)";
|
|
28
|
+
const TILE_ASPECT_RATIO = "3 / 2";
|
|
29
|
+
|
|
30
|
+
const memberTitle = (member: MemberSchemaType) => member.title ?? member.name;
|
|
31
|
+
|
|
32
|
+
function BlockTile({
|
|
33
|
+
member,
|
|
34
|
+
previewUrl,
|
|
35
|
+
onSelect,
|
|
36
|
+
}: {
|
|
37
|
+
member: MemberSchemaType;
|
|
38
|
+
previewUrl: string | undefined;
|
|
39
|
+
onSelect: () => void;
|
|
40
|
+
}) {
|
|
41
|
+
const Icon = member.icon;
|
|
42
|
+
|
|
43
|
+
return (
|
|
44
|
+
<Card
|
|
45
|
+
as="button"
|
|
46
|
+
border
|
|
47
|
+
onClick={onSelect}
|
|
48
|
+
overflow="hidden"
|
|
49
|
+
padding={0}
|
|
50
|
+
radius={2}
|
|
51
|
+
style={{ cursor: "pointer", textAlign: "left", width: "100%" }}
|
|
52
|
+
type="button"
|
|
53
|
+
>
|
|
54
|
+
<Stack space={0}>
|
|
55
|
+
<Box
|
|
56
|
+
style={{
|
|
57
|
+
aspectRatio: TILE_ASPECT_RATIO,
|
|
58
|
+
background: "var(--card-muted-bg-color)",
|
|
59
|
+
position: "relative",
|
|
60
|
+
}}
|
|
61
|
+
>
|
|
62
|
+
{previewUrl ? (
|
|
63
|
+
// biome-ignore lint/performance/noImgElement: Studio-only CMS asset
|
|
64
|
+
<img
|
|
65
|
+
alt=""
|
|
66
|
+
src={`${previewUrl}?w=640&fit=max&auto=format`}
|
|
67
|
+
style={{
|
|
68
|
+
height: "100%",
|
|
69
|
+
inset: 0,
|
|
70
|
+
objectFit: "contain",
|
|
71
|
+
position: "absolute",
|
|
72
|
+
width: "100%",
|
|
73
|
+
}}
|
|
74
|
+
/>
|
|
75
|
+
) : (
|
|
76
|
+
<Flex
|
|
77
|
+
align="center"
|
|
78
|
+
justify="center"
|
|
79
|
+
style={{ inset: 0, position: "absolute" }}
|
|
80
|
+
>
|
|
81
|
+
{Icon ? <Icon /> : null}
|
|
82
|
+
</Flex>
|
|
83
|
+
)}
|
|
84
|
+
</Box>
|
|
85
|
+
<Box padding={2}>
|
|
86
|
+
<Text size={1} textOverflow="ellipsis" weight="medium">
|
|
87
|
+
{memberTitle(member)}
|
|
88
|
+
</Text>
|
|
89
|
+
</Box>
|
|
90
|
+
</Stack>
|
|
91
|
+
</Card>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function InsertMenuDialog({
|
|
96
|
+
sections,
|
|
97
|
+
previewImageUrl,
|
|
98
|
+
onSelect,
|
|
99
|
+
onClose,
|
|
100
|
+
}: {
|
|
101
|
+
sections: InsertMenuSection[];
|
|
102
|
+
previewImageUrl: ((schemaTypeName: string) => string | undefined) | undefined;
|
|
103
|
+
onSelect: (member: MemberSchemaType) => void;
|
|
104
|
+
onClose: () => void;
|
|
105
|
+
}) {
|
|
106
|
+
const [search, setSearch] = useState("");
|
|
107
|
+
|
|
108
|
+
const visibleSections = useMemo(() => {
|
|
109
|
+
const query = search.trim().toLowerCase();
|
|
110
|
+
if (!query) return sections;
|
|
111
|
+
return sections
|
|
112
|
+
.map((section) => ({
|
|
113
|
+
...section,
|
|
114
|
+
members: section.members.filter((member) =>
|
|
115
|
+
memberTitle(member).toLowerCase().includes(query),
|
|
116
|
+
),
|
|
117
|
+
}))
|
|
118
|
+
.filter((section) => section.members.length > 0);
|
|
119
|
+
}, [search, sections]);
|
|
120
|
+
|
|
121
|
+
return (
|
|
122
|
+
<Dialog
|
|
123
|
+
header="Components"
|
|
124
|
+
id="page-builder-insert"
|
|
125
|
+
onClose={onClose}
|
|
126
|
+
open
|
|
127
|
+
width={3}
|
|
128
|
+
>
|
|
129
|
+
<Stack padding={4} space={4}>
|
|
130
|
+
<TextInput
|
|
131
|
+
autoFocus
|
|
132
|
+
icon={SearchIcon}
|
|
133
|
+
onChange={(event) => setSearch(event.currentTarget.value)}
|
|
134
|
+
placeholder="Search components…"
|
|
135
|
+
value={search}
|
|
136
|
+
/>
|
|
137
|
+
<Box style={{ height: SCROLL_HEIGHT, overflowY: "auto" }}>
|
|
138
|
+
{visibleSections.length === 0 ? (
|
|
139
|
+
<Text muted size={1}>
|
|
140
|
+
No components match “{search}”.
|
|
141
|
+
</Text>
|
|
142
|
+
) : (
|
|
143
|
+
<Stack space={5}>
|
|
144
|
+
{visibleSections.map((section) => (
|
|
145
|
+
<Stack key={section.name} space={3}>
|
|
146
|
+
<Text muted size={1} weight="semibold">
|
|
147
|
+
{section.title}
|
|
148
|
+
</Text>
|
|
149
|
+
<Grid columns={[2, 3, 4]} gap={3}>
|
|
150
|
+
{section.members.map((member) => (
|
|
151
|
+
<BlockTile
|
|
152
|
+
key={member.name}
|
|
153
|
+
member={member}
|
|
154
|
+
onSelect={() => onSelect(member)}
|
|
155
|
+
previewUrl={previewImageUrl?.(member.name)}
|
|
156
|
+
/>
|
|
157
|
+
))}
|
|
158
|
+
</Grid>
|
|
159
|
+
</Stack>
|
|
160
|
+
))}
|
|
161
|
+
</Stack>
|
|
162
|
+
)}
|
|
163
|
+
</Box>
|
|
164
|
+
</Stack>
|
|
165
|
+
</Dialog>
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Pass as `arrayFunctions`. Reads `insertMenu.groups` and `insertMenu.views`, so
|
|
171
|
+
* a schema already configuring the built-in menu needs no extra wiring.
|
|
172
|
+
*
|
|
173
|
+
* Replaces the built-in popover, which is anchored to the array grid element and
|
|
174
|
+
* so clips on page scroll and resizes when switching tabs.
|
|
175
|
+
*/
|
|
176
|
+
export function PageBuilderInsertMenu(
|
|
177
|
+
props: ArrayInputFunctionsProps<ArrayItem, ArraySchemaType>,
|
|
178
|
+
) {
|
|
179
|
+
const { onItemAppend, onValueCreate, readOnly, schemaType } = props;
|
|
180
|
+
const [open, setOpen] = useState(false);
|
|
181
|
+
|
|
182
|
+
const insertMenu = schemaType.options?.insertMenu;
|
|
183
|
+
const members = schemaType.of;
|
|
184
|
+
|
|
185
|
+
const sections = useMemo<InsertMenuSection[]>(() => {
|
|
186
|
+
const byName = new Map(members.map((member) => [member.name, member]));
|
|
187
|
+
const groups = insertMenu?.groups;
|
|
188
|
+
if (!groups) {
|
|
189
|
+
return [{ name: "all", title: "Components", members: [...members] }];
|
|
190
|
+
}
|
|
191
|
+
return groups
|
|
192
|
+
.map((group) => ({
|
|
193
|
+
name: group.name,
|
|
194
|
+
title: group.title ?? group.name,
|
|
195
|
+
members: (group.of ?? [])
|
|
196
|
+
.map((name) => byName.get(name))
|
|
197
|
+
.filter((member) => member !== undefined),
|
|
198
|
+
}))
|
|
199
|
+
.filter((section) => section.members.length > 0);
|
|
200
|
+
}, [members, insertMenu]);
|
|
201
|
+
|
|
202
|
+
const previewImageUrl = useMemo(() => {
|
|
203
|
+
const gridView = insertMenu?.views?.find((view) => view.name === "grid");
|
|
204
|
+
return gridView && "previewImageUrl" in gridView
|
|
205
|
+
? gridView.previewImageUrl
|
|
206
|
+
: undefined;
|
|
207
|
+
}, [insertMenu]);
|
|
208
|
+
|
|
209
|
+
const insert = (member: MemberSchemaType) => {
|
|
210
|
+
onItemAppend((onValueCreate as unknown as CreateItem)(member));
|
|
211
|
+
setOpen(false);
|
|
212
|
+
};
|
|
213
|
+
|
|
214
|
+
const onlyMember = members.length === 1 ? members[0] : undefined;
|
|
215
|
+
|
|
216
|
+
if (schemaType.options?.disableActions?.includes("add")) return null;
|
|
217
|
+
|
|
218
|
+
return (
|
|
219
|
+
<>
|
|
220
|
+
<Grid>
|
|
221
|
+
<Button
|
|
222
|
+
disabled={readOnly === true}
|
|
223
|
+
fontSize={1}
|
|
224
|
+
icon={AddIcon}
|
|
225
|
+
mode="ghost"
|
|
226
|
+
onClick={() => (onlyMember ? insert(onlyMember) : setOpen(true))}
|
|
227
|
+
padding={3}
|
|
228
|
+
text={onlyMember ? "Add item" : "Add item…"}
|
|
229
|
+
/>
|
|
230
|
+
</Grid>
|
|
231
|
+
|
|
232
|
+
{open && (
|
|
233
|
+
<InsertMenuDialog
|
|
234
|
+
onClose={() => setOpen(false)}
|
|
235
|
+
onSelect={insert}
|
|
236
|
+
previewImageUrl={previewImageUrl}
|
|
237
|
+
sections={sections}
|
|
238
|
+
/>
|
|
239
|
+
)}
|
|
240
|
+
</>
|
|
241
|
+
);
|
|
242
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { useClient } from "sanity";
|
|
3
|
+
import { apiVersion } from "@/lib/integrations/sanity/env";
|
|
4
|
+
|
|
5
|
+
const PREVIEW_ASSETS_QUERY = `*[
|
|
6
|
+
_type == "studioPreview" &&
|
|
7
|
+
key in $keys &&
|
|
8
|
+
defined(preview.asset._ref)
|
|
9
|
+
] | order(_updatedAt desc) {
|
|
10
|
+
key,
|
|
11
|
+
"url": preview.asset->url
|
|
12
|
+
}`;
|
|
13
|
+
|
|
14
|
+
type PreviewDocument = {
|
|
15
|
+
key: string | null;
|
|
16
|
+
url: string | null;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type StudioPreviewUrls = Record<string, string>;
|
|
20
|
+
|
|
21
|
+
const SEPARATOR = " ";
|
|
22
|
+
|
|
23
|
+
/** `undefined` while in flight, so callers can tell loading from "no preview". */
|
|
24
|
+
export function useStudioPreviewUrls(keys: readonly string[]) {
|
|
25
|
+
const client = useClient({ apiVersion });
|
|
26
|
+
const [urls, setUrls] = useState<StudioPreviewUrls>();
|
|
27
|
+
// Keys off contents, not array identity — callers pass a freshly mapped array.
|
|
28
|
+
const keyList = keys.join(SEPARATOR);
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
if (!keyList) {
|
|
32
|
+
setUrls({});
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let cancelled = false;
|
|
37
|
+
|
|
38
|
+
client
|
|
39
|
+
.fetch<PreviewDocument[]>(PREVIEW_ASSETS_QUERY, {
|
|
40
|
+
keys: keyList.split(SEPARATOR),
|
|
41
|
+
})
|
|
42
|
+
.then((documents) => {
|
|
43
|
+
if (cancelled) return;
|
|
44
|
+
const next: StudioPreviewUrls = {};
|
|
45
|
+
for (const document of documents) {
|
|
46
|
+
if (document.key && document.url && !next[document.key]) {
|
|
47
|
+
next[document.key] = document.url;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
setUrls(next);
|
|
51
|
+
})
|
|
52
|
+
.catch(() => {
|
|
53
|
+
if (!cancelled) setUrls({});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return () => {
|
|
57
|
+
cancelled = true;
|
|
58
|
+
};
|
|
59
|
+
}, [client, keyList]);
|
|
60
|
+
|
|
61
|
+
return urls;
|
|
62
|
+
}
|