@antv/dumi-theme-antv 0.7.3 → 0.7.5-beta.1

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.
@@ -139,6 +139,12 @@ export default function ThemeAntVContextWrapper() {
139
139
  path: "examples",
140
140
  file: `${PAGES_DIR}/Examples`
141
141
  },
142
+ {
143
+ id: "dumi-theme-antv-example-list-en",
144
+ absPath: "/en/examples",
145
+ path: "en/examples",
146
+ file: `${PAGES_DIR}/Examples`
147
+ },
142
148
  {
143
149
  id: "dumi-theme-antv-example-list-lang",
144
150
  absPath: "/:language/examples",
@@ -12,6 +12,8 @@ import { PlayCircleOutlined, PushpinOutlined } from '@ant-design/icons';
12
12
  import React, { useEffect, useRef, useState } from 'react';
13
13
  import styles from "./Preview.module.less";
14
14
  import { safeEval } from "./utils";
15
+ import { compile } from "../CodeEditor/utils";
16
+ import { uniqueId } from 'lodash-es';
15
17
  function normalizeValue(node) {
16
18
  return node instanceof Promise ? node : Promise.resolve(node);
17
19
  }
@@ -41,7 +43,9 @@ export var Preview = function Preview(_ref) {
41
43
  var source = _ref.source,
42
44
  code = _ref.code,
43
45
  _ref$pin = _ref.pin,
44
- pin = _ref$pin === void 0 ? true : _ref$pin;
46
+ pin = _ref$pin === void 0 ? true : _ref$pin,
47
+ _ref$autoMount = _ref.autoMount,
48
+ autoMount = _ref$autoMount === void 0 ? false : _ref$autoMount;
45
49
  var containerRef = useRef(null);
46
50
  var ulRef = useRef(null);
47
51
  var nodeRef = useRef(null);
@@ -57,40 +61,73 @@ export var Preview = function Preview(_ref) {
57
61
  _useState6 = _slicedToArray(_useState5, 2),
58
62
  node = _useState6[0],
59
63
  setNode = _useState6[1];
64
+ var _useState7 = useState("container_".concat(uniqueId())),
65
+ _useState8 = _slicedToArray(_useState7, 1),
66
+ uniqueContainerId = _useState8[0];
67
+ function executeScript(code, scriptContainerId) {
68
+ if (!containerRef.current) return null;
69
+ var containerDiv = document.createElement('div');
70
+ containerDiv.id = scriptContainerId;
71
+
72
+ // 使用唯一ID作为容器ID,确保多个Preview组件不会冲突
73
+ containerDiv.innerHTML = "<div id=\"".concat(uniqueContainerId, "\" class=\"playgroundCodeContainer\" style=\"width: 100%; height: 100%\"></div>");
74
+ containerRef.current.innerHTML = '';
75
+ containerRef.current.appendChild(containerDiv);
76
+ var script = document.createElement('script');
77
+ var modifiedCode = code.replace(/'container'|"container"/g, "'".concat(uniqueContainerId, "'"));
78
+ script.innerHTML = modifiedCode;
79
+ containerDiv.appendChild(script);
80
+ return containerDiv;
81
+ }
60
82
  function execute(_x) {
61
83
  return _execute.apply(this, arguments);
62
84
  }
63
85
  function _execute() {
64
86
  _execute = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(source) {
65
- var value, rendered, n, _node;
87
+ var _node, compiledCode, scriptContainerId, containerNode, value, rendered, n;
66
88
  return _regeneratorRuntime().wrap(function _callee$(_context) {
67
89
  while (1) switch (_context.prev = _context.next) {
68
90
  case 0:
69
91
  setError(null);
70
92
  _context.prev = 1;
93
+ _node = null;
94
+ if (!autoMount) {
95
+ _context.next = 10;
96
+ break;
97
+ }
98
+ // 手动执行,自动挂载到 DOM 上
99
+ compiledCode = compile(source, '', true); // 为每个脚本容器生成唯一ID
100
+ scriptContainerId = "script_container_".concat(uniqueId());
101
+ containerNode = executeScript(compiledCode, scriptContainerId);
102
+ _node = containerNode;
103
+ _context.next = 17;
104
+ break;
105
+ case 10:
106
+ // 自执行函数
71
107
  value = safeEval(source);
72
108
  if (value instanceof Promise) setLoading(true);
73
109
  rendered = normalizeValue(value);
74
- _context.next = 7;
110
+ _context.next = 15;
75
111
  return rendered;
76
- case 7:
112
+ case 15:
77
113
  n = _context.sent;
78
114
  _node = normalizeDOM(n);
115
+ case 17:
79
116
  setNode(_node);
80
117
  setLoading(false);
81
- _context.next = 18;
118
+ _context.next = 26;
82
119
  break;
83
- case 13:
84
- _context.prev = 13;
120
+ case 21:
121
+ _context.prev = 21;
85
122
  _context.t0 = _context["catch"](1);
86
123
  setLoading(false);
87
124
  setError(_context.t0);
88
125
  console.error(_context.t0);
89
- case 18:
126
+ case 26:
90
127
  case "end":
91
128
  return _context.stop();
92
129
  }
93
- }, _callee, null, [[1, 13]]);
130
+ }, _callee, null, [[1, 21]]);
94
131
  }));
95
132
  return _execute.apply(this, arguments);
96
133
  }
@@ -106,6 +143,7 @@ export var Preview = function Preview(_ref) {
106
143
  execute(source);
107
144
  }
108
145
  function updateToolHeight() {
146
+ if (!ulRef.current || !containerRef.current) return;
109
147
  var _sizeOf = sizeOf(code),
110
148
  codeHeight = _sizeOf.height;
111
149
  var _sizeOf2 = sizeOf(containerRef.current),
@@ -119,9 +157,9 @@ export var Preview = function Preview(_ref) {
119
157
  execute(source);
120
158
  }, [source]);
121
159
 
122
- // 更新 node
160
+ // 更新 node(非 autoMount 模式)
123
161
  useEffect(function () {
124
- if (containerRef.current && node) {
162
+ if (containerRef.current && node && !autoMount) {
125
163
  var _nodeRef$current, _nodeRef$current$clea;
126
164
  (_nodeRef$current = nodeRef.current) === null || _nodeRef$current === void 0 || (_nodeRef$current$clea = _nodeRef$current.clear) === null || _nodeRef$current$clea === void 0 || _nodeRef$current$clea.call(_nodeRef$current);
127
165
  nodeRef.current = node;
@@ -133,7 +171,7 @@ export var Preview = function Preview(_ref) {
133
171
  }
134
172
  updateToolHeight();
135
173
  }
136
- }, [node]);
174
+ }, [node, autoMount]);
137
175
 
138
176
  // 销毁的时候调用 DOM 上的 clear 函数
139
177
  useEffect(function () {
@@ -141,13 +179,13 @@ export var Preview = function Preview(_ref) {
141
179
  var _nodeRef$current2, _nodeRef$current2$cle;
142
180
  return (_nodeRef$current2 = nodeRef.current) === null || _nodeRef$current2 === void 0 || (_nodeRef$current2$cle = _nodeRef$current2.clear) === null || _nodeRef$current2$cle === void 0 ? void 0 : _nodeRef$current2$cle.call(_nodeRef$current2);
143
181
  };
144
- });
182
+ }, []);
145
183
 
146
184
  // 是否需要隐藏代码
147
185
  useEffect(function () {
148
186
  if (pin !== false) return;
149
187
  code.style.display = 'none';
150
- }, [pin]);
188
+ }, [pin, code]);
151
189
 
152
190
  // 暂时和隐藏 toolbar
153
191
  useEffect(function () {
@@ -180,7 +218,7 @@ export var Preview = function Preview(_ref) {
180
218
  containerRef.current.removeEventListener('mouseleave', leave);
181
219
  }
182
220
  };
183
- }, []);
221
+ }, [code]);
184
222
  return /*#__PURE__*/React.createElement("div", {
185
223
  className: styles.preview
186
224
  }, loading ? /*#__PURE__*/React.createElement("div", {
package/dist/utils/env.js CHANGED
@@ -6,5 +6,6 @@ export var isBrowser = function isBrowser() {
6
6
  // 安全访问浏览器 API
7
7
  // eslint-disable-next-line no-unused-vars
8
8
  export var safeWindow = function safeWindow(fn) {
9
- if (isBrowser()) fn(window);
9
+ if (isBrowser()) return fn(window);
10
+ return undefined;
10
11
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antv/dumi-theme-antv",
3
- "version": "0.7.3",
3
+ "version": "0.7.5-beta.1",
4
4
  "description": "AntV website theme based on dumi2.",
5
5
  "keywords": [
6
6
  "dumi",
@@ -125,7 +125,7 @@
125
125
  "@types/styled-components": "^5.1.34",
126
126
  "@umijs/lint": "^4.3.34",
127
127
  "css-loader": "^7.1.2",
128
- "dumi": "^2.4.13",
128
+ "dumi": "2.4.18",
129
129
  "eslint": "^8.57.1",
130
130
  "father": "^4.5.1",
131
131
  "father-plugin-dumi-theme": "1.0.0-rc.1",
@@ -145,7 +145,8 @@
145
145
  "react-dom": ">=16.9.0"
146
146
  },
147
147
  "publishConfig": {
148
- "access": "public"
148
+ "access": "public",
149
+ "tag": "beta"
149
150
  },
150
151
  "authors": [
151
152
  "dumi",