-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[desk-tool] Experimental feature: actions (#992)
- Loading branch information
Showing
9 changed files
with
147 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {difference} from 'lodash' | ||
|
||
const ACTIONS_FLAG = '__experimental_actions' | ||
|
||
const DEFAULT_ACTIONS = ['create', 'update', 'delete', 'publish'] | ||
const VALID_ACTIONS = DEFAULT_ACTIONS | ||
|
||
const readActions = schemaType => | ||
ACTIONS_FLAG in schemaType ? schemaType[ACTIONS_FLAG] : DEFAULT_ACTIONS | ||
|
||
const validateActions = (typeName, actions) => { | ||
if (!Array.isArray(actions)) { | ||
throw new Error( | ||
`The value of <type>.${ACTIONS_FLAG} should be an array with any of the actions ${VALID_ACTIONS.join( | ||
', ' | ||
)}` | ||
) | ||
} | ||
const invalid = difference(actions, VALID_ACTIONS) | ||
if (invalid.length > 0) { | ||
throw new Error( | ||
`Invalid action${ | ||
invalid.length > 1 ? 's' : '' | ||
} configured for schema type "${typeName}": ${invalid.join( | ||
', ' | ||
)}. Valid actions are: ${VALID_ACTIONS.join(', ')}` | ||
) | ||
} | ||
return actions | ||
} | ||
|
||
export const resolveEnabledActions = schemaType => | ||
validateActions(schemaType.name, readActions(schemaType)) | ||
|
||
export const isActionEnabled = (schemaType, action) => | ||
resolveEnabledActions(schemaType).includes(action) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/@sanity/structure/src/parts/documentActionUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {SchemaType} from './schema' | ||
|
||
export interface DocumentActionUtils { | ||
isActionEnabled(schema: SchemaType, action: string[]): boolean | ||
resolveEnabledActions(schema: SchemaType): string[] | ||
} | ||
|
||
// We are lazy-loading the part to work around typescript trying to resolve it | ||
export const isActionEnabled = (() => { | ||
const documentActionUtils = require('part:@sanity/base/util/document-action-utils') | ||
return (type: SchemaType, action: string) => documentActionUtils.isActionEnabled(type, action) | ||
})() | ||
|
||
export const resolveEnabledActions = (() => { | ||
const documentActionUtils = require('part:@sanity/base/util/document-action-utils') | ||
return (type: SchemaType) => documentActionUtils.resolveEnabledActions(type) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export default { | ||
name: 'actionsTest', | ||
type: 'document', | ||
title: 'Experimental actions test', | ||
// toggle these to test various combinations of ['create', 'update', 'delete', 'publish'] | ||
__experimental_actions: [], | ||
fields: [ | ||
{ | ||
name: 'title', | ||
title: 'Title', | ||
type: 'string' | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters