@cfp-studio/iframe 18.0.1 → 18.3.0

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.
@@ -28,11 +28,23 @@
28
28
  else if (event.kind === "error" && event.source === "renderer") {
29
29
  window.__PREVIEWJS_IFRAME__.lastRenderFailed = true;
30
30
  }
31
- // 调用控制器的 onPreviewEvent 方法处理事件
32
- (
33
- // 注意: 与 core/src/html-error.ts 中的代码保持同步.
34
- window.__PREVIEWJS_CONTROLLER__ || window.parent.__PREVIEWJS_CONTROLLER__
35
- ).onPreviewEvent(event);
31
+ // 调用控制器的 onPreviewEvent 方法处理事件.
32
+ // 跨域 iframe 无法直接读取 parent 的属性,此时走 postMessage.
33
+ let delivered = false;
34
+ try {
35
+ const controller =
36
+ // 注意: 与 core/src/html-error.ts 中的代码保持同步.
37
+ window.__PREVIEWJS_CONTROLLER__ || window.parent.__PREVIEWJS_CONTROLLER__;
38
+ if (controller) {
39
+ controller.onPreviewEvent(event);
40
+ delivered = true;
41
+ }
42
+ } catch {
43
+ // cross-origin parent; fall through to postMessage
44
+ }
45
+ if (!delivered && window.parent !== window) {
46
+ window.parent.postMessage({ kind: "preview-event", event }, "*");
47
+ }
36
48
  },
37
49
  /**
38
50
  * 刷新预览
package/dist/index.d.ts DELETED
@@ -1,268 +0,0 @@
1
- import { UpdatePayload } from 'vite/types/hmrPayload';
2
-
3
- /**
4
- * 扩展全局 Window 对象,定义在 iframe 和其父窗口中使用的特定属性
5
- */
6
- declare global {
7
- interface Window {
8
- /**
9
- * iframe 中暴露的对象,用于管理渲染状态和报告事件
10
- */
11
- __PREVIEWJS_IFRAME__: {
12
- /**
13
- * 表示最后一次渲染是否失败
14
- */
15
- lastRenderFailed: boolean;
16
- toggleInspect: boolean;
17
- /**
18
- * 处理 HMR 更新
19
- * @param payload - HMR 更新的负载
20
- */
21
- /**
22
- * 报告预览事件
23
- * @param event - 预览事件对象
24
- */
25
- reportEvent(event: PreviewEvent): void;
26
- /**
27
- * 刷新预览,可选参数指定刷新选项
28
- * @param options - 刷新选项
29
- */
30
- refresh(options?: RefreshOptions): void;
31
- /**
32
- * 渲染预览,可选异步函数,接收渲染选项
33
- * @param options - 渲染选项
34
- * @returns 渲染完成的 Promise
35
- */
36
- render?(options: RenderOptions): Promise<void>;
37
- };
38
- /**
39
- * 通常在 iframe 的父窗口中暴露,用于跟踪 iframe 的状态
40
- */
41
- __PREVIEWJS_CONTROLLER__: {
42
- /**
43
- * 处理预览事件
44
- * @param event - 预览事件对象
45
- */
46
- onPreviewEvent(event: PreviewEvent): void;
47
- };
48
- }
49
- }
50
- /**
51
- * 刷新预览的选项
52
- */
53
- type RefreshOptions = {
54
- /**
55
- * 是否重新获取可预览模块
56
- */
57
- refetchPreviewableModule?: boolean;
58
- /**
59
- * 是否重新获取包装模块
60
- * added by Anthony on 2025-04-08
61
- */
62
- refetchWrapperModule?: boolean;
63
- /**
64
- * 可预览模块
65
- */
66
- previewableModule?: any;
67
- /**
68
- * 包装模块
69
- */
70
- wrapperModule?: any;
71
- };
72
- /**
73
- * 预览 iframe 的状态
74
- */
75
- type PreviewIframeState = {
76
- /**
77
- * 表示是否正在加载
78
- */
79
- loading: boolean;
80
- /**
81
- * 表示是否已经渲染
82
- */
83
- rendered: boolean;
84
- /**
85
- * 预览错误列表
86
- */
87
- errors: PreviewError[];
88
- /**
89
- * 日志消息列表
90
- */
91
- logs: LogMessage[];
92
- /**
93
- * 操作列表
94
- */
95
- actions: Action[];
96
- };
97
- /**
98
- * 创建控制器的选项
99
- */
100
- type CreateControllerOptions = {
101
- /**
102
- * 获取 iframe 元素的函数
103
- * @returns HTMLIFrameElement 或 null
104
- */
105
- getIframe: () => HTMLIFrameElement | null;
106
- /**
107
- * 状态更新时的回调函数
108
- * @param state - 预览 iframe 的状态
109
- */
110
- onStateUpdate?: (state: PreviewIframeState) => void;
111
- };
112
- /**
113
- * 渲染选项
114
- */
115
- type RenderOptions = {
116
- /**
117
- * 自动生成的回调属性源代码
118
- */
119
- autogenCallbackPropsSource: string;
120
- /**
121
- * 属性赋值源代码
122
- */
123
- propsAssignmentSource: string;
124
- };
125
- /**
126
- * 创建预览 iframe 控制器
127
- * @param options - 创建控制器的选项
128
- * @returns 预览 iframe 控制器实例
129
- */
130
- declare function createController(options: CreateControllerOptions): PreviewIframeController;
131
- /**
132
- * 预览 iframe 控制器接口
133
- */
134
- type PreviewIframeController = {
135
- /**
136
- * 渲染预览
137
- * @param previewableId - 可预览项的 ID
138
- * @param options - 渲染选项
139
- */
140
- render(previewableId: string, options: RenderOptions): void;
141
- };
142
- /**
143
- * 预览事件类型
144
- */
145
- type PreviewEvent = Bootstrapping | Bootstrapped | ViteBeforeUpdate | ViteAfterUpdate | ViteInvalidate | ViteBeforeReload | BeforeRender | Rendered | Action | LogMessage | PreviewError | ToggleInspect;
146
- type ToggleInspect = {
147
- kind: "toggle-inspect";
148
- enabled: boolean;
149
- };
150
- /**
151
- * 正在初始化事件
152
- */
153
- type Bootstrapping = {
154
- kind: "bootstrapping";
155
- };
156
- /**
157
- * 初始化完成事件
158
- */
159
- type Bootstrapped = {
160
- kind: "bootstrapped";
161
- };
162
- /**
163
- * Vite 预更新事件
164
- */
165
- type ViteBeforeUpdate = {
166
- kind: "vite-before-update";
167
- payload: UpdatePayload;
168
- };
169
- /**
170
- * Vite 更新后事件
171
- */
172
- type ViteAfterUpdate = {
173
- kind: "vite-after-update";
174
- payload: UpdatePayload;
175
- };
176
- /**
177
- * Vite 失效事件
178
- */
179
- type ViteInvalidate = {
180
- kind: "vite-invalidate";
181
- };
182
- /**
183
- * Vite 预重载事件
184
- */
185
- type ViteBeforeReload = {
186
- kind: "vite-before-reload";
187
- };
188
- /**
189
- * 渲染前事件
190
- */
191
- type BeforeRender = {
192
- kind: "before-render";
193
- };
194
- /**
195
- * 渲染完成事件
196
- */
197
- type Rendered = {
198
- kind: "rendered";
199
- };
200
- /**
201
- * 操作事件
202
- */
203
- type Action = {
204
- kind: "action";
205
- type: "fn" | "url";
206
- path: string;
207
- };
208
- /**
209
- * 日志消息事件
210
- */
211
- type LogMessage = {
212
- kind: "log-message";
213
- level: LogLevel;
214
- message: string;
215
- timestamp: number;
216
- };
217
- /**
218
- * 预览错误事件
219
- */
220
- type PreviewError = {
221
- kind: "error";
222
- source: "load";
223
- message: string;
224
- } | {
225
- kind: "error";
226
- source: "hmr";
227
- modulePath: string;
228
- message: string;
229
- } | {
230
- kind: "error";
231
- source: "vite";
232
- message: string;
233
- } | {
234
- kind: "error";
235
- source: "renderer";
236
- message: string;
237
- };
238
- /**
239
- * 日志级别
240
- */
241
- type LogLevel = "log" | "info" | "warn" | "error";
242
- /**
243
- * 渲染器加载器函数
244
- * @param options - 加载器选项
245
- * @returns 包含渲染函数和 JSX 工厂函数的对象
246
- */
247
- type RendererLoader = (options: {
248
- wrapperModule: any;
249
- wrapperName?: string;
250
- previewableModule: any;
251
- previewableName: string;
252
- renderId: number;
253
- shouldAbortRender: () => boolean;
254
- }) => Promise<{
255
- render: (getProps: GetPropsFn) => Promise<void>;
256
- jsxFactory: ((type: any, props: any, ...children: any[]) => any) | null;
257
- }>;
258
- /**
259
- * 获取属性函数
260
- * @param options - 获取属性的选项
261
- * @returns 属性对象
262
- */
263
- type GetPropsFn = (options: {
264
- presetGlobalProps: any;
265
- presetProps: any;
266
- }) => Record<string, any>;
267
-
268
- export { type Action, type BeforeRender, type Bootstrapped, type Bootstrapping, type CreateControllerOptions, type GetPropsFn, type LogLevel, type LogMessage, type PreviewError, type PreviewEvent, type PreviewIframeController, type PreviewIframeState, type RefreshOptions, type RenderOptions, type Rendered, type RendererLoader, type ToggleInspect, type ViteAfterUpdate, type ViteBeforeReload, type ViteBeforeUpdate, type ViteInvalidate, createController };