@stackbit/cms-core 0.0.18-alpha.0 → 0.0.19-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/content-source-interface.d.ts +27 -19
- package/dist/content-source-interface.d.ts.map +1 -1
- package/dist/content-source-interface.js +1 -1
- package/dist/content-source-interface.js.map +1 -1
- package/dist/content-store.d.ts +4 -4
- package/dist/content-store.d.ts.map +1 -1
- package/dist/content-store.js +189 -142
- package/dist/content-store.js.map +1 -1
- package/package.json +2 -2
- package/src/content-source-interface.ts +38 -18
- package/src/content-store.ts +253 -180
package/dist/content-store.js
CHANGED
|
@@ -173,19 +173,23 @@ class ContentStore {
|
|
|
173
173
|
// TODO: load presets externally from config, and create additional map
|
|
174
174
|
// that maps presetIds by model name instead of storing that map inside every model
|
|
175
175
|
this.presets = config.presets;
|
|
176
|
-
const
|
|
177
|
-
const
|
|
178
|
-
const
|
|
179
|
-
|
|
176
|
+
const csiDocuments = await contentSourceInstance.getDocuments({ modelMap });
|
|
177
|
+
const csiAssets = await contentSourceInstance.getAssets();
|
|
178
|
+
const csiDocumentMap = lodash_1.default.keyBy(csiDocuments, 'id');
|
|
179
|
+
const csiAssetMap = lodash_1.default.keyBy(csiAssets, 'id');
|
|
180
|
+
const contentStoreDocuments = mapCSIDocumentsToStoreDocuments({
|
|
181
|
+
csiDocuments,
|
|
180
182
|
contentSourceInstance,
|
|
181
183
|
modelMap,
|
|
182
184
|
defaultLocaleCode
|
|
183
185
|
});
|
|
184
|
-
const contentStoreAssets =
|
|
185
|
-
|
|
186
|
+
const contentStoreAssets = mapCSIAssetsToStoreAssets({
|
|
187
|
+
csiAssets,
|
|
186
188
|
contentSourceInstance,
|
|
187
189
|
defaultLocaleCode
|
|
188
190
|
});
|
|
191
|
+
const documentMap = lodash_1.default.keyBy(contentStoreDocuments, 'srcObjectId');
|
|
192
|
+
const assetMap = lodash_1.default.keyBy(contentStoreAssets, 'srcObjectId');
|
|
189
193
|
this.logger.debug('loaded content source data', {
|
|
190
194
|
contentSourceId,
|
|
191
195
|
locales,
|
|
@@ -203,16 +207,26 @@ class ContentStore {
|
|
|
203
207
|
defaultLocaleCode: defaultLocaleCode,
|
|
204
208
|
models: models,
|
|
205
209
|
modelMap: modelMap,
|
|
210
|
+
csiDocuments: csiDocuments,
|
|
211
|
+
csiDocumentMap: csiDocumentMap,
|
|
206
212
|
documents: contentStoreDocuments,
|
|
207
|
-
documentMap:
|
|
213
|
+
documentMap: documentMap,
|
|
214
|
+
csiAssets: csiAssets,
|
|
215
|
+
csiAssetMap: csiAssetMap,
|
|
208
216
|
assets: contentStoreAssets,
|
|
209
|
-
assetMap:
|
|
217
|
+
assetMap: assetMap
|
|
210
218
|
};
|
|
211
219
|
contentSourceInstance.startWatchingContentUpdates({
|
|
212
220
|
getModelMap: () => {
|
|
213
221
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
214
222
|
return contentSourceData.modelMap;
|
|
215
223
|
},
|
|
224
|
+
getDocument({ documentId }) {
|
|
225
|
+
return csiDocumentMap[documentId];
|
|
226
|
+
},
|
|
227
|
+
getAsset({ assetId }) {
|
|
228
|
+
return csiAssetMap[assetId];
|
|
229
|
+
},
|
|
216
230
|
onContentChange: (contentChangeEvent) => {
|
|
217
231
|
this.logger.debug('content source called onContentChange', { contentSourceId });
|
|
218
232
|
const result = this.onContentChange(contentSourceId, contentChangeEvent);
|
|
@@ -244,11 +258,17 @@ class ContentStore {
|
|
|
244
258
|
deletedAssets: []
|
|
245
259
|
};
|
|
246
260
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
261
|
+
// update contentSourceData with deleted documents
|
|
247
262
|
contentChangeEvent.deletedDocumentIds.forEach((docId) => {
|
|
263
|
+
// delete document from documents map
|
|
248
264
|
delete contentSourceData.documentMap[docId];
|
|
265
|
+
delete contentSourceData.csiDocumentMap[docId];
|
|
266
|
+
// delete document from document array
|
|
249
267
|
const index = contentSourceData.documents.findIndex((document) => document.srcObjectId === docId);
|
|
250
268
|
if (index !== -1) {
|
|
269
|
+
// the indexes of documents and csiDocuments are always the same as they are always updated at the same time
|
|
251
270
|
contentSourceData.documents.splice(index, 1);
|
|
271
|
+
contentSourceData.csiDocuments.splice(index, 1);
|
|
252
272
|
}
|
|
253
273
|
result.deletedDocuments.push({
|
|
254
274
|
srcType: contentSourceData.type,
|
|
@@ -256,11 +276,17 @@ class ContentStore {
|
|
|
256
276
|
srcObjectId: docId
|
|
257
277
|
});
|
|
258
278
|
});
|
|
279
|
+
// update contentSourceData with deleted assets
|
|
259
280
|
contentChangeEvent.deletedAssetIds.forEach((assetId) => {
|
|
281
|
+
// delete document from asset map
|
|
260
282
|
delete contentSourceData.assetMap[assetId];
|
|
283
|
+
delete contentSourceData.csiAssetMap[assetId];
|
|
284
|
+
// delete document from asset array
|
|
261
285
|
const index = contentSourceData.assets.findIndex((asset) => asset.srcObjectId === assetId);
|
|
262
286
|
if (index !== -1) {
|
|
287
|
+
// the indexes of assets and csiAssets are always the same as they are always updated at the same time
|
|
263
288
|
contentSourceData.assets.splice(index, 1);
|
|
289
|
+
contentSourceData.csiAssets.splice(index, 1);
|
|
264
290
|
}
|
|
265
291
|
result.deletedAssets.push({
|
|
266
292
|
srcType: contentSourceData.type,
|
|
@@ -268,26 +294,36 @@ class ContentStore {
|
|
|
268
294
|
srcObjectId: assetId
|
|
269
295
|
});
|
|
270
296
|
});
|
|
271
|
-
|
|
272
|
-
|
|
297
|
+
// map csi documents and assets to content store documents and assets
|
|
298
|
+
const documents = mapCSIDocumentsToStoreDocuments({
|
|
299
|
+
csiDocuments: contentChangeEvent.documents,
|
|
273
300
|
contentSourceInstance: contentSourceData.instance,
|
|
274
301
|
modelMap: contentSourceData.modelMap,
|
|
275
302
|
defaultLocaleCode: contentSourceData.defaultLocaleCode
|
|
276
303
|
});
|
|
277
|
-
const assets =
|
|
278
|
-
|
|
304
|
+
const assets = mapCSIAssetsToStoreAssets({
|
|
305
|
+
csiAssets: contentChangeEvent.assets,
|
|
279
306
|
contentSourceInstance: contentSourceData.instance,
|
|
280
307
|
defaultLocaleCode: contentSourceData.defaultLocaleCode
|
|
281
308
|
});
|
|
309
|
+
// update contentSourceData with new or updated documents and assets
|
|
310
|
+
Object.assign(contentSourceData.csiDocumentMap, lodash_1.default.keyBy(contentChangeEvent.documents, 'id'));
|
|
311
|
+
Object.assign(contentSourceData.csiAssets, lodash_1.default.keyBy(contentChangeEvent.assets, 'id'));
|
|
282
312
|
Object.assign(contentSourceData.documentMap, lodash_1.default.keyBy(documents, 'srcObjectId'));
|
|
283
313
|
Object.assign(contentSourceData.assetMap, lodash_1.default.keyBy(assets, 'srcObjectId'));
|
|
284
|
-
for (
|
|
285
|
-
|
|
286
|
-
|
|
314
|
+
for (let idx = 0; idx < documents.length; idx++) {
|
|
315
|
+
// the indexes of mapped documents and documents from changeEvent are the same
|
|
316
|
+
const document = documents[idx];
|
|
317
|
+
const csiDocument = contentChangeEvent.documents[idx];
|
|
318
|
+
const dataIndex = contentSourceData.documents.findIndex((existingDoc) => existingDoc.srcObjectId === document.srcObjectId);
|
|
319
|
+
if (dataIndex === -1) {
|
|
287
320
|
contentSourceData.documents.push(document);
|
|
321
|
+
contentSourceData.csiDocuments.push(csiDocument);
|
|
288
322
|
}
|
|
289
323
|
else {
|
|
290
|
-
|
|
324
|
+
// the indexes of documents and csiDocuments are always the same as they are always updated at the same time
|
|
325
|
+
contentSourceData.documents.splice(dataIndex, 1, document);
|
|
326
|
+
contentSourceData.csiDocuments.splice(dataIndex, 1, csiDocument);
|
|
291
327
|
}
|
|
292
328
|
result.updatedDocuments.push({
|
|
293
329
|
srcType: contentSourceData.type,
|
|
@@ -295,13 +331,19 @@ class ContentStore {
|
|
|
295
331
|
srcObjectId: document.srcObjectId
|
|
296
332
|
});
|
|
297
333
|
}
|
|
298
|
-
for (
|
|
334
|
+
for (let idx = 0; idx < assets.length; idx++) {
|
|
335
|
+
// the indexes of mapped assets and assets from changeEvent are the same
|
|
336
|
+
const asset = assets[idx];
|
|
337
|
+
const csiAsset = contentChangeEvent.assets[idx];
|
|
299
338
|
const index = contentSourceData.assets.findIndex((existingAsset) => existingAsset.srcObjectId === asset.srcObjectId);
|
|
300
339
|
if (index === -1) {
|
|
301
340
|
contentSourceData.assets.push(asset);
|
|
341
|
+
contentSourceData.csiAssets.push(csiAsset);
|
|
302
342
|
}
|
|
303
343
|
else {
|
|
344
|
+
// the indexes of assets and csiAssets are always the same as they are always updated at the same time
|
|
304
345
|
contentSourceData.assets.splice(index, 1, asset);
|
|
346
|
+
contentSourceData.csiAssets.splice(index, 1, csiAsset);
|
|
305
347
|
}
|
|
306
348
|
result.updatedAssets.push({
|
|
307
349
|
srcType: contentSourceData.type,
|
|
@@ -441,7 +483,8 @@ class ContentStore {
|
|
|
441
483
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
442
484
|
// get the document that is being updated
|
|
443
485
|
const document = contentSourceData.documentMap[srcDocumentId];
|
|
444
|
-
|
|
486
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
487
|
+
if (!document || !csiDocument) {
|
|
445
488
|
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
446
489
|
}
|
|
447
490
|
// get the document model
|
|
@@ -485,7 +528,7 @@ class ContentStore {
|
|
|
485
528
|
refId: result.srcDocumentId
|
|
486
529
|
};
|
|
487
530
|
const updatedDocument = await contentSourceData.instance.updateDocument({
|
|
488
|
-
|
|
531
|
+
document: csiDocument,
|
|
489
532
|
modelMap: modelMap,
|
|
490
533
|
userContext: userContext,
|
|
491
534
|
operations: [
|
|
@@ -515,7 +558,8 @@ class ContentStore {
|
|
|
515
558
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
516
559
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
517
560
|
const document = contentSourceData.documentMap[srcDocumentId];
|
|
518
|
-
|
|
561
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
562
|
+
if (!document || !csiDocument) {
|
|
519
563
|
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
520
564
|
}
|
|
521
565
|
// get the document model
|
|
@@ -551,7 +595,7 @@ class ContentStore {
|
|
|
551
595
|
refId: result.id
|
|
552
596
|
};
|
|
553
597
|
const updatedDocument = await contentSourceData.instance.updateDocument({
|
|
554
|
-
|
|
598
|
+
document: csiDocument,
|
|
555
599
|
modelMap: modelMap,
|
|
556
600
|
userContext: userContext,
|
|
557
601
|
operations: [
|
|
@@ -606,19 +650,20 @@ class ContentStore {
|
|
|
606
650
|
this.logger.debug('updateDocument');
|
|
607
651
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
608
652
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
609
|
-
const modelMap = contentSourceData.modelMap;
|
|
610
653
|
const userContext = getUserContextForSrcType(srcType, user);
|
|
654
|
+
const document = contentSourceData.documentMap[srcDocumentId];
|
|
655
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
656
|
+
if (!document || !csiDocument) {
|
|
657
|
+
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
658
|
+
}
|
|
659
|
+
const modelMap = contentSourceData.modelMap;
|
|
660
|
+
const documentModelName = document.srcModelName;
|
|
661
|
+
const model = modelMap[documentModelName];
|
|
662
|
+
if (!model) {
|
|
663
|
+
throw new Error(`error updating document, could not find document model: '${documentModelName}'`);
|
|
664
|
+
}
|
|
611
665
|
const operations = await (0, utils_1.mapPromise)(updateOperations, async (updateOperation) => {
|
|
612
666
|
var _a;
|
|
613
|
-
const document = contentSourceData.documentMap[srcDocumentId];
|
|
614
|
-
if (!document) {
|
|
615
|
-
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
616
|
-
}
|
|
617
|
-
const documentModelName = document.srcModelName;
|
|
618
|
-
const model = modelMap[documentModelName];
|
|
619
|
-
if (!model) {
|
|
620
|
-
throw new Error(`error updating document, could not find document model: '${documentModelName}'`);
|
|
621
|
-
}
|
|
622
667
|
const locale = (_a = updateOperation.locale) !== null && _a !== void 0 ? _a : contentSourceData.defaultLocaleCode;
|
|
623
668
|
const modelField = getModelFieldForFieldAtPath(document, model, updateOperation.fieldPath, modelMap, locale);
|
|
624
669
|
switch (updateOperation.opType) {
|
|
@@ -660,8 +705,8 @@ class ContentStore {
|
|
|
660
705
|
return { ...updateOperation, modelField };
|
|
661
706
|
}
|
|
662
707
|
});
|
|
663
|
-
const
|
|
664
|
-
|
|
708
|
+
const updatedDocumentResult = await contentSourceData.instance.updateDocument({
|
|
709
|
+
document: csiDocument,
|
|
665
710
|
modelMap,
|
|
666
711
|
userContext,
|
|
667
712
|
operations
|
|
@@ -670,7 +715,7 @@ class ContentStore {
|
|
|
670
715
|
// instead wait for contentSource to call onContentChange(contentChangeEvent)
|
|
671
716
|
// and use data from contentChangeEvent to update the cache
|
|
672
717
|
// contentSourceData.documentMap = Object.assign(contentSourceData.documentMap, { [document.srcObjectId]: document });
|
|
673
|
-
return { srcDocumentId:
|
|
718
|
+
return { srcDocumentId: updatedDocumentResult.id };
|
|
674
719
|
}
|
|
675
720
|
async duplicateDocument({ srcType, srcProjectId, srcDocumentId, object, user }) {
|
|
676
721
|
this.logger.debug('duplicateDocument');
|
|
@@ -726,8 +771,8 @@ class ContentStore {
|
|
|
726
771
|
});
|
|
727
772
|
sourceAssets.push(sourceAsset);
|
|
728
773
|
}
|
|
729
|
-
const storeAssets =
|
|
730
|
-
|
|
774
|
+
const storeAssets = mapCSIAssetsToStoreAssets({
|
|
775
|
+
csiAssets: sourceAssets,
|
|
731
776
|
contentSourceInstance: contentSourceData.instance,
|
|
732
777
|
defaultLocaleCode: contentSourceData.defaultLocaleCode
|
|
733
778
|
});
|
|
@@ -738,7 +783,11 @@ class ContentStore {
|
|
|
738
783
|
const userContext = getUserContextForSrcType(srcType, user);
|
|
739
784
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
740
785
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
741
|
-
|
|
786
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
787
|
+
if (!csiDocument) {
|
|
788
|
+
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
789
|
+
}
|
|
790
|
+
await contentSourceData.instance.deleteDocument({ document: csiDocument, userContext });
|
|
742
791
|
// do not update cache in contentSourceData.documents and documentMap,
|
|
743
792
|
// instead wait for contentSource to call onContentChange(contentChangeEvent)
|
|
744
793
|
// and use data from contentChangeEvent to update the cache
|
|
@@ -748,21 +797,12 @@ class ContentStore {
|
|
|
748
797
|
this.logger.debug('validateDocuments');
|
|
749
798
|
const objectsBySourceId = lodash_1.default.groupBy(objects, (object) => getContentSourceId(object.srcType, object.srcProjectId));
|
|
750
799
|
let errors = [];
|
|
751
|
-
for (const [contentSourceId,
|
|
800
|
+
for (const [contentSourceId, contentSourceObjects] of Object.entries(objectsBySourceId)) {
|
|
752
801
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
753
802
|
locale = locale !== null && locale !== void 0 ? locale : contentSourceData.defaultLocaleCode;
|
|
754
|
-
const
|
|
755
|
-
const assetIds = [];
|
|
756
|
-
for (const object of objects) {
|
|
757
|
-
if (object.srcObjectId in contentSourceData.documentMap) {
|
|
758
|
-
documentIds.push(object.srcObjectId);
|
|
759
|
-
}
|
|
760
|
-
else if (object.srcObjectId in contentSourceData.assetMap) {
|
|
761
|
-
assetIds.push(object.srcObjectId);
|
|
762
|
-
}
|
|
763
|
-
}
|
|
803
|
+
const { documents, assets } = getCSIDocumentsAndAssetsFromContentSourceDataByIds(contentSourceData, contentSourceObjects);
|
|
764
804
|
const userContext = getUserContextForSrcType(contentSourceData.type, user);
|
|
765
|
-
const validationResult = await contentSourceData.instance.validateDocuments({
|
|
805
|
+
const validationResult = await contentSourceData.instance.validateDocuments({ documents, assets, locale, userContext });
|
|
766
806
|
errors = errors.concat(validationResult.errors.map((validationError) => ({
|
|
767
807
|
message: validationError.message,
|
|
768
808
|
srcType: contentSourceData.type,
|
|
@@ -792,20 +832,11 @@ class ContentStore {
|
|
|
792
832
|
async publishDocuments({ objects, user }) {
|
|
793
833
|
this.logger.debug('publishDocuments');
|
|
794
834
|
const objectsBySourceId = lodash_1.default.groupBy(objects, (object) => getContentSourceId(object.srcType, object.srcProjectId));
|
|
795
|
-
for (const [contentSourceId,
|
|
835
|
+
for (const [contentSourceId, contentSourceObjects] of Object.entries(objectsBySourceId)) {
|
|
796
836
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
797
837
|
const userContext = getUserContextForSrcType(contentSourceData.type, user);
|
|
798
|
-
const
|
|
799
|
-
|
|
800
|
-
for (const object of objects) {
|
|
801
|
-
if (object.srcObjectId in contentSourceData.documentMap) {
|
|
802
|
-
documentIds.push(object.srcObjectId);
|
|
803
|
-
}
|
|
804
|
-
else if (object.srcObjectId in contentSourceData.assetMap) {
|
|
805
|
-
assetIds.push(object.srcObjectId);
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
await contentSourceData.instance.publishDocuments({ documentIds, assetIds, userContext });
|
|
838
|
+
const { documents, assets } = getCSIDocumentsAndAssetsFromContentSourceDataByIds(contentSourceData, contentSourceObjects);
|
|
839
|
+
await contentSourceData.instance.publishDocuments({ documents, assets, userContext });
|
|
809
840
|
}
|
|
810
841
|
}
|
|
811
842
|
getContentSourceDataByIdOrThrow(contentSourceId) {
|
|
@@ -825,80 +856,80 @@ function getUserContextForSrcType(srcType, user) {
|
|
|
825
856
|
var _a;
|
|
826
857
|
return (_a = user === null || user === void 0 ? void 0 : user.connections) === null || _a === void 0 ? void 0 : _a.find((connection) => connection.type === srcType);
|
|
827
858
|
}
|
|
828
|
-
function
|
|
859
|
+
function mapCSIAssetsToStoreAssets({ csiAssets, contentSourceInstance, defaultLocaleCode }) {
|
|
829
860
|
const extra = {
|
|
830
861
|
srcType: contentSourceInstance.getContentSourceType(),
|
|
831
862
|
srcProjectId: contentSourceInstance.getProjectId(),
|
|
832
863
|
srcProjectUrl: contentSourceInstance.getProjectManageUrl(),
|
|
833
864
|
srcEnvironment: contentSourceInstance.getProjectEnvironment()
|
|
834
865
|
};
|
|
835
|
-
return
|
|
866
|
+
return csiAssets.map((csiAsset) => sourceAssetToStoreAsset({ csiAsset, defaultLocaleCode, extra }));
|
|
836
867
|
}
|
|
837
|
-
function sourceAssetToStoreAsset({
|
|
868
|
+
function sourceAssetToStoreAsset({ csiAsset, defaultLocaleCode, extra }) {
|
|
838
869
|
return {
|
|
839
870
|
type: 'asset',
|
|
840
871
|
...extra,
|
|
841
|
-
srcObjectId:
|
|
842
|
-
srcObjectUrl:
|
|
843
|
-
srcObjectLabel: getObjectLabel(
|
|
872
|
+
srcObjectId: csiAsset.id,
|
|
873
|
+
srcObjectUrl: csiAsset.manageUrl,
|
|
874
|
+
srcObjectLabel: getObjectLabel(csiAsset.fields, common_schema_1.IMAGE_MODEL, defaultLocaleCode),
|
|
844
875
|
srcModelName: common_schema_1.IMAGE_MODEL.name,
|
|
845
876
|
srcModelLabel: common_schema_1.IMAGE_MODEL.label,
|
|
846
|
-
isChanged:
|
|
847
|
-
status:
|
|
848
|
-
createdAt:
|
|
849
|
-
createdBy:
|
|
850
|
-
updatedAt:
|
|
851
|
-
updatedBy:
|
|
877
|
+
isChanged: csiAsset.status === 'added' || csiAsset.status === 'modified',
|
|
878
|
+
status: csiAsset.status,
|
|
879
|
+
createdAt: csiAsset.createdAt,
|
|
880
|
+
createdBy: csiAsset.createdBy,
|
|
881
|
+
updatedAt: csiAsset.updatedAt,
|
|
882
|
+
updatedBy: csiAsset.updatedBy,
|
|
852
883
|
fields: {
|
|
853
884
|
title: {
|
|
854
885
|
label: 'Title',
|
|
855
|
-
...
|
|
886
|
+
...csiAsset.fields.title
|
|
856
887
|
},
|
|
857
888
|
file: {
|
|
858
889
|
label: 'File',
|
|
859
|
-
...
|
|
890
|
+
...csiAsset.fields.file
|
|
860
891
|
}
|
|
861
892
|
}
|
|
862
893
|
};
|
|
863
894
|
}
|
|
864
|
-
function
|
|
895
|
+
function mapCSIDocumentsToStoreDocuments({ csiDocuments, contentSourceInstance, modelMap, defaultLocaleCode }) {
|
|
865
896
|
const extra = {
|
|
866
897
|
srcType: contentSourceInstance.getContentSourceType(),
|
|
867
898
|
srcProjectId: contentSourceInstance.getProjectId(),
|
|
868
899
|
srcProjectUrl: contentSourceInstance.getProjectManageUrl(),
|
|
869
900
|
srcEnvironment: contentSourceInstance.getProjectEnvironment()
|
|
870
901
|
};
|
|
871
|
-
return
|
|
902
|
+
return csiDocuments.map((csiDocument) => mapCSIDocumentToStoreDocument({ csiDocument, model: modelMap[csiDocument.modelName], modelMap, defaultLocaleCode, extra }));
|
|
872
903
|
}
|
|
873
|
-
function
|
|
904
|
+
function mapCSIDocumentToStoreDocument({ csiDocument, model, modelMap, defaultLocaleCode, extra }) {
|
|
874
905
|
var _a, _b;
|
|
875
906
|
return {
|
|
876
907
|
type: 'document',
|
|
877
908
|
...extra,
|
|
878
|
-
srcObjectId:
|
|
879
|
-
srcObjectUrl:
|
|
880
|
-
srcObjectLabel: getObjectLabel(
|
|
881
|
-
srcModelLabel: (_a = model.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(
|
|
882
|
-
srcModelName:
|
|
883
|
-
isChanged:
|
|
884
|
-
status:
|
|
885
|
-
createdAt:
|
|
886
|
-
createdBy:
|
|
887
|
-
updatedAt:
|
|
888
|
-
updatedBy:
|
|
889
|
-
fields:
|
|
890
|
-
|
|
909
|
+
srcObjectId: csiDocument.id,
|
|
910
|
+
srcObjectUrl: csiDocument.manageUrl,
|
|
911
|
+
srcObjectLabel: getObjectLabel(csiDocument.fields, model, defaultLocaleCode),
|
|
912
|
+
srcModelLabel: (_a = model.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(csiDocument.modelName),
|
|
913
|
+
srcModelName: csiDocument.modelName,
|
|
914
|
+
isChanged: csiDocument.status === 'added' || csiDocument.status === 'modified',
|
|
915
|
+
status: csiDocument.status,
|
|
916
|
+
createdAt: csiDocument.createdAt,
|
|
917
|
+
createdBy: csiDocument.createdBy,
|
|
918
|
+
updatedAt: csiDocument.updatedAt,
|
|
919
|
+
updatedBy: csiDocument.updatedBy,
|
|
920
|
+
fields: mapCSIFieldsToStoreFields({
|
|
921
|
+
csiDocumentFields: csiDocument.fields,
|
|
891
922
|
modelFields: (_b = model.fields) !== null && _b !== void 0 ? _b : [],
|
|
892
923
|
modelMap,
|
|
893
924
|
defaultLocaleCode
|
|
894
925
|
})
|
|
895
926
|
};
|
|
896
927
|
}
|
|
897
|
-
function
|
|
928
|
+
function mapCSIFieldsToStoreFields({ csiDocumentFields, modelFields, modelMap, defaultLocaleCode }) {
|
|
898
929
|
return modelFields.reduce((result, modelField) => {
|
|
899
|
-
const
|
|
930
|
+
const csiDocumentField = csiDocumentFields[modelField.name];
|
|
900
931
|
const docField = mapSourceFieldToStoreField({
|
|
901
|
-
|
|
932
|
+
csiDocumentField,
|
|
902
933
|
modelField,
|
|
903
934
|
modelMap,
|
|
904
935
|
defaultLocaleCode
|
|
@@ -908,8 +939,8 @@ function mapSourceFieldsToStoreFields({ documentFields, modelFields, modelMap, d
|
|
|
908
939
|
return result;
|
|
909
940
|
}, {});
|
|
910
941
|
}
|
|
911
|
-
function mapSourceFieldToStoreField({
|
|
912
|
-
if (!
|
|
942
|
+
function mapSourceFieldToStoreField({ csiDocumentField, modelField, modelMap, defaultLocaleCode }) {
|
|
943
|
+
if (!csiDocumentField) {
|
|
913
944
|
const isUnset = ['object', 'model', 'reference', 'richText', 'markdown', 'image', 'file', 'json'].includes(modelField.type);
|
|
914
945
|
return {
|
|
915
946
|
type: modelField.type,
|
|
@@ -920,27 +951,27 @@ function mapSourceFieldToStoreField({ documentField, modelField, modelMap, defau
|
|
|
920
951
|
// TODO: check if need to add "options" to "enum" and subtype/min/max to "number"
|
|
921
952
|
switch (modelField.type) {
|
|
922
953
|
case 'object':
|
|
923
|
-
return mapObjectField(
|
|
954
|
+
return mapObjectField(csiDocumentField, modelField, modelMap, defaultLocaleCode);
|
|
924
955
|
case 'model':
|
|
925
|
-
return mapModelField(
|
|
956
|
+
return mapModelField(csiDocumentField, modelField, modelMap, defaultLocaleCode);
|
|
926
957
|
case 'list':
|
|
927
|
-
return mapListField(
|
|
958
|
+
return mapListField(csiDocumentField, modelField, modelMap, defaultLocaleCode);
|
|
928
959
|
case 'richText':
|
|
929
|
-
return mapRichTextField(
|
|
960
|
+
return mapRichTextField(csiDocumentField);
|
|
930
961
|
case 'markdown':
|
|
931
|
-
return mapMarkdownField(
|
|
962
|
+
return mapMarkdownField(csiDocumentField);
|
|
932
963
|
default:
|
|
933
|
-
return
|
|
964
|
+
return csiDocumentField;
|
|
934
965
|
}
|
|
935
966
|
}
|
|
936
|
-
function mapObjectField(
|
|
967
|
+
function mapObjectField(csiDocumentField, modelField, modelMap, defaultLocaleCode) {
|
|
937
968
|
var _a, _b, _c;
|
|
938
|
-
if (!(0, content_source_interface_1.isLocalizedField)(
|
|
969
|
+
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
939
970
|
return {
|
|
940
|
-
type:
|
|
941
|
-
srcObjectLabel: getObjectLabel((_a =
|
|
942
|
-
fields:
|
|
943
|
-
|
|
971
|
+
type: csiDocumentField.type,
|
|
972
|
+
srcObjectLabel: getObjectLabel((_a = csiDocumentField.fields) !== null && _a !== void 0 ? _a : {}, modelField !== null && modelField !== void 0 ? modelField : [], defaultLocaleCode),
|
|
973
|
+
fields: mapCSIFieldsToStoreFields({
|
|
974
|
+
csiDocumentFields: (_b = csiDocumentField.fields) !== null && _b !== void 0 ? _b : {},
|
|
944
975
|
modelFields: (_c = modelField.fields) !== null && _c !== void 0 ? _c : [],
|
|
945
976
|
modelMap,
|
|
946
977
|
defaultLocaleCode
|
|
@@ -948,15 +979,15 @@ function mapObjectField(documentField, modelField, modelMap, defaultLocaleCode)
|
|
|
948
979
|
};
|
|
949
980
|
}
|
|
950
981
|
return {
|
|
951
|
-
type:
|
|
982
|
+
type: csiDocumentField.type,
|
|
952
983
|
localized: true,
|
|
953
|
-
locales: lodash_1.default.mapValues(
|
|
984
|
+
locales: lodash_1.default.mapValues(csiDocumentField.locales, (locale) => {
|
|
954
985
|
var _a, _b, _c;
|
|
955
986
|
return {
|
|
956
987
|
locale: locale.locale,
|
|
957
988
|
srcObjectLabel: getObjectLabel((_a = locale.fields) !== null && _a !== void 0 ? _a : {}, modelField, locale.locale),
|
|
958
|
-
fields:
|
|
959
|
-
|
|
989
|
+
fields: mapCSIFieldsToStoreFields({
|
|
990
|
+
csiDocumentFields: (_b = locale.fields) !== null && _b !== void 0 ? _b : {},
|
|
960
991
|
modelFields: (_c = modelField.fields) !== null && _c !== void 0 ? _c : [],
|
|
961
992
|
modelMap,
|
|
962
993
|
defaultLocaleCode
|
|
@@ -965,17 +996,17 @@ function mapObjectField(documentField, modelField, modelMap, defaultLocaleCode)
|
|
|
965
996
|
})
|
|
966
997
|
};
|
|
967
998
|
}
|
|
968
|
-
function mapModelField(
|
|
999
|
+
function mapModelField(csiDocumentField, modelField, modelMap, defaultLocaleCode) {
|
|
969
1000
|
var _a, _b, _c, _d;
|
|
970
|
-
if (!(0, content_source_interface_1.isLocalizedField)(
|
|
971
|
-
const model = modelMap[
|
|
1001
|
+
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
1002
|
+
const model = modelMap[csiDocumentField.modelName];
|
|
972
1003
|
return {
|
|
973
|
-
type:
|
|
974
|
-
srcObjectLabel: getObjectLabel((_a =
|
|
975
|
-
srcModelName:
|
|
1004
|
+
type: csiDocumentField.type,
|
|
1005
|
+
srcObjectLabel: getObjectLabel((_a = csiDocumentField.fields) !== null && _a !== void 0 ? _a : {}, model, defaultLocaleCode),
|
|
1006
|
+
srcModelName: csiDocumentField.modelName,
|
|
976
1007
|
srcModelLabel: (_b = model.label) !== null && _b !== void 0 ? _b : lodash_1.default.startCase(model.name),
|
|
977
|
-
fields:
|
|
978
|
-
|
|
1008
|
+
fields: mapCSIFieldsToStoreFields({
|
|
1009
|
+
csiDocumentFields: (_c = csiDocumentField.fields) !== null && _c !== void 0 ? _c : {},
|
|
979
1010
|
modelFields: (_d = model.fields) !== null && _d !== void 0 ? _d : [],
|
|
980
1011
|
modelMap,
|
|
981
1012
|
defaultLocaleCode
|
|
@@ -983,9 +1014,9 @@ function mapModelField(documentField, modelField, modelMap, defaultLocaleCode) {
|
|
|
983
1014
|
};
|
|
984
1015
|
}
|
|
985
1016
|
return {
|
|
986
|
-
type:
|
|
1017
|
+
type: csiDocumentField.type,
|
|
987
1018
|
localized: true,
|
|
988
|
-
locales: lodash_1.default.mapValues(
|
|
1019
|
+
locales: lodash_1.default.mapValues(csiDocumentField.locales, (locale) => {
|
|
989
1020
|
var _a, _b, _c, _d;
|
|
990
1021
|
const model = modelMap[locale.modelName];
|
|
991
1022
|
return {
|
|
@@ -993,8 +1024,8 @@ function mapModelField(documentField, modelField, modelMap, defaultLocaleCode) {
|
|
|
993
1024
|
srcObjectLabel: getObjectLabel((_a = locale.fields) !== null && _a !== void 0 ? _a : {}, model, locale.locale),
|
|
994
1025
|
srcModelName: locale.modelName,
|
|
995
1026
|
srcModelLabel: (_b = model.label) !== null && _b !== void 0 ? _b : lodash_1.default.startCase(model.name),
|
|
996
|
-
fields:
|
|
997
|
-
|
|
1027
|
+
fields: mapCSIFieldsToStoreFields({
|
|
1028
|
+
csiDocumentFields: (_c = locale.fields) !== null && _c !== void 0 ? _c : {},
|
|
998
1029
|
modelFields: (_d = model.fields) !== null && _d !== void 0 ? _d : [],
|
|
999
1030
|
modelMap,
|
|
1000
1031
|
defaultLocaleCode
|
|
@@ -1003,14 +1034,14 @@ function mapModelField(documentField, modelField, modelMap, defaultLocaleCode) {
|
|
|
1003
1034
|
})
|
|
1004
1035
|
};
|
|
1005
1036
|
}
|
|
1006
|
-
function mapListField(
|
|
1007
|
-
if (!(0, content_source_interface_1.isLocalizedField)(
|
|
1037
|
+
function mapListField(csiDocumentField, modelField, modelMap, defaultLocaleCode) {
|
|
1038
|
+
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
1008
1039
|
return {
|
|
1009
|
-
type:
|
|
1010
|
-
items:
|
|
1040
|
+
type: csiDocumentField.type,
|
|
1041
|
+
items: csiDocumentField.items.map((item) => {
|
|
1011
1042
|
var _a;
|
|
1012
1043
|
return mapSourceFieldToStoreField({
|
|
1013
|
-
|
|
1044
|
+
csiDocumentField: item,
|
|
1014
1045
|
modelField: (_a = modelField.items) !== null && _a !== void 0 ? _a : { type: 'string' },
|
|
1015
1046
|
modelMap,
|
|
1016
1047
|
defaultLocaleCode
|
|
@@ -1019,16 +1050,16 @@ function mapListField(documentField, modelField, modelMap, defaultLocaleCode) {
|
|
|
1019
1050
|
};
|
|
1020
1051
|
}
|
|
1021
1052
|
return {
|
|
1022
|
-
type:
|
|
1053
|
+
type: csiDocumentField.type,
|
|
1023
1054
|
localized: true,
|
|
1024
|
-
locales: lodash_1.default.mapValues(
|
|
1055
|
+
locales: lodash_1.default.mapValues(csiDocumentField.locales, (locale) => {
|
|
1025
1056
|
var _a;
|
|
1026
1057
|
return {
|
|
1027
1058
|
locale: locale.locale,
|
|
1028
1059
|
items: ((_a = locale.items) !== null && _a !== void 0 ? _a : []).map((item) => {
|
|
1029
1060
|
var _a;
|
|
1030
1061
|
return mapSourceFieldToStoreField({
|
|
1031
|
-
|
|
1062
|
+
csiDocumentField: item,
|
|
1032
1063
|
modelField: (_a = modelField.items) !== null && _a !== void 0 ? _a : { type: 'string' },
|
|
1033
1064
|
modelMap,
|
|
1034
1065
|
defaultLocaleCode
|
|
@@ -1038,17 +1069,17 @@ function mapListField(documentField, modelField, modelMap, defaultLocaleCode) {
|
|
|
1038
1069
|
})
|
|
1039
1070
|
};
|
|
1040
1071
|
}
|
|
1041
|
-
function mapRichTextField(
|
|
1042
|
-
if (!(0, content_source_interface_1.isLocalizedField)(
|
|
1072
|
+
function mapRichTextField(csiDocumentField) {
|
|
1073
|
+
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
1043
1074
|
return {
|
|
1044
|
-
...
|
|
1075
|
+
...csiDocumentField,
|
|
1045
1076
|
multiElement: true
|
|
1046
1077
|
};
|
|
1047
1078
|
}
|
|
1048
1079
|
return {
|
|
1049
|
-
type:
|
|
1080
|
+
type: csiDocumentField.type,
|
|
1050
1081
|
localized: true,
|
|
1051
|
-
locales: lodash_1.default.mapValues(
|
|
1082
|
+
locales: lodash_1.default.mapValues(csiDocumentField.locales, (locale) => {
|
|
1052
1083
|
return {
|
|
1053
1084
|
...locale,
|
|
1054
1085
|
multiElement: true
|
|
@@ -1056,18 +1087,18 @@ function mapRichTextField(documentField) {
|
|
|
1056
1087
|
})
|
|
1057
1088
|
};
|
|
1058
1089
|
}
|
|
1059
|
-
function mapMarkdownField(
|
|
1060
|
-
if (!(0, content_source_interface_1.isLocalizedField)(
|
|
1090
|
+
function mapMarkdownField(csiDocumentField) {
|
|
1091
|
+
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
1061
1092
|
return {
|
|
1062
1093
|
type: 'markdown',
|
|
1063
|
-
value:
|
|
1094
|
+
value: csiDocumentField.value,
|
|
1064
1095
|
multiElement: true
|
|
1065
1096
|
};
|
|
1066
1097
|
}
|
|
1067
1098
|
return {
|
|
1068
1099
|
type: 'markdown',
|
|
1069
1100
|
localized: true,
|
|
1070
|
-
locales: lodash_1.default.mapValues(
|
|
1101
|
+
locales: lodash_1.default.mapValues(csiDocumentField.locales, (locale) => {
|
|
1071
1102
|
return {
|
|
1072
1103
|
...locale,
|
|
1073
1104
|
multiElement: true
|
|
@@ -1076,7 +1107,7 @@ function mapMarkdownField(documentField) {
|
|
|
1076
1107
|
};
|
|
1077
1108
|
}
|
|
1078
1109
|
function mapStoreFieldsToSourceFields({ documentFields, modelFields, modelMap }) {
|
|
1079
|
-
// TODO:
|
|
1110
|
+
// TODO: implement
|
|
1080
1111
|
throw new Error(`duplicateDocument not implemented yet`);
|
|
1081
1112
|
}
|
|
1082
1113
|
function getContentSourceIdForContentSource(contentSource) {
|
|
@@ -1640,4 +1671,20 @@ function isStackbitConfigFile(filePath) {
|
|
|
1640
1671
|
const isMainStackbitConfigFile = pathObject.name === 'stackbit' && ['yaml', 'yml'].includes(pathObject.ext.substring(1));
|
|
1641
1672
|
return isMainStackbitConfigFile || isInDotStackbitFolder;
|
|
1642
1673
|
}
|
|
1674
|
+
function getCSIDocumentsAndAssetsFromContentSourceDataByIds(contentSourceData, objects) {
|
|
1675
|
+
const documents = [];
|
|
1676
|
+
const assets = [];
|
|
1677
|
+
for (const object of objects) {
|
|
1678
|
+
if (object.srcObjectId in contentSourceData.csiDocumentMap) {
|
|
1679
|
+
documents.push(contentSourceData.csiDocumentMap[object.srcObjectId]);
|
|
1680
|
+
}
|
|
1681
|
+
else if (object.srcObjectId in contentSourceData.csiAssetMap) {
|
|
1682
|
+
assets.push(contentSourceData.csiAssetMap[object.srcObjectId]);
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
return {
|
|
1686
|
+
documents,
|
|
1687
|
+
assets
|
|
1688
|
+
};
|
|
1689
|
+
}
|
|
1643
1690
|
//# sourceMappingURL=content-store.js.map
|