@contentstorage/core 0.3.48 → 0.4.1

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,12 +1,17 @@
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
  /**
5
5
  * Loads and sets the content for a specific language.
6
6
  * It will internally ensure the application configuration (for contentDir) is loaded.
7
7
  * @param contentJson
8
+ * Language code which is used for live editor to manage pending changes
9
+ * @param languageCode
8
10
  */
9
- export declare function setContentLanguage(contentJson: object): void;
11
+ export declare function setContentLanguage({ languageCode, contentJson, }: {
12
+ languageCode: LanguageCode;
13
+ contentJson: object;
14
+ }): void;
10
15
  export declare function initContentStorage(config: Pick<AppConfig, 'contentKey' | 'languageCodes'>): void;
11
16
  /**
12
17
  * Retrieves the text string from the loaded JSON content for the given path.
@@ -5,21 +5,28 @@ 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
13
+ * Language code which is used for live editor to manage pending changes
14
+ * @param languageCode
12
15
  */
13
- export function setContentLanguage(contentJson) {
16
+ export function setContentLanguage({ languageCode, contentJson, }) {
14
17
  if (!contentJson || typeof contentJson !== 'object') {
15
- throw new Error('[Contentstorage] Invalid contentKey might be provided which caused setContentLanguage to fail.');
18
+ throw new Error('[Contentstorage] Invalid contentJson might be provided which caused setContentLanguage to fail.');
16
19
  }
17
20
  try {
18
21
  activeContent = contentJson; // Relies on augmentation
22
+ if (typeof window !== 'undefined') {
23
+ window.currentLanguageCode = languageCode;
24
+ }
19
25
  console.log(`[Contentstorage] Content loaded.`);
20
26
  }
21
27
  catch (error) {
22
28
  activeContent = null; // Reset on failure
29
+ window.currentLanguageCode = null;
23
30
  console.error(`[Contentstorage] Failed to load content. Error: ${error.message}`);
24
31
  }
25
32
  }
@@ -186,6 +193,17 @@ export function getVariation(contentKey, variationKey, variables) {
186
193
  variationKey in variationObject.data) {
187
194
  if (typeof variationObject.data[variationKey] === 'string') {
188
195
  const current = variationObject.data[variationKey];
196
+ if (window.parent && window.parent !== window) {
197
+ const key = current;
198
+ const existingEntry = window.memoryMap.get(key);
199
+ const idSet = existingEntry ? existingEntry.ids : new Set();
200
+ idSet.add(contentKey); // Add the current ID to the set.
201
+ window.memoryMap.set(key, {
202
+ ids: idSet,
203
+ type: 'variation',
204
+ variation: variationKey,
205
+ });
206
+ }
189
207
  if (!variables || Object.keys(variables).length === 0) {
190
208
  return {
191
209
  contentKey,
@@ -208,6 +226,17 @@ export function getVariation(contentKey, variationKey, variables) {
208
226
  if (variationKey && variationKey !== 'default') {
209
227
  console.warn(`[Contentstorage] getVariation: Variation key "${variationKey}" not found at path "${contentKey}". Returning 'default' variation.`);
210
228
  }
229
+ if (window.parent && window.parent !== window) {
230
+ const key = current;
231
+ const existingEntry = window.memoryMap.get(key);
232
+ const idSet = existingEntry ? existingEntry.ids : new Set();
233
+ idSet.add(contentKey); // Add the current ID to the set.
234
+ window.memoryMap.set(key, {
235
+ ids: idSet,
236
+ type: 'variation',
237
+ variation: 'default',
238
+ });
239
+ }
211
240
  return {
212
241
  contentKey,
213
242
  text: variationObject.data.default,
@@ -24,7 +24,10 @@ export async function fetchContent(language) {
24
24
  throw new Error(`Expected a single JSON object from ${fileUrl} for language ${languageToFetch}, but received type ${typeof jsonData}. Cannot proceed.`);
25
25
  }
26
26
  console.log(`Received JSON for ${languageToFetch}`);
27
- setContentLanguage(jsonData);
27
+ setContentLanguage({
28
+ languageCode: languageToFetch,
29
+ contentJson: jsonData,
30
+ });
28
31
  }
29
32
  catch (error) {
30
33
  // Catch errors related to fetching or saving a single language file
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.48",
5
+ "version": "0.4.1",
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
- };