Skip to content

Commit d6ea940

Browse files
committed
[base] A fix + example schema demonstrating named reference types (#117)
1 parent 9d96954 commit d6ea940

File tree

4 files changed

+70
-30
lines changed

4 files changed

+70
-30
lines changed

packages/@sanity/base/src/preview/createPreviewObserver.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ function resolveMissingHeads(value, paths) {
99
}
1010

1111
function isReference(value) {
12-
return value._type === 'reference'
13-
// should not happen as all references should have _type === 'reference'
14-
|| (!('_type' in value) && ('_ref' in value))
12+
return '_ref' in value
1513
}
1614

1715
function isDocument(value) {

packages/@sanity/base/src/preview/observeForPreview.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import Observable from '@sanity/observable'
66

77
const observe = createPreviewObserver(observeWithPaths)
88

9+
function is(typeName, type) {
10+
return type.name === typeName || (type.type && is(typeName, type.type))
11+
}
12+
913
// Takes a value and its type and prepares a snapshot for it that can be passed to a preview component
1014
export default function observeForPreview(value, type, fields) {
11-
if (type.name === 'reference') {
15+
if (is('reference', type)) {
1216
// if the value is of type reference, but has no _ref property, we cannot prepare any value for the preview
1317
// and the most sane thing to do is to return `null` for snapshot
1418
if (!value._ref) {

packages/example-studio/schemas/blocks.js

+47-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
export const blocksTest = {
32
name: 'blocksTest',
43
title: 'Blocks test',
@@ -15,9 +14,32 @@ export const blocksTest = {
1514
type: 'array',
1615
of: [
1716
{type: 'image', title: 'Image'},
18-
{type: 'reference', to: {type: 'author'}, title: 'Reference to author'},
17+
{
18+
type: 'reference', name: 'authorReference',
19+
to: {type: 'author'},
20+
title: 'Reference to author'
21+
},
22+
{
23+
type: 'reference',
24+
name: 'blogpostReference',
25+
to: {type: 'blogpost'},
26+
title: 'Reference to blogpost'
27+
},
1928
{type: 'author', title: 'Embedded author'},
2029
{type: 'code', title: 'Code'},
30+
{
31+
type: 'object', title: 'Test object', name: 'testObject',
32+
fields: [
33+
{name: 'field1', type: 'string'}
34+
]
35+
},
36+
{
37+
type: 'object', title: 'Other test object', name: 'otherTestObject',
38+
fields: [
39+
{name: 'field1', type: 'string'},
40+
{name: 'field2', type: 'string'}
41+
]
42+
},
2143
{type: 'block'},
2244
{type: 'videoEmbed', title: 'Video embed'}
2345
]
@@ -43,27 +65,27 @@ export const blocksTest = {
4365
title: 'Customized with block types',
4466
type: 'array',
4567
of: [
46-
{type: 'image', title: 'Image'},
47-
{type: 'author', title: 'Author'},
68+
{type: 'image', title: 'Image'},
69+
{type: 'author', title: 'Author'},
4870
{
4971
type: 'block',
5072
styles: [
51-
{title: 'Normal', value: 'normal'},
52-
{title: 'H1', value: 'h1'},
53-
{title: 'H2', value: 'h2'},
54-
{title: 'Quote', value: 'blockquote'}
73+
{title: 'Normal', value: 'normal'},
74+
{title: 'H1', value: 'h1'},
75+
{title: 'H2', value: 'h2'},
76+
{title: 'Quote', value: 'blockquote'}
5577
],
5678
lists: [
57-
{title: 'Bullet', value: 'bullet'},
58-
{title: 'Numbered', value: 'number'}
79+
{title: 'Bullet', value: 'bullet'},
80+
{title: 'Numbered', value: 'number'}
5981
],
6082
span: {
6183
marks: [
62-
{title: 'Strong', value: 'strong'},
63-
{title: 'Emphasis', value: 'em'}
84+
{title: 'Strong', value: 'strong'},
85+
{title: 'Emphasis', value: 'em'}
6486
],
6587
fields: [
66-
{name: 'Author', title: 'Author', type: 'reference', to: {type: 'author'}}
88+
{name: 'Author', title: 'Author', type: 'reference', to: {type: 'author'}}
6789
]
6890
}
6991
}
@@ -74,33 +96,33 @@ export const blocksTest = {
7496
title: 'Blocks deep down',
7597
type: 'object',
7698
fields: [
77-
{name: 'something', title: 'Something', type: 'string'},
99+
{name: 'something', title: 'Something', type: 'string'},
78100
{
79101
name: 'blocks',
80102
type: 'array',
81103
title: 'Blocks',
82104
of: [
83-
{type: 'image', title: 'Image'},
84-
{type: 'author', title: 'Author'},
105+
{type: 'image', title: 'Image'},
106+
{type: 'author', title: 'Author'},
85107
{
86108
type: 'block',
87109
styles: [
88-
{title: 'Normal', value: 'normal'},
89-
{title: 'H1', value: 'h1'},
90-
{title: 'H2', value: 'h2'},
91-
{title: 'Quote', value: 'blockquote'}
110+
{title: 'Normal', value: 'normal'},
111+
{title: 'H1', value: 'h1'},
112+
{title: 'H2', value: 'h2'},
113+
{title: 'Quote', value: 'blockquote'}
92114
],
93115
lists: [
94-
{title: 'Bullet', value: 'bullet'},
95-
{title: 'Numbered', value: 'number'}
116+
{title: 'Bullet', value: 'bullet'},
117+
{title: 'Numbered', value: 'number'}
96118
],
97119
span: {
98120
marks: [
99-
{title: 'Strong', value: 'strong'},
100-
{title: 'Emphasis', value: 'em'}
121+
{title: 'Strong', value: 'strong'},
122+
{title: 'Emphasis', value: 'em'}
101123
],
102124
fields: [
103-
{name: 'Author', title: 'Author', type: 'reference', to: {type: 'author'}}
125+
{name: 'Author', title: 'Author', type: 'reference', to: {type: 'author'}}
104126
]
105127
}
106128
}

packages/example-studio/schemas/referenceTest.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ export default {
44
title: 'Reference Test',
55
fields: [
66
{name: 'title', type: 'string'},
7-
{name: 'selfRef', type: 'reference', to: {type: 'referenceTest'}}
7+
{name: 'selfRef', type: 'reference', to: {type: 'referenceTest'}},
8+
{
9+
name: 'arrayOfNamedReferences',
10+
type: 'array',
11+
of: [
12+
{
13+
type: 'reference',
14+
name: 'authorReference',
15+
to: [{type: 'author', title: 'Reference to author'}]
16+
},
17+
{
18+
type: 'reference',
19+
name: 'blogpostReference',
20+
to: [{type: 'blogpost', title: 'Reference to blog post'}]
21+
}
22+
]
23+
}
824
]
925
}

0 commit comments

Comments
 (0)