@ray-js/code-sandbox 0.0.7-beta-3 → 0.0.7-beta-5

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.
@@ -8,6 +8,9 @@ export interface CodeEditorProps {
8
8
  txpCode: string;
9
9
  baseUri: string;
10
10
  isZhLanguage: boolean;
11
+ style?: React.CSSProperties;
12
+ fileName?: string;
13
+ cssParse?: (css: string) => Promise<string>;
11
14
  }
12
15
  export declare const CodeEditor: React.ForwardRefExoticComponent<CodeEditorProps & React.RefAttributes<{
13
16
  monaco: typeof import("monaco-editor");
@@ -1,4 +1,5 @@
1
- import { Spin } from 'antd';
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import { Skeleton } from 'antd';
2
3
  import React, { useEffect, useImperativeHandle, useRef, useState } from 'react';
3
4
  import { getComponentResource } from '../../services';
4
5
  import { Editor } from '@saber2pr/monaco';
@@ -15,7 +16,10 @@ export const CodeEditor = /*#__PURE__*/React.forwardRef((_ref, parentRef) => {
15
16
  height,
16
17
  txpCode,
17
18
  baseUri,
18
- isZhLanguage = true
19
+ isZhLanguage = true,
20
+ style,
21
+ fileName = 'main.tsx',
22
+ cssParse
19
23
  } = _ref;
20
24
  const [initloading, setInitLoading] = useState(true);
21
25
  const ref = useRef();
@@ -23,8 +27,13 @@ export const CodeEditor = /*#__PURE__*/React.forwardRef((_ref, parentRef) => {
23
27
  ref.current = editor;
24
28
  editor.setValue(`${code}`);
25
29
  editor.getModel().onDidChangeContent(async () => {
26
- const result = await editor.compileTS();
27
- onChange(result.output);
30
+ if (/\.(tsx|ts|jsx|js)$/.test(fileName)) {
31
+ const result = await editor.compileTS();
32
+ onChange(result.output);
33
+ } else {
34
+ const cssCode = editor.getValue(fileName);
35
+ onChange(cssParse ? await cssParse(cssCode) : cssCode);
36
+ }
28
37
  });
29
38
  const addTypeLib = async (name, wrapName) => {
30
39
  try {
@@ -80,8 +89,13 @@ export const CodeEditor = /*#__PURE__*/React.forwardRef((_ref, parentRef) => {
80
89
  await timeout(500);
81
90
  }
82
91
  if (editor) {
83
- const result = await editor.compileTS();
84
- onChange(result.output);
92
+ if (/\.(tsx|ts|jsx|js)$/.test(fileName)) {
93
+ const result = await editor.compileTS();
94
+ onChange(result.output);
95
+ } else {
96
+ const cssCode = editor.getValue(fileName);
97
+ onChange(cssParse ? await cssParse(cssCode) : cssCode);
98
+ }
85
99
  }
86
100
  }
87
101
  };
@@ -89,7 +103,7 @@ export const CodeEditor = /*#__PURE__*/React.forwardRef((_ref, parentRef) => {
89
103
  return () => {
90
104
  window.removeEventListener('message', handle);
91
105
  };
92
- }, [show]);
106
+ }, [show, fileName]);
93
107
  useEffect(() => {
94
108
  const destroyOnClose = () => {
95
109
  const api = ref.current;
@@ -121,17 +135,12 @@ export const CodeEditor = /*#__PURE__*/React.forwardRef((_ref, parentRef) => {
121
135
  }
122
136
  };
123
137
  }
124
- return /*#__PURE__*/React.createElement(Spin, {
125
- spinning: initloading,
126
- style: {
127
- flexGrow: 1
128
- }
129
- }, code && /*#__PURE__*/React.createElement(Editor, {
138
+ return /*#__PURE__*/React.createElement(React.Fragment, null, code && /*#__PURE__*/React.createElement(Editor, {
130
139
  loaderConfig: loaderConfig,
131
140
  theme: "monokai-bright",
132
- style: {
141
+ style: _objectSpread(_objectSpread({}, style || {}), {}, {
133
142
  height
134
- },
143
+ }),
135
144
  options: {
136
145
  minimap: {
137
146
  enabled: false
@@ -140,7 +149,7 @@ export const CodeEditor = /*#__PURE__*/React.forwardRef((_ref, parentRef) => {
140
149
  },
141
150
  deps: [title, code],
142
151
  modalFiles: {
143
- 'main.tsx': ''
152
+ [fileName]: ''
144
153
  },
145
154
  tsconfig: {
146
155
  strict: false,
@@ -155,5 +164,16 @@ export const CodeEditor = /*#__PURE__*/React.forwardRef((_ref, parentRef) => {
155
164
  initMonaco(editor);
156
165
  setInitLoading(false);
157
166
  }
158
- }));
167
+ }), initloading && /*#__PURE__*/React.createElement("div", {
168
+ style: _objectSpread(_objectSpread({
169
+ position: 'absolute',
170
+ zIndex: 10
171
+ }, style || {}), {}, {
172
+ height
173
+ })
174
+ }, /*#__PURE__*/React.createElement(Skeleton, {
175
+ active: true,
176
+ title: true,
177
+ paragraph: true
178
+ })));
159
179
  });
@@ -14,6 +14,7 @@ export interface CodeSandboxProps {
14
14
  editorApi?: EditorAPI;
15
15
  cssCode?: string;
16
16
  styMap?: any;
17
+ cssParse?: (css: string) => Promise<string>;
17
18
  }
18
19
  export declare const CodeSandbox: React.FC<CodeSandboxProps>;
19
20
  export declare const checkShowCodeSandbox: (code: string) => boolean;
@@ -32,8 +33,10 @@ export interface UseCodeSandboxDrawerOps {
32
33
  editorApi?: EditorAPI;
33
34
  cssCode?: string;
34
35
  styMap?: any;
36
+ fileName?: string;
37
+ cssParse?: (css: string) => Promise<string>;
35
38
  }
36
- export declare const useCodeSandboxDrawer: ({ getContainer, title, height, placement, isZhLanguage, pageName, txpCode, emptyText, langKey, baseUri, code, editorApi, cssCode, styMap, }: UseCodeSandboxDrawerOps) => {
39
+ export declare const useCodeSandboxDrawer: ({ getContainer, title, height, placement, isZhLanguage, pageName, txpCode, emptyText, langKey, baseUri, code, editorApi, cssCode, styMap, cssParse, }: UseCodeSandboxDrawerOps) => {
37
40
  modal: React.JSX.Element;
38
41
  show: boolean;
39
42
  setShow: React.Dispatch<React.SetStateAction<boolean>>;
@@ -9,7 +9,7 @@ import React, { useCallback, useEffect, useRef, useState } from 'react';
9
9
  import { CodeSandboxOutlined } from '@ant-design/icons';
10
10
  import { Demo } from '../demo';
11
11
  import { CodeEditor } from './code-editor';
12
- import { Contain, Content, DrawerContain, Main, Right } from './index.style';
12
+ import { Contain, Content, DrawerContain, Main, MainContent, MainHeader, MainHeaderItem, Right } from './index.style';
13
13
  import { postSandboxCode, postSandboxStyleCode } from './postSandboxCode';
14
14
  export const CodeSandbox = _ref => {
15
15
  let {
@@ -24,7 +24,8 @@ export const CodeSandbox = _ref => {
24
24
  isZhLanguage,
25
25
  editorApi,
26
26
  cssCode,
27
- styMap
27
+ styMap,
28
+ cssParse
28
29
  } = _ref;
29
30
  const iframeRef = useRef();
30
31
  const [ts, setTs] = useState(Date.now());
@@ -52,18 +53,38 @@ export const CodeSandbox = _ref => {
52
53
  postSandboxStyleCode(iframeRef.current, newCode, styMap);
53
54
  }
54
55
  }, 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
+ return /*#__PURE__*/React.createElement(Contain, null, /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(Main, null, styMap ? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(MainHeader, null, /*#__PURE__*/React.createElement(MainHeaderItem, null, "CSS Style Sheet"), /*#__PURE__*/React.createElement(MainHeaderItem, null, "Js Code")), /*#__PURE__*/React.createElement(MainContent, null, /*#__PURE__*/React.createElement(CodeEditor, {
56
57
  isZhLanguage: isZhLanguage,
57
58
  baseUri: baseUri,
58
- txpCode: cssCode,
59
+ txpCode: txpCode,
59
60
  height: `calc(90vh - 103px + 48px)`,
61
+ style: {
62
+ width: '50%'
63
+ },
60
64
  show: show,
61
65
  title: title,
62
- code: code,
66
+ fileName: "main.less",
67
+ code: cssCode,
63
68
  onChange: newCode => {
64
69
  postStyleCode(newCode);
65
- }
70
+ },
71
+ cssParse: cssParse
66
72
  }), /*#__PURE__*/React.createElement(CodeEditor, {
73
+ isZhLanguage: isZhLanguage,
74
+ baseUri: baseUri,
75
+ txpCode: txpCode,
76
+ height: `calc(90vh - 103px + 48px)`,
77
+ style: {
78
+ width: '50%'
79
+ },
80
+ ref: apiRef,
81
+ show: show,
82
+ title: title,
83
+ code: code,
84
+ onChange: newCode => {
85
+ postCode(newCode);
86
+ }
87
+ }))) : /*#__PURE__*/React.createElement(CodeEditor, {
67
88
  isZhLanguage: isZhLanguage,
68
89
  baseUri: baseUri,
69
90
  txpCode: txpCode,
@@ -110,7 +131,8 @@ export const useCodeSandboxDrawer = _ref2 => {
110
131
  code,
111
132
  editorApi,
112
133
  cssCode,
113
- styMap
134
+ styMap,
135
+ cssParse
114
136
  } = _ref2;
115
137
  const [show, setShow] = useState(false);
116
138
  return {
@@ -142,7 +164,8 @@ export const useCodeSandboxDrawer = _ref2 => {
142
164
  title: title,
143
165
  code: code,
144
166
  styMap: styMap,
145
- cssCode: cssCode
167
+ cssCode: cssCode,
168
+ cssParse: cssParse
146
169
  })),
147
170
  show,
148
171
  setShow
@@ -3,5 +3,8 @@ export declare const Contain: import("styled-components/dist/types").IStyledComp
3
3
  export declare const Bottom: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
4
4
  export declare const Content: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
5
5
  export declare const Main: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
6
+ export declare const MainHeader: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
7
+ export declare const MainHeaderItem: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
8
+ export declare const MainContent: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
6
9
  export declare const Right: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
7
10
  export declare const DrawerContain: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
@@ -14,6 +14,20 @@ export const Content = styled.div`
14
14
  `;
15
15
  export const Main = styled.div`
16
16
  flex-grow: 1;
17
+ `;
18
+ export const MainHeader = styled.div`
19
+ display: flex;
20
+ border-bottom: 1px solid #0078d4;
21
+ `;
22
+ export const MainHeaderItem = styled.div`
23
+ background-color: #1f1f1f;
24
+ font-size: 16px;
25
+ font-weight: bold;
26
+ width: 50%;
27
+ color: #e2e2e2;
28
+ padding: 0 28px;
29
+ `;
30
+ export const MainContent = styled.div`
17
31
  display: flex;
18
32
  `;
19
33
  export const Right = styled.div`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/code-sandbox",
3
- "version": "0.0.7-beta-3",
3
+ "version": "0.0.7-beta-5",
4
4
  "description": "小程序 CodeSandbox 容器",
5
5
  "main": "lib/index",
6
6
  "files": [