@sanity/assist 4.3.0 → 4.3.2
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 +173 -163
- package/dist/index.d.mts +17 -2
- package/dist/index.d.ts +17 -2
- package/dist/index.esm.js +10 -5
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +10 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +10 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/fieldActions/customFieldActions.tsx +26 -10
- package/src/helpers/misc.ts +4 -0
- package/src/helpers/typeUtils.ts +6 -0
- package/src/index.ts +2 -0
- package/src/plugin.tsx +4 -1
package/package.json
CHANGED
|
@@ -20,6 +20,7 @@ import {
|
|
|
20
20
|
instructionTaskTypeName,
|
|
21
21
|
} from '../types'
|
|
22
22
|
import {randomKey} from '../_lib/randomKey'
|
|
23
|
+
import {isDefined} from '../helpers/misc'
|
|
23
24
|
|
|
24
25
|
export interface AgentActionConditionalPath {
|
|
25
26
|
path: AgentActionPath
|
|
@@ -159,7 +160,11 @@ export type AssistFieldActionGroup = Omit<
|
|
|
159
160
|
DocumentFieldActionGroup,
|
|
160
161
|
'renderAsButton' | 'expanded' | 'children'
|
|
161
162
|
> & {
|
|
162
|
-
|
|
163
|
+
/**
|
|
164
|
+
* `children` can include undefined entries in the action array. These will be filtered out.
|
|
165
|
+
* If the group has no defined children, the group will also be filtered out.
|
|
166
|
+
*/
|
|
167
|
+
children: (AssistFieldActionNode | undefined)[]
|
|
163
168
|
}
|
|
164
169
|
|
|
165
170
|
type PushToast = (params: ToastParams) => string
|
|
@@ -206,14 +211,17 @@ export function useCustomFieldActions(
|
|
|
206
211
|
|
|
207
212
|
return useMemo(() => {
|
|
208
213
|
const title = fieldActions?.title
|
|
209
|
-
const customActions = configActions
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
214
|
+
const customActions = configActions
|
|
215
|
+
?.filter(isDefined)
|
|
216
|
+
.map((node) => {
|
|
217
|
+
return createSafeNode({
|
|
218
|
+
node,
|
|
219
|
+
pushToast,
|
|
220
|
+
addSyntheticTask,
|
|
221
|
+
removeSyntheticTask,
|
|
222
|
+
})
|
|
215
223
|
})
|
|
216
|
-
|
|
224
|
+
.filter(isDefined)
|
|
217
225
|
const onlyGroups =
|
|
218
226
|
customActions?.length && customActions?.every((node) => node.type === 'group')
|
|
219
227
|
const groups = customActions?.length
|
|
@@ -237,17 +245,25 @@ function createSafeNode(args: {
|
|
|
237
245
|
pushToast: PushToast
|
|
238
246
|
addSyntheticTask: (task: InstructionTask) => void
|
|
239
247
|
removeSyntheticTask: (task: InstructionTask) => void
|
|
240
|
-
}): DocumentFieldActionNode {
|
|
248
|
+
}): DocumentFieldActionNode | undefined {
|
|
241
249
|
const {node} = args
|
|
242
250
|
switch (node.type) {
|
|
243
251
|
case 'action':
|
|
244
252
|
return createSafeAction({...args, action: node})
|
|
245
253
|
case 'group':
|
|
254
|
+
// eslint-disable-next-line no-case-declarations
|
|
255
|
+
const children = node.children
|
|
256
|
+
?.filter(isDefined)
|
|
257
|
+
.map((child) => createSafeNode({...args, node: child}))
|
|
258
|
+
.filter(isDefined)
|
|
259
|
+
if (!children?.length) {
|
|
260
|
+
return undefined
|
|
261
|
+
}
|
|
246
262
|
return {
|
|
247
263
|
...node,
|
|
248
264
|
renderAsButton: false,
|
|
249
265
|
expanded: true,
|
|
250
|
-
children
|
|
266
|
+
children,
|
|
251
267
|
}
|
|
252
268
|
case 'divider':
|
|
253
269
|
default:
|
package/src/helpers/misc.ts
CHANGED
|
@@ -19,3 +19,7 @@ export function getPathKey(path: Path | string) {
|
|
|
19
19
|
export function getInstructionTitle(instruction?: StudioInstruction) {
|
|
20
20
|
return instruction?.title ?? 'Untitled'
|
|
21
21
|
}
|
|
22
|
+
|
|
23
|
+
export function isDefined<T>(t: T | undefined | null): t is T {
|
|
24
|
+
return t !== undefined && t !== null
|
|
25
|
+
}
|
package/src/helpers/typeUtils.ts
CHANGED
|
@@ -4,6 +4,12 @@ export function isPortableTextArray(type: ArraySchemaType) {
|
|
|
4
4
|
return type.of.find((t) => isType(t, 'block'))
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* Returns true if the `schemaType` or any of its parent types (`schemaType.type`)` has `name` equal
|
|
9
|
+
* to `typeName`.
|
|
10
|
+
*
|
|
11
|
+
* Useful for checking if `schemaType` is a type alias of `ìmage`, `code` or similar.
|
|
12
|
+
*/
|
|
7
13
|
export function isType(schemaType: SchemaType, typeName: string): boolean {
|
|
8
14
|
if (schemaType.name === typeName) {
|
|
9
15
|
return true
|
package/src/index.ts
CHANGED
package/src/plugin.tsx
CHANGED
|
@@ -33,7 +33,10 @@ export interface AssistPluginConfig {
|
|
|
33
33
|
|
|
34
34
|
fieldActions?: {
|
|
35
35
|
title?: string
|
|
36
|
-
|
|
36
|
+
/**
|
|
37
|
+
* The returned array can include `undefined` entries in the action array. These will be filtered out.
|
|
38
|
+
*/
|
|
39
|
+
useFieldActions?: (props: AssistFieldActionProps) => (AssistFieldActionNode | undefined)[]
|
|
37
40
|
}
|
|
38
41
|
|
|
39
42
|
/**
|