@contentstorage/core 0.3.24 → 0.3.26
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/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/lib/contentManagement.d.ts +6 -18
- package/dist/lib/contentManagement.js +78 -78
- package/dist/types.d.ts +12 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { AppConfig, LanguageCode, ContentStructure } from './types.js';
|
|
2
|
-
import { setContentLanguage, getText } from './lib/contentManagement.js';
|
|
2
|
+
import { setContentLanguage, getText, getImage } from './lib/contentManagement.js';
|
|
3
3
|
export { AppConfig, LanguageCode, ContentStructure };
|
|
4
|
-
export { setContentLanguage, getText };
|
|
4
|
+
export { setContentLanguage, getText, getImage };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { setContentLanguage, getText } from './lib/contentManagement.js';
|
|
2
|
-
export { setContentLanguage, getText };
|
|
1
|
+
import { setContentLanguage, getText, getImage } from './lib/contentManagement.js';
|
|
2
|
+
export { setContentLanguage, getText, getImage };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ContentStructure } from '../types.js';
|
|
1
|
+
import { ContentStructure, ImageObject } from '../types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Loads and sets the content for a specific language.
|
|
4
4
|
* It will internally ensure the application configuration (for contentDir) is loaded.
|
|
@@ -16,20 +16,8 @@ export declare function setContentLanguage(contentJson: object): void;
|
|
|
16
16
|
* @returns The text string from the JSON, or the fallbackValue, or undefined.
|
|
17
17
|
*/
|
|
18
18
|
export declare function getText(pathString: keyof ContentStructure, fallbackValue?: string): string | undefined;
|
|
19
|
-
export declare
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
connectedCallback(): void;
|
|
25
|
-
disconnectedCallback(): void;
|
|
26
|
-
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void;
|
|
27
|
-
get idKey(): string | null;
|
|
28
|
-
set idKey(value: string | null);
|
|
29
|
-
/**
|
|
30
|
-
* Handles cases where properties might be set on the element before it's upgraded.
|
|
31
|
-
* Ensures initial attribute values are processed.
|
|
32
|
-
*/
|
|
33
|
-
private upgradeProperties;
|
|
34
|
-
private updateText;
|
|
35
|
-
}
|
|
19
|
+
export declare function getImage(pathString: string, // Using string for dynamic paths
|
|
20
|
+
fallbackValue?: ImageObject): ImageObject | undefined;
|
|
21
|
+
export declare function getVariation<Path extends keyof ContentStructure>(pathString: Path, variationKey?: ContentStructure[Path] extends {
|
|
22
|
+
data: infer D;
|
|
23
|
+
} ? keyof D : string, fallbackString?: string): string | undefined;
|
|
@@ -27,10 +27,7 @@ export function setContentLanguage(contentJson) {
|
|
|
27
27
|
* If not provided, and path is not found/value not string, undefined is returned.
|
|
28
28
|
* @returns The text string from the JSON, or the fallbackValue, or undefined.
|
|
29
29
|
*/
|
|
30
|
-
export function getText(
|
|
31
|
-
// The pathString is now a direct key of your (flattened) content structure.
|
|
32
|
-
// TypeScript will provide autocompletion for these keys.
|
|
33
|
-
pathString, fallbackValue) {
|
|
30
|
+
export function getText(pathString, fallbackValue) {
|
|
34
31
|
if (!activeContent) {
|
|
35
32
|
const msg = `[Contentstorage] getText: Content not loaded (Key: "${String(pathString)}"). Ensure setContentLanguage() was called and completed successfully.`;
|
|
36
33
|
console.warn(msg);
|
|
@@ -57,90 +54,93 @@ pathString, fallbackValue) {
|
|
|
57
54
|
return fallbackValue;
|
|
58
55
|
}
|
|
59
56
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
static get observedAttributes() {
|
|
67
|
-
// Define which attributes to observe for changes.
|
|
68
|
-
return ['data-contentstorage-id'];
|
|
69
|
-
}
|
|
70
|
-
constructor() {
|
|
71
|
-
super();
|
|
72
|
-
this._id = null;
|
|
73
|
-
// You can attach a Shadow DOM here if you need style encapsulation:
|
|
74
|
-
// this.attachShadow({ mode: 'open' });
|
|
75
|
-
// For simplicity, this example will manipulate the light DOM (this.textContent).
|
|
76
|
-
}
|
|
77
|
-
connectedCallback() {
|
|
78
|
-
// Called when the element is added to the document's DOM.
|
|
79
|
-
this.upgradeProperties();
|
|
80
|
-
this.updateText();
|
|
81
|
-
}
|
|
82
|
-
disconnectedCallback() {
|
|
83
|
-
// Called when the element is removed from the document's DOM.
|
|
84
|
-
// Perform any necessary cleanup here (e.g., remove event listeners if any were added).
|
|
57
|
+
export function getImage(pathString, // Using string for dynamic paths
|
|
58
|
+
fallbackValue) {
|
|
59
|
+
if (!activeContent) {
|
|
60
|
+
const msg = `[Contentstorage] getImage: Content not loaded (Key: "${pathString}"). Ensure setContentLanguage() was called and completed successfully.`;
|
|
61
|
+
console.warn(msg);
|
|
62
|
+
return fallbackValue;
|
|
85
63
|
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
if (
|
|
90
|
-
|
|
91
|
-
// Important: The string from the attribute needs to be treated as keyof ContentStructure.
|
|
92
|
-
// This cast assumes the provided ID string is a valid key.
|
|
93
|
-
// Runtime validation against available keys in `activeContent` could be added for robustness.
|
|
94
|
-
this._keyForGetText = newValue;
|
|
95
|
-
shouldUpdate = true;
|
|
64
|
+
const keys = pathString.split('.');
|
|
65
|
+
let current = activeContent;
|
|
66
|
+
for (const key of keys) {
|
|
67
|
+
if (current && typeof current === 'object' && key in current) {
|
|
68
|
+
current = current[key];
|
|
96
69
|
}
|
|
97
|
-
|
|
98
|
-
|
|
70
|
+
else {
|
|
71
|
+
const msg = `[Contentstorage] getImage: Path "${pathString}" not found in loaded content.`;
|
|
72
|
+
console.warn(msg);
|
|
73
|
+
return fallbackValue;
|
|
99
74
|
}
|
|
100
75
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
76
|
+
if (current &&
|
|
77
|
+
typeof current === 'object' &&
|
|
78
|
+
current.contentstorage_type === 'image' &&
|
|
79
|
+
typeof current.url === 'string' &&
|
|
80
|
+
typeof current.altText === 'string') {
|
|
81
|
+
return current;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
const msg = `[Contentstorage] getImage: Value at path "${pathString}" is not a valid image object (actual value: ${JSON.stringify(current)}).`;
|
|
85
|
+
console.warn(msg);
|
|
86
|
+
return fallbackValue;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
export function getVariation(pathString, variationKey, fallbackString) {
|
|
90
|
+
if (!activeContent) {
|
|
91
|
+
const msg = `[Contentstorage] getVariation: Content not loaded (Key: "${pathString}", Variation: "${variationKey?.toString()}"). Ensure setContentLanguage() was called and completed successfully.`;
|
|
92
|
+
console.warn(msg);
|
|
93
|
+
return fallbackString;
|
|
104
94
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (
|
|
109
|
-
|
|
95
|
+
const keys = pathString.split('.');
|
|
96
|
+
let current = activeContent;
|
|
97
|
+
for (const key of keys) {
|
|
98
|
+
if (current && typeof current === 'object' && key in current) {
|
|
99
|
+
current = current[key];
|
|
110
100
|
}
|
|
111
101
|
else {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Handles cases where properties might be set on the element before it's upgraded.
|
|
118
|
-
* Ensures initial attribute values are processed.
|
|
119
|
-
*/
|
|
120
|
-
upgradeProperties() {
|
|
121
|
-
// Check if properties were set before upgrade by checking attributes.
|
|
122
|
-
if (!this._id && this.hasAttribute('data-contentstorage-id')) {
|
|
123
|
-
this._id = this.getAttribute('data-contentstorage-id');
|
|
124
|
-
this._keyForGetText = this._id;
|
|
102
|
+
const msg = `[Contentstorage] getVariation: Path "${pathString}" for variation object not found in loaded content.`;
|
|
103
|
+
console.warn(msg);
|
|
104
|
+
return fallbackString;
|
|
125
105
|
}
|
|
126
106
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
107
|
+
if (current &&
|
|
108
|
+
typeof current === 'object' &&
|
|
109
|
+
current.contentstorage_type === 'variation' &&
|
|
110
|
+
typeof current.data === 'object' &&
|
|
111
|
+
current.data !== null) {
|
|
112
|
+
const variationObject = current;
|
|
113
|
+
if (variationKey && typeof variationKey === 'string' && variationKey in variationObject.data) {
|
|
114
|
+
if (typeof variationObject.data[variationKey] === 'string') {
|
|
115
|
+
return variationObject.data[variationKey];
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
const msg = `[Contentstorage] getVariation: Variation value for key "${variationKey}" at path "${pathString}" is not a string (actual type: ${typeof variationObject.data[variationKey]}).`;
|
|
119
|
+
console.warn(msg);
|
|
120
|
+
}
|
|
131
121
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
122
|
+
// If specific variationKey is not found or not provided, try to return the 'default' variation
|
|
123
|
+
if ('default' in variationObject.data && typeof variationKey === 'string') {
|
|
124
|
+
if (typeof variationObject.data.default === 'string') {
|
|
125
|
+
if (variationKey && variationKey !== 'default') { // Warn if specific key was requested but default is being returned
|
|
126
|
+
const msg = `[Contentstorage] getVariation: Variation key "${variationKey}" not found at path "${pathString}". Returning 'default' variation.`;
|
|
127
|
+
console.warn(msg);
|
|
128
|
+
}
|
|
129
|
+
return variationObject.data.default;
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
const msg = `[Contentstorage] getVariation: 'default' variation value at path "${pathString}" is not a string (actual type: ${typeof variationObject.data.default}).`;
|
|
133
|
+
console.warn(msg);
|
|
134
|
+
}
|
|
139
135
|
}
|
|
136
|
+
// If neither specific key nor 'default' is found or valid
|
|
137
|
+
const msg = `[Contentstorage] getVariation: Neither variation key "${variationKey?.toString()}" nor 'default' variation found or valid at path "${pathString}".`;
|
|
138
|
+
console.warn(msg);
|
|
139
|
+
return fallbackString;
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
const msg = `[Contentstorage] getVariation: Value at path "${pathString}" is not a valid variation object (actual value: ${JSON.stringify(current)}).`;
|
|
143
|
+
console.warn(msg);
|
|
144
|
+
return fallbackString;
|
|
140
145
|
}
|
|
141
|
-
}
|
|
142
|
-
// Define the custom element for the browser to use.
|
|
143
|
-
// Ensure this is called only once.
|
|
144
|
-
if (typeof window !== 'undefined' && !customElements.get('cs-text')) {
|
|
145
|
-
customElements.define('cs-text', CsText);
|
|
146
146
|
}
|
package/dist/types.d.ts
CHANGED
|
@@ -22,3 +22,15 @@ export type LanguageCode = 'SQ' | 'BE' | 'BS' | 'BG' | 'HR' | 'CS' | 'DA' | 'NL'
|
|
|
22
22
|
*/
|
|
23
23
|
export interface ContentStructure {
|
|
24
24
|
}
|
|
25
|
+
export interface ImageObject {
|
|
26
|
+
contentstorage_type: "image";
|
|
27
|
+
url: string;
|
|
28
|
+
altText: string;
|
|
29
|
+
}
|
|
30
|
+
export interface VariationData {
|
|
31
|
+
[key: string]: string;
|
|
32
|
+
}
|
|
33
|
+
export interface VariationObject {
|
|
34
|
+
contentstorage_type: "variation";
|
|
35
|
+
data: VariationData;
|
|
36
|
+
}
|
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.
|
|
5
|
+
"version": "0.3.26",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"description": "Fetch content from contentstorage and generate TypeScript types",
|
|
8
8
|
"module": "dist/index.js",
|