This project uses a pattern to treat specific document types as singletons (e.g., settings or navigation). This is achieved through two configuration steps in defineConfig:
- Filtering Templates: The
schema.templates function is used to filter out singleton types from the global "New document" menu, preventing users from creating multiple instances of a singleton. - Restricting Actions: The
document.actions function filters the available actions for a document based on whether it is a singleton. Only actions defined in the singletonActions set (such as publish, discardChanges, restore, and unpublish) are permitted for singleton types.
To add a new singleton, add its schema type name to the singletonTypes Set and ensure its allowed actions are in the singletonActions Set.
const singletonActions = new Set([
"publish",
"discardChanges",
"restore",
"unpublish",
]);
const singletonTypes = new Set(["settings", "navigation"]);
// Inside defineConfig...
// ...
schema: {
types: schemaTypes,
templates: (templates) =>
templates.filter(({ schemaType }) => !singletonTypes.has(schemaType)),
},
document: {
actions: (input, context) =>
singletonTypes.has(context.schemaType)
? input.filter(({ action }) => action && singletonActions.has(action))
: input,
},
// ...