@sanity/document-internationalization 2.0.3 → 2.1.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/README.md +17 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.esm.js +84 -10
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +83 -9
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/DocumentInternationalizationContext.tsx +3 -2
- package/src/components/DocumentInternationalizationMenu.tsx +3 -3
- package/src/components/LanguageOption.tsx +15 -8
- package/src/types.ts +32 -0
- package/src/utils/excludePaths.ts +123 -0
package/dist/index.js
CHANGED
|
@@ -12,6 +12,7 @@ var sanityPluginUtils = require('sanity-plugin-utils');
|
|
|
12
12
|
var uuid = require('@sanity/uuid');
|
|
13
13
|
var desk = require('sanity/desk');
|
|
14
14
|
var router = require('sanity/router');
|
|
15
|
+
var mutator = require('@sanity/mutator');
|
|
15
16
|
var styled = require('styled-components');
|
|
16
17
|
var sanityPluginInternationalizedArray = require('sanity-plugin-internationalized-array');
|
|
17
18
|
function _interopDefaultCompat(e) {
|
|
@@ -229,6 +230,7 @@ function DocumentInternationalizationProvider(props) {
|
|
|
229
230
|
const client = sanity.useClient({
|
|
230
231
|
apiVersion: pluginConfig.apiVersion
|
|
231
232
|
});
|
|
233
|
+
const workspace = sanity.useWorkspace();
|
|
232
234
|
const supportedLanguages = Array.isArray(pluginConfig.supportedLanguages) ? pluginConfig.supportedLanguages :
|
|
233
235
|
// eslint-disable-next-line require-await
|
|
234
236
|
suspend(async () => {
|
|
@@ -236,7 +238,7 @@ function DocumentInternationalizationProvider(props) {
|
|
|
236
238
|
return pluginConfig.supportedLanguages(client);
|
|
237
239
|
}
|
|
238
240
|
return pluginConfig.supportedLanguages;
|
|
239
|
-
}, []);
|
|
241
|
+
}, [workspace]);
|
|
240
242
|
return /* @__PURE__ */jsxRuntime.jsx(DocumentInternationalizationContext.Provider, {
|
|
241
243
|
value: {
|
|
242
244
|
...pluginConfig,
|
|
@@ -414,6 +416,77 @@ function createReference(key, ref, type) {
|
|
|
414
416
|
}
|
|
415
417
|
};
|
|
416
418
|
}
|
|
419
|
+
function removeExcludedPaths(doc, schemaType) {
|
|
420
|
+
if (!sanity.isDocumentSchemaType(schemaType) || !doc) {
|
|
421
|
+
return doc;
|
|
422
|
+
}
|
|
423
|
+
const pathsToExclude = extractPaths(doc, schemaType, []).filter(field => {
|
|
424
|
+
var _a, _b, _c;
|
|
425
|
+
return ((_c = (_b = (_a = field.schemaType) == null ? void 0 : _a.options) == null ? void 0 : _b.documentInternationalization) == null ? void 0 : _c.exclude) === true;
|
|
426
|
+
}).map(field => {
|
|
427
|
+
return sanity.pathToString(field.path);
|
|
428
|
+
});
|
|
429
|
+
const mut = new mutator.Mutation({
|
|
430
|
+
mutations: [{
|
|
431
|
+
patch: {
|
|
432
|
+
id: doc._id,
|
|
433
|
+
unset: pathsToExclude
|
|
434
|
+
}
|
|
435
|
+
}]
|
|
436
|
+
});
|
|
437
|
+
return mut.apply(doc);
|
|
438
|
+
}
|
|
439
|
+
function extractPaths(doc, schemaType, path) {
|
|
440
|
+
return schemaType.fields.reduce((acc, field) => {
|
|
441
|
+
var _a, _b;
|
|
442
|
+
const fieldPath = [...path, field.name];
|
|
443
|
+
const fieldSchema = field.type;
|
|
444
|
+
const {
|
|
445
|
+
value
|
|
446
|
+
} = (_a = mutator.extractWithPath(sanity.pathToString(fieldPath), doc)[0]) != null ? _a : {};
|
|
447
|
+
if (!value) {
|
|
448
|
+
return acc;
|
|
449
|
+
}
|
|
450
|
+
const thisFieldWithPath = {
|
|
451
|
+
path: fieldPath,
|
|
452
|
+
name: field.name,
|
|
453
|
+
schemaType: fieldSchema,
|
|
454
|
+
value
|
|
455
|
+
};
|
|
456
|
+
if (fieldSchema.jsonType === "object") {
|
|
457
|
+
const innerFields = extractPaths(doc, fieldSchema, fieldPath);
|
|
458
|
+
return [...acc, thisFieldWithPath, ...innerFields];
|
|
459
|
+
} else if (fieldSchema.jsonType === "array" && fieldSchema.of.length && fieldSchema.of.some(item => "fields" in item)) {
|
|
460
|
+
const {
|
|
461
|
+
value: arrayValue
|
|
462
|
+
} = (_b = mutator.extractWithPath(sanity.pathToString(fieldPath), doc)[0]) != null ? _b : {};
|
|
463
|
+
let arrayPaths = [];
|
|
464
|
+
if (arrayValue == null ? void 0 : arrayValue.length) {
|
|
465
|
+
for (const item of arrayValue) {
|
|
466
|
+
const itemPath = [...fieldPath, {
|
|
467
|
+
_key: item._key
|
|
468
|
+
}];
|
|
469
|
+
let itemSchema = fieldSchema.of.find(t => t.name === item._type);
|
|
470
|
+
if (!item._type) {
|
|
471
|
+
itemSchema = fieldSchema.of[0];
|
|
472
|
+
}
|
|
473
|
+
if (item._key && itemSchema) {
|
|
474
|
+
const innerFields = extractPaths(doc, itemSchema, itemPath);
|
|
475
|
+
const arrayMember = {
|
|
476
|
+
path: itemPath,
|
|
477
|
+
name: item._key,
|
|
478
|
+
schemaType: itemSchema,
|
|
479
|
+
value: item
|
|
480
|
+
};
|
|
481
|
+
arrayPaths = [...arrayPaths, arrayMember, ...innerFields];
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
return [...acc, thisFieldWithPath, ...arrayPaths];
|
|
486
|
+
}
|
|
487
|
+
return [...acc, thisFieldWithPath];
|
|
488
|
+
}, []);
|
|
489
|
+
}
|
|
417
490
|
function LanguageOption(props) {
|
|
418
491
|
var _a;
|
|
419
492
|
const {
|
|
@@ -437,7 +510,7 @@ function LanguageOption(props) {
|
|
|
437
510
|
apiVersion
|
|
438
511
|
});
|
|
439
512
|
const toast = ui.useToast();
|
|
440
|
-
const open = useOpenInNewPane((_a = translation == null ? void 0 : translation.value) == null ? void 0 : _a._ref, schemaType);
|
|
513
|
+
const open = useOpenInNewPane((_a = translation == null ? void 0 : translation.value) == null ? void 0 : _a._ref, schemaType.name);
|
|
441
514
|
const handleOpen = react.useCallback(() => open(), [open]);
|
|
442
515
|
const handleCreate = react.useCallback(async () => {
|
|
443
516
|
if (!source) {
|
|
@@ -451,19 +524,20 @@ function LanguageOption(props) {
|
|
|
451
524
|
}
|
|
452
525
|
const transaction = client.transaction();
|
|
453
526
|
const newTranslationDocumentId = uuid.uuid();
|
|
454
|
-
|
|
527
|
+
let newTranslationDocument = {
|
|
455
528
|
...source,
|
|
456
529
|
_id: "drafts.".concat(newTranslationDocumentId),
|
|
457
530
|
// 2. Update language of the translation
|
|
458
531
|
[languageField]: language.id
|
|
459
532
|
};
|
|
533
|
+
newTranslationDocument = removeExcludedPaths(newTranslationDocument, schemaType);
|
|
460
534
|
transaction.create(newTranslationDocument);
|
|
461
|
-
const sourceReference = createReference(sourceLanguageId, documentId, schemaType, !weakReferences);
|
|
462
|
-
const newTranslationReference = createReference(language.id, newTranslationDocumentId, schemaType, !weakReferences);
|
|
535
|
+
const sourceReference = createReference(sourceLanguageId, documentId, schemaType.name, !weakReferences);
|
|
536
|
+
const newTranslationReference = createReference(language.id, newTranslationDocumentId, schemaType.name, !weakReferences);
|
|
463
537
|
const newMetadataDocument = {
|
|
464
538
|
_id: metadataId,
|
|
465
539
|
_type: METADATA_SCHEMA_NAME,
|
|
466
|
-
schemaTypes: [schemaType],
|
|
540
|
+
schemaTypes: [schemaType.name],
|
|
467
541
|
translations: [sourceReference]
|
|
468
542
|
};
|
|
469
543
|
transaction.createIfNotExists(newMetadataDocument);
|
|
@@ -616,7 +690,7 @@ function DocumentInternationalizationMenu(props) {
|
|
|
616
690
|
const {
|
|
617
691
|
documentId
|
|
618
692
|
} = props;
|
|
619
|
-
const schemaType = props.schemaType
|
|
693
|
+
const schemaType = props.schemaType;
|
|
620
694
|
const {
|
|
621
695
|
languageField,
|
|
622
696
|
supportedLanguages
|
|
@@ -651,7 +725,7 @@ function DocumentInternationalizationMenu(props) {
|
|
|
651
725
|
const {
|
|
652
726
|
draft,
|
|
653
727
|
published
|
|
654
|
-
} = sanity.useEditState(documentId, schemaType);
|
|
728
|
+
} = sanity.useEditState(documentId, schemaType.name);
|
|
655
729
|
const source = draft || published;
|
|
656
730
|
const documentIsInOneMetadataDocument = react.useMemo(() => {
|
|
657
731
|
return Array.isArray(data) && data.length <= 1;
|
|
@@ -736,7 +810,7 @@ function DocumentInternationalizationMenu(props) {
|
|
|
736
810
|
if (!documentId) {
|
|
737
811
|
return null;
|
|
738
812
|
}
|
|
739
|
-
if (!schemaType) {
|
|
813
|
+
if (!schemaType || !schemaType.name) {
|
|
740
814
|
return null;
|
|
741
815
|
}
|
|
742
816
|
return /* @__PURE__ */jsxRuntime.jsx(ui.Popover, {
|