@ray-js/code-sandbox 0.0.5 → 0.0.7-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.
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { DrawerProps } from 'antd';
|
|
1
|
+
import { ButtonProps, DrawerProps } from 'antd';
|
|
2
2
|
import React from 'react';
|
|
3
|
+
import { EditorAPI } from '@saber2pr/monaco';
|
|
3
4
|
export interface CodeSandboxProps {
|
|
4
5
|
code: string;
|
|
5
6
|
title: string;
|
|
@@ -10,6 +11,9 @@ export interface CodeSandboxProps {
|
|
|
10
11
|
langKey?: string;
|
|
11
12
|
emptyText?: string;
|
|
12
13
|
isZhLanguage?: boolean;
|
|
14
|
+
editorApi?: EditorAPI;
|
|
15
|
+
cssCode?: string;
|
|
16
|
+
styMap?: any;
|
|
13
17
|
}
|
|
14
18
|
export declare const CodeSandbox: React.FC<CodeSandboxProps>;
|
|
15
19
|
export declare const checkShowCodeSandbox: (code: string) => boolean;
|
|
@@ -25,8 +29,11 @@ export interface UseCodeSandboxDrawerOps {
|
|
|
25
29
|
isZhLanguage?: boolean;
|
|
26
30
|
code: string;
|
|
27
31
|
langKey?: string;
|
|
32
|
+
editorApi?: EditorAPI;
|
|
33
|
+
cssCode?: string;
|
|
34
|
+
styMap?: any;
|
|
28
35
|
}
|
|
29
|
-
export declare const useCodeSandboxDrawer: ({ getContainer, title, height, placement, isZhLanguage, pageName, txpCode, emptyText, langKey, baseUri, code, }: UseCodeSandboxDrawerOps) => {
|
|
36
|
+
export declare const useCodeSandboxDrawer: ({ getContainer, title, height, placement, isZhLanguage, pageName, txpCode, emptyText, langKey, baseUri, code, editorApi, cssCode, styMap, }: UseCodeSandboxDrawerOps) => {
|
|
30
37
|
modal: React.JSX.Element;
|
|
31
38
|
show: boolean;
|
|
32
39
|
setShow: React.Dispatch<React.SetStateAction<boolean>>;
|
|
@@ -41,4 +48,6 @@ export declare const CodeSandboxButton: React.FC<{
|
|
|
41
48
|
baseUri: string;
|
|
42
49
|
emptyText?: string;
|
|
43
50
|
isZhLanguage?: boolean;
|
|
51
|
+
editorApi?: EditorAPI;
|
|
52
|
+
buttonProps?: ButtonProps;
|
|
44
53
|
}>;
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
import _extends from "@babel/runtime/helpers/esm/extends";
|
|
1
2
|
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
|
2
3
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
3
|
-
const _excluded = ["tooltip"];
|
|
4
|
+
const _excluded = ["tooltip", "buttonProps"];
|
|
5
|
+
/* eslint-disable react/self-closing-comp */
|
|
4
6
|
import { Button, Drawer, Tooltip } from 'antd';
|
|
5
7
|
import _ from 'lodash';
|
|
6
8
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
@@ -8,7 +10,7 @@ import { CodeSandboxOutlined } from '@ant-design/icons';
|
|
|
8
10
|
import { Demo } from '../demo';
|
|
9
11
|
import { CodeEditor } from './code-editor';
|
|
10
12
|
import { Contain, Content, DrawerContain, Main, Right } from './index.style';
|
|
11
|
-
import { postSandboxCode } from './postSandboxCode';
|
|
13
|
+
import { postSandboxCode, postSandboxStyleCode } from './postSandboxCode';
|
|
12
14
|
export const CodeSandbox = _ref => {
|
|
13
15
|
let {
|
|
14
16
|
txpCode,
|
|
@@ -19,11 +21,14 @@ export const CodeSandbox = _ref => {
|
|
|
19
21
|
baseUri,
|
|
20
22
|
langKey,
|
|
21
23
|
emptyText,
|
|
22
|
-
isZhLanguage
|
|
24
|
+
isZhLanguage,
|
|
25
|
+
editorApi,
|
|
26
|
+
cssCode,
|
|
27
|
+
styMap
|
|
23
28
|
} = _ref;
|
|
24
29
|
const iframeRef = useRef();
|
|
25
30
|
const [ts, setTs] = useState(Date.now());
|
|
26
|
-
const apiRef = useRef();
|
|
31
|
+
const apiRef = useRef(editorApi);
|
|
27
32
|
useEffect(() => {
|
|
28
33
|
const handle = event => {
|
|
29
34
|
var _event$data;
|
|
@@ -42,7 +47,23 @@ export const CodeSandbox = _ref => {
|
|
|
42
47
|
postSandboxCode(iframeRef.current, newCode, title);
|
|
43
48
|
}
|
|
44
49
|
}, 500), [show]);
|
|
45
|
-
|
|
50
|
+
const postStyleCode = useCallback(_.debounce(newCode => {
|
|
51
|
+
if (iframeRef.current && styMap) {
|
|
52
|
+
postSandboxStyleCode(iframeRef.current, newCode, styMap);
|
|
53
|
+
}
|
|
54
|
+
}, 500), [show, styMap]);
|
|
55
|
+
return /*#__PURE__*/React.createElement(Contain, null, /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(Main, null, styMap && /*#__PURE__*/React.createElement(CodeEditor, {
|
|
56
|
+
isZhLanguage: isZhLanguage,
|
|
57
|
+
baseUri: baseUri,
|
|
58
|
+
txpCode: cssCode,
|
|
59
|
+
height: `calc(90vh - 103px + 48px)`,
|
|
60
|
+
show: show,
|
|
61
|
+
title: title,
|
|
62
|
+
code: code,
|
|
63
|
+
onChange: newCode => {
|
|
64
|
+
postStyleCode(newCode);
|
|
65
|
+
}
|
|
66
|
+
}), /*#__PURE__*/React.createElement(CodeEditor, {
|
|
46
67
|
isZhLanguage: isZhLanguage,
|
|
47
68
|
baseUri: baseUri,
|
|
48
69
|
txpCode: txpCode,
|
|
@@ -86,7 +107,10 @@ export const useCodeSandboxDrawer = _ref2 => {
|
|
|
86
107
|
emptyText,
|
|
87
108
|
langKey,
|
|
88
109
|
baseUri,
|
|
89
|
-
code
|
|
110
|
+
code,
|
|
111
|
+
editorApi,
|
|
112
|
+
cssCode,
|
|
113
|
+
styMap
|
|
90
114
|
} = _ref2;
|
|
91
115
|
const [show, setShow] = useState(false);
|
|
92
116
|
return {
|
|
@@ -107,6 +131,7 @@ export const useCodeSandboxDrawer = _ref2 => {
|
|
|
107
131
|
setShow(false);
|
|
108
132
|
}
|
|
109
133
|
}, /*#__PURE__*/React.createElement(CodeSandbox, {
|
|
134
|
+
editorApi: editorApi,
|
|
110
135
|
isZhLanguage: isZhLanguage,
|
|
111
136
|
emptyText: emptyText,
|
|
112
137
|
langKey: langKey,
|
|
@@ -115,7 +140,9 @@ export const useCodeSandboxDrawer = _ref2 => {
|
|
|
115
140
|
pageName: pageName,
|
|
116
141
|
show: show,
|
|
117
142
|
title: title,
|
|
118
|
-
code: code
|
|
143
|
+
code: code,
|
|
144
|
+
styMap: styMap,
|
|
145
|
+
cssCode: cssCode
|
|
119
146
|
})),
|
|
120
147
|
show,
|
|
121
148
|
setShow
|
|
@@ -123,7 +150,8 @@ export const useCodeSandboxDrawer = _ref2 => {
|
|
|
123
150
|
};
|
|
124
151
|
export const CodeSandboxButton = _ref3 => {
|
|
125
152
|
let {
|
|
126
|
-
tooltip
|
|
153
|
+
tooltip,
|
|
154
|
+
buttonProps
|
|
127
155
|
} = _ref3,
|
|
128
156
|
props = _objectWithoutProperties(_ref3, _excluded);
|
|
129
157
|
const api = useCodeSandboxDrawer(_objectSpread(_objectSpread({}, props), {}, {
|
|
@@ -135,7 +163,7 @@ export const CodeSandboxButton = _ref3 => {
|
|
|
135
163
|
ref: ref
|
|
136
164
|
}, /*#__PURE__*/React.createElement(Tooltip, {
|
|
137
165
|
title: tooltip
|
|
138
|
-
}, /*#__PURE__*/React.createElement(Button, {
|
|
166
|
+
}, /*#__PURE__*/React.createElement(Button, _extends({
|
|
139
167
|
type: "link",
|
|
140
168
|
icon: /*#__PURE__*/React.createElement(CodeSandboxOutlined, {
|
|
141
169
|
style: {
|
|
@@ -149,5 +177,5 @@ export const CodeSandboxButton = _ref3 => {
|
|
|
149
177
|
justifyContent: 'center'
|
|
150
178
|
},
|
|
151
179
|
onClick: () => api.setShow(true)
|
|
152
|
-
})), api.modal);
|
|
180
|
+
}, buttonProps || {}))), api.modal);
|
|
153
181
|
};
|
|
@@ -38,4 +38,15 @@ export const postSandboxCode = (iframe, code, title) => {
|
|
|
38
38
|
}
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
|
+
};
|
|
42
|
+
export const postSandboxStyleCode = (iframe, code, styMap) => {
|
|
43
|
+
if (iframe) {
|
|
44
|
+
iframe.postMsg({
|
|
45
|
+
type: 'addStyle',
|
|
46
|
+
payload: {
|
|
47
|
+
code: Base64.encode(code),
|
|
48
|
+
styMap
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
41
52
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ray-js/code-sandbox",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7-beta-1",
|
|
4
4
|
"description": "小程序 CodeSandbox 容器",
|
|
5
5
|
"main": "lib/index",
|
|
6
6
|
"files": [
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
],
|
|
18
18
|
"scripts": {
|
|
19
19
|
"lint": "eslint src --ext .js,.jsx,.ts,.tsx --fix",
|
|
20
|
-
"build": "patch-package && ray build --type=component",
|
|
20
|
+
"build": "export BROWSERSLIST='Last 3 versions' && patch-package && ray build --type=component",
|
|
21
21
|
"watch": "ray start --type=component --output ./example/src/lib",
|
|
22
22
|
"build:tuya": "ray build ./example",
|
|
23
23
|
"build:wechat": "ray build ./example --target=wechat",
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
"start:tuya": "ray start ./example",
|
|
28
28
|
"start:wechat": "ray start ./example -t wechat --verbose",
|
|
29
29
|
"start:web": "ray start ./example -t web",
|
|
30
|
-
"prepublishOnly": "yarn build",
|
|
31
30
|
"release-it": "standard-version"
|
|
32
31
|
},
|
|
33
32
|
"peerDependencies": {
|
|
@@ -70,4 +69,4 @@
|
|
|
70
69
|
"git add"
|
|
71
70
|
]
|
|
72
71
|
}
|
|
73
|
-
}
|
|
72
|
+
}
|