Skip to content

Commit

Permalink
Copy paste fields (#2435)
Browse files Browse the repository at this point in the history
* ✨ Customize studio theme, upgrade sanity for copy/paste #1045

* πŸ› Prevent document copy #1045

* πŸ› Prevent copy paste on lang #1045
  • Loading branch information
padms authored Aug 16, 2024
1 parent 59d38fd commit e6b2465
Show file tree
Hide file tree
Showing 6 changed files with 990 additions and 299 deletions.
52 changes: 52 additions & 0 deletions sanityv3/actions/fieldActions/CustomCopyFieldAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { CopyIcon } from '@sanity/icons'
import { Toast, useToast } from '@sanity/ui'
import { useCallback } from 'react'

import { defineDocumentFieldAction } from 'sanity'
import { useTranslation } from 'sanity'
import { useCopyPaste } from 'sanity'
import { useGetFormValue } from 'sanity'
import { type FormDocumentValue } from 'sanity'

export const copyAction = defineDocumentFieldAction({
name: 'copyField',
useAction({ path }) {
const getFormValue = useGetFormValue()
const { onCopy } = useCopyPaste()
const { t } = useTranslation('copy-paste')

const isDocument = path.length === 0

const documentTitle = t('copy-paste.field-action-copy-button.document.title')
const fieldTitle = t('copy-paste.field-action-copy-button.field.title')
const title = isDocument ? documentTitle : fieldTitle
const toast = useToast()

const onAction = useCallback(() => {
if (path.length === 0) {
toast.push({
title: 'Cannot copy document',
description: 'Cannot copy document. Use duplicate instead.',
})
return
} else if (path[0] === 'lang') {
toast.push({
title: 'Cannot copy language',
description: 'Copying language is not allowed.',
})
return
}
const value = getFormValue([]) as FormDocumentValue
onCopy(path, value, {
context: { source: isDocument ? 'documentFieldAction' : 'fieldAction' },
})
}, [path, isDocument, onCopy, getFormValue])

return {
type: 'action',
icon: CopyIcon,
onAction,
title,
}
},
})
6 changes: 6 additions & 0 deletions sanityv3/customStyles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/*
* To change toggle color of sanity ui
*/
[data-ui='Switch'] input:checked + * {
--switch-bg-color: #0f9d58 !important;
}
3 changes: 1 addition & 2 deletions sanityv3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"@sanity/client": "^6.15.14",
"@sanity/cross-dataset-duplicator": "^1.3.0",
"@sanity/icons": "^2.11.8",
"@sanity/scheduled-publishing": "^1.2.2",
"@sanity/ui": "^2.1.3",
"@sanity/uuid": "^3.0.2",
"@sanity/vision": "^3.37.2",
Expand All @@ -33,7 +32,7 @@
"react-dom": "^18.2.0",
"react-is": "^18.2.0",
"rxjs": "^7.8.0",
"sanity": "^3.37.2",
"sanity": "^3.52.4",
"sanity-plugin-documents-pane": "^2.1.0",
"sanity-plugin-iframe-pane": "^2.3.0",
"sanity-plugin-media": "^2.2.5",
Expand Down
Loading

0 comments on commit e6b2465

Please sign in to comment.