@contentstorage/core 1.0.1 → 1.0.2
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 +45 -37
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -2,5 +2,5 @@ import { AppConfig, LanguageCode, ContentStructure } from './types.js';
|
|
|
2
2
|
import { setContentLanguage, getText, getImage, getVariation, initContentStorage } from './lib/contentManagement.js';
|
|
3
3
|
import { fetchContent } from './lib/functions/fetchContent.js';
|
|
4
4
|
export { AppConfig, LanguageCode, ContentStructure };
|
|
5
|
-
export { initContentStorage, fetchContent, setContentLanguage, getText, getImage, getVariation, };
|
|
6
|
-
|
|
5
|
+
export { initContentStorage, fetchContent, setContentLanguage, getText, getImage, getVariation, liveEditorReady, };
|
|
6
|
+
declare function liveEditorReady(retries?: number, delay?: number): Promise<boolean>;
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { setContentLanguage, getText, getImage, getVariation, initContentStorage, } from './lib/contentManagement.js';
|
|
2
2
|
import { fetchContent } from './lib/functions/fetchContent.js';
|
|
3
|
-
export { initContentStorage, fetchContent, setContentLanguage, getText, getImage, getVariation, };
|
|
4
|
-
|
|
3
|
+
export { initContentStorage, fetchContent, setContentLanguage, getText, getImage, getVariation, liveEditorReady, };
|
|
4
|
+
let liveEditorReadyPromise = null;
|
|
5
5
|
async function isLiveEditorMode() {
|
|
6
6
|
try {
|
|
7
7
|
const inIframe = window.self !== window.top;
|
|
@@ -10,44 +10,52 @@ async function isLiveEditorMode() {
|
|
|
10
10
|
return !!(inIframe && iframeMarkerFromParent);
|
|
11
11
|
}
|
|
12
12
|
catch (e) {
|
|
13
|
-
|
|
14
|
-
// where accessing window.top might throw an error.
|
|
15
|
-
// If an error occurs, it's safer to assume it might be in an iframe.
|
|
16
|
-
console.warn('Error accessing window.top, assuming iframe context:', e);
|
|
13
|
+
console.warn('Error accessing window.top:', e);
|
|
17
14
|
return false;
|
|
18
15
|
}
|
|
19
16
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return;
|
|
17
|
+
function liveEditorReady(retries = 2, delay = 3000) {
|
|
18
|
+
if (liveEditorReadyPromise) {
|
|
19
|
+
return liveEditorReadyPromise;
|
|
24
20
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
21
|
+
liveEditorReadyPromise = new Promise(async (resolve) => {
|
|
22
|
+
const isLiveMode = await isLiveEditorMode();
|
|
23
|
+
if (!isLiveMode) {
|
|
24
|
+
resolve(false);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const cdnScriptUrl = `https://cdn.contentstorage.app/live-editor.js?contentstorage-live-editor=true`;
|
|
28
|
+
const loadScript = (attempt = 1) => {
|
|
29
|
+
console.log(`Attempting to load Contentstorage live editor script (attempt ${attempt}/${retries})`);
|
|
30
|
+
const scriptElement = document.createElement('script');
|
|
31
|
+
scriptElement.type = 'text/javascript';
|
|
32
|
+
scriptElement.src = cdnScriptUrl;
|
|
33
|
+
scriptElement.onload = () => {
|
|
34
|
+
console.log(`Script loaded successfully from: ${cdnScriptUrl}`);
|
|
35
|
+
resolve(true);
|
|
36
|
+
};
|
|
37
|
+
scriptElement.onerror = (error) => {
|
|
38
|
+
// Clean up the failed script element to avoid clutter
|
|
39
|
+
scriptElement.remove();
|
|
40
|
+
console.error(`Failed to load script (attempt ${attempt}/${retries})`, error);
|
|
41
|
+
if (attempt < retries) {
|
|
42
|
+
setTimeout(() => loadScript(attempt + 1), delay);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
console.error(`All ${retries} attempts to load the script failed.`);
|
|
46
|
+
resolve(false);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
document.head.appendChild(scriptElement);
|
|
41
50
|
};
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
+
loadScript();
|
|
52
|
+
});
|
|
53
|
+
return liveEditorReadyPromise;
|
|
54
|
+
}
|
|
55
|
+
if (typeof window !== 'undefined') {
|
|
56
|
+
liveEditorReady().then((result) => {
|
|
57
|
+
if (result === true) {
|
|
58
|
+
console.log('Contentstorage live editor script loaded!');
|
|
59
|
+
}
|
|
51
60
|
});
|
|
52
|
-
|
|
53
|
-
});
|
|
61
|
+
}
|
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": "1.0.
|
|
5
|
+
"version": "1.0.2",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"description": "Fetch content from contentstorage and generate TypeScript types",
|
|
8
8
|
"module": "dist/index.js",
|