@sanity/assist 1.2.15-lang.6 → 1.2.15-lang.7
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.cjs.mjs +1 -0
- package/dist/index.d.ts +16 -12
- package/dist/index.esm.js +118 -331
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +117 -329
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/assistLayout/AssistLayout.tsx +0 -3
- package/src/index.ts +3 -0
- package/src/node_modules/.vitest/results.json +1 -1
- package/src/plugin.tsx +0 -5
- package/src/translate/FieldTranslationProvider.tsx +54 -28
- package/src/translate/paths.test.ts +8 -7
- package/src/translate/paths.ts +18 -5
- package/src/translate/types.ts +1 -0
- package/src/assistLayout/AlphaMigration.tsx +0 -310
- package/src/legacy-types.ts +0 -72
package/dist/index.cjs.mjs
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -21,10 +21,6 @@ export declare interface AssistOptions {
|
|
|
21
21
|
|
|
22
22
|
declare interface AssistPluginConfig {
|
|
23
23
|
translate?: TranslationConfig
|
|
24
|
-
/**
|
|
25
|
-
* Set this to false to disable model migration from the alpha version of this plugin
|
|
26
|
-
*/
|
|
27
|
-
alphaMigration?: boolean
|
|
28
24
|
/**
|
|
29
25
|
* @internal
|
|
30
26
|
*/
|
|
@@ -33,13 +29,21 @@ declare interface AssistPluginConfig {
|
|
|
33
29
|
|
|
34
30
|
export declare const contextDocumentTypeName: 'assist.instruction.context'
|
|
35
31
|
|
|
36
|
-
|
|
32
|
+
/**
|
|
33
|
+
* Default implementation for plugin config `translate.field.translationOutputs`
|
|
34
|
+
*
|
|
35
|
+
* @see FieldTranslationConfig#translationOutputs
|
|
36
|
+
*/
|
|
37
|
+
export declare const defaultLanguageOutputs: TranslationOutputsFunction
|
|
38
|
+
|
|
39
|
+
export declare interface DocumentMember {
|
|
37
40
|
schemaType: SchemaType
|
|
38
41
|
path: Path
|
|
39
42
|
name: string
|
|
43
|
+
value: unknown
|
|
40
44
|
}
|
|
41
45
|
|
|
42
|
-
declare interface DocumentTranslationConfig {
|
|
46
|
+
export declare interface DocumentTranslationConfig {
|
|
43
47
|
/**
|
|
44
48
|
* Path to language field in documents. Can be a hidden field.
|
|
45
49
|
* For instance: 'config.language'
|
|
@@ -61,7 +65,7 @@ declare interface DocumentTranslationConfig {
|
|
|
61
65
|
documentTypes?: string[]
|
|
62
66
|
}
|
|
63
67
|
|
|
64
|
-
declare interface FieldTranslationConfig {
|
|
68
|
+
export declare interface FieldTranslationConfig {
|
|
65
69
|
/**
|
|
66
70
|
* `documentTypes` should be an array of strings where each entry must match a name from your document schemas.
|
|
67
71
|
*
|
|
@@ -152,19 +156,19 @@ declare interface FieldTranslationConfig {
|
|
|
152
156
|
translationOutputs?: TranslationOutputsFunction
|
|
153
157
|
}
|
|
154
158
|
|
|
155
|
-
declare interface Language {
|
|
159
|
+
export declare interface Language {
|
|
156
160
|
id: string
|
|
157
161
|
title?: string
|
|
158
162
|
}
|
|
159
163
|
|
|
160
|
-
declare type LanguageCallback = (
|
|
164
|
+
export declare type LanguageCallback = (
|
|
161
165
|
client: SanityClient,
|
|
162
166
|
selectedLanguageParams: Record<string, unknown>,
|
|
163
167
|
) => Promise<Language[]>
|
|
164
168
|
|
|
165
169
|
export declare function SchemaTypeTool(): JSX_2.Element
|
|
166
170
|
|
|
167
|
-
declare interface TranslationConfig {
|
|
171
|
+
export declare interface TranslationConfig {
|
|
168
172
|
/**
|
|
169
173
|
* Config for document types with fields in multiple languages in the same document.
|
|
170
174
|
*/
|
|
@@ -175,13 +179,13 @@ declare interface TranslationConfig {
|
|
|
175
179
|
document?: DocumentTranslationConfig
|
|
176
180
|
}
|
|
177
181
|
|
|
178
|
-
declare interface TranslationOutput {
|
|
182
|
+
export declare interface TranslationOutput {
|
|
179
183
|
/** Language id */
|
|
180
184
|
id: string
|
|
181
185
|
outputPath: Path
|
|
182
186
|
}
|
|
183
187
|
|
|
184
|
-
declare type TranslationOutputsFunction = (
|
|
188
|
+
export declare type TranslationOutputsFunction = (
|
|
185
189
|
documentMember: DocumentMember,
|
|
186
190
|
enclosingType: SchemaType,
|
|
187
191
|
translateFromLanguageId: string,
|
package/dist/index.esm.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
|
|
2
|
-
import { useClient, isArraySchemaType, typed, useSchema, useDocumentStore, useDocumentPresence, createPatchChannel, FormBuilder, fromMutationPatches,
|
|
2
|
+
import { useClient, isArraySchemaType, typed, useSchema, isKeySegment, isDocumentSchemaType, pathToString, useDocumentStore, useDocumentPresence, createPatchChannel, FormBuilder, fromMutationPatches, isObjectSchemaType, stringToPath as stringToPath$2, useEditState, useCurrentUser, StatusButton, FormFieldHeaderText, PatchEvent, unset, set, useFormCallbacks, FormCallbacksProvider, FormInput, setIfMissing, insert, PresenceOverlay, VirtualizerScrollInstanceProvider, useColorSchemeValue, ObjectInputMember, defineType, defineField, defineArrayMember, isArrayOfObjectsSchemaType, getPublishedId, useSyncState, definePlugin } from 'sanity';
|
|
3
3
|
import { Card, Stack, Box, Button, Spinner, Flex, Label, focusFirstDescendant, Text, useClickOutside, Popover, useLayer, useGlobalKeyDown, useToast, Dialog, Tooltip, TextArea, Container, Autocomplete, Breadcrumbs, Badge, useTheme, rgba, Radio, Checkbox, ThemeProvider, ErrorBoundary, Switch, MenuButton, Menu, MenuItem } from '@sanity/ui';
|
|
4
4
|
import { useState, useRef, useEffect, useMemo, useCallback, createContext, useReducer, forwardRef, createElement, useContext, useId } from 'react';
|
|
5
5
|
import { SyncIcon, DocumentIcon, LinkIcon, ImageIcon, BlockContentIcon, OlistIcon, BlockquoteIcon, StringIcon, ErrorOutlineIcon, CheckmarkCircleIcon, CloseCircleIcon, ClockIcon, PlayIcon, SparklesIcon, ArrowLeftIcon, SearchIcon, RetryIcon, ArrowRightIcon, CloseIcon, CheckmarkIcon, icons, TokenIcon, DocumentTextIcon, ThListIcon, CodeIcon, ComposeIcon, LockIcon, TranslateIcon, ControlsIcon } from '@sanity/icons';
|
|
@@ -7,8 +7,8 @@ import { mergeMap, share, take, filter, distinctUntilChanged, catchError, tap }
|
|
|
7
7
|
import isEqual from 'react-fast-compare';
|
|
8
8
|
import { throwError, of, partition, merge, switchMap, delay, defer } from 'rxjs';
|
|
9
9
|
import { exhaustMapToWithTrailing } from 'rxjs-exhaustmap-with-trailing';
|
|
10
|
-
import { useDocumentPane, usePaneRouter, DocumentInspectorHeader, DocumentPaneProvider } from 'sanity/desk';
|
|
11
10
|
import { extractWithPath } from '@sanity/mutator';
|
|
11
|
+
import { useDocumentPane, usePaneRouter, DocumentInspectorHeader, DocumentPaneProvider } from 'sanity/desk';
|
|
12
12
|
import styled, { keyframes } from 'styled-components';
|
|
13
13
|
import { minutesToMilliseconds, isAfter, addSeconds } from 'date-fns';
|
|
14
14
|
import formatDistanceToNow from 'date-fns/formatDistanceToNow';
|
|
@@ -393,6 +393,101 @@ function SchemaEntry(_ref) {
|
|
|
393
393
|
children: out
|
|
394
394
|
});
|
|
395
395
|
}
|
|
396
|
+
const MAX_DEPTH = 6;
|
|
397
|
+
function getDocumentMembersFlat(doc, schemaType) {
|
|
398
|
+
if (!isDocumentSchemaType(schemaType)) {
|
|
399
|
+
console.error("Schema type is not a document");
|
|
400
|
+
return [];
|
|
401
|
+
}
|
|
402
|
+
return extractPaths(doc, schemaType, [], MAX_DEPTH);
|
|
403
|
+
}
|
|
404
|
+
function extractPaths(doc, schemaType, path, maxDepth) {
|
|
405
|
+
if (path.length >= maxDepth) {
|
|
406
|
+
return [];
|
|
407
|
+
}
|
|
408
|
+
return schemaType.fields.reduce((acc, field) => {
|
|
409
|
+
var _a, _b;
|
|
410
|
+
const fieldPath = [...path, field.name];
|
|
411
|
+
const fieldSchema = field.type;
|
|
412
|
+
const {
|
|
413
|
+
value
|
|
414
|
+
} = (_a = extractWithPath(pathToString(fieldPath), doc)[0]) != null ? _a : {};
|
|
415
|
+
if (!value) {
|
|
416
|
+
return acc;
|
|
417
|
+
}
|
|
418
|
+
const thisFieldWithPath = {
|
|
419
|
+
path: fieldPath,
|
|
420
|
+
name: field.name,
|
|
421
|
+
schemaType: fieldSchema,
|
|
422
|
+
value
|
|
423
|
+
};
|
|
424
|
+
if (fieldSchema.jsonType === "object") {
|
|
425
|
+
const innerFields = extractPaths(doc, fieldSchema, fieldPath, maxDepth);
|
|
426
|
+
return [...acc, thisFieldWithPath, ...innerFields];
|
|
427
|
+
} else if (fieldSchema.jsonType === "array" && fieldSchema.of.length && fieldSchema.of.some(item => "fields" in item)) {
|
|
428
|
+
const {
|
|
429
|
+
value: arrayValue
|
|
430
|
+
} = (_b = extractWithPath(pathToString(fieldPath), doc)[0]) != null ? _b : {};
|
|
431
|
+
let arrayPaths = [];
|
|
432
|
+
if (arrayValue == null ? void 0 : arrayValue.length) {
|
|
433
|
+
for (const item of arrayValue) {
|
|
434
|
+
const arrayItemSchema = fieldSchema.of.find(t => t.name === item._type);
|
|
435
|
+
if (item._key && arrayItemSchema) {
|
|
436
|
+
const itemPath = [...fieldPath, {
|
|
437
|
+
_key: item._key
|
|
438
|
+
}];
|
|
439
|
+
const innerFields = extractPaths(doc, arrayItemSchema, itemPath, maxDepth);
|
|
440
|
+
const arrayMember = {
|
|
441
|
+
path: itemPath,
|
|
442
|
+
name: item._key,
|
|
443
|
+
schemaType: arrayItemSchema,
|
|
444
|
+
value: item
|
|
445
|
+
};
|
|
446
|
+
arrayPaths = [...arrayPaths, arrayMember, ...innerFields];
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
return [...acc, thisFieldWithPath, ...arrayPaths];
|
|
451
|
+
}
|
|
452
|
+
return [...acc, thisFieldWithPath];
|
|
453
|
+
}, []);
|
|
454
|
+
}
|
|
455
|
+
const defaultLanguageOutputs = function (member, enclosingType, translateFromLanguageId, translateToLanguageIds) {
|
|
456
|
+
if (member.schemaType.jsonType === "object" && member.schemaType.name.startsWith("internationalizedArray")) {
|
|
457
|
+
const pathEnd = member.path.slice(-1);
|
|
458
|
+
const language = isKeySegment(pathEnd[0]) ? pathEnd[0]._key : null;
|
|
459
|
+
return language === translateFromLanguageId ? translateToLanguageIds.map(translateToId => ({
|
|
460
|
+
id: translateToId,
|
|
461
|
+
outputPath: [...member.path.slice(0, -1), {
|
|
462
|
+
_key: translateToId
|
|
463
|
+
}]
|
|
464
|
+
})) : void 0;
|
|
465
|
+
}
|
|
466
|
+
if (enclosingType.jsonType === "object" && enclosingType.name.startsWith("locale")) {
|
|
467
|
+
return translateFromLanguageId === member.name ? translateToLanguageIds.map(translateToId => ({
|
|
468
|
+
id: translateToId,
|
|
469
|
+
outputPath: [...member.path.slice(0, -1), translateToId]
|
|
470
|
+
})) : void 0;
|
|
471
|
+
}
|
|
472
|
+
return void 0;
|
|
473
|
+
};
|
|
474
|
+
function getFieldLanguageMap(documentSchema, documentMembers, translateFromLanguageId, outputLanguageIds, langFn) {
|
|
475
|
+
var _a, _b, _c;
|
|
476
|
+
const translationMaps = [];
|
|
477
|
+
for (const member of documentMembers) {
|
|
478
|
+
const parentPath = member.path.slice(0, -1);
|
|
479
|
+
const enclosingType = (_b = (_a = documentMembers.find(m => pathToString(m.path) === pathToString(parentPath))) == null ? void 0 : _a.schemaType) != null ? _b : documentSchema;
|
|
480
|
+
const translations = (_c = langFn(member, enclosingType, translateFromLanguageId, outputLanguageIds)) == null ? void 0 : _c.filter(translation => translation.id !== translateFromLanguageId);
|
|
481
|
+
if (translations) {
|
|
482
|
+
translationMaps.push({
|
|
483
|
+
inputLanguageId: translateFromLanguageId,
|
|
484
|
+
inputPath: member.path,
|
|
485
|
+
outputs: translations
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return translationMaps;
|
|
490
|
+
}
|
|
396
491
|
const aiInspectorId = "ai-assistance";
|
|
397
492
|
const preventDefault = ev => ev.preventDefault();
|
|
398
493
|
function DocumentForm(props) {
|
|
@@ -3093,322 +3188,6 @@ function AssistConnectorsOverlay(props) {
|
|
|
3093
3188
|
}), DEBUG]
|
|
3094
3189
|
});
|
|
3095
3190
|
}
|
|
3096
|
-
const legacyAssistDocumentIdPrefix = "sanity.ai.";
|
|
3097
|
-
const legacyAssistDocumentTypeName = "sanity.ai.docType";
|
|
3098
|
-
const legacyContextDocumentTypeName = "ai.instruction.context";
|
|
3099
|
-
const legacyAssistStatusDocumentTypeName = "sanity.ai.instructionStatus";
|
|
3100
|
-
const NO_ASSIST_DOCS = [];
|
|
3101
|
-
const NO_CONTEXT_DOCS = [];
|
|
3102
|
-
const NO_IDS = [];
|
|
3103
|
-
function AlphaMigration() {
|
|
3104
|
-
const [alphaAssistDocs, setAlphaAssistDocs] = useState(NO_ASSIST_DOCS);
|
|
3105
|
-
const [contextDocs, setContextDocs] = useState(NO_CONTEXT_DOCS);
|
|
3106
|
-
const [staleStatusDocIds, setStaleStatusDocs] = useState(NO_IDS);
|
|
3107
|
-
const [error, setError] = useState(void 0);
|
|
3108
|
-
const [progress, setProgress] = useState(void 0);
|
|
3109
|
-
const toast = useToast();
|
|
3110
|
-
const client = useClient({
|
|
3111
|
-
apiVersion: "2023-06-01"
|
|
3112
|
-
});
|
|
3113
|
-
useEffect(() => {
|
|
3114
|
-
let canUpdate = true;
|
|
3115
|
-
client.fetch('\n {\n "assistDocs": *[_type=="'.concat(legacyAssistDocumentTypeName, '"],\n "staleStatusDocIds": *[_type=="').concat(legacyAssistStatusDocumentTypeName, '"]._id,\n "contextDocs": *[_type=="').concat(legacyContextDocumentTypeName, '"],\n }\n ')).then(result => {
|
|
3116
|
-
var _a, _b, _c;
|
|
3117
|
-
if (!canUpdate || !result) {
|
|
3118
|
-
return;
|
|
3119
|
-
}
|
|
3120
|
-
setAlphaAssistDocs((_a = result == null ? void 0 : result.assistDocs) != null ? _a : NO_ASSIST_DOCS);
|
|
3121
|
-
setStaleStatusDocs((_b = result == null ? void 0 : result.staleStatusDocIds) != null ? _b : NO_IDS);
|
|
3122
|
-
setContextDocs((_c = result == null ? void 0 : result.contextDocs) != null ? _c : NO_CONTEXT_DOCS);
|
|
3123
|
-
});
|
|
3124
|
-
return () => {
|
|
3125
|
-
canUpdate = false;
|
|
3126
|
-
};
|
|
3127
|
-
}, [client, setAlphaAssistDocs, setStaleStatusDocs, setContextDocs]);
|
|
3128
|
-
const convert = useCallback(async () => {
|
|
3129
|
-
try {
|
|
3130
|
-
setProgress(1e-4);
|
|
3131
|
-
const tasks = [() => convertContextDocs(client, contextDocs), subtaskProgress => deleteDocs(client, staleStatusDocIds, subtaskProgress), subtaskProgress => convertDocs(client, alphaAssistDocs, subtaskProgress), subtaskProgress => deleteDocs(client, contextDocs.map(d => d._id), subtaskProgress)];
|
|
3132
|
-
const taskSize = 1 / tasks.length;
|
|
3133
|
-
for (let i = 0; i < tasks.length; i++) {
|
|
3134
|
-
const startProgress = i / tasks.length;
|
|
3135
|
-
await tasks[i](subProgress => setProgress(startProgress + subProgress * taskSize));
|
|
3136
|
-
setProgress((i + 1) / tasks.length);
|
|
3137
|
-
}
|
|
3138
|
-
setProgress(1);
|
|
3139
|
-
setAlphaAssistDocs(NO_ASSIST_DOCS);
|
|
3140
|
-
setContextDocs(NO_CONTEXT_DOCS);
|
|
3141
|
-
setStaleStatusDocs(NO_IDS);
|
|
3142
|
-
toast.push({
|
|
3143
|
-
title: "Converted instructions to new format.",
|
|
3144
|
-
status: "success",
|
|
3145
|
-
closable: true
|
|
3146
|
-
});
|
|
3147
|
-
} catch (e) {
|
|
3148
|
-
console.error(e);
|
|
3149
|
-
toast.push({
|
|
3150
|
-
title: "An error occurred",
|
|
3151
|
-
status: "error",
|
|
3152
|
-
closable: true
|
|
3153
|
-
});
|
|
3154
|
-
setError(e);
|
|
3155
|
-
setProgress(void 0);
|
|
3156
|
-
}
|
|
3157
|
-
}, [contextDocs, client, alphaAssistDocs, staleStatusDocIds, setProgress, toast]);
|
|
3158
|
-
if ((alphaAssistDocs.length || staleStatusDocIds.length || contextDocs.length) && (!progress || progress < 1)) {
|
|
3159
|
-
return /* @__PURE__ */jsx(Dialog, {
|
|
3160
|
-
id: "outdated-assist-docs",
|
|
3161
|
-
header: pluginTitle,
|
|
3162
|
-
children: /* @__PURE__ */jsx(Card, {
|
|
3163
|
-
padding: 3,
|
|
3164
|
-
children: /* @__PURE__ */jsxs(Stack, {
|
|
3165
|
-
space: 4,
|
|
3166
|
-
style: {
|
|
3167
|
-
maxWidth: 500
|
|
3168
|
-
},
|
|
3169
|
-
children: [/* @__PURE__ */jsxs(Text, {
|
|
3170
|
-
size: 1,
|
|
3171
|
-
children: ["It seems like this workspace contains documents from an", " ", /* @__PURE__ */jsxs("strong", {
|
|
3172
|
-
children: ["older version of ", pluginTitle]
|
|
3173
|
-
}), "."]
|
|
3174
|
-
}), /* @__PURE__ */jsx(Text, {
|
|
3175
|
-
size: 1,
|
|
3176
|
-
children: "Cleanup is required."
|
|
3177
|
-
}), error ? /* @__PURE__ */jsxs(Card, {
|
|
3178
|
-
padding: 2,
|
|
3179
|
-
tone: "critical",
|
|
3180
|
-
border: true,
|
|
3181
|
-
children: [/* @__PURE__ */jsx(Text, {
|
|
3182
|
-
size: 1,
|
|
3183
|
-
children: "An error occurred. See console for details."
|
|
3184
|
-
}), " "]
|
|
3185
|
-
}) : null, /* @__PURE__ */jsx(Button, {
|
|
3186
|
-
icon: progress ? /* @__PURE__ */jsx(Box, {
|
|
3187
|
-
style: {
|
|
3188
|
-
marginTop: 5
|
|
3189
|
-
},
|
|
3190
|
-
children: /* @__PURE__ */jsx(Spinner, {})
|
|
3191
|
-
}) : CheckmarkIcon,
|
|
3192
|
-
disabled: !!progress,
|
|
3193
|
-
text: progress ? "".concat(Math.floor(progress * 100), "%") : "Ok, convert to new format!",
|
|
3194
|
-
tone: "primary",
|
|
3195
|
-
onClick: convert
|
|
3196
|
-
})]
|
|
3197
|
-
})
|
|
3198
|
-
})
|
|
3199
|
-
});
|
|
3200
|
-
}
|
|
3201
|
-
return null;
|
|
3202
|
-
}
|
|
3203
|
-
async function deleteDocs(client, ids, updateProgress) {
|
|
3204
|
-
const chunkSize = 50;
|
|
3205
|
-
for (let i = 0; i < ids.length; i += chunkSize) {
|
|
3206
|
-
const progressCount = Math.min(ids.length, i + chunkSize);
|
|
3207
|
-
const chunk = ids.slice(i, progressCount);
|
|
3208
|
-
const trans = client.transaction();
|
|
3209
|
-
chunk.forEach(id => trans.delete(id));
|
|
3210
|
-
await trans.commit();
|
|
3211
|
-
updateProgress(progressCount / ids.length);
|
|
3212
|
-
}
|
|
3213
|
-
}
|
|
3214
|
-
async function convertContextDocs(client, docs) {
|
|
3215
|
-
const trans = client.transaction();
|
|
3216
|
-
for (const doc of docs) {
|
|
3217
|
-
const {
|
|
3218
|
-
_id,
|
|
3219
|
-
_type,
|
|
3220
|
-
...rest
|
|
3221
|
-
} = doc;
|
|
3222
|
-
trans.createOrReplace({
|
|
3223
|
-
...rest,
|
|
3224
|
-
_id: "port.".concat(_id),
|
|
3225
|
-
_alphaId: _id,
|
|
3226
|
-
_type: contextDocumentTypeName
|
|
3227
|
-
});
|
|
3228
|
-
}
|
|
3229
|
-
await trans.commit();
|
|
3230
|
-
}
|
|
3231
|
-
async function convertDocs(client, docs, updateProgress) {
|
|
3232
|
-
const chunkSize = 10;
|
|
3233
|
-
for (let i = 0; i < docs.length; i += chunkSize) {
|
|
3234
|
-
const progressCount = Math.min(docs.length, i + chunkSize);
|
|
3235
|
-
const chunk = docs.slice(i, progressCount);
|
|
3236
|
-
const trans = client.transaction();
|
|
3237
|
-
const contextDocs = await client.fetch('*[_type=="'.concat(contextDocumentTypeName, '" && _alphaId != null]{_id, _alphaId}'));
|
|
3238
|
-
chunk.forEach(oldDoc => {
|
|
3239
|
-
var _a;
|
|
3240
|
-
const documentType = oldDoc._id.replace(new RegExp("^(".concat(legacyAssistDocumentIdPrefix, "|").concat(assistDocumentIdPrefix, ")")), "");
|
|
3241
|
-
const id = assistDocumentId(documentType);
|
|
3242
|
-
const fields = ((_a = oldDoc.fields) != null ? _a : []).filter(field => {
|
|
3243
|
-
var _a2;
|
|
3244
|
-
return (_a2 = field.instructions) == null ? void 0 : _a2.length;
|
|
3245
|
-
}).map(oldField => {
|
|
3246
|
-
var _a2;
|
|
3247
|
-
const instructions = ((_a2 = oldField.instructions) != null ? _a2 : []).map(inst => {
|
|
3248
|
-
var _a3;
|
|
3249
|
-
const prompt = ((_a3 = inst.prompt) != null ? _a3 : []).map(block => {
|
|
3250
|
-
return mapBlock(block, contextDocs);
|
|
3251
|
-
});
|
|
3252
|
-
return {
|
|
3253
|
-
...inst,
|
|
3254
|
-
_type: instructionTypeName,
|
|
3255
|
-
prompt
|
|
3256
|
-
};
|
|
3257
|
-
});
|
|
3258
|
-
return {
|
|
3259
|
-
...oldField,
|
|
3260
|
-
_type: assistFieldTypeName,
|
|
3261
|
-
instructions
|
|
3262
|
-
};
|
|
3263
|
-
});
|
|
3264
|
-
if (fields.length) {
|
|
3265
|
-
trans.createOrReplace({
|
|
3266
|
-
_id: id,
|
|
3267
|
-
_type: assistDocumentTypeName,
|
|
3268
|
-
fields
|
|
3269
|
-
});
|
|
3270
|
-
}
|
|
3271
|
-
trans.delete(oldDoc._id);
|
|
3272
|
-
});
|
|
3273
|
-
await trans.commit();
|
|
3274
|
-
updateProgress(progressCount / docs.length);
|
|
3275
|
-
}
|
|
3276
|
-
}
|
|
3277
|
-
function isFieldRef(block) {
|
|
3278
|
-
return block._type === "sanity.ai.prompt.fieldRef";
|
|
3279
|
-
}
|
|
3280
|
-
function isContext(block) {
|
|
3281
|
-
return block._type === "sanity.ai.prompt.context";
|
|
3282
|
-
}
|
|
3283
|
-
function isUserInput(block) {
|
|
3284
|
-
return block._type === "sanity.ai.prompt.userInput";
|
|
3285
|
-
}
|
|
3286
|
-
function isSpan(block) {
|
|
3287
|
-
return block._type === "span";
|
|
3288
|
-
}
|
|
3289
|
-
function mapBlock(block, migratedContexts) {
|
|
3290
|
-
var _a, _b, _c, _d;
|
|
3291
|
-
if (isFieldRef(block)) {
|
|
3292
|
-
return {
|
|
3293
|
-
...block,
|
|
3294
|
-
_type: fieldReferenceTypeName
|
|
3295
|
-
};
|
|
3296
|
-
}
|
|
3297
|
-
if (isUserInput(block)) {
|
|
3298
|
-
return {
|
|
3299
|
-
...block,
|
|
3300
|
-
_type: userInputTypeName
|
|
3301
|
-
};
|
|
3302
|
-
}
|
|
3303
|
-
if (isContext(block)) {
|
|
3304
|
-
const newBlock = {
|
|
3305
|
-
...block,
|
|
3306
|
-
_type: instructionContextTypeName,
|
|
3307
|
-
reference: {
|
|
3308
|
-
_type: "reference",
|
|
3309
|
-
_ref: (_c = (_a = migratedContexts.find(c => {
|
|
3310
|
-
var _a2;
|
|
3311
|
-
return c._alphaId === ((_a2 = block.reference) == null ? void 0 : _a2._ref);
|
|
3312
|
-
})) == null ? void 0 : _a._id) != null ? _c : (_b = block.reference) == null ? void 0 : _b._ref
|
|
3313
|
-
}
|
|
3314
|
-
};
|
|
3315
|
-
return newBlock;
|
|
3316
|
-
}
|
|
3317
|
-
if (isSpan(block)) {
|
|
3318
|
-
return block;
|
|
3319
|
-
}
|
|
3320
|
-
const textBlock = block;
|
|
3321
|
-
return {
|
|
3322
|
-
...textBlock,
|
|
3323
|
-
children: ((_d = textBlock.children) != null ? _d : []).map(child => mapBlock(child, migratedContexts))
|
|
3324
|
-
};
|
|
3325
|
-
}
|
|
3326
|
-
const MAX_DEPTH = 6;
|
|
3327
|
-
function getDocumentMembersFlat(doc, schemaType) {
|
|
3328
|
-
if (!isDocumentSchemaType(schemaType)) {
|
|
3329
|
-
console.error("Schema type is not a document");
|
|
3330
|
-
return [];
|
|
3331
|
-
}
|
|
3332
|
-
return extractPaths(doc, schemaType, [], MAX_DEPTH);
|
|
3333
|
-
}
|
|
3334
|
-
function extractPaths(doc, schemaType, path, maxDepth) {
|
|
3335
|
-
if (path.length >= maxDepth) {
|
|
3336
|
-
return [];
|
|
3337
|
-
}
|
|
3338
|
-
return schemaType.fields.reduce((acc, field) => {
|
|
3339
|
-
var _a;
|
|
3340
|
-
const fieldPath = [...path, field.name];
|
|
3341
|
-
const fieldSchema = field.type;
|
|
3342
|
-
const thisFieldWithPath = {
|
|
3343
|
-
path: fieldPath,
|
|
3344
|
-
name: field.name,
|
|
3345
|
-
schemaType: fieldSchema
|
|
3346
|
-
};
|
|
3347
|
-
if (fieldSchema.jsonType === "object") {
|
|
3348
|
-
const innerFields = extractPaths(doc, fieldSchema, fieldPath, maxDepth);
|
|
3349
|
-
return [...acc, thisFieldWithPath, ...innerFields];
|
|
3350
|
-
} else if (fieldSchema.jsonType === "array" && fieldSchema.of.length && fieldSchema.of.some(item => "fields" in item)) {
|
|
3351
|
-
const {
|
|
3352
|
-
value: arrayValue
|
|
3353
|
-
} = (_a = extractWithPath(pathToString(fieldPath), doc)[0]) != null ? _a : {};
|
|
3354
|
-
let arrayPaths = [];
|
|
3355
|
-
if (arrayValue == null ? void 0 : arrayValue.length) {
|
|
3356
|
-
for (const item of arrayValue) {
|
|
3357
|
-
const arrayItemSchema = fieldSchema.of.find(t => t.name === item._type);
|
|
3358
|
-
if (item._key && arrayItemSchema) {
|
|
3359
|
-
const itemPath = [...fieldPath, {
|
|
3360
|
-
_key: item._key
|
|
3361
|
-
}];
|
|
3362
|
-
const innerFields = extractPaths(doc, arrayItemSchema, itemPath, maxDepth);
|
|
3363
|
-
arrayPaths = [...arrayPaths, {
|
|
3364
|
-
path: itemPath,
|
|
3365
|
-
name: item._key,
|
|
3366
|
-
schemaType: arrayItemSchema
|
|
3367
|
-
}, ...innerFields];
|
|
3368
|
-
}
|
|
3369
|
-
}
|
|
3370
|
-
}
|
|
3371
|
-
return [...acc, thisFieldWithPath, ...arrayPaths];
|
|
3372
|
-
}
|
|
3373
|
-
return [...acc, thisFieldWithPath];
|
|
3374
|
-
}, []);
|
|
3375
|
-
}
|
|
3376
|
-
const defaultLanguageOutputs = function (member, enclosingType, translateFromLanguageId, translateToLanguageIds) {
|
|
3377
|
-
if (member.schemaType.jsonType === "object" && member.schemaType.name.startsWith("internationalizedArray")) {
|
|
3378
|
-
const pathEnd = member.path.slice(-1);
|
|
3379
|
-
const language = isKeySegment(pathEnd[0]) ? pathEnd[0]._key : null;
|
|
3380
|
-
return language === translateFromLanguageId ? translateToLanguageIds.map(translateToId => ({
|
|
3381
|
-
id: translateToId,
|
|
3382
|
-
outputPath: [...member.path.slice(0, -1), {
|
|
3383
|
-
_key: translateToId
|
|
3384
|
-
}]
|
|
3385
|
-
})) : void 0;
|
|
3386
|
-
}
|
|
3387
|
-
if (enclosingType.jsonType === "object" && enclosingType.name.startsWith("locale")) {
|
|
3388
|
-
return translateFromLanguageId === member.name ? translateToLanguageIds.map(translateToId => ({
|
|
3389
|
-
id: translateToId,
|
|
3390
|
-
outputPath: [...member.path.slice(0, -1), translateToId]
|
|
3391
|
-
})) : void 0;
|
|
3392
|
-
}
|
|
3393
|
-
return void 0;
|
|
3394
|
-
};
|
|
3395
|
-
function getFieldLanguageMap(documentSchema, documentMembers, translateFromLanguageId, outputLanguageIds, langFn) {
|
|
3396
|
-
var _a, _b, _c;
|
|
3397
|
-
const translationMaps = [];
|
|
3398
|
-
for (const member of documentMembers) {
|
|
3399
|
-
const parentPath = member.path.slice(0, -1);
|
|
3400
|
-
const enclosingType = (_b = (_a = documentMembers.find(m => pathToString(m.path) === pathToString(parentPath))) == null ? void 0 : _a.schemaType) != null ? _b : documentSchema;
|
|
3401
|
-
const translations = (_c = langFn(member, enclosingType, translateFromLanguageId, outputLanguageIds)) == null ? void 0 : _c.filter(translation => translation.id !== translateFromLanguageId);
|
|
3402
|
-
if (translations) {
|
|
3403
|
-
translationMaps.push({
|
|
3404
|
-
inputLanguageId: translateFromLanguageId,
|
|
3405
|
-
inputPath: member.path,
|
|
3406
|
-
outputs: translations
|
|
3407
|
-
});
|
|
3408
|
-
}
|
|
3409
|
-
}
|
|
3410
|
-
return translationMaps;
|
|
3411
|
-
}
|
|
3412
3191
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
3413
3192
|
function getDefaultExportFromCjs(x) {
|
|
3414
3193
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
@@ -4642,6 +4421,9 @@ const FieldTranslationContext = createContext({
|
|
|
4642
4421
|
function useFieldTranslation() {
|
|
4643
4422
|
return useContext(FieldTranslationContext);
|
|
4644
4423
|
}
|
|
4424
|
+
function hasValuesToTranslate(fieldLanguageMaps, fromLanguage, basePath) {
|
|
4425
|
+
return fieldLanguageMaps == null ? void 0 : fieldLanguageMaps.some(map => map.inputLanguageId === (fromLanguage == null ? void 0 : fromLanguage.id) && map.inputPath && pathToString(map.inputPath).startsWith(pathToString(basePath)));
|
|
4426
|
+
}
|
|
4645
4427
|
function FieldTranslationProvider(props) {
|
|
4646
4428
|
var _a, _b, _c;
|
|
4647
4429
|
const {
|
|
@@ -4687,7 +4469,7 @@ function FieldTranslationProvider(props) {
|
|
|
4687
4469
|
const allToIds = (_a2 = allToLanguages == null ? void 0 : allToLanguages.map(l => l.id)) != null ? _a2 : [];
|
|
4688
4470
|
const docMembers = getDocumentMembersFlat(document, documentSchema);
|
|
4689
4471
|
if (fromId && (allToIds == null ? void 0 : allToIds.length)) {
|
|
4690
|
-
const transMap = getFieldLanguageMap(documentSchema, docMembers, fromId, allToIds, (_b2 = config == null ? void 0 : config.translationOutputs) != null ? _b2 : defaultLanguageOutputs);
|
|
4472
|
+
const transMap = getFieldLanguageMap(documentSchema, docMembers, fromId, allToIds.filter(toId => fromId !== toId), (_b2 = config == null ? void 0 : config.translationOutputs) != null ? _b2 : defaultLanguageOutputs);
|
|
4691
4473
|
setFieldLanguageMaps(transMap);
|
|
4692
4474
|
} else {
|
|
4693
4475
|
setFieldLanguageMaps(void 0);
|
|
@@ -4721,7 +4503,7 @@ function FieldTranslationProvider(props) {
|
|
|
4721
4503
|
translationLoading: false
|
|
4722
4504
|
};
|
|
4723
4505
|
}, [openFieldTranslation]);
|
|
4724
|
-
const runDisabled = !fromLanguage || !(toLanguages == null ? void 0 : toLanguages.length) || !(fieldLanguageMaps == null ? void 0 : fieldLanguageMaps.length) || !documentId;
|
|
4506
|
+
const runDisabled = !fromLanguage || !(toLanguages == null ? void 0 : toLanguages.length) || !(fieldLanguageMaps == null ? void 0 : fieldLanguageMaps.length) || !documentId || !hasValuesToTranslate(fieldLanguageMaps, fromLanguage, fieldTranslationParams.translatePath);
|
|
4725
4507
|
const onRunTranslation = useCallback(() => {
|
|
4726
4508
|
const translatePath = fieldTranslationParams == null ? void 0 : fieldTranslationParams.translatePath;
|
|
4727
4509
|
if (fieldLanguageMaps && documentId && translatePath) {
|
|
@@ -4763,7 +4545,7 @@ function FieldTranslationProvider(props) {
|
|
|
4763
4545
|
content: /* @__PURE__ */jsx(Flex, {
|
|
4764
4546
|
padding: 2,
|
|
4765
4547
|
children: /* @__PURE__ */jsx(Text, {
|
|
4766
|
-
children: "
|
|
4548
|
+
children: "There is nothing to translate in the selected from-language."
|
|
4767
4549
|
})
|
|
4768
4550
|
}),
|
|
4769
4551
|
placement: "top",
|
|
@@ -4791,11 +4573,12 @@ function FieldTranslationProvider(props) {
|
|
|
4791
4573
|
return /* @__PURE__ */jsxs(Flex, {
|
|
4792
4574
|
gap: 3,
|
|
4793
4575
|
align: "center",
|
|
4576
|
+
as: "label",
|
|
4794
4577
|
children: [/* @__PURE__ */jsx(Radio, {
|
|
4795
4578
|
name: "fromLang",
|
|
4796
4579
|
value: l.id,
|
|
4797
4580
|
checked: l.id === (fromLanguage == null ? void 0 : fromLanguage.id),
|
|
4798
|
-
|
|
4581
|
+
onChange: () => selectFromLanguage(l, languages, fieldTranslationParams)
|
|
4799
4582
|
}), /* @__PURE__ */jsx(Text, {
|
|
4800
4583
|
children: (_a2 = l.title) != null ? _a2 : l.id
|
|
4801
4584
|
})]
|
|
@@ -4809,17 +4592,23 @@ function FieldTranslationProvider(props) {
|
|
|
4809
4592
|
weight: "semibold",
|
|
4810
4593
|
children: "To"
|
|
4811
4594
|
})
|
|
4812
|
-
}), languages
|
|
4595
|
+
}), languages.map(l => {
|
|
4813
4596
|
var _a2;
|
|
4814
4597
|
return /* @__PURE__ */jsxs(Flex, {
|
|
4815
4598
|
gap: 3,
|
|
4816
4599
|
align: "center",
|
|
4600
|
+
as: "label",
|
|
4601
|
+
style: l.id === (fromLanguage == null ? void 0 : fromLanguage.id) ? {
|
|
4602
|
+
opacity: 0.5
|
|
4603
|
+
} : void 0,
|
|
4817
4604
|
children: [/* @__PURE__ */jsx(Checkbox, {
|
|
4818
4605
|
name: "toLang",
|
|
4819
4606
|
value: l.id,
|
|
4820
|
-
checked: !!(toLanguages == null ? void 0 : toLanguages.find(tl => tl.id === l.id)),
|
|
4821
|
-
|
|
4607
|
+
checked: l.id !== (fromLanguage == null ? void 0 : fromLanguage.id) && !!(toLanguages == null ? void 0 : toLanguages.find(tl => tl.id === l.id)),
|
|
4608
|
+
onChange: () => toggleToLanguage(l, toLanguages, languages),
|
|
4609
|
+
disabled: l.id === (fromLanguage == null ? void 0 : fromLanguage.id)
|
|
4822
4610
|
}), /* @__PURE__ */jsx(Text, {
|
|
4611
|
+
muted: l.id === (fromLanguage == null ? void 0 : fromLanguage.id),
|
|
4823
4612
|
children: (_a2 = l.title) != null ? _a2 : l.id
|
|
4824
4613
|
})]
|
|
4825
4614
|
}, l.id);
|
|
@@ -4840,12 +4629,10 @@ function FieldTranslationProvider(props) {
|
|
|
4840
4629
|
});
|
|
4841
4630
|
}
|
|
4842
4631
|
function AssistLayout(props) {
|
|
4843
|
-
var _a;
|
|
4844
4632
|
const [connectors, setConnectors] = useState([]);
|
|
4845
|
-
|
|
4846
|
-
return /* @__PURE__ */jsxs(AiAssistanceConfigProvider, {
|
|
4633
|
+
return /* @__PURE__ */jsx(AiAssistanceConfigProvider, {
|
|
4847
4634
|
config: props.config,
|
|
4848
|
-
children:
|
|
4635
|
+
children: /* @__PURE__ */jsx(RunInstructionProvider, {
|
|
4849
4636
|
children: /* @__PURE__ */jsx(FieldTranslationProvider, {
|
|
4850
4637
|
children: /* @__PURE__ */jsxs(ConnectorsProvider, {
|
|
4851
4638
|
onConnectorsChange: setConnectors,
|
|
@@ -4857,7 +4644,7 @@ function AssistLayout(props) {
|
|
|
4857
4644
|
})]
|
|
4858
4645
|
})
|
|
4859
4646
|
})
|
|
4860
|
-
})
|
|
4647
|
+
})
|
|
4861
4648
|
});
|
|
4862
4649
|
}
|
|
4863
4650
|
var __freeze$1 = Object.freeze;
|
|
@@ -6891,5 +6678,5 @@ const assist = definePlugin(config => {
|
|
|
6891
6678
|
})()]
|
|
6892
6679
|
};
|
|
6893
6680
|
});
|
|
6894
|
-
export { SchemaTypeTool, assist, contextDocumentTypeName };
|
|
6681
|
+
export { SchemaTypeTool, assist, contextDocumentTypeName, defaultLanguageOutputs };
|
|
6895
6682
|
//# sourceMappingURL=index.esm.js.map
|