@sanity/document-internationalization 2.1.2 → 3.0.1
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/dist/index.d.mts +90 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.esm.js +768 -1166
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +753 -1176
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1048 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +34 -38
- package/src/actions/DeleteMetadataAction.tsx +5 -5
- package/src/actions/DeleteTranslationAction.tsx +6 -2
- package/src/badges/index.tsx +1 -1
- package/src/components/BulkPublish/DocumentCheck.tsx +1 -1
- package/src/components/BulkPublish/InfoIcon.tsx +4 -3
- package/src/components/BulkPublish/index.tsx +3 -2
- package/src/components/ConstrainedBox.tsx +1 -1
- package/src/components/DeleteTranslationDialog/index.tsx +1 -1
- package/src/components/DeleteTranslationDialog/separateReferences.ts +1 -1
- package/src/components/DocumentInternationalizationContext.tsx +2 -2
- package/src/components/DocumentInternationalizationMenu.tsx +3 -2
- package/src/components/LanguageManage.tsx +1 -0
- package/src/components/LanguageOption.tsx +28 -4
- package/src/components/LanguagePatch.tsx +2 -2
- package/src/components/OptimisticallyStrengthen/ReferencePatcher.tsx +2 -2
- package/src/components/OptimisticallyStrengthen/index.tsx +1 -1
- package/src/components/Warning.tsx +1 -1
- package/src/constants.ts +1 -1
- package/src/hooks/useLanguageMetadata.tsx +1 -1
- package/src/hooks/useOpenInNewPane.tsx +2 -2
- package/src/plugin.tsx +4 -4
- package/src/schema/translation/metadata.ts +2 -4
- package/src/utils/createReference.ts +1 -1
- package/src/utils/excludePaths.ts +4 -4
- package/dist/index.cjs.mjs +0 -7
package/dist/index.esm.js
CHANGED
|
@@ -1,281 +1,180 @@
|
|
|
1
|
-
import { jsx, jsxs, Fragment } from
|
|
2
|
-
import { TrashIcon, CogIcon, SplitVerticalIcon, CheckmarkIcon, AddIcon, EditIcon, TranslateIcon, InfoOutlineIcon } from
|
|
3
|
-
import { Flex, Spinner, Stack, Text, Card, Grid, Button, useToast, Tooltip, Box, Badge, useClickOutside, TextInput, Popover, Inline, Dialog } from
|
|
4
|
-
import { useMemo, useEffect, createContext, useContext, useState, useCallback } from
|
|
5
|
-
import { useSchema, Preview, useClient, useWorkspace, isDocumentSchemaType, pathToString, useEditState, useValidationStatus, TextWithTone, PatchEvent, unset, defineType, defineField, definePlugin, isSanityDocument } from
|
|
6
|
-
import { Feedback, useListeningQuery } from
|
|
7
|
-
import { uuid } from
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { Mutation, extractWithPath } from
|
|
11
|
-
import styled from
|
|
12
|
-
import { internationalizedArray } from
|
|
1
|
+
import { jsx, jsxs, Fragment } from "react/jsx-runtime";
|
|
2
|
+
import { TrashIcon, CogIcon, SplitVerticalIcon, CheckmarkIcon, AddIcon, EditIcon, TranslateIcon, InfoOutlineIcon } from "@sanity/icons";
|
|
3
|
+
import { Flex, Spinner, Stack, Text, Card, Grid, Button, useToast, Tooltip, Box, Badge, useClickOutside, TextInput, Popover, Inline, Dialog } from "@sanity/ui";
|
|
4
|
+
import { useMemo, useEffect, createContext, useContext, useState, useCallback } from "react";
|
|
5
|
+
import { useSchema, Preview, useClient, useWorkspace, isDocumentSchemaType, pathToString, useEditState, useValidationStatus, TextWithTone, PatchEvent, unset, defineType, defineField, definePlugin, isSanityDocument } from "sanity";
|
|
6
|
+
import { Feedback, useListeningQuery } from "sanity-plugin-utils";
|
|
7
|
+
import { uuid } from "@sanity/uuid";
|
|
8
|
+
import { RouterContext } from "sanity/router";
|
|
9
|
+
import { usePaneRouter, useDocumentPane } from "sanity/structure";
|
|
10
|
+
import { Mutation, extractWithPath } from "@sanity/mutator";
|
|
11
|
+
import { styled } from "styled-components";
|
|
12
|
+
import { internationalizedArray } from "sanity-plugin-internationalized-array";
|
|
13
13
|
function DocumentPreview(props) {
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
if (!schemaType) {
|
|
17
|
-
return /* @__PURE__ */jsx(Feedback, {
|
|
18
|
-
tone: "critical",
|
|
19
|
-
title: "Schema type not found"
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
return /* @__PURE__ */jsx(Preview, {
|
|
23
|
-
value: props.value,
|
|
24
|
-
schemaType
|
|
25
|
-
});
|
|
14
|
+
const schemaType = useSchema().get(props.type);
|
|
15
|
+
return schemaType ? /* @__PURE__ */ jsx(Preview, { value: props.value, schemaType }) : /* @__PURE__ */ jsx(Feedback, { tone: "critical", title: "Schema type not found" });
|
|
26
16
|
}
|
|
27
|
-
const METADATA_SCHEMA_NAME = "translation.metadata"
|
|
28
|
-
const TRANSLATIONS_ARRAY_NAME = "translations";
|
|
29
|
-
const API_VERSION = "2023-05-22";
|
|
30
|
-
const DEFAULT_CONFIG = {
|
|
17
|
+
const METADATA_SCHEMA_NAME = "translation.metadata", TRANSLATIONS_ARRAY_NAME = "translations", API_VERSION = "2023-05-22", DEFAULT_CONFIG = {
|
|
31
18
|
supportedLanguages: [],
|
|
32
19
|
schemaTypes: [],
|
|
33
20
|
languageField: "language",
|
|
34
|
-
weakReferences:
|
|
35
|
-
bulkPublish:
|
|
21
|
+
weakReferences: !1,
|
|
22
|
+
bulkPublish: !1,
|
|
36
23
|
metadataFields: [],
|
|
37
24
|
apiVersion: API_VERSION
|
|
38
25
|
};
|
|
39
|
-
function separateReferences() {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
data.forEach(doc => {
|
|
45
|
-
if (doc._type === METADATA_SCHEMA_NAME) {
|
|
46
|
-
translations.push(doc);
|
|
47
|
-
} else {
|
|
48
|
-
otherReferences.push(doc);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
return {
|
|
53
|
-
translations,
|
|
54
|
-
otherReferences
|
|
55
|
-
};
|
|
26
|
+
function separateReferences(data = []) {
|
|
27
|
+
const translations = [], otherReferences = [];
|
|
28
|
+
return data && data.length > 0 && data.forEach((doc) => {
|
|
29
|
+
doc._type === METADATA_SCHEMA_NAME ? translations.push(doc) : otherReferences.push(doc);
|
|
30
|
+
}), { translations, otherReferences };
|
|
56
31
|
}
|
|
57
32
|
function DeleteTranslationDialog(props) {
|
|
58
|
-
const {
|
|
59
|
-
|
|
60
|
-
documentId,
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
} = useListeningQuery("*[references($id)]{_id, _type}", {
|
|
67
|
-
params: {
|
|
68
|
-
id: documentId
|
|
69
|
-
},
|
|
70
|
-
initialValue: []
|
|
71
|
-
});
|
|
72
|
-
const {
|
|
73
|
-
translations,
|
|
74
|
-
otherReferences
|
|
75
|
-
} = useMemo(() => separateReferences(data), [data]);
|
|
76
|
-
useEffect(() => {
|
|
33
|
+
const { doc, documentId, setTranslations } = props, { data, loading } = useListeningQuery(
|
|
34
|
+
"*[references($id)]{_id, _type}",
|
|
35
|
+
{ params: { id: documentId }, initialValue: [] }
|
|
36
|
+
), { translations, otherReferences } = useMemo(
|
|
37
|
+
() => separateReferences(data),
|
|
38
|
+
[data]
|
|
39
|
+
);
|
|
40
|
+
return useEffect(() => {
|
|
77
41
|
setTranslations(translations);
|
|
78
|
-
}, [setTranslations, translations])
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
padding: 3,
|
|
96
|
-
children: /* @__PURE__ */jsxs(Stack, {
|
|
97
|
-
space: 4,
|
|
98
|
-
children: [/* @__PURE__ */jsx(Text, {
|
|
99
|
-
size: 1,
|
|
100
|
-
weight: "semibold",
|
|
101
|
-
children: translations && translations.length > 0 ? /* @__PURE__ */jsx(Fragment, {
|
|
102
|
-
children: "Before this document can be deleted"
|
|
103
|
-
}) : /* @__PURE__ */jsx(Fragment, {
|
|
104
|
-
children: "This document can now be deleted"
|
|
105
|
-
})
|
|
106
|
-
}), /* @__PURE__ */jsx(DocumentPreview, {
|
|
107
|
-
value: doc,
|
|
108
|
-
type: doc._type
|
|
109
|
-
}), translations && translations.length > 0 ? /* @__PURE__ */jsxs(Fragment, {
|
|
110
|
-
children: [/* @__PURE__ */jsx(Card, {
|
|
111
|
-
borderTop: true
|
|
112
|
-
}), /* @__PURE__ */jsxs(Text, {
|
|
113
|
-
size: 1,
|
|
114
|
-
weight: "semibold",
|
|
115
|
-
children: ["The reference in", " ", translations.length === 1 ? "this translations document" : "these translations documents", " ", "must be removed"]
|
|
116
|
-
}), translations.map(translation => /* @__PURE__ */jsx(DocumentPreview, {
|
|
42
|
+
}, [setTranslations, translations]), loading ? /* @__PURE__ */ jsx(Flex, { padding: 4, align: "center", justify: "center", children: /* @__PURE__ */ jsx(Spinner, {}) }) : /* @__PURE__ */ jsxs(Stack, { space: 4, children: [
|
|
43
|
+
translations && translations.length > 0 ? /* @__PURE__ */ jsx(Text, { children: "This document is a language-specific version which other translations depend on." }) : /* @__PURE__ */ jsx(Text, { children: "This document does not have connected translations." }),
|
|
44
|
+
/* @__PURE__ */ jsx(Card, { border: !0, padding: 3, children: /* @__PURE__ */ jsxs(Stack, { space: 4, children: [
|
|
45
|
+
/* @__PURE__ */ jsx(Text, { size: 1, weight: "semibold", children: translations && translations.length > 0 ? /* @__PURE__ */ jsx(Fragment, { children: "Before this document can be deleted" }) : /* @__PURE__ */ jsx(Fragment, { children: "This document can now be deleted" }) }),
|
|
46
|
+
/* @__PURE__ */ jsx(DocumentPreview, { value: doc, type: doc._type }),
|
|
47
|
+
translations && translations.length > 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
48
|
+
/* @__PURE__ */ jsx(Card, { borderTop: !0 }),
|
|
49
|
+
/* @__PURE__ */ jsxs(Text, { size: 1, weight: "semibold", children: [
|
|
50
|
+
"The reference in",
|
|
51
|
+
" ",
|
|
52
|
+
translations.length === 1 ? "this translations document" : "these translations documents",
|
|
53
|
+
" ",
|
|
54
|
+
"must be removed"
|
|
55
|
+
] }),
|
|
56
|
+
translations.map((translation) => /* @__PURE__ */ jsx(
|
|
57
|
+
DocumentPreview,
|
|
58
|
+
{
|
|
117
59
|
value: translation,
|
|
118
60
|
type: translation._type
|
|
119
|
-
},
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
61
|
+
},
|
|
62
|
+
translation._id
|
|
63
|
+
))
|
|
64
|
+
] }) : null,
|
|
65
|
+
otherReferences && otherReferences.length > 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
66
|
+
/* @__PURE__ */ jsx(Card, { borderTop: !0 }),
|
|
67
|
+
/* @__PURE__ */ jsxs(Text, { size: 1, weight: "semibold", children: [
|
|
68
|
+
otherReferences.length === 1 ? "There is an additional reference" : "There are additional references",
|
|
69
|
+
" ",
|
|
70
|
+
"to this document"
|
|
71
|
+
] }),
|
|
72
|
+
otherReferences.map((reference) => /* @__PURE__ */ jsx(
|
|
73
|
+
DocumentPreview,
|
|
74
|
+
{
|
|
128
75
|
value: reference,
|
|
129
76
|
type: reference._type
|
|
130
|
-
},
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}) : /* @__PURE__ */jsx(Text, {
|
|
136
|
-
|
|
137
|
-
})]
|
|
138
|
-
});
|
|
77
|
+
},
|
|
78
|
+
reference._id
|
|
79
|
+
))
|
|
80
|
+
] }) : null
|
|
81
|
+
] }) }),
|
|
82
|
+
otherReferences.length === 0 ? /* @__PURE__ */ jsx(Text, { children: "This document has no other references." }) : /* @__PURE__ */ jsx(Text, { children: "You may not be able to delete this document because other documents refer to it." })
|
|
83
|
+
] });
|
|
139
84
|
}
|
|
140
85
|
function DeleteTranslationFooter(props) {
|
|
141
|
-
const {
|
|
142
|
-
|
|
143
|
-
onClose,
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}), /* @__PURE__ */jsx(Button, {
|
|
154
|
-
text: translations && translations.length > 0 ? "Unset translation reference" : "Delete document",
|
|
155
|
-
onClick: onProceed,
|
|
156
|
-
tone: "critical"
|
|
157
|
-
})]
|
|
158
|
-
});
|
|
86
|
+
const { translations, onClose, onProceed } = props;
|
|
87
|
+
return /* @__PURE__ */ jsxs(Grid, { columns: 2, gap: 2, children: [
|
|
88
|
+
/* @__PURE__ */ jsx(Button, { text: "Cancel", onClick: onClose, mode: "ghost" }),
|
|
89
|
+
/* @__PURE__ */ jsx(
|
|
90
|
+
Button,
|
|
91
|
+
{
|
|
92
|
+
text: translations && translations.length > 0 ? "Unset translation reference" : "Delete document",
|
|
93
|
+
onClick: onProceed,
|
|
94
|
+
tone: "critical"
|
|
95
|
+
}
|
|
96
|
+
)
|
|
97
|
+
] });
|
|
159
98
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
if (arrA === arrB)
|
|
163
|
-
|
|
99
|
+
const isPromise = (promise) => typeof promise == "object" && typeof promise.then == "function", globalCache = [];
|
|
100
|
+
function shallowEqualArrays(arrA, arrB, equal = (a, b) => a === b) {
|
|
101
|
+
if (arrA === arrB)
|
|
102
|
+
return !0;
|
|
103
|
+
if (!arrA || !arrB)
|
|
104
|
+
return !1;
|
|
164
105
|
const len = arrA.length;
|
|
165
|
-
if (arrB.length !== len)
|
|
166
|
-
|
|
167
|
-
|
|
106
|
+
if (arrB.length !== len)
|
|
107
|
+
return !1;
|
|
108
|
+
for (let i = 0; i < len; i++)
|
|
109
|
+
if (!equal(arrA[i], arrB[i]))
|
|
110
|
+
return !1;
|
|
111
|
+
return !0;
|
|
168
112
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
if (
|
|
178
|
-
|
|
179
|
-
if (
|
|
180
|
-
|
|
181
|
-
if (Object.prototype.hasOwnProperty.call(entry, 'response')) return entry.response; // If the promise is still unresolved, throw
|
|
182
|
-
|
|
183
|
-
if (!preload) throw entry.promise;
|
|
113
|
+
function query$1(fn, keys = null, preload = !1, config = {}) {
|
|
114
|
+
keys === null && (keys = [fn]);
|
|
115
|
+
for (const entry2 of globalCache)
|
|
116
|
+
if (shallowEqualArrays(keys, entry2.keys, entry2.equal)) {
|
|
117
|
+
if (preload)
|
|
118
|
+
return;
|
|
119
|
+
if (Object.prototype.hasOwnProperty.call(entry2, "error"))
|
|
120
|
+
throw entry2.error;
|
|
121
|
+
if (Object.prototype.hasOwnProperty.call(entry2, "response"))
|
|
122
|
+
return config.lifespan && config.lifespan > 0 && (entry2.timeout && clearTimeout(entry2.timeout), entry2.timeout = setTimeout(entry2.remove, config.lifespan)), entry2.response;
|
|
123
|
+
if (!preload)
|
|
124
|
+
throw entry2.promise;
|
|
184
125
|
}
|
|
185
|
-
} // The request is new or has changed.
|
|
186
|
-
|
|
187
126
|
const entry = {
|
|
188
127
|
keys,
|
|
189
128
|
equal: config.equal,
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
}; // Register the entry
|
|
204
|
-
|
|
205
|
-
globalCache.push(entry); // And throw the promise, this yields control back to React
|
|
206
|
-
|
|
207
|
-
if (!preload) throw entry.promise;
|
|
208
|
-
return undefined;
|
|
129
|
+
remove: () => {
|
|
130
|
+
const index = globalCache.indexOf(entry);
|
|
131
|
+
index !== -1 && globalCache.splice(index, 1);
|
|
132
|
+
},
|
|
133
|
+
promise: (
|
|
134
|
+
// Execute the promise
|
|
135
|
+
(isPromise(fn) ? fn : fn(...keys)).then((response) => {
|
|
136
|
+
entry.response = response, config.lifespan && config.lifespan > 0 && (entry.timeout = setTimeout(entry.remove, config.lifespan));
|
|
137
|
+
}).catch((error) => entry.error = error)
|
|
138
|
+
)
|
|
139
|
+
};
|
|
140
|
+
if (globalCache.push(entry), !preload)
|
|
141
|
+
throw entry.promise;
|
|
209
142
|
}
|
|
210
|
-
const suspend = (fn, keys, config) => query$1(fn, keys,
|
|
211
|
-
const DocumentInternationalizationContext = createContext(DEFAULT_CONFIG);
|
|
143
|
+
const suspend = (fn, keys, config) => query$1(fn, keys, !1, config), DocumentInternationalizationContext = createContext(DEFAULT_CONFIG);
|
|
212
144
|
function useDocumentInternationalizationContext() {
|
|
213
145
|
return useContext(DocumentInternationalizationContext);
|
|
214
146
|
}
|
|
215
147
|
function DocumentInternationalizationProvider(props) {
|
|
216
|
-
const {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
suspend(async () => {
|
|
226
|
-
if (typeof pluginConfig.supportedLanguages === "function") {
|
|
227
|
-
return pluginConfig.supportedLanguages(client);
|
|
148
|
+
const { pluginConfig } = props, client = useClient({ apiVersion: pluginConfig.apiVersion }), workspace = useWorkspace(), supportedLanguages = Array.isArray(pluginConfig.supportedLanguages) ? pluginConfig.supportedLanguages : (
|
|
149
|
+
// eslint-disable-next-line require-await
|
|
150
|
+
suspend(async () => typeof pluginConfig.supportedLanguages == "function" ? pluginConfig.supportedLanguages(client) : pluginConfig.supportedLanguages, [workspace])
|
|
151
|
+
);
|
|
152
|
+
return /* @__PURE__ */ jsx(
|
|
153
|
+
DocumentInternationalizationContext.Provider,
|
|
154
|
+
{
|
|
155
|
+
value: { ...pluginConfig, supportedLanguages },
|
|
156
|
+
children: props.renderDefault(props)
|
|
228
157
|
}
|
|
229
|
-
|
|
230
|
-
}, [workspace]);
|
|
231
|
-
return /* @__PURE__ */jsx(DocumentInternationalizationContext.Provider, {
|
|
232
|
-
value: {
|
|
233
|
-
...pluginConfig,
|
|
234
|
-
supportedLanguages
|
|
235
|
-
},
|
|
236
|
-
children: props.renderDefault(props)
|
|
237
|
-
});
|
|
158
|
+
);
|
|
238
159
|
}
|
|
239
|
-
const DeleteTranslationAction = props => {
|
|
240
|
-
const {
|
|
241
|
-
id: documentId,
|
|
242
|
-
published,
|
|
243
|
-
draft
|
|
244
|
-
} = props;
|
|
245
|
-
const doc = draft || published;
|
|
246
|
-
const {
|
|
247
|
-
languageField
|
|
248
|
-
} = useDocumentInternationalizationContext();
|
|
249
|
-
const [isDialogOpen, setDialogOpen] = useState(false);
|
|
250
|
-
const [translations, setTranslations] = useState([]);
|
|
251
|
-
const onClose = useCallback(() => setDialogOpen(false), []);
|
|
252
|
-
const documentLanguage = doc ? doc[languageField] : null;
|
|
253
|
-
const toast = useToast();
|
|
254
|
-
const client = useClient({
|
|
255
|
-
apiVersion: API_VERSION
|
|
256
|
-
});
|
|
257
|
-
const onProceed = useCallback(() => {
|
|
160
|
+
const DeleteTranslationAction = (props) => {
|
|
161
|
+
const { id: documentId, published, draft } = props, doc = draft || published, { languageField } = useDocumentInternationalizationContext(), [isDialogOpen, setDialogOpen] = useState(!1), [translations, setTranslations] = useState([]), onClose = useCallback(() => setDialogOpen(!1), []), documentLanguage = doc ? doc[languageField] : null, toast = useToast(), client = useClient({ apiVersion: API_VERSION }), onProceed = useCallback(() => {
|
|
258
162
|
const tx = client.transaction();
|
|
259
163
|
let operation = "DELETE";
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
tx.commit().then(() => {
|
|
270
|
-
if (operation === "DELETE") {
|
|
271
|
-
onClose();
|
|
272
|
-
}
|
|
273
|
-
toast.push({
|
|
164
|
+
documentLanguage && translations.length > 0 ? (operation = "UNSET", translations.forEach((translation) => {
|
|
165
|
+
tx.patch(
|
|
166
|
+
translation._id,
|
|
167
|
+
(patch) => patch.unset([
|
|
168
|
+
`${TRANSLATIONS_ARRAY_NAME}[_key == "${documentLanguage}"]`
|
|
169
|
+
])
|
|
170
|
+
);
|
|
171
|
+
})) : (tx.delete(documentId), tx.delete(`drafts.${documentId}`)), tx.commit().then(() => {
|
|
172
|
+
operation === "DELETE" && onClose(), toast.push({
|
|
274
173
|
status: "success",
|
|
275
174
|
title: operation === "UNSET" ? "Translation reference unset" : "Document deleted",
|
|
276
175
|
description: operation === "UNSET" ? "The document can now be deleted" : null
|
|
277
176
|
});
|
|
278
|
-
}).catch(err => {
|
|
177
|
+
}).catch((err) => {
|
|
279
178
|
toast.push({
|
|
280
179
|
status: "error",
|
|
281
180
|
title: operation === "unset" ? "Failed to unset translation reference" : "Failed to delete document",
|
|
@@ -289,153 +188,123 @@ const DeleteTranslationAction = props => {
|
|
|
289
188
|
icon: TrashIcon,
|
|
290
189
|
tone: "critical",
|
|
291
190
|
onHandle: () => {
|
|
292
|
-
setDialogOpen(
|
|
191
|
+
setDialogOpen(!0);
|
|
293
192
|
},
|
|
294
193
|
dialog: isDialogOpen && {
|
|
295
194
|
type: "dialog",
|
|
296
195
|
onClose,
|
|
297
196
|
header: "Delete translation",
|
|
298
|
-
content: doc ? /* @__PURE__ */jsx(
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
197
|
+
content: doc ? /* @__PURE__ */ jsx(
|
|
198
|
+
DeleteTranslationDialog,
|
|
199
|
+
{
|
|
200
|
+
doc,
|
|
201
|
+
documentId,
|
|
202
|
+
setTranslations
|
|
203
|
+
}
|
|
204
|
+
) : null,
|
|
205
|
+
footer: /* @__PURE__ */ jsx(
|
|
206
|
+
DeleteTranslationFooter,
|
|
207
|
+
{
|
|
208
|
+
onClose,
|
|
209
|
+
onProceed,
|
|
210
|
+
translations
|
|
211
|
+
}
|
|
212
|
+
)
|
|
308
213
|
}
|
|
309
214
|
};
|
|
310
|
-
}
|
|
311
|
-
|
|
215
|
+
}, query = `*[_type == $translationSchema && $id in translations[].value._ref]{
|
|
216
|
+
_id,
|
|
217
|
+
_createdAt,
|
|
218
|
+
translations
|
|
219
|
+
}`;
|
|
312
220
|
function useTranslationMetadata(id) {
|
|
313
|
-
const {
|
|
314
|
-
|
|
315
|
-
loading,
|
|
316
|
-
error
|
|
317
|
-
} = useListeningQuery(query, {
|
|
318
|
-
params: {
|
|
319
|
-
id,
|
|
320
|
-
translationSchema: METADATA_SCHEMA_NAME
|
|
321
|
-
}
|
|
221
|
+
const { data, loading, error } = useListeningQuery(query, {
|
|
222
|
+
params: { id, translationSchema: METADATA_SCHEMA_NAME }
|
|
322
223
|
});
|
|
323
|
-
return {
|
|
324
|
-
data,
|
|
325
|
-
loading,
|
|
326
|
-
error
|
|
327
|
-
};
|
|
224
|
+
return { data, loading, error };
|
|
328
225
|
}
|
|
329
226
|
function useOpenInNewPane(id, type) {
|
|
330
|
-
const routerContext = useContext(RouterContext);
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
groupIndex
|
|
334
|
-
} = usePaneRouter();
|
|
335
|
-
const openInNewPane = useCallback(() => {
|
|
336
|
-
if (!routerContext || !id || !type) {
|
|
227
|
+
const routerContext = useContext(RouterContext), { routerPanesState, groupIndex } = usePaneRouter();
|
|
228
|
+
return useCallback(() => {
|
|
229
|
+
if (!routerContext || !id || !type)
|
|
337
230
|
return;
|
|
338
|
-
}
|
|
339
231
|
if (!routerPanesState.length) {
|
|
340
|
-
routerContext.navigateIntent("edit", {
|
|
341
|
-
id,
|
|
342
|
-
type
|
|
343
|
-
});
|
|
232
|
+
routerContext.navigateIntent("edit", { id, type });
|
|
344
233
|
return;
|
|
345
234
|
}
|
|
346
235
|
const panes = [...routerPanesState];
|
|
347
|
-
panes.splice(groupIndex + 1, 0, [
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
type
|
|
236
|
+
panes.splice(groupIndex + 1, 0, [
|
|
237
|
+
{
|
|
238
|
+
id,
|
|
239
|
+
params: { type }
|
|
351
240
|
}
|
|
352
|
-
|
|
353
|
-
const href = routerContext.resolvePathFromState({
|
|
354
|
-
|
|
355
|
-
});
|
|
356
|
-
routerContext.navigateUrl({
|
|
357
|
-
path: href
|
|
358
|
-
});
|
|
241
|
+
]);
|
|
242
|
+
const href = routerContext.resolvePathFromState({ panes });
|
|
243
|
+
routerContext.navigateUrl({ path: href });
|
|
359
244
|
}, [id, type, routerContext, routerPanesState, groupIndex]);
|
|
360
|
-
return openInNewPane;
|
|
361
245
|
}
|
|
362
246
|
function LanguageManage(props) {
|
|
363
|
-
const {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
icon: CogIcon,
|
|
385
|
-
onClick: () => open()
|
|
386
|
-
})
|
|
387
|
-
})
|
|
388
|
-
});
|
|
247
|
+
const { id } = props, open = useOpenInNewPane(id, METADATA_SCHEMA_NAME);
|
|
248
|
+
return /* @__PURE__ */ jsx(
|
|
249
|
+
Tooltip,
|
|
250
|
+
{
|
|
251
|
+
animate: !0,
|
|
252
|
+
content: id ? null : /* @__PURE__ */ jsx(Box, { padding: 2, children: /* @__PURE__ */ jsx(Text, { muted: !0, size: 1, children: "Document has no other translations" }) }),
|
|
253
|
+
fallbackPlacements: ["right", "left"],
|
|
254
|
+
placement: "top",
|
|
255
|
+
portal: !0,
|
|
256
|
+
children: /* @__PURE__ */ jsx(Stack, { children: /* @__PURE__ */ jsx(
|
|
257
|
+
Button,
|
|
258
|
+
{
|
|
259
|
+
disabled: !id,
|
|
260
|
+
mode: "ghost",
|
|
261
|
+
text: "Manage Translations",
|
|
262
|
+
icon: CogIcon,
|
|
263
|
+
onClick: () => open()
|
|
264
|
+
}
|
|
265
|
+
) })
|
|
266
|
+
}
|
|
267
|
+
);
|
|
389
268
|
}
|
|
390
|
-
function createReference(key, ref, type) {
|
|
391
|
-
let strengthenOnPublish = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
|
|
269
|
+
function createReference(key, ref, type, strengthenOnPublish = !0) {
|
|
392
270
|
return {
|
|
393
271
|
_key: key,
|
|
394
272
|
_type: "internationalizedArrayReferenceValue",
|
|
395
273
|
value: {
|
|
396
274
|
_type: "reference",
|
|
397
275
|
_ref: ref,
|
|
398
|
-
_weak:
|
|
276
|
+
_weak: !0,
|
|
399
277
|
// If the user has configured weakReferences, we won't want to strengthen them
|
|
400
|
-
...
|
|
401
|
-
_strengthenOnPublish: {
|
|
402
|
-
type
|
|
403
|
-
}
|
|
404
|
-
} : {})
|
|
278
|
+
...strengthenOnPublish ? { _strengthenOnPublish: { type } } : {}
|
|
405
279
|
}
|
|
406
280
|
};
|
|
407
281
|
}
|
|
408
282
|
function removeExcludedPaths(doc, schemaType) {
|
|
409
|
-
if (!isDocumentSchemaType(schemaType) || !doc)
|
|
283
|
+
if (!isDocumentSchemaType(schemaType) || !doc)
|
|
410
284
|
return doc;
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
285
|
+
const pathsToExclude = extractPaths(doc, schemaType, []).filter(
|
|
286
|
+
(field) => {
|
|
287
|
+
var _a, _b, _c;
|
|
288
|
+
return ((_c = (_b = (_a = field.schemaType) == null ? void 0 : _a.options) == null ? void 0 : _b.documentInternationalization) == null ? void 0 : _c.exclude) === !0;
|
|
289
|
+
}
|
|
290
|
+
).map((field) => pathToString(field.path));
|
|
291
|
+
return new Mutation({
|
|
292
|
+
mutations: [
|
|
293
|
+
{
|
|
294
|
+
patch: {
|
|
295
|
+
id: doc._id,
|
|
296
|
+
unset: pathsToExclude
|
|
297
|
+
}
|
|
423
298
|
}
|
|
424
|
-
|
|
425
|
-
});
|
|
426
|
-
return mut.apply(doc);
|
|
299
|
+
]
|
|
300
|
+
}).apply(doc);
|
|
427
301
|
}
|
|
428
302
|
function extractPaths(doc, schemaType, path) {
|
|
429
303
|
return schemaType.fields.reduce((acc, field) => {
|
|
430
304
|
var _a, _b;
|
|
431
|
-
const fieldPath = [...path, field.name];
|
|
432
|
-
|
|
433
|
-
const {
|
|
434
|
-
value
|
|
435
|
-
} = (_a = extractWithPath(pathToString(fieldPath), doc)[0]) != null ? _a : {};
|
|
436
|
-
if (!value) {
|
|
305
|
+
const fieldPath = [...path, field.name], fieldSchema = field.type, { value } = (_a = extractWithPath(pathToString(fieldPath), doc)[0]) != null ? _a : {};
|
|
306
|
+
if (!value)
|
|
437
307
|
return acc;
|
|
438
|
-
}
|
|
439
308
|
const thisFieldWithPath = {
|
|
440
309
|
path: fieldPath,
|
|
441
310
|
name: field.name,
|
|
@@ -445,23 +314,19 @@ function extractPaths(doc, schemaType, path) {
|
|
|
445
314
|
if (fieldSchema.jsonType === "object") {
|
|
446
315
|
const innerFields = extractPaths(doc, fieldSchema, fieldPath);
|
|
447
316
|
return [...acc, thisFieldWithPath, ...innerFields];
|
|
448
|
-
} else if (fieldSchema.jsonType === "array" && fieldSchema.of.length && fieldSchema.of.some(item => "fields" in item)) {
|
|
449
|
-
const {
|
|
450
|
-
value: arrayValue
|
|
451
|
-
} = (_b = extractWithPath(pathToString(fieldPath), doc)[0]) != null ? _b : {};
|
|
317
|
+
} else if (fieldSchema.jsonType === "array" && fieldSchema.of.length && fieldSchema.of.some((item) => "fields" in item)) {
|
|
318
|
+
const { value: arrayValue } = (_b = extractWithPath(pathToString(fieldPath), doc)[0]) != null ? _b : {};
|
|
452
319
|
let arrayPaths = [];
|
|
453
|
-
if (arrayValue
|
|
320
|
+
if (arrayValue != null && arrayValue.length)
|
|
454
321
|
for (const item of arrayValue) {
|
|
455
|
-
const itemPath = [...fieldPath, {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
const innerFields = extractPaths(doc, itemSchema, itemPath);
|
|
464
|
-
const arrayMember = {
|
|
322
|
+
const itemPath = [...fieldPath, { _key: item._key }];
|
|
323
|
+
let itemSchema = fieldSchema.of.find((t) => t.name === item._type);
|
|
324
|
+
if (item._type || (itemSchema = fieldSchema.of[0]), item._key && itemSchema) {
|
|
325
|
+
const innerFields = extractPaths(
|
|
326
|
+
doc,
|
|
327
|
+
itemSchema,
|
|
328
|
+
itemPath
|
|
329
|
+
), arrayMember = {
|
|
465
330
|
path: itemPath,
|
|
466
331
|
name: item._key,
|
|
467
332
|
schemaType: itemSchema,
|
|
@@ -470,7 +335,6 @@ function extractPaths(doc, schemaType, path) {
|
|
|
470
335
|
arrayPaths = [...arrayPaths, arrayMember, ...innerFields];
|
|
471
336
|
}
|
|
472
337
|
}
|
|
473
|
-
}
|
|
474
338
|
return [...acc, thisFieldWithPath, ...arrayPaths];
|
|
475
339
|
}
|
|
476
340
|
return [...acc, thisFieldWithPath];
|
|
@@ -485,376 +349,258 @@ function LanguageOption(props) {
|
|
|
485
349
|
current,
|
|
486
350
|
source,
|
|
487
351
|
sourceLanguageId,
|
|
488
|
-
metadata,
|
|
352
|
+
metadata: metadata2,
|
|
489
353
|
metadataId
|
|
490
|
-
} = props;
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
apiVersion,
|
|
495
|
-
languageField,
|
|
496
|
-
weakReferences
|
|
497
|
-
} = useDocumentInternationalizationContext();
|
|
498
|
-
const client = useClient({
|
|
499
|
-
apiVersion
|
|
500
|
-
});
|
|
501
|
-
const toast = useToast();
|
|
502
|
-
const open = useOpenInNewPane((_a = translation == null ? void 0 : translation.value) == null ? void 0 : _a._ref, schemaType.name);
|
|
503
|
-
const handleOpen = useCallback(() => open(), [open]);
|
|
354
|
+
} = props, [userHasClicked, setUserHasClicked] = useState(!1), disabled = props.disabled || userHasClicked || current || !source || !sourceLanguageId || !metadataId, translation = metadata2 != null && metadata2.translations.length ? metadata2.translations.find((t) => t._key === language.id) : void 0, { apiVersion, languageField, weakReferences } = useDocumentInternationalizationContext(), client = useClient({ apiVersion }), toast = useToast(), open = useOpenInNewPane((_a = translation == null ? void 0 : translation.value) == null ? void 0 : _a._ref, schemaType.name), handleOpen = useCallback(() => open(), [open]);
|
|
355
|
+
useEffect(() => {
|
|
356
|
+
setUserHasClicked(!1);
|
|
357
|
+
}, [!!translation]);
|
|
504
358
|
const handleCreate = useCallback(async () => {
|
|
505
|
-
if (!source)
|
|
359
|
+
if (!source)
|
|
506
360
|
throw new Error("Cannot create translation without source document");
|
|
507
|
-
|
|
508
|
-
if (!sourceLanguageId) {
|
|
361
|
+
if (!sourceLanguageId)
|
|
509
362
|
throw new Error("Cannot create translation without source language ID");
|
|
510
|
-
|
|
511
|
-
if (!metadataId) {
|
|
363
|
+
if (!metadataId)
|
|
512
364
|
throw new Error("Cannot create translation without a metadata ID");
|
|
513
|
-
|
|
514
|
-
const transaction = client.transaction();
|
|
515
|
-
const newTranslationDocumentId = uuid();
|
|
365
|
+
setUserHasClicked(!0);
|
|
366
|
+
const transaction = client.transaction(), newTranslationDocumentId = uuid();
|
|
516
367
|
let newTranslationDocument = {
|
|
517
368
|
...source,
|
|
518
|
-
_id:
|
|
369
|
+
_id: `drafts.${newTranslationDocumentId}`,
|
|
519
370
|
// 2. Update language of the translation
|
|
520
371
|
[languageField]: language.id
|
|
521
372
|
};
|
|
522
|
-
newTranslationDocument = removeExcludedPaths(
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
const
|
|
373
|
+
newTranslationDocument = removeExcludedPaths(
|
|
374
|
+
newTranslationDocument,
|
|
375
|
+
schemaType
|
|
376
|
+
), transaction.create(newTranslationDocument);
|
|
377
|
+
const sourceReference = createReference(
|
|
378
|
+
sourceLanguageId,
|
|
379
|
+
documentId,
|
|
380
|
+
schemaType.name,
|
|
381
|
+
!weakReferences
|
|
382
|
+
), newTranslationReference = createReference(
|
|
383
|
+
language.id,
|
|
384
|
+
newTranslationDocumentId,
|
|
385
|
+
schemaType.name,
|
|
386
|
+
!weakReferences
|
|
387
|
+
), newMetadataDocument = {
|
|
527
388
|
_id: metadataId,
|
|
528
389
|
_type: METADATA_SCHEMA_NAME,
|
|
529
390
|
schemaTypes: [schemaType.name],
|
|
530
391
|
translations: [sourceReference]
|
|
531
392
|
};
|
|
532
393
|
transaction.createIfNotExists(newMetadataDocument);
|
|
533
|
-
const metadataPatch = client.patch(metadataId).setIfMissing({
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
transaction.patch(metadataPatch);
|
|
537
|
-
transaction.commit().then(() => {
|
|
538
|
-
const metadataExisted = Boolean(metadata == null ? void 0 : metadata._createdAt);
|
|
394
|
+
const metadataPatch = client.patch(metadataId).setIfMissing({ translations: [sourceReference] }).insert("after", "translations[-1]", [newTranslationReference]);
|
|
395
|
+
transaction.patch(metadataPatch), transaction.commit().then(() => {
|
|
396
|
+
const metadataExisted = !!(metadata2 != null && metadata2._createdAt);
|
|
539
397
|
return toast.push({
|
|
540
398
|
status: "success",
|
|
541
|
-
title:
|
|
399
|
+
title: `Created "${language.title}" translation`,
|
|
542
400
|
description: metadataExisted ? "Updated Translations Metadata" : "Created Translations Metadata"
|
|
543
401
|
});
|
|
544
|
-
}).catch(err => {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
402
|
+
}).catch((err) => (console.error(err), setUserHasClicked(!1), toast.push({
|
|
403
|
+
status: "error",
|
|
404
|
+
title: "Error creating translation",
|
|
405
|
+
description: err.message
|
|
406
|
+
})));
|
|
407
|
+
}, [
|
|
408
|
+
client,
|
|
409
|
+
documentId,
|
|
410
|
+
language.id,
|
|
411
|
+
language.title,
|
|
412
|
+
languageField,
|
|
413
|
+
metadata2 == null ? void 0 : metadata2._createdAt,
|
|
414
|
+
metadataId,
|
|
415
|
+
schemaType,
|
|
416
|
+
source,
|
|
417
|
+
sourceLanguageId,
|
|
418
|
+
toast,
|
|
419
|
+
weakReferences
|
|
420
|
+
]);
|
|
553
421
|
let message;
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
children: /* @__PURE__ */jsxs(Flex, {
|
|
578
|
-
gap: 3,
|
|
579
|
-
align: "center",
|
|
580
|
-
children: [disabled && !current ? /* @__PURE__ */jsx(Spinner, {}) : /* @__PURE__ */jsx(Text, {
|
|
581
|
-
size: 2,
|
|
582
|
-
children: translation ? /* @__PURE__ */jsx(SplitVerticalIcon, {}) : current ? /* @__PURE__ */jsx(CheckmarkIcon, {}) : /* @__PURE__ */jsx(AddIcon, {})
|
|
583
|
-
}), /* @__PURE__ */jsx(Box, {
|
|
584
|
-
flex: 1,
|
|
585
|
-
children: /* @__PURE__ */jsx(Text, {
|
|
586
|
-
children: language.title
|
|
587
|
-
})
|
|
588
|
-
}), /* @__PURE__ */jsx(Badge, {
|
|
589
|
-
tone: disabled || current ? "default" : "primary",
|
|
590
|
-
children: language.id
|
|
591
|
-
})]
|
|
592
|
-
})
|
|
593
|
-
})
|
|
594
|
-
});
|
|
422
|
+
return current ? message = "Current document" : translation ? message = `Open ${language.title} translation` : translation || (message = `Create new ${language.title} translation`), /* @__PURE__ */ jsx(
|
|
423
|
+
Tooltip,
|
|
424
|
+
{
|
|
425
|
+
animate: !0,
|
|
426
|
+
content: /* @__PURE__ */ jsx(Box, { padding: 2, children: /* @__PURE__ */ jsx(Text, { muted: !0, size: 1, children: message }) }),
|
|
427
|
+
fallbackPlacements: ["right", "left"],
|
|
428
|
+
placement: "top",
|
|
429
|
+
portal: !0,
|
|
430
|
+
children: /* @__PURE__ */ jsx(
|
|
431
|
+
Button,
|
|
432
|
+
{
|
|
433
|
+
onClick: translation ? handleOpen : handleCreate,
|
|
434
|
+
mode: current && disabled ? "default" : "bleed",
|
|
435
|
+
disabled,
|
|
436
|
+
children: /* @__PURE__ */ jsxs(Flex, { gap: 3, align: "center", children: [
|
|
437
|
+
disabled && !current ? /* @__PURE__ */ jsx(Spinner, {}) : /* @__PURE__ */ jsx(Text, { size: 2, children: translation ? /* @__PURE__ */ jsx(SplitVerticalIcon, {}) : current ? /* @__PURE__ */ jsx(CheckmarkIcon, {}) : /* @__PURE__ */ jsx(AddIcon, {}) }),
|
|
438
|
+
/* @__PURE__ */ jsx(Box, { flex: 1, children: /* @__PURE__ */ jsx(Text, { children: language.title }) }),
|
|
439
|
+
/* @__PURE__ */ jsx(Badge, { tone: disabled || current ? "default" : "primary", children: language.id })
|
|
440
|
+
] })
|
|
441
|
+
}
|
|
442
|
+
)
|
|
443
|
+
}
|
|
444
|
+
);
|
|
595
445
|
}
|
|
596
446
|
function LanguagePatch(props) {
|
|
597
|
-
const {
|
|
598
|
-
|
|
599
|
-
source
|
|
600
|
-
} = props;
|
|
601
|
-
const {
|
|
602
|
-
apiVersion,
|
|
603
|
-
languageField
|
|
604
|
-
} = useDocumentInternationalizationContext();
|
|
605
|
-
const disabled = props.disabled || !source;
|
|
606
|
-
const client = useClient({
|
|
607
|
-
apiVersion
|
|
608
|
-
});
|
|
609
|
-
const toast = useToast();
|
|
610
|
-
const handleClick = useCallback(() => {
|
|
611
|
-
if (!source) {
|
|
447
|
+
const { language, source } = props, { apiVersion, languageField } = useDocumentInternationalizationContext(), disabled = props.disabled || !source, client = useClient({ apiVersion }), toast = useToast(), handleClick = useCallback(() => {
|
|
448
|
+
if (!source)
|
|
612
449
|
throw new Error("Cannot patch missing document");
|
|
613
|
-
}
|
|
614
450
|
const currentId = source._id;
|
|
615
|
-
client.patch(currentId).set({
|
|
616
|
-
[languageField]: language.id
|
|
617
|
-
}).commit().then(() => {
|
|
451
|
+
client.patch(currentId).set({ [languageField]: language.id }).commit().then(() => {
|
|
618
452
|
toast.push({
|
|
619
|
-
title:
|
|
453
|
+
title: `Set document language to ${language.title}`,
|
|
620
454
|
status: "success"
|
|
621
455
|
});
|
|
622
|
-
}).catch(err => {
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
status: "error"
|
|
627
|
-
});
|
|
628
|
-
});
|
|
456
|
+
}).catch((err) => (console.error(err), toast.push({
|
|
457
|
+
title: `Failed to set document language to ${language.title}`,
|
|
458
|
+
status: "error"
|
|
459
|
+
})));
|
|
629
460
|
}, [source, client, languageField, language, toast]);
|
|
630
|
-
return /* @__PURE__ */jsx(
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
align: "center",
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
})
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
children: language.title
|
|
645
|
-
})
|
|
646
|
-
}), /* @__PURE__ */jsx(Badge, {
|
|
647
|
-
children: language.id
|
|
648
|
-
})]
|
|
649
|
-
})
|
|
650
|
-
});
|
|
461
|
+
return /* @__PURE__ */ jsx(
|
|
462
|
+
Button,
|
|
463
|
+
{
|
|
464
|
+
mode: "bleed",
|
|
465
|
+
onClick: handleClick,
|
|
466
|
+
disabled,
|
|
467
|
+
justify: "flex-start",
|
|
468
|
+
children: /* @__PURE__ */ jsxs(Flex, { gap: 3, align: "center", children: [
|
|
469
|
+
/* @__PURE__ */ jsx(Text, { size: 2, children: /* @__PURE__ */ jsx(EditIcon, {}) }),
|
|
470
|
+
/* @__PURE__ */ jsx(Box, { flex: 1, children: /* @__PURE__ */ jsx(Text, { children: language.title }) }),
|
|
471
|
+
/* @__PURE__ */ jsx(Badge, { children: language.id })
|
|
472
|
+
] })
|
|
473
|
+
}
|
|
474
|
+
);
|
|
651
475
|
}
|
|
652
|
-
var
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
}));
|
|
657
|
-
var _a;
|
|
658
|
-
var ConstrainedBox = styled(Box)(_a || (_a = __template(["\n max-width: 280px;\n"])));
|
|
659
|
-
function Warning(_ref) {
|
|
660
|
-
let {
|
|
661
|
-
children
|
|
662
|
-
} = _ref;
|
|
663
|
-
return /* @__PURE__ */jsx(Card, {
|
|
664
|
-
tone: "caution",
|
|
665
|
-
padding: 3,
|
|
666
|
-
children: /* @__PURE__ */jsx(Flex, {
|
|
667
|
-
justify: "center",
|
|
668
|
-
children: /* @__PURE__ */jsx(ConstrainedBox, {
|
|
669
|
-
children: /* @__PURE__ */jsx(Text, {
|
|
670
|
-
size: 1,
|
|
671
|
-
align: "center",
|
|
672
|
-
children
|
|
673
|
-
})
|
|
674
|
-
})
|
|
675
|
-
})
|
|
676
|
-
});
|
|
476
|
+
var ConstrainedBox = styled(Box)`
|
|
477
|
+
max-width: 280px;
|
|
478
|
+
`;
|
|
479
|
+
function Warning({ children }) {
|
|
480
|
+
return /* @__PURE__ */ jsx(Card, { tone: "caution", padding: 3, children: /* @__PURE__ */ jsx(Flex, { justify: "center", children: /* @__PURE__ */ jsx(ConstrainedBox, { children: /* @__PURE__ */ jsx(Text, { size: 1, align: "center", children }) }) }) });
|
|
677
481
|
}
|
|
678
482
|
function DocumentInternationalizationMenu(props) {
|
|
679
|
-
const {
|
|
680
|
-
|
|
681
|
-
} =
|
|
682
|
-
const schemaType = props.schemaType;
|
|
683
|
-
const {
|
|
684
|
-
languageField,
|
|
685
|
-
supportedLanguages
|
|
686
|
-
} = useDocumentInternationalizationContext();
|
|
687
|
-
const [query, setQuery] = useState("");
|
|
688
|
-
const handleQuery = useCallback(event => {
|
|
689
|
-
if (event.currentTarget.value) {
|
|
690
|
-
setQuery(event.currentTarget.value);
|
|
691
|
-
} else {
|
|
692
|
-
setQuery("");
|
|
693
|
-
}
|
|
694
|
-
}, []);
|
|
695
|
-
const [open, setOpen] = useState(false);
|
|
696
|
-
const handleClick = useCallback(() => setOpen(o => !o), []);
|
|
697
|
-
const [button, setButton] = useState(null);
|
|
698
|
-
const [popover, setPopover] = useState(null);
|
|
699
|
-
const handleClickOutside = useCallback(() => setOpen(false), []);
|
|
483
|
+
const { documentId } = props, schemaType = props.schemaType, { languageField, supportedLanguages } = useDocumentInternationalizationContext(), [query2, setQuery] = useState(""), handleQuery = useCallback((event) => {
|
|
484
|
+
event.currentTarget.value ? setQuery(event.currentTarget.value) : setQuery("");
|
|
485
|
+
}, []), [open, setOpen] = useState(!1), handleClick = useCallback(() => setOpen((o) => !o), []), [button, setButton] = useState(null), [popover, setPopover] = useState(null), handleClickOutside = useCallback(() => setOpen(!1), []);
|
|
700
486
|
useClickOutside(handleClickOutside, [button, popover]);
|
|
701
|
-
const {
|
|
702
|
-
data,
|
|
703
|
-
loading,
|
|
704
|
-
error
|
|
705
|
-
} = useTranslationMetadata(documentId);
|
|
706
|
-
const metadata = Array.isArray(data) && data.length ? data[0] : null;
|
|
707
|
-
const metadataId = useMemo(() => {
|
|
487
|
+
const { data, loading, error } = useTranslationMetadata(documentId), metadata2 = Array.isArray(data) && data.length ? data[0] : null, metadataId = useMemo(() => {
|
|
708
488
|
var _a;
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
const sourceLanguageIsValid = supportedLanguages.some(l => l.id === sourceLanguageId);
|
|
724
|
-
const allLanguagesAreValid = useMemo(() => {
|
|
725
|
-
const valid = supportedLanguages.every(l => l.id && l.title);
|
|
726
|
-
if (!valid) {
|
|
727
|
-
console.warn('Not all languages are valid. It should be an array of objects with an "id" and "title" property. Or a function that returns an array of objects with an "id" and "title" property.', supportedLanguages);
|
|
728
|
-
}
|
|
729
|
-
return valid;
|
|
730
|
-
}, [supportedLanguages]);
|
|
731
|
-
const content = /* @__PURE__ */jsx(Box, {
|
|
732
|
-
padding: 1,
|
|
733
|
-
children: error ? /* @__PURE__ */jsx(Card, {
|
|
734
|
-
tone: "critical",
|
|
735
|
-
padding: 1,
|
|
736
|
-
children: /* @__PURE__ */jsx(Text, {
|
|
737
|
-
children: "There was an error returning translations metadata"
|
|
738
|
-
})
|
|
739
|
-
}) : /* @__PURE__ */jsxs(Stack, {
|
|
740
|
-
space: 1,
|
|
741
|
-
children: [/* @__PURE__ */jsx(LanguageManage, {
|
|
742
|
-
id: metadata == null ? void 0 : metadata._id
|
|
743
|
-
}), supportedLanguages.length > 4 ? /* @__PURE__ */jsx(TextInput, {
|
|
489
|
+
return loading ? null : (_a = metadata2 == null ? void 0 : metadata2._id) != null ? _a : uuid();
|
|
490
|
+
}, [loading, metadata2 == null ? void 0 : metadata2._id]), { draft, published } = useEditState(documentId, schemaType.name), source = draft || published, documentIsInOneMetadataDocument = useMemo(() => Array.isArray(data) && data.length <= 1, [data]), sourceLanguageId = source == null ? void 0 : source[languageField], sourceLanguageIsValid = supportedLanguages.some(
|
|
491
|
+
(l) => l.id === sourceLanguageId
|
|
492
|
+
), allLanguagesAreValid = useMemo(() => {
|
|
493
|
+
const valid = supportedLanguages.every((l) => l.id && l.title);
|
|
494
|
+
return valid || console.warn(
|
|
495
|
+
'Not all languages are valid. It should be an array of objects with an "id" and "title" property. Or a function that returns an array of objects with an "id" and "title" property.',
|
|
496
|
+
supportedLanguages
|
|
497
|
+
), valid;
|
|
498
|
+
}, [supportedLanguages]), content = /* @__PURE__ */ jsx(Box, { padding: 1, children: error ? /* @__PURE__ */ jsx(Card, { tone: "critical", padding: 1, children: /* @__PURE__ */ jsx(Text, { children: "There was an error returning translations metadata" }) }) : /* @__PURE__ */ jsxs(Stack, { space: 1, children: [
|
|
499
|
+
/* @__PURE__ */ jsx(LanguageManage, { id: metadata2 == null ? void 0 : metadata2._id }),
|
|
500
|
+
supportedLanguages.length > 4 ? /* @__PURE__ */ jsx(
|
|
501
|
+
TextInput,
|
|
502
|
+
{
|
|
744
503
|
onChange: handleQuery,
|
|
745
|
-
value:
|
|
504
|
+
value: query2,
|
|
746
505
|
placeholder: "Filter languages"
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
})
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
return true;
|
|
767
|
-
}).map(language => {
|
|
506
|
+
}
|
|
507
|
+
) : null,
|
|
508
|
+
supportedLanguages.length > 0 ? /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
509
|
+
loading ? null : /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
510
|
+
data && documentIsInOneMetadataDocument ? null : /* @__PURE__ */ jsx(Warning, { children: "This document has been found in more than one Translations Metadata documents" }),
|
|
511
|
+
allLanguagesAreValid ? null : /* @__PURE__ */ jsx(Warning, { children: "Not all language objects are valid. See the console." }),
|
|
512
|
+
sourceLanguageId ? null : /* @__PURE__ */ jsxs(Warning, { children: [
|
|
513
|
+
"Choose a language to apply to",
|
|
514
|
+
" ",
|
|
515
|
+
/* @__PURE__ */ jsx("strong", { children: "this Document" })
|
|
516
|
+
] }),
|
|
517
|
+
sourceLanguageId && !sourceLanguageIsValid ? /* @__PURE__ */ jsxs(Warning, { children: [
|
|
518
|
+
"Select a supported language. Current language value:",
|
|
519
|
+
" ",
|
|
520
|
+
/* @__PURE__ */ jsx("code", { children: sourceLanguageId })
|
|
521
|
+
] }) : null
|
|
522
|
+
] }),
|
|
523
|
+
supportedLanguages.filter((language) => query2 ? language.title.toLowerCase().includes(query2.toLowerCase()) : !0).map(
|
|
524
|
+
(language) => {
|
|
768
525
|
var _a;
|
|
769
|
-
return !loading && sourceLanguageId && sourceLanguageIsValid ?
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
language
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
526
|
+
return !loading && sourceLanguageId && sourceLanguageIsValid ? (
|
|
527
|
+
// Button to duplicate this document to a new translation
|
|
528
|
+
// And either create or update the metadata document
|
|
529
|
+
/* @__PURE__ */ jsx(
|
|
530
|
+
LanguageOption,
|
|
531
|
+
{
|
|
532
|
+
language,
|
|
533
|
+
schemaType,
|
|
534
|
+
documentId,
|
|
535
|
+
disabled: loading || !allLanguagesAreValid,
|
|
536
|
+
current: language.id === sourceLanguageId,
|
|
537
|
+
metadata: metadata2,
|
|
538
|
+
metadataId,
|
|
539
|
+
source,
|
|
540
|
+
sourceLanguageId
|
|
541
|
+
},
|
|
542
|
+
language.id
|
|
543
|
+
)
|
|
544
|
+
) : (
|
|
545
|
+
// Button to set a language field on *this* document
|
|
546
|
+
/* @__PURE__ */ jsx(
|
|
547
|
+
LanguagePatch,
|
|
548
|
+
{
|
|
549
|
+
source,
|
|
550
|
+
language,
|
|
551
|
+
disabled: (_a = loading || !allLanguagesAreValid || (metadata2 == null ? void 0 : metadata2.translations.filter((t) => {
|
|
552
|
+
var _a2;
|
|
553
|
+
return ((_a2 = t == null ? void 0 : t.value) == null ? void 0 : _a2._ref) !== documentId;
|
|
554
|
+
}).some((t) => t._key === language.id))) != null ? _a : !1
|
|
555
|
+
},
|
|
556
|
+
language.id
|
|
557
|
+
)
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
)
|
|
561
|
+
] }) : null
|
|
562
|
+
] }) }), issueWithTranslations = !loading && sourceLanguageId && !sourceLanguageIsValid;
|
|
563
|
+
return !documentId || !schemaType || !schemaType.name ? null : /* @__PURE__ */ jsx(
|
|
564
|
+
Popover,
|
|
565
|
+
{
|
|
566
|
+
animate: !0,
|
|
567
|
+
constrainSize: !0,
|
|
568
|
+
content,
|
|
569
|
+
open,
|
|
570
|
+
portal: !0,
|
|
571
|
+
ref: setPopover,
|
|
572
|
+
overflow: "auto",
|
|
573
|
+
tone: "default",
|
|
574
|
+
children: /* @__PURE__ */ jsx(
|
|
575
|
+
Button,
|
|
576
|
+
{
|
|
577
|
+
text: "Translations",
|
|
578
|
+
mode: "bleed",
|
|
579
|
+
disabled: !source,
|
|
580
|
+
tone: !source || loading || !issueWithTranslations ? void 0 : "caution",
|
|
581
|
+
icon: TranslateIcon,
|
|
582
|
+
onClick: handleClick,
|
|
583
|
+
ref: setButton,
|
|
584
|
+
selected: open
|
|
585
|
+
}
|
|
586
|
+
)
|
|
587
|
+
}
|
|
588
|
+
);
|
|
824
589
|
}
|
|
825
|
-
const DeleteMetadataAction = props => {
|
|
826
|
-
const {
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
onComplete
|
|
831
|
-
} = props;
|
|
832
|
-
const doc = draft || published;
|
|
833
|
-
const [isDialogOpen, setDialogOpen] = useState(false);
|
|
834
|
-
const onClose = useCallback(() => setDialogOpen(false), []);
|
|
835
|
-
const translations = useMemo(() => doc && Array.isArray(doc[TRANSLATIONS_ARRAY_NAME]) ? doc[TRANSLATIONS_ARRAY_NAME] : [], [doc]);
|
|
836
|
-
const toast = useToast();
|
|
837
|
-
const client = useClient({
|
|
838
|
-
apiVersion: API_VERSION
|
|
839
|
-
});
|
|
840
|
-
const onProceed = useCallback(() => {
|
|
590
|
+
const DeleteMetadataAction = (props) => {
|
|
591
|
+
const { id: documentId, published, draft, onComplete } = props, doc = draft || published, [isDialogOpen, setDialogOpen] = useState(!1), onClose = useCallback(() => setDialogOpen(!1), []), translations = useMemo(
|
|
592
|
+
() => doc && Array.isArray(doc[TRANSLATIONS_ARRAY_NAME]) ? doc[TRANSLATIONS_ARRAY_NAME] : [],
|
|
593
|
+
[doc]
|
|
594
|
+
), toast = useToast(), client = useClient({ apiVersion: API_VERSION }), onProceed = useCallback(() => {
|
|
841
595
|
const tx = client.transaction();
|
|
842
|
-
tx.patch(documentId, patch => patch.unset([TRANSLATIONS_ARRAY_NAME]))
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
tx.delete("drafts.".concat(translation.value._ref));
|
|
847
|
-
});
|
|
848
|
-
}
|
|
849
|
-
tx.delete(documentId);
|
|
850
|
-
tx.delete("drafts.".concat(documentId));
|
|
851
|
-
tx.commit().then(() => {
|
|
852
|
-
onClose();
|
|
853
|
-
toast.push({
|
|
596
|
+
tx.patch(documentId, (patch) => patch.unset([TRANSLATIONS_ARRAY_NAME])), translations.length > 0 && translations.forEach((translation) => {
|
|
597
|
+
tx.delete(translation.value._ref), tx.delete(`drafts.${translation.value._ref}`);
|
|
598
|
+
}), tx.delete(documentId), tx.delete(`drafts.${documentId}`), tx.commit().then(() => {
|
|
599
|
+
onClose(), toast.push({
|
|
854
600
|
status: "success",
|
|
855
601
|
title: "Deleted document and translations"
|
|
856
602
|
});
|
|
857
|
-
}).catch(err => {
|
|
603
|
+
}).catch((err) => {
|
|
858
604
|
toast.push({
|
|
859
605
|
status: "error",
|
|
860
606
|
title: "Failed to delete document and translations",
|
|
@@ -868,32 +614,25 @@ const DeleteMetadataAction = props => {
|
|
|
868
614
|
icon: TrashIcon,
|
|
869
615
|
tone: "critical",
|
|
870
616
|
onHandle: () => {
|
|
871
|
-
setDialogOpen(
|
|
617
|
+
setDialogOpen(!0);
|
|
872
618
|
},
|
|
873
619
|
dialog: isDialogOpen && {
|
|
874
620
|
type: "confirm",
|
|
875
621
|
onCancel: onComplete,
|
|
876
622
|
onConfirm: () => {
|
|
877
|
-
onProceed();
|
|
878
|
-
onComplete();
|
|
623
|
+
onProceed(), onComplete();
|
|
879
624
|
},
|
|
880
625
|
tone: "critical",
|
|
881
|
-
message: translations.length === 1 ? "Delete 1 translation and this document" :
|
|
626
|
+
message: translations.length === 1 ? "Delete 1 translation and this document" : `Delete all ${translations.length} translations and this document`
|
|
882
627
|
}
|
|
883
628
|
};
|
|
884
629
|
};
|
|
885
630
|
function LanguageBadge(props) {
|
|
886
631
|
var _a, _b;
|
|
887
|
-
const source = (props == null ? void 0 : props.draft) || (props == null ? void 0 : props.published);
|
|
888
|
-
|
|
889
|
-
languageField,
|
|
890
|
-
supportedLanguages
|
|
891
|
-
} = useDocumentInternationalizationContext();
|
|
892
|
-
const languageId = source == null ? void 0 : source[languageField];
|
|
893
|
-
if (!languageId) {
|
|
632
|
+
const source = (props == null ? void 0 : props.draft) || (props == null ? void 0 : props.published), { languageField, supportedLanguages } = useDocumentInternationalizationContext(), languageId = source == null ? void 0 : source[languageField];
|
|
633
|
+
if (!languageId)
|
|
894
634
|
return null;
|
|
895
|
-
|
|
896
|
-
const language = Array.isArray(supportedLanguages) ? supportedLanguages.find(l => l.id === languageId) : null;
|
|
635
|
+
const language = Array.isArray(supportedLanguages) ? supportedLanguages.find((l) => l.id === languageId) : null;
|
|
897
636
|
return {
|
|
898
637
|
label: (_a = language == null ? void 0 : language.id) != null ? _a : String(languageId),
|
|
899
638
|
title: (_b = language == null ? void 0 : language.title) != null ? _b : void 0,
|
|
@@ -908,140 +647,75 @@ function DocumentCheck(props) {
|
|
|
908
647
|
removeInvalidId,
|
|
909
648
|
addDraftId,
|
|
910
649
|
removeDraftId
|
|
911
|
-
} = props;
|
|
912
|
-
|
|
913
|
-
|
|
650
|
+
} = props, editState = useEditState(id, ""), { isValidating, validation } = useValidationStatus(id, ""), schema = useSchema(), validationHasErrors = useMemo(() => !isValidating && validation.length > 0 && validation.some((item) => item.level === "error"), [isValidating, validation]);
|
|
651
|
+
if (useEffect(() => {
|
|
652
|
+
validationHasErrors ? addInvalidId(id) : removeInvalidId(id), editState.draft ? addDraftId(id) : removeDraftId(id), isValidating || onCheckComplete(id);
|
|
653
|
+
}, [
|
|
654
|
+
addDraftId,
|
|
655
|
+
addInvalidId,
|
|
656
|
+
editState.draft,
|
|
657
|
+
id,
|
|
914
658
|
isValidating,
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
}, [isValidating, validation]);
|
|
921
|
-
useEffect(() => {
|
|
922
|
-
if (validationHasErrors) {
|
|
923
|
-
addInvalidId(id);
|
|
924
|
-
} else {
|
|
925
|
-
removeInvalidId(id);
|
|
926
|
-
}
|
|
927
|
-
if (editState.draft) {
|
|
928
|
-
addDraftId(id);
|
|
929
|
-
} else {
|
|
930
|
-
removeDraftId(id);
|
|
931
|
-
}
|
|
932
|
-
if (!isValidating) {
|
|
933
|
-
onCheckComplete(id);
|
|
934
|
-
}
|
|
935
|
-
}, [addDraftId, addInvalidId, editState.draft, id, isValidating, onCheckComplete, removeDraftId, removeInvalidId, validationHasErrors]);
|
|
936
|
-
if (!editState.draft) {
|
|
659
|
+
onCheckComplete,
|
|
660
|
+
removeDraftId,
|
|
661
|
+
removeInvalidId,
|
|
662
|
+
validationHasErrors
|
|
663
|
+
]), !editState.draft)
|
|
937
664
|
return null;
|
|
938
|
-
}
|
|
939
665
|
const schemaType = schema.get(editState.draft._type);
|
|
940
|
-
return /* @__PURE__ */jsx(
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
666
|
+
return /* @__PURE__ */ jsx(
|
|
667
|
+
Card,
|
|
668
|
+
{
|
|
669
|
+
border: !0,
|
|
670
|
+
padding: 2,
|
|
671
|
+
tone: validationHasErrors ? "critical" : "positive",
|
|
672
|
+
children: editState.draft && schemaType ? /* @__PURE__ */ jsx(
|
|
673
|
+
Preview,
|
|
674
|
+
{
|
|
675
|
+
layout: "default",
|
|
676
|
+
value: editState.draft,
|
|
677
|
+
schemaType
|
|
678
|
+
}
|
|
679
|
+
) : /* @__PURE__ */ jsx(Spinner, {})
|
|
680
|
+
}
|
|
681
|
+
);
|
|
950
682
|
}
|
|
951
683
|
function InfoIcon(props) {
|
|
952
|
-
const {
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
children
|
|
963
|
-
}) : /* @__PURE__ */jsx(Box, {
|
|
964
|
-
padding: 2,
|
|
965
|
-
children: /* @__PURE__ */jsx(Text, {
|
|
966
|
-
size: 1,
|
|
967
|
-
children: text
|
|
968
|
-
})
|
|
969
|
-
}),
|
|
970
|
-
children: /* @__PURE__ */jsx(TextWithTone, {
|
|
971
|
-
tone,
|
|
972
|
-
size: 1,
|
|
973
|
-
children: /* @__PURE__ */jsx(Icon, {})
|
|
974
|
-
})
|
|
975
|
-
});
|
|
684
|
+
const { text, icon, tone, children } = props;
|
|
685
|
+
return /* @__PURE__ */ jsx(
|
|
686
|
+
Tooltip,
|
|
687
|
+
{
|
|
688
|
+
animate: !0,
|
|
689
|
+
portal: !0,
|
|
690
|
+
content: children ? /* @__PURE__ */ jsx(Fragment, { children }) : /* @__PURE__ */ jsx(Box, { padding: 2, children: /* @__PURE__ */ jsx(Text, { size: 1, children: text }) }),
|
|
691
|
+
children: /* @__PURE__ */ jsx(TextWithTone, { tone, size: 1, children: /* @__PURE__ */ jsx(icon, {}) })
|
|
692
|
+
}
|
|
693
|
+
);
|
|
976
694
|
}
|
|
977
695
|
function Info() {
|
|
978
|
-
return /* @__PURE__ */jsx(InfoIcon, {
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
children: /* @__PURE__ */
|
|
982
|
-
|
|
983
|
-
space: 4,
|
|
984
|
-
style: {
|
|
985
|
-
maxWidth: 250
|
|
986
|
-
},
|
|
987
|
-
children: [/* @__PURE__ */jsx(Box, {
|
|
988
|
-
children: /* @__PURE__ */jsx(Text, {
|
|
989
|
-
size: 1,
|
|
990
|
-
children: "Bulk publishing uses the Scheduling API."
|
|
991
|
-
})
|
|
992
|
-
}), /* @__PURE__ */jsx(Box, {
|
|
993
|
-
children: /* @__PURE__ */jsx(Text, {
|
|
994
|
-
size: 1,
|
|
995
|
-
children: "Customized Document Actions in the Studio will not execute. Webhooks will execute."
|
|
996
|
-
})
|
|
997
|
-
}), /* @__PURE__ */jsx(Box, {
|
|
998
|
-
children: /* @__PURE__ */jsx(Text, {
|
|
999
|
-
size: 1,
|
|
1000
|
-
children: "Validation is checked before rendering the button below, but the Scheduling API will not check for \u2013 or enforce \u2013 validation."
|
|
1001
|
-
})
|
|
1002
|
-
})]
|
|
1003
|
-
})
|
|
1004
|
-
});
|
|
696
|
+
return /* @__PURE__ */ jsx(InfoIcon, { icon: InfoOutlineIcon, tone: "primary", children: /* @__PURE__ */ jsxs(Stack, { padding: 3, space: 4, style: { maxWidth: 250 }, children: [
|
|
697
|
+
/* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsx(Text, { size: 1, children: "Bulk publishing uses the Scheduling API." }) }),
|
|
698
|
+
/* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsx(Text, { size: 1, children: "Customized Document Actions in the Studio will not execute. Webhooks will execute." }) }),
|
|
699
|
+
/* @__PURE__ */ jsx(Box, { children: /* @__PURE__ */ jsx(Text, { size: 1, children: "Validation is checked before rendering the button below, but the Scheduling API will not check for \u2013 or enforce \u2013 validation." }) })
|
|
700
|
+
] }) });
|
|
1005
701
|
}
|
|
1006
702
|
function BulkPublish(props) {
|
|
1007
|
-
const {
|
|
1008
|
-
|
|
1009
|
-
} =
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
const [checkedIds, setCheckedIds] = useState([]);
|
|
1020
|
-
const onCheckComplete = useCallback(id => {
|
|
1021
|
-
setCheckedIds(ids => Array.from( /* @__PURE__ */new Set([...ids, id])));
|
|
1022
|
-
}, []);
|
|
1023
|
-
const [open, setOpen] = useState(false);
|
|
1024
|
-
const onOpen = useCallback(() => setOpen(true), []);
|
|
1025
|
-
const onClose = useCallback(() => setOpen(false), []);
|
|
1026
|
-
const addInvalidId = useCallback(id => {
|
|
1027
|
-
setInvalidIds(ids => ids ? Array.from( /* @__PURE__ */new Set([...ids, id])) : [id]);
|
|
1028
|
-
}, []);
|
|
1029
|
-
const removeInvalidId = useCallback(id => {
|
|
1030
|
-
setInvalidIds(ids => ids ? ids.filter(i => i !== id) : []);
|
|
1031
|
-
}, []);
|
|
1032
|
-
const [draftIds, setDraftIds] = useState([]);
|
|
1033
|
-
const addDraftId = useCallback(id => {
|
|
1034
|
-
setDraftIds(ids => Array.from( /* @__PURE__ */new Set([...ids, id])));
|
|
1035
|
-
}, []);
|
|
1036
|
-
const removeDraftId = useCallback(id => {
|
|
1037
|
-
setDraftIds(ids => ids.filter(i => i !== id));
|
|
1038
|
-
}, []);
|
|
1039
|
-
const handleBulkPublish = useCallback(() => {
|
|
1040
|
-
const body = translations.map(translation => ({
|
|
703
|
+
const { translations } = props, client = useClient({ apiVersion: API_VERSION }), { projectId, dataset } = useWorkspace(), toast = useToast(), [invalidIds, setInvalidIds] = useState(null), [checkedIds, setCheckedIds] = useState([]), onCheckComplete = useCallback((id) => {
|
|
704
|
+
setCheckedIds((ids) => Array.from(/* @__PURE__ */ new Set([...ids, id])));
|
|
705
|
+
}, []), [open, setOpen] = useState(!1), onOpen = useCallback(() => setOpen(!0), []), onClose = useCallback(() => setOpen(!1), []), addInvalidId = useCallback((id) => {
|
|
706
|
+
setInvalidIds((ids) => ids ? Array.from(/* @__PURE__ */ new Set([...ids, id])) : [id]);
|
|
707
|
+
}, []), removeInvalidId = useCallback((id) => {
|
|
708
|
+
setInvalidIds((ids) => ids ? ids.filter((i) => i !== id) : []);
|
|
709
|
+
}, []), [draftIds, setDraftIds] = useState([]), addDraftId = useCallback((id) => {
|
|
710
|
+
setDraftIds((ids) => Array.from(/* @__PURE__ */ new Set([...ids, id])));
|
|
711
|
+
}, []), removeDraftId = useCallback((id) => {
|
|
712
|
+
setDraftIds((ids) => ids.filter((i) => i !== id));
|
|
713
|
+
}, []), handleBulkPublish = useCallback(() => {
|
|
714
|
+
const body = translations.map((translation) => ({
|
|
1041
715
|
documentId: translation.value._ref
|
|
1042
716
|
}));
|
|
1043
717
|
client.request({
|
|
1044
|
-
uri:
|
|
718
|
+
uri: `/publish/${projectId}/${dataset}`,
|
|
1045
719
|
method: "POST",
|
|
1046
720
|
body
|
|
1047
721
|
}).then(() => {
|
|
@@ -1050,184 +724,157 @@ function BulkPublish(props) {
|
|
|
1050
724
|
title: "Success",
|
|
1051
725
|
description: "Bulk publish complete"
|
|
1052
726
|
});
|
|
1053
|
-
}).catch(err => {
|
|
1054
|
-
console.error(err)
|
|
1055
|
-
toast.push({
|
|
727
|
+
}).catch((err) => {
|
|
728
|
+
console.error(err), toast.push({
|
|
1056
729
|
status: "error",
|
|
1057
730
|
title: "Error",
|
|
1058
731
|
description: "Bulk publish failed"
|
|
1059
732
|
});
|
|
1060
733
|
});
|
|
1061
|
-
}, [translations, client, projectId, dataset, toast])
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
onClick: onOpen,
|
|
1085
|
-
text: "Prepare bulk publishing",
|
|
1086
|
-
mode: "ghost"
|
|
1087
|
-
})
|
|
1088
|
-
}), open && /* @__PURE__ */jsx(Dialog, {
|
|
734
|
+
}, [translations, client, projectId, dataset, toast]), disabled = (
|
|
735
|
+
// Not all documents have been checked
|
|
736
|
+
checkedIds.length !== translations.length || // Some document(s) are invalid
|
|
737
|
+
!!(invalidIds && (invalidIds == null ? void 0 : invalidIds.length) > 0) || // No documents are drafts
|
|
738
|
+
!draftIds.length
|
|
739
|
+
);
|
|
740
|
+
return (translations == null ? void 0 : translations.length) > 0 ? /* @__PURE__ */ jsx(Card, { padding: 4, border: !0, radius: 2, children: /* @__PURE__ */ jsxs(Stack, { space: 3, children: [
|
|
741
|
+
/* @__PURE__ */ jsxs(Inline, { space: 3, children: [
|
|
742
|
+
/* @__PURE__ */ jsx(Text, { weight: "bold", size: 1, children: "Bulk publishing" }),
|
|
743
|
+
/* @__PURE__ */ jsx(Info, {})
|
|
744
|
+
] }),
|
|
745
|
+
/* @__PURE__ */ jsx(Stack, { children: /* @__PURE__ */ jsx(
|
|
746
|
+
Button,
|
|
747
|
+
{
|
|
748
|
+
onClick: onOpen,
|
|
749
|
+
text: "Prepare bulk publishing",
|
|
750
|
+
mode: "ghost"
|
|
751
|
+
}
|
|
752
|
+
) }),
|
|
753
|
+
open && /* @__PURE__ */ jsx(
|
|
754
|
+
Dialog,
|
|
755
|
+
{
|
|
756
|
+
animate: !0,
|
|
1089
757
|
header: "Bulk publishing",
|
|
1090
758
|
id: "bulk-publish-dialog",
|
|
1091
759
|
onClose,
|
|
1092
760
|
zOffset: 1e3,
|
|
1093
761
|
width: 3,
|
|
1094
|
-
children: /* @__PURE__ */jsxs(Stack, {
|
|
1095
|
-
space:
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
}) : /* @__PURE__ */jsx(TextWithTone, {
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
})
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
var _a;
|
|
1115
|
-
return (_a = translation == null ? void 0 : translation.value) == null ? void 0 : _a._ref;
|
|
1116
|
-
}).map(translation => /* @__PURE__ */jsx(DocumentCheck, {
|
|
762
|
+
children: /* @__PURE__ */ jsxs(Stack, { space: 4, padding: 4, children: [
|
|
763
|
+
draftIds.length > 0 ? /* @__PURE__ */ jsxs(Stack, { space: 2, children: [
|
|
764
|
+
/* @__PURE__ */ jsxs(Text, { size: 1, children: [
|
|
765
|
+
"There",
|
|
766
|
+
" ",
|
|
767
|
+
draftIds.length === 1 ? "is 1 draft document" : `are ${draftIds.length} draft documents`,
|
|
768
|
+
"."
|
|
769
|
+
] }),
|
|
770
|
+
invalidIds && invalidIds.length > 0 ? /* @__PURE__ */ jsxs(TextWithTone, { tone: "critical", size: 1, children: [
|
|
771
|
+
invalidIds && invalidIds.length === 1 ? "1 draft document has" : `${invalidIds && invalidIds.length} draft documents have`,
|
|
772
|
+
" ",
|
|
773
|
+
"validation issues that must addressed first"
|
|
774
|
+
] }) : /* @__PURE__ */ jsx(TextWithTone, { tone: "positive", size: 1, children: "All drafts are valid and can be bulk published" })
|
|
775
|
+
] }) : null,
|
|
776
|
+
/* @__PURE__ */ jsx(Stack, { space: 1, children: translations.filter((translation) => {
|
|
777
|
+
var _a;
|
|
778
|
+
return (_a = translation == null ? void 0 : translation.value) == null ? void 0 : _a._ref;
|
|
779
|
+
}).map((translation) => /* @__PURE__ */ jsx(
|
|
780
|
+
DocumentCheck,
|
|
781
|
+
{
|
|
1117
782
|
id: translation.value._ref,
|
|
1118
783
|
onCheckComplete,
|
|
1119
784
|
addInvalidId,
|
|
1120
785
|
removeInvalidId,
|
|
1121
786
|
addDraftId,
|
|
1122
787
|
removeDraftId
|
|
1123
|
-
},
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
788
|
+
},
|
|
789
|
+
translation._key
|
|
790
|
+
)) }),
|
|
791
|
+
draftIds.length > 0 ? /* @__PURE__ */ jsx(
|
|
792
|
+
Button,
|
|
793
|
+
{
|
|
794
|
+
mode: "ghost",
|
|
795
|
+
tone: invalidIds && (invalidIds == null ? void 0 : invalidIds.length) > 0 ? "caution" : "positive",
|
|
796
|
+
text: draftIds.length === 1 ? "Publish draft document" : `Bulk publish ${draftIds.length} draft documents`,
|
|
797
|
+
onClick: handleBulkPublish,
|
|
798
|
+
disabled
|
|
799
|
+
}
|
|
800
|
+
) : /* @__PURE__ */ jsx(Text, { muted: !0, size: 1, children: "No draft documents to publish" })
|
|
801
|
+
] })
|
|
802
|
+
}
|
|
803
|
+
)
|
|
804
|
+
] }) }) : null;
|
|
1139
805
|
}
|
|
1140
806
|
function ReferencePatcher(props) {
|
|
1141
|
-
const {
|
|
1142
|
-
|
|
1143
|
-
documentType,
|
|
1144
|
-
metadataId
|
|
1145
|
-
} = props;
|
|
1146
|
-
const editState = useEditState(translation.value._ref, documentType);
|
|
1147
|
-
const client = useClient({
|
|
1148
|
-
apiVersion: API_VERSION
|
|
1149
|
-
});
|
|
1150
|
-
const {
|
|
1151
|
-
onChange
|
|
1152
|
-
} = useDocumentPane();
|
|
1153
|
-
useEffect(() => {
|
|
807
|
+
const { translation, documentType, metadataId } = props, editState = useEditState(translation.value._ref, documentType), client = useClient({ apiVersion: API_VERSION }), { onChange } = useDocumentPane();
|
|
808
|
+
return useEffect(() => {
|
|
1154
809
|
if (
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
onChange(
|
|
810
|
+
// We have a reference
|
|
811
|
+
translation.value._ref && // It's still weak and not-yet-strengthened
|
|
812
|
+
translation.value._weak && // We also want to keep this check because maybe the user *configured* weak refs
|
|
813
|
+
translation.value._strengthenOnPublish && // The referenced document has just been published
|
|
814
|
+
!editState.draft && editState.published && editState.ready
|
|
815
|
+
) {
|
|
816
|
+
const referencePathBase = [
|
|
817
|
+
"translations",
|
|
818
|
+
{ _key: translation._key },
|
|
819
|
+
"value"
|
|
820
|
+
];
|
|
821
|
+
onChange(
|
|
822
|
+
new PatchEvent([
|
|
823
|
+
unset([...referencePathBase, "_weak"]),
|
|
824
|
+
unset([...referencePathBase, "_strengthenOnPublish"])
|
|
825
|
+
])
|
|
826
|
+
);
|
|
1167
827
|
}
|
|
1168
|
-
}, [translation, editState, metadataId, client, onChange]);
|
|
1169
|
-
return null;
|
|
828
|
+
}, [translation, editState, metadataId, client, onChange]), null;
|
|
1170
829
|
}
|
|
1171
830
|
function OptimisticallyStrengthen(props) {
|
|
1172
|
-
const {
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
} = props;
|
|
1176
|
-
if (!translations.length) {
|
|
1177
|
-
return null;
|
|
1178
|
-
}
|
|
1179
|
-
return /* @__PURE__ */jsx(Fragment, {
|
|
1180
|
-
children: translations.map(translation => {
|
|
831
|
+
const { translations = [], metadataId } = props;
|
|
832
|
+
return translations.length ? /* @__PURE__ */ jsx(Fragment, { children: translations.map(
|
|
833
|
+
(translation) => {
|
|
1181
834
|
var _a;
|
|
1182
|
-
return (
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
835
|
+
return (_a = translation.value._strengthenOnPublish) != null && _a.type ? /* @__PURE__ */ jsx(
|
|
836
|
+
ReferencePatcher,
|
|
837
|
+
{
|
|
838
|
+
translation,
|
|
839
|
+
documentType: translation.value._strengthenOnPublish.type,
|
|
840
|
+
metadataId
|
|
841
|
+
},
|
|
842
|
+
translation._key
|
|
843
|
+
) : null;
|
|
844
|
+
}
|
|
845
|
+
) }) : null;
|
|
1189
846
|
}
|
|
1190
847
|
var metadata = (schemaTypes, metadataFields) => defineType({
|
|
1191
848
|
type: "document",
|
|
1192
849
|
name: METADATA_SCHEMA_NAME,
|
|
1193
850
|
title: "Translation metadata",
|
|
1194
851
|
icon: TranslateIcon,
|
|
1195
|
-
liveEdit:
|
|
1196
|
-
fields: [
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
readOnly: _ref2 => {
|
|
1212
|
-
let {
|
|
1213
|
-
value
|
|
1214
|
-
} = _ref2;
|
|
1215
|
-
return Boolean(value);
|
|
1216
|
-
}
|
|
1217
|
-
}), ...metadataFields],
|
|
852
|
+
liveEdit: !0,
|
|
853
|
+
fields: [
|
|
854
|
+
defineField({
|
|
855
|
+
name: TRANSLATIONS_ARRAY_NAME,
|
|
856
|
+
type: "internationalizedArrayReference"
|
|
857
|
+
}),
|
|
858
|
+
defineField({
|
|
859
|
+
name: "schemaTypes",
|
|
860
|
+
description: "Optional: Used to filter the reference fields above so all translations share the same types.",
|
|
861
|
+
type: "array",
|
|
862
|
+
of: [{ type: "string" }],
|
|
863
|
+
options: { list: schemaTypes },
|
|
864
|
+
readOnly: ({ value }) => !!value
|
|
865
|
+
}),
|
|
866
|
+
...metadataFields
|
|
867
|
+
],
|
|
1218
868
|
preview: {
|
|
1219
869
|
select: {
|
|
1220
870
|
translations: TRANSLATIONS_ARRAY_NAME,
|
|
1221
871
|
documentSchemaTypes: "schemaTypes"
|
|
1222
872
|
},
|
|
1223
873
|
prepare(selection) {
|
|
1224
|
-
const {
|
|
1225
|
-
|
|
1226
|
-
documentSchemaTypes
|
|
1227
|
-
|
|
1228
|
-
const title = translations.length === 1 ? "1 Translation" : "".concat(translations.length, " Translations");
|
|
1229
|
-
const languageKeys = translations.length ? translations.map(t => t._key.toUpperCase()).join(", ") : "";
|
|
1230
|
-
const subtitle = [languageKeys ? "(".concat(languageKeys, ")") : null, (documentSchemaTypes == null ? void 0 : documentSchemaTypes.length) ? documentSchemaTypes.map(s => s).join(", ") : ""].filter(Boolean).join(" ");
|
|
874
|
+
const { translations = [], documentSchemaTypes = [] } = selection, title = translations.length === 1 ? "1 Translation" : `${translations.length} Translations`, languageKeys = translations.length ? translations.map((t) => t._key.toUpperCase()).join(", ") : "", subtitle = [
|
|
875
|
+
languageKeys ? `(${languageKeys})` : null,
|
|
876
|
+
documentSchemaTypes != null && documentSchemaTypes.length ? documentSchemaTypes.map((s) => s).join(", ") : ""
|
|
877
|
+
].filter(Boolean).join(" ");
|
|
1231
878
|
return {
|
|
1232
879
|
title,
|
|
1233
880
|
subtitle
|
|
@@ -1235,212 +882,167 @@ var metadata = (schemaTypes, metadataFields) => defineType({
|
|
|
1235
882
|
}
|
|
1236
883
|
}
|
|
1237
884
|
});
|
|
1238
|
-
const documentInternationalization = definePlugin(
|
|
1239
|
-
|
|
1240
|
-
...DEFAULT_CONFIG,
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
components: {
|
|
1257
|
-
layout: props => DocumentInternationalizationProvider({
|
|
1258
|
-
...props,
|
|
1259
|
-
pluginConfig
|
|
1260
|
-
})
|
|
1261
|
-
}
|
|
1262
|
-
},
|
|
1263
|
-
// Adds:
|
|
1264
|
-
// - A bulk-publishing UI component to the form
|
|
1265
|
-
// - Will only work for projects on a compatible plan
|
|
1266
|
-
form: {
|
|
1267
|
-
components: {
|
|
1268
|
-
input: props => {
|
|
1269
|
-
var _a, _b, _c;
|
|
1270
|
-
if (props.id === "root" && props.schemaType.name === METADATA_SCHEMA_NAME && isSanityDocument(props == null ? void 0 : props.value)) {
|
|
1271
|
-
const metadataId = (_a = props == null ? void 0 : props.value) == null ? void 0 : _a._id;
|
|
1272
|
-
const translations = (_c = (_b = props == null ? void 0 : props.value) == null ? void 0 : _b.translations) != null ? _c : [];
|
|
1273
|
-
const weakAndTypedTranslations = translations.filter(_ref3 => {
|
|
1274
|
-
let {
|
|
1275
|
-
value
|
|
1276
|
-
} = _ref3;
|
|
1277
|
-
return value && value._weak && value._strengthenOnPublish;
|
|
1278
|
-
});
|
|
1279
|
-
return /* @__PURE__ */jsxs(Stack, {
|
|
1280
|
-
space: 5,
|
|
1281
|
-
children: [bulkPublish ? /* @__PURE__ */jsx(BulkPublish, {
|
|
1282
|
-
translations
|
|
1283
|
-
}) : null, weakAndTypedTranslations.length > 0 ? /* @__PURE__ */jsx(OptimisticallyStrengthen, {
|
|
1284
|
-
metadataId,
|
|
1285
|
-
translations: weakAndTypedTranslations
|
|
1286
|
-
}) : null, props.renderDefault(props)]
|
|
1287
|
-
});
|
|
1288
|
-
}
|
|
1289
|
-
return props.renderDefault(props);
|
|
885
|
+
const documentInternationalization = definePlugin(
|
|
886
|
+
(config) => {
|
|
887
|
+
const pluginConfig = { ...DEFAULT_CONFIG, ...config }, {
|
|
888
|
+
supportedLanguages,
|
|
889
|
+
schemaTypes,
|
|
890
|
+
languageField,
|
|
891
|
+
bulkPublish,
|
|
892
|
+
metadataFields
|
|
893
|
+
} = pluginConfig;
|
|
894
|
+
if (schemaTypes.length === 0)
|
|
895
|
+
throw new Error(
|
|
896
|
+
"You must specify at least one schema type on which to enable document internationalization. Update the `schemaTypes` option in the documentInternationalization() configuration."
|
|
897
|
+
);
|
|
898
|
+
return {
|
|
899
|
+
name: "@sanity/document-internationalization",
|
|
900
|
+
studio: {
|
|
901
|
+
components: {
|
|
902
|
+
layout: (props) => DocumentInternationalizationProvider({ ...props, pluginConfig })
|
|
1290
903
|
}
|
|
1291
|
-
}
|
|
1292
|
-
},
|
|
1293
|
-
// Adds:
|
|
1294
|
-
// - The `Translations` dropdown to the editing form
|
|
1295
|
-
// - `Badges` to documents with a language value
|
|
1296
|
-
// - The `DeleteMetadataAction` action to the metadata document type
|
|
1297
|
-
document: {
|
|
1298
|
-
unstable_languageFilter: (prev, ctx) => {
|
|
1299
|
-
const {
|
|
1300
|
-
schemaType,
|
|
1301
|
-
documentId
|
|
1302
|
-
} = ctx;
|
|
1303
|
-
return schemaTypes.includes(schemaType) && documentId ? [...prev, props => DocumentInternationalizationMenu({
|
|
1304
|
-
...props,
|
|
1305
|
-
documentId
|
|
1306
|
-
})] : prev;
|
|
1307
904
|
},
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
905
|
+
// Adds:
|
|
906
|
+
// - A bulk-publishing UI component to the form
|
|
907
|
+
// - Will only work for projects on a compatible plan
|
|
908
|
+
form: {
|
|
909
|
+
components: {
|
|
910
|
+
input: (props) => {
|
|
911
|
+
var _a, _b, _c;
|
|
912
|
+
if (props.id === "root" && props.schemaType.name === METADATA_SCHEMA_NAME && isSanityDocument(props == null ? void 0 : props.value)) {
|
|
913
|
+
const metadataId = (_a = props == null ? void 0 : props.value) == null ? void 0 : _a._id, translations = (_c = (_b = props == null ? void 0 : props.value) == null ? void 0 : _b.translations) != null ? _c : [], weakAndTypedTranslations = translations.filter(
|
|
914
|
+
({ value }) => (value == null ? void 0 : value._weak) && value._strengthenOnPublish
|
|
915
|
+
);
|
|
916
|
+
return /* @__PURE__ */ jsxs(Stack, { space: 5, children: [
|
|
917
|
+
bulkPublish ? /* @__PURE__ */ jsx(BulkPublish, { translations }) : null,
|
|
918
|
+
weakAndTypedTranslations.length > 0 ? /* @__PURE__ */ jsx(
|
|
919
|
+
OptimisticallyStrengthen,
|
|
920
|
+
{
|
|
921
|
+
metadataId,
|
|
922
|
+
translations: weakAndTypedTranslations
|
|
923
|
+
}
|
|
924
|
+
) : null,
|
|
925
|
+
props.renderDefault(props)
|
|
926
|
+
] });
|
|
927
|
+
}
|
|
928
|
+
return props.renderDefault(props);
|
|
929
|
+
}
|
|
1314
930
|
}
|
|
1315
|
-
return [props => LanguageBadge(props), ...prev];
|
|
1316
931
|
},
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
//
|
|
1333
|
-
//
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
value: _ref7 => {
|
|
1353
|
-
let {
|
|
1354
|
-
languageId
|
|
1355
|
-
} = _ref7;
|
|
1356
|
-
return {
|
|
932
|
+
// Adds:
|
|
933
|
+
// - The `Translations` dropdown to the editing form
|
|
934
|
+
// - `Badges` to documents with a language value
|
|
935
|
+
// - The `DeleteMetadataAction` action to the metadata document type
|
|
936
|
+
document: {
|
|
937
|
+
unstable_languageFilter: (prev, ctx) => {
|
|
938
|
+
const { schemaType, documentId } = ctx;
|
|
939
|
+
return schemaTypes.includes(schemaType) && documentId ? [
|
|
940
|
+
...prev,
|
|
941
|
+
(props) => DocumentInternationalizationMenu({ ...props, documentId })
|
|
942
|
+
] : prev;
|
|
943
|
+
},
|
|
944
|
+
badges: (prev, { schemaType }) => schemaTypes.includes(schemaType) ? [(props) => LanguageBadge(props), ...prev] : prev,
|
|
945
|
+
actions: (prev, { schemaType }) => schemaType === METADATA_SCHEMA_NAME ? [...prev, DeleteMetadataAction] : prev
|
|
946
|
+
},
|
|
947
|
+
// Adds:
|
|
948
|
+
// - The `Translations metadata` document type to the schema
|
|
949
|
+
schema: {
|
|
950
|
+
// Create the metadata document type
|
|
951
|
+
types: [metadata(schemaTypes, metadataFields)],
|
|
952
|
+
// For every schema type this plugin is enabled on
|
|
953
|
+
// Create an initial value template to set the language
|
|
954
|
+
templates: (prev, { schema }) => {
|
|
955
|
+
if (!Array.isArray(supportedLanguages))
|
|
956
|
+
return prev;
|
|
957
|
+
const parameterizedTemplates = schemaTypes.map((schemaType) => {
|
|
958
|
+
var _a, _b;
|
|
959
|
+
return {
|
|
960
|
+
id: `${schemaType}-parameterized`,
|
|
961
|
+
title: `${(_b = (_a = schema == null ? void 0 : schema.get(schemaType)) == null ? void 0 : _a.title) != null ? _b : schemaType}: with Language`,
|
|
962
|
+
schemaType,
|
|
963
|
+
parameters: [
|
|
964
|
+
{ name: "languageId", title: "Language ID", type: "string" }
|
|
965
|
+
],
|
|
966
|
+
value: ({ languageId }) => ({
|
|
1357
967
|
[languageField]: languageId
|
|
1358
|
-
}
|
|
1359
|
-
}
|
|
1360
|
-
}
|
|
1361
|
-
});
|
|
1362
|
-
const staticTemplates = schemaTypes.flatMap(schemaType => {
|
|
1363
|
-
return supportedLanguages.map(language => {
|
|
968
|
+
})
|
|
969
|
+
};
|
|
970
|
+
}), staticTemplates = schemaTypes.flatMap((schemaType) => supportedLanguages.map((language) => {
|
|
1364
971
|
var _a, _b;
|
|
1365
972
|
return {
|
|
1366
|
-
id:
|
|
1367
|
-
title:
|
|
973
|
+
id: `${schemaType}-${language.id}`,
|
|
974
|
+
title: `${language.title} ${(_b = (_a = schema == null ? void 0 : schema.get(schemaType)) == null ? void 0 : _a.title) != null ? _b : schemaType}`,
|
|
1368
975
|
schemaType,
|
|
1369
976
|
value: {
|
|
1370
977
|
[languageField]: language.id
|
|
1371
978
|
}
|
|
1372
979
|
};
|
|
1373
|
-
});
|
|
1374
|
-
|
|
1375
|
-
return [...prev, ...parameterizedTemplates, ...staticTemplates];
|
|
1376
|
-
}
|
|
1377
|
-
},
|
|
1378
|
-
// Uses:
|
|
1379
|
-
// - `sanity-plugin-internationalized-array` to maintain the translations array
|
|
1380
|
-
plugins: [
|
|
1381
|
-
// Translation metadata stores its references using this plugin
|
|
1382
|
-
// It cuts down on attribute usage and gives UI conveniences to add new translations
|
|
1383
|
-
internationalizedArray({
|
|
1384
|
-
languages: supportedLanguages,
|
|
1385
|
-
fieldTypes: [defineField({
|
|
1386
|
-
name: "reference",
|
|
1387
|
-
type: "reference",
|
|
1388
|
-
to: schemaTypes.map(type => ({
|
|
1389
|
-
type
|
|
1390
|
-
})),
|
|
1391
|
-
weak: pluginConfig.weakReferences,
|
|
1392
|
-
// Reference filters don't actually enforce validation!
|
|
1393
|
-
validation: Rule => Rule.custom(async (item, context) => {
|
|
1394
|
-
var _a;
|
|
1395
|
-
if (!((_a = item == null ? void 0 : item.value) == null ? void 0 : _a._ref) || !(item == null ? void 0 : item._key)) {
|
|
1396
|
-
return true;
|
|
1397
|
-
}
|
|
1398
|
-
const client = context.getClient({
|
|
1399
|
-
apiVersion: API_VERSION
|
|
1400
|
-
});
|
|
1401
|
-
const valueLanguage = await client.fetch("*[_id in [$ref, $draftRef]][0].".concat(languageField), {
|
|
1402
|
-
ref: item.value._ref,
|
|
1403
|
-
draftRef: "drafts.".concat(item.value._ref)
|
|
1404
|
-
});
|
|
1405
|
-
if (valueLanguage && valueLanguage === item._key) {
|
|
1406
|
-
return true;
|
|
1407
|
-
}
|
|
1408
|
-
return "Referenced document does not have the correct language value";
|
|
1409
|
-
}),
|
|
1410
|
-
options: {
|
|
1411
|
-
// TODO: Update type once it knows the values of this filter
|
|
1412
|
-
// @ts-expect-error
|
|
1413
|
-
filter: _ref8 => {
|
|
1414
|
-
let {
|
|
1415
|
-
parent,
|
|
1416
|
-
document
|
|
1417
|
-
} = _ref8;
|
|
1418
|
-
if (!parent) return null;
|
|
1419
|
-
const parentArray = Array.isArray(parent) ? parent : [parent];
|
|
1420
|
-
const language = parentArray.find(p => p._key);
|
|
1421
|
-
if (!(language == null ? void 0 : language._key)) return null;
|
|
1422
|
-
if (document.schemaTypes) {
|
|
1423
|
-
return {
|
|
1424
|
-
filter: "_type in $schemaTypes && ".concat(languageField, " == $language"),
|
|
1425
|
-
params: {
|
|
1426
|
-
schemaTypes: document.schemaTypes,
|
|
1427
|
-
language: language._key
|
|
1428
|
-
}
|
|
1429
|
-
};
|
|
1430
|
-
}
|
|
1431
|
-
return {
|
|
1432
|
-
filter: "".concat(languageField, " == $language"),
|
|
1433
|
-
params: {
|
|
1434
|
-
language: language._key
|
|
1435
|
-
}
|
|
1436
|
-
};
|
|
1437
|
-
}
|
|
980
|
+
}));
|
|
981
|
+
return [...prev, ...parameterizedTemplates, ...staticTemplates];
|
|
1438
982
|
}
|
|
1439
|
-
},
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
983
|
+
},
|
|
984
|
+
// Uses:
|
|
985
|
+
// - `sanity-plugin-internationalized-array` to maintain the translations array
|
|
986
|
+
plugins: [
|
|
987
|
+
// Translation metadata stores its references using this plugin
|
|
988
|
+
// It cuts down on attribute usage and gives UI conveniences to add new translations
|
|
989
|
+
internationalizedArray({
|
|
990
|
+
languages: supportedLanguages,
|
|
991
|
+
fieldTypes: [
|
|
992
|
+
defineField(
|
|
993
|
+
{
|
|
994
|
+
name: "reference",
|
|
995
|
+
type: "reference",
|
|
996
|
+
to: schemaTypes.map((type) => ({ type })),
|
|
997
|
+
weak: pluginConfig.weakReferences,
|
|
998
|
+
// Reference filters don't actually enforce validation!
|
|
999
|
+
validation: (Rule) => (
|
|
1000
|
+
// @ts-expect-error - fix typings
|
|
1001
|
+
Rule.custom(async (item, context) => {
|
|
1002
|
+
var _a;
|
|
1003
|
+
if (!((_a = item == null ? void 0 : item.value) != null && _a._ref) || !(item != null && item._key))
|
|
1004
|
+
return !0;
|
|
1005
|
+
const valueLanguage = await context.getClient({ apiVersion: API_VERSION }).fetch(
|
|
1006
|
+
`*[_id in [$ref, $draftRef]][0].${languageField}`,
|
|
1007
|
+
{
|
|
1008
|
+
ref: item.value._ref,
|
|
1009
|
+
draftRef: `drafts.${item.value._ref}`
|
|
1010
|
+
}
|
|
1011
|
+
);
|
|
1012
|
+
return valueLanguage && valueLanguage === item._key ? !0 : "Referenced document does not have the correct language value";
|
|
1013
|
+
})
|
|
1014
|
+
),
|
|
1015
|
+
options: {
|
|
1016
|
+
// @ts-expect-error - Update type once it knows the values of this filter
|
|
1017
|
+
filter: ({ parent, document }) => {
|
|
1018
|
+
if (!parent)
|
|
1019
|
+
return null;
|
|
1020
|
+
const language = (Array.isArray(parent) ? parent : [parent]).find((p) => p._key);
|
|
1021
|
+
return language != null && language._key ? document.schemaTypes ? {
|
|
1022
|
+
filter: `_type in $schemaTypes && ${languageField} == $language`,
|
|
1023
|
+
params: {
|
|
1024
|
+
schemaTypes: document.schemaTypes,
|
|
1025
|
+
language: language._key
|
|
1026
|
+
}
|
|
1027
|
+
} : {
|
|
1028
|
+
filter: `${languageField} == $language`,
|
|
1029
|
+
params: { language: language._key }
|
|
1030
|
+
} : null;
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
},
|
|
1034
|
+
{ strict: !1 }
|
|
1035
|
+
)
|
|
1036
|
+
]
|
|
1037
|
+
})
|
|
1038
|
+
]
|
|
1039
|
+
};
|
|
1040
|
+
}
|
|
1041
|
+
);
|
|
1042
|
+
export {
|
|
1043
|
+
DeleteTranslationAction,
|
|
1044
|
+
DocumentInternationalizationMenu,
|
|
1045
|
+
documentInternationalization,
|
|
1046
|
+
useDocumentInternationalizationContext
|
|
1047
|
+
};
|
|
1446
1048
|
//# sourceMappingURL=index.esm.js.map
|