Skip to content

Commit

Permalink
Merge pull request #116 from tinloof/disable-creation-update-types
Browse files Browse the repository at this point in the history
Simplier route with target types
  • Loading branch information
stilyan-tinloof authored Jan 3, 2025
2 parents a0026a7 + 87bbbdc commit 003834d
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 91 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-meals-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tinloof/sanity-studio": patch
---

Disable creation plugin made simplier
23 changes: 2 additions & 21 deletions packages/sanity-studio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,32 +517,13 @@ export default defineConfig({
schema: {
types: schemas,
},
plugins: [disableCreation({ schemaTypes: schemas })],
});
```
```tsx
/schemas/egilnnosst / home.ts;

import { defineType } from "sanity";
export default defineType({
type: "document",
name: "home",
fields: [
{
type: "string",
name: "title",
},
],
options: {
disableCreation: true,
},
plugins: [disableCreation({ schemas: ["home", "header", "footer"] })],
});
```
### Parameters
- `schemaTypes`: array of schemas found within your Studio
- `schemas`: String array of document types
- `overrideDocumentActions`: The document actions to override, defaults to publish, discardChanges, restore
### Important notice
Expand Down
6 changes: 3 additions & 3 deletions packages/sanity-studio/src/plugins/disableCreation/actions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { DocumentActionComponent, DocumentActionsContext } from "sanity";

import { DefaultDocumentActions } from "./types";
import { getIsCreationDisabledDocument } from "./utils";

const defaultActions: DefaultDocumentActions[] = [
"publish",
Expand All @@ -12,10 +11,11 @@ const defaultActions: DefaultDocumentActions[] = [
// Disable creation actions for documents that have disableCreation set to true
export const actions = (
prev: DocumentActionComponent[],
{ schema, schemaType }: DocumentActionsContext,
{ schemaType }: DocumentActionsContext,
schemas: string[],
overrideDocumentActions?: DefaultDocumentActions[]
): DocumentActionComponent[] => {
const isCreationDisabled = getIsCreationDisabledDocument(schema, schemaType);
const isCreationDisabled = schemas.includes(schemaType);

const documentActions = overrideDocumentActions?.length
? overrideDocumentActions
Expand Down
30 changes: 6 additions & 24 deletions packages/sanity-studio/src/plugins/disableCreation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import getTemplates from "./templates";
import { DisableCreationPluginOptions } from "./types";

/**
* The `disableCreation` plugin can be used to disable creation of documents that have disableCreation option set to true.
* The `disableCreation` plugin can be used to disable creation of documents.
*
* @example
*
Expand All @@ -14,40 +14,22 @@ import { DisableCreationPluginOptions } from "./types";
* import schemas from "@/sanity/schemas";
*
* export default defineConfig({
* plugins: [disableCreation({ schemaTypes: schemas })],
* plugins: [disableCreation({ schemas: ['home', 'header', 'footer'] })],
* });
* ```
*
* ```tsx
* import {defineType} from 'sanity'
*
* export default defineType({
* type: 'document',
* name: 'home',
* fields: [
* {
* type: 'string',
* name: 'title',
* },
* ],
* options: {
* disableCreation: true,
* },
* })
* ```
*
* @param schemaTypes - The schema types found in the studio, used to filter out templates that have disableCreation set to true
* @param schemas - The schema types to be disabled from creation
* @param overrideDocumentActions - The document actions to override, defaults to publish, discardChanges, restore
*/
export const disableCreation = definePlugin<DisableCreationPluginOptions>(
({ schemaTypes, overrideDocumentActions }) => ({
({ schemas, overrideDocumentActions }) => ({
name: "tinloof-sanity-disable-creation",
document: {
actions: (prev, context) =>
actions(prev, context, overrideDocumentActions),
actions(prev, context, schemas, overrideDocumentActions),
},
schema: {
templates: (templates) => getTemplates(templates, schemaTypes),
templates: (templates) => getTemplates(templates, schemas),
},
})
);
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { Template } from "sanity";
import { SchemaTypeDefinition } from "sanity";

import { getCreationDisabledDocuments } from "./utils";

export default function getTemplates(
templates: Template[],
schemaTypes: SchemaTypeDefinition[]
schemas: string[]
): Template[] {
return templates?.filter(
(template) =>
!getCreationDisabledDocuments(schemaTypes)?.includes(template.schemaType)
(template) => !schemas?.includes(template.schemaType)
);
}
8 changes: 1 addition & 7 deletions packages/sanity-studio/src/plugins/disableCreation/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import { SchemaTypeDefinition } from "sanity";

export type DisableCreationSchemaOptions = {
disableCreation?: boolean;
};

export type DefaultDocumentActions =
| "publish"
| "discardChanges"
Expand All @@ -13,6 +7,6 @@ export type DefaultDocumentActions =
| "duplicate";

export type DisableCreationPluginOptions = {
schemaTypes: SchemaTypeDefinition[];
schemas: string[];
overrideDocumentActions?: DefaultDocumentActions[];
};
30 changes: 0 additions & 30 deletions packages/sanity-studio/src/plugins/disableCreation/utils.ts

This file was deleted.

0 comments on commit 003834d

Please sign in to comment.