@sanity/assist 1.2.15-lang.1 → 1.2.15-lang.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1360,29 +1360,37 @@ function getAssistableDocId(documentSchemaType, documentId) {
1360
1360
  return documentSchemaType.liveEdit ? baseId : "drafts.".concat(baseId);
1361
1361
  }
1362
1362
  function useRequestRunInstruction(args) {
1363
- const {
1364
- documentOnChange,
1365
- isDocAssistable: isDocAssistable2
1366
- } = args;
1367
1363
  const {
1368
1364
  runInstruction,
1369
1365
  instructionLoading
1370
1366
  } = useRunInstruction();
1371
- const [queuedTask, setQueuedTask] = useState(void 0);
1372
- useEffect(() => {
1373
- if (queuedTask && isDocAssistable2) {
1374
- runInstruction(queuedTask);
1375
- setQueuedTask(void 0);
1376
- }
1377
- }, [queuedTask, isDocAssistable2, runInstruction]);
1367
+ const requestRunInstruction = useDraftDelayedTask({
1368
+ ...args,
1369
+ task: runInstruction
1370
+ });
1378
1371
  return {
1379
1372
  instructionLoading,
1380
- requestRunInstruction: useCallback(task => {
1381
- documentOnChange(PatchEvent.from([unset(["_force_document_creation"])]));
1382
- setQueuedTask(task);
1383
- }, [setQueuedTask, documentOnChange])
1373
+ requestRunInstruction
1384
1374
  };
1385
1375
  }
1376
+ function useDraftDelayedTask(args) {
1377
+ const {
1378
+ documentOnChange,
1379
+ isDocAssistable: isDocAssistable2,
1380
+ task
1381
+ } = args;
1382
+ const [queuedArgs, setQueuedArgs] = useState(void 0);
1383
+ useEffect(() => {
1384
+ if (queuedArgs && isDocAssistable2) {
1385
+ task(queuedArgs);
1386
+ setQueuedArgs(void 0);
1387
+ }
1388
+ }, [queuedArgs, isDocAssistable2, task]);
1389
+ return useCallback(taskArgs => {
1390
+ documentOnChange(PatchEvent.from([unset(["_force_document_creation"])]));
1391
+ setQueuedArgs(taskArgs);
1392
+ }, [setQueuedArgs, documentOnChange]);
1393
+ }
1386
1394
  const SparklesIllustration = styled(SparklesIcon)({
1387
1395
  fontSize: "3.125em",
1388
1396
  "& path": {
@@ -4540,6 +4548,7 @@ const getLanguageParams = (select, document) => {
4540
4548
  const FieldTranslationContext = createContext({
4541
4549
  openFieldTranslation: () => {},
4542
4550
  translationLoading: false
4551
+ //loadLanguages: () => {},
4543
4552
  });
4544
4553
  function useFieldTranslation() {
4545
4554
  return useContext(FieldTranslationContext);
@@ -4599,25 +4608,30 @@ function FieldTranslationProvider(props) {
4599
4608
  const newToLangs = languages2.filter(anyLang => !!(toLanguages2 == null ? void 0 : toLanguages2.find(selectedLang => toggledLang.id !== selectedLang.id && selectedLang.id === anyLang.id)) || toggledLang.id === anyLang.id && !wasSelected);
4600
4609
  setToLanguages(newToLangs);
4601
4610
  }, []);
4611
+ const openFieldTranslation = useCallback(async _ref11 => {
4612
+ let {
4613
+ document: document2,
4614
+ documentSchema: documentSchema2
4615
+ } = _ref11;
4616
+ setDialogOpen(true);
4617
+ const languageParams = getLanguageParams(config == null ? void 0 : config.selectLanguageParams, document2);
4618
+ const languages2 = await (typeof (config == null ? void 0 : config.languages) === "function" ? config == null ? void 0 : config.languages(languageClient, languageParams) : Promise.resolve(config == null ? void 0 : config.languages));
4619
+ setLanguages(languages2);
4620
+ setDocument(document2);
4621
+ setDocumentSchema(documentSchema2);
4622
+ const fromLanguage2 = languages2 == null ? void 0 : languages2[0];
4623
+ if (fromLanguage2) {
4624
+ selectFromLanguage(fromLanguage2, languages2, document2, documentSchema2);
4625
+ } else {
4626
+ console.error("No languages available for selected language params", languageParams);
4627
+ }
4628
+ }, [selectFromLanguage, config, languageClient]);
4602
4629
  const contextValue = useMemo(() => {
4603
4630
  return {
4604
- openFieldTranslation: async (document2, documentSchema2) => {
4605
- setDialogOpen(true);
4606
- const languageParams = getLanguageParams(config == null ? void 0 : config.selectLanguageParams, document2);
4607
- const languages2 = await (typeof (config == null ? void 0 : config.languages) === "function" ? config == null ? void 0 : config.languages(languageClient, languageParams) : Promise.resolve(config == null ? void 0 : config.languages));
4608
- setLanguages(languages2);
4609
- setDocument(document2);
4610
- setDocumentSchema(documentSchema2);
4611
- const fromLanguage2 = languages2 == null ? void 0 : languages2[0];
4612
- if (fromLanguage2) {
4613
- selectFromLanguage(fromLanguage2, languages2, document2, documentSchema2);
4614
- } else {
4615
- console.error("No languages available for selected language params", languageParams);
4616
- }
4617
- },
4631
+ openFieldTranslation,
4618
4632
  translationLoading: false
4619
4633
  };
4620
- }, [selectFromLanguage, config, languageClient]);
4634
+ }, [openFieldTranslation]);
4621
4635
  const runDisabled = !fromLanguage || !(toLanguages == null ? void 0 : toLanguages.length) || !(translationMap == null ? void 0 : translationMap.length) || !documentId;
4622
4636
  const onRunTranslation = useCallback(() => {
4623
4637
  if (translationMap && documentId) {
@@ -4911,11 +4925,11 @@ function InstructionInput(props) {
4911
4925
  })]
4912
4926
  });
4913
4927
  }
4914
- function ObjectMember(_ref11) {
4928
+ function ObjectMember(_ref12) {
4915
4929
  let {
4916
4930
  fieldName,
4917
4931
  ...props
4918
- } = _ref11;
4932
+ } = _ref12;
4919
4933
  const member = findFieldMember(props.members, fieldName);
4920
4934
  return member ? /* @__PURE__ */jsx(ObjectInputMember, {
4921
4935
  ...props,
@@ -5054,8 +5068,8 @@ function IconInput(props) {
5054
5068
  onChange
5055
5069
  } = props;
5056
5070
  const id = useId();
5057
- const items = useMemo(() => Object.entries(icons).map(_ref12 => {
5058
- let [key, icon] = _ref12;
5071
+ const items = useMemo(() => Object.entries(icons).map(_ref13 => {
5072
+ let [key, icon] = _ref13;
5059
5073
  return /* @__PURE__ */jsx(IconItem, {
5060
5074
  iconKey: key,
5061
5075
  icon,
@@ -5083,12 +5097,12 @@ function IconInput(props) {
5083
5097
  }
5084
5098
  });
5085
5099
  }
5086
- function IconItem(_ref13) {
5100
+ function IconItem(_ref14) {
5087
5101
  let {
5088
5102
  icon,
5089
5103
  iconKey: key,
5090
5104
  onChange
5091
- } = _ref13;
5105
+ } = _ref14;
5092
5106
  const onClick = useCallback(() => onChange(set(key)), [onChange, key]);
5093
5107
  return /* @__PURE__ */jsx(MenuItem, {
5094
5108
  icon,
@@ -5099,8 +5113,8 @@ function IconItem(_ref13) {
5099
5113
  }
5100
5114
  function getIcon(iconName) {
5101
5115
  var _a, _b;
5102
- return (_b = (_a = Object.entries(icons).find(_ref14 => {
5103
- let [key] = _ref14;
5116
+ return (_b = (_a = Object.entries(icons).find(_ref15 => {
5117
+ let [key] = _ref15;
5104
5118
  return key === iconName;
5105
5119
  })) == null ? void 0 : _a[1]) != null ? _b : icons.sparkles;
5106
5120
  }
@@ -5259,11 +5273,11 @@ const contextDocumentSchema = defineType({
5259
5273
  title: "title",
5260
5274
  context: "context"
5261
5275
  },
5262
- prepare(_ref15) {
5276
+ prepare(_ref16) {
5263
5277
  let {
5264
5278
  title,
5265
5279
  context
5266
- } = _ref15;
5280
+ } = _ref16;
5267
5281
  var _a;
5268
5282
  const text = context == null ? void 0 : context.flatMap(block => block == null ? void 0 : block.children).flatMap(child => {
5269
5283
  var _a2;
@@ -5363,11 +5377,11 @@ function InstructionOutputField(props) {
5363
5377
  children: props.children
5364
5378
  });
5365
5379
  }
5366
- function EnabledOutputField(_ref16) {
5380
+ function EnabledOutputField(_ref17) {
5367
5381
  let {
5368
5382
  fieldSchema,
5369
5383
  ...props
5370
- } = _ref16;
5384
+ } = _ref17;
5371
5385
  var _a;
5372
5386
  const [open, setOpen] = useState(!!((_a = props.value) == null ? void 0 : _a.length));
5373
5387
  const onExpand = useCallback(() => setOpen(true), []);
@@ -5415,11 +5429,11 @@ function useEmptySelectAllValue(value, allowedValues, onChange) {
5415
5429
  }
5416
5430
  }, [allowedValues, value, onChange]);
5417
5431
  }
5418
- function ObjectOutputInput(_ref17) {
5432
+ function ObjectOutputInput(_ref18) {
5419
5433
  let {
5420
5434
  fieldSchema,
5421
5435
  ...props
5422
- } = _ref17;
5436
+ } = _ref18;
5423
5437
  const {
5424
5438
  value,
5425
5439
  onChange
@@ -5466,11 +5480,11 @@ function ObjectOutputInput(_ref17) {
5466
5480
  })
5467
5481
  });
5468
5482
  }
5469
- function ArrayOutputInput(_ref18) {
5483
+ function ArrayOutputInput(_ref19) {
5470
5484
  let {
5471
5485
  fieldSchema,
5472
5486
  ...props
5473
- } = _ref18;
5487
+ } = _ref19;
5474
5488
  const {
5475
5489
  value,
5476
5490
  onChange
@@ -5515,13 +5529,13 @@ function ArrayOutputInput(_ref18) {
5515
5529
  })
5516
5530
  });
5517
5531
  }
5518
- function Selectable(_ref19) {
5532
+ function Selectable(_ref20) {
5519
5533
  let {
5520
5534
  title,
5521
5535
  arrayValue,
5522
5536
  value,
5523
5537
  onChange
5524
- } = _ref19;
5538
+ } = _ref20;
5525
5539
  const checked = !(arrayValue == null ? void 0 : arrayValue.length) || !!(arrayValue == null ? void 0 : arrayValue.find(v => v._key === value));
5526
5540
  const handleChange = useCallback(() => onChange(checked, value), [onChange, checked, value]);
5527
5541
  return /* @__PURE__ */jsxs(Flex, {
@@ -5586,10 +5600,10 @@ const fieldReference = defineType({
5586
5600
  select: {
5587
5601
  path: "path"
5588
5602
  },
5589
- prepare(_ref20) {
5603
+ prepare(_ref21) {
5590
5604
  let {
5591
5605
  path
5592
- } = _ref20;
5606
+ } = _ref21;
5593
5607
  return {
5594
5608
  title: path,
5595
5609
  path,
@@ -5734,12 +5748,12 @@ const instruction = defineType({
5734
5748
  title: "title",
5735
5749
  userId: "userId"
5736
5750
  },
5737
- prepare: _ref21 => {
5751
+ prepare: _ref22 => {
5738
5752
  let {
5739
5753
  icon,
5740
5754
  title,
5741
5755
  userId
5742
- } = _ref21;
5756
+ } = _ref22;
5743
5757
  return {
5744
5758
  title,
5745
5759
  icon: icon ? icons[icon] : SparklesIcon,
@@ -6196,27 +6210,32 @@ const translateActions = {
6196
6210
  config
6197
6211
  } = useAiAssistanceConfig();
6198
6212
  const apiClient = useApiClient(config == null ? void 0 : config.__customApiClient);
6199
- const {
6200
- translate,
6201
- loading
6202
- } = useTranslate(apiClient);
6203
6213
  const isDocumentLevel = props.path.length === 0;
6204
6214
  const {
6205
6215
  schemaType,
6206
- documentId
6216
+ documentId,
6217
+ documentIsAssistable
6207
6218
  } = props;
6208
- if (isDocumentLevel) {
6219
+ const fieldTransEnabled = (_c = (_b = (_a = config.translate) == null ? void 0 : _a.field) == null ? void 0 : _b.documentTypes) == null ? void 0 : _c.includes(schemaType.name);
6220
+ const docTransTypes = (_e = (_d = config.translate) == null ? void 0 : _d.document) == null ? void 0 : _e.documentTypes;
6221
+ const documentTranslationEnabled = isDocumentLevel && (!docTransTypes && isAssistSupported(schemaType) || (docTransTypes == null ? void 0 : docTransTypes.includes(schemaType.name)));
6222
+ if (documentTranslationEnabled || fieldTransEnabled) {
6209
6223
  const {
6210
- value: documentValue
6224
+ value: documentValue,
6225
+ onChange: documentOnChange
6211
6226
  } = useDocumentPane();
6212
6227
  const docRef = useRef(documentValue);
6228
+ const translationApi = useTranslate(apiClient);
6229
+ const translate = useDraftDelayedTask({
6230
+ documentOnChange,
6231
+ isDocAssistable: documentIsAssistable != null ? documentIsAssistable : false,
6232
+ task: translationApi.translate
6233
+ });
6213
6234
  docRef.current = documentValue;
6214
- const docTransTypes = (_b = (_a = config.translate) == null ? void 0 : _a.document) == null ? void 0 : _b.documentTypes;
6215
- const documentTranslation = !docTransTypes && isAssistSupported(schemaType) || (docTransTypes == null ? void 0 : docTransTypes.includes(schemaType.name));
6216
- const languagePath = (_d = (_c = config.translate) == null ? void 0 : _c.document) == null ? void 0 : _d.languageField;
6217
- const translateDocumentAction = useMemo(() => languagePath && documentTranslation ? node$1({
6235
+ const languagePath = (_g = (_f = config.translate) == null ? void 0 : _f.document) == null ? void 0 : _g.languageField;
6236
+ const translateDocumentAction = useMemo(() => languagePath && documentTranslationEnabled ? node$1({
6218
6237
  type: "action",
6219
- icon: loading ? () => /* @__PURE__ */jsx(Box, {
6238
+ icon: translationApi.loading ? () => /* @__PURE__ */jsx(Box, {
6220
6239
  style: {
6221
6240
  height: 17
6222
6241
  },
@@ -6228,7 +6247,7 @@ const translateActions = {
6228
6247
  }) : TranslateIcon,
6229
6248
  title: "Translate document",
6230
6249
  onAction: () => {
6231
- if (loading || !languagePath || !documentId) {
6250
+ if (translationApi.loading || !languagePath || !documentId) {
6232
6251
  return;
6233
6252
  }
6234
6253
  translate({
@@ -6237,13 +6256,17 @@ const translateActions = {
6237
6256
  });
6238
6257
  },
6239
6258
  renderAsButton: true,
6240
- disabled: loading
6241
- }) : void 0, [languagePath, translate, documentId, loading, documentTranslation]);
6259
+ disabled: translationApi.loading
6260
+ }) : void 0, [languagePath, translate, documentId, translationApi.loading, documentTranslationEnabled]);
6242
6261
  const fieldTranslate = useFieldTranslation();
6243
- const fieldTransEnabled = (_g = (_f = (_e = config.translate) == null ? void 0 : _e.field) == null ? void 0 : _f.documentTypes) == null ? void 0 : _g.includes(schemaType.name);
6262
+ const openFieldTranslation = useDraftDelayedTask({
6263
+ documentOnChange,
6264
+ isDocAssistable: documentIsAssistable != null ? documentIsAssistable : false,
6265
+ task: fieldTranslate.openFieldTranslation
6266
+ });
6244
6267
  const translateFieldsAction = useMemo(() => fieldTransEnabled ? node$1({
6245
6268
  type: "action",
6246
- icon: loading ? () => /* @__PURE__ */jsx(Box, {
6269
+ icon: fieldTranslate.translationLoading ? () => /* @__PURE__ */jsx(Box, {
6247
6270
  style: {
6248
6271
  height: 17
6249
6272
  },
@@ -6255,14 +6278,17 @@ const translateActions = {
6255
6278
  }) : TranslateIcon,
6256
6279
  title: "Translate fields",
6257
6280
  onAction: () => {
6258
- if (loading || !documentId) {
6281
+ if (fieldTranslate.translationLoading || !documentId) {
6259
6282
  return;
6260
6283
  }
6261
- fieldTranslate.openFieldTranslation(docRef.current, schemaType);
6284
+ openFieldTranslation({
6285
+ document: docRef.current,
6286
+ documentSchema: schemaType
6287
+ });
6262
6288
  },
6263
6289
  renderAsButton: true,
6264
- disabled: loading
6265
- }) : void 0, [fieldTranslate, schemaType, documentId, loading, fieldTransEnabled]);
6290
+ disabled: fieldTranslate.translationLoading
6291
+ }) : void 0, [openFieldTranslation, schemaType, documentId, fieldTranslate.translationLoading, fieldTransEnabled]);
6266
6292
  return useMemo(() => {
6267
6293
  return node$1({
6268
6294
  type: "group",
@@ -6332,10 +6358,11 @@ const assistFieldActions = {
6332
6358
  const isPathSelected = pathKey === selectedPath;
6333
6359
  const isSelected = isInspectorOpen && isPathSelected;
6334
6360
  const imageCaptionAction = generateCaptionsActions.useAction(props);
6335
- const translateAction = translateActions.useAction({
6361
+ const translateAction = translateActions.useAction(typed({
6336
6362
  ...props,
6337
- documentId: assistableDocumentId
6338
- });
6363
+ documentId: assistableDocumentId,
6364
+ documentIsAssistable
6365
+ }));
6339
6366
  const manageInstructions = useCallback(() => isSelected ? closeInspector(aiInspectorId) : openInspector(aiInspectorId, {
6340
6367
  [fieldPathParam]: pathKey,
6341
6368
  [instructionParam]: void 0
@@ -6518,11 +6545,11 @@ function AssistDocumentInputWrapper(props) {
6518
6545
  documentId
6519
6546
  });
6520
6547
  }
6521
- function AssistDocumentInput(_ref22) {
6548
+ function AssistDocumentInput(_ref23) {
6522
6549
  let {
6523
6550
  documentId,
6524
6551
  ...props
6525
- } = _ref22;
6552
+ } = _ref23;
6526
6553
  useInstructionToaster(documentId, props.schemaType);
6527
6554
  return /* @__PURE__ */jsx(FirstAssistedPathProvider, {
6528
6555
  members: props.members,
@@ -6611,23 +6638,23 @@ const assist = definePlugin(config => {
6611
6638
  }
6612
6639
  return prev;
6613
6640
  },
6614
- unstable_fieldActions: (prev, _ref23) => {
6641
+ unstable_fieldActions: (prev, _ref24) => {
6615
6642
  let {
6616
6643
  documentType,
6617
6644
  schema
6618
- } = _ref23;
6645
+ } = _ref24;
6619
6646
  const docSchema = schema.get(documentType);
6620
6647
  if (docSchema && isSchemaAssistEnabled(docSchema)) {
6621
6648
  return [...prev, assistFieldActions];
6622
6649
  }
6623
6650
  return prev;
6624
6651
  },
6625
- unstable_languageFilter: (prev, _ref24) => {
6652
+ unstable_languageFilter: (prev, _ref25) => {
6626
6653
  let {
6627
6654
  documentId,
6628
6655
  schema,
6629
6656
  schemaType
6630
- } = _ref24;
6657
+ } = _ref25;
6631
6658
  const docSchema = schema.get(schemaType);
6632
6659
  if (docSchema && isObjectSchemaType(docSchema) && isSchemaAssistEnabled(docSchema)) {
6633
6660
  return [...prev, createAssistDocumentPresence(documentId, docSchema)];