@sanity/assist 2.0.4-canary.0 → 2.0.5
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 +233 -214
- package/dist/index.esm.js +1769 -1769
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1767 -1767
- package/dist/index.js.map +1 -1
- package/package.json +17 -18
- package/src/_lib/connector/ConnectFromRegion.tsx +4 -3
- package/src/_lib/connector/ConnectToRegion.tsx +1 -0
- package/src/_lib/connector/ConnectorRegion.tsx +2 -1
- package/src/_lib/connector/ConnectorsProvider.tsx +2 -1
- package/src/_lib/connector/ConnectorsStoreContext.ts +1 -0
- package/src/_lib/connector/index.ts +1 -1
- package/src/_lib/connector/mapConnectorToLine.ts +2 -2
- package/src/_lib/connector/useConnectorsStore.ts +2 -1
- package/src/_lib/connector/useRegionRects.ts +4 -3
- package/src/_lib/fixedListenQuery.ts +11 -11
- package/src/_lib/form/DocumentForm.tsx +4 -3
- package/src/_lib/form/helpers.ts +2 -1
- package/src/_lib/useListeningQuery.ts +4 -3
- package/src/assistConnectors/AssistConnectorsOverlay.tsx +1 -0
- package/src/assistConnectors/ConnectorPath.tsx +3 -2
- package/src/assistConnectors/draw/connectorPath.ts +13 -13
- package/src/assistDocument/AssistDocumentContext.tsx +3 -2
- package/src/assistDocument/AssistDocumentContextProvider.tsx +2 -1
- package/src/assistDocument/AssistDocumentInput.tsx +9 -8
- package/src/assistDocument/RequestRunInstructionProvider.tsx +4 -3
- package/src/assistDocument/components/AssistDocumentForm.tsx +25 -24
- package/src/assistDocument/components/FieldRefPreview.tsx +4 -3
- package/src/assistDocument/components/InstructionsArrayInput.tsx +4 -3
- package/src/assistDocument/components/SelectedFieldContext.tsx +1 -1
- package/src/assistDocument/components/helpers.ts +4 -4
- package/src/assistDocument/components/instruction/BackToInstructionsLink.tsx +3 -2
- package/src/assistDocument/components/instruction/FieldRefInput.tsx +5 -4
- package/src/assistDocument/components/instruction/InstructionInput.tsx +3 -2
- package/src/assistDocument/components/instruction/InstructionOutputField.tsx +2 -1
- package/src/assistDocument/components/instruction/InstructionOutputInput.tsx +14 -13
- package/src/assistDocument/components/instruction/PromptInput.tsx +4 -3
- package/src/assistDocument/components/instruction/appearance/IconInput.tsx +2 -2
- package/src/assistDocument/components/instruction/appearance/InstructionVisibility.tsx +1 -1
- package/src/assistDocument/hooks/useAssistDocumentContextValue.tsx +7 -6
- package/src/assistDocument/hooks/useInstructionToaster.tsx +6 -5
- package/src/assistDocument/hooks/useStudioAssistDocument.ts +14 -13
- package/src/assistFormComponents/AssistField.tsx +9 -8
- package/src/assistFormComponents/AssistFormBlock.tsx +5 -4
- package/src/assistFormComponents/AssistInlineFormBlock.tsx +1 -1
- package/src/assistFormComponents/AssistItem.tsx +3 -2
- package/src/assistFormComponents/validation/listItem.tsx +2 -2
- package/src/assistFormComponents/validation/validationList.tsx +3 -2
- package/src/assistInspector/AssistInspector.tsx +16 -15
- package/src/assistInspector/FieldAutocomplete.tsx +4 -3
- package/src/assistInspector/InstructionTaskHistoryButton.tsx +19 -18
- package/src/assistInspector/helpers.ts +10 -9
- package/src/assistInspector/index.ts +4 -3
- package/src/assistLayout/AiAssistanceConfigContext.tsx +1 -0
- package/src/assistLayout/AssistLayout.tsx +5 -4
- package/src/assistLayout/RunInstructionProvider.tsx +18 -15
- package/src/components/FadeInContent.tsx +1 -1
- package/src/components/HideReferenceChangedBannerInput.tsx +1 -1
- package/src/components/ImageContext.tsx +4 -3
- package/src/components/SafeValueInput.tsx +7 -6
- package/src/components/TimeAgo.tsx +1 -1
- package/src/fieldActions/assistFieldActions.tsx +31 -30
- package/src/fieldActions/generateCaptionActions.tsx +8 -7
- package/src/fieldActions/generateImageActions.tsx +6 -5
- package/src/helpers/assistSupported.ts +1 -0
- package/src/helpers/conditionalMembers.test.ts +2 -1
- package/src/helpers/conditionalMembers.ts +17 -14
- package/src/helpers/misc.ts +3 -2
- package/src/helpers/typeUtils.ts +1 -1
- package/src/helpers/useAssistSupported.ts +2 -1
- package/src/onboarding/FieldActionsOnboarding.tsx +2 -1
- package/src/onboarding/FirstAssistedPathProvider.tsx +4 -3
- package/src/onboarding/InspectorOnboarding.tsx +3 -2
- package/src/onboarding/onboardingStore.ts +1 -1
- package/src/presence/AiFieldPresence.tsx +3 -1
- package/src/presence/AssistDocumentPresence.tsx +7 -6
- package/src/presence/useAssistPresence.ts +6 -3
- package/src/schemas/assistDocumentSchema.tsx +21 -20
- package/src/schemas/contextDocumentSchema.tsx +3 -2
- package/src/schemas/index.ts +2 -1
- package/src/schemas/serialize/SchemTypeTool.tsx +4 -3
- package/src/schemas/serialize/serializeSchema.test.ts +3 -2
- package/src/schemas/serialize/serializeSchema.ts +15 -14
- package/src/schemas/serializedSchemaTypeSchema.ts +2 -1
- package/src/translate/FieldTranslationProvider.tsx +33 -25
- package/src/translate/getLanguageParams.ts +3 -3
- package/src/translate/paths.test.ts +11 -4
- package/src/translate/paths.ts +12 -11
- package/src/translate/translateActions.tsx +12 -11
- package/src/translate/types.ts +2 -2
- package/src/useApiClient.ts +9 -9
package/src/helpers/typeUtils.ts
CHANGED
|
@@ -30,7 +30,7 @@ export function getDescriptionFieldOption(schemaType: SchemaType | undefined): s
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
export function getImageInstructionFieldOption(
|
|
33
|
-
schemaType: SchemaType | undefined
|
|
33
|
+
schemaType: SchemaType | undefined,
|
|
34
34
|
): string | undefined {
|
|
35
35
|
if (!schemaType) {
|
|
36
36
|
return undefined
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import {ArrowRightIcon, CheckmarkIcon, SparklesIcon} from '@sanity/icons'
|
|
1
2
|
import {Button, Card, Flex, Popover, Stack, Text} from '@sanity/ui'
|
|
2
3
|
import {useRef} from 'react'
|
|
4
|
+
|
|
3
5
|
import {AssistFeatureBadge} from '../components/AssistFeatureBadge'
|
|
4
|
-
import {ArrowRightIcon, CheckmarkIcon, SparklesIcon} from '@sanity/icons'
|
|
5
6
|
import {pluginTitle, releaseAnnouncementUrl} from '../constants'
|
|
6
7
|
|
|
7
8
|
export interface FieldActionsOnboardingProps {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {ObjectInputProps, FieldMember, pathToString} from 'sanity'
|
|
2
1
|
import {createContext, PropsWithChildren, useMemo} from 'react'
|
|
2
|
+
import {FieldMember, ObjectInputProps, pathToString} from 'sanity'
|
|
3
|
+
|
|
3
4
|
import {isAssistSupported} from '../helpers/assistSupported'
|
|
4
5
|
|
|
5
6
|
export const FirstAssistedPathContext = createContext<string | undefined>(undefined)
|
|
@@ -9,14 +10,14 @@ export interface FirstAssistedPathProviderProps {
|
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
export function FirstAssistedPathProvider(
|
|
12
|
-
props: PropsWithChildren<FirstAssistedPathProviderProps
|
|
13
|
+
props: PropsWithChildren<FirstAssistedPathProviderProps>,
|
|
13
14
|
) {
|
|
14
15
|
const {members} = props
|
|
15
16
|
|
|
16
17
|
const firstAssistedPath = useMemo(() => {
|
|
17
18
|
const firstAssisted = members.find(
|
|
18
19
|
(member): member is FieldMember =>
|
|
19
|
-
member.kind === 'field' && isAssistSupported(member.field.schemaType)
|
|
20
|
+
member.kind === 'field' && isAssistSupported(member.field.schemaType),
|
|
20
21
|
)
|
|
21
22
|
return firstAssisted?.field.path ? pathToString(firstAssisted?.field.path) : undefined
|
|
22
23
|
}, [members])
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {Box, Button, Container, Flex, Stack, Text} from '@sanity/ui'
|
|
2
1
|
import {SparklesIcon} from '@sanity/icons'
|
|
3
|
-
import {
|
|
2
|
+
import {Box, Button, Container, Flex, Stack, Text} from '@sanity/ui'
|
|
4
3
|
import styled from 'styled-components'
|
|
5
4
|
|
|
5
|
+
import {releaseAnnouncementUrl} from '../constants'
|
|
6
|
+
|
|
6
7
|
const SparklesIllustration = styled(SparklesIcon)({
|
|
7
8
|
fontSize: '3.125em',
|
|
8
9
|
'& path': {
|
|
@@ -22,7 +22,7 @@ export function dismissFeatureOnboarding(featureKey: string) {
|
|
|
22
22
|
|
|
23
23
|
export function useOnboardingFeature(featureKey: string) {
|
|
24
24
|
const [showOnboarding, setShowOnboarding] = useState(
|
|
25
|
-
() => !isFeatureOnboardingDismissed(featureKey)
|
|
25
|
+
() => !isFeatureOnboardingDismissed(featureKey),
|
|
26
26
|
)
|
|
27
27
|
const dismissOnboarding = useCallback(() => {
|
|
28
28
|
setShowOnboarding(false)
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// eslint-disable-next-line react/no-unused-prop-types
|
|
2
|
-
import {FormNodePresence} from 'sanity'
|
|
3
2
|
import {Card, Flex, Text, Tooltip} from '@sanity/ui'
|
|
3
|
+
import type {FormNodePresence} from 'sanity'
|
|
4
|
+
|
|
4
5
|
import {FadeInContent} from '../components/FadeInContent'
|
|
5
6
|
import {AssistAvatar} from './AssistAvatar'
|
|
6
7
|
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
9
|
export function AiFieldPresence(props: {presence: FormNodePresence}) {
|
|
8
10
|
return (
|
|
9
11
|
<Card style={{position: 'relative', background: 'transparent'}} contentEditable={false}>
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {Card, Flex} from '@sanity/ui'
|
|
2
2
|
import {useMemo} from 'react'
|
|
3
|
+
import {ObjectSchemaType} from 'sanity'
|
|
4
|
+
|
|
3
5
|
import {useAssistDocumentContextValue} from '../assistDocument/hooks/useAssistDocumentContextValue'
|
|
4
|
-
import {aiPresence} from './useAssistPresence'
|
|
5
6
|
import {documentRootKey, fieldPresenceTypeName} from '../types'
|
|
6
|
-
import {Card, Flex} from '@sanity/ui'
|
|
7
7
|
import {AiFieldPresence} from './AiFieldPresence'
|
|
8
|
+
import {aiPresence} from './useAssistPresence'
|
|
8
9
|
|
|
9
10
|
export function createAssistDocumentPresence(
|
|
10
11
|
documentId: string | undefined,
|
|
11
|
-
schemaType: ObjectSchemaType
|
|
12
|
+
schemaType: ObjectSchemaType,
|
|
12
13
|
) {
|
|
13
14
|
return function AssistDocumentPresenceWrapper() {
|
|
14
15
|
return documentId ? (
|
|
@@ -20,7 +21,7 @@ export function createAssistDocumentPresence(
|
|
|
20
21
|
function AssistDocumentPresence(props: {documentId: string; schemaType: ObjectSchemaType}) {
|
|
21
22
|
const {assistDocument} = useAssistDocumentContextValue(
|
|
22
23
|
props.documentId,
|
|
23
|
-
props.schemaType as ObjectSchemaType
|
|
24
|
+
props.schemaType as ObjectSchemaType,
|
|
24
25
|
)
|
|
25
26
|
const anyPresence = useMemo(() => {
|
|
26
27
|
const anyPresence = assistDocument?.tasks
|
|
@@ -41,7 +42,7 @@ function AssistDocumentPresence(props: {documentId: string; schemaType: ObjectSc
|
|
|
41
42
|
_key: anyRun._key,
|
|
42
43
|
_type: fieldPresenceTypeName,
|
|
43
44
|
},
|
|
44
|
-
[]
|
|
45
|
+
[],
|
|
45
46
|
)
|
|
46
47
|
: undefined
|
|
47
48
|
}, [assistDocument?.tasks])
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import {useMemo} from 'react'
|
|
2
|
-
import {FormNodePresence, isKeySegment, Path, stringToPath} from 'sanity'
|
|
2
|
+
import {type FormNodePresence, isKeySegment, type Path, stringToPath} from 'sanity'
|
|
3
|
+
|
|
3
4
|
import {useAssistDocumentContext} from '../assistDocument/AssistDocumentContext'
|
|
4
|
-
import {AiPresence} from '../types'
|
|
5
5
|
import {maxHistoryVisibilityMs, pluginTitle} from '../constants'
|
|
6
|
+
import type {AiPresence} from '../types'
|
|
6
7
|
|
|
7
8
|
const NO_PRESENCE: FormNodePresence[] = []
|
|
8
9
|
|
|
@@ -17,7 +18,8 @@ export function useAssistPresence(path: Path, showFocusWithin?: boolean): FormNo
|
|
|
17
18
|
?.flatMap((task) => task.presence ?? [])
|
|
18
19
|
?.filter(
|
|
19
20
|
(p) =>
|
|
20
|
-
p.started &&
|
|
21
|
+
p.started &&
|
|
22
|
+
new Date().getTime() - new Date(p.started).getTime() < maxHistoryVisibilityMs,
|
|
21
23
|
)
|
|
22
24
|
.filter((presence) => {
|
|
23
25
|
if (!presence.path || !path.length) {
|
|
@@ -48,6 +50,7 @@ export function useAssistPresence(path: Path, showFocusWithin?: boolean): FormNo
|
|
|
48
50
|
}, [showFocusWithin, tasks, path])
|
|
49
51
|
}
|
|
50
52
|
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
51
54
|
export function aiPresence(presence: AiPresence, path: Path, title?: string): FormNodePresence {
|
|
52
55
|
return {
|
|
53
56
|
user: {
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {defineArrayMember, defineField, defineType, ObjectSchemaType} from 'sanity'
|
|
2
1
|
import {
|
|
3
2
|
ArrowRightIcon,
|
|
4
3
|
CodeIcon,
|
|
@@ -9,6 +8,25 @@ import {
|
|
|
9
8
|
SparklesIcon,
|
|
10
9
|
ThListIcon,
|
|
11
10
|
} from '@sanity/icons'
|
|
11
|
+
import {Box, Flex, Stack, Text, Tooltip} from '@sanity/ui'
|
|
12
|
+
import {createElement} from 'react'
|
|
13
|
+
import {defineArrayMember, defineField, defineType, ObjectSchemaType} from 'sanity'
|
|
14
|
+
|
|
15
|
+
import {AssistDocumentForm} from '../assistDocument/components/AssistDocumentForm'
|
|
16
|
+
import {FieldRefPreview} from '../assistDocument/components/FieldRefPreview'
|
|
17
|
+
import {HiddenFieldTitle} from '../assistDocument/components/generic/HiddenFieldTitle'
|
|
18
|
+
import {IconInput} from '../assistDocument/components/instruction/appearance/IconInput'
|
|
19
|
+
import {InstructionVisibility} from '../assistDocument/components/instruction/appearance/InstructionVisibility'
|
|
20
|
+
import {FieldRefPathInput} from '../assistDocument/components/instruction/FieldRefInput'
|
|
21
|
+
import {InstructionInput} from '../assistDocument/components/instruction/InstructionInput'
|
|
22
|
+
import {InstructionOutputField} from '../assistDocument/components/instruction/InstructionOutputField'
|
|
23
|
+
import {InstructionOutputInput} from '../assistDocument/components/instruction/InstructionOutputInput'
|
|
24
|
+
import {PromptInput} from '../assistDocument/components/instruction/PromptInput'
|
|
25
|
+
import {InstructionsArrayField} from '../assistDocument/components/InstructionsArrayField'
|
|
26
|
+
import {InstructionsArrayInput} from '../assistDocument/components/InstructionsArrayInput'
|
|
27
|
+
import {getFieldRefsWithDocument} from '../assistInspector/helpers'
|
|
28
|
+
import {instructionGuideUrl} from '../constants'
|
|
29
|
+
import {getInstructionTitle} from '../helpers/misc'
|
|
12
30
|
import {
|
|
13
31
|
assistDocumentIdPrefix,
|
|
14
32
|
assistDocumentTypeName,
|
|
@@ -23,24 +41,7 @@ import {
|
|
|
23
41
|
promptTypeName,
|
|
24
42
|
userInputTypeName,
|
|
25
43
|
} from '../types'
|
|
26
|
-
import {Box, Flex, Stack, Text, Tooltip} from '@sanity/ui'
|
|
27
|
-
import {getInstructionTitle} from '../helpers/misc'
|
|
28
|
-
import {AssistDocumentForm} from '../assistDocument/components/AssistDocumentForm'
|
|
29
|
-
import {InstructionInput} from '../assistDocument/components/instruction/InstructionInput'
|
|
30
|
-
import {HiddenFieldTitle} from '../assistDocument/components/generic/HiddenFieldTitle'
|
|
31
|
-
import {InstructionVisibility} from '../assistDocument/components/instruction/appearance/InstructionVisibility'
|
|
32
|
-
import {IconInput} from '../assistDocument/components/instruction/appearance/IconInput'
|
|
33
|
-
import {FieldRefPathInput} from '../assistDocument/components/instruction/FieldRefInput'
|
|
34
|
-
import {InstructionsArrayInput} from '../assistDocument/components/InstructionsArrayInput'
|
|
35
|
-
import {FieldRefPreview} from '../assistDocument/components/FieldRefPreview'
|
|
36
|
-
import {createElement} from 'react'
|
|
37
44
|
import {contextDocumentSchema} from './contextDocumentSchema'
|
|
38
|
-
import {PromptInput} from '../assistDocument/components/instruction/PromptInput'
|
|
39
|
-
import {instructionGuideUrl} from '../constants'
|
|
40
|
-
import {InstructionsArrayField} from '../assistDocument/components/InstructionsArrayField'
|
|
41
|
-
import {getFieldRefsWithDocument} from '../assistInspector/helpers'
|
|
42
|
-
import {InstructionOutputField} from '../assistDocument/components/instruction/InstructionOutputField'
|
|
43
|
-
import {InstructionOutputInput} from '../assistDocument/components/instruction/InstructionOutputInput'
|
|
44
45
|
|
|
45
46
|
export const fieldReference = defineType({
|
|
46
47
|
type: 'object',
|
|
@@ -346,7 +347,7 @@ export const instruction = defineType({
|
|
|
346
347
|
initialValue: (params, context) => context.currentUser?.id ?? '',
|
|
347
348
|
readOnly: (context) =>
|
|
348
349
|
Boolean(
|
|
349
|
-
context.parent?.createdById && context.parent?.createdById !== context.currentUser?.id
|
|
350
|
+
context.parent?.createdById && context.parent?.createdById !== context.currentUser?.id,
|
|
350
351
|
),
|
|
351
352
|
}),
|
|
352
353
|
defineField({
|
|
@@ -418,7 +419,7 @@ export const assistDocumentSchema = defineType({
|
|
|
418
419
|
|
|
419
420
|
components: {
|
|
420
421
|
input: AssistDocumentForm,
|
|
421
|
-
field: (props) => {
|
|
422
|
+
field: (props: any) => {
|
|
422
423
|
return props.renderDefault({...props, title: ''})
|
|
423
424
|
},
|
|
424
425
|
},
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import {defineArrayMember, defineField, defineType} from 'sanity'
|
|
2
|
-
import {contextDocumentTypeName} from '../types'
|
|
3
1
|
import {DocumentTextIcon, TokenIcon} from '@sanity/icons'
|
|
2
|
+
import {defineArrayMember, defineField, defineType} from 'sanity'
|
|
3
|
+
|
|
4
4
|
import {HideReferenceChangedBannerInput} from '../components/HideReferenceChangedBannerInput'
|
|
5
|
+
import {contextDocumentTypeName} from '../types'
|
|
5
6
|
|
|
6
7
|
export const contextDocumentSchema = defineType({
|
|
7
8
|
type: 'document',
|
package/src/schemas/index.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
/* eslint-disable camelcase */
|
|
2
|
+
import {ArrayOfType, FieldProps, SchemaTypeDefinition} from 'sanity'
|
|
3
|
+
|
|
2
4
|
import {
|
|
3
5
|
assistDocumentSchema,
|
|
4
6
|
documentInstructionStatus,
|
|
@@ -13,7 +15,6 @@ import {
|
|
|
13
15
|
userInput,
|
|
14
16
|
} from './assistDocumentSchema'
|
|
15
17
|
import {contextDocumentSchema} from './contextDocumentSchema'
|
|
16
|
-
import {FieldProps, SchemaTypeDefinition, ArrayOfType} from 'sanity'
|
|
17
18
|
|
|
18
19
|
function excludeComments<T extends SchemaTypeDefinition | ArrayOfType>(type: T): T {
|
|
19
20
|
const existingRender = (type as any)?.components?.field
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {SyncIcon} from '@sanity/icons'
|
|
2
2
|
import {Box, Button, Card, Flex, Label, Spinner, Stack} from '@sanity/ui'
|
|
3
3
|
import {useCallback, useMemo, useState} from 'react'
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import {useClient, useSchema} from 'sanity'
|
|
5
|
+
|
|
6
6
|
import {useListeningQuery} from '../../_lib/useListeningQuery'
|
|
7
|
+
import {assistSerializedTypeName, SerializedSchemaType} from '../../types'
|
|
7
8
|
import {serializeSchema} from './serializeSchema'
|
|
8
9
|
|
|
9
10
|
const NO_DATA: SerializedSchemaType[] = []
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {describe, expect, test} from 'vitest'
|
|
2
1
|
import {Schema} from '@sanity/schema'
|
|
3
|
-
import {serializeSchema} from './serializeSchema'
|
|
4
2
|
import {defineArrayMember, defineField, defineType} from 'sanity'
|
|
3
|
+
import {describe, expect, test} from 'vitest'
|
|
4
|
+
|
|
5
5
|
import {AssistOptions} from '../typeDefExtensions'
|
|
6
|
+
import {serializeSchema} from './serializeSchema'
|
|
6
7
|
|
|
7
8
|
const mockStudioTypes = [
|
|
8
9
|
defineField({
|
|
@@ -9,6 +9,9 @@ import {
|
|
|
9
9
|
SchemaType,
|
|
10
10
|
typed,
|
|
11
11
|
} from 'sanity'
|
|
12
|
+
|
|
13
|
+
import {isAssistSupported} from '../../helpers/assistSupported'
|
|
14
|
+
import {isType} from '../../helpers/typeUtils'
|
|
12
15
|
import {
|
|
13
16
|
assistSchemaIdPrefix,
|
|
14
17
|
assistSerializedFieldTypeName,
|
|
@@ -17,8 +20,6 @@ import {
|
|
|
17
20
|
SerializedSchemaType,
|
|
18
21
|
} from '../../types'
|
|
19
22
|
import {hiddenTypes} from './schemaUtils'
|
|
20
|
-
import {isAssistSupported} from '../../helpers/assistSupported'
|
|
21
|
-
import {isType} from '../../helpers/typeUtils'
|
|
22
23
|
|
|
23
24
|
interface Options {
|
|
24
25
|
leanFormat?: boolean
|
|
@@ -54,7 +55,7 @@ export function serializeSchema(schema: Schema, options?: Options): SerializedSc
|
|
|
54
55
|
function getSchemaStub(
|
|
55
56
|
schemaType: SchemaType,
|
|
56
57
|
schema: Schema,
|
|
57
|
-
options?: Options
|
|
58
|
+
options?: Options,
|
|
58
59
|
): SerializedSchemaType {
|
|
59
60
|
if (!schemaType.type?.name) {
|
|
60
61
|
console.error('Missing type name', schemaType.type)
|
|
@@ -78,7 +79,7 @@ function getBaseFields(
|
|
|
78
79
|
schema: Schema,
|
|
79
80
|
type: SchemaType,
|
|
80
81
|
typeName: string,
|
|
81
|
-
options: Options | undefined
|
|
82
|
+
options: Options | undefined,
|
|
82
83
|
) {
|
|
83
84
|
const schemaOptions: SerializedSchemaType['options'] = removeUndef({
|
|
84
85
|
imagePromptField: (type.options as ImageOptions)?.aiAssist?.imageInstructionField,
|
|
@@ -88,7 +89,7 @@ function getBaseFields(
|
|
|
88
89
|
options: Object.keys(schemaOptions).length ? schemaOptions : undefined,
|
|
89
90
|
values: Array.isArray(type?.options?.list)
|
|
90
91
|
? type?.options?.list.map((v: string | {value: string; title: string}) =>
|
|
91
|
-
typeof v === 'string' ? v : v.value ?? `${v.title}
|
|
92
|
+
typeof v === 'string' ? v : v.value ?? `${v.title}`,
|
|
92
93
|
)
|
|
93
94
|
: undefined,
|
|
94
95
|
of: 'of' in type && typeName === 'array' ? arrayOf(type, schema, options) : undefined,
|
|
@@ -114,15 +115,15 @@ function getBaseFields(
|
|
|
114
115
|
typeof type.readOnly === 'function'
|
|
115
116
|
? ('function' as const)
|
|
116
117
|
: type.readOnly
|
|
117
|
-
|
|
118
|
-
|
|
118
|
+
? true
|
|
119
|
+
: undefined,
|
|
119
120
|
})
|
|
120
121
|
}
|
|
121
122
|
|
|
122
123
|
function serializeFields(
|
|
123
124
|
schema: Schema,
|
|
124
125
|
schemaType: ObjectSchemaType,
|
|
125
|
-
options: Options | undefined
|
|
126
|
+
options: Options | undefined,
|
|
126
127
|
) {
|
|
127
128
|
const fields = schemaType.fieldsets
|
|
128
129
|
? schemaType.fieldsets.flatMap((fs) =>
|
|
@@ -138,7 +139,7 @@ function serializeFields(
|
|
|
138
139
|
hidden:
|
|
139
140
|
typeof fs.hidden === 'function' ? fs.hidden : fs.hidden ? true : f.type.hidden,
|
|
140
141
|
},
|
|
141
|
-
}))
|
|
142
|
+
})),
|
|
142
143
|
)
|
|
143
144
|
: schemaType.fields
|
|
144
145
|
|
|
@@ -152,7 +153,7 @@ function serializeMember(
|
|
|
152
153
|
schema: Schema,
|
|
153
154
|
type: SchemaType,
|
|
154
155
|
name: string,
|
|
155
|
-
options: Options | undefined
|
|
156
|
+
options: Options | undefined,
|
|
156
157
|
): SerializedSchemaMember {
|
|
157
158
|
const typeNameExists = !!schema.get(type?.name)
|
|
158
159
|
const typeName = typeNameExists ? type.name : type.type?.name ?? ''
|
|
@@ -168,7 +169,7 @@ function serializeMember(
|
|
|
168
169
|
function serializeInlineOf(
|
|
169
170
|
blockSchemaType: ObjectSchemaType,
|
|
170
171
|
schema: Schema,
|
|
171
|
-
options: Options | undefined
|
|
172
|
+
options: Options | undefined,
|
|
172
173
|
): SerializedSchemaMember[] | undefined {
|
|
173
174
|
const childrenField = blockSchemaType.fields.find((f) => f.name === 'children')
|
|
174
175
|
const childrenType = childrenField?.type
|
|
@@ -181,14 +182,14 @@ function serializeInlineOf(
|
|
|
181
182
|
of: childrenType.of.filter((t) => !isType(t, 'span')),
|
|
182
183
|
},
|
|
183
184
|
schema,
|
|
184
|
-
options
|
|
185
|
+
options,
|
|
185
186
|
)
|
|
186
187
|
}
|
|
187
188
|
|
|
188
189
|
function serializeAnnotations(
|
|
189
190
|
blockSchemaType: ObjectSchemaType,
|
|
190
191
|
schema: Schema,
|
|
191
|
-
options: Options | undefined
|
|
192
|
+
options: Options | undefined,
|
|
192
193
|
): SerializedSchemaMember[] | undefined {
|
|
193
194
|
const markDefs = blockSchemaType.fields.find((f) => f.name === 'markDefs')
|
|
194
195
|
const marksType = markDefs?.type
|
|
@@ -201,7 +202,7 @@ function serializeAnnotations(
|
|
|
201
202
|
function arrayOf(
|
|
202
203
|
arrayType: ArraySchemaType,
|
|
203
204
|
schema: Schema,
|
|
204
|
-
options: Options | undefined
|
|
205
|
+
options: Options | undefined,
|
|
205
206
|
): SerializedSchemaMember[] {
|
|
206
207
|
return arrayType.of
|
|
207
208
|
.filter((type) => isAssistSupported(type))
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import {CodeIcon, StarIcon} from '@sanity/icons'
|
|
1
2
|
import {defineField, defineType} from 'sanity'
|
|
3
|
+
|
|
2
4
|
import {assistSerializedFieldTypeName, assistSerializedTypeName} from '../types'
|
|
3
|
-
import {CodeIcon, StarIcon} from '@sanity/icons'
|
|
4
5
|
|
|
5
6
|
export const serializedSchemaField = defineType({
|
|
6
7
|
type: 'object',
|
|
@@ -1,27 +1,34 @@
|
|
|
1
|
+
import {PlayIcon} from '@sanity/icons'
|
|
2
|
+
import {Box, Button, Checkbox, Dialog, Flex, Radio, Spinner, Stack, Text, Tooltip} from '@sanity/ui'
|
|
1
3
|
import {
|
|
2
4
|
createContext,
|
|
3
|
-
PropsWithChildren,
|
|
5
|
+
type PropsWithChildren,
|
|
4
6
|
useCallback,
|
|
5
7
|
useContext,
|
|
6
8
|
useId,
|
|
7
9
|
useMemo,
|
|
8
10
|
useState,
|
|
9
11
|
} from 'react'
|
|
10
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
type ObjectSchemaType,
|
|
14
|
+
type Path,
|
|
15
|
+
pathToString,
|
|
16
|
+
type SanityDocumentLike,
|
|
17
|
+
useClient,
|
|
18
|
+
} from 'sanity'
|
|
19
|
+
|
|
11
20
|
import {useAiAssistanceConfig} from '../assistLayout/AiAssistanceConfigContext'
|
|
21
|
+
import type {ConditionalMemberState} from '../helpers/conditionalMembers'
|
|
12
22
|
import {useApiClient, useTranslate} from '../useApiClient'
|
|
13
|
-
import {
|
|
23
|
+
import {getLanguageParams} from './getLanguageParams'
|
|
24
|
+
import {getPreferredToFieldLanguages, setPreferredToFieldLanguages} from './languageStore'
|
|
14
25
|
import {
|
|
15
26
|
defaultLanguageOutputs,
|
|
16
|
-
FieldLanguageMap,
|
|
27
|
+
type FieldLanguageMap,
|
|
17
28
|
getDocumentMembersFlat,
|
|
18
29
|
getFieldLanguageMap,
|
|
19
30
|
} from './paths'
|
|
20
|
-
import {
|
|
21
|
-
import {Language} from './types'
|
|
22
|
-
import {getLanguageParams} from './getLanguageParams'
|
|
23
|
-
import {getPreferredToFieldLanguages, setPreferredToFieldLanguages} from './languageStore'
|
|
24
|
-
import {ConditionalMemberState} from '../helpers/conditionalMembers'
|
|
31
|
+
import type {Language} from './types'
|
|
25
32
|
|
|
26
33
|
interface FieldTranslationParams {
|
|
27
34
|
document: SanityDocumentLike
|
|
@@ -47,16 +54,17 @@ export function useFieldTranslation() {
|
|
|
47
54
|
function hasValuesToTranslate(
|
|
48
55
|
fieldLanguageMaps: FieldLanguageMap[] | undefined,
|
|
49
56
|
fromLanguage: Language | undefined,
|
|
50
|
-
basePath: Path
|
|
57
|
+
basePath: Path,
|
|
51
58
|
) {
|
|
52
59
|
return fieldLanguageMaps?.some(
|
|
53
60
|
(map) =>
|
|
54
61
|
map.inputLanguageId === fromLanguage?.id &&
|
|
55
62
|
map.inputPath &&
|
|
56
|
-
pathToString(map.inputPath).startsWith(pathToString(basePath))
|
|
63
|
+
pathToString(map.inputPath).startsWith(pathToString(basePath)),
|
|
57
64
|
)
|
|
58
65
|
}
|
|
59
66
|
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
60
68
|
export function FieldTranslationProvider(props: PropsWithChildren<{}>) {
|
|
61
69
|
const {config: assistConfig} = useAiAssistanceConfig()
|
|
62
70
|
const apiClient = useApiClient(assistConfig.__customApiClient)
|
|
@@ -86,7 +94,7 @@ export function FieldTranslationProvider(props: PropsWithChildren<{}>) {
|
|
|
86
94
|
(
|
|
87
95
|
from: Language,
|
|
88
96
|
languages: Language[] | undefined,
|
|
89
|
-
params: FieldTranslationParams | undefined
|
|
97
|
+
params: FieldTranslationParams | undefined,
|
|
90
98
|
) => {
|
|
91
99
|
const {document, documentSchema} = params ?? {}
|
|
92
100
|
setFromLanguage(from)
|
|
@@ -98,7 +106,7 @@ export function FieldTranslationProvider(props: PropsWithChildren<{}>) {
|
|
|
98
106
|
const preferred = getPreferredToFieldLanguages(from.id)
|
|
99
107
|
const allToLanguages = languages.filter((l) => l.id !== from?.id)
|
|
100
108
|
const filteredToLanguages = allToLanguages.filter(
|
|
101
|
-
(l) => !preferred.length || preferred.includes(l.id)
|
|
109
|
+
(l) => !preferred.length || preferred.includes(l.id),
|
|
102
110
|
)
|
|
103
111
|
|
|
104
112
|
setToLanguages(filteredToLanguages)
|
|
@@ -111,21 +119,21 @@ export function FieldTranslationProvider(props: PropsWithChildren<{}>) {
|
|
|
111
119
|
docMembers,
|
|
112
120
|
fromId,
|
|
113
121
|
allToIds.filter((toId) => fromId !== toId),
|
|
114
|
-
config?.translationOutputs ?? defaultLanguageOutputs
|
|
122
|
+
config?.translationOutputs ?? defaultLanguageOutputs,
|
|
115
123
|
)
|
|
116
124
|
setFieldLanguageMaps(transMap)
|
|
117
125
|
} else {
|
|
118
126
|
setFieldLanguageMaps(undefined)
|
|
119
127
|
}
|
|
120
128
|
},
|
|
121
|
-
[config]
|
|
129
|
+
[config],
|
|
122
130
|
)
|
|
123
131
|
|
|
124
132
|
const toggleToLanguage = useCallback(
|
|
125
133
|
(
|
|
126
134
|
toggledLang: Language,
|
|
127
135
|
toLanguages: Language[] | undefined,
|
|
128
|
-
languages: Language[] | undefined
|
|
136
|
+
languages: Language[] | undefined,
|
|
129
137
|
) => {
|
|
130
138
|
if (!languages || !fromLanguage) {
|
|
131
139
|
return
|
|
@@ -134,17 +142,17 @@ export function FieldTranslationProvider(props: PropsWithChildren<{}>) {
|
|
|
134
142
|
const newToLangs = languages.filter(
|
|
135
143
|
(anyLang) =>
|
|
136
144
|
!!toLanguages?.find(
|
|
137
|
-
(selectedLang) => toggledLang.id !== selectedLang.id && selectedLang.id === anyLang.id
|
|
145
|
+
(selectedLang) => toggledLang.id !== selectedLang.id && selectedLang.id === anyLang.id,
|
|
138
146
|
) ||
|
|
139
|
-
(toggledLang.id === anyLang.id && !wasSelected)
|
|
147
|
+
(toggledLang.id === anyLang.id && !wasSelected),
|
|
140
148
|
)
|
|
141
149
|
setToLanguages(newToLangs)
|
|
142
150
|
setPreferredToFieldLanguages(
|
|
143
151
|
fromLanguage.id,
|
|
144
|
-
newToLangs.map((l) => l.id)
|
|
152
|
+
newToLangs.map((l) => l.id),
|
|
145
153
|
)
|
|
146
154
|
},
|
|
147
|
-
[fromLanguage]
|
|
155
|
+
[fromLanguage],
|
|
148
156
|
)
|
|
149
157
|
|
|
150
158
|
const openFieldTranslation = useCallback(
|
|
@@ -163,7 +171,7 @@ export function FieldTranslationProvider(props: PropsWithChildren<{}>) {
|
|
|
163
171
|
console.error('No languages available for selected language params', languageParams)
|
|
164
172
|
}
|
|
165
173
|
},
|
|
166
|
-
[selectFromLanguage, config, languageClient]
|
|
174
|
+
[selectFromLanguage, config, languageClient],
|
|
167
175
|
)
|
|
168
176
|
|
|
169
177
|
const contextValue: FieldTranslationContextValue = useMemo(() => {
|
|
@@ -298,7 +306,7 @@ function ToLanguageCheckbox(props: {
|
|
|
298
306
|
toggleToLanguage: (
|
|
299
307
|
toggledLang: Language,
|
|
300
308
|
toLanguages: Language[] | undefined,
|
|
301
|
-
languages: Language[] | undefined
|
|
309
|
+
languages: Language[] | undefined,
|
|
302
310
|
) => void
|
|
303
311
|
languages: Language[]
|
|
304
312
|
}) {
|
|
@@ -306,7 +314,7 @@ function ToLanguageCheckbox(props: {
|
|
|
306
314
|
const langId = checkboxLanguage.id
|
|
307
315
|
const onChange = useCallback(
|
|
308
316
|
() => toggleToLanguage(checkboxLanguage, toLanguages, languages),
|
|
309
|
-
[toggleToLanguage, checkboxLanguage, toLanguages, languages]
|
|
317
|
+
[toggleToLanguage, checkboxLanguage, toLanguages, languages],
|
|
310
318
|
)
|
|
311
319
|
return (
|
|
312
320
|
<Flex
|
|
@@ -334,7 +342,7 @@ function FromLanguageRadio(props: {
|
|
|
334
342
|
selectFromLanguage: (
|
|
335
343
|
from: Language,
|
|
336
344
|
languages: Language[] | undefined,
|
|
337
|
-
params: FieldTranslationParams | undefined
|
|
345
|
+
params: FieldTranslationParams | undefined,
|
|
338
346
|
) => void
|
|
339
347
|
languages: Language[] | undefined
|
|
340
348
|
fieldTranslationParams: FieldTranslationParams | undefined
|
|
@@ -344,7 +352,7 @@ function FromLanguageRadio(props: {
|
|
|
344
352
|
|
|
345
353
|
const onChange = useCallback(
|
|
346
354
|
() => selectFromLanguage(radioLanguage, languages, fieldTranslationParams),
|
|
347
|
-
[selectFromLanguage, radioLanguage, languages, fieldTranslationParams]
|
|
355
|
+
[selectFromLanguage, radioLanguage, languages, fieldTranslationParams],
|
|
348
356
|
)
|
|
349
357
|
return (
|
|
350
358
|
<Flex key={langId} gap={3} align="center" as={'label'}>
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {SanityDocumentLike} from 'sanity'
|
|
2
1
|
import get from 'lodash/get'
|
|
2
|
+
import {SanityDocumentLike} from 'sanity'
|
|
3
3
|
|
|
4
4
|
export const getLanguageParams = (
|
|
5
5
|
select: Record<string, string> | undefined,
|
|
6
|
-
document: SanityDocumentLike | undefined
|
|
6
|
+
document: SanityDocumentLike | undefined,
|
|
7
7
|
): Record<string, unknown> => {
|
|
8
8
|
if (!select || !document) {
|
|
9
9
|
return {}
|
|
@@ -16,7 +16,7 @@ export const getLanguageParams = (
|
|
|
16
16
|
if (Array.isArray(value)) {
|
|
17
17
|
// If there are references in the array, ensure they have `_ref` set, otherwise they are considered empty and can safely be ignored
|
|
18
18
|
value = value.filter((item) =>
|
|
19
|
-
typeof item === 'object' ? item?._type !== 'reference' || '_ref' in item : true
|
|
19
|
+
typeof item === 'object' ? item?._type !== 'reference' || '_ref' in item : true,
|
|
20
20
|
)
|
|
21
21
|
}
|
|
22
22
|
selectedValue[key] = value
|
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import {describe, expect, test} from 'vitest'
|
|
2
1
|
import {Schema} from '@sanity/schema'
|
|
3
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
defineType,
|
|
4
|
+
type ObjectSchemaType,
|
|
5
|
+
pathToString,
|
|
6
|
+
type SanityDocumentLike,
|
|
7
|
+
typed,
|
|
8
|
+
} from 'sanity'
|
|
9
|
+
import {describe, expect, test} from 'vitest'
|
|
10
|
+
|
|
4
11
|
import {
|
|
5
12
|
defaultLanguageOutputs,
|
|
6
|
-
FieldLanguageMap,
|
|
13
|
+
type FieldLanguageMap,
|
|
7
14
|
getDocumentMembersFlat,
|
|
8
15
|
getFieldLanguageMap,
|
|
9
16
|
} from './paths'
|
|
@@ -82,7 +89,7 @@ describe('paths', () => {
|
|
|
82
89
|
inputPath: ['translations', {_key: 'en'}],
|
|
83
90
|
outputs: [{id: 'nb', outputPath: ['translations', {_key: 'nb'}]}],
|
|
84
91
|
},
|
|
85
|
-
])
|
|
92
|
+
]),
|
|
86
93
|
)
|
|
87
94
|
})
|
|
88
95
|
|