@stackbit/cms-core 0.1.10 → 0.1.12-locale.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-store-types.d.ts +9 -3
- package/dist/content-store-types.d.ts.map +1 -1
- package/dist/content-store.d.ts +12 -4
- package/dist/content-store.d.ts.map +1 -1
- package/dist/content-store.js +67 -50
- package/dist/content-store.js.map +1 -1
- package/dist/utils/create-update-csi-docs.d.ts.map +1 -1
- package/dist/utils/create-update-csi-docs.js +11 -0
- package/dist/utils/create-update-csi-docs.js.map +1 -1
- package/dist/utils/csi-to-store-docs-converter.d.ts +20 -0
- package/dist/utils/csi-to-store-docs-converter.d.ts.map +1 -1
- package/dist/utils/csi-to-store-docs-converter.js +23 -0
- package/dist/utils/csi-to-store-docs-converter.js.map +1 -1
- package/dist/utils/duplicate-document.d.ts +12 -0
- package/dist/utils/duplicate-document.d.ts.map +1 -0
- package/dist/utils/duplicate-document.js +196 -0
- package/dist/utils/duplicate-document.js.map +1 -0
- package/dist/utils/store-to-api-docs-converter.d.ts.map +1 -1
- package/dist/utils/store-to-api-docs-converter.js +56 -36
- package/dist/utils/store-to-api-docs-converter.js.map +1 -1
- package/package.json +4 -4
- package/src/content-store-types.ts +9 -3
- package/src/content-store.ts +68 -60
- package/src/utils/create-update-csi-docs.ts +11 -0
- package/src/utils/csi-to-store-docs-converter.ts +23 -0
- package/src/utils/duplicate-document.ts +254 -0
- package/src/utils/store-to-api-docs-converter.ts +53 -36
|
@@ -24,13 +24,20 @@ function toLocalizedAPIFields(docFields, locale) {
|
|
|
24
24
|
}
|
|
25
25
|
function toLocalizedAPIField(docField, locale, isListItem = false) {
|
|
26
26
|
var _a;
|
|
27
|
-
function localeFields(localized
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
27
|
+
function localeFields(localized) {
|
|
28
|
+
const isLocalized = !!localized;
|
|
29
|
+
if (isListItem) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
if (!isLocalized) {
|
|
33
|
+
return {
|
|
34
|
+
localized: isLocalized
|
|
33
35
|
};
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
localized: isLocalized,
|
|
39
|
+
locale: locale
|
|
40
|
+
};
|
|
34
41
|
}
|
|
35
42
|
switch (docField.type) {
|
|
36
43
|
case 'string':
|
|
@@ -51,38 +58,52 @@ function toLocalizedAPIField(docField, locale, isListItem = false) {
|
|
|
51
58
|
case 'richText':
|
|
52
59
|
if (docField.localized) {
|
|
53
60
|
const { localized, locales, ...base } = docField;
|
|
54
|
-
const localeProps = locale ? locales[locale] : undefined;
|
|
61
|
+
const localeProps = locales && locale ? locales[locale] : undefined;
|
|
55
62
|
return {
|
|
56
63
|
...base,
|
|
57
64
|
...(localeProps !== null && localeProps !== void 0 ? localeProps : { value: null }),
|
|
58
|
-
...localeFields(localized
|
|
65
|
+
...localeFields(localized),
|
|
59
66
|
...(['file', 'json', 'markdown', 'richText'].includes(docField.type) && !localeProps ? { isUnset: true } : null)
|
|
60
67
|
};
|
|
61
68
|
}
|
|
62
69
|
return {
|
|
63
70
|
...docField,
|
|
64
|
-
...localeFields(docField.localized
|
|
71
|
+
...localeFields(docField.localized)
|
|
65
72
|
};
|
|
66
73
|
case 'image':
|
|
67
74
|
if (docField.localized) {
|
|
68
75
|
const { localized, locales, ...base } = docField;
|
|
69
|
-
const localeProps = locale ? locales[locale] : undefined;
|
|
76
|
+
const localeProps = locales && locale ? locales[locale] : undefined;
|
|
70
77
|
return {
|
|
71
78
|
...base,
|
|
72
|
-
...(localeProps
|
|
73
|
-
|
|
79
|
+
...(localeProps
|
|
80
|
+
? {
|
|
81
|
+
...localeProps,
|
|
82
|
+
fields: toLocalizedAPIFields(localeProps.fields, locale)
|
|
83
|
+
}
|
|
84
|
+
: { isUnset: true }),
|
|
85
|
+
...localeFields(localized)
|
|
74
86
|
};
|
|
75
87
|
}
|
|
76
88
|
return {
|
|
77
|
-
...docField
|
|
78
|
-
|
|
89
|
+
...(!docField.isUnset
|
|
90
|
+
? {
|
|
91
|
+
...docField,
|
|
92
|
+
type: 'image',
|
|
93
|
+
fields: toLocalizedAPIFields(docField.fields, locale)
|
|
94
|
+
}
|
|
95
|
+
: {
|
|
96
|
+
...docField,
|
|
97
|
+
type: 'image'
|
|
98
|
+
}),
|
|
99
|
+
...localeFields(docField.localized)
|
|
79
100
|
};
|
|
80
101
|
case 'object':
|
|
81
102
|
case 'model':
|
|
82
103
|
if (docField.localized) {
|
|
83
104
|
if (docField.type === 'object') {
|
|
84
105
|
const { localized, locales, ...base } = docField;
|
|
85
|
-
const localeProps = locale ? locales[locale] : undefined;
|
|
106
|
+
const localeProps = locales && locale ? locales[locale] : undefined;
|
|
86
107
|
return {
|
|
87
108
|
...base,
|
|
88
109
|
...(localeProps
|
|
@@ -91,12 +112,12 @@ function toLocalizedAPIField(docField, locale, isListItem = false) {
|
|
|
91
112
|
fields: toLocalizedAPIFields(localeProps.fields, locale)
|
|
92
113
|
}
|
|
93
114
|
: { isUnset: true }),
|
|
94
|
-
...localeFields(localized
|
|
115
|
+
...localeFields(localized)
|
|
95
116
|
};
|
|
96
117
|
}
|
|
97
118
|
else {
|
|
98
119
|
const { localized, locales, ...base } = docField;
|
|
99
|
-
const localeProps = locale ? locales[locale] : undefined;
|
|
120
|
+
const localeProps = locales && locale ? locales[locale] : undefined;
|
|
100
121
|
return {
|
|
101
122
|
...base,
|
|
102
123
|
type: 'object',
|
|
@@ -106,7 +127,7 @@ function toLocalizedAPIField(docField, locale, isListItem = false) {
|
|
|
106
127
|
fields: toLocalizedAPIFields(localeProps.fields, locale)
|
|
107
128
|
}
|
|
108
129
|
: { isUnset: true }),
|
|
109
|
-
...localeFields(localized
|
|
130
|
+
...localeFields(localized)
|
|
110
131
|
};
|
|
111
132
|
}
|
|
112
133
|
}
|
|
@@ -121,19 +142,19 @@ function toLocalizedAPIField(docField, locale, isListItem = false) {
|
|
|
121
142
|
...docField,
|
|
122
143
|
type: 'object'
|
|
123
144
|
}),
|
|
124
|
-
...localeFields(docField.localized
|
|
145
|
+
...localeFields(docField.localized)
|
|
125
146
|
};
|
|
126
147
|
case 'reference':
|
|
127
148
|
if (docField.localized) {
|
|
128
149
|
const { type, refType, localized, locales, ...base } = docField;
|
|
129
|
-
const localeProps = locale ? locales[locale] : undefined;
|
|
150
|
+
const localeProps = locales && locale ? locales[locale] : undefined;
|
|
130
151
|
// if reference field isUnset === true, it behaves like a regular object
|
|
131
152
|
if (!localeProps || localeProps.isUnset) {
|
|
132
153
|
return {
|
|
133
154
|
type: 'object',
|
|
134
155
|
isUnset: true,
|
|
135
156
|
...base,
|
|
136
|
-
...localeFields(localized
|
|
157
|
+
...localeFields(localized)
|
|
137
158
|
};
|
|
138
159
|
}
|
|
139
160
|
return {
|
|
@@ -141,7 +162,7 @@ function toLocalizedAPIField(docField, locale, isListItem = false) {
|
|
|
141
162
|
refType: refType === 'asset' ? 'image' : 'object',
|
|
142
163
|
...base,
|
|
143
164
|
...localeProps,
|
|
144
|
-
...localeFields(localized
|
|
165
|
+
...localeFields(localized)
|
|
145
166
|
};
|
|
146
167
|
}
|
|
147
168
|
const { type, refType, ...base } = docField;
|
|
@@ -149,29 +170,29 @@ function toLocalizedAPIField(docField, locale, isListItem = false) {
|
|
|
149
170
|
return {
|
|
150
171
|
type: 'object',
|
|
151
172
|
...base,
|
|
152
|
-
...localeFields(docField.localized
|
|
173
|
+
...localeFields(docField.localized)
|
|
153
174
|
};
|
|
154
175
|
}
|
|
155
176
|
return {
|
|
156
177
|
type: 'unresolved_reference',
|
|
157
178
|
refType: refType === 'asset' ? 'image' : 'object',
|
|
158
179
|
...base,
|
|
159
|
-
...localeFields(docField.localized
|
|
180
|
+
...localeFields(docField.localized)
|
|
160
181
|
};
|
|
161
182
|
case 'list':
|
|
162
183
|
if (docField.localized) {
|
|
163
184
|
const { localized, locales, ...base } = docField;
|
|
164
|
-
const localeProps = locale ? locales[locale] : undefined;
|
|
185
|
+
const localeProps = locales && locale ? locales[locale] : undefined;
|
|
165
186
|
return {
|
|
166
187
|
...base,
|
|
167
188
|
...localeProps,
|
|
168
189
|
items: ((_a = localeProps === null || localeProps === void 0 ? void 0 : localeProps.items) !== null && _a !== void 0 ? _a : []).map((field) => toLocalizedAPIField(field, locale, true)),
|
|
169
|
-
...localeFields(localized
|
|
190
|
+
...localeFields(localized)
|
|
170
191
|
};
|
|
171
192
|
}
|
|
172
193
|
return {
|
|
173
194
|
...docField,
|
|
174
|
-
...localeFields(docField.localized
|
|
195
|
+
...localeFields(docField.localized),
|
|
175
196
|
items: docField.items.map((field) => toLocalizedAPIField(field, locale, true))
|
|
176
197
|
};
|
|
177
198
|
default:
|
|
@@ -193,7 +214,7 @@ function assetToLocalizedApiImage(asset, locale) {
|
|
|
193
214
|
};
|
|
194
215
|
}
|
|
195
216
|
function localizeAssetFields(assetFields, locale) {
|
|
196
|
-
var _a, _b;
|
|
217
|
+
var _a, _b, _c;
|
|
197
218
|
const fields = {
|
|
198
219
|
title: {
|
|
199
220
|
type: 'string',
|
|
@@ -206,17 +227,16 @@ function localizeAssetFields(assetFields, locale) {
|
|
|
206
227
|
};
|
|
207
228
|
const titleFieldNonLocalized = (0, content_store_utils_1.getDocumentFieldForLocale)(assetFields.title, locale);
|
|
208
229
|
fields.title.value = titleFieldNonLocalized === null || titleFieldNonLocalized === void 0 ? void 0 : titleFieldNonLocalized.value;
|
|
209
|
-
fields.title.locale = locale
|
|
230
|
+
fields.title.locale = locale;
|
|
210
231
|
const assetFileField = assetFields.file;
|
|
211
232
|
if (assetFileField.localized) {
|
|
212
233
|
if (locale) {
|
|
213
|
-
fields.url.value = (_b = (_a = assetFileField.locales[locale]) === null ||
|
|
234
|
+
fields.url.value = (_c = (_b = (_a = assetFileField.locales) === null || _a === void 0 ? void 0 : _a[locale]) === null || _b === void 0 ? void 0 : _b.url) !== null && _c !== void 0 ? _c : null;
|
|
214
235
|
fields.url.locale = locale;
|
|
215
236
|
}
|
|
216
237
|
}
|
|
217
238
|
else {
|
|
218
239
|
fields.url.value = assetFileField.url;
|
|
219
|
-
fields.url.locale = assetFileField.locale;
|
|
220
240
|
}
|
|
221
241
|
return fields;
|
|
222
242
|
}
|
|
@@ -225,11 +245,11 @@ function mapStoreAssetsToAPIAssets(assets, locale) {
|
|
|
225
245
|
}
|
|
226
246
|
exports.mapStoreAssetsToAPIAssets = mapStoreAssetsToAPIAssets;
|
|
227
247
|
function storeAssetToAPIAsset(asset, locale) {
|
|
228
|
-
var _a, _b;
|
|
248
|
+
var _a, _b, _c, _d;
|
|
229
249
|
const assetTitleField = asset.fields.title;
|
|
230
|
-
const localizedTitleField = assetTitleField.localized ? assetTitleField.locales[locale] : assetTitleField;
|
|
250
|
+
const localizedTitleField = assetTitleField.localized ? (_a = assetTitleField.locales) === null || _a === void 0 ? void 0 : _a[locale] : assetTitleField;
|
|
231
251
|
const assetFileField = asset.fields.file;
|
|
232
|
-
const localizedFileField = assetFileField.localized ? assetFileField.locales[locale] : assetFileField;
|
|
252
|
+
const localizedFileField = assetFileField.localized ? (_b = assetFileField.locales) === null || _b === void 0 ? void 0 : _b[locale] : assetFileField;
|
|
233
253
|
return {
|
|
234
254
|
objectId: asset.srcObjectId,
|
|
235
255
|
createdAt: asset.createdAt,
|
|
@@ -239,8 +259,8 @@ function storeAssetToAPIAsset(asset, locale) {
|
|
|
239
259
|
fileName: localizedFileField.fileName,
|
|
240
260
|
contentType: localizedFileField.contentType,
|
|
241
261
|
size: localizedFileField.size,
|
|
242
|
-
width: (
|
|
243
|
-
height: (
|
|
262
|
+
width: (_c = localizedFileField.dimensions) === null || _c === void 0 ? void 0 : _c.width,
|
|
263
|
+
height: (_d = localizedFileField.dimensions) === null || _d === void 0 ? void 0 : _d.height
|
|
244
264
|
})
|
|
245
265
|
};
|
|
246
266
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"store-to-api-docs-converter.js","sourceRoot":"","sources":["../../src/utils/store-to-api-docs-converter.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAuB;AACvB,2CAA4C;AAE5C,gEAAmE;AAEnE,SAAgB,iCAAiC,CAAC,SAAuC,EAAE,MAAe;IACtG,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,4BAA4B,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;AACvF,CAAC;AAFD,8EAEC;AAED,SAAS,4BAA4B,CAAC,QAAoC,EAAE,MAAe;IACvF,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,QAAQ,CAAC;IAC3C,OAAO;QACH,IAAI,EAAE,QAAQ;QACd,GAAG,IAAI;QACP,MAAM,EAAE,oBAAoB,CAAC,MAAM,EAAE,MAAM,CAAC;KAC/C,CAAC;AACN,CAAC;AAED,SAAS,oBAAoB,CAAC,SAA0D,EAAE,MAAe;IACrG,OAAO,gBAAC,CAAC,SAAS,CAAC,SAAS,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC,mBAAmB,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;AACvF,CAAC;AAED,SAAS,mBAAmB,CAAC,QAAyC,EAAE,MAAe,EAAE,UAAU,GAAG,KAAK;;IAEvG,SAAS,YAAY,
|
|
1
|
+
{"version":3,"file":"store-to-api-docs-converter.js","sourceRoot":"","sources":["../../src/utils/store-to-api-docs-converter.ts"],"names":[],"mappings":";;;;;;AAAA,oDAAuB;AACvB,2CAA4C;AAE5C,gEAAmE;AAEnE,SAAgB,iCAAiC,CAAC,SAAuC,EAAE,MAAe;IACtG,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,4BAA4B,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;AACvF,CAAC;AAFD,8EAEC;AAED,SAAS,4BAA4B,CAAC,QAAoC,EAAE,MAAe;IACvF,MAAM,EAAE,IAAI,EAAE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3C,MAAM,EAAE,MAAA,kBAAkB,CAAC,UAAU,0CAAE,MAAM;SAChD,CAAC;KACL,CAAC;AACN,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stackbit/cms-core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12-locale.0",
|
|
4
4
|
"description": "stackbit-dev",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
"@babel/parser": "^7.11.5",
|
|
30
30
|
"@babel/traverse": "^7.11.5",
|
|
31
31
|
"@iarna/toml": "^2.2.3",
|
|
32
|
-
"@stackbit/sdk": "^0.3.
|
|
33
|
-
"@stackbit/types": "^0.1.
|
|
32
|
+
"@stackbit/sdk": "^0.3.8-locale.0",
|
|
33
|
+
"@stackbit/types": "^0.1.6-locale.0",
|
|
34
34
|
"@stackbit/utils": "^0.2.11",
|
|
35
35
|
"chalk": "^4.0.1",
|
|
36
36
|
"esm": "^3.2.25",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"sanitize-filename": "^1.6.3",
|
|
46
46
|
"slugify": "^1.6.5"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "5ae211b3a7770c097d36efe3b860ed4d4d8a3c25"
|
|
49
49
|
}
|
|
@@ -19,6 +19,7 @@ export interface Document {
|
|
|
19
19
|
createdBy?: string;
|
|
20
20
|
updatedAt: string;
|
|
21
21
|
updatedBy?: string[];
|
|
22
|
+
locale?: string;
|
|
22
23
|
fields: Record<string, DocumentField>;
|
|
23
24
|
}
|
|
24
25
|
|
|
@@ -39,6 +40,7 @@ export interface Asset {
|
|
|
39
40
|
createdBy?: string;
|
|
40
41
|
updatedAt: string;
|
|
41
42
|
updatedBy?: string[];
|
|
43
|
+
locale?: string;
|
|
42
44
|
fields: AssetFields;
|
|
43
45
|
}
|
|
44
46
|
|
|
@@ -177,7 +179,6 @@ export type DocumentFieldTypeNonLocalized<BaseFieldProps, LocalizedFieldProps> =
|
|
|
177
179
|
LocalizedFieldProps & {
|
|
178
180
|
label?: string;
|
|
179
181
|
localized?: false;
|
|
180
|
-
locale?: string;
|
|
181
182
|
}
|
|
182
183
|
>;
|
|
183
184
|
|
|
@@ -318,8 +319,13 @@ export type DocumentImageFieldLocalized = DocumentFieldTypeLocalized<DocumentIma
|
|
|
318
319
|
export type DocumentImageFieldNonLocalized = DocumentFieldTypeNonLocalized<DocumentImageFieldBase, DocumentImageFieldProps>;
|
|
319
320
|
export type DocumentImageFieldAPI = DocumentFieldTypeAPI<DocumentImageFieldBase, DocumentImageFieldPropsAPI>;
|
|
320
321
|
export type DocumentImageFieldBase = { type: 'image' };
|
|
321
|
-
export type DocumentImageFieldProps = DocumentFieldUnsetProps | { isUnset?: false; fields:
|
|
322
|
-
export type DocumentImageFieldPropsAPI = DocumentFieldUnsetProps | { isUnset?: false; fields:
|
|
322
|
+
export type DocumentImageFieldProps = DocumentFieldUnsetProps | { isUnset?: false; fields: ImageFields };
|
|
323
|
+
export type DocumentImageFieldPropsAPI = DocumentFieldUnsetProps | { isUnset?: false; fields: ImageFieldsAPI };
|
|
324
|
+
export type ImageFields = {
|
|
325
|
+
title: DocumentFieldForType<'string'>;
|
|
326
|
+
url: DocumentFieldForType<'string'>;
|
|
327
|
+
};
|
|
328
|
+
export type ImageFieldsAPI = AssetFieldsAPI;
|
|
323
329
|
|
|
324
330
|
// assetFile
|
|
325
331
|
export type AssetFileField = AssetFileFieldLocalized | AssetFileFieldNonLocalized;
|
package/src/content-store.ts
CHANGED
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
} from './content-store-utils';
|
|
37
37
|
import { mapAssetsToLocalizedApiImages, mapDocumentsToLocalizedApiObjects, mapStoreAssetsToAPIAssets } from './utils/store-to-api-docs-converter';
|
|
38
38
|
import { convertOperationField, createDocumentRecursively, getCreateDocumentThunk } from './utils/create-update-csi-docs';
|
|
39
|
+
import { mergeObjectWithDocument } from './utils/duplicate-document';
|
|
39
40
|
import { normalizeModels, validateModels } from './utils/model-utils';
|
|
40
41
|
import { IMAGE_MODEL } from './common/common-schema';
|
|
41
42
|
|
|
@@ -707,8 +708,11 @@ export class ContentStore {
|
|
|
707
708
|
);
|
|
708
709
|
}
|
|
709
710
|
|
|
710
|
-
getPresets(): Record<string, any> {
|
|
711
|
-
|
|
711
|
+
getPresets({ locale }: { locale?: string } = {}): Record<string, any> {
|
|
712
|
+
if (!this.presets || !locale) {
|
|
713
|
+
return this.presets ?? {};
|
|
714
|
+
}
|
|
715
|
+
return _.pickBy(this.presets, preset => !preset.locale || preset.locale === locale);
|
|
712
716
|
}
|
|
713
717
|
|
|
714
718
|
getContentSourceEnvironment({ srcProjectId, srcType }: { srcProjectId: string; srcType: string }): string {
|
|
@@ -838,11 +842,19 @@ export class ContentStore {
|
|
|
838
842
|
return contentSourceData.documentMap[srcDocumentId];
|
|
839
843
|
}
|
|
840
844
|
|
|
841
|
-
getDocuments(): ContentStoreTypes.Document[] {
|
|
845
|
+
getDocuments({ contentSourceIds, locale }: { contentSourceIds?: string[], locale?: string }): ContentStoreTypes.Document[] {
|
|
846
|
+
const hasExplicitLocale = !_.isEmpty(locale);
|
|
842
847
|
return _.reduce(
|
|
843
848
|
this.contentSourceDataById,
|
|
844
849
|
(documents: ContentStoreTypes.Document[], contentSourceData) => {
|
|
845
|
-
|
|
850
|
+
if (contentSourceIds && !contentSourceIds.includes(contentSourceData.id)) {
|
|
851
|
+
return documents;
|
|
852
|
+
}
|
|
853
|
+
const currentLocale = locale ?? contentSourceData.defaultLocaleCode;
|
|
854
|
+
const currentDocuments = hasExplicitLocale
|
|
855
|
+
? contentSourceData.documents.filter(document => !document.locale || document.locale === currentLocale)
|
|
856
|
+
: contentSourceData.documents;
|
|
857
|
+
return documents.concat(currentDocuments);
|
|
846
858
|
},
|
|
847
859
|
[]
|
|
848
860
|
);
|
|
@@ -854,23 +866,35 @@ export class ContentStore {
|
|
|
854
866
|
return contentSourceData.assetMap[srcAssetId];
|
|
855
867
|
}
|
|
856
868
|
|
|
857
|
-
getAssets(): ContentStoreTypes.Asset[] {
|
|
869
|
+
getAssets({ locale }: { locale?: string }): ContentStoreTypes.Asset[] {
|
|
870
|
+
const hasExplicitLocale = !_.isEmpty(locale);
|
|
858
871
|
return _.reduce(
|
|
859
872
|
this.contentSourceDataById,
|
|
860
873
|
(assets: ContentStoreTypes.Asset[], contentSourceData) => {
|
|
861
|
-
|
|
874
|
+
const currentLocale = locale ?? contentSourceData.defaultLocaleCode;
|
|
875
|
+
const currentAssets = hasExplicitLocale
|
|
876
|
+
? contentSourceData.assets.filter(asset => !asset.locale || asset.locale === currentLocale)
|
|
877
|
+
: contentSourceData.assets;
|
|
878
|
+
return assets.concat(currentAssets);
|
|
862
879
|
},
|
|
863
880
|
[]
|
|
864
881
|
);
|
|
865
882
|
}
|
|
866
883
|
|
|
867
884
|
getLocalizedApiObjects({ locale }: { locale?: string }): ContentStoreTypes.APIObject[] {
|
|
885
|
+
const hasExplicitLocale = !_.isEmpty(locale);
|
|
868
886
|
return _.reduce(
|
|
869
887
|
this.contentSourceDataById,
|
|
870
888
|
(objects: ContentStoreTypes.APIObject[], contentSourceData) => {
|
|
871
|
-
|
|
872
|
-
const
|
|
873
|
-
|
|
889
|
+
const currentLocale = locale ?? contentSourceData.defaultLocaleCode;
|
|
890
|
+
const documents = hasExplicitLocale
|
|
891
|
+
? contentSourceData.documents.filter(document => !document.locale || document.locale === currentLocale)
|
|
892
|
+
: contentSourceData.documents;
|
|
893
|
+
const assets = hasExplicitLocale
|
|
894
|
+
? contentSourceData.assets.filter(asset => !asset.locale || asset.locale === currentLocale)
|
|
895
|
+
: contentSourceData.assets;
|
|
896
|
+
const documentObjects = mapDocumentsToLocalizedApiObjects(documents, currentLocale);
|
|
897
|
+
const imageObjects = mapAssetsToLocalizedApiImages(assets, currentLocale);
|
|
874
898
|
return objects.concat(documentObjects, imageObjects);
|
|
875
899
|
},
|
|
876
900
|
[]
|
|
@@ -1268,15 +1292,17 @@ export class ContentStore {
|
|
|
1268
1292
|
srcProjectId,
|
|
1269
1293
|
srcDocumentId,
|
|
1270
1294
|
object,
|
|
1295
|
+
locale,
|
|
1271
1296
|
user
|
|
1272
1297
|
}: {
|
|
1273
1298
|
srcType: string;
|
|
1274
1299
|
srcProjectId: string;
|
|
1275
1300
|
srcDocumentId: string;
|
|
1276
1301
|
object?: Record<string, any>;
|
|
1302
|
+
locale?: string;
|
|
1277
1303
|
user?: ContentStoreTypes.User;
|
|
1278
1304
|
}): Promise<{ srcDocumentId: string }> {
|
|
1279
|
-
this.logger.debug('duplicateDocument');
|
|
1305
|
+
this.logger.debug('duplicateDocument', { srcType, srcProjectId, srcDocumentId, locale });
|
|
1280
1306
|
|
|
1281
1307
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
1282
1308
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
@@ -1287,46 +1313,38 @@ export class ContentStore {
|
|
|
1287
1313
|
const modelMap = contentSourceData.modelMap;
|
|
1288
1314
|
const csiModelMap = contentSourceData.csiModelMap;
|
|
1289
1315
|
const model = modelMap[document.srcModelName];
|
|
1290
|
-
|
|
1291
|
-
if (!model || !csiModel) {
|
|
1316
|
+
if (!model) {
|
|
1292
1317
|
throw new Error(`no model with name '${document.srcModelName}' was found`);
|
|
1293
1318
|
}
|
|
1294
1319
|
|
|
1295
1320
|
const userContext = getUserContextForSrcType(srcType, user);
|
|
1321
|
+
const resolvedLocale = locale ?? contentSourceData.defaultLocaleCode;
|
|
1296
1322
|
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
// DocumentFields and then merging both documents into a single
|
|
1306
|
-
// Document and then calling mapStoreFieldsToOperationFields (needs to be implemented)
|
|
1307
|
-
// While doing all that, need to handle existing references in the
|
|
1308
|
-
// existing Document and to duplicate the referenced documents if their
|
|
1309
|
-
// model is marked as 'duplicatable' and to reuse the existing documents
|
|
1310
|
-
// if their model is marked as non-duplicatable.
|
|
1311
|
-
const updateOperationFields = mapStoreFieldsToOperationFields({
|
|
1312
|
-
documentFields: document.fields,
|
|
1313
|
-
modelFields: model.fields!,
|
|
1314
|
-
modelMap: contentSourceData.modelMap
|
|
1323
|
+
const extendedObject = mergeObjectWithDocument({
|
|
1324
|
+
object,
|
|
1325
|
+
document,
|
|
1326
|
+
locale: resolvedLocale,
|
|
1327
|
+
documentMap: contentSourceData.documentMap,
|
|
1328
|
+
referenceBehavior: this.stackbitConfig?.presetReferenceBehavior,
|
|
1329
|
+
duplicatableModels: this.stackbitConfig?.duplicatableModels,
|
|
1330
|
+
nonDuplicatableModels: this.stackbitConfig?.nonDuplicatableModels
|
|
1315
1331
|
});
|
|
1316
1332
|
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1333
|
+
const result = await createDocumentRecursively({
|
|
1334
|
+
object: extendedObject,
|
|
1335
|
+
modelName: model.name,
|
|
1336
|
+
modelMap,
|
|
1337
|
+
createDocument: getCreateDocumentThunk({
|
|
1338
|
+
locale: resolvedLocale,
|
|
1339
|
+
csiModelMap,
|
|
1340
|
+
userContext,
|
|
1341
|
+
contentSourceInstance: contentSourceData.instance
|
|
1342
|
+
})
|
|
1327
1343
|
});
|
|
1328
1344
|
|
|
1329
|
-
|
|
1345
|
+
this.logger.debug('duplicated document', { srcType, srcProjectId, srcDocumentId, newDocumentId: result.document.id, modelName: model.name });
|
|
1346
|
+
|
|
1347
|
+
return { srcDocumentId: result.document.id };
|
|
1330
1348
|
}
|
|
1331
1349
|
|
|
1332
1350
|
async uploadAssets({
|
|
@@ -1471,22 +1489,25 @@ export class ContentStore {
|
|
|
1471
1489
|
items: ContentStoreTypes.Document[];
|
|
1472
1490
|
}> {
|
|
1473
1491
|
this.logger.debug('searchDocuments');
|
|
1474
|
-
|
|
1492
|
+
let locale = data.locale;
|
|
1475
1493
|
const objectsBySourceId = _.groupBy(data.models, (object) => getContentSourceId(object.srcType, object.srcProjectId));
|
|
1476
|
-
const
|
|
1494
|
+
const contentSourceIds = Object.keys(objectsBySourceId);
|
|
1495
|
+
const documents: ContentStoreTypes.Document[] = this.getDocuments({ contentSourceIds, locale });
|
|
1477
1496
|
const schema: Record<string, Record<string, Record<string, Model>>> = {};
|
|
1478
1497
|
|
|
1479
|
-
|
|
1498
|
+
contentSourceIds.forEach((contentSourceId) => {
|
|
1480
1499
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
1481
|
-
|
|
1482
|
-
documents.push(...contentSourceData.documents);
|
|
1483
1500
|
_.set(schema, [contentSourceData.srcType, contentSourceData.srcProjectId], contentSourceData.modelMap);
|
|
1501
|
+
if (!locale && contentSourceData.defaultLocaleCode) {
|
|
1502
|
+
locale = contentSourceData.defaultLocaleCode;
|
|
1503
|
+
}
|
|
1484
1504
|
});
|
|
1485
1505
|
|
|
1486
1506
|
return searchDocuments({
|
|
1487
1507
|
...data,
|
|
1488
1508
|
documents,
|
|
1489
|
-
schema
|
|
1509
|
+
schema,
|
|
1510
|
+
locale
|
|
1490
1511
|
});
|
|
1491
1512
|
}
|
|
1492
1513
|
|
|
@@ -1524,19 +1545,6 @@ export class ContentStore {
|
|
|
1524
1545
|
}
|
|
1525
1546
|
}
|
|
1526
1547
|
|
|
1527
|
-
function mapStoreFieldsToOperationFields({
|
|
1528
|
-
documentFields,
|
|
1529
|
-
modelFields,
|
|
1530
|
-
modelMap
|
|
1531
|
-
}: {
|
|
1532
|
-
documentFields: Record<string, ContentStoreTypes.DocumentField>;
|
|
1533
|
-
modelFields: Field[];
|
|
1534
|
-
modelMap: Record<string, Model>;
|
|
1535
|
-
}): Record<string, CSITypes.UpdateOperationField> {
|
|
1536
|
-
// TODO: implement
|
|
1537
|
-
throw new Error(`duplicateDocument not implemented yet`);
|
|
1538
|
-
}
|
|
1539
|
-
|
|
1540
1548
|
function getCSIDocumentsAndAssetsFromContentSourceDataByIds(
|
|
1541
1549
|
contentSourceData: ContentSourceData,
|
|
1542
1550
|
objects: { srcObjectId: string }[]
|
|
@@ -161,16 +161,25 @@ async function createNestedObjectRecursively({
|
|
|
161
161
|
fields: {},
|
|
162
162
|
newRefDocuments: []
|
|
163
163
|
};
|
|
164
|
+
// When creating new documents, we are iterating over the model's fields to
|
|
165
|
+
// construct the update operations. In case object has a field that does not
|
|
166
|
+
// exist on the model, it will be ignored. On the other hand, if a model has
|
|
167
|
+
// a field with "const" or "default" property that does not exist on the
|
|
168
|
+
// object, the new document will be created with that field set to the value
|
|
169
|
+
// of the "const" or the "default" value.
|
|
164
170
|
const objectFieldNames = Object.keys(object);
|
|
165
171
|
for (const modelField of modelFields) {
|
|
166
172
|
const fieldName = modelField.name;
|
|
167
173
|
let value;
|
|
168
174
|
if (fieldName in object) {
|
|
175
|
+
// if the object has a field name matching a model field, use it
|
|
169
176
|
value = object[fieldName];
|
|
170
177
|
_.pull(objectFieldNames, fieldName);
|
|
171
178
|
} else if (modelField.const) {
|
|
179
|
+
// if the model field has the "const" property, use its value
|
|
172
180
|
value = modelField.const;
|
|
173
181
|
} else if (!_.isNil(modelField.default)) {
|
|
182
|
+
// if the model field has the "default" property, use its value
|
|
174
183
|
value = modelField.default;
|
|
175
184
|
}
|
|
176
185
|
if (!_.isNil(value)) {
|
|
@@ -254,6 +263,8 @@ async function createNestedField({
|
|
|
254
263
|
};
|
|
255
264
|
} else if (modelField.type === 'image') {
|
|
256
265
|
let refId: string | undefined;
|
|
266
|
+
// TODO: if modelField.source is cloudinary, the new document field
|
|
267
|
+
// should be of the 'image' type with 'title' and 'url' properties
|
|
257
268
|
if (_.isPlainObject(value)) {
|
|
258
269
|
refId = value.$$ref;
|
|
259
270
|
} else {
|
|
@@ -48,6 +48,7 @@ function sourceAssetToStoreAsset({
|
|
|
48
48
|
createdBy: csiAsset.createdBy,
|
|
49
49
|
updatedAt: csiAsset.updatedAt,
|
|
50
50
|
updatedBy: csiAsset.updatedBy,
|
|
51
|
+
locale: csiAsset.locale,
|
|
51
52
|
fields: {
|
|
52
53
|
title: {
|
|
53
54
|
label: 'Title',
|
|
@@ -61,6 +62,26 @@ function sourceAssetToStoreAsset({
|
|
|
61
62
|
};
|
|
62
63
|
}
|
|
63
64
|
|
|
65
|
+
/**
|
|
66
|
+
* CSI documents do not specify unset fields. For example, if the "title" field
|
|
67
|
+
* is not set in CMS, the CSI document will not have the title field in its
|
|
68
|
+
* "fields" map.
|
|
69
|
+
*
|
|
70
|
+
* On the other hand, and for historical reasons, Stackbit client requires all
|
|
71
|
+
* fields to be defined on the document, even the fields that are not set to any
|
|
72
|
+
* value. Therefore, until this issue is solved and Stackbit client will be able
|
|
73
|
+
* to infer unset document field from the model, every content store document
|
|
74
|
+
* need to be extended and list all the fields from the matching model.
|
|
75
|
+
*
|
|
76
|
+
* Empty primitive fields like "string" are regarded empty when they have no
|
|
77
|
+
* "value" property. Other more complex fields like objects and references have
|
|
78
|
+
* the special "isUnset" property.
|
|
79
|
+
*
|
|
80
|
+
* @param csiDocuments
|
|
81
|
+
* @param contentSourceInstance
|
|
82
|
+
* @param modelMap
|
|
83
|
+
* @param defaultLocaleCode
|
|
84
|
+
*/
|
|
64
85
|
export function mapCSIDocumentsToStoreDocuments({
|
|
65
86
|
csiDocuments,
|
|
66
87
|
contentSourceInstance,
|
|
@@ -111,6 +132,7 @@ function mapCSIDocumentToStoreDocument({
|
|
|
111
132
|
createdBy: csiDocument.createdBy,
|
|
112
133
|
updatedAt: csiDocument.updatedAt,
|
|
113
134
|
updatedBy: csiDocument.updatedBy,
|
|
135
|
+
locale: csiDocument.locale,
|
|
114
136
|
fields: mapCSIFieldsToStoreFields({
|
|
115
137
|
csiDocumentFields: csiDocument.fields,
|
|
116
138
|
modelFields: model.fields ?? [],
|
|
@@ -144,6 +166,7 @@ function mapCSIFieldsToStoreFields({
|
|
|
144
166
|
context
|
|
145
167
|
});
|
|
146
168
|
docField.label = modelField.label;
|
|
169
|
+
docField.localized = modelField.localized;
|
|
147
170
|
result[modelField.name] = docField;
|
|
148
171
|
return result;
|
|
149
172
|
}, {});
|