sanity-plugin-singleton-management 1.0.7 → 1.1.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/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  # sanity-plugin-singleton-tools
4
4
 
5
- > This is compatible with Sanity versions 3.x, 4.x, and 5.x.
5
+ > This is compatible with Sanity versions 3.x, 4.x, 5.x, and 6.x.
6
6
 
7
7
  ## What does this plugin do?
8
8
 
package/dist/index.cjs CHANGED
@@ -1,55 +1,39 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: !0 });
3
- var sanity = require("sanity"), icons = require("@sanity/icons");
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let sanity = require("sanity"), _sanity_icons = require("@sanity/icons");
4
3
  const getSingletonDocuments = (schema) => {
5
- if (schema?._original?.types)
6
- return schema._original.types.filter(({ options }) => options?.singleton).map((s) => s.name);
7
- }, getIsSingleton = (schema, schemaType) => !schema?._original?.types || !schemaType ? !1 : schema._original.types.find(
8
- ({ name }) => name === schemaType
9
- )?.options?.singleton ?? !1, actions = (prev, { schema, schemaType }) => getIsSingleton(schema, schemaType) ? prev.filter(
10
- ({ action }) => ["publish", "discardChanges", "restore"].includes(action)
11
- ) : prev, newDocumentOptions = (prev, { schema, creationContext: { type, schemaType } }) => {
12
- const singletons = getSingletonDocuments(schema), filterSingletons = ({ templateId }) => !singletons?.includes(templateId);
13
- return type === "global" || singletons?.includes(schemaType ?? "") ? prev.filter(filterSingletons) : prev;
14
- }, singletonTools = sanity.definePlugin(() => ({
15
- name: "singleton-tools",
16
- document: {
17
- newDocumentOptions,
18
- actions
19
- }
4
+ if (schema?._original?.types) return schema._original.types.filter(({ options }) => options?.singleton).map((s) => s.name);
5
+ }, getIsSingleton = (schema, schemaType) => !schema?._original?.types || !schemaType ? !1 : schema._original.types.find(({ name }) => name === schemaType)?.options?.singleton ?? !1, actions = (prev, { schema, schemaType }) => getIsSingleton(schema, schemaType) ? prev.filter(({ action }) => [
6
+ "publish",
7
+ "discardChanges",
8
+ "restore"
9
+ ].includes(action)) : prev, newDocumentOptions = (prev, { schema, creationContext: { type, schemaType } }) => {
10
+ let singletons = getSingletonDocuments(schema), filterSingletons = ({ templateId }) => !singletons?.includes(templateId);
11
+ return type === "global" || singletons?.includes(schemaType ?? "") ? prev.filter(filterSingletons) : prev;
12
+ }, singletonTools = (0, sanity.definePlugin)(() => ({
13
+ name: "singleton-tools",
14
+ document: {
15
+ newDocumentOptions,
16
+ actions
17
+ }
20
18
  })), singletonDocumentListItem = (config) => {
21
- if (!config?.S || !config?.type || !config.context)
22
- throw new Error(
23
- "S, context, and type must be provided to singletonDocumentListItem. Example: singletonDocumentListItem({ S, context, type: 'product' })"
24
- );
25
- const { S, type, title, icon, id, context } = config, { schema } = context;
26
- if (!schema)
27
- throw new Error(
28
- "Schema is required in context for singletonDocumentListItem"
29
- );
30
- const schemaType = schema.get(type), listTitle = title ?? schemaType?.title ?? type, listIcon = icon ?? schemaType?.icon ?? icons.DocumentIcon, listId = id ?? type;
31
- return S.listItem().title(listTitle).icon(listIcon).child(S.document().schemaType(type).title(listTitle).id(listId));
19
+ if (!config?.S || !config?.type || !config.context) throw Error("S, context, and type must be provided to singletonDocumentListItem. Example: singletonDocumentListItem({ S, context, type: 'product' })");
20
+ let { S, type, title, icon, id, context } = config, { schema } = context;
21
+ if (!schema) throw Error("Schema is required in context for singletonDocumentListItem");
22
+ let schemaType = schema.get(type), listTitle = title ?? schemaType?.title ?? type, listIcon = icon ?? schemaType?.icon ?? _sanity_icons.DocumentIcon, listId = id ?? type;
23
+ return S.listItem().title(listTitle).icon(listIcon).child(S.document().schemaType(type).title(listTitle).id(listId));
32
24
  }, singletonDocumentListItems = (config) => {
33
- if (!config.S || !config.context)
34
- throw new Error(
35
- "S and context must be provided to singletonDocumentListItems. Example: singletonDocumentListItems({ S, context })"
36
- );
37
- const { S, context } = config, { schema } = context;
38
- return getSingletonDocuments(schema)?.map(
39
- (schemaType) => singletonDocumentListItem({ S, context, type: schemaType })
40
- ) || [];
25
+ if (!config.S || !config.context) throw Error("S and context must be provided to singletonDocumentListItems. Example: singletonDocumentListItems({ S, context })");
26
+ let { S, context } = config, { schema } = context;
27
+ return getSingletonDocuments(schema)?.map((schemaType) => singletonDocumentListItem({
28
+ S,
29
+ context,
30
+ type: schemaType
31
+ })) || [];
41
32
  }, filteredDocumentListItems = (config) => {
42
- if (!config.S || !config.context)
43
- throw new Error(
44
- "S and context must be provided to filteredDocumentListItems. Example: filteredDocumentListItems({ S, context })"
45
- );
46
- const { S, context } = config, { schema } = context, singletons = getSingletonDocuments(schema);
47
- return S.documentTypeListItems().filter(
48
- (type) => !singletons || !singletons.includes(type.getId())
49
- );
33
+ if (!config.S || !config.context) throw Error("S and context must be provided to filteredDocumentListItems. Example: filteredDocumentListItems({ S, context })");
34
+ let { S, context } = config, { schema } = context, singletons = getSingletonDocuments(schema);
35
+ return S.documentTypeListItems().filter((type) => !singletons || !singletons.includes(type.getId()));
50
36
  };
51
- exports.filteredDocumentListItems = filteredDocumentListItems;
52
- exports.singletonDocumentListItem = singletonDocumentListItem;
53
- exports.singletonDocumentListItems = singletonDocumentListItems;
54
- exports.singletonTools = singletonTools;
55
- //# sourceMappingURL=index.cjs.map
37
+ exports.filteredDocumentListItems = filteredDocumentListItems, exports.singletonDocumentListItem = singletonDocumentListItem, exports.singletonDocumentListItems = singletonDocumentListItems, exports.singletonTools = singletonTools;
38
+
39
+ //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/helpers/index.ts","../src/actions.ts","../src/newDocumentOptions.ts","../src/pluginConfig.ts","../src/structure/index.ts"],"sourcesContent":["import { Schema } from \"sanity\";\n\nimport { SingletonPluginOptions } from \"../types\";\n\nexport const getSingletonDocuments = (schema: Schema): string[] | undefined => {\n if (!schema?._original?.types) {\n return undefined;\n }\n\n return schema._original.types\n .filter(({ options }) => (options as SingletonPluginOptions)?.singleton)\n .map((s: { name: string }) => s.name);\n};\n\nexport const getIsSingleton = (schema: Schema, schemaType: string): boolean => {\n if (!schema?._original?.types || !schemaType) {\n return false;\n }\n\n const documentSchema = schema._original.types.find(\n ({ name }) => name === schemaType,\n );\n\n return (\n (documentSchema?.options as SingletonPluginOptions)?.singleton ?? false\n );\n};\n","import { DocumentActionsContext, DocumentActionsResolver } from \"sanity\";\n\nimport { getIsSingleton } from \"./helpers\";\n\nexport const actions: DocumentActionsResolver = (\n prev,\n { schema, schemaType }: DocumentActionsContext,\n) => {\n return getIsSingleton(schema, schemaType)\n ? prev.filter(({ action }) =>\n [\"publish\", \"discardChanges\", \"restore\"].includes(action as string),\n )\n : prev;\n};\n","import { NewDocumentOptionsContext, NewDocumentOptionsResolver } from \"sanity\";\n\nimport { getSingletonDocuments } from \"./helpers\";\n\nexport const newDocumentOptions: NewDocumentOptionsResolver = (\n prev,\n { schema, creationContext: { type, schemaType } }: NewDocumentOptionsContext,\n) => {\n const singletons = getSingletonDocuments(schema);\n\n const filterSingletons = ({ templateId }: { templateId: string }) =>\n !singletons?.includes(templateId);\n\n if (type === \"global\") return prev.filter(filterSingletons);\n\n return singletons?.includes(schemaType ?? \"\")\n ? prev.filter(filterSingletons)\n : prev;\n};\n","import { definePlugin } from \"sanity\";\n\nimport { actions } from \"./actions\";\nimport { newDocumentOptions } from \"./newDocumentOptions\";\n\nexport const singletonTools = definePlugin(() => {\n return {\n name: \"singleton-tools\",\n document: {\n newDocumentOptions,\n actions,\n },\n };\n});\n","import { DocumentIcon } from \"@sanity/icons\";\nimport type { ListItemBuilder } from \"sanity/structure\";\n\nimport { getSingletonDocuments } from \"../helpers\";\nimport {\n SingletonDocumentListItemConfig,\n SingletonPluginListItemsConfig,\n} from \"../types\";\n\nconst singletonDocumentListItem = (\n config: SingletonDocumentListItemConfig,\n): ListItemBuilder => {\n if (!config?.S || !config?.type || !config.context) {\n throw new Error(\n \"S, context, and type must be provided to singletonDocumentListItem. \" +\n \"Example: singletonDocumentListItem({ S, context, type: 'product' })\",\n );\n }\n const { S, type, title, icon, id, context } = config;\n const { schema } = context;\n\n if (!schema) {\n throw new Error(\n \"Schema is required in context for singletonDocumentListItem\",\n );\n }\n\n const schemaType = schema.get(type);\n const listTitle = title ?? schemaType?.title ?? type;\n const listIcon = icon ?? schemaType?.icon ?? DocumentIcon;\n const listId = id ?? type;\n\n return S.listItem()\n .title(listTitle)\n .icon(listIcon)\n .child(S.document().schemaType(type).title(listTitle).id(listId));\n};\n\nconst singletonDocumentListItems = (\n config: SingletonPluginListItemsConfig,\n): ListItemBuilder[] => {\n if (!config.S || !config.context) {\n throw new Error(\n \"S and context must be provided to singletonDocumentListItems. \" +\n \"Example: singletonDocumentListItems({ S, context })\",\n );\n }\n\n const { S, context } = config;\n const { schema } = context;\n\n const singletons = getSingletonDocuments(schema);\n\n return (\n singletons?.map((schemaType) =>\n singletonDocumentListItem({ S, context, type: schemaType }),\n ) || []\n );\n};\n\nconst filteredDocumentListItems = (\n config: SingletonPluginListItemsConfig,\n): ListItemBuilder[] => {\n if (!config.S || !config.context) {\n throw new Error(\n \"S and context must be provided to filteredDocumentListItems. \" +\n \"Example: filteredDocumentListItems({ S, context })\",\n );\n }\n const { S, context } = config;\n const { schema } = context;\n\n const singletons = getSingletonDocuments(schema);\n\n return S.documentTypeListItems().filter(\n (type) => !singletons || !singletons.includes(type.getId() as string),\n );\n};\n\nexport {\n filteredDocumentListItems,\n singletonDocumentListItem,\n singletonDocumentListItems,\n};\n"],"names":["definePlugin","DocumentIcon"],"mappings":";;;AAIO,MAAM,wBAAwB,CAAC,WAAyC;AAC7E,MAAK,QAAQ,WAAW;AAIxB,WAAO,OAAO,UAAU,MACrB,OAAO,CAAC,EAAE,QAAA,MAAe,SAAoC,SAAS,EACtE,IAAI,CAAC,MAAwB,EAAE,IAAI;AACxC,GAEa,iBAAiB,CAAC,QAAgB,eACzC,CAAC,QAAQ,WAAW,SAAS,CAAC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C,OAAO;AACvB,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAKJ,QAAM,EAAE,GAAG,QAAA,IAAY,QACjB,EAAE,WAAW;AAInB,SAFmB,sBAAsB,MAAM,GAGjC;AAAA,IAAI,CAAC,eACf,0BAA0B,EAAE,GAAG,SAAS,MAAM,YAAY;AAAA,EAAA,KACvD,CAAA;AAET,GAEM,4BAA4B,CAChC,WACsB;AACtB,MAAI,CAAC,OAAO,KAAK,CAAC,OAAO;AACvB,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAIJ,QAAM,EAAE,GAAG,QAAA,IAAY,QACjB,EAAE,OAAA,IAAW,SAEb,aAAa,sBAAsB,MAAM;AAE/C,SAAO,EAAE,wBAAwB;AAAA,IAC/B,CAAC,SAAS,CAAC,cAAc,CAAC,WAAW,SAAS,KAAK,MAAA,CAAiB;AAAA,EAAA;AAExE;;;;;"}
1
+ {"version":3,"file":"index.cjs","names":["definePlugin","DocumentIcon"],"sources":["../src/helpers/index.ts","../src/actions.ts","../src/newDocumentOptions.ts","../src/pluginConfig.ts","../src/structure/index.ts"],"sourcesContent":["import { Schema } from \"sanity\";\n\nimport { SingletonPluginOptions } from \"../types\";\n\nexport const getSingletonDocuments = (schema: Schema): string[] | undefined => {\n if (!schema?._original?.types) {\n return undefined;\n }\n\n return schema._original.types\n .filter(({ options }) => (options as SingletonPluginOptions)?.singleton)\n .map((s: { name: string }) => s.name);\n};\n\nexport const getIsSingleton = (schema: Schema, schemaType: string): boolean => {\n if (!schema?._original?.types || !schemaType) {\n return false;\n }\n\n const documentSchema = schema._original.types.find(\n ({ name }) => name === schemaType,\n );\n\n return (\n (documentSchema?.options as SingletonPluginOptions)?.singleton ?? false\n );\n};\n","import { DocumentActionsContext, DocumentActionsResolver } from \"sanity\";\n\nimport { getIsSingleton } from \"./helpers\";\n\nexport const actions: DocumentActionsResolver = (\n prev,\n { schema, schemaType }: DocumentActionsContext,\n) => {\n return getIsSingleton(schema, schemaType)\n ? prev.filter(({ action }) =>\n [\"publish\", \"discardChanges\", \"restore\"].includes(action as string),\n )\n : prev;\n};\n","import { NewDocumentOptionsContext, NewDocumentOptionsResolver } from \"sanity\";\n\nimport { getSingletonDocuments } from \"./helpers\";\n\nexport const newDocumentOptions: NewDocumentOptionsResolver = (\n prev,\n { schema, creationContext: { type, schemaType } }: NewDocumentOptionsContext,\n) => {\n const singletons = getSingletonDocuments(schema);\n\n const filterSingletons = ({ templateId }: { templateId: string }) =>\n !singletons?.includes(templateId);\n\n if (type === \"global\") return prev.filter(filterSingletons);\n\n return singletons?.includes(schemaType ?? \"\")\n ? prev.filter(filterSingletons)\n : prev;\n};\n","import { definePlugin } from \"sanity\";\n\nimport { actions } from \"./actions\";\nimport { newDocumentOptions } from \"./newDocumentOptions\";\n\nexport const singletonTools = definePlugin(() => {\n return {\n name: \"singleton-tools\",\n document: {\n newDocumentOptions,\n actions,\n },\n };\n});\n","import { DocumentIcon } from \"@sanity/icons\";\nimport type { ListItemBuilder } from \"sanity/structure\";\n\nimport { getSingletonDocuments } from \"../helpers\";\nimport {\n SingletonDocumentListItemConfig,\n SingletonPluginListItemsConfig,\n} from \"../types\";\n\nconst singletonDocumentListItem = (\n config: SingletonDocumentListItemConfig,\n): ListItemBuilder => {\n if (!config?.S || !config?.type || !config.context) {\n throw new Error(\n \"S, context, and type must be provided to singletonDocumentListItem. \" +\n \"Example: singletonDocumentListItem({ S, context, type: 'product' })\",\n );\n }\n const { S, type, title, icon, id, context } = config;\n const { schema } = context;\n\n if (!schema) {\n throw new Error(\n \"Schema is required in context for singletonDocumentListItem\",\n );\n }\n\n const schemaType = schema.get(type);\n const listTitle = title ?? schemaType?.title ?? type;\n const listIcon = icon ?? schemaType?.icon ?? DocumentIcon;\n const listId = id ?? type;\n\n return S.listItem()\n .title(listTitle)\n .icon(listIcon)\n .child(S.document().schemaType(type).title(listTitle).id(listId));\n};\n\nconst singletonDocumentListItems = (\n config: SingletonPluginListItemsConfig,\n): ListItemBuilder[] => {\n if (!config.S || !config.context) {\n throw new Error(\n \"S and context must be provided to singletonDocumentListItems. \" +\n \"Example: singletonDocumentListItems({ S, context })\",\n );\n }\n\n const { S, context } = config;\n const { schema } = context;\n\n const singletons = getSingletonDocuments(schema);\n\n return (\n singletons?.map((schemaType) =>\n singletonDocumentListItem({ S, context, type: schemaType }),\n ) || []\n );\n};\n\nconst filteredDocumentListItems = (\n config: SingletonPluginListItemsConfig,\n): ListItemBuilder[] => {\n if (!config.S || !config.context) {\n throw new Error(\n \"S and context must be provided to filteredDocumentListItems. \" +\n \"Example: filteredDocumentListItems({ S, context })\",\n );\n }\n const { S, context } = config;\n const { schema } = context;\n\n const singletons = getSingletonDocuments(schema);\n\n return S.documentTypeListItems().filter(\n (type) => !singletons || !singletons.includes(type.getId() as string),\n );\n};\n\nexport {\n filteredDocumentListItems,\n singletonDocumentListItem,\n singletonDocumentListItems,\n};\n"],"mappings":";;AAIA,MAAa,yBAAyB,WAAyC;CACxE,YAAQ,WAAW,OAIxB,OAAO,OAAO,UAAU,MACrB,QAAQ,EAAE,cAAe,SAAoC,SAAS,CAAC,CACvE,KAAK,MAAwB,EAAE,IAAI;AACxC,GAEa,kBAAkB,QAAgB,eACzC,CAAC,QAAQ,WAAW,SAAS,CAAC,aACzB,KAGc,OAAO,UAAU,MAAM,MAC3C,EAAE,WAAW,SAAS,UAIT,CAAC,EAAE,SAAoC,aAAa,ICpBzD,WACX,MACA,EAAE,QAAQ,iBAEH,eAAe,QAAQ,UAAU,IACpC,KAAK,QAAQ,EAAE,aACb;CAAC;CAAW;CAAkB;AAAS,CAAC,CAAC,SAAS,MAAgB,CACpE,IACA,MCRO,sBACX,MACA,EAAE,QAAQ,iBAAiB,EAAE,MAAM,mBAChC;CACH,IAAM,aAAa,sBAAsB,MAAM,GAEzC,oBAAoB,EAAE,iBAC1B,CAAC,YAAY,SAAS,UAAU;CAIlC,OAFI,SAAS,YAEN,YAAY,SAAS,cAAc,EAAE,IAFd,KAAK,OAAO,gBAAgB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Builder, StructureBuilder } from "sanity/structure";
2
+ import { ComponentType, ReactNode } from "react";
2
3
  import { ConfigContext } from "sanity";
3
- import type { ListItemBuilder } from "sanity/structure";
4
- import { Plugin as Plugin_2 } from "sanity";
5
- import { ReactNode } from "react";
6
- import { StructureBuilder } from "sanity/structure";
7
-
8
- export declare const filteredDocumentListItems: (
9
- config: SingletonPluginListItemsConfig,
10
- ) => ListItemBuilder[];
11
-
12
- export declare const singletonDocumentListItem: (
13
- config: SingletonDocumentListItemConfig,
14
- ) => ListItemBuilder;
15
-
16
- declare interface SingletonDocumentListItemConfig {
4
+ declare const singletonTools: import("sanity").Plugin<void>;
5
+ interface SingletonDocumentListItemConfig {
17
6
  S: StructureBuilder;
18
7
  context: ConfigContext;
19
8
  type: string;
@@ -21,24 +10,18 @@ declare interface SingletonDocumentListItemConfig {
21
10
  id?: string;
22
11
  icon?: ComponentType | ReactNode;
23
12
  }
24
-
25
- export declare const singletonDocumentListItems: (
26
- config: SingletonPluginListItemsConfig,
27
- ) => ListItemBuilder[];
28
-
29
- declare interface SingletonPluginListItemsConfig {
13
+ interface SingletonPluginListItemsConfig {
30
14
  S: StructureBuilder;
31
15
  context: ConfigContext;
32
16
  }
33
-
34
- export declare interface SingletonPluginOptions {
17
+ interface SingletonPluginOptions {
35
18
  singleton?: boolean;
36
19
  }
37
-
38
- export declare const singletonTools: Plugin_2<void>;
39
-
40
- export {};
41
-
42
20
  declare module "sanity" {
43
21
  interface DocumentOptions extends SingletonPluginOptions {}
44
22
  }
23
+ declare const singletonDocumentListItem: (config: SingletonDocumentListItemConfig) => ListItemBuilder;
24
+ declare const singletonDocumentListItems: (config: SingletonPluginListItemsConfig) => ListItemBuilder[];
25
+ declare const filteredDocumentListItems: (config: SingletonPluginListItemsConfig) => ListItemBuilder[];
26
+ export { type SingletonPluginOptions, filteredDocumentListItems, singletonDocumentListItem, singletonDocumentListItems, singletonTools };
27
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1,27 @@
1
+ import { ConfigContext } from "sanity";
2
+ import { ListItemBuilder, StructureBuilder } from "sanity/structure";
3
+ import { ComponentType, ReactNode } from "react";
4
+ declare const singletonTools: import("sanity").Plugin<void>;
5
+ interface SingletonDocumentListItemConfig {
6
+ S: StructureBuilder;
7
+ context: ConfigContext;
8
+ type: string;
9
+ title?: string;
10
+ id?: string;
11
+ icon?: ComponentType | ReactNode;
12
+ }
13
+ interface SingletonPluginListItemsConfig {
14
+ S: StructureBuilder;
15
+ context: ConfigContext;
16
+ }
17
+ interface SingletonPluginOptions {
18
+ singleton?: boolean;
19
+ }
20
+ declare module "sanity" {
21
+ interface DocumentOptions extends SingletonPluginOptions {}
22
+ }
23
+ declare const singletonDocumentListItem: (config: SingletonDocumentListItemConfig) => ListItemBuilder;
24
+ declare const singletonDocumentListItems: (config: SingletonPluginListItemsConfig) => ListItemBuilder[];
25
+ declare const filteredDocumentListItems: (config: SingletonPluginListItemsConfig) => ListItemBuilder[];
26
+ export { type SingletonPluginOptions, filteredDocumentListItems, singletonDocumentListItem, singletonDocumentListItems, singletonTools };
27
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,39 @@
1
+ import { definePlugin } from "sanity";
2
+ import { DocumentIcon } from "@sanity/icons";
3
+ const getSingletonDocuments = (schema) => {
4
+ if (schema?._original?.types) return schema._original.types.filter(({ options }) => options?.singleton).map((s) => s.name);
5
+ }, getIsSingleton = (schema, schemaType) => !schema?._original?.types || !schemaType ? !1 : schema._original.types.find(({ name }) => name === schemaType)?.options?.singleton ?? !1, actions = (prev, { schema, schemaType }) => getIsSingleton(schema, schemaType) ? prev.filter(({ action }) => [
6
+ "publish",
7
+ "discardChanges",
8
+ "restore"
9
+ ].includes(action)) : prev, newDocumentOptions = (prev, { schema, creationContext: { type, schemaType } }) => {
10
+ let singletons = getSingletonDocuments(schema), filterSingletons = ({ templateId }) => !singletons?.includes(templateId);
11
+ return type === "global" || singletons?.includes(schemaType ?? "") ? prev.filter(filterSingletons) : prev;
12
+ }, singletonTools = definePlugin(() => ({
13
+ name: "singleton-tools",
14
+ document: {
15
+ newDocumentOptions,
16
+ actions
17
+ }
18
+ })), singletonDocumentListItem = (config) => {
19
+ if (!config?.S || !config?.type || !config.context) throw Error("S, context, and type must be provided to singletonDocumentListItem. Example: singletonDocumentListItem({ S, context, type: 'product' })");
20
+ let { S, type, title, icon, id, context } = config, { schema } = context;
21
+ if (!schema) throw Error("Schema is required in context for singletonDocumentListItem");
22
+ let schemaType = schema.get(type), listTitle = title ?? schemaType?.title ?? type, listIcon = icon ?? schemaType?.icon ?? DocumentIcon, listId = id ?? type;
23
+ return S.listItem().title(listTitle).icon(listIcon).child(S.document().schemaType(type).title(listTitle).id(listId));
24
+ }, singletonDocumentListItems = (config) => {
25
+ if (!config.S || !config.context) throw Error("S and context must be provided to singletonDocumentListItems. Example: singletonDocumentListItems({ S, context })");
26
+ let { S, context } = config, { schema } = context;
27
+ return getSingletonDocuments(schema)?.map((schemaType) => singletonDocumentListItem({
28
+ S,
29
+ context,
30
+ type: schemaType
31
+ })) || [];
32
+ }, filteredDocumentListItems = (config) => {
33
+ if (!config.S || !config.context) throw Error("S and context must be provided to filteredDocumentListItems. Example: filteredDocumentListItems({ S, context })");
34
+ let { S, context } = config, { schema } = context, singletons = getSingletonDocuments(schema);
35
+ return S.documentTypeListItems().filter((type) => !singletons || !singletons.includes(type.getId()));
36
+ };
37
+ export { filteredDocumentListItems, singletonDocumentListItem, singletonDocumentListItems, singletonTools };
38
+
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/helpers/index.ts","../src/actions.ts","../src/newDocumentOptions.ts","../src/pluginConfig.ts","../src/structure/index.ts"],"sourcesContent":["import { Schema } from \"sanity\";\n\nimport { SingletonPluginOptions } from \"../types\";\n\nexport const getSingletonDocuments = (schema: Schema): string[] | undefined => {\n if (!schema?._original?.types) {\n return undefined;\n }\n\n return schema._original.types\n .filter(({ options }) => (options as SingletonPluginOptions)?.singleton)\n .map((s: { name: string }) => s.name);\n};\n\nexport const getIsSingleton = (schema: Schema, schemaType: string): boolean => {\n if (!schema?._original?.types || !schemaType) {\n return false;\n }\n\n const documentSchema = schema._original.types.find(\n ({ name }) => name === schemaType,\n );\n\n return (\n (documentSchema?.options as SingletonPluginOptions)?.singleton ?? false\n );\n};\n","import { DocumentActionsContext, DocumentActionsResolver } from \"sanity\";\n\nimport { getIsSingleton } from \"./helpers\";\n\nexport const actions: DocumentActionsResolver = (\n prev,\n { schema, schemaType }: DocumentActionsContext,\n) => {\n return getIsSingleton(schema, schemaType)\n ? prev.filter(({ action }) =>\n [\"publish\", \"discardChanges\", \"restore\"].includes(action as string),\n )\n : prev;\n};\n","import { NewDocumentOptionsContext, NewDocumentOptionsResolver } from \"sanity\";\n\nimport { getSingletonDocuments } from \"./helpers\";\n\nexport const newDocumentOptions: NewDocumentOptionsResolver = (\n prev,\n { schema, creationContext: { type, schemaType } }: NewDocumentOptionsContext,\n) => {\n const singletons = getSingletonDocuments(schema);\n\n const filterSingletons = ({ templateId }: { templateId: string }) =>\n !singletons?.includes(templateId);\n\n if (type === \"global\") return prev.filter(filterSingletons);\n\n return singletons?.includes(schemaType ?? \"\")\n ? prev.filter(filterSingletons)\n : prev;\n};\n","import { definePlugin } from \"sanity\";\n\nimport { actions } from \"./actions\";\nimport { newDocumentOptions } from \"./newDocumentOptions\";\n\nexport const singletonTools = definePlugin(() => {\n return {\n name: \"singleton-tools\",\n document: {\n newDocumentOptions,\n actions,\n },\n };\n});\n","import { DocumentIcon } from \"@sanity/icons\";\nimport type { ListItemBuilder } from \"sanity/structure\";\n\nimport { getSingletonDocuments } from \"../helpers\";\nimport {\n SingletonDocumentListItemConfig,\n SingletonPluginListItemsConfig,\n} from \"../types\";\n\nconst singletonDocumentListItem = (\n config: SingletonDocumentListItemConfig,\n): ListItemBuilder => {\n if (!config?.S || !config?.type || !config.context) {\n throw new Error(\n \"S, context, and type must be provided to singletonDocumentListItem. \" +\n \"Example: singletonDocumentListItem({ S, context, type: 'product' })\",\n );\n }\n const { S, type, title, icon, id, context } = config;\n const { schema } = context;\n\n if (!schema) {\n throw new Error(\n \"Schema is required in context for singletonDocumentListItem\",\n );\n }\n\n const schemaType = schema.get(type);\n const listTitle = title ?? schemaType?.title ?? type;\n const listIcon = icon ?? schemaType?.icon ?? DocumentIcon;\n const listId = id ?? type;\n\n return S.listItem()\n .title(listTitle)\n .icon(listIcon)\n .child(S.document().schemaType(type).title(listTitle).id(listId));\n};\n\nconst singletonDocumentListItems = (\n config: SingletonPluginListItemsConfig,\n): ListItemBuilder[] => {\n if (!config.S || !config.context) {\n throw new Error(\n \"S and context must be provided to singletonDocumentListItems. \" +\n \"Example: singletonDocumentListItems({ S, context })\",\n );\n }\n\n const { S, context } = config;\n const { schema } = context;\n\n const singletons = getSingletonDocuments(schema);\n\n return (\n singletons?.map((schemaType) =>\n singletonDocumentListItem({ S, context, type: schemaType }),\n ) || []\n );\n};\n\nconst filteredDocumentListItems = (\n config: SingletonPluginListItemsConfig,\n): ListItemBuilder[] => {\n if (!config.S || !config.context) {\n throw new Error(\n \"S and context must be provided to filteredDocumentListItems. \" +\n \"Example: filteredDocumentListItems({ S, context })\",\n );\n }\n const { S, context } = config;\n const { schema } = context;\n\n const singletons = getSingletonDocuments(schema);\n\n return S.documentTypeListItems().filter(\n (type) => !singletons || !singletons.includes(type.getId() as string),\n );\n};\n\nexport {\n filteredDocumentListItems,\n singletonDocumentListItem,\n singletonDocumentListItems,\n};\n"],"mappings":";;AAIA,MAAa,yBAAyB,WAAyC;CACxE,YAAQ,WAAW,OAIxB,OAAO,OAAO,UAAU,MACrB,QAAQ,EAAE,cAAe,SAAoC,SAAS,CAAC,CACvE,KAAK,MAAwB,EAAE,IAAI;AACxC,GAEa,kBAAkB,QAAgB,eACzC,CAAC,QAAQ,WAAW,SAAS,CAAC,aACzB,KAGc,OAAO,UAAU,MAAM,MAC3C,EAAE,WAAW,SAA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C,CAC5D,KAAK,CAAC;AAEV,GAEM,6BACJ,WACsB;CACtB,IAAI,CAAC,OAAO,KAAK,CAAC,OAAO,SACvB,MAAU,MACR,iHAEF;CAEF,IAAM,EAAE,GAAG,YAAY,QACjB,EAAE,WAAW,SAEb,aAAa,sBAAsB,MAAM;CAE/C,OAAO,EAAE,sBAAsB,CAAC,CAAC,QAC9B,SAAS,CAAC,cAAc,CAAC,WAAW,SAAS,KAAK,MAAM,CAAW,CACtE;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sanity-plugin-singleton-management",
3
- "version": "1.0.7",
3
+ "version": "1.1.0",
4
4
  "description": "A plugin to streamline singleton management in your Sanity Studio",
5
5
  "keywords": [
6
6
  "sanity",
@@ -14,16 +14,16 @@
14
14
  "exports": {
15
15
  ".": {
16
16
  "source": "./src/index.ts",
17
- "import": "./dist/index.esm.js",
17
+ "import": "./dist/index.js",
18
18
  "require": "./dist/index.cjs",
19
- "default": "./dist/index.esm.js"
19
+ "default": "./dist/index.js"
20
20
  },
21
21
  "./package.json": "./package.json"
22
22
  },
23
23
  "type": "module",
24
24
  "main": "./dist/index.cjs",
25
- "module": "./dist/index.esm.js",
26
- "types": "./dist/index.esm.d.ts",
25
+ "module": "./dist/index.js",
26
+ "types": "./dist/index.d.cts",
27
27
  "browserslist": "extends @sanity/browserslist-config",
28
28
  "files": [
29
29
  "dist",
@@ -49,45 +49,45 @@
49
49
  "test:coverage": "vitest run --coverage"
50
50
  },
51
51
  "dependencies": {
52
- "@sanity/icons": "^3.7.4",
52
+ "@sanity/icons": "^5.2.1",
53
53
  "@sanity/incompatible-plugin": "^1.0.5"
54
54
  },
55
55
  "devDependencies": {
56
- "@emnapi/core": "^1.9.2",
57
- "@emnapi/runtime": "^1.9.2",
58
- "@commitlint/cli": "^20.4.0",
59
- "@commitlint/config-conventional": "^20.0.0",
60
- "@commitlint/prompt-cli": "^20.3.1",
61
- "@eslint-react/eslint-plugin": "^4.2.3",
56
+ "@emnapi/core": "^1.11.3",
57
+ "@emnapi/runtime": "^1.11.3",
58
+ "@commitlint/cli": "^21.2.2",
59
+ "@commitlint/config-conventional": "^21.2.2",
60
+ "@commitlint/prompt-cli": "^21.2.2",
61
+ "@eslint-react/eslint-plugin": "^5.18.6",
62
62
  "@eslint/js": "^10.0.1",
63
63
  "@sanity/browserslist-config": "^1.0.5",
64
- "@sanity/pkg-utils": "^10.4.1",
65
- "@sanity/plugin-kit": "^4.0.20",
66
- "@semantic-release/changelog": "^6.0.3",
67
- "@semantic-release/git": "^10.0.1",
68
- "@semantic-release/github": "^12.0.6",
64
+ "@sanity/pkg-utils": "^12.1.2",
65
+ "@sanity/plugin-kit": "^10.0.5",
66
+ "@semantic-release/changelog": "^7.0.0",
67
+ "@semantic-release/git": "^11.0.1",
68
+ "@semantic-release/github": "^12.0.9",
69
69
  "@semantic-release/npm": "^13.1.5",
70
- "@testing-library/jest-dom": "^6.4.6",
71
- "@types/react": "^19.2.9",
72
- "@vitest/coverage-v8": "^4.1.2",
73
- "@vitest/ui": "^4.1.2",
74
- "eslint": "^10.2.0",
70
+ "@testing-library/jest-dom": "^7.0.1",
71
+ "@types/react": "^19.2.18",
72
+ "@vitest/coverage-v8": "^4.1.10",
73
+ "@vitest/ui": "^4.1.10",
74
+ "eslint": "^10.8.1",
75
75
  "eslint-config-prettier": "^10.1.8",
76
- "eslint-plugin-react-hooks": "7.1.0-canary-ed69815c-20260323",
76
+ "eslint-plugin-react-hooks": "7.1.1",
77
77
  "husky": "^9.1.7",
78
78
  "npm-run-all": "^4.1.5",
79
- "prettier": "^3.2.5",
80
- "prettier-plugin-packagejson": "^3.0.0",
81
- "react": "^19.2.3",
82
- "react-dom": "^19.2.4",
83
- "react-is": "^19.2.3",
84
- "rimraf": "^6.0.1",
85
- "sanity": "^5.19.0",
86
- "semantic-release": "^25.0.3",
87
- "styled-components": "^6.3.8",
88
- "typescript": "^6.0.2",
89
- "typescript-eslint": "^8.58.0",
90
- "vitest": "^4.1.2"
79
+ "prettier": "^3.9.6",
80
+ "prettier-plugin-packagejson": "^3.0.2",
81
+ "react": "^19.2.8",
82
+ "react-dom": "^19.2.8",
83
+ "react-is": "^19.2.8",
84
+ "rimraf": "^6.1.3",
85
+ "sanity": "^6.9.2",
86
+ "semantic-release": "^25.0.9",
87
+ "styled-components": "^6.5.3",
88
+ "typescript": "^6.0.3",
89
+ "typescript-eslint": "^8.67.0",
90
+ "vitest": "^4.1.10"
91
91
  },
92
92
  "overrides": {
93
93
  "esbuild": "0.25.0",
@@ -101,22 +101,29 @@
101
101
  "js-yaml": "^3.14.2",
102
102
  "ajv": "^8.18.0",
103
103
  "lodash": "^4.18.0",
104
- "brace-expansion": "^5.0.5",
104
+ "brace-expansion": "^5.0.9",
105
105
  "picomatch": "^4.0.4",
106
- "typescript": "^6.0.2",
106
+ "smol-toml": "^1.6.1",
107
+ "typescript": "^6.0.3",
108
+ "typeid-js": {
109
+ "uuid": "^11.1.1"
110
+ },
107
111
  "eslint": {
108
112
  "ajv": "^6.14.0"
109
113
  },
114
+ "@module-federation/dts-plugin": {
115
+ "undici": "^7.29.0"
116
+ },
110
117
  "@sanity/pkg-utils": {
111
- "typescript": "^6.0.2"
118
+ "typescript": "^6.0.3"
112
119
  }
113
120
  },
114
121
  "peerDependencies": {
115
122
  "react": "^18 || ^19.2.1",
116
- "sanity": "^3 || ^4 || ^5.0.0-0"
123
+ "sanity": "^3 || ^4 || ^5.0.0-0 || ^6"
117
124
  },
118
125
  "engines": {
119
- "node": ">=20.19.0"
126
+ "node": ">=24"
120
127
  },
121
128
  "repository": {
122
129
  "type": "git",
@@ -126,9 +133,9 @@
126
133
  "publishConfig": {
127
134
  "exports": {
128
135
  ".": {
129
- "import": "./dist/index.esm.js",
136
+ "import": "./dist/index.js",
130
137
  "require": "./dist/index.cjs",
131
- "default": "./dist/index.esm.js"
138
+ "default": "./dist/index.js"
132
139
  },
133
140
  "./package.json": "./package.json"
134
141
  }
@@ -1,44 +0,0 @@
1
- import { ComponentType } from "react";
2
- import { ConfigContext } from "sanity";
3
- import type { ListItemBuilder } from "sanity/structure";
4
- import { Plugin as Plugin_2 } from "sanity";
5
- import { ReactNode } from "react";
6
- import { StructureBuilder } from "sanity/structure";
7
-
8
- export declare const filteredDocumentListItems: (
9
- config: SingletonPluginListItemsConfig,
10
- ) => ListItemBuilder[];
11
-
12
- export declare const singletonDocumentListItem: (
13
- config: SingletonDocumentListItemConfig,
14
- ) => ListItemBuilder;
15
-
16
- declare interface SingletonDocumentListItemConfig {
17
- S: StructureBuilder;
18
- context: ConfigContext;
19
- type: string;
20
- title?: string;
21
- id?: string;
22
- icon?: ComponentType | ReactNode;
23
- }
24
-
25
- export declare const singletonDocumentListItems: (
26
- config: SingletonPluginListItemsConfig,
27
- ) => ListItemBuilder[];
28
-
29
- declare interface SingletonPluginListItemsConfig {
30
- S: StructureBuilder;
31
- context: ConfigContext;
32
- }
33
-
34
- export declare interface SingletonPluginOptions {
35
- singleton?: boolean;
36
- }
37
-
38
- export declare const singletonTools: Plugin_2<void>;
39
-
40
- export {};
41
-
42
- declare module "sanity" {
43
- interface DocumentOptions extends SingletonPluginOptions {}
44
- }
package/dist/index.esm.js DELETED
@@ -1,56 +0,0 @@
1
- import { definePlugin } from "sanity";
2
- import { DocumentIcon } from "@sanity/icons";
3
- const getSingletonDocuments = (schema) => {
4
- if (schema?._original?.types)
5
- return schema._original.types.filter(({ options }) => options?.singleton).map((s) => s.name);
6
- }, getIsSingleton = (schema, schemaType) => !schema?._original?.types || !schemaType ? !1 : schema._original.types.find(
7
- ({ name }) => name === schemaType
8
- )?.options?.singleton ?? !1, actions = (prev, { schema, schemaType }) => getIsSingleton(schema, schemaType) ? prev.filter(
9
- ({ action }) => ["publish", "discardChanges", "restore"].includes(action)
10
- ) : prev, newDocumentOptions = (prev, { schema, creationContext: { type, schemaType } }) => {
11
- const singletons = getSingletonDocuments(schema), filterSingletons = ({ templateId }) => !singletons?.includes(templateId);
12
- return type === "global" || singletons?.includes(schemaType ?? "") ? prev.filter(filterSingletons) : prev;
13
- }, singletonTools = definePlugin(() => ({
14
- name: "singleton-tools",
15
- document: {
16
- newDocumentOptions,
17
- actions
18
- }
19
- })), singletonDocumentListItem = (config) => {
20
- if (!config?.S || !config?.type || !config.context)
21
- throw new Error(
22
- "S, context, and type must be provided to singletonDocumentListItem. Example: singletonDocumentListItem({ S, context, type: 'product' })"
23
- );
24
- const { S, type, title, icon, id, context } = config, { schema } = context;
25
- if (!schema)
26
- throw new Error(
27
- "Schema is required in context for singletonDocumentListItem"
28
- );
29
- const schemaType = schema.get(type), listTitle = title ?? schemaType?.title ?? type, listIcon = icon ?? schemaType?.icon ?? DocumentIcon, listId = id ?? type;
30
- return S.listItem().title(listTitle).icon(listIcon).child(S.document().schemaType(type).title(listTitle).id(listId));
31
- }, singletonDocumentListItems = (config) => {
32
- if (!config.S || !config.context)
33
- throw new Error(
34
- "S and context must be provided to singletonDocumentListItems. Example: singletonDocumentListItems({ S, context })"
35
- );
36
- const { S, context } = config, { schema } = context;
37
- return getSingletonDocuments(schema)?.map(
38
- (schemaType) => singletonDocumentListItem({ S, context, type: schemaType })
39
- ) || [];
40
- }, filteredDocumentListItems = (config) => {
41
- if (!config.S || !config.context)
42
- throw new Error(
43
- "S and context must be provided to filteredDocumentListItems. Example: filteredDocumentListItems({ S, context })"
44
- );
45
- const { S, context } = config, { schema } = context, singletons = getSingletonDocuments(schema);
46
- return S.documentTypeListItems().filter(
47
- (type) => !singletons || !singletons.includes(type.getId())
48
- );
49
- };
50
- export {
51
- filteredDocumentListItems,
52
- singletonDocumentListItem,
53
- singletonDocumentListItems,
54
- singletonTools
55
- };
56
- //# sourceMappingURL=index.esm.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.esm.js","sources":["../src/helpers/index.ts","../src/actions.ts","../src/newDocumentOptions.ts","../src/pluginConfig.ts","../src/structure/index.ts"],"sourcesContent":["import { Schema } from \"sanity\";\n\nimport { SingletonPluginOptions } from \"../types\";\n\nexport const getSingletonDocuments = (schema: Schema): string[] | undefined => {\n if (!schema?._original?.types) {\n return undefined;\n }\n\n return schema._original.types\n .filter(({ options }) => (options as SingletonPluginOptions)?.singleton)\n .map((s: { name: string }) => s.name);\n};\n\nexport const getIsSingleton = (schema: Schema, schemaType: string): boolean => {\n if (!schema?._original?.types || !schemaType) {\n return false;\n }\n\n const documentSchema = schema._original.types.find(\n ({ name }) => name === schemaType,\n );\n\n return (\n (documentSchema?.options as SingletonPluginOptions)?.singleton ?? false\n );\n};\n","import { DocumentActionsContext, DocumentActionsResolver } from \"sanity\";\n\nimport { getIsSingleton } from \"./helpers\";\n\nexport const actions: DocumentActionsResolver = (\n prev,\n { schema, schemaType }: DocumentActionsContext,\n) => {\n return getIsSingleton(schema, schemaType)\n ? prev.filter(({ action }) =>\n [\"publish\", \"discardChanges\", \"restore\"].includes(action as string),\n )\n : prev;\n};\n","import { NewDocumentOptionsContext, NewDocumentOptionsResolver } from \"sanity\";\n\nimport { getSingletonDocuments } from \"./helpers\";\n\nexport const newDocumentOptions: NewDocumentOptionsResolver = (\n prev,\n { schema, creationContext: { type, schemaType } }: NewDocumentOptionsContext,\n) => {\n const singletons = getSingletonDocuments(schema);\n\n const filterSingletons = ({ templateId }: { templateId: string }) =>\n !singletons?.includes(templateId);\n\n if (type === \"global\") return prev.filter(filterSingletons);\n\n return singletons?.includes(schemaType ?? \"\")\n ? prev.filter(filterSingletons)\n : prev;\n};\n","import { definePlugin } from \"sanity\";\n\nimport { actions } from \"./actions\";\nimport { newDocumentOptions } from \"./newDocumentOptions\";\n\nexport const singletonTools = definePlugin(() => {\n return {\n name: \"singleton-tools\",\n document: {\n newDocumentOptions,\n actions,\n },\n };\n});\n","import { DocumentIcon } from \"@sanity/icons\";\nimport type { ListItemBuilder } from \"sanity/structure\";\n\nimport { getSingletonDocuments } from \"../helpers\";\nimport {\n SingletonDocumentListItemConfig,\n SingletonPluginListItemsConfig,\n} from \"../types\";\n\nconst singletonDocumentListItem = (\n config: SingletonDocumentListItemConfig,\n): ListItemBuilder => {\n if (!config?.S || !config?.type || !config.context) {\n throw new Error(\n \"S, context, and type must be provided to singletonDocumentListItem. \" +\n \"Example: singletonDocumentListItem({ S, context, type: 'product' })\",\n );\n }\n const { S, type, title, icon, id, context } = config;\n const { schema } = context;\n\n if (!schema) {\n throw new Error(\n \"Schema is required in context for singletonDocumentListItem\",\n );\n }\n\n const schemaType = schema.get(type);\n const listTitle = title ?? schemaType?.title ?? type;\n const listIcon = icon ?? schemaType?.icon ?? DocumentIcon;\n const listId = id ?? type;\n\n return S.listItem()\n .title(listTitle)\n .icon(listIcon)\n .child(S.document().schemaType(type).title(listTitle).id(listId));\n};\n\nconst singletonDocumentListItems = (\n config: SingletonPluginListItemsConfig,\n): ListItemBuilder[] => {\n if (!config.S || !config.context) {\n throw new Error(\n \"S and context must be provided to singletonDocumentListItems. \" +\n \"Example: singletonDocumentListItems({ S, context })\",\n );\n }\n\n const { S, context } = config;\n const { schema } = context;\n\n const singletons = getSingletonDocuments(schema);\n\n return (\n singletons?.map((schemaType) =>\n singletonDocumentListItem({ S, context, type: schemaType }),\n ) || []\n );\n};\n\nconst filteredDocumentListItems = (\n config: SingletonPluginListItemsConfig,\n): ListItemBuilder[] => {\n if (!config.S || !config.context) {\n throw new Error(\n \"S and context must be provided to filteredDocumentListItems. \" +\n \"Example: filteredDocumentListItems({ S, context })\",\n );\n }\n const { S, context } = config;\n const { schema } = context;\n\n const singletons = getSingletonDocuments(schema);\n\n return S.documentTypeListItems().filter(\n (type) => !singletons || !singletons.includes(type.getId() as string),\n );\n};\n\nexport {\n filteredDocumentListItems,\n singletonDocumentListItem,\n singletonDocumentListItems,\n};\n"],"names":[],"mappings":";;AAIO,MAAM,wBAAwB,CAAC,WAAyC;AAC7E,MAAK,QAAQ,WAAW;AAIxB,WAAO,OAAO,UAAU,MACrB,OAAO,CAAC,EAAE,QAAA,MAAe,SAAoC,SAAS,EACtE,IAAI,CAAC,MAAwB,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