Skip to content

Commit

Permalink
[base] A fix + example schema demonstrating named reference types (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoerge authored Aug 18, 2017
1 parent 5e17d86 commit 660dcf1
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 30 deletions.
4 changes: 1 addition & 3 deletions packages/@sanity/base/src/preview/createPreviewObserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ function resolveMissingHeads(value, paths) {
}

function isReference(value) {
return value._type === 'reference'
// should not happen as all references should have _type === 'reference'
|| (!('_type' in value) && ('_ref' in value))
return '_ref' in value
}

function isDocument(value) {
Expand Down
6 changes: 5 additions & 1 deletion packages/@sanity/base/src/preview/observeForPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import Observable from '@sanity/observable'

const observe = createPreviewObserver(observeWithPaths)

function is(typeName, type) {
return type.name === typeName || (type.type && is(typeName, type.type))
}

// Takes a value and its type and prepares a snapshot for it that can be passed to a preview component
export default function observeForPreview(value, type, fields) {
if (type.name === 'reference') {
if (is('reference', type)) {
// if the value is of type reference, but has no _ref property, we cannot prepare any value for the preview
// and the most sane thing to do is to return `null` for snapshot
if (!value._ref) {
Expand Down
72 changes: 47 additions & 25 deletions packages/example-studio/schemas/blocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export const blocksTest = {
name: 'blocksTest',
title: 'Blocks test',
Expand All @@ -15,9 +14,32 @@ export const blocksTest = {
type: 'array',
of: [
{type: 'image', title: 'Image'},
{type: 'reference', to: {type: 'author'}, title: 'Reference to author'},
{
type: 'reference', name: 'authorReference',
to: {type: 'author'},
title: 'Reference to author'
},
{
type: 'reference',
name: 'blogpostReference',
to: {type: 'blogpost'},
title: 'Reference to blogpost'
},
{type: 'author', title: 'Embedded author'},
{type: 'code', title: 'Code'},
{
type: 'object', title: 'Test object', name: 'testObject',
fields: [
{name: 'field1', type: 'string'}
]
},
{
type: 'object', title: 'Other test object', name: 'otherTestObject',
fields: [
{name: 'field1', type: 'string'},
{name: 'field2', type: 'string'}
]
},
{type: 'block'},
{type: 'videoEmbed', title: 'Video embed'}
]
Expand All @@ -43,27 +65,27 @@ export const blocksTest = {
title: 'Customized with block types',
type: 'array',
of: [
{type: 'image', title: 'Image'},
{type: 'author', title: 'Author'},
{type: 'image', title: 'Image'},
{type: 'author', title: 'Author'},
{
type: 'block',
styles: [
{title: 'Normal', value: 'normal'},
{title: 'H1', value: 'h1'},
{title: 'H2', value: 'h2'},
{title: 'Quote', value: 'blockquote'}
{title: 'Normal', value: 'normal'},
{title: 'H1', value: 'h1'},
{title: 'H2', value: 'h2'},
{title: 'Quote', value: 'blockquote'}
],
lists: [
{title: 'Bullet', value: 'bullet'},
{title: 'Numbered', value: 'number'}
{title: 'Bullet', value: 'bullet'},
{title: 'Numbered', value: 'number'}
],
span: {
marks: [
{title: 'Strong', value: 'strong'},
{title: 'Emphasis', value: 'em'}
{title: 'Strong', value: 'strong'},
{title: 'Emphasis', value: 'em'}
],
fields: [
{name: 'Author', title: 'Author', type: 'reference', to: {type: 'author'}}
{name: 'Author', title: 'Author', type: 'reference', to: {type: 'author'}}
]
}
}
Expand All @@ -74,33 +96,33 @@ export const blocksTest = {
title: 'Blocks deep down',
type: 'object',
fields: [
{name: 'something', title: 'Something', type: 'string'},
{name: 'something', title: 'Something', type: 'string'},
{
name: 'blocks',
type: 'array',
title: 'Blocks',
of: [
{type: 'image', title: 'Image'},
{type: 'author', title: 'Author'},
{type: 'image', title: 'Image'},
{type: 'author', title: 'Author'},
{
type: 'block',
styles: [
{title: 'Normal', value: 'normal'},
{title: 'H1', value: 'h1'},
{title: 'H2', value: 'h2'},
{title: 'Quote', value: 'blockquote'}
{title: 'Normal', value: 'normal'},
{title: 'H1', value: 'h1'},
{title: 'H2', value: 'h2'},
{title: 'Quote', value: 'blockquote'}
],
lists: [
{title: 'Bullet', value: 'bullet'},
{title: 'Numbered', value: 'number'}
{title: 'Bullet', value: 'bullet'},
{title: 'Numbered', value: 'number'}
],
span: {
marks: [
{title: 'Strong', value: 'strong'},
{title: 'Emphasis', value: 'em'}
{title: 'Strong', value: 'strong'},
{title: 'Emphasis', value: 'em'}
],
fields: [
{name: 'Author', title: 'Author', type: 'reference', to: {type: 'author'}}
{name: 'Author', title: 'Author', type: 'reference', to: {type: 'author'}}
]
}
}
Expand Down
18 changes: 17 additions & 1 deletion packages/example-studio/schemas/referenceTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ export default {
title: 'Reference Test',
fields: [
{name: 'title', type: 'string'},
{name: 'selfRef', type: 'reference', to: {type: 'referenceTest'}}
{name: 'selfRef', type: 'reference', to: {type: 'referenceTest'}},
{
name: 'arrayOfNamedReferences',
type: 'array',
of: [
{
type: 'reference',
name: 'authorReference',
to: [{type: 'author', title: 'Reference to author'}]
},
{
type: 'reference',
name: 'blogpostReference',
to: [{type: 'blogpost', title: 'Reference to blog post'}]
}
]
}
]
}

0 comments on commit 660dcf1

Please sign in to comment.