Skip to content

Commit 5b751e9

Browse files
SimeonGriggssnorrees
authored andcommitted
feat: richer preview component
1 parent c322eea commit 5b751e9

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/PreviewCode.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Suspense} from 'react'
22
import styled from 'styled-components'
3-
import {Box, Card} from '@sanity/ui'
3+
import {Label, Box, Card, Flex, Text} from '@sanity/ui'
44
import {CodeInputValue, CodeSchemaType} from './types'
55
import {PreviewProps} from 'sanity'
66
import {useCodeMirror} from './codemirror/useCodeMirror'
@@ -28,12 +28,31 @@ export default function PreviewCode(props: PreviewCodeProps) {
2828
return (
2929
<PreviewContainer>
3030
<Card padding={4}>
31+
{selection?.filename || selection?.language ? (
32+
<Card
33+
paddingBottom={4}
34+
marginBottom={selection.code ? 4 : 0}
35+
borderBottom={!!selection.code}
36+
>
37+
<Flex align="center" justify="flex-end">
38+
{selection?.filename ? (
39+
<Box flex={1}>
40+
<Text>
41+
<code>{selection.filename}</code>
42+
</Text>
43+
</Box>
44+
) : null}
45+
{selection?.language ? <Label muted>{selection.language}</Label> : null}
46+
</Flex>
47+
</Card>
48+
) : null}
3149
{CodeMirror && (
3250
<Suspense fallback={<Card padding={2}>Loading code preview...</Card>}>
3351
<CodeMirror
3452
readOnly
3553
editable={false}
3654
value={selection?.code || ''}
55+
highlightLines={selection?.highlightedLines || []}
3756
basicSetup={{
3857
lineNumbers: false,
3958
foldGutter: false,

src/__workshop__/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export default defineScope({
88
title: 'Props',
99
component: lazy(() => import('./props')),
1010
},
11+
{
12+
name: 'preview',
13+
title: 'Preview',
14+
component: lazy(() => import('./preview')),
15+
},
1116
{
1217
name: 'lazy',
1318
title: 'Lazy',

src/__workshop__/preview.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {Box, Container} from '@sanity/ui'
2+
import {useWorkshopSanity} from '@sanity/ui-workshop-plugin-sanity'
3+
import {isObjectSchemaType, useSchema} from 'sanity'
4+
import {DocumentFormProvider, SelectedInput} from 'sanity-extra'
5+
6+
export default function PreviewStory() {
7+
const {onPatchEvent} = useWorkshopSanity()
8+
const schema = useSchema()
9+
const schemaType = schema.get('test')
10+
11+
if (!isObjectSchemaType(schemaType)) {
12+
return <>Not an object type</>
13+
}
14+
15+
return (
16+
<DocumentFormProvider documentId="test" onPatchEvent={onPatchEvent} schemaType={schemaType}>
17+
<Container width={1}>
18+
<Box paddingX={4} paddingY={[5, 6, 7]}>
19+
<SelectedInput selectedPath={['content']} />
20+
</Box>
21+
</Container>
22+
</DocumentFormProvider>
23+
)
24+
}

test/schema.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ const testType = defineType({
1515
name: 'code',
1616
title: 'Code',
1717
},
18+
{
19+
type: 'array',
20+
name: 'content',
21+
of: [{name: 'code', type: 'code', options: {withFilename: true}}],
22+
},
1823
],
1924
})
2025

0 commit comments

Comments
 (0)