Skip to content

Commit

Permalink
refactor(core): use new value transfer function for copy/paste + tests (
Browse files Browse the repository at this point in the history
  • Loading branch information
skogsmaskin committed Jun 10, 2024
1 parent b285429 commit 7231f47
Show file tree
Hide file tree
Showing 7 changed files with 772 additions and 302 deletions.
11 changes: 1 addition & 10 deletions packages/sanity/src/core/studio/copyPaste/CopyPasteProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ export const CopyPasteProvider: React.FC<{
const documentMetaRef = useRef<DocumentMeta | null>(null)
const [copyResult, setCopyResult] = useState<CopyActionResult | null>(null)

const isValidTargetType = useCallback(
(target: string) => {
const source = copyResult?.schemaTypeName
return source === target
},
[copyResult],
)

const setDocumentMeta = useCallback(
({documentId, documentType, schemaType, onChange}: Required<DocumentMeta>) => {
documentMetaRef.current = {
Expand All @@ -43,9 +35,8 @@ export const CopyPasteProvider: React.FC<{
getDocumentMeta,
setCopyResult,
setDocumentMeta,
isValidTargetType,
}),
[copyResult, getDocumentMeta, setDocumentMeta, isValidTargetType],
[copyResult, getDocumentMeta, setDocumentMeta],
)

return <CopyPasteContext.Provider value={contextValue}>{children}</CopyPasteContext.Provider>
Expand Down
288 changes: 108 additions & 180 deletions packages/sanity/src/core/studio/copyPaste/__test__/schema.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {defineField, defineType, type Schema} from '@sanity/types'
import {defineType, type Schema} from '@sanity/types'

import {createSchema} from '../../../schema'

const Icon = () => null

const linkType = defineType({
type: 'object',
name: 'link',
Expand All @@ -17,242 +15,172 @@ const linkType = defineType({
validation: (Rule) => Rule.required(),
})

const myStringType = defineType({
const myStringObjectType = defineType({
type: 'object',
name: 'myStringObject',
fields: [{type: 'string', name: 'myString', validation: (Rule) => Rule.required()}],
})

const nestedObjectType = defineType({
type: 'object',
name: 'test',
fields: [{type: 'string', name: 'mystring', validation: (Rule) => Rule.required()}],
name: 'nestedObject',
fields: [
{
name: 'title',
type: 'string',
},
{
type: 'array',
name: 'objectList',
of: [{type: 'nestedObject'}],
},
],
})

export const schema = createSchema({
name: 'default',
types: [
linkType,
myStringType,
// {
// name: 'customNamedBlock',
// type: 'block',
// title: 'A named custom block',
// marks: {
// annotations: [linkType, myStringType],
// },
// of: [
// {type: 'image'},
// {
// type: 'object',
// name: 'test',
// fields: [myStringType],
// },
// {
// type: 'reference',
// name: 'strongAuthorRef',
// title: 'A strong author ref',
// to: {type: 'author'},
// },
// ],
// },
defineType({
name: 'author',
title: 'Author',
type: 'document',
//icon: Icon,
fields: [
myStringObjectType,
nestedObjectType,
{
name: 'customNamedBlock',
type: 'block',
title: 'A named custom block',
marks: {
annotations: [linkType, myStringObjectType],
},
of: [
{
name: 'name',
type: 'string',
type: 'object',
name: 'test',
fields: [myStringObjectType],
},
{
name: 'role',
type: 'string',
},
// {
// name: 'bio',
// type: 'array',
// of: [{type: 'customNamedBlock'}],
// },
defineField({
name: 'bestFriend',
type: 'reference',
to: [{type: 'author'}],
}),
],
initialValue: () => ({
role: 'Developer',
}),
}),
defineType({
name: 'address',
title: 'Address',
type: 'object',
fields: [
{
name: 'street',
type: 'string',
initialValue: 'one old street',
},
{
name: 'streetNo',
type: 'string',
initialValue: '123',
name: 'strongAuthorRef',
title: 'A strong author ref',
to: {type: 'author'},
},
],
}),
},
{
name: 'contact',
title: 'Contact',
type: 'object',
name: 'author',
title: 'Author',
type: 'document',
fields: [
{
name: 'email',
name: 'name',
type: 'string',
},
{
name: 'phone',
type: 'string',
},
],
},
{
name: 'person',
title: 'Person',
type: 'document',
icon: Icon,
fields: [
{
name: 'address',
type: 'address',
name: 'born',
type: 'number',
},
{
name: 'contact',
type: 'contact',
name: 'favoriteNumbers',
type: 'array',
of: [{type: 'number'}],
},
],
},

{
name: 'post',
title: 'Post',
type: 'document',
icon: Icon,
fields: [
{type: 'image', name: 'profileImage'},
{
name: 'title',
type: 'string',
type: 'object',
name: 'socialLinks',
fields: [
{type: 'string', name: 'twitter'},
{type: 'string', name: 'linkedin'},
],
},
// {
// name: 'body',
// type: 'array',
// of: [{type: 'customNamedBlock'}],
// },
{
name: 'author',
type: 'reference',
to: [{type: 'author'}],
name: 'nestedTest',
type: 'nestedObject',
},
],
},

{
name: 'captionedImage',
type: 'object',
fields: [
// {
// // This doesn't have a default value, so shouldn't be present,
// // not even with a `_type` stub
// name: 'asset',
// type: 'reference',
// to: [{type: 'sanity.imageAsset'}],
// },
{
name: 'caption',
type: 'string',
name: 'bio',
type: 'array',
of: [{type: 'customNamedBlock'}, {type: 'myStringObject'}],
},
],
initialValue: {caption: 'Default caption!'},
},

{
name: 'recursiveObject',
type: 'object',
fields: [
{
name: 'name',
type: 'string',
name: 'friends',
type: 'array',
of: [{type: 'reference', to: [{type: 'author'}]}],
},
{
name: 'child',
type: 'recursiveObject',
name: 'bestFriend',
type: 'reference',
to: [{type: 'author'}],
},
],
initialValue: {
name: '∞ recursion is ∞',
},
},

{
name: 'developer',
name: 'editor',
title: 'Editor',
type: 'document',
initialValue: () => ({
name: 'A default name!',

// Should clear the default value below (but ideally not actually be part
// of the value, eg no `undefined` in the resolved value)
numberOfCats: undefined,
}),
fields: [
{
name: 'name',
type: 'string',
},
{
name: 'hasPet',
type: 'boolean',
initialValue: false,
},
{
name: 'age',
name: 'born',
type: 'number',
initialValue: 30,
},
{type: 'image', name: 'profileImage'},
{
name: 'numberOfCats',
type: 'number',
initialValue: 3,
name: 'bio',
type: 'array',
of: [{type: 'customNamedBlock'}],
},
{
name: 'heroImage',
type: 'captionedImage',
name: 'favoriteNumbers',
type: 'array',
of: [{type: 'number'}],
},
{
name: 'awards',
type: 'array',
of: [{type: 'string'}],
initialValue: () => ['TypeScript Wizard of the Year'],
name: 'nestedTest',
type: 'nestedObject',
},
{
name: 'tasks',
type: 'array',
of: [
name: 'profile',
type: 'object',
fields: [
{type: 'string', name: 'email'},
{type: 'image', name: 'avatar'},
{
name: 'task',
type: 'object',
name: 'social',
fields: [
{name: 'description', type: 'string'},
{name: 'isDone', type: 'boolean', initialValue: false},
{type: 'string', name: 'twitter'},
{type: 'string', name: 'linkedin'},
],
},
],
initialValue: () => [
{
_type: 'task',
description: 'Mark as done',
isDone: false,
},
],
},
{
name: 'recursive',
type: 'recursiveObject',
// Initial value set on it's actual type
name: 'friends',
type: 'array',
of: [{type: 'reference', to: [{type: 'editor'}, {type: 'author'}]}],
},
],
},
{
name: 'post',
title: 'Post',
type: 'document',
fields: [
{
name: 'title',
type: 'string',
},
{
name: 'body',
type: 'array',
of: [{type: 'customNamedBlock'}],
},
{
name: 'author',
type: 'reference',
to: [{type: 'author'}],
},
],
},
Expand Down
Loading

0 comments on commit 7231f47

Please sign in to comment.