@sanity/document-internationalization 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/document-internationalization",
3
- "version": "1.1.1",
3
+ "version": "1.1.2",
4
4
  "description": "Provides document level translations for Sanity Studio",
5
5
  "keywords": [
6
6
  "sanity",
@@ -122,7 +122,7 @@
122
122
  "react-router-dom": "^5.0.0",
123
123
  "rxjs": "^7.0.0",
124
124
  "sanity": "^3.0.0",
125
- "styled-components": "^5.0.0"
125
+ "styled-components": "^5.2 || ^6"
126
126
  },
127
127
  "engines": {
128
128
  "node": ">=14"
@@ -3,8 +3,14 @@ import {CopyIcon} from '@sanity/icons'
3
3
  import {useDocumentOperation} from 'sanity'
4
4
  import {useToast} from '@sanity/ui'
5
5
  import {uuid} from '@sanity/uuid'
6
- import type {DocumentActionComponent, DocumentActionDescription} from 'sanity'
6
+ import type {
7
+ DocumentActionComponent,
8
+ DocumentActionDescription,
9
+ Reference,
10
+ SanityDocument,
11
+ } from 'sanity'
7
12
  import {
13
+ ApplyConfigResult,
8
14
  buildDocId,
9
15
  getBaseIdFromId,
10
16
  getLanguageFromDocument,
@@ -23,6 +29,27 @@ const DISABLED_REASON_TITLE = {
23
29
  NOTHING_TO_DUPLICATE: "This document doesn't yet exist so there's nothing to duplicate",
24
30
  }
25
31
 
32
+ function buildDupeDocument(
33
+ base: SanityDocument,
34
+ config: ApplyConfigResult,
35
+ dupeId: string,
36
+ type: string
37
+ ): SanityDocument {
38
+ const referencesField = config.fieldNames.references
39
+ const baseReferences = base[referencesField] as Reference[]
40
+ const dupeReferences = baseReferences.map((ref) => ({
41
+ ...ref,
42
+ _ref: buildDocId(config, dupeId, ref._key),
43
+ }))
44
+
45
+ return {
46
+ ...base,
47
+ [referencesField]: dupeReferences,
48
+ _id: dupeId,
49
+ _type: type,
50
+ }
51
+ }
52
+
26
53
  export function createDuplicateAction(pluginConfig: Ti18nConfig): DocumentActionComponent {
27
54
  const Action: DocumentActionComponent = ({
28
55
  id,
@@ -43,16 +70,27 @@ export function createDuplicateAction(pluginConfig: Ti18nConfig): DocumentAction
43
70
  const dupeId = uuid()
44
71
  const translations = await getTranslationsFor(client, config, baseDocumentId)
45
72
  const transaction = client.transaction()
46
- transaction.create({
47
- ...(draft ?? published),
48
- _id: dupeId,
49
- _type: type,
50
- })
73
+ const baseDoc = draft ?? published
74
+ if (!baseDoc) throw new Error('no base doc defined')
75
+
76
+ transaction.create(
77
+ buildDupeDocument(
78
+ {
79
+ ...baseDoc,
80
+ _id: dupeId,
81
+ _type: type,
82
+ },
83
+ config,
84
+ dupeId,
85
+ type
86
+ )
87
+ )
51
88
  translations.forEach((t) => {
52
89
  const isDraft = t._id.startsWith('drafts.')
53
90
  const newId = buildDocId(config, dupeId, getLanguageFromDocument(t, config))
54
91
  transaction.create({
55
92
  ...t,
93
+ [config.fieldNames.baseReference]: {_ref: dupeId, _type: 'reference', _key: uuid()},
56
94
  _id: isDraft ? `drafts.${newId}` : newId,
57
95
  })
58
96
  })