vue-wswg-editor 0.0.16 → 0.0.18

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-wswg-editor",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "type": "module",
5
5
  "main": "./dist/vue-wswg-editor.es.js",
6
6
  "module": "./dist/vue-wswg-editor.es.js",
@@ -21,6 +21,7 @@ import {
21
21
  sendSettingsOpen,
22
22
  sendScrollToBlock,
23
23
  handleIframeMessage,
24
+ sendInitIframeApp,
24
25
  } from "./messageHandler";
25
26
  import type { Block } from "../../types/Block";
26
27
  import { getIframeAppModuleUrl } from "./iframePreviewApp";
@@ -122,6 +123,26 @@ function setupMessageListener() {
122
123
 
123
124
  console.info("[parent] Received message from iframe:", event.data);
124
125
 
126
+ // Handle IFRAME_NEEDS_MODULE_URL - send module URL when iframe can't import it directly
127
+ if (event.data && event.data.type === "IFRAME_NEEDS_MODULE_URL") {
128
+ console.info("[parent] IFRAME_NEEDS_MODULE_URL received, sending module URL");
129
+ if (iframeRef.value && iframeAppModuleUrl) {
130
+ sendInitIframeApp(iframeRef.value, iframeAppModuleUrl);
131
+ } else {
132
+ console.warn("[parent] Cannot send module URL - iframe ref or module URL not available");
133
+ }
134
+ return;
135
+ }
136
+
137
+ // Handle IFRAME_READY_FOR_APP - legacy fallback mechanism
138
+ if (event.data && event.data.type === "IFRAME_READY_FOR_APP") {
139
+ console.info("[parent] IFRAME_READY_FOR_APP received, sending module URL");
140
+ if (iframeRef.value && iframeAppModuleUrl) {
141
+ sendInitIframeApp(iframeRef.value, iframeAppModuleUrl);
142
+ }
143
+ return;
144
+ }
145
+
125
146
  handleIframeMessage(event, {
126
147
  onBlockClick: (_blockId: string, block: any) => {
127
148
  emit("click-block", block);
@@ -141,22 +141,77 @@ export function generateIframeHTML(iframeAppModuleUrl?: string): string {
141
141
  // Make vue-router available globally so it can be accessed in the iframe app
142
142
  window.VueRouter = VueRouter;
143
143
 
144
+ // Use dynamic import with error handling - static imports fail if module doesn't exist
144
145
  console.info('[iframe] Attempting to import createIframeApp from:', '${iframeAppModuleUrl}');
145
- import { createIframeApp } from '${iframeAppModuleUrl}';
146
- console.info('[iframe] createIframeApp imported successfully');
146
+ let appInitialized = false;
147
147
 
148
- const appEl = document.getElementById('app');
149
- console.info('[iframe] App element found:', appEl);
150
- if (appEl) {
151
- console.info('[iframe] Calling createIframeApp...');
152
- createIframeApp(appEl).then(() => {
153
- console.info('[iframe] createIframeApp completed successfully');
154
- }).catch(error => {
155
- console.error('[iframe] Failed to create iframe app:', error);
156
- console.error('[iframe] Error stack:', error.stack);
148
+ try {
149
+ const module = await import('${iframeAppModuleUrl}');
150
+ console.info('[iframe] Module imported successfully:', module);
151
+
152
+ if (module && module.createIframeApp) {
153
+ const appEl = document.getElementById('app');
154
+ console.info('[iframe] App element found:', appEl);
155
+ if (appEl) {
156
+ console.info('[iframe] Calling createIframeApp...');
157
+ appInitialized = true;
158
+ module.createIframeApp(appEl).then(() => {
159
+ console.info('[iframe] createIframeApp completed successfully');
160
+ }).catch(error => {
161
+ console.error('[iframe] Failed to create iframe app:', error);
162
+ console.error('[iframe] Error stack:', error.stack);
163
+ });
164
+ } else {
165
+ console.error('[iframe] App element (#app) not found!');
166
+ }
167
+ } else {
168
+ console.error('[iframe] Module imported but createIframeApp not found');
169
+ throw new Error('createIframeApp not found in module');
170
+ }
171
+ } catch (error) {
172
+ console.error('[iframe] Failed to import module, falling back to postMessage:', error);
173
+ console.error('[iframe] Error details:', error.message, error.stack);
174
+
175
+ // Fallback to postMessage mechanism
176
+ window.addEventListener('message', async (event) => {
177
+ console.info('[iframe] Received message (fallback):', event.data);
178
+ if (event.data.type === 'INIT_IFRAME_APP' && event.data.moduleUrl && !appInitialized) {
179
+ appInitialized = true;
180
+ console.info('[iframe] Initializing app with module URL (fallback):', event.data.moduleUrl);
181
+ try {
182
+ const { createIframeApp } = await import(event.data.moduleUrl);
183
+ console.info('[iframe] createIframeApp imported successfully (fallback)');
184
+ const appEl = document.getElementById('app');
185
+ console.info('[iframe] App element found (fallback):', appEl);
186
+ if (appEl) {
187
+ console.info('[iframe] Calling createIframeApp (fallback)...');
188
+ createIframeApp(appEl).then(() => {
189
+ console.info('[iframe] createIframeApp completed successfully (fallback)');
190
+ }).catch(err => {
191
+ console.error('[iframe] createIframeApp failed (fallback):', err);
192
+ });
193
+ } else {
194
+ console.error('[iframe] App element not found (fallback)!');
195
+ }
196
+ } catch (err) {
197
+ console.error('[iframe] Failed to load iframe app module (fallback):', err);
198
+ // Fallback: create minimal Vue app
199
+ const appEl = document.getElementById('app');
200
+ if (appEl) {
201
+ console.info('[iframe] Creating fallback Vue app');
202
+ createApp({
203
+ template: '<div class="p-4">Loading preview...</div>'
204
+ }).mount(appEl);
205
+ }
206
+ }
207
+ }
157
208
  });
158
- } else {
159
- console.error('[iframe] App element (#app) not found!');
209
+
210
+ // Notify parent that we need the module URL via postMessage
211
+ if (window.parent) {
212
+ console.info('[iframe] Notifying parent that iframe needs module URL');
213
+ window.parent.postMessage({ type: 'IFRAME_NEEDS_MODULE_URL' }, '*');
214
+ }
160
215
  }
161
216
  </script>`
162
217
  : `<script type="module">
@@ -149,6 +149,25 @@ export function sendScrollToBlock(iframe: HTMLIFrameElement | null, blockId: str
149
149
  sendToIframe(iframe, message);
150
150
  }
151
151
 
152
+ /**
153
+ * Send module URL to iframe for initialization
154
+ * Used when iframe requests the module URL via postMessage
155
+ */
156
+ export function sendInitIframeApp(iframe: HTMLIFrameElement | null, moduleUrl: string): void {
157
+ if (!iframe || !iframe.contentWindow) {
158
+ console.warn("[parent] Cannot send INIT_IFRAME_APP: iframe not ready");
159
+ return;
160
+ }
161
+
162
+ try {
163
+ console.info("[parent] Sending INIT_IFRAME_APP with module URL:", moduleUrl);
164
+ iframe.contentWindow.postMessage({ type: "INIT_IFRAME_APP", moduleUrl }, "*");
165
+ console.info("[parent] INIT_IFRAME_APP message sent successfully");
166
+ } catch (error) {
167
+ console.error("[parent] Error sending INIT_IFRAME_APP message:", error);
168
+ }
169
+ }
170
+
152
171
  /**
153
172
  * Validate incoming message from iframe
154
173
  */