@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.
- package/README.md +37 -0
- package/dist/index.d.mts +168 -149
- package/dist/index.mjs +1 -1
- package/package.json +21 -9
- package/preview/__previewjs_internal__/index.ts +2 -3
- package/preview/__previewjs_internal__/links.ts +0 -1
- package/preview/__previewjs_internal__/logs.ts +0 -4
- package/preview/__previewjs_internal__/overlay.ts +187 -0
- package/preview/__previewjs_internal__/run-renderer.ts +4 -3
- package/preview/__previewjs_internal__/runtime-bridge.ts +559 -0
- package/preview/__previewjs_internal__/vscode-control.ts +75 -50
- package/preview/index.html +17 -5
- package/dist/index.d.ts +0 -268
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# @cfp-studio/iframe
|
|
2
|
+
|
|
3
|
+
CFP Studio 预览 iframe 通信与渲染核心包,负责 iframe 与主应用/设计器的消息通信、状态同步、渲染控制、HMR 支持等。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
- 提供 iframe 与父窗口的高效通信协议
|
|
7
|
+
- 支持渲染状态、事件、错误、刷新等多种消息类型
|
|
8
|
+
- 支持 HMR(热模块替换)与自动刷新
|
|
9
|
+
- 兼容多框架渲染器(通过 preview/renderer/)
|
|
10
|
+
- 支持全局状态、事件上报、调试等
|
|
11
|
+
|
|
12
|
+
## 架构与工作原理
|
|
13
|
+
- 通过全局 `__PREVIEWJS_IFRAME__` 对象暴露渲染、刷新、事件上报等能力
|
|
14
|
+
- 预览页面(preview/index.html)自动注入全局对象与通信桥
|
|
15
|
+
- 支持与主应用/设计器的双向事件流与状态同步
|
|
16
|
+
- 支持多渲染器、错误处理、调试工具等扩展
|
|
17
|
+
|
|
18
|
+
## 主要 API/模块
|
|
19
|
+
- `src/index.ts`:iframe 通信与状态主入口
|
|
20
|
+
- `preview/index.html`:预览页面模板,注入全局对象
|
|
21
|
+
- `preview/__previewjs_internal__/`:内部工具与渲染控制
|
|
22
|
+
- `preview/renderer/`:多框架渲染器入口
|
|
23
|
+
|
|
24
|
+
## 用法示例
|
|
25
|
+
|
|
26
|
+
集成于 CFP Studio 设计器/主应用,无需单独调用。自定义渲染器可参考 preview/renderer/
|
|
27
|
+
|
|
28
|
+
## 依赖
|
|
29
|
+
- `assert-never`、`immer`、`typescript`、`vite`、`tsdown`
|
|
30
|
+
|
|
31
|
+
## 构建
|
|
32
|
+
```sh
|
|
33
|
+
pnpm build
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## 许可证
|
|
37
|
+
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -1,126 +1,145 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region ../../../../node_modules/.pnpm/vite@6.4.5_@types+node@24.19.2_jiti@2.6.1_less@4.2.2_lightningcss@1.30.2_sass@1.85.1_terser@5.40.0_tsx@4.19.3_yaml@2.7.0/node_modules/vite/types/hmrPayload.d.ts
|
|
2
|
+
interface UpdatePayload {
|
|
3
|
+
type: 'update';
|
|
4
|
+
updates: Update[];
|
|
5
|
+
}
|
|
6
|
+
interface Update {
|
|
7
|
+
type: 'js-update' | 'css-update';
|
|
8
|
+
path: string;
|
|
9
|
+
acceptedPath: string;
|
|
10
|
+
timestamp: number;
|
|
11
|
+
/** @internal */
|
|
12
|
+
explicitImportRequired?: boolean;
|
|
13
|
+
/** @internal */
|
|
14
|
+
isWithinCircularImport?: boolean;
|
|
15
|
+
/** @internal */
|
|
16
|
+
firstInvalidatedBy?: string;
|
|
17
|
+
/** @internal */
|
|
18
|
+
invalidates?: string[];
|
|
19
|
+
}
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/index.d.ts
|
|
3
22
|
/**
|
|
4
23
|
* 扩展全局 Window 对象,定义在 iframe 和其父窗口中使用的特定属性
|
|
5
24
|
*/
|
|
6
25
|
declare global {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
26
|
+
interface Window {
|
|
27
|
+
/**
|
|
28
|
+
* iframe 中暴露的对象,用于管理渲染状态和报告事件
|
|
29
|
+
*/
|
|
30
|
+
__PREVIEWJS_IFRAME__: {
|
|
31
|
+
/**
|
|
32
|
+
* 表示最后一次渲染是否失败
|
|
33
|
+
*/
|
|
34
|
+
lastRenderFailed: boolean;
|
|
35
|
+
toggleInspect: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* 处理 HMR 更新
|
|
38
|
+
* @param payload - HMR 更新的负载
|
|
39
|
+
*/
|
|
40
|
+
/**
|
|
41
|
+
* 报告预览事件
|
|
42
|
+
* @param event - 预览事件对象
|
|
43
|
+
*/
|
|
44
|
+
reportEvent(event: PreviewEvent): void;
|
|
45
|
+
/**
|
|
46
|
+
* 刷新预览,可选参数指定刷新选项
|
|
47
|
+
* @param options - 刷新选项
|
|
48
|
+
*/
|
|
49
|
+
refresh(options?: RefreshOptions): void;
|
|
50
|
+
/**
|
|
51
|
+
* 渲染预览,可选异步函数,接收渲染选项
|
|
52
|
+
* @param options - 渲染选项
|
|
53
|
+
* @returns 渲染完成的 Promise
|
|
54
|
+
*/
|
|
55
|
+
render?(options: RenderOptions): Promise<void>;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* 通常在 iframe 的父窗口中暴露,用于跟踪 iframe 的状态
|
|
59
|
+
*/
|
|
60
|
+
__PREVIEWJS_CONTROLLER__: {
|
|
61
|
+
/**
|
|
62
|
+
* 处理预览事件
|
|
63
|
+
* @param event - 预览事件对象
|
|
64
|
+
*/
|
|
65
|
+
onPreviewEvent(event: PreviewEvent): void;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
49
68
|
}
|
|
50
69
|
/**
|
|
51
70
|
* 刷新预览的选项
|
|
52
71
|
*/
|
|
53
72
|
type RefreshOptions = {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
73
|
+
/**
|
|
74
|
+
* 是否重新获取可预览模块
|
|
75
|
+
*/
|
|
76
|
+
refetchPreviewableModule?: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* 是否重新获取包装模块
|
|
79
|
+
* added by Anthony on 2025-04-08
|
|
80
|
+
*/
|
|
81
|
+
refetchWrapperModule?: boolean;
|
|
82
|
+
/**
|
|
83
|
+
* 可预览模块
|
|
84
|
+
*/
|
|
85
|
+
previewableModule?: any;
|
|
86
|
+
/**
|
|
87
|
+
* 包装模块
|
|
88
|
+
*/
|
|
89
|
+
wrapperModule?: any;
|
|
71
90
|
};
|
|
72
91
|
/**
|
|
73
92
|
* 预览 iframe 的状态
|
|
74
93
|
*/
|
|
75
94
|
type PreviewIframeState = {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
95
|
+
/**
|
|
96
|
+
* 表示是否正在加载
|
|
97
|
+
*/
|
|
98
|
+
loading: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* 表示是否已经渲染
|
|
101
|
+
*/
|
|
102
|
+
rendered: boolean;
|
|
103
|
+
/**
|
|
104
|
+
* 预览错误列表
|
|
105
|
+
*/
|
|
106
|
+
errors: PreviewError[];
|
|
107
|
+
/**
|
|
108
|
+
* 日志消息列表
|
|
109
|
+
*/
|
|
110
|
+
logs: LogMessage[];
|
|
111
|
+
/**
|
|
112
|
+
* 操作列表
|
|
113
|
+
*/
|
|
114
|
+
actions: Action[];
|
|
96
115
|
};
|
|
97
116
|
/**
|
|
98
117
|
* 创建控制器的选项
|
|
99
118
|
*/
|
|
100
119
|
type CreateControllerOptions = {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
120
|
+
/**
|
|
121
|
+
* 获取 iframe 元素的函数
|
|
122
|
+
* @returns HTMLIFrameElement 或 null
|
|
123
|
+
*/
|
|
124
|
+
getIframe: () => HTMLIFrameElement | null;
|
|
125
|
+
/**
|
|
126
|
+
* 状态更新时的回调函数
|
|
127
|
+
* @param state - 预览 iframe 的状态
|
|
128
|
+
*/
|
|
129
|
+
onStateUpdate?: (state: PreviewIframeState) => void;
|
|
111
130
|
};
|
|
112
131
|
/**
|
|
113
132
|
* 渲染选项
|
|
114
133
|
*/
|
|
115
134
|
type RenderOptions = {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
135
|
+
/**
|
|
136
|
+
* 自动生成的回调属性源代码
|
|
137
|
+
*/
|
|
138
|
+
autogenCallbackPropsSource: string;
|
|
139
|
+
/**
|
|
140
|
+
* 属性赋值源代码
|
|
141
|
+
*/
|
|
142
|
+
propsAssignmentSource: string;
|
|
124
143
|
};
|
|
125
144
|
/**
|
|
126
145
|
* 创建预览 iframe 控制器
|
|
@@ -132,108 +151,108 @@ declare function createController(options: CreateControllerOptions): PreviewIfra
|
|
|
132
151
|
* 预览 iframe 控制器接口
|
|
133
152
|
*/
|
|
134
153
|
type PreviewIframeController = {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
154
|
+
/**
|
|
155
|
+
* 渲染预览
|
|
156
|
+
* @param previewableId - 可预览项的 ID
|
|
157
|
+
* @param options - 渲染选项
|
|
158
|
+
*/
|
|
159
|
+
render(previewableId: string, options: RenderOptions): void;
|
|
141
160
|
};
|
|
142
161
|
/**
|
|
143
162
|
* 预览事件类型
|
|
144
163
|
*/
|
|
145
164
|
type PreviewEvent = Bootstrapping | Bootstrapped | ViteBeforeUpdate | ViteAfterUpdate | ViteInvalidate | ViteBeforeReload | BeforeRender | Rendered | Action | LogMessage | PreviewError | ToggleInspect;
|
|
146
165
|
type ToggleInspect = {
|
|
147
|
-
|
|
148
|
-
|
|
166
|
+
kind: "toggle-inspect";
|
|
167
|
+
enabled: boolean;
|
|
149
168
|
};
|
|
150
169
|
/**
|
|
151
170
|
* 正在初始化事件
|
|
152
171
|
*/
|
|
153
172
|
type Bootstrapping = {
|
|
154
|
-
|
|
173
|
+
kind: "bootstrapping";
|
|
155
174
|
};
|
|
156
175
|
/**
|
|
157
176
|
* 初始化完成事件
|
|
158
177
|
*/
|
|
159
178
|
type Bootstrapped = {
|
|
160
|
-
|
|
179
|
+
kind: "bootstrapped";
|
|
161
180
|
};
|
|
162
181
|
/**
|
|
163
182
|
* Vite 预更新事件
|
|
164
183
|
*/
|
|
165
184
|
type ViteBeforeUpdate = {
|
|
166
|
-
|
|
167
|
-
|
|
185
|
+
kind: "vite-before-update";
|
|
186
|
+
payload: UpdatePayload;
|
|
168
187
|
};
|
|
169
188
|
/**
|
|
170
189
|
* Vite 更新后事件
|
|
171
190
|
*/
|
|
172
191
|
type ViteAfterUpdate = {
|
|
173
|
-
|
|
174
|
-
|
|
192
|
+
kind: "vite-after-update";
|
|
193
|
+
payload: UpdatePayload;
|
|
175
194
|
};
|
|
176
195
|
/**
|
|
177
196
|
* Vite 失效事件
|
|
178
197
|
*/
|
|
179
198
|
type ViteInvalidate = {
|
|
180
|
-
|
|
199
|
+
kind: "vite-invalidate";
|
|
181
200
|
};
|
|
182
201
|
/**
|
|
183
202
|
* Vite 预重载事件
|
|
184
203
|
*/
|
|
185
204
|
type ViteBeforeReload = {
|
|
186
|
-
|
|
205
|
+
kind: "vite-before-reload";
|
|
187
206
|
};
|
|
188
207
|
/**
|
|
189
208
|
* 渲染前事件
|
|
190
209
|
*/
|
|
191
210
|
type BeforeRender = {
|
|
192
|
-
|
|
211
|
+
kind: "before-render";
|
|
193
212
|
};
|
|
194
213
|
/**
|
|
195
214
|
* 渲染完成事件
|
|
196
215
|
*/
|
|
197
216
|
type Rendered = {
|
|
198
|
-
|
|
217
|
+
kind: "rendered";
|
|
199
218
|
};
|
|
200
219
|
/**
|
|
201
220
|
* 操作事件
|
|
202
221
|
*/
|
|
203
222
|
type Action = {
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
223
|
+
kind: "action";
|
|
224
|
+
type: "fn" | "url";
|
|
225
|
+
path: string;
|
|
207
226
|
};
|
|
208
227
|
/**
|
|
209
228
|
* 日志消息事件
|
|
210
229
|
*/
|
|
211
230
|
type LogMessage = {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
231
|
+
kind: "log-message";
|
|
232
|
+
level: LogLevel;
|
|
233
|
+
message: string;
|
|
234
|
+
timestamp: number;
|
|
216
235
|
};
|
|
217
236
|
/**
|
|
218
237
|
* 预览错误事件
|
|
219
238
|
*/
|
|
220
239
|
type PreviewError = {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
240
|
+
kind: "error";
|
|
241
|
+
source: "load";
|
|
242
|
+
message: string;
|
|
224
243
|
} | {
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
244
|
+
kind: "error";
|
|
245
|
+
source: "hmr";
|
|
246
|
+
modulePath: string;
|
|
247
|
+
message: string;
|
|
229
248
|
} | {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
249
|
+
kind: "error";
|
|
250
|
+
source: "vite";
|
|
251
|
+
message: string;
|
|
233
252
|
} | {
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
253
|
+
kind: "error";
|
|
254
|
+
source: "renderer";
|
|
255
|
+
message: string;
|
|
237
256
|
};
|
|
238
257
|
/**
|
|
239
258
|
* 日志级别
|
|
@@ -245,15 +264,15 @@ type LogLevel = "log" | "info" | "warn" | "error";
|
|
|
245
264
|
* @returns 包含渲染函数和 JSX 工厂函数的对象
|
|
246
265
|
*/
|
|
247
266
|
type RendererLoader = (options: {
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
267
|
+
wrapperModule: any;
|
|
268
|
+
wrapperName?: string;
|
|
269
|
+
previewableModule: any;
|
|
270
|
+
previewableName: string;
|
|
271
|
+
renderId: number;
|
|
272
|
+
shouldAbortRender: () => boolean;
|
|
254
273
|
}) => Promise<{
|
|
255
|
-
|
|
256
|
-
|
|
274
|
+
render: (getProps: GetPropsFn) => Promise<void>;
|
|
275
|
+
jsxFactory: ((type: any, props: any, ...children: any[]) => any) | null;
|
|
257
276
|
}>;
|
|
258
277
|
/**
|
|
259
278
|
* 获取属性函数
|
|
@@ -261,8 +280,8 @@ type RendererLoader = (options: {
|
|
|
261
280
|
* @returns 属性对象
|
|
262
281
|
*/
|
|
263
282
|
type GetPropsFn = (options: {
|
|
264
|
-
|
|
265
|
-
|
|
283
|
+
presetGlobalProps: any;
|
|
284
|
+
presetProps: any;
|
|
266
285
|
}) => Record<string, any>;
|
|
267
|
-
|
|
268
|
-
export {
|
|
286
|
+
//#endregion
|
|
287
|
+
export { Action, BeforeRender, Bootstrapped, Bootstrapping, CreateControllerOptions, GetPropsFn, LogLevel, LogMessage, PreviewError, PreviewEvent, PreviewIframeController, PreviewIframeState, RefreshOptions, RenderOptions, Rendered, RendererLoader, ToggleInspect, ViteAfterUpdate, ViteBeforeReload, ViteBeforeUpdate, ViteInvalidate, createController };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var z={};Object.defineProperty(z,"__esModule",{value:!0});var Y=z.assertNever=U;function U(e,t){if(typeof t=="string")throw new Error(t);if(typeof t=="function")throw new Error(t(e));if(t)return e;throw new Error("Unhandled discriminated union member: ".concat(JSON.stringify(e)))}z.default=U;var V=Symbol.for("immer-nothing"),B=Symbol.for("immer-draftable"),f=Symbol.for("immer-state"),Z=process.env.NODE_ENV!=="production"?[function(e){return`The plugin for '${e}' has not been loaded into Immer. To enable the plugin, import and call \`enable${e}()\` when initializing your application.`},function(e){return`produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${e}'`},"This object has been frozen and should not be mutated",function(e){return"Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? "+e},"An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.","Immer forbids circular references","The first or second argument to `produce` must be a function","The third argument to `produce` must be a function or undefined","First argument to `createDraft` must be a plain object, an array, or an immerable object","First argument to `finishDraft` must be a draft returned by `createDraft`",function(e){return`'current' expects a draft, got: ${e}`},"Object.defineProperty() cannot be used on an Immer draft","Object.setPrototypeOf() cannot be used on an Immer draft","Immer only supports deleting array indices","Immer only supports setting array indices and the 'length' property",function(e){return`'original' expects a draft, got: ${e}`}]:[];function c(e,...t){if(process.env.NODE_ENV!=="production"){const r=Z[e],n=typeof r=="function"?r.apply(null,t):r;throw new Error(`[Immer] ${n}`)}throw new Error(`[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`)}var _=Object.getPrototypeOf;function y(e){return!!e&&!!e[f]}function l(e){return e?M(e)||Array.isArray(e)||!!e[B]||!!e.constructor?.[B]||w(e)||S(e):!1}var ee=Object.prototype.constructor.toString();function M(e){if(!e||typeof e!="object")return!1;const t=_(e);if(t===null)return!0;const r=Object.hasOwnProperty.call(t,"constructor")&&t.constructor;return r===Object?!0:typeof r=="function"&&Function.toString.call(r)===ee}function E(e,t){P(e)===0?Reflect.ownKeys(e).forEach(r=>{t(r,e[r],e)}):e.forEach((r,n)=>t(n,r,e))}function P(e){const t=e[f];return t?t.type_:Array.isArray(e)?1:w(e)?2:S(e)?3:0}function F(e,t){return P(e)===2?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function W(e,t,r){const n=P(e);n===2?e.set(t,r):n===3?e.add(r):e[t]=r}function te(e,t){return e===t?e!==0||1/e===1/t:e!==e&&t!==t}function w(e){return e instanceof Map}function S(e){return e instanceof Set}function h(e){return e.copy_||e.base_}function D(e,t){if(w(e))return new Map(e);if(S(e))return new Set(e);if(Array.isArray(e))return Array.prototype.slice.call(e);const r=M(e);if(t===!0||t==="class_only"&&!r){const n=Object.getOwnPropertyDescriptors(e);delete n[f];let i=Reflect.ownKeys(n);for(let o=0;o<i.length;o++){const s=i[o],a=n[s];a.writable===!1&&(a.writable=!0,a.configurable=!0),(a.get||a.set)&&(n[s]={configurable:!0,writable:!0,enumerable:a.enumerable,value:e[s]})}return Object.create(_(e),n)}else{const n=_(e);if(n!==null&&r)return{...e};const i=Object.create(n);return Object.assign(i,e)}}function O(e,t=!1){return v(e)||y(e)||!l(e)||(P(e)>1&&(e.set=e.add=e.clear=e.delete=re),Object.freeze(e),t&&Object.entries(e).forEach(([r,n])=>O(n,!0))),e}function re(){c(2)}function v(e){return Object.isFrozen(e)}var ne={};function p(e){const t=ne[e];return t||c(0,e),t}var g;function j(){return g}function ie(e,t){return{drafts_:[],parent_:e,immer_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}function $(e,t){t&&(p("Patches"),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function k(e){A(e),e.drafts_.forEach(oe),e.drafts_=null}function A(e){e===g&&(g=e.parent_)}function J(e){return g=ie(g,e)}function oe(e){const t=e[f];t.type_===0||t.type_===1?t.revoke_():t.revoked_=!0}function K(e,t){t.unfinalizedDrafts_=t.drafts_.length;const r=t.drafts_[0];return e!==void 0&&e!==r?(r[f].modified_&&(k(t),c(4)),l(e)&&(e=R(t,e),t.parent_||I(t,e)),t.patches_&&p("Patches").generateReplacementPatches_(r[f].base_,e,t.patches_,t.inversePatches_)):e=R(t,r,[]),k(t),t.patches_&&t.patchListener_(t.patches_,t.inversePatches_),e!==V?e:void 0}function R(e,t,r){if(v(t))return t;const n=t[f];if(!n)return E(t,(i,o)=>G(e,n,t,i,o,r)),t;if(n.scope_!==e)return t;if(!n.modified_)return I(e,n.base_,!0),n.base_;if(!n.finalized_){n.finalized_=!0,n.scope_.unfinalizedDrafts_--;const i=n.copy_;let o=i,s=!1;n.type_===3&&(o=new Set(i),i.clear(),s=!0),E(o,(a,d)=>G(e,n,i,a,d,r,s)),I(e,i,!1),r&&e.patches_&&p("Patches").generatePatches_(n,r,e.patches_,e.inversePatches_)}return n.copy_}function G(e,t,r,n,i,o,s){if(process.env.NODE_ENV!=="production"&&i===r&&c(5),y(i)){const a=o&&t&&t.type_!==3&&!F(t.assigned_,n)?o.concat(n):void 0,d=R(e,i,a);if(W(r,n,d),y(d))e.canAutoFreeze_=!1;else return}else s&&r.add(i);if(l(i)&&!v(i)){if(!e.immer_.autoFreeze_&&e.unfinalizedDrafts_<1)return;R(e,i),(!t||!t.scope_.parent_)&&typeof n!="symbol"&&Object.prototype.propertyIsEnumerable.call(r,n)&&I(e,i)}}function I(e,t,r=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&O(t,r)}function se(e,t){const r=Array.isArray(e),n={type_:r?1:0,scope_:t?t.scope_:j(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1};let i=n,o=N;r&&(i=[n],o=b);const{revoke:s,proxy:a}=Proxy.revocable(i,o);return n.draft_=a,n.revoke_=s,a}var N={get(e,t){if(t===f)return e;const r=h(e);if(!F(r,t))return ae(e,r,t);const n=r[t];return e.finalized_||!l(n)?n:n===L(e.base_,t)?(x(e),e.copy_[t]=C(n,e)):n},has(e,t){return t in h(e)},ownKeys(e){return Reflect.ownKeys(h(e))},set(e,t,r){const n=H(h(e),t);if(n?.set)return n.set.call(e.draft_,r),!0;if(!e.modified_){const i=L(h(e),t),o=i?.[f];if(o&&o.base_===r)return e.copy_[t]=r,e.assigned_[t]=!1,!0;if(te(r,i)&&(r!==void 0||F(e.base_,t)))return!0;x(e),T(e)}return e.copy_[t]===r&&(r!==void 0||t in e.copy_)||Number.isNaN(r)&&Number.isNaN(e.copy_[t])||(e.copy_[t]=r,e.assigned_[t]=!0),!0},deleteProperty(e,t){return L(e.base_,t)!==void 0||t in e.base_?(e.assigned_[t]=!1,x(e),T(e)):delete e.assigned_[t],e.copy_&&delete e.copy_[t],!0},getOwnPropertyDescriptor(e,t){const r=h(e),n=Reflect.getOwnPropertyDescriptor(r,t);return n&&{writable:!0,configurable:e.type_!==1||t!=="length",enumerable:n.enumerable,value:r[t]}},defineProperty(){c(11)},getPrototypeOf(e){return _(e.base_)},setPrototypeOf(){c(12)}},b={};E(N,(e,t)=>{b[e]=function(){return arguments[0]=arguments[0][0],t.apply(this,arguments)}}),b.deleteProperty=function(e,t){return process.env.NODE_ENV!=="production"&&isNaN(parseInt(t))&&c(13),b.set.call(this,e,t,void 0)},b.set=function(e,t,r){return process.env.NODE_ENV!=="production"&&t!=="length"&&isNaN(parseInt(t))&&c(14),N.set.call(this,e[0],t,r,e[0])};function L(e,t){const r=e[f];return(r?h(r):e)[t]}function ae(e,t,r){const n=H(t,r);return n?"value"in n?n.value:n.get?.call(e.draft_):void 0}function H(e,t){if(!(t in e))return;let r=_(e);for(;r;){const n=Object.getOwnPropertyDescriptor(r,t);if(n)return n;r=_(r)}}function T(e){e.modified_||(e.modified_=!0,e.parent_&&T(e.parent_))}function x(e){e.copy_||(e.copy_=D(e.base_,e.scope_.immer_.useStrictShallowCopy_))}var ce=class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.produce=(t,r,n)=>{if(typeof t=="function"&&typeof r!="function"){const o=r;r=t;const s=this;return function(d=o,...q){return s.produce(d,Q=>r.call(this,Q,...q))}}typeof r!="function"&&c(6),n!==void 0&&typeof n!="function"&&c(7);let i;if(l(t)){const o=J(this),s=C(t,void 0);let a=!0;try{i=r(s),a=!1}finally{a?k(o):A(o)}return $(o,n),K(i,o)}else if(!t||typeof t!="object"){if(i=r(t),i===void 0&&(i=t),i===V&&(i=void 0),this.autoFreeze_&&O(i,!0),n){const o=[],s=[];p("Patches").generateReplacementPatches_(t,i,o,s),n(o,s)}return i}else c(1,t)},this.produceWithPatches=(t,r)=>{if(typeof t=="function")return(s,...a)=>this.produceWithPatches(s,d=>t(d,...a));let n,i;return[this.produce(t,r,(s,a)=>{n=s,i=a}),n,i]},typeof e?.autoFreeze=="boolean"&&this.setAutoFreeze(e.autoFreeze),typeof e?.useStrictShallowCopy=="boolean"&&this.setUseStrictShallowCopy(e.useStrictShallowCopy)}createDraft(e){l(e)||c(8),y(e)&&(e=fe(e));const t=J(this),r=C(e,void 0);return r[f].isManual_=!0,A(t),r}finishDraft(e,t){const r=e&&e[f];(!r||!r.isManual_)&&c(9);const{scope_:n}=r;return $(n,t),K(void 0,n)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}applyPatches(e,t){let r;for(r=t.length-1;r>=0;r--){const i=t[r];if(i.path.length===0&&i.op==="replace"){e=i.value;break}}r>-1&&(t=t.slice(r+1));const n=p("Patches").applyPatches_;return y(e)?n(e,t):this.produce(e,i=>n(i,t))}};function C(e,t){const r=w(e)?p("MapSet").proxyMap_(e,t):S(e)?p("MapSet").proxySet_(e,t):se(e,t);return(t?t.scope_:j()).drafts_.push(r),r}function fe(e){return y(e)||c(10,e),X(e)}function X(e){if(!l(e)||v(e))return e;const t=e[f];let r;if(t){if(!t.modified_)return t.base_;t.finalized_=!0,r=D(e,t.scope_.immer_.useStrictShallowCopy_)}else r=D(e,!0);return E(r,(n,i)=>{W(r,n,X(i))}),t&&(t.finalized_=!1),r}var u=new ce,ue=u.produce;u.produceWithPatches.bind(u),u.setAutoFreeze.bind(u),u.setUseStrictShallowCopy.bind(u),u.applyPatches.bind(u),u.createDraft.bind(u),u.finishDraft.bind(u);var de=Object.defineProperty,le=(e,t,r)=>t in e?de(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,m=(e,t,r)=>(le(e,typeof t!="symbol"?t+"":t,r),r);function he(e){const t=new pe(e);return window.__PREVIEWJS_CONTROLLER__=t,t}class pe{constructor(t){this.options=t,m(this,"lastRender",null),m(this,"bootstrapStatus","pending"),m(this,"expectRenderTimeout"),m(this,"state",{loading:!1,rendered:!1,errors:[],logs:[],actions:[]}),m(this,"onViteBeforeUpdateLogsLength",0),m(this,"onViteBeforeUpdateErrorsLength",0)}async render(t,r){const n=this.lastRender;if(this.lastRender={previewableId:t,options:r},n?.previewableId!==t||this.bootstrapStatus==="failure"){this.resetIframe(t);return}const i=this.options.getIframe()?.contentWindow;if(!i||this.bootstrapStatus!=="success")return;if(!i.__PREVIEWJS_IFRAME__.render){i.__PREVIEWJS_IFRAME__.reportEvent({kind:"error",source:"renderer",message:"Iframe was bootstrapped but not fully initialised."});return}const o=i.__PREVIEWJS_IFRAME__.render(r);this.clearExpectRenderTimeout(),this.expectRenderTimeout=setTimeout(()=>{console.warn("Expected render did not occur after 5 seconds. Reloading iframe..."),this.resetIframe(t)},5e3),await o}resetIframe(t){const r=this.options.getIframe();r&&(r.src=`/${t}/?t=${Date.now()}`,this.bootstrapStatus="not-started")}onPreviewEvent(t){if(!(this.bootstrapStatus==="not-started"&&t.kind!=="bootstrapping"))switch(t.kind){case"bootstrapping":this.bootstrapStatus="pending";break;case"bootstrapped":this.bootstrapStatus="success",this.onViteBeforeUpdateLogsLength=0,this.onViteBeforeUpdateErrorsLength=0,this.updateState(r=>{r.loading=!0,r.rendered=!1,r.errors=[],r.logs=[]}),this.lastRender&&this.render(this.lastRender.previewableId,this.lastRender.options).catch(console.error);break;case"vite-before-reload":this.updateState(r=>{r.logs=[],r.errors=[]}),this.lastRender&&this.resetIframe(this.lastRender.previewableId);break;case"vite-before-update":this.onViteBeforeUpdateLogsLength=this.state.logs.length,this.updateState(r=>{for(const n of t.payload.updates)r.errors=r.errors.filter(i=>i.source!=="hmr"||i.modulePath!==n.acceptedPath)}),this.onViteBeforeUpdateErrorsLength=this.state.errors.length;break;case"vite-invalidate":this.updateState(r=>{this.onViteBeforeUpdateLogsLength=0,this.onViteBeforeUpdateErrorsLength=0,r.logs=[]});break;case"vite-after-update":this.updateState(r=>{const n=this.onViteBeforeUpdateLogsLength,i=this.onViteBeforeUpdateErrorsLength;this.onViteBeforeUpdateLogsLength=0,this.onViteBeforeUpdateErrorsLength=0,r.logs=r.logs.slice(n),r.errors=r.errors.slice(i)});break;case"before-render":this.updateState(r=>{this.onViteBeforeUpdateLogsLength=0,r.logs=[]});break;case"rendered":{this.updateState(r=>{r.loading=!1,r.rendered=!0}),this.clearExpectRenderTimeout();break}case"error":this.updateState(r=>{r.loading=!1,!(t.source==="vite"&&t.message.includes("Failed to fetch dynamically imported module")&&r.errors.find(n=>n.source==="vite"))&&(r.errors=r.errors.filter(n=>n.source!==t.source),r.errors.push(t))}),this.clearExpectRenderTimeout(),this.bootstrapStatus==="pending"&&(this.bootstrapStatus="failure");break;case"log-message":this.updateState(r=>{r.logs.push(t)});break;case"action":this.updateState(r=>{r.actions.push(t)});break;case"toggle-inspect":{console.log("\u89E6\u53D1toggle-inspect",t.enabled);const r=this.options.getIframe()?.contentWindow;r&&(r.__PREVIEWJS_IFRAME__.toggleInspect=t.enabled,r.__PREVIEWJS_IFRAME__.refresh({refetchWrapperModule:!0}));break}default:throw Y(t)}}updateState(t){this.state=ue(this.state,t),this.options.onStateUpdate?.(this.state)}clearExpectRenderTimeout(){this.expectRenderTimeout&&(clearTimeout(this.expectRenderTimeout),this.expectRenderTimeout=null)}}export{he as createController};
|
|
1
|
+
var e=((e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports))((e=>{Object.defineProperty(e,`__esModule`,{value:!0}),e.assertNever=t;function t(e,t){if(typeof t==`string`)throw Error(t);if(typeof t==`function`)throw Error(t(e));if(t)return e;throw Error(`Unhandled discriminated union member: ${JSON.stringify(e)}`)}}))(),t=Symbol.for(`immer-nothing`),n=Symbol.for(`immer-draftable`),r=Symbol.for(`immer-state`),i=process.env.NODE_ENV===`production`?[]:[function(e){return`The plugin for '${e}' has not been loaded into Immer. To enable the plugin, import and call \`enable${e}()\` when initializing your application.`},function(e){return`produce can only be called on things that are draftable: plain objects, arrays, Map, Set or classes that are marked with '[immerable]: true'. Got '${e}'`},`This object has been frozen and should not be mutated`,function(e){return`Cannot use a proxy that has been revoked. Did you pass an object from inside an immer function to an async process? `+e},`An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.`,`Immer forbids circular references`,"The first or second argument to `produce` must be a function","The third argument to `produce` must be a function or undefined","First argument to `createDraft` must be a plain object, an array, or an immerable object","First argument to `finishDraft` must be a draft returned by `createDraft`",function(e){return`'current' expects a draft, got: ${e}`},`Object.defineProperty() cannot be used on an Immer draft`,`Object.setPrototypeOf() cannot be used on an Immer draft`,`Immer only supports deleting array indices`,`Immer only supports setting array indices and the 'length' property`,function(e){return`'original' expects a draft, got: ${e}`}];function a(e,...t){if(process.env.NODE_ENV!==`production`){let n=i[e],r=typeof n==`function`?n.apply(null,t):n;throw Error(`[Immer] ${r}`)}throw Error(`[Immer] minified error nr: ${e}. Full error at: https://bit.ly/3cXEKWf`)}var o=Object.getPrototypeOf;function s(e){return!!e&&!!e[r]}function c(e){return e?u(e)||Array.isArray(e)||!!e[n]||!!e.constructor?.[n]||g(e)||_(e):!1}var l=Object.prototype.constructor.toString();function u(e){if(!e||typeof e!=`object`)return!1;let t=o(e);if(t===null)return!0;let n=Object.hasOwnProperty.call(t,`constructor`)&&t.constructor;return n===Object?!0:typeof n==`function`&&Function.toString.call(n)===l}function d(e,t){f(e)===0?Reflect.ownKeys(e).forEach(n=>{t(n,e[n],e)}):e.forEach((n,r)=>t(r,n,e))}function f(e){let t=e[r];return t?t.type_:Array.isArray(e)?1:g(e)?2:_(e)?3:0}function p(e,t){return f(e)===2?e.has(t):Object.prototype.hasOwnProperty.call(e,t)}function m(e,t,n){let r=f(e);r===2?e.set(t,n):r===3?e.add(n):e[t]=n}function h(e,t){return e===t?e!==0||1/e==1/t:e!==e&&t!==t}function g(e){return e instanceof Map}function _(e){return e instanceof Set}function v(e){return e.copy_||e.base_}function y(e,t){if(g(e))return new Map(e);if(_(e))return new Set(e);if(Array.isArray(e))return Array.prototype.slice.call(e);let n=u(e);if(t===!0||t===`class_only`&&!n){let t=Object.getOwnPropertyDescriptors(e);delete t[r];let n=Reflect.ownKeys(t);for(let r=0;r<n.length;r++){let i=n[r],a=t[i];a.writable===!1&&(a.writable=!0,a.configurable=!0),(a.get||a.set)&&(t[i]={configurable:!0,writable:!0,enumerable:a.enumerable,value:e[i]})}return Object.create(o(e),t)}else{let t=o(e);if(t!==null&&n)return{...e};let r=Object.create(t);return Object.assign(r,e)}}function b(e,t=!1){return S(e)||s(e)||!c(e)?e:(f(e)>1&&(e.set=e.add=e.clear=e.delete=x),Object.freeze(e),t&&Object.entries(e).forEach(([e,t])=>b(t,!0)),e)}function x(){a(2)}function S(e){return Object.isFrozen(e)}var C={};function w(e){let t=C[e];return t||a(0,e),t}var T;function E(){return T}function D(e,t){return{drafts_:[],parent_:e,immer_:t,canAutoFreeze_:!0,unfinalizedDrafts_:0}}function O(e,t){t&&(w(`Patches`),e.patches_=[],e.inversePatches_=[],e.patchListener_=t)}function k(e){A(e),e.drafts_.forEach(M),e.drafts_=null}function A(e){e===T&&(T=e.parent_)}function j(e){return T=D(T,e)}function M(e){let t=e[r];t.type_===0||t.type_===1?t.revoke_():t.revoked_=!0}function N(e,n){n.unfinalizedDrafts_=n.drafts_.length;let i=n.drafts_[0];return e!==void 0&&e!==i?(i[r].modified_&&(k(n),a(4)),c(e)&&(e=P(n,e),n.parent_||I(n,e)),n.patches_&&w(`Patches`).generateReplacementPatches_(i[r].base_,e,n.patches_,n.inversePatches_)):e=P(n,i,[]),k(n),n.patches_&&n.patchListener_(n.patches_,n.inversePatches_),e===t?void 0:e}function P(e,t,n){if(S(t))return t;let i=t[r];if(!i)return d(t,(r,a)=>F(e,i,t,r,a,n)),t;if(i.scope_!==e)return t;if(!i.modified_)return I(e,i.base_,!0),i.base_;if(!i.finalized_){i.finalized_=!0,i.scope_.unfinalizedDrafts_--;let t=i.copy_,r=t,a=!1;i.type_===3&&(r=new Set(t),t.clear(),a=!0),d(r,(r,o)=>F(e,i,t,r,o,n,a)),I(e,t,!1),n&&e.patches_&&w(`Patches`).generatePatches_(i,n,e.patches_,e.inversePatches_)}return i.copy_}function F(e,t,n,r,i,o,l){if(process.env.NODE_ENV!==`production`&&i===n&&a(5),s(i)){let a=P(e,i,o&&t&&t.type_!==3&&!p(t.assigned_,r)?o.concat(r):void 0);if(m(n,r,a),s(a))e.canAutoFreeze_=!1;else return}else l&&n.add(i);if(c(i)&&!S(i)){if(!e.immer_.autoFreeze_&&e.unfinalizedDrafts_<1)return;P(e,i),(!t||!t.scope_.parent_)&&typeof r!=`symbol`&&Object.prototype.propertyIsEnumerable.call(n,r)&&I(e,i)}}function I(e,t,n=!1){!e.parent_&&e.immer_.autoFreeze_&&e.canAutoFreeze_&&b(t,n)}function L(e,t){let n=Array.isArray(e),r={type_:n?1:0,scope_:t?t.scope_:E(),modified_:!1,finalized_:!1,assigned_:{},parent_:t,base_:e,draft_:null,copy_:null,revoke_:null,isManual_:!1},i=r,a=R;n&&(i=[r],a=z);let{revoke:o,proxy:s}=Proxy.revocable(i,a);return r.draft_=s,r.revoke_=o,s}var R={get(e,t){if(t===r)return e;let n=v(e);if(!p(n,t))return V(e,n,t);let i=n[t];return e.finalized_||!c(i)?i:i===B(e.base_,t)?(W(e),e.copy_[t]=K(i,e)):i},has(e,t){return t in v(e)},ownKeys(e){return Reflect.ownKeys(v(e))},set(e,t,n){let i=H(v(e),t);if(i?.set)return i.set.call(e.draft_,n),!0;if(!e.modified_){let i=B(v(e),t),a=i?.[r];if(a&&a.base_===n)return e.copy_[t]=n,e.assigned_[t]=!1,!0;if(h(n,i)&&(n!==void 0||p(e.base_,t)))return!0;W(e),U(e)}return e.copy_[t]===n&&(n!==void 0||t in e.copy_)||Number.isNaN(n)&&Number.isNaN(e.copy_[t])?!0:(e.copy_[t]=n,e.assigned_[t]=!0,!0)},deleteProperty(e,t){return B(e.base_,t)!==void 0||t in e.base_?(e.assigned_[t]=!1,W(e),U(e)):delete e.assigned_[t],e.copy_&&delete e.copy_[t],!0},getOwnPropertyDescriptor(e,t){let n=v(e),r=Reflect.getOwnPropertyDescriptor(n,t);return r&&{writable:!0,configurable:e.type_!==1||t!==`length`,enumerable:r.enumerable,value:n[t]}},defineProperty(){a(11)},getPrototypeOf(e){return o(e.base_)},setPrototypeOf(){a(12)}},z={};d(R,(e,t)=>{z[e]=function(){return arguments[0]=arguments[0][0],t.apply(this,arguments)}}),z.deleteProperty=function(e,t){return process.env.NODE_ENV!==`production`&&isNaN(parseInt(t))&&a(13),z.set.call(this,e,t,void 0)},z.set=function(e,t,n){return process.env.NODE_ENV!==`production`&&t!==`length`&&isNaN(parseInt(t))&&a(14),R.set.call(this,e[0],t,n,e[0])};function B(e,t){let n=e[r];return(n?v(n):e)[t]}function V(e,t,n){let r=H(t,n);return r?`value`in r?r.value:r.get?.call(e.draft_):void 0}function H(e,t){if(!(t in e))return;let n=o(e);for(;n;){let e=Object.getOwnPropertyDescriptor(n,t);if(e)return e;n=o(n)}}function U(e){e.modified_||(e.modified_=!0,e.parent_&&U(e.parent_))}function W(e){e.copy_||=y(e.base_,e.scope_.immer_.useStrictShallowCopy_)}var G=class{constructor(e){this.autoFreeze_=!0,this.useStrictShallowCopy_=!1,this.produce=(e,n,r)=>{if(typeof e==`function`&&typeof n!=`function`){let t=n;n=e;let r=this;return function(e=t,...i){return r.produce(e,e=>n.call(this,e,...i))}}typeof n!=`function`&&a(6),r!==void 0&&typeof r!=`function`&&a(7);let i;if(c(e)){let t=j(this),a=K(e,void 0),o=!0;try{i=n(a),o=!1}finally{o?k(t):A(t)}return O(t,r),N(i,t)}else if(!e||typeof e!=`object`){if(i=n(e),i===void 0&&(i=e),i===t&&(i=void 0),this.autoFreeze_&&b(i,!0),r){let t=[],n=[];w(`Patches`).generateReplacementPatches_(e,i,t,n),r(t,n)}return i}else a(1,e)},this.produceWithPatches=(e,t)=>{if(typeof e==`function`)return(t,...n)=>this.produceWithPatches(t,t=>e(t,...n));let n,r;return[this.produce(e,t,(e,t)=>{n=e,r=t}),n,r]},typeof e?.autoFreeze==`boolean`&&this.setAutoFreeze(e.autoFreeze),typeof e?.useStrictShallowCopy==`boolean`&&this.setUseStrictShallowCopy(e.useStrictShallowCopy)}createDraft(e){c(e)||a(8),s(e)&&(e=q(e));let t=j(this),n=K(e,void 0);return n[r].isManual_=!0,A(t),n}finishDraft(e,t){let n=e&&e[r];(!n||!n.isManual_)&&a(9);let{scope_:i}=n;return O(i,t),N(void 0,i)}setAutoFreeze(e){this.autoFreeze_=e}setUseStrictShallowCopy(e){this.useStrictShallowCopy_=e}applyPatches(e,t){let n;for(n=t.length-1;n>=0;n--){let r=t[n];if(r.path.length===0&&r.op===`replace`){e=r.value;break}}n>-1&&(t=t.slice(n+1));let r=w(`Patches`).applyPatches_;return s(e)?r(e,t):this.produce(e,e=>r(e,t))}};function K(e,t){let n=g(e)?w(`MapSet`).proxyMap_(e,t):_(e)?w(`MapSet`).proxySet_(e,t):L(e,t);return(t?t.scope_:E()).drafts_.push(n),n}function q(e){return s(e)||a(10,e),J(e)}function J(e){if(!c(e)||S(e))return e;let t=e[r],n;if(t){if(!t.modified_)return t.base_;t.finalized_=!0,n=y(e,t.scope_.immer_.useStrictShallowCopy_)}else n=y(e,!0);return d(n,(e,t)=>{m(n,e,J(t))}),t&&(t.finalized_=!1),n}var Y=new G,X=Y.produce;Y.produceWithPatches.bind(Y),Y.setAutoFreeze.bind(Y),Y.setUseStrictShallowCopy.bind(Y),Y.applyPatches.bind(Y),Y.createDraft.bind(Y),Y.finishDraft.bind(Y);function Z(e){let t=new Q(e);return window.__PREVIEWJS_CONTROLLER__=t,t}var Q=class{constructor(e){this.options=e,this.lastRender=null,this.bootstrapStatus=`pending`,this.state={loading:!1,rendered:!1,errors:[],logs:[],actions:[]},this.onViteBeforeUpdateLogsLength=0,this.onViteBeforeUpdateErrorsLength=0}async render(e,t){let n=this.lastRender;if(this.lastRender={previewableId:e,options:t},n?.previewableId!==e||this.bootstrapStatus===`failure`){this.resetIframe(e);return}let r=this.options.getIframe()?.contentWindow;if(!r||this.bootstrapStatus!==`success`)return;if(!r.__PREVIEWJS_IFRAME__.render){r.__PREVIEWJS_IFRAME__.reportEvent({kind:`error`,source:`renderer`,message:`Iframe was bootstrapped but not fully initialised.`});return}let i=r.__PREVIEWJS_IFRAME__.render(t);this.clearExpectRenderTimeout(),this.expectRenderTimeout=setTimeout(()=>{console.warn(`Expected render did not occur after 5 seconds. Reloading iframe...`),this.resetIframe(e)},5e3),await i}resetIframe(e){let t=this.options.getIframe();t&&(t.src=`/${e}/?t=${Date.now()}`,this.bootstrapStatus=`not-started`)}onPreviewEvent(t){if(!(this.bootstrapStatus===`not-started`&&t.kind!==`bootstrapping`))switch(t.kind){case`bootstrapping`:this.bootstrapStatus=`pending`;break;case`bootstrapped`:this.bootstrapStatus=`success`,this.onViteBeforeUpdateLogsLength=0,this.onViteBeforeUpdateErrorsLength=0,this.updateState(e=>{e.loading=!0,e.rendered=!1,e.errors=[],e.logs=[]}),this.lastRender&&this.render(this.lastRender.previewableId,this.lastRender.options).catch(console.error);break;case`vite-before-reload`:this.updateState(e=>{e.logs=[],e.errors=[]}),this.lastRender&&this.resetIframe(this.lastRender.previewableId);break;case`vite-before-update`:this.onViteBeforeUpdateLogsLength=this.state.logs.length,this.updateState(e=>{for(let n of t.payload.updates)e.errors=e.errors.filter(e=>e.source!==`hmr`||e.modulePath!==n.acceptedPath)}),this.onViteBeforeUpdateErrorsLength=this.state.errors.length;break;case`vite-invalidate`:this.updateState(e=>{this.onViteBeforeUpdateLogsLength=0,this.onViteBeforeUpdateErrorsLength=0,e.logs=[]});break;case`vite-after-update`:this.updateState(e=>{let t=this.onViteBeforeUpdateLogsLength,n=this.onViteBeforeUpdateErrorsLength;this.onViteBeforeUpdateLogsLength=0,this.onViteBeforeUpdateErrorsLength=0,e.logs=e.logs.slice(t),e.errors=e.errors.slice(n)});break;case`before-render`:this.updateState(e=>{this.onViteBeforeUpdateLogsLength=0,e.logs=[]});break;case`rendered`:this.updateState(e=>{e.loading=!1,e.rendered=!0}),this.clearExpectRenderTimeout();break;case`error`:this.updateState(e=>{e.loading=!1,!(t.source===`vite`&&t.message.includes(`Failed to fetch dynamically imported module`)&&e.errors.find(e=>e.source===`vite`))&&(e.errors=e.errors.filter(e=>e.source!==t.source),e.errors.push(t))}),this.clearExpectRenderTimeout(),this.bootstrapStatus===`pending`&&(this.bootstrapStatus=`failure`);break;case`log-message`:this.updateState(e=>{e.logs.push(t)});break;case`action`:this.updateState(e=>{e.actions.push(t)});break;case`toggle-inspect`:{let e=this.options.getIframe()?.contentWindow;e&&(e.__PREVIEWJS_IFRAME__.toggleInspect=t.enabled,e.__PREVIEWJS_IFRAME__.refresh({refetchWrapperModule:!0}));break}default:throw(0,e.assertNever)(t)}}updateState(e){this.state=X(this.state,e),this.options.onStateUpdate?.(this.state)}clearExpectRenderTimeout(){this.expectRenderTimeout&&=(clearTimeout(this.expectRenderTimeout),null)}};export{Z as createController};
|
package/package.json
CHANGED
|
@@ -1,23 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cfp-studio/iframe",
|
|
3
|
-
"version": "18.0
|
|
3
|
+
"version": "18.3.0",
|
|
4
4
|
"license": "MIT",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"main": "./dist/index.mjs",
|
|
7
|
-
"types": "./dist/index.d.ts",
|
|
8
5
|
"files": [
|
|
9
6
|
"dist",
|
|
10
7
|
"preview"
|
|
11
8
|
],
|
|
9
|
+
"type": "module",
|
|
10
|
+
"main": "./dist/index.mjs",
|
|
11
|
+
"types": "./dist/index.d.mts",
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@cfp-studio/fiber-reader": "0.2.0"
|
|
14
|
+
},
|
|
12
15
|
"devDependencies": {
|
|
13
16
|
"assert-never": "1.4.0",
|
|
14
17
|
"immer": "10.1.1",
|
|
15
|
-
"typescript": "5.
|
|
16
|
-
"
|
|
17
|
-
|
|
18
|
+
"typescript": "5.9.3",
|
|
19
|
+
"vite": "6.4.5"
|
|
20
|
+
},
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=20"
|
|
23
|
+
},
|
|
24
|
+
"cfp": {
|
|
25
|
+
"domain": "web-preview",
|
|
26
|
+
"layer": "engine"
|
|
18
27
|
},
|
|
19
28
|
"scripts": {
|
|
20
|
-
"build": "
|
|
21
|
-
"
|
|
29
|
+
"build": "tsdown",
|
|
30
|
+
"typecheck": "tsc --noEmit",
|
|
31
|
+
"release": "tsdown --minify",
|
|
32
|
+
"lint": "oxlint",
|
|
33
|
+
"format": "oxfmt --write ."
|
|
22
34
|
}
|
|
23
35
|
}
|
|
@@ -4,6 +4,7 @@ import { setUpLogInterception } from "./logs";
|
|
|
4
4
|
import { loadRenderer } from "./renderer";
|
|
5
5
|
import { runRenderer } from "./run-renderer";
|
|
6
6
|
import { setState } from "./state";
|
|
7
|
+
import { setupRuntimeBridge } from "./runtime-bridge";
|
|
7
8
|
import { setupViteHmrListener } from "./vite-hmr-listener";
|
|
8
9
|
import { setupVSCodeControlListener } from "./vscode-control";
|
|
9
10
|
|
|
@@ -21,6 +22,7 @@ export function initListeners() {
|
|
|
21
22
|
setUpLinkInterception();
|
|
22
23
|
overrideCopyCutPaste();
|
|
23
24
|
setupVSCodeControlListener();
|
|
25
|
+
setupRuntimeBridge();
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
export function initPreview({
|
|
@@ -29,10 +31,8 @@ export function initPreview({
|
|
|
29
31
|
wrapperModule,
|
|
30
32
|
wrapperName,
|
|
31
33
|
}: {
|
|
32
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
33
34
|
previewableModule: any;
|
|
34
35
|
previewableName: string;
|
|
35
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
36
36
|
wrapperModule: any;
|
|
37
37
|
wrapperName: string;
|
|
38
38
|
}): typeof window.__PREVIEWJS_IFRAME__.refresh {
|
|
@@ -67,7 +67,6 @@ export function initPreview({
|
|
|
67
67
|
shouldAbortRender: () => renderId !== thisRenderId,
|
|
68
68
|
loadRenderer,
|
|
69
69
|
});
|
|
70
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
71
70
|
} catch (error: any) {
|
|
72
71
|
if (root.innerHTML !== rootHtml) {
|
|
73
72
|
// 重新调用前一次渲染,以确保预览不会白屏。
|
|
@@ -8,7 +8,6 @@ export function setUpLinkInterception() {
|
|
|
8
8
|
break;
|
|
9
9
|
}
|
|
10
10
|
if (node.nodeName === "A") {
|
|
11
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
12
11
|
const url = (node as any).href;
|
|
13
12
|
// Handle click here by posting data back to VS Code
|
|
14
13
|
// for your extension to handle
|
|
@@ -14,7 +14,6 @@ const HMR_FAILED_UPDATE_REGEX =
|
|
|
14
14
|
export function setUpLogInterception() {
|
|
15
15
|
const makeLogger =
|
|
16
16
|
(level: LogLevel, defaultFn: typeof console.log) =>
|
|
17
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
18
17
|
(...args: any[]) => {
|
|
19
18
|
// Prevent an infinite call stack size if anything here logs.
|
|
20
19
|
const self = console[level];
|
|
@@ -74,7 +73,6 @@ export function setUpLogInterception() {
|
|
|
74
73
|
* @returns
|
|
75
74
|
*/
|
|
76
75
|
|
|
77
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
78
76
|
function formatError(error?: any, message?: any): string {
|
|
79
77
|
// 如果 error 对象存在且包含 stack 和 message 属性
|
|
80
78
|
if (error?.stack && error.message) {
|
|
@@ -95,7 +93,6 @@ function formatError(error?: any, message?: any): string {
|
|
|
95
93
|
* @param args
|
|
96
94
|
* @returns
|
|
97
95
|
*/
|
|
98
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
99
96
|
function formatLogMessage(...args: any[]) {
|
|
100
97
|
if (args.length === 0) {
|
|
101
98
|
return "";
|
|
@@ -110,7 +107,6 @@ function formatLogMessage(...args: any[]) {
|
|
|
110
107
|
* @param value
|
|
111
108
|
* @returns
|
|
112
109
|
*/
|
|
113
|
-
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
|
114
110
|
function formatValue(value: any) {
|
|
115
111
|
if (typeof value === "string") {
|
|
116
112
|
return value;
|