diff --git a/packages/sanity/src/structure/structureTool.ts b/packages/sanity/src/structure/structureTool.ts index 0f192d78b29c..930cc8f0826e 100644 --- a/packages/sanity/src/structure/structureTool.ts +++ b/packages/sanity/src/structure/structureTool.ts @@ -106,11 +106,7 @@ export const structureTool = definePlugin((options) title: options?.title || 'Structure', icon, component: lazy(() => import('./components/structureTool')), - canHandleIntent: (intent, params) => { - if (intent === 'create') return canHandleCreateIntent(params) - if (intent === 'edit') return canHandleEditIntent(params) - return false - }, + canHandleIntent: options?.canHandleIntent || canHandleIntent, getIntentState, // Controlled by sanity/src/structure/components/structureTool/StructureTitle.tsx controlsDocumentTitle: true, @@ -125,6 +121,18 @@ export const structureTool = definePlugin((options) } }) +/** + * The default implementation of the `canHandleIntent` function for the Structure Tool. + */ +export function canHandleIntent( + intent: string, + params: Record, +): boolean | {[key: string]: boolean} { + if (intent === 'create') return canHandleCreateIntent(params) + if (intent === 'edit') return canHandleEditIntent(params) + return false +} + function canHandleCreateIntent(params: Record) { // We can't handle create intents without a `type` parameter if (!('type' in params)) { diff --git a/packages/sanity/src/structure/types.ts b/packages/sanity/src/structure/types.ts index 60a9459dbdad..1e58a2b2accf 100644 --- a/packages/sanity/src/structure/types.ts +++ b/packages/sanity/src/structure/types.ts @@ -8,6 +8,7 @@ import { type I18nTextRecord, type InitialValueTemplateItem, type LocaleSource, + type Tool, } from 'sanity' import { @@ -147,6 +148,27 @@ export interface StructureToolOptions { * The title that will be displayed for the tool. Defaults to Structure */ title?: string + /** + * Determines whether the tool can handle the given intent. + * By default, the Structure tool can handle create and edit intents for all document types. + * This function can be used to override this behavior; for example you could have multiple + * Structure tools in the same workspace, each handling intents for different document types. + * + * @example + * ```ts + * // This Structure Tool only wants to handle intents for "author" documents + * structureTool({ + * title: 'Authors', + * name: 'authors', + * canHandleIntent: (intent, params) => { + * if (params.type !== 'author') return false + * + * return canHandleIntent(intent, params) + * }, + * }) + * ``` + */ + canHandleIntent?: Tool['canHandleIntent'] } /**