@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 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
- export declare let isInContentstorageIframe: boolean;
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
- export let isInContentstorageIframe = false;
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
- // This catch block is for rare edge cases or highly sandboxed environments
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
- isLiveEditorMode().then(async (isLiveMode) => {
21
- console.log('isLiveMode', isLiveMode);
22
- if (!isLiveMode) {
23
- return;
17
+ function liveEditorReady(retries = 2, delay = 3000) {
18
+ if (liveEditorReadyPromise) {
19
+ return liveEditorReadyPromise;
24
20
  }
25
- isInContentstorageIframe = true;
26
- const cdnScriptUrl = `https://cdn.contentstorage.app/live-editor.js?contentstorage-live-editor=true`;
27
- return new Promise((resolve, reject) => {
28
- console.log(`Attempting to load script from: ${cdnScriptUrl}`);
29
- // 1. Create a new <script> element
30
- const scriptElement = document.createElement('script');
31
- scriptElement.type = 'text/javascript';
32
- // 2. Set the src attribute to your script's URL
33
- // The browser will fetch and execute it.
34
- scriptElement.src = cdnScriptUrl;
35
- // 3. Handle successful loading
36
- scriptElement.onload = () => {
37
- console.log(`Script loaded successfully from: ${cdnScriptUrl}`);
38
- // The script has been fetched and executed by the browser.
39
- // If it's an IIFE, it has already run.
40
- resolve(true); // Resolve the promise indicating success
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
- // 4. Handle errors during loading (e.g., network error, 404)
43
- scriptElement.onerror = (error) => {
44
- console.error(`Failed to load script from: ${cdnScriptUrl}`, error);
45
- reject(new Error(`Failed to load script: ${cdnScriptUrl}`)); // Reject the promise
46
- };
47
- // 5. Append the script element to the document's head (or body)
48
- // This triggers the browser to start loading the script.
49
- document.head.appendChild(scriptElement);
50
- // Or: document.body.appendChild(scriptElement);
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
- // fetch script to handle iframe communication
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.1",
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",