sanity-plugin-studio-smartling 1.0.1 → 1.2.0
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/README.md +5 -5
- package/dist/helpers.d.ts +5 -0
- package/dist/index.d.ts +4 -4
- package/dist/sanity-plugin-studio-smartling.cjs.development.js +364 -135
- package/dist/sanity-plugin-studio-smartling.cjs.development.js.map +1 -1
- package/dist/sanity-plugin-studio-smartling.cjs.production.min.js +1 -1
- package/dist/sanity-plugin-studio-smartling.cjs.production.min.js.map +1 -1
- package/dist/sanity-plugin-studio-smartling.esm.js +362 -135
- package/dist/sanity-plugin-studio-smartling.esm.js.map +1 -1
- package/package.json +4 -4
- package/src/3rdparty-typings/sanity-parts.d.ts +1 -0
- package/src/adapter/createTask.ts +43 -56
- package/src/adapter/getTranslationTask.ts +1 -1
- package/src/helpers.ts +116 -0
- package/src/index.ts +24 -28
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { SanityDocument } from '@sanity/types'
|
|
2
|
+
import sanityClient from 'part:@sanity/base/client'
|
|
3
|
+
import { BaseDocumentMerger } from 'sanity-naive-html-serializer'
|
|
4
|
+
|
|
5
|
+
const client = sanityClient.withConfig({ apiVersion: '2021-03-25' })
|
|
6
|
+
|
|
7
|
+
//document fetch
|
|
8
|
+
export const findLatestDraft = (documentId: string, ignoreI18n = true) => {
|
|
9
|
+
//eliminates i18n versions
|
|
10
|
+
const query = `*[_id match $id ${
|
|
11
|
+
ignoreI18n ? ' && (_id in path("drafts.*") || _id in path("*"))' : ''
|
|
12
|
+
}]`
|
|
13
|
+
const params = { id: `*${documentId}` }
|
|
14
|
+
return client
|
|
15
|
+
.fetch(query, params)
|
|
16
|
+
.then(
|
|
17
|
+
(docs: SanityDocument[]) =>
|
|
18
|
+
docs.find(doc => doc._id.includes('draft')) ?? docs[0]
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
//revision fetch
|
|
23
|
+
export const findDocumentAtRevision = async (
|
|
24
|
+
documentId: string,
|
|
25
|
+
rev: string
|
|
26
|
+
) => {
|
|
27
|
+
const dataset = client.config().dataset
|
|
28
|
+
let baseUrl = `/data/history/${dataset}/documents/${documentId}?revision=${rev}`
|
|
29
|
+
let url = client.getUrl(baseUrl)
|
|
30
|
+
let revisionDoc = await fetch(url, { credentials: 'include' })
|
|
31
|
+
.then(req => req.json())
|
|
32
|
+
.then(req => req.documents[0])
|
|
33
|
+
/* endpoint will silently give you incorrect doc
|
|
34
|
+
* if you don't request draft and the rev belongs to a draft, so check
|
|
35
|
+
*/
|
|
36
|
+
if (!revisionDoc || revisionDoc._rev !== rev) {
|
|
37
|
+
baseUrl = `/data/history/${dataset}/documents/drafts.${documentId}?revision=${rev}`
|
|
38
|
+
url = client.getUrl(baseUrl)
|
|
39
|
+
revisionDoc = await fetch(url, { credentials: 'include' })
|
|
40
|
+
.then(req => req.json())
|
|
41
|
+
.then(req => req.documents[0])
|
|
42
|
+
}
|
|
43
|
+
return revisionDoc
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//document-level patch
|
|
47
|
+
export const documentLevelPatch = async (
|
|
48
|
+
documentId: string,
|
|
49
|
+
translatedFields: SanityDocument,
|
|
50
|
+
localeId: string
|
|
51
|
+
) => {
|
|
52
|
+
let baseDoc: SanityDocument
|
|
53
|
+
if (translatedFields._rev) {
|
|
54
|
+
baseDoc = await findDocumentAtRevision(documentId, translatedFields._rev)
|
|
55
|
+
} else {
|
|
56
|
+
baseDoc = await findLatestDraft(documentId)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const merged = BaseDocumentMerger.documentLevelMerge(
|
|
60
|
+
translatedFields,
|
|
61
|
+
baseDoc
|
|
62
|
+
)
|
|
63
|
+
const targetId = `i18n.${documentId}.${localeId}`
|
|
64
|
+
const i18nDoc = await findLatestDraft(targetId, false)
|
|
65
|
+
|
|
66
|
+
if (i18nDoc) {
|
|
67
|
+
const cleanedMerge: Record<string, any> = {}
|
|
68
|
+
//don't overwrite any existing values on the i18n doc
|
|
69
|
+
Object.entries(merged).forEach(([key, value]) => {
|
|
70
|
+
if (
|
|
71
|
+
Object.keys(translatedFields).includes(key) &&
|
|
72
|
+
!['_id', '_rev', '_updatedAt'].includes(key)
|
|
73
|
+
) {
|
|
74
|
+
cleanedMerge[key] = value
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
client
|
|
78
|
+
.transaction()
|
|
79
|
+
//@ts-ignore
|
|
80
|
+
.patch(i18nDoc._id, p => p.set(cleanedMerge))
|
|
81
|
+
.commit()
|
|
82
|
+
} else {
|
|
83
|
+
merged._id = `drafts.${targetId}`
|
|
84
|
+
//account for legacy implementations of i18n plugin lang
|
|
85
|
+
if (baseDoc._lang) {
|
|
86
|
+
merged._lang = localeId
|
|
87
|
+
} else if (baseDoc.__i18n_lang) {
|
|
88
|
+
merged.__i18n_lang = localeId
|
|
89
|
+
}
|
|
90
|
+
client.create(merged)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
//field level patch
|
|
95
|
+
export const fieldLevelPatch = async (
|
|
96
|
+
documentId: string,
|
|
97
|
+
translatedFields: SanityDocument,
|
|
98
|
+
localeId: string
|
|
99
|
+
) => {
|
|
100
|
+
let baseDoc: SanityDocument
|
|
101
|
+
if (translatedFields._rev) {
|
|
102
|
+
baseDoc = await findDocumentAtRevision(documentId, translatedFields._rev)
|
|
103
|
+
} else {
|
|
104
|
+
baseDoc = await findLatestDraft(documentId)
|
|
105
|
+
}
|
|
106
|
+
const merged = BaseDocumentMerger.fieldLevelMerge(
|
|
107
|
+
translatedFields,
|
|
108
|
+
baseDoc,
|
|
109
|
+
localeId,
|
|
110
|
+
'en'
|
|
111
|
+
)
|
|
112
|
+
client
|
|
113
|
+
.patch(baseDoc._id)
|
|
114
|
+
.set(merged)
|
|
115
|
+
.commit()
|
|
116
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,48 +2,44 @@ import { TranslationsTab } from 'sanity-translations-tab'
|
|
|
2
2
|
import {
|
|
3
3
|
BaseDocumentDeserializer,
|
|
4
4
|
BaseDocumentSerializer,
|
|
5
|
-
|
|
5
|
+
BaseDocumentMerger,
|
|
6
6
|
defaultStopTypes,
|
|
7
7
|
customSerializers,
|
|
8
8
|
} from 'sanity-naive-html-serializer'
|
|
9
9
|
import { SmartlingAdapter } from './adapter'
|
|
10
|
+
import { findLatestDraft, documentLevelPatch, fieldLevelPatch } from './helpers'
|
|
11
|
+
import { SanityDocument } from '@sanity/types/dist/dts'
|
|
10
12
|
|
|
11
13
|
const defaultDocumentLevelConfig = {
|
|
12
|
-
exportForTranslation: (id: string) =>
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
),
|
|
14
|
+
exportForTranslation: async (id: string) => {
|
|
15
|
+
const doc = await findLatestDraft(id)
|
|
16
|
+
const serialized = BaseDocumentSerializer.serializeDocument(doc, 'document')
|
|
17
|
+
//needed for lookup by translation tab
|
|
18
|
+
serialized.name = id
|
|
19
|
+
return serialized
|
|
20
|
+
},
|
|
20
21
|
importTranslation: (id: string, localeId: string, document: string) => {
|
|
21
|
-
|
|
22
|
-
id,
|
|
22
|
+
const deserialized = BaseDocumentDeserializer.deserializeDocument(
|
|
23
23
|
document
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
)
|
|
24
|
+
) as SanityDocument
|
|
25
|
+
documentLevelPatch(id, deserialized, localeId)
|
|
27
26
|
},
|
|
28
27
|
adapter: SmartlingAdapter,
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
const defaultFieldLevelConfig = {
|
|
32
|
-
exportForTranslation: (id: string) =>
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
),
|
|
31
|
+
exportForTranslation: async (id: string) => {
|
|
32
|
+
const doc = await findLatestDraft(id)
|
|
33
|
+
const serialized = BaseDocumentSerializer.serializeDocument(doc, 'field')
|
|
34
|
+
//needed for lookup by translation tab
|
|
35
|
+
serialized.name = id
|
|
36
|
+
return serialized
|
|
37
|
+
},
|
|
40
38
|
importTranslation: (id: string, localeId: string, document: string) => {
|
|
41
|
-
|
|
42
|
-
id,
|
|
39
|
+
const deserialized = BaseDocumentDeserializer.deserializeDocument(
|
|
43
40
|
document
|
|
44
|
-
)
|
|
45
|
-
|
|
46
|
-
)
|
|
41
|
+
) as SanityDocument
|
|
42
|
+
fieldLevelPatch(id, deserialized, localeId)
|
|
47
43
|
},
|
|
48
44
|
adapter: SmartlingAdapter,
|
|
49
45
|
}
|
|
@@ -52,7 +48,7 @@ export {
|
|
|
52
48
|
TranslationsTab,
|
|
53
49
|
BaseDocumentDeserializer,
|
|
54
50
|
BaseDocumentSerializer,
|
|
55
|
-
|
|
51
|
+
BaseDocumentMerger,
|
|
56
52
|
defaultStopTypes,
|
|
57
53
|
customSerializers,
|
|
58
54
|
SmartlingAdapter,
|