@sanity/document-internationalization 2.0.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +17 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.esm.js +82 -9
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +81 -8
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
- 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) {
|
|
@@ -414,6 +415,77 @@ function createReference(key, ref, type) {
|
|
|
414
415
|
}
|
|
415
416
|
};
|
|
416
417
|
}
|
|
418
|
+
function removeExcludedPaths(doc, schemaType) {
|
|
419
|
+
if (!sanity.isDocumentSchemaType(schemaType) || !doc) {
|
|
420
|
+
return doc;
|
|
421
|
+
}
|
|
422
|
+
const pathsToExclude = extractPaths(doc, schemaType, []).filter(field => {
|
|
423
|
+
var _a, _b, _c;
|
|
424
|
+
return ((_c = (_b = (_a = field.schemaType) == null ? void 0 : _a.options) == null ? void 0 : _b.documentInternationalization) == null ? void 0 : _c.exclude) === true;
|
|
425
|
+
}).map(field => {
|
|
426
|
+
return sanity.pathToString(field.path);
|
|
427
|
+
});
|
|
428
|
+
const mut = new mutator.Mutation({
|
|
429
|
+
mutations: [{
|
|
430
|
+
patch: {
|
|
431
|
+
id: doc._id,
|
|
432
|
+
unset: pathsToExclude
|
|
433
|
+
}
|
|
434
|
+
}]
|
|
435
|
+
});
|
|
436
|
+
return mut.apply(doc);
|
|
437
|
+
}
|
|
438
|
+
function extractPaths(doc, schemaType, path) {
|
|
439
|
+
return schemaType.fields.reduce((acc, field) => {
|
|
440
|
+
var _a, _b;
|
|
441
|
+
const fieldPath = [...path, field.name];
|
|
442
|
+
const fieldSchema = field.type;
|
|
443
|
+
const {
|
|
444
|
+
value
|
|
445
|
+
} = (_a = mutator.extractWithPath(sanity.pathToString(fieldPath), doc)[0]) != null ? _a : {};
|
|
446
|
+
if (!value) {
|
|
447
|
+
return acc;
|
|
448
|
+
}
|
|
449
|
+
const thisFieldWithPath = {
|
|
450
|
+
path: fieldPath,
|
|
451
|
+
name: field.name,
|
|
452
|
+
schemaType: fieldSchema,
|
|
453
|
+
value
|
|
454
|
+
};
|
|
455
|
+
if (fieldSchema.jsonType === "object") {
|
|
456
|
+
const innerFields = extractPaths(doc, fieldSchema, fieldPath);
|
|
457
|
+
return [...acc, thisFieldWithPath, ...innerFields];
|
|
458
|
+
} else if (fieldSchema.jsonType === "array" && fieldSchema.of.length && fieldSchema.of.some(item => "fields" in item)) {
|
|
459
|
+
const {
|
|
460
|
+
value: arrayValue
|
|
461
|
+
} = (_b = mutator.extractWithPath(sanity.pathToString(fieldPath), doc)[0]) != null ? _b : {};
|
|
462
|
+
let arrayPaths = [];
|
|
463
|
+
if (arrayValue == null ? void 0 : arrayValue.length) {
|
|
464
|
+
for (const item of arrayValue) {
|
|
465
|
+
const itemPath = [...fieldPath, {
|
|
466
|
+
_key: item._key
|
|
467
|
+
}];
|
|
468
|
+
let itemSchema = fieldSchema.of.find(t => t.name === item._type);
|
|
469
|
+
if (!item._type) {
|
|
470
|
+
itemSchema = fieldSchema.of[0];
|
|
471
|
+
}
|
|
472
|
+
if (item._key && itemSchema) {
|
|
473
|
+
const innerFields = extractPaths(doc, itemSchema, itemPath);
|
|
474
|
+
const arrayMember = {
|
|
475
|
+
path: itemPath,
|
|
476
|
+
name: item._key,
|
|
477
|
+
schemaType: itemSchema,
|
|
478
|
+
value: item
|
|
479
|
+
};
|
|
480
|
+
arrayPaths = [...arrayPaths, arrayMember, ...innerFields];
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
return [...acc, thisFieldWithPath, ...arrayPaths];
|
|
485
|
+
}
|
|
486
|
+
return [...acc, thisFieldWithPath];
|
|
487
|
+
}, []);
|
|
488
|
+
}
|
|
417
489
|
function LanguageOption(props) {
|
|
418
490
|
var _a;
|
|
419
491
|
const {
|
|
@@ -437,7 +509,7 @@ function LanguageOption(props) {
|
|
|
437
509
|
apiVersion
|
|
438
510
|
});
|
|
439
511
|
const toast = ui.useToast();
|
|
440
|
-
const open = useOpenInNewPane((_a = translation == null ? void 0 : translation.value) == null ? void 0 : _a._ref, schemaType);
|
|
512
|
+
const open = useOpenInNewPane((_a = translation == null ? void 0 : translation.value) == null ? void 0 : _a._ref, schemaType.name);
|
|
441
513
|
const handleOpen = react.useCallback(() => open(), [open]);
|
|
442
514
|
const handleCreate = react.useCallback(async () => {
|
|
443
515
|
if (!source) {
|
|
@@ -451,19 +523,20 @@ function LanguageOption(props) {
|
|
|
451
523
|
}
|
|
452
524
|
const transaction = client.transaction();
|
|
453
525
|
const newTranslationDocumentId = uuid.uuid();
|
|
454
|
-
|
|
526
|
+
let newTranslationDocument = {
|
|
455
527
|
...source,
|
|
456
528
|
_id: "drafts.".concat(newTranslationDocumentId),
|
|
457
529
|
// 2. Update language of the translation
|
|
458
530
|
[languageField]: language.id
|
|
459
531
|
};
|
|
532
|
+
newTranslationDocument = removeExcludedPaths(newTranslationDocument, schemaType);
|
|
460
533
|
transaction.create(newTranslationDocument);
|
|
461
|
-
const sourceReference = createReference(sourceLanguageId, documentId, schemaType, !weakReferences);
|
|
462
|
-
const newTranslationReference = createReference(language.id, newTranslationDocumentId, schemaType, !weakReferences);
|
|
534
|
+
const sourceReference = createReference(sourceLanguageId, documentId, schemaType.name, !weakReferences);
|
|
535
|
+
const newTranslationReference = createReference(language.id, newTranslationDocumentId, schemaType.name, !weakReferences);
|
|
463
536
|
const newMetadataDocument = {
|
|
464
537
|
_id: metadataId,
|
|
465
538
|
_type: METADATA_SCHEMA_NAME,
|
|
466
|
-
schemaTypes: [schemaType],
|
|
539
|
+
schemaTypes: [schemaType.name],
|
|
467
540
|
translations: [sourceReference]
|
|
468
541
|
};
|
|
469
542
|
transaction.createIfNotExists(newMetadataDocument);
|
|
@@ -616,7 +689,7 @@ function DocumentInternationalizationMenu(props) {
|
|
|
616
689
|
const {
|
|
617
690
|
documentId
|
|
618
691
|
} = props;
|
|
619
|
-
const schemaType = props.schemaType
|
|
692
|
+
const schemaType = props.schemaType;
|
|
620
693
|
const {
|
|
621
694
|
languageField,
|
|
622
695
|
supportedLanguages
|
|
@@ -651,7 +724,7 @@ function DocumentInternationalizationMenu(props) {
|
|
|
651
724
|
const {
|
|
652
725
|
draft,
|
|
653
726
|
published
|
|
654
|
-
} = sanity.useEditState(documentId, schemaType);
|
|
727
|
+
} = sanity.useEditState(documentId, schemaType.name);
|
|
655
728
|
const source = draft || published;
|
|
656
729
|
const documentIsInOneMetadataDocument = react.useMemo(() => {
|
|
657
730
|
return Array.isArray(data) && data.length <= 1;
|
|
@@ -736,7 +809,7 @@ function DocumentInternationalizationMenu(props) {
|
|
|
736
809
|
if (!documentId) {
|
|
737
810
|
return null;
|
|
738
811
|
}
|
|
739
|
-
if (!schemaType) {
|
|
812
|
+
if (!schemaType || !schemaType.name) {
|
|
740
813
|
return null;
|
|
741
814
|
}
|
|
742
815
|
return /* @__PURE__ */jsxRuntime.jsx(ui.Popover, {
|