@ant-design/agentic-ui 2.0.22 → 2.0.23

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.
@@ -23,66 +23,84 @@ import { Node } from "slate";
23
23
  import stringWidth from "string-width";
24
24
  import { getMediaType } from "../utils/dom";
25
25
  var inlineNode = /* @__PURE__ */ new Set(["break"]);
26
- var parserNode = (node, preString = "", parent, plugins) => {
26
+ var tryPluginConversion = (node, preString, parent, plugins) => {
27
27
  var _a;
28
- let str = "";
29
- if (!node)
30
- return str;
31
- if (plugins == null ? void 0 : plugins.length) {
32
- for (const plugin of plugins) {
33
- const rule = (_a = plugin.toMarkdown) == null ? void 0 : _a.find((r) => r.match(node));
34
- if (rule) {
35
- const converted = rule.convert(node);
36
- if (converted.type === "code") {
37
- const codeNode = converted;
38
- const language = codeNode.lang || "";
39
- const value = codeNode.value || "";
40
- if (!(value == null ? void 0 : value.trim())) {
41
- return `${preString}\`\`\`${language}
28
+ if (!(plugins == null ? void 0 : plugins.length))
29
+ return null;
30
+ for (const plugin of plugins) {
31
+ const rule = (_a = plugin.toMarkdown) == null ? void 0 : _a.find((r) => r.match(node));
32
+ if (!rule)
33
+ continue;
34
+ const converted = rule.convert(node);
35
+ return convertPluginNode(converted, preString, parent, plugins);
36
+ }
37
+ return null;
38
+ };
39
+ var convertPluginNode = (converted, preString, parent, plugins) => {
40
+ switch (converted.type) {
41
+ case "code":
42
+ return convertCodeNode(converted, preString);
43
+ case "blockquote":
44
+ return convertBlockquoteNode(converted, preString, parent, plugins);
45
+ case "paragraph":
46
+ return convertParagraphNode(converted, preString, parent, plugins);
47
+ case "heading":
48
+ return convertHeadingNode(converted, preString, parent, plugins);
49
+ case "text":
50
+ return converted.value || "";
51
+ default:
52
+ return "";
53
+ }
54
+ };
55
+ var convertCodeNode = (codeNode, preString) => {
56
+ const language = codeNode.lang || "";
57
+ const value = codeNode.value || "";
58
+ if (!(value == null ? void 0 : value.trim())) {
59
+ return `${preString}\`\`\`${language}
42
60
  ${preString}\`\`\``;
43
- }
44
- const codeLines = value.split("\n");
45
- const indentedCode = codeLines.map((line, index) => {
46
- if (index === 0 || index === codeLines.length - 1) {
47
- return line;
48
- }
49
- return preString + line;
50
- }).join("\n");
51
- return `${preString}\`\`\`${language}
61
+ }
62
+ const codeLines = value.split("\n");
63
+ const indentedCode = codeLines.map((line, index) => {
64
+ const isFirstOrLast = index === 0 || index === codeLines.length - 1;
65
+ return isFirstOrLast ? line : preString + line;
66
+ }).join("\n");
67
+ return `${preString}\`\`\`${language}
52
68
  ${indentedCode}
53
69
  ${preString}\`\`\``;
54
- } else if (converted.type === "blockquote") {
55
- const blockquoteNode = converted;
56
- return "> " + parserSlateNodeToMarkdown(
57
- blockquoteNode.children || [],
58
- preString,
59
- [...parent, __spreadProps(__spreadValues({}, blockquoteNode), { converted: true })],
60
- plugins
61
- );
62
- } else if (converted.type === "paragraph") {
63
- const paragraphNode = converted;
64
- return preString + parserSlateNodeToMarkdown(
65
- paragraphNode.children || [],
66
- preString,
67
- [...parent, __spreadProps(__spreadValues({}, paragraphNode), { converted: true })],
68
- plugins
69
- );
70
- } else if (converted.type === "heading") {
71
- const headingNode = converted;
72
- const level = headingNode.depth || 1;
73
- const content = parserSlateNodeToMarkdown(
74
- headingNode.children || [],
75
- preString,
76
- [...parent, __spreadProps(__spreadValues({}, headingNode), { converted: true })],
77
- plugins
78
- );
79
- return "#".repeat(level) + " " + content.replace(/\n+$/, "");
80
- } else if (converted.type === "text") {
81
- return converted.value || "";
82
- }
83
- return "";
84
- }
85
- }
70
+ };
71
+ var convertBlockquoteNode = (blockquoteNode, preString, parent, plugins) => {
72
+ return "> " + parserSlateNodeToMarkdown(
73
+ blockquoteNode.children || [],
74
+ preString,
75
+ [...parent, __spreadProps(__spreadValues({}, blockquoteNode), { converted: true })],
76
+ plugins
77
+ );
78
+ };
79
+ var convertParagraphNode = (paragraphNode, preString, parent, plugins) => {
80
+ return preString + parserSlateNodeToMarkdown(
81
+ paragraphNode.children || [],
82
+ preString,
83
+ [...parent, __spreadProps(__spreadValues({}, paragraphNode), { converted: true })],
84
+ plugins
85
+ );
86
+ };
87
+ var convertHeadingNode = (headingNode, preString, parent, plugins) => {
88
+ const level = headingNode.depth || 1;
89
+ const content = parserSlateNodeToMarkdown(
90
+ headingNode.children || [],
91
+ preString,
92
+ [...parent, __spreadProps(__spreadValues({}, headingNode), { converted: true })],
93
+ plugins
94
+ );
95
+ return "#".repeat(level) + " " + content.replace(/\n+$/, "");
96
+ };
97
+ var parserNode = (node, preString = "", parent, plugins) => {
98
+ let str = "";
99
+ if (!node)
100
+ return str;
101
+ const pluginResult = tryPluginConversion(node, preString, parent, plugins);
102
+ if (pluginResult !== null) {
103
+ return pluginResult;
86
104
  }
87
105
  switch (node.type) {
88
106
  case "card-before":
@@ -330,6 +330,42 @@ export declare class EditorStore {
330
330
  * @private
331
331
  */
332
332
  private compareCells;
333
+ /**
334
+ * 比较单元格属性
335
+ */
336
+ private compareCellProperties;
337
+ /**
338
+ * 比较单元格子节点
339
+ */
340
+ private compareCellChildren;
341
+ /**
342
+ * 判断是否是简单文本单元格
343
+ */
344
+ private isSimpleTextCell;
345
+ /**
346
+ * 比较简单文本单元格
347
+ */
348
+ private compareSimpleTextCell;
349
+ /**
350
+ * 比较文本节点属性
351
+ */
352
+ private compareTextNodeProperties;
353
+ /**
354
+ * 比较复杂单元格子节点
355
+ */
356
+ private compareComplexCellChildren;
357
+ /**
358
+ * 判断结构是否不同
359
+ */
360
+ private isStructurallyDifferent;
361
+ /**
362
+ * 替换复杂单元格子节点
363
+ */
364
+ private replaceComplexCellChildren;
365
+ /**
366
+ * 逐个比较子节点
367
+ */
368
+ private compareChildrenSequentially;
333
369
  /**
334
370
  * 执行操作队列中的所有操作。
335
371
  *
@@ -242,27 +242,76 @@ var EditorStore = class {
242
242
  var _a, _b, _c, _d;
243
243
  if (md === void 0)
244
244
  return;
245
- if (md === parserSlateNodeToMarkdown(this._editor.current.children))
246
- return;
245
+ try {
246
+ const currentMD = parserSlateNodeToMarkdown(
247
+ this._editor.current.children
248
+ );
249
+ if (md.trim() === currentMD.trim())
250
+ return;
251
+ } catch (error) {
252
+ console.warn(
253
+ "Failed to compare current content, proceeding with setMDContent:",
254
+ error
255
+ );
256
+ }
257
+ this.cancelSetMDContent();
247
258
  const chunkSize = (_a = options == null ? void 0 : options.chunkSize) != null ? _a : 5e3;
248
259
  const separator = (_b = options == null ? void 0 : options.separator) != null ? _b : /\n\n/;
249
260
  const useRAF = (_c = options == null ? void 0 : options.useRAF) != null ? _c : true;
250
261
  const batchSize = (_d = options == null ? void 0 : options.batchSize) != null ? _d : 50;
251
262
  const targetPlugins = plugins || this.plugins;
252
263
  if (md.length <= chunkSize) {
253
- const nodeList = parserMdToSchema(md, targetPlugins).schema;
254
- this.setContent(nodeList);
255
- this._editor.current.children = nodeList;
256
- ReactEditor.deselect(this._editor.current);
257
- if (options == null ? void 0 : options.onProgress) {
258
- options.onProgress(1);
264
+ try {
265
+ const nodeList = parserMdToSchema(md, targetPlugins).schema;
266
+ this.setContent(nodeList);
267
+ this._editor.current.children = nodeList;
268
+ ReactEditor.deselect(this._editor.current);
269
+ if (options == null ? void 0 : options.onProgress) {
270
+ options.onProgress(1);
271
+ }
272
+ } catch (error) {
273
+ console.error("Failed to set MD content:", error);
274
+ throw error;
259
275
  }
260
276
  return;
261
277
  }
278
+ const chunks = this._splitMarkdown(md, separator);
262
279
  if (!useRAF) {
263
- const chunks2 = this._splitMarkdown(md, separator);
280
+ try {
281
+ const allNodes = [];
282
+ for (const chunk of chunks) {
283
+ if (chunk.trim()) {
284
+ const { schema } = parserMdToSchema(chunk, targetPlugins);
285
+ allNodes.push(...schema);
286
+ }
287
+ }
288
+ if (allNodes.length > 0) {
289
+ this.setContent(allNodes);
290
+ this._editor.current.children = allNodes;
291
+ ReactEditor.deselect(this._editor.current);
292
+ }
293
+ if (options == null ? void 0 : options.onProgress) {
294
+ options.onProgress(1);
295
+ }
296
+ } catch (error) {
297
+ console.error("Failed to set MD content synchronously:", error);
298
+ throw error;
299
+ }
300
+ return;
301
+ }
302
+ if (chunks.length > 10) {
303
+ this._currentAbortController = new AbortController();
304
+ return this._parseAndSetContentWithRAF(
305
+ chunks,
306
+ targetPlugins || [],
307
+ batchSize,
308
+ options == null ? void 0 : options.onProgress,
309
+ this._currentAbortController.signal
310
+ );
311
+ }
312
+ try {
264
313
  const allNodes = [];
265
- for (const chunk of chunks2) {
314
+ for (const chunk of chunks) {
266
315
  if (chunk.trim()) {
267
316
  const { schema } = parserMdToSchema(chunk, targetPlugins);
268
317
  allNodes.push(...schema);
@@ -276,18 +325,9 @@ var EditorStore = class {
276
325
  if (options == null ? void 0 : options.onProgress) {
277
326
  options.onProgress(1);
278
327
  }
279
- return;
280
- }
281
- const chunks = this._splitMarkdown(md, separator);
282
- if (useRAF && chunks.length > 10) {
283
- this._currentAbortController = new AbortController();
284
- return this._parseAndSetContentWithRAF(
285
- chunks,
286
- targetPlugins || [],
287
- batchSize,
288
- options == null ? void 0 : options.onProgress,
289
- this._currentAbortController.signal
290
- );
328
+ } catch (error) {
329
+ console.error("Failed to set MD content with small chunks:", error);
330
+ throw error;
291
331
  }
292
332
  }
293
333
  /**
@@ -328,6 +368,15 @@ var EditorStore = class {
328
368
  reject(new Error("Operation was cancelled"));
329
369
  return;
330
370
  }
371
+ if (!this._editor.current) {
372
+ if (rafId) {
373
+ cancelAnimationFrame(rafId);
374
+ rafId = null;
375
+ }
376
+ this._currentAbortController = null;
377
+ reject(new Error("Editor instance is no longer available"));
378
+ return;
379
+ }
331
380
  const endIndex = Math.min(
332
381
  currentChunkIndex + parseChunksPerFrame,
333
382
  totalChunks
@@ -335,24 +384,32 @@ var EditorStore = class {
335
384
  for (let i = currentChunkIndex; i < endIndex; i++) {
336
385
  const chunk = chunks[i];
337
386
  if (chunk.trim()) {
338
- const { schema } = parserMdToSchema(chunk, plugins);
339
- if (schema.length > 0) {
340
- if (isFirstBatch) {
341
- this._editor.current.children = schema;
342
- this._editor.current.onChange();
343
- isFirstBatch = false;
344
- } else {
345
- Transforms.insertNodes(this._editor.current, schema, {
346
- at: [this._editor.current.children.length]
347
- });
387
+ try {
388
+ const { schema } = parserMdToSchema(chunk, plugins);
389
+ if (schema.length > 0) {
390
+ if (isFirstBatch) {
391
+ this._editor.current.children = schema;
392
+ this._editor.current.onChange();
393
+ isFirstBatch = false;
394
+ } else {
395
+ Transforms.insertNodes(this._editor.current, schema, {
396
+ at: [this._editor.current.children.length]
397
+ });
398
+ }
348
399
  }
400
+ } catch (chunkError) {
401
+ console.warn(`Failed to parse chunk ${i}:`, chunkError);
349
402
  }
350
403
  }
351
404
  }
352
405
  currentChunkIndex = endIndex;
353
406
  const progress = currentChunkIndex / totalChunks;
354
407
  if (onProgress) {
355
- onProgress(progress);
408
+ try {
409
+ onProgress(progress);
410
+ } catch (progressError) {
411
+ console.warn("Progress callback failed:", progressError);
412
+ }
356
413
  }
357
414
  if (currentChunkIndex < totalChunks) {
358
415
  rafId = requestAnimationFrame(parseAndInsertNextBatch);
@@ -817,6 +874,15 @@ var EditorStore = class {
817
874
  * @private
818
875
  */
819
876
  compareCells(newCell, oldCell, path, operations) {
877
+ this.compareCellProperties(newCell, oldCell, path, operations);
878
+ const newChildren = newCell.children || [];
879
+ const oldChildren = oldCell.children || [];
880
+ this.compareCellChildren(newChildren, oldChildren, path, operations);
881
+ }
882
+ /**
883
+ * 比较单元格属性
884
+ */
885
+ compareCellProperties(newCell, oldCell, path, operations) {
820
886
  const newCellProps = __spreadProps(__spreadValues({}, newCell), { children: void 0 });
821
887
  const oldCellProps = __spreadProps(__spreadValues({}, oldCell), { children: void 0 });
822
888
  if (!isEqual(newCellProps, oldCellProps)) {
@@ -827,50 +893,116 @@ var EditorStore = class {
827
893
  priority: 7
828
894
  });
829
895
  }
830
- const newChildren = newCell.children || [];
831
- const oldChildren = oldCell.children || [];
832
- if (newChildren.length === 1 && oldChildren.length === 1 && typeof newChildren[0].text === "string" && typeof oldChildren[0].text === "string") {
833
- if (newChildren[0].text !== oldChildren[0].text) {
834
- operations.push({
835
- type: "text",
836
- path: [...path, 0],
837
- text: newChildren[0].text,
838
- priority: 8
839
- });
840
- }
841
- const newTextProps = __spreadValues({}, newChildren[0]);
842
- const oldTextProps = __spreadValues({}, oldChildren[0]);
843
- delete newTextProps.text;
844
- delete oldTextProps.text;
845
- if (!isEqual(newTextProps, oldTextProps)) {
846
- operations.push({
847
- type: "update",
848
- path: [...path, 0],
849
- properties: newTextProps,
850
- priority: 7
851
- });
852
- }
853
- } else {
854
- const structurallyDifferent = newChildren.length !== oldChildren.length || newChildren.some(
855
- (n, i) => oldChildren[i] && n.type !== oldChildren[i].type
896
+ }
897
+ /**
898
+ * 比较单元格子节点
899
+ */
900
+ compareCellChildren(newChildren, oldChildren, path, operations) {
901
+ if (this.isSimpleTextCell(newChildren, oldChildren)) {
902
+ this.compareSimpleTextCell(newChildren, oldChildren, path, operations);
903
+ return;
904
+ }
905
+ this.compareComplexCellChildren(newChildren, oldChildren, path, operations);
906
+ }
907
+ /**
908
+ * 判断是否是简单文本单元格
909
+ */
910
+ isSimpleTextCell(newChildren, oldChildren) {
911
+ return newChildren.length === 1 && oldChildren.length === 1 && typeof newChildren[0].text === "string" && typeof oldChildren[0].text === "string";
912
+ }
913
+ /**
914
+ * 比较简单文本单元格
915
+ */
916
+ compareSimpleTextCell(newChildren, oldChildren, path, operations) {
917
+ if (newChildren[0].text !== oldChildren[0].text) {
918
+ operations.push({
919
+ type: "text",
920
+ path: [...path, 0],
921
+ text: newChildren[0].text,
922
+ priority: 8
923
+ });
924
+ }
925
+ this.compareTextNodeProperties(
926
+ newChildren[0],
927
+ oldChildren[0],
928
+ path,
929
+ operations
930
+ );
931
+ }
932
+ /**
933
+ * 比较文本节点属性
934
+ */
935
+ compareTextNodeProperties(newChild, oldChild, path, operations) {
936
+ const newTextProps = __spreadValues({}, newChild);
937
+ const oldTextProps = __spreadValues({}, oldChild);
938
+ delete newTextProps.text;
939
+ delete oldTextProps.text;
940
+ if (!isEqual(newTextProps, oldTextProps)) {
941
+ operations.push({
942
+ type: "update",
943
+ path: [...path, 0],
944
+ properties: newTextProps,
945
+ priority: 7
946
+ });
947
+ }
948
+ }
949
+ /**
950
+ * 比较复杂单元格子节点
951
+ */
952
+ compareComplexCellChildren(newChildren, oldChildren, path, operations) {
953
+ const structurallyDifferent = this.isStructurallyDifferent(
954
+ newChildren,
955
+ oldChildren
956
+ );
957
+ if (structurallyDifferent) {
958
+ this.replaceComplexCellChildren(
959
+ newChildren,
960
+ oldChildren,
961
+ path,
962
+ operations
963
+ );
964
+ return;
965
+ }
966
+ this.compareChildrenSequentially(
967
+ newChildren,
968
+ oldChildren,
969
+ path,
970
+ operations
971
+ );
972
+ }
973
+ /**
974
+ * 判断结构是否不同
975
+ */
976
+ isStructurallyDifferent(newChildren, oldChildren) {
977
+ if (newChildren.length !== oldChildren.length)
978
+ return true;
979
+ return newChildren.some(
980
+ (n, i) => oldChildren[i] && n.type !== oldChildren[i].type
981
+ );
982
+ }
983
+ /**
984
+ * 替换复杂单元格子节点
985
+ */
986
+ replaceComplexCellChildren(newChildren, oldChildren, path, operations) {
987
+ const childOps = this.generateDiffOperations(newChildren, oldChildren);
988
+ childOps.forEach((op) => {
989
+ operations.push(__spreadProps(__spreadValues({}, op), {
990
+ path: [...path, ...op.path]
991
+ }));
992
+ });
993
+ }
994
+ /**
995
+ * 逐个比较子节点
996
+ */
997
+ compareChildrenSequentially(newChildren, oldChildren, path, operations) {
998
+ const length = Math.min(newChildren.length, oldChildren.length);
999
+ for (let i = 0; i < length; i++) {
1000
+ this.compareNodes(
1001
+ newChildren[i],
1002
+ oldChildren[i],
1003
+ [...path, i],
1004
+ operations
856
1005
  );
857
- if (structurallyDifferent) {
858
- const childOps = this.generateDiffOperations(newChildren, oldChildren);
859
- childOps.forEach((op) => {
860
- operations.push(__spreadProps(__spreadValues({}, op), {
861
- path: [...path, ...op.path]
862
- }));
863
- });
864
- } else {
865
- for (let i = 0; i < Math.min(newChildren.length, oldChildren.length); i++) {
866
- this.compareNodes(
867
- newChildren[i],
868
- oldChildren[i],
869
- [...path, i],
870
- operations
871
- );
872
- }
873
- }
874
1006
  }
875
1007
  }
876
1008
  /**
@@ -56,6 +56,85 @@ import {
56
56
  import { mdDataSchemaValidator } from "../validator";
57
57
  import { TemplateEngine } from "./templateEngine";
58
58
  export * from "./templateEngine";
59
+ var createSandboxInstance = (sandboxConfig) => {
60
+ var _a, _b;
61
+ return createSandbox(__spreadProps(__spreadValues({}, DEFAULT_SANDBOX_CONFIG), {
62
+ allowDOM: (_a = sandboxConfig.allowDOM) != null ? _a : true,
63
+ allowedGlobals: sandboxConfig.allowedGlobals || DEFAULT_SANDBOX_CONFIG.allowedGlobals,
64
+ forbiddenGlobals: sandboxConfig.forbiddenGlobals || DEFAULT_SANDBOX_CONFIG.forbiddenGlobals,
65
+ strictMode: (_b = sandboxConfig.strictMode) != null ? _b : true,
66
+ timeout: sandboxConfig.timeout || 3e3
67
+ }));
68
+ };
69
+ var createSandboxContext = (shadowRoot) => {
70
+ return {
71
+ shadowRoot,
72
+ safeWindow: {
73
+ devicePixelRatio: typeof window !== "undefined" ? window.devicePixelRatio : 1,
74
+ innerWidth: typeof window !== "undefined" ? window.innerWidth : 1024,
75
+ innerHeight: typeof window !== "undefined" ? window.innerHeight : 768
76
+ }
77
+ };
78
+ };
79
+ var executeUnsafeScript = (script, shadowRoot) => {
80
+ console.warn("沙箱已禁用,使用不安全的脚本执行方式");
81
+ const scriptFn = new Function(
82
+ "shadowRoot",
83
+ "window",
84
+ script.textContent || ""
85
+ );
86
+ try {
87
+ scriptFn(shadowRoot, {
88
+ devicePixelRatio: window.devicePixelRatio
89
+ });
90
+ } catch (evalError) {
91
+ console.error("执行脚本错误:", evalError);
92
+ }
93
+ };
94
+ var executeExternalScript = (script, shadowRoot) => {
95
+ console.warn("外部脚本暂时不通过沙箱执行:", script.src);
96
+ try {
97
+ shadowRoot == null ? void 0 : shadowRoot.appendChild(script);
98
+ } catch (appendError) {
99
+ console.error("Error appending external script:", appendError);
100
+ }
101
+ };
102
+ var executeSandboxedScript = (script, shadowRoot, sandboxConfig) => __async(void 0, null, function* () {
103
+ const sandbox = createSandboxInstance(sandboxConfig);
104
+ try {
105
+ const result = yield sandbox.execute(
106
+ script.textContent || "",
107
+ createSandboxContext(shadowRoot)
108
+ );
109
+ if (!result.success && result.error) {
110
+ console.error("沙箱脚本执行错误:", result.error);
111
+ }
112
+ } catch (evalError) {
113
+ console.error("沙箱执行失败:", evalError);
114
+ } finally {
115
+ sandbox.destroy();
116
+ }
117
+ });
118
+ var executeInlineScript = (script, shadowRoot, sandboxConfig) => __async(void 0, null, function* () {
119
+ if (!sandboxConfig.enabled) {
120
+ executeUnsafeScript(script, shadowRoot);
121
+ return;
122
+ }
123
+ yield executeSandboxedScript(script, shadowRoot, sandboxConfig);
124
+ });
125
+ var executeScript = (script, shadowRoot, sandboxConfig) => __async(void 0, null, function* () {
126
+ try {
127
+ if (!script.src && script.textContent) {
128
+ yield executeInlineScript(script, shadowRoot, sandboxConfig);
129
+ return;
130
+ }
131
+ if (script.src) {
132
+ executeExternalScript(script, shadowRoot);
133
+ }
134
+ } catch (scriptError) {
135
+ console.error("Script execution error:", scriptError);
136
+ }
137
+ });
59
138
  var ErrorBoundary = class extends Component {
60
139
  constructor(props) {
61
140
  super(props);
@@ -354,61 +433,7 @@ a:active {
354
433
  }
355
434
  });
356
435
  scripts.forEach((script) => __async(void 0, null, function* () {
357
- var _a2, _b2;
358
- try {
359
- if (!script.src && script.textContent) {
360
- if (sandboxConfig.enabled) {
361
- const sandbox = createSandbox(__spreadProps(__spreadValues({}, DEFAULT_SANDBOX_CONFIG), {
362
- allowDOM: (_a2 = sandboxConfig.allowDOM) != null ? _a2 : true,
363
- allowedGlobals: sandboxConfig.allowedGlobals || DEFAULT_SANDBOX_CONFIG.allowedGlobals,
364
- forbiddenGlobals: sandboxConfig.forbiddenGlobals || DEFAULT_SANDBOX_CONFIG.forbiddenGlobals,
365
- strictMode: (_b2 = sandboxConfig.strictMode) != null ? _b2 : true,
366
- timeout: sandboxConfig.timeout || 3e3
367
- }));
368
- try {
369
- const result = yield sandbox.execute(script.textContent, {
370
- shadowRoot,
371
- // 提供一个安全的 window 上下文
372
- safeWindow: {
373
- devicePixelRatio: typeof window !== "undefined" ? window.devicePixelRatio : 1,
374
- innerWidth: typeof window !== "undefined" ? window.innerWidth : 1024,
375
- innerHeight: typeof window !== "undefined" ? window.innerHeight : 768
376
- }
377
- });
378
- if (!result.success && result.error) {
379
- console.error("沙箱脚本执行错误:", result.error);
380
- }
381
- } catch (evalError) {
382
- console.error("沙箱执行失败:", evalError);
383
- } finally {
384
- sandbox.destroy();
385
- }
386
- } else {
387
- console.warn("沙箱已禁用,使用不安全的脚本执行方式");
388
- const scriptFn = new Function(
389
- "shadowRoot",
390
- "window",
391
- script.textContent
392
- );
393
- try {
394
- scriptFn(shadowRoot, {
395
- devicePixelRatio: window.devicePixelRatio
396
- });
397
- } catch (evalError) {
398
- console.error("执行脚本错误:", evalError);
399
- }
400
- }
401
- } else if (script.src) {
402
- console.warn("外部脚本暂时不通过沙箱执行:", script.src);
403
- try {
404
- shadowRoot == null ? void 0 : shadowRoot.appendChild(script);
405
- } catch (appendError) {
406
- console.error("Error appending external script:", appendError);
407
- }
408
- }
409
- } catch (scriptError) {
410
- console.error("Script execution error:", scriptError);
411
- }
436
+ yield executeScript(script, shadowRoot, sandboxConfig);
412
437
  }));
413
438
  } catch (contentError) {
414
439
  console.error("Error processing content:", contentError);