@sanity/assist 1.0.8 → 1.0.9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/assist",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "",
5
5
  "keywords": [
6
6
  "sanity",
@@ -82,7 +82,7 @@ export function getFieldRefs(
82
82
  const fields =
83
83
  field.type.jsonType === 'object' ? getFieldRefs(field.type, fieldRef, depth + 1) : []
84
84
 
85
- if (!isAssistSupported(field.type)) {
85
+ if (!isAssistSupported(field.type, true)) {
86
86
  return fields
87
87
  }
88
88
  return [fieldRef, ...fields]
@@ -28,7 +28,9 @@ function node(node: DocumentFieldActionItem | DocumentFieldActionGroup) {
28
28
  export const assistFieldActions: DocumentFieldAction = {
29
29
  name: 'sanity-assist-actions',
30
30
  useAction(props) {
31
- const assistSupported = useAssistSupported(props.path, props.schemaType)
31
+ const {schemaType} = props
32
+ const assistSupported = useAssistSupported(props.path, schemaType)
33
+
32
34
  const isDocumentLevel = props.path.length === 0
33
35
 
34
36
  const {
@@ -47,7 +49,7 @@ export const assistFieldActions: DocumentFieldAction = {
47
49
  // conditional hook _should_ be safe here since the logical path will be stable
48
50
  isDocumentLevel
49
51
  ? // eslint-disable-next-line react-hooks/rules-of-hooks
50
- useAssistDocumentContextValue(props.documentId, props.schemaType as ObjectSchemaType)
52
+ useAssistDocumentContextValue(props.documentId, schemaType as ObjectSchemaType)
51
53
  : // eslint-disable-next-line react-hooks/rules-of-hooks
52
54
  useAssistDocumentContext()
53
55
 
@@ -6,30 +6,40 @@ export function isSchemaAssistEnabled(type: SchemaType) {
6
6
  return !(type.options as AssistOptions | undefined)?.aiWritingAssistance?.exclude
7
7
  }
8
8
 
9
- export function isAssistSupported(type: SchemaType) {
9
+ export function isAssistSupported(type: SchemaType, allowReadonlyHidden = false) {
10
10
  if (!isSchemaAssistEnabled(type)) {
11
11
  return false
12
12
  }
13
13
 
14
- if (isUnsupportedType(type)) {
14
+ if (isDisabled(type, allowReadonlyHidden)) {
15
15
  return false
16
16
  }
17
17
 
18
18
  if (type.jsonType === 'array') {
19
- const unsupportedArray = type.of.every((t) => isUnsupportedType(t))
19
+ const unsupportedArray = type.of.every((t) => isDisabled(t, allowReadonlyHidden))
20
20
  return !unsupportedArray
21
21
  }
22
22
 
23
23
  if (type.jsonType === 'object') {
24
- const unsupportedObject = type.fields.every((field) => isUnsupportedType(field.type))
24
+ const unsupportedObject = type.fields.every((field) =>
25
+ isDisabled(field.type, allowReadonlyHidden)
26
+ )
25
27
  return !unsupportedObject
26
28
  }
27
29
  return true
28
30
  }
29
31
 
30
- function isUnsupportedType(type: SchemaType) {
32
+ function isDisabled(type: SchemaType, allowReadonlyHidden: boolean) {
33
+ const readonlyHidden = !!type.readOnly || !!type.hidden
31
34
  return (
32
35
  !isSchemaAssistEnabled(type) ||
36
+ isUnsupportedType(type) ||
37
+ (!allowReadonlyHidden && readonlyHidden)
38
+ )
39
+ }
40
+
41
+ function isUnsupportedType(type: SchemaType) {
42
+ return (
33
43
  type.jsonType === 'number' ||
34
44
  type.name === 'sanity.imageCrop' ||
35
45
  type.name === 'sanity.imageHotspot' ||
@@ -391,4 +391,30 @@ describe('serializeSchema', () => {
391
391
  {name: 'list', title: 'String', type: 'string', values: ['a', 'b']},
392
392
  ])
393
393
  })
394
+
395
+ test('should exclude truthy hidden and readonly', () => {
396
+ const schema = Schema.compile({
397
+ name: 'test',
398
+ types: [
399
+ {
400
+ type: 'document',
401
+ name: 'article',
402
+ fields: [
403
+ {type: 'string', name: 'title', hidden: () => true},
404
+ {type: 'some', name: 'some'},
405
+ ],
406
+ },
407
+ defineType({
408
+ type: 'object',
409
+ name: 'some',
410
+ readOnly: true,
411
+ fields: [{type: 'string', name: 'title'}],
412
+ }),
413
+ ],
414
+ })
415
+
416
+ const serializedTypes = serializeSchema(schema, {leanFormat: true})
417
+
418
+ expect(serializedTypes).toEqual([])
419
+ })
394
420
  })
@@ -29,6 +29,7 @@ export function serializeSchema(schema: Schema, options?: Options): SerializedSc
29
29
  .filter((t) => !(hiddenTypes.includes(t) || t.startsWith('sanity.')))
30
30
  .map((t) => schema.get(t))
31
31
  .filter((t): t is SchemaType => !!t)
32
+ .filter((t) => !t.hidden && !t.readOnly)
32
33
  .map((t) => getSchemaStub(t, schema, options))
33
34
  .filter((t) => {
34
35
  if ('to' in t && t.to && !t.to.length) {
@@ -106,6 +107,7 @@ function serializeFields(
106
107
  return schemaType.fields
107
108
  .filter((f) => !['sanity.imageHotspot', 'sanity.imageCrop'].includes(f.type?.name ?? ''))
108
109
  .filter((f) => isAssistSupported(f.type))
110
+ .filter((f) => !f.type.hidden && !f.type.readOnly)
109
111
  .map((field) => serializeMember(schema, field.type, field.name, options))
110
112
  }
111
113