@sanity/assist 1.2.14 → 1.2.15-lang.10
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 +546 -24
- package/dist/index.cjs.mjs +1 -0
- package/dist/index.d.ts +253 -11
- package/dist/index.esm.js +2367 -393
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +2361 -386
- package/dist/index.js.map +1 -1
- package/package.json +15 -14
- package/src/_lib/form/DocumentForm.tsx +3 -2
- package/src/_lib/form/constants.ts +1 -0
- package/src/assistDocument/AssistDocumentInput.tsx +24 -4
- package/src/assistDocument/RequestRunInstructionProvider.tsx +37 -21
- package/src/assistDocument/components/instruction/InstructionInput.tsx +5 -4
- package/src/assistDocument/components/instruction/InstructionOutputField.tsx +45 -0
- package/src/assistDocument/components/instruction/InstructionOutputInput.tsx +205 -0
- package/src/assistDocument/hooks/useStudioAssistDocument.ts +5 -32
- package/src/assistFormComponents/AssistField.tsx +11 -5
- package/src/assistFormComponents/AssistFormBlock.tsx +2 -3
- package/src/assistFormComponents/validation/listItem.tsx +2 -2
- package/src/assistInspector/AssistInspector.tsx +6 -0
- package/src/assistInspector/FieldAutocomplete.tsx +1 -0
- package/src/assistInspector/InstructionTaskHistoryButton.tsx +2 -3
- package/src/assistInspector/helpers.ts +9 -11
- package/src/assistLayout/AssistLayout.tsx +9 -9
- package/src/components/ImageContext.tsx +19 -9
- package/src/components/SafeValueInput.tsx +4 -1
- package/src/coverage/.tmp/coverage-0.json +1 -0
- package/src/coverage/base.css +224 -0
- package/src/coverage/block-navigation.js +87 -0
- package/src/coverage/clover.xml +8613 -0
- package/src/coverage/coverage-final.json +107 -0
- package/src/coverage/favicon.png +0 -0
- package/src/coverage/index.html +461 -0
- package/src/coverage/lcov-report/base.css +224 -0
- package/src/coverage/lcov-report/block-navigation.js +87 -0
- package/src/coverage/lcov-report/favicon.png +0 -0
- package/src/coverage/lcov-report/index.html +461 -0
- package/src/coverage/lcov-report/prettify.css +1 -0
- package/src/coverage/lcov-report/prettify.js +2 -0
- package/src/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/src/coverage/lcov-report/sorter.js +196 -0
- package/src/coverage/lcov-report/src/_lib/connector/ConnectFromRegion.tsx.html +157 -0
- package/src/coverage/lcov-report/src/_lib/connector/ConnectToRegion.tsx.html +151 -0
- package/src/coverage/lcov-report/src/_lib/connector/ConnectorRegion.tsx.html +154 -0
- package/src/coverage/lcov-report/src/_lib/connector/ConnectorsProvider.tsx.html +142 -0
- package/src/coverage/lcov-report/src/_lib/connector/ConnectorsStore.ts.html +451 -0
- package/src/coverage/lcov-report/src/_lib/connector/ConnectorsStoreContext.ts.html +97 -0
- package/src/coverage/lcov-report/src/_lib/connector/helpers.ts.html +100 -0
- package/src/coverage/lcov-report/src/_lib/connector/index.html +281 -0
- package/src/coverage/lcov-report/src/_lib/connector/index.ts.html +112 -0
- package/src/coverage/lcov-report/src/_lib/connector/mapConnectorToLine.ts.html +334 -0
- package/src/coverage/lcov-report/src/_lib/connector/types.ts.html +253 -0
- package/src/coverage/lcov-report/src/_lib/connector/useConnectorsStore.ts.html +124 -0
- package/src/coverage/lcov-report/src/_lib/connector/useRegionRects.ts.html +508 -0
- package/src/coverage/lcov-report/src/_lib/fixedListenQuery.ts.html +388 -0
- package/src/coverage/lcov-report/src/_lib/form/DocumentForm.tsx.html +676 -0
- package/src/coverage/lcov-report/src/_lib/form/helpers.ts.html +178 -0
- package/src/coverage/lcov-report/src/_lib/form/index.html +146 -0
- package/src/coverage/lcov-report/src/_lib/form/index.ts.html +88 -0
- package/src/coverage/lcov-report/src/_lib/index.html +161 -0
- package/src/coverage/lcov-report/src/_lib/randomKey.ts.html +172 -0
- package/src/coverage/lcov-report/src/_lib/useListeningQuery.ts.html +268 -0
- package/src/coverage/lcov-report/src/_lib/usePrevious.ts.html +112 -0
- package/src/coverage/lcov-report/src/assistConnectors/AssistConnectorsOverlay.tsx.html +481 -0
- package/src/coverage/lcov-report/src/assistConnectors/ConnectorPath.tsx.html +271 -0
- package/src/coverage/lcov-report/src/assistConnectors/draw/arrowPath.ts.html +112 -0
- package/src/coverage/lcov-report/src/assistConnectors/draw/connectorPath.ts.html +511 -0
- package/src/coverage/lcov-report/src/assistConnectors/draw/index.html +131 -0
- package/src/coverage/lcov-report/src/assistConnectors/index.html +146 -0
- package/src/coverage/lcov-report/src/assistConnectors/index.ts.html +88 -0
- package/src/coverage/lcov-report/src/assistDocument/AssistDocumentContext.tsx.html +178 -0
- package/src/coverage/lcov-report/src/assistDocument/AssistDocumentContextProvider.tsx.html +136 -0
- package/src/coverage/lcov-report/src/assistDocument/AssistDocumentInput.tsx.html +280 -0
- package/src/coverage/lcov-report/src/assistDocument/RequestRunInstructionProvider.tsx.html +283 -0
- package/src/coverage/lcov-report/src/assistDocument/components/AssistDocumentForm.tsx.html +775 -0
- package/src/coverage/lcov-report/src/assistDocument/components/FieldRefPreview.tsx.html +166 -0
- package/src/coverage/lcov-report/src/assistDocument/components/InstructionsArrayField.tsx.html +109 -0
- package/src/coverage/lcov-report/src/assistDocument/components/InstructionsArrayInput.tsx.html +163 -0
- package/src/coverage/lcov-report/src/assistDocument/components/SelectedFieldContext.tsx.html +115 -0
- package/src/coverage/lcov-report/src/assistDocument/components/generic/HiddenFieldTitle.tsx.html +100 -0
- package/src/coverage/lcov-report/src/assistDocument/components/generic/index.html +116 -0
- package/src/coverage/lcov-report/src/assistDocument/components/helpers.ts.html +148 -0
- package/src/coverage/lcov-report/src/assistDocument/components/index.html +191 -0
- package/src/coverage/lcov-report/src/assistDocument/components/instruction/BackToInstructionsLink.tsx.html +178 -0
- package/src/coverage/lcov-report/src/assistDocument/components/instruction/FieldRefInput.tsx.html +244 -0
- package/src/coverage/lcov-report/src/assistDocument/components/instruction/InstructionInput.tsx.html +349 -0
- package/src/coverage/lcov-report/src/assistDocument/components/instruction/InstructionOutputField.tsx.html +220 -0
- package/src/coverage/lcov-report/src/assistDocument/components/instruction/InstructionOutputInput.tsx.html +700 -0
- package/src/coverage/lcov-report/src/assistDocument/components/instruction/PromptInput.tsx.html +241 -0
- package/src/coverage/lcov-report/src/assistDocument/components/instruction/appearance/IconInput.tsx.html +223 -0
- package/src/coverage/lcov-report/src/assistDocument/components/instruction/appearance/InstructionVisibility.tsx.html +196 -0
- package/src/coverage/lcov-report/src/assistDocument/components/instruction/appearance/index.html +131 -0
- package/src/coverage/lcov-report/src/assistDocument/components/instruction/index.html +191 -0
- package/src/coverage/lcov-report/src/assistDocument/hooks/index.html +161 -0
- package/src/coverage/lcov-report/src/assistDocument/hooks/useAssistDocumentContextValue.tsx.html +289 -0
- package/src/coverage/lcov-report/src/assistDocument/hooks/useDocumentState.ts.html +103 -0
- package/src/coverage/lcov-report/src/assistDocument/hooks/useInstructionToaster.tsx.html +307 -0
- package/src/coverage/lcov-report/src/assistDocument/hooks/useStudioAssistDocument.ts.html +379 -0
- package/src/coverage/lcov-report/src/assistDocument/index.html +176 -0
- package/src/coverage/lcov-report/src/assistDocument/index.ts.html +88 -0
- package/src/coverage/lcov-report/src/assistFormComponents/AssistField.tsx.html +301 -0
- package/src/coverage/lcov-report/src/assistFormComponents/AssistFormBlock.tsx.html +175 -0
- package/src/coverage/lcov-report/src/assistFormComponents/AssistInlineFormBlock.tsx.html +124 -0
- package/src/coverage/lcov-report/src/assistFormComponents/AssistItem.tsx.html +145 -0
- package/src/coverage/lcov-report/src/assistFormComponents/index.html +161 -0
- package/src/coverage/lcov-report/src/assistFormComponents/validation/index.html +131 -0
- package/src/coverage/lcov-report/src/assistFormComponents/validation/listItem.tsx.html +274 -0
- package/src/coverage/lcov-report/src/assistFormComponents/validation/validationList.tsx.html +352 -0
- package/src/coverage/lcov-report/src/assistInspector/AssistInspector.tsx.html +1318 -0
- package/src/coverage/lcov-report/src/assistInspector/FieldAutocomplete.tsx.html +505 -0
- package/src/coverage/lcov-report/src/assistInspector/InstructionTaskHistoryButton.tsx.html +865 -0
- package/src/coverage/lcov-report/src/assistInspector/constants.ts.html +88 -0
- package/src/coverage/lcov-report/src/assistInspector/helpers.ts.html +724 -0
- package/src/coverage/lcov-report/src/assistInspector/index.html +191 -0
- package/src/coverage/lcov-report/src/assistInspector/index.ts.html +163 -0
- package/src/coverage/lcov-report/src/assistLayout/AiAssistanceConfigContext.tsx.html +331 -0
- package/src/coverage/lcov-report/src/assistLayout/AssistLayout.tsx.html +199 -0
- package/src/coverage/lcov-report/src/assistLayout/RunInstructionProvider.tsx.html +751 -0
- package/src/coverage/lcov-report/src/assistLayout/index.html +146 -0
- package/src/coverage/lcov-report/src/components/AssistFeatureBadge.tsx.html +112 -0
- package/src/coverage/lcov-report/src/components/FadeInContent.tsx.html +205 -0
- package/src/coverage/lcov-report/src/components/HideReferenceChangedBannerInput.tsx.html +160 -0
- package/src/coverage/lcov-report/src/components/ImageContext.tsx.html +229 -0
- package/src/coverage/lcov-report/src/components/SafeValueInput.tsx.html +313 -0
- package/src/coverage/lcov-report/src/components/TimeAgo.tsx.html +139 -0
- package/src/coverage/lcov-report/src/components/index.html +191 -0
- package/src/coverage/lcov-report/src/constants.ts.html +145 -0
- package/src/coverage/lcov-report/src/fieldActions/PrivateIcon.tsx.html +145 -0
- package/src/coverage/lcov-report/src/fieldActions/assistFieldActions.tsx.html +907 -0
- package/src/coverage/lcov-report/src/fieldActions/generateCaptionActions.tsx.html +259 -0
- package/src/coverage/lcov-report/src/fieldActions/generateImageActions.tsx.html +256 -0
- package/src/coverage/lcov-report/src/fieldActions/index.html +161 -0
- package/src/coverage/lcov-report/src/helpers/assistSupported.ts.html +226 -0
- package/src/coverage/lcov-report/src/helpers/conditionalMembers.ts.html +466 -0
- package/src/coverage/lcov-report/src/helpers/ids.ts.html +142 -0
- package/src/coverage/lcov-report/src/helpers/index.html +191 -0
- package/src/coverage/lcov-report/src/helpers/misc.ts.html +133 -0
- package/src/coverage/lcov-report/src/helpers/typeUtils.ts.html +208 -0
- package/src/coverage/lcov-report/src/helpers/useAssistSupported.ts.html +106 -0
- package/src/coverage/lcov-report/src/index.html +176 -0
- package/src/coverage/lcov-report/src/index.ts.html +112 -0
- package/src/coverage/lcov-report/src/onboarding/FieldActionsOnboarding.tsx.html +286 -0
- package/src/coverage/lcov-report/src/onboarding/FirstAssistedPathProvider.tsx.html +172 -0
- package/src/coverage/lcov-report/src/onboarding/InspectorOnboarding.tsx.html +223 -0
- package/src/coverage/lcov-report/src/onboarding/index.html +161 -0
- package/src/coverage/lcov-report/src/onboarding/onboardingStore.ts.html +184 -0
- package/src/coverage/lcov-report/src/plugin.tsx.html +427 -0
- package/src/coverage/lcov-report/src/presence/AiFieldPresence.tsx.html +163 -0
- package/src/coverage/lcov-report/src/presence/AssistAvatar.tsx.html +373 -0
- package/src/coverage/lcov-report/src/presence/AssistDocumentPresence.tsx.html +259 -0
- package/src/coverage/lcov-report/src/presence/index.html +161 -0
- package/src/coverage/lcov-report/src/presence/useAssistPresence.ts.html +268 -0
- package/src/coverage/lcov-report/src/schemas/assistDocumentSchema.tsx.html +1552 -0
- package/src/coverage/lcov-report/src/schemas/contextDocumentSchema.tsx.html +253 -0
- package/src/coverage/lcov-report/src/schemas/index.html +176 -0
- package/src/coverage/lcov-report/src/schemas/index.ts.html +277 -0
- package/src/coverage/lcov-report/src/schemas/serialize/SchemTypeTool.tsx.html +391 -0
- package/src/coverage/lcov-report/src/schemas/serialize/index.html +146 -0
- package/src/coverage/lcov-report/src/schemas/serialize/schemaUtils.ts.html +196 -0
- package/src/coverage/lcov-report/src/schemas/serialize/serializeSchema.ts.html +754 -0
- package/src/coverage/lcov-report/src/schemas/serializedSchemaTypeSchema.ts.html +262 -0
- package/src/coverage/lcov-report/src/schemas/typeDefExtensions.ts.html +397 -0
- package/src/coverage/lcov-report/src/translate/FieldTranslationProvider.tsx.html +1003 -0
- package/src/coverage/lcov-report/src/translate/getLanguageParams.ts.html +163 -0
- package/src/coverage/lcov-report/src/translate/index.html +191 -0
- package/src/coverage/lcov-report/src/translate/languageStore.ts.html +139 -0
- package/src/coverage/lcov-report/src/translate/paths.ts.html +577 -0
- package/src/coverage/lcov-report/src/translate/translateActions.tsx.html +637 -0
- package/src/coverage/lcov-report/src/translate/types.ts.html +565 -0
- package/src/coverage/lcov-report/src/types.ts.html +649 -0
- package/src/coverage/lcov-report/src/useApiClient.ts.html +1015 -0
- package/src/coverage/lcov.info +9519 -0
- package/src/coverage/prettify.css +1 -0
- package/src/coverage/prettify.js +2 -0
- package/src/coverage/sort-arrow-sprite.png +0 -0
- package/src/coverage/sorter.js +196 -0
- package/src/coverage/src/_lib/connector/ConnectFromRegion.tsx.html +157 -0
- package/src/coverage/src/_lib/connector/ConnectToRegion.tsx.html +151 -0
- package/src/coverage/src/_lib/connector/ConnectorRegion.tsx.html +154 -0
- package/src/coverage/src/_lib/connector/ConnectorsProvider.tsx.html +142 -0
- package/src/coverage/src/_lib/connector/ConnectorsStore.ts.html +451 -0
- package/src/coverage/src/_lib/connector/ConnectorsStoreContext.ts.html +97 -0
- package/src/coverage/src/_lib/connector/helpers.ts.html +100 -0
- package/src/coverage/src/_lib/connector/index.html +281 -0
- package/src/coverage/src/_lib/connector/index.ts.html +112 -0
- package/src/coverage/src/_lib/connector/mapConnectorToLine.ts.html +334 -0
- package/src/coverage/src/_lib/connector/types.ts.html +253 -0
- package/src/coverage/src/_lib/connector/useConnectorsStore.ts.html +124 -0
- package/src/coverage/src/_lib/connector/useRegionRects.ts.html +508 -0
- package/src/coverage/src/_lib/fixedListenQuery.ts.html +388 -0
- package/src/coverage/src/_lib/form/DocumentForm.tsx.html +676 -0
- package/src/coverage/src/_lib/form/helpers.ts.html +178 -0
- package/src/coverage/src/_lib/form/index.html +146 -0
- package/src/coverage/src/_lib/form/index.ts.html +88 -0
- package/src/coverage/src/_lib/index.html +161 -0
- package/src/coverage/src/_lib/randomKey.ts.html +172 -0
- package/src/coverage/src/_lib/useListeningQuery.ts.html +268 -0
- package/src/coverage/src/_lib/usePrevious.ts.html +112 -0
- package/src/coverage/src/assistConnectors/AssistConnectorsOverlay.tsx.html +481 -0
- package/src/coverage/src/assistConnectors/ConnectorPath.tsx.html +271 -0
- package/src/coverage/src/assistConnectors/draw/arrowPath.ts.html +112 -0
- package/src/coverage/src/assistConnectors/draw/connectorPath.ts.html +511 -0
- package/src/coverage/src/assistConnectors/draw/index.html +131 -0
- package/src/coverage/src/assistConnectors/index.html +146 -0
- package/src/coverage/src/assistConnectors/index.ts.html +88 -0
- package/src/coverage/src/assistDocument/AssistDocumentContext.tsx.html +178 -0
- package/src/coverage/src/assistDocument/AssistDocumentContextProvider.tsx.html +136 -0
- package/src/coverage/src/assistDocument/AssistDocumentInput.tsx.html +280 -0
- package/src/coverage/src/assistDocument/RequestRunInstructionProvider.tsx.html +283 -0
- package/src/coverage/src/assistDocument/components/AssistDocumentForm.tsx.html +775 -0
- package/src/coverage/src/assistDocument/components/FieldRefPreview.tsx.html +166 -0
- package/src/coverage/src/assistDocument/components/InstructionsArrayField.tsx.html +109 -0
- package/src/coverage/src/assistDocument/components/InstructionsArrayInput.tsx.html +163 -0
- package/src/coverage/src/assistDocument/components/SelectedFieldContext.tsx.html +115 -0
- package/src/coverage/src/assistDocument/components/generic/HiddenFieldTitle.tsx.html +100 -0
- package/src/coverage/src/assistDocument/components/generic/index.html +116 -0
- package/src/coverage/src/assistDocument/components/helpers.ts.html +148 -0
- package/src/coverage/src/assistDocument/components/index.html +191 -0
- package/src/coverage/src/assistDocument/components/instruction/BackToInstructionsLink.tsx.html +178 -0
- package/src/coverage/src/assistDocument/components/instruction/FieldRefInput.tsx.html +244 -0
- package/src/coverage/src/assistDocument/components/instruction/InstructionInput.tsx.html +349 -0
- package/src/coverage/src/assistDocument/components/instruction/InstructionOutputField.tsx.html +220 -0
- package/src/coverage/src/assistDocument/components/instruction/InstructionOutputInput.tsx.html +700 -0
- package/src/coverage/src/assistDocument/components/instruction/PromptInput.tsx.html +241 -0
- package/src/coverage/src/assistDocument/components/instruction/appearance/IconInput.tsx.html +223 -0
- package/src/coverage/src/assistDocument/components/instruction/appearance/InstructionVisibility.tsx.html +196 -0
- package/src/coverage/src/assistDocument/components/instruction/appearance/index.html +131 -0
- package/src/coverage/src/assistDocument/components/instruction/index.html +191 -0
- package/src/coverage/src/assistDocument/hooks/index.html +161 -0
- package/src/coverage/src/assistDocument/hooks/useAssistDocumentContextValue.tsx.html +289 -0
- package/src/coverage/src/assistDocument/hooks/useDocumentState.ts.html +103 -0
- package/src/coverage/src/assistDocument/hooks/useInstructionToaster.tsx.html +307 -0
- package/src/coverage/src/assistDocument/hooks/useStudioAssistDocument.ts.html +379 -0
- package/src/coverage/src/assistDocument/index.html +176 -0
- package/src/coverage/src/assistDocument/index.ts.html +88 -0
- package/src/coverage/src/assistFormComponents/AssistField.tsx.html +301 -0
- package/src/coverage/src/assistFormComponents/AssistFormBlock.tsx.html +175 -0
- package/src/coverage/src/assistFormComponents/AssistInlineFormBlock.tsx.html +124 -0
- package/src/coverage/src/assistFormComponents/AssistItem.tsx.html +145 -0
- package/src/coverage/src/assistFormComponents/index.html +161 -0
- package/src/coverage/src/assistFormComponents/validation/index.html +131 -0
- package/src/coverage/src/assistFormComponents/validation/listItem.tsx.html +274 -0
- package/src/coverage/src/assistFormComponents/validation/validationList.tsx.html +352 -0
- package/src/coverage/src/assistInspector/AssistInspector.tsx.html +1318 -0
- package/src/coverage/src/assistInspector/FieldAutocomplete.tsx.html +505 -0
- package/src/coverage/src/assistInspector/InstructionTaskHistoryButton.tsx.html +865 -0
- package/src/coverage/src/assistInspector/constants.ts.html +88 -0
- package/src/coverage/src/assistInspector/helpers.ts.html +724 -0
- package/src/coverage/src/assistInspector/index.html +191 -0
- package/src/coverage/src/assistInspector/index.ts.html +163 -0
- package/src/coverage/src/assistLayout/AiAssistanceConfigContext.tsx.html +331 -0
- package/src/coverage/src/assistLayout/AssistLayout.tsx.html +199 -0
- package/src/coverage/src/assistLayout/RunInstructionProvider.tsx.html +751 -0
- package/src/coverage/src/assistLayout/index.html +146 -0
- package/src/coverage/src/components/AssistFeatureBadge.tsx.html +112 -0
- package/src/coverage/src/components/FadeInContent.tsx.html +205 -0
- package/src/coverage/src/components/HideReferenceChangedBannerInput.tsx.html +160 -0
- package/src/coverage/src/components/ImageContext.tsx.html +229 -0
- package/src/coverage/src/components/SafeValueInput.tsx.html +313 -0
- package/src/coverage/src/components/TimeAgo.tsx.html +139 -0
- package/src/coverage/src/components/index.html +191 -0
- package/src/coverage/src/constants.ts.html +145 -0
- package/src/coverage/src/fieldActions/PrivateIcon.tsx.html +145 -0
- package/src/coverage/src/fieldActions/assistFieldActions.tsx.html +907 -0
- package/src/coverage/src/fieldActions/generateCaptionActions.tsx.html +259 -0
- package/src/coverage/src/fieldActions/generateImageActions.tsx.html +256 -0
- package/src/coverage/src/fieldActions/index.html +161 -0
- package/src/coverage/src/helpers/assistSupported.ts.html +226 -0
- package/src/coverage/src/helpers/conditionalMembers.ts.html +466 -0
- package/src/coverage/src/helpers/ids.ts.html +142 -0
- package/src/coverage/src/helpers/index.html +191 -0
- package/src/coverage/src/helpers/misc.ts.html +133 -0
- package/src/coverage/src/helpers/typeUtils.ts.html +208 -0
- package/src/coverage/src/helpers/useAssistSupported.ts.html +106 -0
- package/src/coverage/src/index.html +176 -0
- package/src/coverage/src/index.ts.html +112 -0
- package/src/coverage/src/onboarding/FieldActionsOnboarding.tsx.html +286 -0
- package/src/coverage/src/onboarding/FirstAssistedPathProvider.tsx.html +172 -0
- package/src/coverage/src/onboarding/InspectorOnboarding.tsx.html +223 -0
- package/src/coverage/src/onboarding/index.html +161 -0
- package/src/coverage/src/onboarding/onboardingStore.ts.html +184 -0
- package/src/coverage/src/plugin.tsx.html +427 -0
- package/src/coverage/src/presence/AiFieldPresence.tsx.html +163 -0
- package/src/coverage/src/presence/AssistAvatar.tsx.html +373 -0
- package/src/coverage/src/presence/AssistDocumentPresence.tsx.html +259 -0
- package/src/coverage/src/presence/index.html +161 -0
- package/src/coverage/src/presence/useAssistPresence.ts.html +268 -0
- package/src/coverage/src/schemas/assistDocumentSchema.tsx.html +1552 -0
- package/src/coverage/src/schemas/contextDocumentSchema.tsx.html +253 -0
- package/src/coverage/src/schemas/index.html +176 -0
- package/src/coverage/src/schemas/index.ts.html +277 -0
- package/src/coverage/src/schemas/serialize/SchemTypeTool.tsx.html +391 -0
- package/src/coverage/src/schemas/serialize/index.html +146 -0
- package/src/coverage/src/schemas/serialize/schemaUtils.ts.html +196 -0
- package/src/coverage/src/schemas/serialize/serializeSchema.ts.html +754 -0
- package/src/coverage/src/schemas/serializedSchemaTypeSchema.ts.html +262 -0
- package/src/coverage/src/schemas/typeDefExtensions.ts.html +397 -0
- package/src/coverage/src/translate/FieldTranslationProvider.tsx.html +1003 -0
- package/src/coverage/src/translate/getLanguageParams.ts.html +163 -0
- package/src/coverage/src/translate/index.html +191 -0
- package/src/coverage/src/translate/languageStore.ts.html +139 -0
- package/src/coverage/src/translate/paths.ts.html +577 -0
- package/src/coverage/src/translate/translateActions.tsx.html +637 -0
- package/src/coverage/src/translate/types.ts.html +565 -0
- package/src/coverage/src/types.ts.html +649 -0
- package/src/coverage/src/useApiClient.ts.html +1015 -0
- package/src/fieldActions/assistFieldActions.tsx +42 -13
- package/src/fieldActions/generateCaptionActions.tsx +2 -2
- package/src/fieldActions/generateImageActions.tsx +57 -0
- package/src/helpers/assistSupported.ts +10 -16
- package/src/helpers/conditionalMembers.test.ts +200 -0
- package/src/helpers/conditionalMembers.ts +127 -0
- package/src/helpers/typeUtils.ts +19 -5
- package/src/index.ts +3 -0
- package/src/node_modules/.vitest/deps/_metadata.json +8 -0
- package/src/node_modules/.vitest/deps/package.json +3 -0
- package/src/node_modules/.vitest/results.json +1 -0
- package/src/plugin.tsx +14 -5
- package/src/presence/AssistAvatar.tsx +1 -1
- package/src/schemas/assistDocumentSchema.tsx +40 -1
- package/src/schemas/serialize/serializeSchema.test.ts +239 -8
- package/src/schemas/serialize/serializeSchema.ts +77 -10
- package/src/schemas/typeDefExtensions.ts +89 -5
- package/src/translate/FieldTranslationProvider.tsx +306 -0
- package/src/translate/getLanguageParams.ts +26 -0
- package/src/translate/languageStore.ts +18 -0
- package/src/translate/paths.test.ts +133 -0
- package/src/translate/paths.ts +175 -0
- package/src/translate/translateActions.tsx +186 -0
- package/src/translate/types.ts +160 -0
- package/src/types.ts +33 -12
- package/src/useApiClient.ts +130 -2
- package/src/assistLayout/AlphaMigration.tsx +0 -310
- package/src/legacy-types.ts +0 -72
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
PropsWithChildren,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useId,
|
|
7
|
+
useMemo,
|
|
8
|
+
useState,
|
|
9
|
+
} from 'react'
|
|
10
|
+
import {ObjectSchemaType, Path, pathToString, SanityDocumentLike, useClient} from 'sanity'
|
|
11
|
+
import {useAiAssistanceConfig} from '../assistLayout/AiAssistanceConfigContext'
|
|
12
|
+
import {useApiClient, useTranslate} from '../useApiClient'
|
|
13
|
+
import {Box, Button, Checkbox, Dialog, Flex, Radio, Spinner, Stack, Text, Tooltip} from '@sanity/ui'
|
|
14
|
+
import {
|
|
15
|
+
defaultLanguageOutputs,
|
|
16
|
+
FieldLanguageMap,
|
|
17
|
+
getDocumentMembersFlat,
|
|
18
|
+
getFieldLanguageMap,
|
|
19
|
+
} from './paths'
|
|
20
|
+
import {PlayIcon} from '@sanity/icons'
|
|
21
|
+
import {Language} from './types'
|
|
22
|
+
import {getLanguageParams} from './getLanguageParams'
|
|
23
|
+
import {getPreferredToFieldLanguages, setPreferredToFieldLanguages} from './languageStore'
|
|
24
|
+
import {ConditionalMemberState} from '../helpers/conditionalMembers'
|
|
25
|
+
|
|
26
|
+
interface FieldTranslationParams {
|
|
27
|
+
document: SanityDocumentLike
|
|
28
|
+
documentSchema: ObjectSchemaType
|
|
29
|
+
translatePath: Path
|
|
30
|
+
conditionalMembers: ConditionalMemberState[]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface FieldTranslationContextValue {
|
|
34
|
+
openFieldTranslation: (args: FieldTranslationParams) => void
|
|
35
|
+
translationLoading: boolean
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const FieldTranslationContext = createContext<FieldTranslationContextValue>({
|
|
39
|
+
openFieldTranslation: () => {},
|
|
40
|
+
translationLoading: false,
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
export function useFieldTranslation() {
|
|
44
|
+
return useContext(FieldTranslationContext)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function hasValuesToTranslate(
|
|
48
|
+
fieldLanguageMaps: FieldLanguageMap[] | undefined,
|
|
49
|
+
fromLanguage: Language | undefined,
|
|
50
|
+
basePath: Path
|
|
51
|
+
) {
|
|
52
|
+
return fieldLanguageMaps?.some(
|
|
53
|
+
(map) =>
|
|
54
|
+
map.inputLanguageId === fromLanguage?.id &&
|
|
55
|
+
map.inputPath &&
|
|
56
|
+
pathToString(map.inputPath).startsWith(pathToString(basePath))
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function FieldTranslationProvider(props: PropsWithChildren<{}>) {
|
|
61
|
+
const {config: assistConfig} = useAiAssistanceConfig()
|
|
62
|
+
const apiClient = useApiClient(assistConfig.__customApiClient)
|
|
63
|
+
const config = assistConfig.translate?.field
|
|
64
|
+
const {translate: runTranslate} = useTranslate(apiClient)
|
|
65
|
+
|
|
66
|
+
const [dialogOpen, setDialogOpen] = useState(false)
|
|
67
|
+
|
|
68
|
+
const [fieldTranslationParams, setFieldTranslationParams] = useState<
|
|
69
|
+
FieldTranslationParams | undefined
|
|
70
|
+
>()
|
|
71
|
+
const [languages, setLanguages] = useState<Language[] | undefined>()
|
|
72
|
+
const [fromLanguage, setFromLanguage] = useState<Language | undefined>(undefined)
|
|
73
|
+
const [toLanguages, setToLanguages] = useState<Language[] | undefined>(undefined)
|
|
74
|
+
const [fieldLanguageMaps, setFieldLanguageMaps] = useState<FieldLanguageMap[] | undefined>()
|
|
75
|
+
|
|
76
|
+
const close = useCallback(() => {
|
|
77
|
+
setDialogOpen(false)
|
|
78
|
+
setLanguages(undefined)
|
|
79
|
+
setFieldTranslationParams(undefined)
|
|
80
|
+
}, [])
|
|
81
|
+
const languageClient = useClient({apiVersion: config?.apiVersion ?? '2022-11-27'})
|
|
82
|
+
const documentId = fieldTranslationParams?.document?._id
|
|
83
|
+
const id = useId()
|
|
84
|
+
|
|
85
|
+
const selectFromLanguage = useCallback(
|
|
86
|
+
(
|
|
87
|
+
from: Language,
|
|
88
|
+
languages: Language[] | undefined,
|
|
89
|
+
params: FieldTranslationParams | undefined
|
|
90
|
+
) => {
|
|
91
|
+
const {document, documentSchema} = params ?? {}
|
|
92
|
+
setFromLanguage(from)
|
|
93
|
+
if (!document || !documentSchema || !params || !languages) {
|
|
94
|
+
setFieldLanguageMaps(undefined)
|
|
95
|
+
return
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const preferred = getPreferredToFieldLanguages(from.id)
|
|
99
|
+
const allToLanguages = languages.filter((l) => l.id !== from?.id)
|
|
100
|
+
const filteredToLanguages = allToLanguages.filter(
|
|
101
|
+
(l) => !preferred.length || preferred.includes(l.id)
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
setToLanguages(filteredToLanguages)
|
|
105
|
+
const fromId = from?.id
|
|
106
|
+
const allToIds = allToLanguages?.map((l) => l.id) ?? []
|
|
107
|
+
const docMembers = getDocumentMembersFlat(document, documentSchema)
|
|
108
|
+
if (fromId && allToIds?.length) {
|
|
109
|
+
const transMap = getFieldLanguageMap(
|
|
110
|
+
documentSchema,
|
|
111
|
+
docMembers,
|
|
112
|
+
fromId,
|
|
113
|
+
allToIds.filter((toId) => fromId !== toId),
|
|
114
|
+
config?.translationOutputs ?? defaultLanguageOutputs
|
|
115
|
+
)
|
|
116
|
+
setFieldLanguageMaps(transMap)
|
|
117
|
+
} else {
|
|
118
|
+
setFieldLanguageMaps(undefined)
|
|
119
|
+
}
|
|
120
|
+
},
|
|
121
|
+
[config]
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
const toggleToLanguage = useCallback(
|
|
125
|
+
(
|
|
126
|
+
toggledLang: Language,
|
|
127
|
+
toLanguages: Language[] | undefined,
|
|
128
|
+
languages: Language[] | undefined
|
|
129
|
+
) => {
|
|
130
|
+
if (!languages || !fromLanguage) {
|
|
131
|
+
return
|
|
132
|
+
}
|
|
133
|
+
const wasSelected = !!toLanguages?.find((l) => l.id === toggledLang.id)
|
|
134
|
+
const newToLangs = languages.filter(
|
|
135
|
+
(anyLang) =>
|
|
136
|
+
!!toLanguages?.find(
|
|
137
|
+
(selectedLang) => toggledLang.id !== selectedLang.id && selectedLang.id === anyLang.id
|
|
138
|
+
) ||
|
|
139
|
+
(toggledLang.id === anyLang.id && !wasSelected)
|
|
140
|
+
)
|
|
141
|
+
setToLanguages(newToLangs)
|
|
142
|
+
setPreferredToFieldLanguages(
|
|
143
|
+
fromLanguage.id,
|
|
144
|
+
newToLangs.map((l) => l.id)
|
|
145
|
+
)
|
|
146
|
+
},
|
|
147
|
+
[fromLanguage]
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
const openFieldTranslation = useCallback(
|
|
151
|
+
async (params: FieldTranslationParams) => {
|
|
152
|
+
setDialogOpen(true)
|
|
153
|
+
const languageParams = getLanguageParams(config?.selectLanguageParams, params.document)
|
|
154
|
+
const languages: Language[] | undefined = await (typeof config?.languages === 'function'
|
|
155
|
+
? config?.languages(languageClient, languageParams)
|
|
156
|
+
: Promise.resolve(config?.languages))
|
|
157
|
+
setLanguages(languages)
|
|
158
|
+
setFieldTranslationParams(params)
|
|
159
|
+
const fromLanguage = languages?.[0]
|
|
160
|
+
if (fromLanguage) {
|
|
161
|
+
selectFromLanguage(fromLanguage, languages, params)
|
|
162
|
+
} else {
|
|
163
|
+
console.error('No languages available for selected language params', languageParams)
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
[selectFromLanguage, config, languageClient]
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
const contextValue: FieldTranslationContextValue = useMemo(() => {
|
|
170
|
+
return {
|
|
171
|
+
openFieldTranslation,
|
|
172
|
+
translationLoading: false,
|
|
173
|
+
}
|
|
174
|
+
}, [openFieldTranslation])
|
|
175
|
+
|
|
176
|
+
const runDisabled =
|
|
177
|
+
!fromLanguage ||
|
|
178
|
+
!toLanguages?.length ||
|
|
179
|
+
!fieldLanguageMaps?.length ||
|
|
180
|
+
!documentId ||
|
|
181
|
+
!hasValuesToTranslate(fieldLanguageMaps, fromLanguage, fieldTranslationParams.translatePath)
|
|
182
|
+
|
|
183
|
+
const onRunTranslation = useCallback(() => {
|
|
184
|
+
const translatePath = fieldTranslationParams?.translatePath
|
|
185
|
+
if (fieldLanguageMaps && documentId && translatePath) {
|
|
186
|
+
runTranslate({
|
|
187
|
+
documentId,
|
|
188
|
+
translatePath: translatePath,
|
|
189
|
+
fieldLanguageMap: fieldLanguageMaps.map((map) => ({
|
|
190
|
+
...map,
|
|
191
|
+
// eslint-disable-next-line max-nested-callbacks
|
|
192
|
+
outputs: map.outputs.filter((out) => !!toLanguages?.find((l) => l.id === out.id)),
|
|
193
|
+
})),
|
|
194
|
+
conditionalMembers: fieldTranslationParams?.conditionalMembers,
|
|
195
|
+
})
|
|
196
|
+
}
|
|
197
|
+
close()
|
|
198
|
+
}, [
|
|
199
|
+
fieldLanguageMaps,
|
|
200
|
+
documentId,
|
|
201
|
+
runTranslate,
|
|
202
|
+
close,
|
|
203
|
+
toLanguages,
|
|
204
|
+
fieldTranslationParams?.translatePath,
|
|
205
|
+
])
|
|
206
|
+
|
|
207
|
+
const runButton = (
|
|
208
|
+
<Button
|
|
209
|
+
text={`Translate`}
|
|
210
|
+
tone="primary"
|
|
211
|
+
icon={PlayIcon}
|
|
212
|
+
style={{width: '100%'}}
|
|
213
|
+
disabled={runDisabled}
|
|
214
|
+
onClick={onRunTranslation}
|
|
215
|
+
/>
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
return (
|
|
219
|
+
<FieldTranslationContext.Provider value={contextValue}>
|
|
220
|
+
{dialogOpen ? (
|
|
221
|
+
<Dialog
|
|
222
|
+
id={id}
|
|
223
|
+
width={1}
|
|
224
|
+
open={dialogOpen}
|
|
225
|
+
onClose={close}
|
|
226
|
+
header="Translate fields"
|
|
227
|
+
footer={
|
|
228
|
+
<Flex justify="space-between" padding={2} flex={1}>
|
|
229
|
+
{runDisabled ? (
|
|
230
|
+
<Tooltip
|
|
231
|
+
content={
|
|
232
|
+
<Flex padding={2}>
|
|
233
|
+
<Text>There is nothing to translate in the selected from-language.</Text>
|
|
234
|
+
</Flex>
|
|
235
|
+
}
|
|
236
|
+
placement="top"
|
|
237
|
+
>
|
|
238
|
+
<Flex flex={1}>{runButton}</Flex>
|
|
239
|
+
</Tooltip>
|
|
240
|
+
) : (
|
|
241
|
+
runButton
|
|
242
|
+
)}
|
|
243
|
+
</Flex>
|
|
244
|
+
}
|
|
245
|
+
>
|
|
246
|
+
{languages ? (
|
|
247
|
+
<Flex padding={4} gap={5} align="flex-start" justify="center">
|
|
248
|
+
<Stack space={2}>
|
|
249
|
+
<Box marginBottom={2}>
|
|
250
|
+
<Text weight="semibold">From</Text>
|
|
251
|
+
</Box>
|
|
252
|
+
{languages?.map((l) => {
|
|
253
|
+
return (
|
|
254
|
+
<Flex key={l.id} gap={3} align="center" as={'label'}>
|
|
255
|
+
<Radio
|
|
256
|
+
name="fromLang"
|
|
257
|
+
value={l.id}
|
|
258
|
+
checked={l.id === fromLanguage?.id}
|
|
259
|
+
onChange={() => selectFromLanguage(l, languages, fieldTranslationParams)}
|
|
260
|
+
/>
|
|
261
|
+
<Text>{l.title ?? l.id}</Text>
|
|
262
|
+
</Flex>
|
|
263
|
+
)
|
|
264
|
+
})}
|
|
265
|
+
</Stack>
|
|
266
|
+
|
|
267
|
+
<Stack space={2}>
|
|
268
|
+
<Box marginBottom={2}>
|
|
269
|
+
<Text weight="semibold">To</Text>
|
|
270
|
+
</Box>
|
|
271
|
+
{languages.map((l) => (
|
|
272
|
+
<Flex
|
|
273
|
+
key={l.id}
|
|
274
|
+
gap={3}
|
|
275
|
+
align="center"
|
|
276
|
+
as={'label'}
|
|
277
|
+
style={l.id === fromLanguage?.id ? {opacity: 0.5} : undefined}
|
|
278
|
+
>
|
|
279
|
+
<Checkbox
|
|
280
|
+
name="toLang"
|
|
281
|
+
value={l.id}
|
|
282
|
+
checked={
|
|
283
|
+
l.id !== fromLanguage?.id && !!toLanguages?.find((tl) => tl.id === l.id)
|
|
284
|
+
}
|
|
285
|
+
onChange={() => toggleToLanguage(l, toLanguages, languages)}
|
|
286
|
+
disabled={l.id === fromLanguage?.id}
|
|
287
|
+
/>
|
|
288
|
+
<Text muted={l.id === fromLanguage?.id}>{l.title ?? l.id}</Text>
|
|
289
|
+
</Flex>
|
|
290
|
+
))}
|
|
291
|
+
</Stack>
|
|
292
|
+
</Flex>
|
|
293
|
+
) : (
|
|
294
|
+
<Flex padding={4} gap={2} align="flex-start" justify="center">
|
|
295
|
+
<Box>
|
|
296
|
+
<Spinner />
|
|
297
|
+
</Box>
|
|
298
|
+
<Text>Loading languages...</Text>
|
|
299
|
+
</Flex>
|
|
300
|
+
)}
|
|
301
|
+
</Dialog>
|
|
302
|
+
) : null}
|
|
303
|
+
{props.children}
|
|
304
|
+
</FieldTranslationContext.Provider>
|
|
305
|
+
)
|
|
306
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import {SanityDocumentLike} from 'sanity'
|
|
2
|
+
import get from 'lodash/get'
|
|
3
|
+
|
|
4
|
+
export const getLanguageParams = (
|
|
5
|
+
select: Record<string, string> | undefined,
|
|
6
|
+
document: SanityDocumentLike | undefined
|
|
7
|
+
): Record<string, unknown> => {
|
|
8
|
+
if (!select || !document) {
|
|
9
|
+
return {}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const selection: Record<string, string> = select || {}
|
|
13
|
+
const selectedValue: Record<string, unknown> = {}
|
|
14
|
+
for (const [key, path] of Object.entries(selection)) {
|
|
15
|
+
let value = get(document, path)
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
// If there are references in the array, ensure they have `_ref` set, otherwise they are considered empty and can safely be ignored
|
|
18
|
+
value = value.filter((item) =>
|
|
19
|
+
typeof item === 'object' ? item?._type !== 'reference' || '_ref' in item : true
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
selectedValue[key] = value
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return selectedValue
|
|
26
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export const toFieldLanguagesKeyPrefix = 'sanityStudio:assist:field-languages:from:'
|
|
2
|
+
|
|
3
|
+
export function getPreferredToFieldLanguages(fromLanguageId: string): string[] {
|
|
4
|
+
if (typeof localStorage === 'undefined') {
|
|
5
|
+
return []
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const value = localStorage.getItem(`${toFieldLanguagesKeyPrefix}${fromLanguageId}`)
|
|
9
|
+
return value ? (JSON.parse(value) as string[]) : []
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function setPreferredToFieldLanguages(fromLanguageId: string, languageIds: string[]) {
|
|
13
|
+
if (typeof localStorage === 'undefined') {
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
localStorage.setItem(`${toFieldLanguagesKeyPrefix}${fromLanguageId}`, JSON.stringify(languageIds))
|
|
18
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {describe, expect, test} from 'vitest'
|
|
2
|
+
import {Schema} from '@sanity/schema'
|
|
3
|
+
import {defineType, ObjectSchemaType, pathToString, SanityDocumentLike, typed} from 'sanity'
|
|
4
|
+
import {
|
|
5
|
+
defaultLanguageOutputs,
|
|
6
|
+
FieldLanguageMap,
|
|
7
|
+
getDocumentMembersFlat,
|
|
8
|
+
getFieldLanguageMap,
|
|
9
|
+
} from './paths'
|
|
10
|
+
|
|
11
|
+
describe('paths', () => {
|
|
12
|
+
test('should return internationalizedArrayString paths and find translation mappings', () => {
|
|
13
|
+
const docSchema: ObjectSchemaType = Schema.compile({
|
|
14
|
+
name: 'test',
|
|
15
|
+
types: [
|
|
16
|
+
defineType({
|
|
17
|
+
type: 'document',
|
|
18
|
+
name: 'article',
|
|
19
|
+
fields: [
|
|
20
|
+
{type: 'string', name: 'title'},
|
|
21
|
+
{
|
|
22
|
+
type: 'object',
|
|
23
|
+
name: 'localeTitle',
|
|
24
|
+
fields: [
|
|
25
|
+
{type: 'string', name: 'en'},
|
|
26
|
+
{type: 'string', name: 'no'},
|
|
27
|
+
],
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
type: 'array',
|
|
31
|
+
name: 'translations',
|
|
32
|
+
of: [
|
|
33
|
+
{
|
|
34
|
+
type: 'object',
|
|
35
|
+
name: 'internationalizedArrayString',
|
|
36
|
+
fields: [{type: 'string', name: 'value'}],
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
},
|
|
40
|
+
],
|
|
41
|
+
}),
|
|
42
|
+
],
|
|
43
|
+
}).get('article')
|
|
44
|
+
|
|
45
|
+
const doc: SanityDocumentLike = {
|
|
46
|
+
_id: 'na',
|
|
47
|
+
_type: 'article',
|
|
48
|
+
title: 'some title',
|
|
49
|
+
localeTitle: {
|
|
50
|
+
en: 'en string',
|
|
51
|
+
},
|
|
52
|
+
translations: [
|
|
53
|
+
{
|
|
54
|
+
_type: 'internationalizedArrayString',
|
|
55
|
+
_key: 'en',
|
|
56
|
+
value: 'some string',
|
|
57
|
+
},
|
|
58
|
+
],
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const members = getDocumentMembersFlat(doc, docSchema)
|
|
62
|
+
expect(members.map((p) => pathToString(p.path))).toEqual([
|
|
63
|
+
'title',
|
|
64
|
+
'localeTitle',
|
|
65
|
+
'localeTitle.en',
|
|
66
|
+
// this path has no value in the document, so are not included
|
|
67
|
+
//'localeTitle.no',
|
|
68
|
+
'translations',
|
|
69
|
+
'translations[_key=="en"]',
|
|
70
|
+
'translations[_key=="en"].value',
|
|
71
|
+
// these path has no value in the document, so are not included
|
|
72
|
+
//'translations[_key=="nb"]',
|
|
73
|
+
//'translations[_key=="nb"].value',
|
|
74
|
+
])
|
|
75
|
+
|
|
76
|
+
const transMap = getFieldLanguageMap(docSchema, members, 'en', ['nb'], defaultLanguageOutputs)
|
|
77
|
+
|
|
78
|
+
expect(transMap).toEqual(
|
|
79
|
+
typed<FieldLanguageMap[]>([
|
|
80
|
+
{
|
|
81
|
+
inputLanguageId: 'en',
|
|
82
|
+
inputPath: ['translations', {_key: 'en'}],
|
|
83
|
+
outputs: [{id: 'nb', outputPath: ['translations', {_key: 'nb'}]}],
|
|
84
|
+
},
|
|
85
|
+
])
|
|
86
|
+
)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
test('should use first type in array when array item is missing _type', () => {
|
|
90
|
+
const docSchema: ObjectSchemaType = Schema.compile({
|
|
91
|
+
name: 'test',
|
|
92
|
+
types: [
|
|
93
|
+
defineType({
|
|
94
|
+
type: 'document',
|
|
95
|
+
name: 'article',
|
|
96
|
+
fields: [
|
|
97
|
+
{
|
|
98
|
+
type: 'array',
|
|
99
|
+
name: 'translations',
|
|
100
|
+
of: [
|
|
101
|
+
{
|
|
102
|
+
type: 'object',
|
|
103
|
+
name: 'internationalizedArrayString',
|
|
104
|
+
fields: [{type: 'string', name: 'value'}],
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
}),
|
|
110
|
+
],
|
|
111
|
+
}).get('article')
|
|
112
|
+
|
|
113
|
+
const doc: SanityDocumentLike = {
|
|
114
|
+
_id: 'na',
|
|
115
|
+
_type: 'article',
|
|
116
|
+
translations: [
|
|
117
|
+
{
|
|
118
|
+
//assume type is missing in the data for some reason
|
|
119
|
+
//_type: 'internationalizedArrayString',
|
|
120
|
+
_key: 'en',
|
|
121
|
+
value: 'some string',
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const members = getDocumentMembersFlat(doc, docSchema)
|
|
127
|
+
expect(members.map((p) => pathToString(p.path))).toEqual([
|
|
128
|
+
'translations',
|
|
129
|
+
'translations[_key=="en"]',
|
|
130
|
+
'translations[_key=="en"].value',
|
|
131
|
+
])
|
|
132
|
+
})
|
|
133
|
+
})
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isDocumentSchemaType,
|
|
3
|
+
isKeySegment,
|
|
4
|
+
ObjectSchemaType,
|
|
5
|
+
Path,
|
|
6
|
+
pathToString,
|
|
7
|
+
SanityDocumentLike,
|
|
8
|
+
} from 'sanity'
|
|
9
|
+
import {extractWithPath} from '@sanity/mutator'
|
|
10
|
+
import {DocumentMember, TranslationOutput, TranslationOutputsFunction} from './types'
|
|
11
|
+
|
|
12
|
+
export interface FieldLanguageMap {
|
|
13
|
+
inputLanguageId: string
|
|
14
|
+
inputPath: Path
|
|
15
|
+
outputs: TranslationOutput[]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const MAX_DEPTH = 6
|
|
19
|
+
|
|
20
|
+
export function getDocumentMembersFlat(doc: SanityDocumentLike, schemaType: ObjectSchemaType) {
|
|
21
|
+
if (!isDocumentSchemaType(schemaType)) {
|
|
22
|
+
console.error(`Schema type is not a document`)
|
|
23
|
+
return []
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return extractPaths(doc, schemaType, [], MAX_DEPTH)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function extractPaths(
|
|
30
|
+
doc: SanityDocumentLike,
|
|
31
|
+
schemaType: ObjectSchemaType,
|
|
32
|
+
path: Path,
|
|
33
|
+
maxDepth: number
|
|
34
|
+
): DocumentMember[] {
|
|
35
|
+
if (path.length >= maxDepth) {
|
|
36
|
+
return []
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return schemaType.fields.reduce<DocumentMember[]>((acc, field) => {
|
|
40
|
+
const fieldPath = [...path, field.name]
|
|
41
|
+
const fieldSchema = field.type
|
|
42
|
+
const {value} = extractWithPath(pathToString(fieldPath), doc)[0] ?? {}
|
|
43
|
+
if (!value) {
|
|
44
|
+
return acc
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const thisFieldWithPath: DocumentMember = {
|
|
48
|
+
path: fieldPath,
|
|
49
|
+
name: field.name,
|
|
50
|
+
schemaType: fieldSchema,
|
|
51
|
+
value,
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (fieldSchema.jsonType === 'object') {
|
|
55
|
+
const innerFields = extractPaths(doc, fieldSchema, fieldPath, maxDepth)
|
|
56
|
+
|
|
57
|
+
return [...acc, thisFieldWithPath, ...innerFields]
|
|
58
|
+
} else if (
|
|
59
|
+
fieldSchema.jsonType === 'array' &&
|
|
60
|
+
fieldSchema.of.length &&
|
|
61
|
+
fieldSchema.of.some((item) => 'fields' in item)
|
|
62
|
+
) {
|
|
63
|
+
const {value: arrayValue} = extractWithPath(pathToString(fieldPath), doc)[0] ?? {}
|
|
64
|
+
|
|
65
|
+
let arrayPaths: DocumentMember[] = []
|
|
66
|
+
if ((arrayValue as any)?.length) {
|
|
67
|
+
for (const item of arrayValue as any[]) {
|
|
68
|
+
const itemPath = [...fieldPath, {_key: item._key}]
|
|
69
|
+
let itemSchema = fieldSchema.of.find((t) => t.name === item._type)
|
|
70
|
+
if (!item._type) {
|
|
71
|
+
itemSchema = fieldSchema.of[0]
|
|
72
|
+
console.warn(
|
|
73
|
+
'Array item is missing _type - using the first defined type in the array.of schema',
|
|
74
|
+
{
|
|
75
|
+
itemPath,
|
|
76
|
+
item,
|
|
77
|
+
itemSchema,
|
|
78
|
+
}
|
|
79
|
+
)
|
|
80
|
+
}
|
|
81
|
+
if (item._key && itemSchema) {
|
|
82
|
+
const innerFields = extractPaths(
|
|
83
|
+
doc,
|
|
84
|
+
itemSchema as ObjectSchemaType,
|
|
85
|
+
itemPath,
|
|
86
|
+
maxDepth
|
|
87
|
+
)
|
|
88
|
+
const arrayMember = {
|
|
89
|
+
path: itemPath,
|
|
90
|
+
name: item._key,
|
|
91
|
+
schemaType: itemSchema,
|
|
92
|
+
value: item,
|
|
93
|
+
}
|
|
94
|
+
arrayPaths = [...arrayPaths, arrayMember, ...innerFields]
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return [...acc, thisFieldWithPath, ...arrayPaths]
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return [...acc, thisFieldWithPath]
|
|
103
|
+
}, [])
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Default implementation for plugin config `translate.field.translationOutputs`
|
|
108
|
+
*
|
|
109
|
+
* @see FieldTranslationConfig#translationOutputs
|
|
110
|
+
*/
|
|
111
|
+
export const defaultLanguageOutputs: TranslationOutputsFunction = function (
|
|
112
|
+
member,
|
|
113
|
+
enclosingType,
|
|
114
|
+
translateFromLanguageId,
|
|
115
|
+
translateToLanguageIds
|
|
116
|
+
) {
|
|
117
|
+
if (
|
|
118
|
+
member.schemaType.jsonType === 'object' &&
|
|
119
|
+
member.schemaType.name.startsWith('internationalizedArray')
|
|
120
|
+
) {
|
|
121
|
+
const pathEnd = member.path.slice(-1)
|
|
122
|
+
|
|
123
|
+
const language = isKeySegment(pathEnd[0]) ? pathEnd[0]._key : null
|
|
124
|
+
return language === translateFromLanguageId
|
|
125
|
+
? translateToLanguageIds.map((translateToId) => ({
|
|
126
|
+
id: translateToId,
|
|
127
|
+
outputPath: [...member.path.slice(0, -1), {_key: translateToId}],
|
|
128
|
+
}))
|
|
129
|
+
: undefined
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (enclosingType.jsonType === 'object' && enclosingType.name.startsWith('locale')) {
|
|
133
|
+
return translateFromLanguageId === member.name
|
|
134
|
+
? translateToLanguageIds.map((translateToId) => ({
|
|
135
|
+
id: translateToId,
|
|
136
|
+
outputPath: [...member.path.slice(0, -1), translateToId],
|
|
137
|
+
}))
|
|
138
|
+
: undefined
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return undefined
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function getFieldLanguageMap(
|
|
145
|
+
documentSchema: ObjectSchemaType,
|
|
146
|
+
documentMembers: DocumentMember[],
|
|
147
|
+
translateFromLanguageId: string,
|
|
148
|
+
outputLanguageIds: string[],
|
|
149
|
+
langFn: TranslationOutputsFunction
|
|
150
|
+
): FieldLanguageMap[] {
|
|
151
|
+
const translationMaps: FieldLanguageMap[] = []
|
|
152
|
+
for (const member of documentMembers) {
|
|
153
|
+
const parentPath = member.path.slice(0, -1)
|
|
154
|
+
const enclosingType =
|
|
155
|
+
documentMembers.find((m) => pathToString(m.path) === pathToString(parentPath))?.schemaType ??
|
|
156
|
+
documentSchema
|
|
157
|
+
|
|
158
|
+
const translations = langFn(
|
|
159
|
+
member,
|
|
160
|
+
enclosingType,
|
|
161
|
+
translateFromLanguageId,
|
|
162
|
+
outputLanguageIds
|
|
163
|
+
)?.filter((translation) => translation.id !== translateFromLanguageId)
|
|
164
|
+
|
|
165
|
+
if (translations) {
|
|
166
|
+
translationMaps.push({
|
|
167
|
+
inputLanguageId: translateFromLanguageId,
|
|
168
|
+
inputPath: member.path,
|
|
169
|
+
outputs: translations,
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return translationMaps
|
|
175
|
+
}
|