@stackbit/cms-core 0.7.6-develop.1 → 0.8.0-develop.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/content-store-utils.d.ts +44 -0
  3. package/dist/content-store-utils.d.ts.map +1 -1
  4. package/dist/content-store-utils.js +213 -1
  5. package/dist/content-store-utils.js.map +1 -1
  6. package/dist/content-store.d.ts +14 -1
  7. package/dist/content-store.d.ts.map +1 -1
  8. package/dist/content-store.js +70 -6
  9. package/dist/content-store.js.map +1 -1
  10. package/dist/index.d.ts +0 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +0 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/types/content-store-document-fields.d.ts +105 -0
  15. package/dist/types/content-store-document-fields.d.ts.map +1 -1
  16. package/dist/types/content-store-documents.d.ts +4 -1
  17. package/dist/types/content-store-documents.d.ts.map +1 -1
  18. package/dist/types/content-store-types.d.ts +10 -10
  19. package/dist/types/content-store-types.d.ts.map +1 -1
  20. package/dist/types/custom-actions.d.ts +108 -0
  21. package/dist/types/custom-actions.d.ts.map +1 -0
  22. package/dist/types/custom-actions.js +3 -0
  23. package/dist/types/custom-actions.js.map +1 -0
  24. package/dist/types/index.d.ts +1 -0
  25. package/dist/types/index.d.ts.map +1 -1
  26. package/dist/types/index.js +1 -0
  27. package/dist/types/index.js.map +1 -1
  28. package/dist/utils/csi-to-store-docs-converter.d.ts +2 -1
  29. package/dist/utils/csi-to-store-docs-converter.d.ts.map +1 -1
  30. package/dist/utils/csi-to-store-docs-converter.js +144 -11
  31. package/dist/utils/csi-to-store-docs-converter.js.map +1 -1
  32. package/dist/utils/custom-actions.d.ts +53 -0
  33. package/dist/utils/custom-actions.d.ts.map +1 -0
  34. package/dist/utils/custom-actions.js +564 -0
  35. package/dist/utils/custom-actions.js.map +1 -0
  36. package/dist/utils/document-hooks.d.ts +9 -0
  37. package/dist/utils/document-hooks.d.ts.map +1 -1
  38. package/dist/utils/document-hooks.js +3 -2
  39. package/dist/utils/document-hooks.js.map +1 -1
  40. package/dist/utils/search-utils.d.ts +1 -1
  41. package/dist/utils/search-utils.d.ts.map +1 -1
  42. package/dist/utils/site-map.d.ts +4 -4
  43. package/dist/utils/site-map.d.ts.map +1 -1
  44. package/dist/utils/site-map.js +2 -11
  45. package/dist/utils/site-map.js.map +1 -1
  46. package/dist/utils/store-to-api-docs-converter.d.ts.map +1 -1
  47. package/dist/utils/store-to-api-docs-converter.js +77 -29
  48. package/dist/utils/store-to-api-docs-converter.js.map +1 -1
  49. package/dist/utils/store-to-csi-docs-converter.d.ts +3 -0
  50. package/dist/utils/store-to-csi-docs-converter.d.ts.map +1 -1
  51. package/dist/utils/store-to-csi-docs-converter.js +2 -1
  52. package/dist/utils/store-to-csi-docs-converter.js.map +1 -1
  53. package/dist/utils/tree-views.d.ts +10 -0
  54. package/dist/utils/tree-views.d.ts.map +1 -0
  55. package/dist/utils/tree-views.js +64 -0
  56. package/dist/utils/tree-views.js.map +1 -0
  57. package/package.json +5 -5
  58. package/src/content-store-utils.ts +247 -0
  59. package/src/content-store.ts +90 -7
  60. package/src/index.ts +0 -1
  61. package/src/types/content-store-document-fields.ts +55 -2
  62. package/src/types/content-store-documents.ts +4 -1
  63. package/src/types/content-store-types.ts +10 -10
  64. package/src/types/custom-actions.ts +154 -0
  65. package/src/types/index.ts +1 -0
  66. package/src/utils/csi-to-store-docs-converter.ts +220 -12
  67. package/src/utils/custom-actions.ts +707 -0
  68. package/src/utils/document-hooks.ts +3 -3
  69. package/src/utils/search-utils.ts +1 -1
  70. package/src/utils/site-map.ts +4 -14
  71. package/src/utils/store-to-api-docs-converter.ts +88 -33
  72. package/src/utils/store-to-csi-docs-converter.ts +4 -4
  73. package/src/utils/tree-views.ts +72 -0
@@ -1,5 +1,5 @@
1
1
  import _ from 'lodash';
2
- import { Model, ObjectModel, ImageModel } from '@stackbit/sdk';
2
+ import { Model, ObjectModel, ImageModel, PageModel, DataModel } from '@stackbit/sdk';
3
3
  import { omitByNil } from '@stackbit/utils';
4
4
  import {
5
5
  Field,
@@ -17,6 +17,11 @@ import * as ContentStoreTypes from '../types';
17
17
  import { IMAGE_MODEL } from '../common/common-schema';
18
18
  import { BackCompatContentSourceInterface } from './backward-compatibility';
19
19
  import { getImageFieldsFromSourceData } from './asset-sources-utils';
20
+ import {
21
+ convertDocumentActionToAPICustomDocumentAction,
22
+ convertFieldActionToAPICustomAction,
23
+ convertObjectModelActionToAPICustomObjectAction
24
+ } from './custom-actions';
20
25
 
21
26
  export function mapCSIAssetsToStoreAssets({
22
27
  csiAssets,
@@ -100,7 +105,8 @@ export function mapCSIDocumentsToStoreDocuments({
100
105
  modelMap,
101
106
  defaultLocaleCode,
102
107
  assetSources,
103
- createConfigDelegate
108
+ createConfigDelegate,
109
+ customActionMap
104
110
  }: {
105
111
  csiDocuments: CSITypes.Document[];
106
112
  contentSourceInstance: BackCompatContentSourceInterface;
@@ -108,6 +114,7 @@ export function mapCSIDocumentsToStoreDocuments({
108
114
  defaultLocaleCode?: string;
109
115
  assetSources: CSITypes.AssetSource[];
110
116
  createConfigDelegate: () => CSITypes.ConfigDelegate;
117
+ customActionMap: ContentStoreTypes.ContentStoreCustomActionMap;
111
118
  }): ContentStoreTypes.Document[] {
112
119
  const meta = getMetadataFromContentStore({ contentSourceInstance });
113
120
  return csiDocuments.map((csiDocument) =>
@@ -118,7 +125,8 @@ export function mapCSIDocumentsToStoreDocuments({
118
125
  defaultLocaleCode,
119
126
  meta,
120
127
  assetSources,
121
- createConfigDelegate
128
+ createConfigDelegate,
129
+ customActionMap
122
130
  })
123
131
  );
124
132
  }
@@ -130,7 +138,8 @@ function mapCSIDocumentToStoreDocument({
130
138
  defaultLocaleCode,
131
139
  meta,
132
140
  assetSources,
133
- createConfigDelegate
141
+ createConfigDelegate,
142
+ customActionMap
134
143
  }: {
135
144
  csiDocument: CSITypes.Document;
136
145
  model: Model;
@@ -139,6 +148,7 @@ function mapCSIDocumentToStoreDocument({
139
148
  meta: { srcType: string; srcProjectId: string; srcProjectUrl: string; srcEnvironment: string };
140
149
  assetSources: CSITypes.AssetSource[];
141
150
  createConfigDelegate: () => CSITypes.ConfigDelegate;
151
+ customActionMap: ContentStoreTypes.ContentStoreCustomActionMap;
142
152
  }): ContentStoreTypes.Document {
143
153
  return omitByNil({
144
154
  type: 'document',
@@ -163,6 +173,14 @@ function mapCSIDocumentToStoreDocument({
163
173
  updatedAt: csiDocument.updatedAt,
164
174
  updatedBy: csiDocument.updatedBy,
165
175
  locale: getDocumentLocale({ csiDocument, model }),
176
+ getDocumentActions: () =>
177
+ getDocumentActions({
178
+ csiDocument,
179
+ model: model as PageModel | DataModel,
180
+ srcType: meta.srcType,
181
+ srcProjectId: meta.srcProjectId,
182
+ customActionMap
183
+ }),
166
184
  fields: mapCSIFieldsToStoreFields({
167
185
  csiDocumentFields: csiDocument.fields,
168
186
  modelFields: model.fields ?? [],
@@ -173,7 +191,9 @@ function mapCSIDocumentToStoreDocument({
173
191
  modelMap,
174
192
  defaultLocaleCode,
175
193
  assetSources,
176
- createConfigDelegate
194
+ createConfigDelegate,
195
+ customActionMap,
196
+ fieldPath: []
177
197
  }
178
198
  })
179
199
  });
@@ -187,6 +207,8 @@ type MapContext = {
187
207
  defaultLocaleCode?: string;
188
208
  assetSources: CSITypes.AssetSource[];
189
209
  createConfigDelegate: () => CSITypes.ConfigDelegate;
210
+ customActionMap: ContentStoreTypes.ContentStoreCustomActionMap;
211
+ fieldPath: (string | number)[];
190
212
  };
191
213
 
192
214
  function mapCSIFieldsToStoreFields({
@@ -200,13 +222,30 @@ function mapCSIFieldsToStoreFields({
200
222
  }): Record<string, ContentStoreTypes.DocumentField> {
201
223
  return modelFields.reduce((result: Record<string, ContentStoreTypes.DocumentField>, modelField) => {
202
224
  const csiDocumentField = csiDocumentFields[modelField.name];
225
+ const fieldPath = context.fieldPath.concat(modelField.name);
203
226
  const docField = mapCSIFieldToStoreField({
204
227
  csiDocumentField,
205
228
  modelField,
206
229
  localized: modelField.localized,
207
- context
230
+ context: {
231
+ ...context,
232
+ fieldPath: fieldPath
233
+ }
208
234
  });
209
235
  docField.label = modelField.label;
236
+ if (Array.isArray(modelField.actions)) {
237
+ docField.getFieldActions = ({ locale }: { locale?: string } = {}) => {
238
+ return getFieldActions({
239
+ modelField,
240
+ csiParentDocument: context.parentDocument,
241
+ srcType: context.srcType,
242
+ srcProjectId: context.srcProjectId,
243
+ customActionMap: context.customActionMap,
244
+ fieldPath: fieldPath,
245
+ locale
246
+ });
247
+ };
248
+ }
210
249
  result[modelField.name] = docField;
211
250
  return result;
212
251
  }, {});
@@ -483,10 +522,20 @@ function mapObjectField(
483
522
  locale
484
523
  }) as ContentStoreTypes.DocumentObjectFieldPreview;
485
524
  };
525
+ const _getObjectActions = (fieldPath: (string | number)[]) => () =>
526
+ getObjectFieldActions({
527
+ modelField: modelField,
528
+ csiParentDocument: context.parentDocument,
529
+ srcType: context.srcType,
530
+ srcProjectId: context.srcProjectId,
531
+ customActionMap: context.customActionMap,
532
+ fieldPath: fieldPath
533
+ });
486
534
  if (!isLocalizedField(csiDocumentField)) {
487
535
  return {
488
536
  type: csiDocumentField.type,
489
537
  getPreview: _getObjectPreview,
538
+ getObjectActions: _getObjectActions(context.fieldPath),
490
539
  fields: mapCSIFieldsToStoreFields({
491
540
  csiDocumentFields: csiDocumentField.fields ?? {},
492
541
  modelFields: modelField.fields ?? [],
@@ -498,13 +547,18 @@ function mapObjectField(
498
547
  type: csiDocumentField.type,
499
548
  localized: true,
500
549
  locales: _.mapValues(csiDocumentField.locales, (locale) => {
550
+ const fieldPath = context.fieldPath.concat(locale.locale);
501
551
  return {
502
552
  locale: locale.locale,
503
553
  getPreview: _getObjectPreview,
554
+ getObjectActions: _getObjectActions(fieldPath),
504
555
  fields: mapCSIFieldsToStoreFields({
505
556
  csiDocumentFields: locale.fields ?? {},
506
557
  modelFields: modelField.fields ?? [],
507
- context
558
+ context: {
559
+ ...context,
560
+ fieldPath: fieldPath
561
+ }
508
562
  })
509
563
  };
510
564
  })
@@ -525,6 +579,15 @@ function mapModelField(csiDocumentField: CSITypes.DocumentModelField, modelField
525
579
  locale
526
580
  }) as ContentStoreTypes.DocumentModelFieldPreview;
527
581
  };
582
+ const _getObjectActions = (model: ObjectModel, fieldPath: (string | number)[]) => () =>
583
+ getObjectModelActions({
584
+ csiParentDocument: context.parentDocument,
585
+ model: model,
586
+ srcType: context.srcType,
587
+ srcProjectId: context.srcProjectId,
588
+ customActionMap: context.customActionMap,
589
+ fieldPath: fieldPath
590
+ });
528
591
  if (!isLocalizedField(csiDocumentField)) {
529
592
  const model = context.modelMap[csiDocumentField.modelName]! as ObjectModel;
530
593
  return {
@@ -532,6 +595,7 @@ function mapModelField(csiDocumentField: CSITypes.DocumentModelField, modelField
532
595
  getPreview: _getObjectPreview(model),
533
596
  srcModelName: csiDocumentField.modelName,
534
597
  srcModelLabel: model.label ?? _.startCase(model.name),
598
+ getObjectActions: _getObjectActions(model, context.fieldPath),
535
599
  fields: mapCSIFieldsToStoreFields({
536
600
  csiDocumentFields: csiDocumentField.fields ?? {},
537
601
  modelFields: model.fields ?? [],
@@ -544,15 +608,20 @@ function mapModelField(csiDocumentField: CSITypes.DocumentModelField, modelField
544
608
  localized: true,
545
609
  locales: _.mapValues(csiDocumentField.locales, (locale) => {
546
610
  const model = context.modelMap[locale.modelName]! as ObjectModel;
611
+ const fieldPath = context.fieldPath.concat(locale.locale);
547
612
  return {
548
613
  locale: locale.locale,
549
614
  getPreview: _getObjectPreview(model),
550
615
  srcModelName: locale.modelName,
551
616
  srcModelLabel: model.label ?? _.startCase(model.name),
617
+ getObjectActions: _getObjectActions(model, fieldPath),
552
618
  fields: mapCSIFieldsToStoreFields({
553
619
  csiDocumentFields: locale.fields ?? {},
554
620
  modelFields: model.fields ?? [],
555
- context
621
+ context: {
622
+ ...context,
623
+ fieldPath
624
+ }
556
625
  })
557
626
  };
558
627
  })
@@ -564,13 +633,16 @@ function mapListField(csiDocumentField: CSITypes.DocumentListField, modelField:
564
633
  return {
565
634
  type: csiDocumentField.type,
566
635
  items: csiDocumentField.items.map(
567
- (item) =>
636
+ (item, index) =>
568
637
  mapCSIFieldToStoreField({
569
638
  csiDocumentField: item,
570
639
  modelField: modelField.items ?? { type: 'string' },
571
640
  // list items can not be localized, only the list itself can be localized
572
641
  localized: false,
573
- context
642
+ context: {
643
+ ...context,
644
+ fieldPath: context.fieldPath.concat(index)
645
+ }
574
646
  }) as ContentStoreTypes.DocumentListFieldItems
575
647
  )
576
648
  };
@@ -582,13 +654,16 @@ function mapListField(csiDocumentField: CSITypes.DocumentListField, modelField:
582
654
  return {
583
655
  locale: locale.locale,
584
656
  items: (locale.items ?? []).map(
585
- (item) =>
657
+ (item, index) =>
586
658
  mapCSIFieldToStoreField({
587
659
  csiDocumentField: item,
588
660
  modelField: modelField.items ?? { type: 'string' },
589
661
  // list items can not be localized, only the list itself can be localized
590
662
  localized: false,
591
- context
663
+ context: {
664
+ ...context,
665
+ fieldPath: context.fieldPath.concat([locale.locale, index])
666
+ }
592
667
  }) as ContentStoreTypes.DocumentListFieldItems
593
668
  )
594
669
  };
@@ -832,3 +907,136 @@ function getAssetLabelFromLabelField(csiAsset: CSITypes.Asset, imageModel: Image
832
907
  function getObjectTitleFromModel(model: Model | ImageModel): string {
833
908
  return model.label ? model.label : _.startCase(model.name);
834
909
  }
910
+
911
+ function getDocumentActions({
912
+ csiDocument,
913
+ model,
914
+ srcType,
915
+ srcProjectId,
916
+ customActionMap
917
+ }: {
918
+ csiDocument: CSITypes.Document;
919
+ model: PageModel | DataModel;
920
+ srcType: string;
921
+ srcProjectId: string;
922
+ customActionMap: ContentStoreTypes.ContentStoreCustomActionMap;
923
+ }): ContentStoreTypes.APICustomActionDocument[] | undefined {
924
+ if (!('actions' in model) || !Array.isArray(model.actions)) {
925
+ return undefined;
926
+ }
927
+ return model.actions.map((action) => {
928
+ return convertDocumentActionToAPICustomDocumentAction({
929
+ action,
930
+ customActionMap,
931
+ csiDocument: {
932
+ ...csiDocument,
933
+ srcType,
934
+ srcProjectId
935
+ }
936
+ });
937
+ });
938
+ }
939
+
940
+ function getObjectModelActions({
941
+ model,
942
+ csiParentDocument,
943
+ srcType,
944
+ srcProjectId,
945
+ customActionMap,
946
+ fieldPath
947
+ }: {
948
+ model: ObjectModel;
949
+ csiParentDocument: CSITypes.Document;
950
+ srcType: string;
951
+ srcProjectId: string;
952
+ customActionMap: ContentStoreTypes.ContentStoreCustomActionMap;
953
+ fieldPath: (string | number)[];
954
+ }): ContentStoreTypes.APICustomActionObject[] | undefined {
955
+ if (!('actions' in model) || !Array.isArray(model.actions)) {
956
+ return undefined;
957
+ }
958
+ return model.actions.map((action) => {
959
+ return convertObjectModelActionToAPICustomObjectAction({
960
+ action,
961
+ customActionMap,
962
+ fieldPath: fieldPath,
963
+ csiParentDocument: {
964
+ ...csiParentDocument,
965
+ srcType,
966
+ srcProjectId
967
+ }
968
+ });
969
+ });
970
+ }
971
+
972
+ function getObjectFieldActions({
973
+ modelField,
974
+ csiParentDocument,
975
+ srcType,
976
+ srcProjectId,
977
+ customActionMap,
978
+ fieldPath,
979
+ locale
980
+ }: {
981
+ modelField: FieldObjectProps;
982
+ csiParentDocument: CSITypes.Document;
983
+ srcType: string;
984
+ srcProjectId: string;
985
+ customActionMap: ContentStoreTypes.ContentStoreCustomActionMap;
986
+ fieldPath: (string | number)[];
987
+ locale?: string;
988
+ }): ContentStoreTypes.APICustomActionObject[] | undefined {
989
+ if (!('actions' in modelField) || !Array.isArray(modelField.actions)) {
990
+ return undefined;
991
+ }
992
+ return modelField.actions
993
+ .filter((action): action is CSITypes.CustomActionObjectField => action.type === 'object')
994
+ .map((action) => {
995
+ return convertFieldActionToAPICustomAction({
996
+ action,
997
+ customActionMap,
998
+ fieldPath: locale ? fieldPath.concat(locale) : fieldPath,
999
+ csiParentDocument: {
1000
+ ...csiParentDocument,
1001
+ srcType,
1002
+ srcProjectId
1003
+ }
1004
+ });
1005
+ });
1006
+ }
1007
+
1008
+ function getFieldActions({
1009
+ modelField,
1010
+ csiParentDocument,
1011
+ srcType,
1012
+ srcProjectId,
1013
+ customActionMap,
1014
+ fieldPath,
1015
+ locale
1016
+ }: {
1017
+ modelField: FieldSpecificProps;
1018
+ csiParentDocument: CSITypes.Document;
1019
+ srcType: string;
1020
+ srcProjectId: string;
1021
+ customActionMap: ContentStoreTypes.ContentStoreCustomActionMap;
1022
+ fieldPath: (string | number)[];
1023
+ locale?: string;
1024
+ }): ContentStoreTypes.APICustomActionField[] | undefined {
1025
+ if (!('actions' in modelField) || !Array.isArray(modelField.actions)) {
1026
+ return undefined;
1027
+ }
1028
+ return modelField.actions
1029
+ .filter((action): action is CSITypes.CustomActionField => action.type !== 'object')
1030
+ .map((action) => {
1031
+ return convertFieldActionToAPICustomAction({
1032
+ action,
1033
+ customActionMap,
1034
+ fieldPath: locale ? fieldPath.concat(locale) : fieldPath,
1035
+ csiParentDocument: {
1036
+ ...csiParentDocument,
1037
+ srcType,
1038
+ srcProjectId
1039
+ }
1040
+ });
1041
+ });
1042
+ }