@sulesky/next-react-sandbox 1.0.3
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/LICENSE.md +20 -0
- package/README.md +1 -0
- package/dist/designable.react-sandbox.umd.production.css +1 -0
- package/dist/designable.react-sandbox.umd.production.js +1 -0
- package/dist/designable.react-sandbox.umd.production.min.css +1 -0
- package/dist/designable.react-sandbox.umd.production.min.js +11155 -0
- package/esm/index.d.ts +11 -0
- package/esm/index.js +87 -0
- package/lib/index.d.ts +11 -0
- package/lib/index.js +127 -0
- package/package.json +37 -0
- package/rollup.config.mjs +3 -0
- package/src/index.ts +142 -0
- package/tsconfig.build.json +10 -0
- package/tsconfig.json +5 -0
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface ISandboxProps {
|
|
3
|
+
style?: React.CSSProperties;
|
|
4
|
+
cssAssets?: string[];
|
|
5
|
+
jsAssets?: string[];
|
|
6
|
+
scope?: any;
|
|
7
|
+
}
|
|
8
|
+
export declare const useSandbox: (props: React.PropsWithChildren<ISandboxProps>) => React.MutableRefObject<HTMLIFrameElement>;
|
|
9
|
+
export declare const useSandboxScope: () => any;
|
|
10
|
+
export declare const renderSandboxContent: (render: (scope?: any) => React.ReactNode) => void;
|
|
11
|
+
export declare const Sandbox: React.FC<ISandboxProps>;
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
var __assign = (this && this.__assign) || function () {
|
|
2
|
+
__assign = Object.assign || function(t) {
|
|
3
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
+
s = arguments[i];
|
|
5
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
+
t[p] = s[p];
|
|
7
|
+
}
|
|
8
|
+
return t;
|
|
9
|
+
};
|
|
10
|
+
return __assign.apply(this, arguments);
|
|
11
|
+
};
|
|
12
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
13
|
+
var t = {};
|
|
14
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
15
|
+
t[p] = s[p];
|
|
16
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
17
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
18
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
19
|
+
t[p[i]] = s[p[i]];
|
|
20
|
+
}
|
|
21
|
+
return t;
|
|
22
|
+
};
|
|
23
|
+
import React, { useRef, useEffect } from 'react';
|
|
24
|
+
import { isFn, globalThisPolyfill } from '@sulesky/next-shared';
|
|
25
|
+
import { useDesigner, useWorkspace, useLayout, usePrefix, } from '@sulesky/next-react';
|
|
26
|
+
import { createRoot } from 'react-dom/client';
|
|
27
|
+
var sandboxRoot = null;
|
|
28
|
+
export var useSandbox = function (props) {
|
|
29
|
+
var ref = useRef(null);
|
|
30
|
+
var appCls = usePrefix('app');
|
|
31
|
+
var designer = useDesigner();
|
|
32
|
+
var workspace = useWorkspace();
|
|
33
|
+
var layout = useLayout();
|
|
34
|
+
var cssAssets = props.cssAssets || [];
|
|
35
|
+
var jsAssets = props.jsAssets || [];
|
|
36
|
+
var getCSSVar = function (name) {
|
|
37
|
+
return getComputedStyle(document.querySelector(".".concat(appCls))).getPropertyValue(name);
|
|
38
|
+
};
|
|
39
|
+
useEffect(function () {
|
|
40
|
+
var _a, _b;
|
|
41
|
+
if (ref.current && workspace) {
|
|
42
|
+
var styles = (_a = cssAssets === null || cssAssets === void 0 ? void 0 : cssAssets.map) === null || _a === void 0 ? void 0 : _a.call(cssAssets, function (css) {
|
|
43
|
+
return "<link media=\"all\" rel=\"stylesheet\" href=\"".concat(css, "\" />");
|
|
44
|
+
}).join('\n');
|
|
45
|
+
var scripts = (_b = jsAssets === null || jsAssets === void 0 ? void 0 : jsAssets.map) === null || _b === void 0 ? void 0 : _b.call(jsAssets, function (js) {
|
|
46
|
+
return "<script src=\"".concat(js, "\" type=\"text/javascript\" ></script>");
|
|
47
|
+
}).join('\n');
|
|
48
|
+
ref.current.contentWindow['__DESIGNABLE_SANDBOX_SCOPE__'] = props.scope;
|
|
49
|
+
ref.current.contentWindow['__DESIGNABLE_LAYOUT__'] = layout;
|
|
50
|
+
ref.current.contentWindow['__DESIGNABLE_ENGINE__'] = designer;
|
|
51
|
+
ref.current.contentWindow['__DESIGNABLE_WORKSPACE__'] = workspace;
|
|
52
|
+
ref.current.contentWindow['Formily'] = globalThisPolyfill['Formily'];
|
|
53
|
+
ref.current.contentWindow['Designable'] = globalThisPolyfill['Designable'];
|
|
54
|
+
ref.current.contentDocument.open();
|
|
55
|
+
ref.current.contentDocument.write("\n <!DOCTYPE html>\n <head>\n ".concat(styles, "\n </head>\n <style>\n html{\n overflow: overlay;\n }\n ::-webkit-scrollbar {\n width: 5px;\n height: 5px;\n }\n ::-webkit-scrollbar-thumb {\n background-color:").concat(getCSSVar('--dn-scrollbar-color'), ";\n border-radius: 0;\n transition: all .25s ease-in-out;\n }\n ::-webkit-scrollbar-thumb:hover {\n background-color: ").concat(getCSSVar('--dn-scrollbar-hover-color'), ";\n }\n body{\n margin:0;\n padding:0;\n overflow-anchor: none;\n user-select:none;\n background-color:").concat(layout.theme === 'light' ? '#fff' : 'transparent', " !important;\n }\n html{\n overflow-anchor: none;\n }\n .inherit-cusor * {\n cursor: inherit !important;\n }\n </style>\n <body>\n <div id=\"__SANDBOX_ROOT__\"></div>\n ").concat(scripts, "\n </body>\n </html>\n "));
|
|
56
|
+
ref.current.contentDocument.close();
|
|
57
|
+
}
|
|
58
|
+
}, [workspace]);
|
|
59
|
+
return ref;
|
|
60
|
+
};
|
|
61
|
+
if (globalThisPolyfill.frameElement) {
|
|
62
|
+
//解决iframe内嵌如果iframe被移除,内部React无法回收内存的问题
|
|
63
|
+
globalThisPolyfill.addEventListener('unload', function () {
|
|
64
|
+
if (sandboxRoot) {
|
|
65
|
+
sandboxRoot.unmount();
|
|
66
|
+
sandboxRoot = null;
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
export var useSandboxScope = function () {
|
|
71
|
+
return globalThisPolyfill['__DESIGNABLE_SANDBOX_SCOPE__'];
|
|
72
|
+
};
|
|
73
|
+
export var renderSandboxContent = function (render) {
|
|
74
|
+
if (isFn(render)) {
|
|
75
|
+
var container = document.getElementById('__SANDBOX_ROOT__');
|
|
76
|
+
if (container) {
|
|
77
|
+
if (!sandboxRoot) {
|
|
78
|
+
sandboxRoot = createRoot(container);
|
|
79
|
+
}
|
|
80
|
+
sandboxRoot.render(render(useSandboxScope()));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
export var Sandbox = function (props) {
|
|
85
|
+
var cssAssets = props.cssAssets, jsAssets = props.jsAssets, scope = props.scope, style = props.style, iframeProps = __rest(props, ["cssAssets", "jsAssets", "scope", "style"]);
|
|
86
|
+
return React.createElement('iframe', __assign(__assign({}, iframeProps), { ref: useSandbox(props), style: __assign({ height: '100%', width: '100%', border: 'none', display: 'block' }, style) }));
|
|
87
|
+
};
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export interface ISandboxProps {
|
|
3
|
+
style?: React.CSSProperties;
|
|
4
|
+
cssAssets?: string[];
|
|
5
|
+
jsAssets?: string[];
|
|
6
|
+
scope?: any;
|
|
7
|
+
}
|
|
8
|
+
export declare const useSandbox: (props: React.PropsWithChildren<ISandboxProps>) => React.MutableRefObject<HTMLIFrameElement>;
|
|
9
|
+
export declare const useSandboxScope: () => any;
|
|
10
|
+
export declare const renderSandboxContent: (render: (scope?: any) => React.ReactNode) => void;
|
|
11
|
+
export declare const Sandbox: React.FC<ISandboxProps>;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
16
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
17
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
18
|
+
}
|
|
19
|
+
Object.defineProperty(o, k2, desc);
|
|
20
|
+
}) : (function(o, m, k, k2) {
|
|
21
|
+
if (k2 === undefined) k2 = k;
|
|
22
|
+
o[k2] = m[k];
|
|
23
|
+
}));
|
|
24
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
25
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
26
|
+
}) : function(o, v) {
|
|
27
|
+
o["default"] = v;
|
|
28
|
+
});
|
|
29
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
30
|
+
var ownKeys = function(o) {
|
|
31
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
32
|
+
var ar = [];
|
|
33
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
34
|
+
return ar;
|
|
35
|
+
};
|
|
36
|
+
return ownKeys(o);
|
|
37
|
+
};
|
|
38
|
+
return function (mod) {
|
|
39
|
+
if (mod && mod.__esModule) return mod;
|
|
40
|
+
var result = {};
|
|
41
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
42
|
+
__setModuleDefault(result, mod);
|
|
43
|
+
return result;
|
|
44
|
+
};
|
|
45
|
+
})();
|
|
46
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
47
|
+
var t = {};
|
|
48
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
49
|
+
t[p] = s[p];
|
|
50
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
51
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
52
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
53
|
+
t[p[i]] = s[p[i]];
|
|
54
|
+
}
|
|
55
|
+
return t;
|
|
56
|
+
};
|
|
57
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
58
|
+
exports.Sandbox = exports.renderSandboxContent = exports.useSandboxScope = exports.useSandbox = void 0;
|
|
59
|
+
var react_1 = __importStar(require("react"));
|
|
60
|
+
var next_shared_1 = require("@sulesky/next-shared");
|
|
61
|
+
var next_react_1 = require("@sulesky/next-react");
|
|
62
|
+
var client_1 = require("react-dom/client");
|
|
63
|
+
var sandboxRoot = null;
|
|
64
|
+
var useSandbox = function (props) {
|
|
65
|
+
var ref = (0, react_1.useRef)(null);
|
|
66
|
+
var appCls = (0, next_react_1.usePrefix)('app');
|
|
67
|
+
var designer = (0, next_react_1.useDesigner)();
|
|
68
|
+
var workspace = (0, next_react_1.useWorkspace)();
|
|
69
|
+
var layout = (0, next_react_1.useLayout)();
|
|
70
|
+
var cssAssets = props.cssAssets || [];
|
|
71
|
+
var jsAssets = props.jsAssets || [];
|
|
72
|
+
var getCSSVar = function (name) {
|
|
73
|
+
return getComputedStyle(document.querySelector(".".concat(appCls))).getPropertyValue(name);
|
|
74
|
+
};
|
|
75
|
+
(0, react_1.useEffect)(function () {
|
|
76
|
+
var _a, _b;
|
|
77
|
+
if (ref.current && workspace) {
|
|
78
|
+
var styles = (_a = cssAssets === null || cssAssets === void 0 ? void 0 : cssAssets.map) === null || _a === void 0 ? void 0 : _a.call(cssAssets, function (css) {
|
|
79
|
+
return "<link media=\"all\" rel=\"stylesheet\" href=\"".concat(css, "\" />");
|
|
80
|
+
}).join('\n');
|
|
81
|
+
var scripts = (_b = jsAssets === null || jsAssets === void 0 ? void 0 : jsAssets.map) === null || _b === void 0 ? void 0 : _b.call(jsAssets, function (js) {
|
|
82
|
+
return "<script src=\"".concat(js, "\" type=\"text/javascript\" ></script>");
|
|
83
|
+
}).join('\n');
|
|
84
|
+
ref.current.contentWindow['__DESIGNABLE_SANDBOX_SCOPE__'] = props.scope;
|
|
85
|
+
ref.current.contentWindow['__DESIGNABLE_LAYOUT__'] = layout;
|
|
86
|
+
ref.current.contentWindow['__DESIGNABLE_ENGINE__'] = designer;
|
|
87
|
+
ref.current.contentWindow['__DESIGNABLE_WORKSPACE__'] = workspace;
|
|
88
|
+
ref.current.contentWindow['Formily'] = next_shared_1.globalThisPolyfill['Formily'];
|
|
89
|
+
ref.current.contentWindow['Designable'] = next_shared_1.globalThisPolyfill['Designable'];
|
|
90
|
+
ref.current.contentDocument.open();
|
|
91
|
+
ref.current.contentDocument.write("\n <!DOCTYPE html>\n <head>\n ".concat(styles, "\n </head>\n <style>\n html{\n overflow: overlay;\n }\n ::-webkit-scrollbar {\n width: 5px;\n height: 5px;\n }\n ::-webkit-scrollbar-thumb {\n background-color:").concat(getCSSVar('--dn-scrollbar-color'), ";\n border-radius: 0;\n transition: all .25s ease-in-out;\n }\n ::-webkit-scrollbar-thumb:hover {\n background-color: ").concat(getCSSVar('--dn-scrollbar-hover-color'), ";\n }\n body{\n margin:0;\n padding:0;\n overflow-anchor: none;\n user-select:none;\n background-color:").concat(layout.theme === 'light' ? '#fff' : 'transparent', " !important;\n }\n html{\n overflow-anchor: none;\n }\n .inherit-cusor * {\n cursor: inherit !important;\n }\n </style>\n <body>\n <div id=\"__SANDBOX_ROOT__\"></div>\n ").concat(scripts, "\n </body>\n </html>\n "));
|
|
92
|
+
ref.current.contentDocument.close();
|
|
93
|
+
}
|
|
94
|
+
}, [workspace]);
|
|
95
|
+
return ref;
|
|
96
|
+
};
|
|
97
|
+
exports.useSandbox = useSandbox;
|
|
98
|
+
if (next_shared_1.globalThisPolyfill.frameElement) {
|
|
99
|
+
//解决iframe内嵌如果iframe被移除,内部React无法回收内存的问题
|
|
100
|
+
next_shared_1.globalThisPolyfill.addEventListener('unload', function () {
|
|
101
|
+
if (sandboxRoot) {
|
|
102
|
+
sandboxRoot.unmount();
|
|
103
|
+
sandboxRoot = null;
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
var useSandboxScope = function () {
|
|
108
|
+
return next_shared_1.globalThisPolyfill['__DESIGNABLE_SANDBOX_SCOPE__'];
|
|
109
|
+
};
|
|
110
|
+
exports.useSandboxScope = useSandboxScope;
|
|
111
|
+
var renderSandboxContent = function (render) {
|
|
112
|
+
if ((0, next_shared_1.isFn)(render)) {
|
|
113
|
+
var container = document.getElementById('__SANDBOX_ROOT__');
|
|
114
|
+
if (container) {
|
|
115
|
+
if (!sandboxRoot) {
|
|
116
|
+
sandboxRoot = (0, client_1.createRoot)(container);
|
|
117
|
+
}
|
|
118
|
+
sandboxRoot.render(render((0, exports.useSandboxScope)()));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
exports.renderSandboxContent = renderSandboxContent;
|
|
123
|
+
var Sandbox = function (props) {
|
|
124
|
+
var cssAssets = props.cssAssets, jsAssets = props.jsAssets, scope = props.scope, style = props.style, iframeProps = __rest(props, ["cssAssets", "jsAssets", "scope", "style"]);
|
|
125
|
+
return react_1.default.createElement('iframe', __assign(__assign({}, iframeProps), { ref: (0, exports.useSandbox)(props), style: __assign({ height: '100%', width: '100%', border: 'none', display: 'block' }, style) }));
|
|
126
|
+
};
|
|
127
|
+
exports.Sandbox = Sandbox;
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sulesky/next-react-sandbox",
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"main": "lib",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"engines": {
|
|
8
|
+
"npm": ">=3.0.0"
|
|
9
|
+
},
|
|
10
|
+
"module": "esm",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/kapelan/designable.git"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/kapelan/designable/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/kapelan/designable#readme",
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "rimraf -rf lib esm dist && npm run build:cjs && npm run build:esm && npm run build:umd",
|
|
21
|
+
"build:cjs": "tsc --project tsconfig.build.json",
|
|
22
|
+
"build:esm": "tsc --project tsconfig.build.json --module es2015 --outDir esm",
|
|
23
|
+
"build:umd": "rollup --config rollup.config.mjs"
|
|
24
|
+
},
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"react": ">=16.8.0 || >=17.0.0 || >=18.0.0 || >=19.0.0",
|
|
27
|
+
"react-dom": ">=16.8.0 || >=17.0.0 || >=18.0.0 || >=19.0.0"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@sulesky/next-react": "1.0.3",
|
|
31
|
+
"@sulesky/next-shared": "1.0.3"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
},
|
|
36
|
+
"gitHead": "c352e7550efc1420491dfc18b9418ec4fcfcc4ae"
|
|
37
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import React, { useRef, useEffect } from 'react'
|
|
2
|
+
import { isFn, globalThisPolyfill } from '@sulesky/next-shared'
|
|
3
|
+
import {
|
|
4
|
+
useDesigner,
|
|
5
|
+
useWorkspace,
|
|
6
|
+
useLayout,
|
|
7
|
+
usePrefix,
|
|
8
|
+
} from '@sulesky/next-react'
|
|
9
|
+
import { createRoot, Root } from 'react-dom/client'
|
|
10
|
+
|
|
11
|
+
let sandboxRoot: Root | null = null
|
|
12
|
+
|
|
13
|
+
export interface ISandboxProps {
|
|
14
|
+
style?: React.CSSProperties
|
|
15
|
+
cssAssets?: string[]
|
|
16
|
+
jsAssets?: string[]
|
|
17
|
+
scope?: any
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export const useSandbox = (props: React.PropsWithChildren<ISandboxProps>) => {
|
|
21
|
+
const ref = useRef<HTMLIFrameElement>(null)
|
|
22
|
+
const appCls = usePrefix('app')
|
|
23
|
+
const designer = useDesigner()
|
|
24
|
+
const workspace = useWorkspace()
|
|
25
|
+
const layout = useLayout()
|
|
26
|
+
const cssAssets = props.cssAssets || []
|
|
27
|
+
const jsAssets = props.jsAssets || []
|
|
28
|
+
const getCSSVar = (name: string) => {
|
|
29
|
+
return getComputedStyle(
|
|
30
|
+
document.querySelector(`.${appCls}`),
|
|
31
|
+
).getPropertyValue(name)
|
|
32
|
+
}
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
if (ref.current && workspace) {
|
|
35
|
+
const styles = cssAssets
|
|
36
|
+
?.map?.((css) => {
|
|
37
|
+
return `<link media="all" rel="stylesheet" href="${css}" />`
|
|
38
|
+
})
|
|
39
|
+
.join('\n')
|
|
40
|
+
const scripts = jsAssets
|
|
41
|
+
?.map?.((js) => {
|
|
42
|
+
return `<script src="${js}" type="text/javascript" ></script>`
|
|
43
|
+
})
|
|
44
|
+
.join('\n')
|
|
45
|
+
ref.current.contentWindow['__DESIGNABLE_SANDBOX_SCOPE__'] = props.scope
|
|
46
|
+
ref.current.contentWindow['__DESIGNABLE_LAYOUT__'] = layout
|
|
47
|
+
ref.current.contentWindow['__DESIGNABLE_ENGINE__'] = designer
|
|
48
|
+
ref.current.contentWindow['__DESIGNABLE_WORKSPACE__'] = workspace
|
|
49
|
+
ref.current.contentWindow['Formily'] = globalThisPolyfill['Formily']
|
|
50
|
+
ref.current.contentWindow['Designable'] = globalThisPolyfill['Designable']
|
|
51
|
+
ref.current.contentDocument.open()
|
|
52
|
+
ref.current.contentDocument.write(`
|
|
53
|
+
<!DOCTYPE html>
|
|
54
|
+
<head>
|
|
55
|
+
${styles}
|
|
56
|
+
</head>
|
|
57
|
+
<style>
|
|
58
|
+
html{
|
|
59
|
+
overflow: overlay;
|
|
60
|
+
}
|
|
61
|
+
::-webkit-scrollbar {
|
|
62
|
+
width: 5px;
|
|
63
|
+
height: 5px;
|
|
64
|
+
}
|
|
65
|
+
::-webkit-scrollbar-thumb {
|
|
66
|
+
background-color:${getCSSVar('--dn-scrollbar-color')};
|
|
67
|
+
border-radius: 0;
|
|
68
|
+
transition: all .25s ease-in-out;
|
|
69
|
+
}
|
|
70
|
+
::-webkit-scrollbar-thumb:hover {
|
|
71
|
+
background-color: ${getCSSVar('--dn-scrollbar-hover-color')};
|
|
72
|
+
}
|
|
73
|
+
body{
|
|
74
|
+
margin:0;
|
|
75
|
+
padding:0;
|
|
76
|
+
overflow-anchor: none;
|
|
77
|
+
user-select:none;
|
|
78
|
+
background-color:${
|
|
79
|
+
layout.theme === 'light' ? '#fff' : 'transparent'
|
|
80
|
+
} !important;
|
|
81
|
+
}
|
|
82
|
+
html{
|
|
83
|
+
overflow-anchor: none;
|
|
84
|
+
}
|
|
85
|
+
.inherit-cusor * {
|
|
86
|
+
cursor: inherit !important;
|
|
87
|
+
}
|
|
88
|
+
</style>
|
|
89
|
+
<body>
|
|
90
|
+
<div id="__SANDBOX_ROOT__"></div>
|
|
91
|
+
${scripts}
|
|
92
|
+
</body>
|
|
93
|
+
</html>
|
|
94
|
+
`)
|
|
95
|
+
ref.current.contentDocument.close()
|
|
96
|
+
}
|
|
97
|
+
}, [workspace])
|
|
98
|
+
return ref
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (globalThisPolyfill.frameElement) {
|
|
102
|
+
//解决iframe内嵌如果iframe被移除,内部React无法回收内存的问题
|
|
103
|
+
globalThisPolyfill.addEventListener('unload', () => {
|
|
104
|
+
if (sandboxRoot) {
|
|
105
|
+
sandboxRoot.unmount()
|
|
106
|
+
sandboxRoot = null
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export const useSandboxScope = () => {
|
|
112
|
+
return globalThisPolyfill['__DESIGNABLE_SANDBOX_SCOPE__']
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export const renderSandboxContent = (
|
|
116
|
+
render: (scope?: any) => React.ReactNode,
|
|
117
|
+
) => {
|
|
118
|
+
if (isFn(render)) {
|
|
119
|
+
const container = document.getElementById('__SANDBOX_ROOT__')
|
|
120
|
+
if (container) {
|
|
121
|
+
if (!sandboxRoot) {
|
|
122
|
+
sandboxRoot = createRoot(container)
|
|
123
|
+
}
|
|
124
|
+
sandboxRoot.render(render(useSandboxScope()))
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export const Sandbox: React.FC<ISandboxProps> = (props) => {
|
|
130
|
+
const { cssAssets, jsAssets, scope, style, ...iframeProps } = props
|
|
131
|
+
return React.createElement('iframe', {
|
|
132
|
+
...iframeProps,
|
|
133
|
+
ref: useSandbox(props),
|
|
134
|
+
style: {
|
|
135
|
+
height: '100%',
|
|
136
|
+
width: '100%',
|
|
137
|
+
border: 'none',
|
|
138
|
+
display: 'block',
|
|
139
|
+
...style,
|
|
140
|
+
},
|
|
141
|
+
})
|
|
142
|
+
}
|