How to change the preview for a grid tile in a schema with an array of objects.
12 replies
Last updated: Jun 4, 2021
A
hello, my schema contains an array of objects. i'm display the array with
options.layout: 'grid', but the grid doesn't seem to display the video field from my object that i'd like, it appears like attached. does anybody know how i'm able to change the preview for this grid tile/what fields to return from the preview function if any?
Jun 2, 2021, 4:15 AM
G
Hi User. You can use the
preview.select.mediaproperty to specify a field that can be used as a preview thumbnail, as they’ve done below the gotcha here .
Jun 2, 2021, 2:41 PM
A
thanks so much!
Jun 4, 2021, 12:08 AM
A
export default [ { name: "playPage", type: "document", fields: [ { name: 'title', type: 'string' }, { name: 'videos', type: 'array', of: [{ type: 'videoWithSize' }], options: { layout: 'grid', }, preview: { select: { media: 'videoWithSize.video' }, } } ] }, { name: 'videoWithSize', type: 'object', // preview: { // select: { // media: 'video', // }, // // prepare(selection) { // // console.log('selection', selection); // // return ({ media: selection.video }) // // }, // }, fields: [ { name: 'size', type: 'string' }, { name: 'video', type: 'cloudinary.asset' }, ] } ];
Jun 4, 2021, 12:25 AM
A
any idea why this isn't working?
Jun 4, 2021, 12:25 AM
A
i've tried it with the preview in the
videoWithSizeobject as well, no luck
Jun 4, 2021, 12:26 AM
A
also it's worth noting that the selected field is a pointing to a video
Jun 4, 2021, 12:31 AM
A
when i log the selection inside
videoWithSizei get
selection {media: {…}} media: access_mode: "public" bytes: 10601093 created_at: "2021-03-10T23:37:29Z" duration: 9 format: "mp4" height: 1080 metadata: [] public_id: "jimmybrings_sophiemonk_16_9_81a37ecc_8cf10b5bf4" resource_type: "video" secure_url: "<https://res.cloudinary.com/paper-moose/video/upload/v1615419449/jimmybrings_sophiemonk_16_9_81a37ecc_8cf10b5bf4.mp4>" tags: [] type: "upload" url: "<http://res.cloudinary.com/paper-moose/video/upload/v1615419449/jimmybrings_sophiemonk_16_9_81a37ecc_8cf10b5bf4.mp4>" version: 1615419449 width: 1920 _key: "wgSlAllEOSrVzx4DWr2cG" _type: "cloudinary.asset" _version: 1 __proto__: Object
Jun 4, 2021, 12:31 AM
G
it’s worth noting that the selected field is a pointing to a videoI’ve never seen someone put a video in the preview. You can use JSX in
media, and when I tried a video element it did incorporate the video (though it wouldn’t autoplay, I could right-click and watch picture-in-picture and when I paused, the preview thumbnail updated to the current frame). I’d have questions about performance though and might suggest pulling a thumbnail from each video instead.
Edit: By adding
controlsto the video element, it’s possible to play the video. 🤷♂️
media: <video src="<http://res.cloudinary.com/paper-moose/video/upload/v1615419449/jimmybrings_sophiemonk_16_9_81a37ecc_8cf10b5bf4.mp4>" autoplay controls width="300" height="200">Not supported</video>
Jun 4, 2021, 1:17 AM
A
hmm ok
Jun 4, 2021, 1:22 AM
A
so just so i understand correctly, the return value of
preparemust contain an object with a
mediaproperty which points to an image or video element is that right?
Jun 4, 2021, 1:22 AM
A
for it to display in the array grid correctly
Jun 4, 2021, 1:23 AM
G
the return value ofFor an image, I think a url would suffice. The extra steps in prepare are only needed to try and render out a video (same would go if one wanted to render out emojis, like the last code block in this section ).preparemust contain an object with amediaproperty which points to an image or video element
Jun 4, 2021, 1:27 AM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.