@contentstorage/core 0.3.47 → 0.4.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.
@@ -0,0 +1,6 @@
1
+ import { AppConfig } from '../types.js';
2
+ /**
3
+ * Helper function to define your application configuration.
4
+ * Provides autocompletion and type-checking for contentstorage.config.js files.
5
+ */
6
+ export declare function defineConfig(config: AppConfig): AppConfig;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Helper function to define your application configuration.
3
+ * Provides autocompletion and type-checking for contentstorage.config.js files.
4
+ */
5
+ export function defineConfig(config) {
6
+ // You can add basic runtime validation here if desired,
7
+ // e.g., check if contentUrl is a valid URL format,
8
+ // or if languageCodes is not empty.
9
+ if (!config.languageCodes || config.languageCodes.length === 0) {
10
+ console.warn('Warning: languageCodes array is empty or missing in the configuration.');
11
+ }
12
+ if (!config.contentDir) {
13
+ // This would typically be a hard error, but defineConfig is more for type safety at edit time.
14
+ // Runtime validation (see point 3) is better for hard errors.
15
+ console.warn('Warning: contentDir is missing in the configuration.');
16
+ }
17
+ // ... other checks
18
+ return config;
19
+ }
@@ -1,4 +1,4 @@
1
- import { AppConfig, ContentStructure, GetImageReturn, GetTextReturn, GetVariationReturn } from '../types.js';
1
+ import { AppConfig, ContentStructure, GetImageReturn, GetTextReturn, GetVariationReturn, LanguageCode } from '../types.js';
2
2
  export declare let activeContent: object | null;
3
3
  export declare let appConfig: Pick<AppConfig, 'contentKey' | 'languageCodes'> | null;
4
4
  /**
@@ -6,7 +6,10 @@ export declare let appConfig: Pick<AppConfig, 'contentKey' | 'languageCodes'> |
6
6
  * It will internally ensure the application configuration (for contentDir) is loaded.
7
7
  * @param contentJson
8
8
  */
9
- export declare function setContentLanguage(contentJson: object): void;
9
+ export declare function setContentLanguage({ languageCode, contentJson, }: {
10
+ languageCode: LanguageCode;
11
+ contentJson: object;
12
+ }): void;
10
13
  export declare function initContentStorage(config: Pick<AppConfig, 'contentKey' | 'languageCodes'>): void;
11
14
  /**
12
15
  * Retrieves the text string from the loaded JSON content for the given path.
@@ -5,21 +5,26 @@ export let appConfig = null;
5
5
  * NB! Only used when live editor mode is on
6
6
  */
7
7
  window.memoryMap = new Map();
8
+ window.currentLanguageCode = null;
8
9
  /**
9
10
  * Loads and sets the content for a specific language.
10
11
  * It will internally ensure the application configuration (for contentDir) is loaded.
11
12
  * @param contentJson
12
13
  */
13
- export function setContentLanguage(contentJson) {
14
+ export function setContentLanguage({ languageCode, contentJson, }) {
14
15
  if (!contentJson || typeof contentJson !== 'object') {
15
- throw new Error('[Contentstorage] Invalid contentKey might be provided which caused setContentLanguage to fail.');
16
+ throw new Error('[Contentstorage] Invalid contentJson might be provided which caused setContentLanguage to fail.');
16
17
  }
17
18
  try {
18
19
  activeContent = contentJson; // Relies on augmentation
20
+ if (typeof window !== 'undefined') {
21
+ window.currentLanguageCode = languageCode;
22
+ }
19
23
  console.log(`[Contentstorage] Content loaded.`);
20
24
  }
21
25
  catch (error) {
22
26
  activeContent = null; // Reset on failure
27
+ window.currentLanguageCode = null;
23
28
  console.error(`[Contentstorage] Failed to load content. Error: ${error.message}`);
24
29
  }
25
30
  }
@@ -128,8 +133,8 @@ export function getImage(contentKey) {
128
133
  current.contentstorage_type === 'image' &&
129
134
  typeof current.url === 'string') {
130
135
  const currentData = current;
136
+ const key = `https://di0fmnnsdfsl2.cloudfront.net/${currentData.url}`;
131
137
  if (window.parent && window.parent !== window) {
132
- const key = currentData.url;
133
138
  const existingEntry = window.memoryMap.get(key);
134
139
  const idSet = existingEntry ? existingEntry.ids : new Set();
135
140
  idSet.add(contentKey); // Add the current ID to the set.
@@ -143,7 +148,7 @@ export function getImage(contentKey) {
143
148
  contentKey,
144
149
  data: {
145
150
  ...currentData,
146
- url: `https://di0fmnnsdfsl2.cloudfront.net/${currentData.url}`,
151
+ url: key,
147
152
  },
148
153
  };
149
154
  }
@@ -186,6 +191,17 @@ export function getVariation(contentKey, variationKey, variables) {
186
191
  variationKey in variationObject.data) {
187
192
  if (typeof variationObject.data[variationKey] === 'string') {
188
193
  const current = variationObject.data[variationKey];
194
+ if (window.parent && window.parent !== window) {
195
+ const key = current;
196
+ const existingEntry = window.memoryMap.get(key);
197
+ const idSet = existingEntry ? existingEntry.ids : new Set();
198
+ idSet.add(contentKey); // Add the current ID to the set.
199
+ window.memoryMap.set(key, {
200
+ ids: idSet,
201
+ type: 'variation',
202
+ variation: variationKey,
203
+ });
204
+ }
189
205
  if (!variables || Object.keys(variables).length === 0) {
190
206
  return {
191
207
  contentKey,
@@ -208,6 +224,17 @@ export function getVariation(contentKey, variationKey, variables) {
208
224
  if (variationKey && variationKey !== 'default') {
209
225
  console.warn(`[Contentstorage] getVariation: Variation key "${variationKey}" not found at path "${contentKey}". Returning 'default' variation.`);
210
226
  }
227
+ if (window.parent && window.parent !== window) {
228
+ const key = current;
229
+ const existingEntry = window.memoryMap.get(key);
230
+ const idSet = existingEntry ? existingEntry.ids : new Set();
231
+ idSet.add(contentKey); // Add the current ID to the set.
232
+ window.memoryMap.set(key, {
233
+ ids: idSet,
234
+ type: 'variation',
235
+ variation: 'default',
236
+ });
237
+ }
211
238
  return {
212
239
  contentKey,
213
240
  text: variationObject.data.default,
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@contentstorage/core",
3
3
  "author": "Kaido Hussar <kaidohus@gmail.com>",
4
4
  "homepage": "https://contentstorage.app",
5
- "version": "0.3.47",
5
+ "version": "0.4.0",
6
6
  "type": "module",
7
7
  "description": "Fetch content from contentstorage and generate TypeScript types",
8
8
  "module": "dist/index.js",
@@ -1 +0,0 @@
1
- export declare const setKeyToHtmlTag: (contentKey: string) => void;
@@ -1,5 +0,0 @@
1
- const htmlElem = document.documentElement;
2
- export const setKeyToHtmlTag = (contentKey) => {
3
- const dataKey = `data-contentstorage-key-${contentKey}`;
4
- htmlElem.setAttribute(dataKey, 'true');
5
- };