@ray-js/code-sandbox 0.0.2

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.
Files changed (48) hide show
  1. package/README.md +121 -0
  2. package/lib/catch-error/index.d.ts +15 -0
  3. package/lib/catch-error/index.js +25 -0
  4. package/lib/codesandbox/index.d.ts +7 -0
  5. package/lib/codesandbox/index.js +50 -0
  6. package/lib/codesandbox/index.module.less +18 -0
  7. package/lib/decoder/index.d.ts +7 -0
  8. package/lib/decoder/index.js +17 -0
  9. package/lib/index.d.ts +6 -0
  10. package/lib/index.js +6 -0
  11. package/lib/listener/index.css +1 -0
  12. package/lib/listener/index.d.ts +10 -0
  13. package/lib/listener/index.js +5 -0
  14. package/lib/listener/index.json +4 -0
  15. package/lib/listener/index.sjs +8 -0
  16. package/lib/listener/index.tyml +6 -0
  17. package/lib/loading/index.d.ts +5 -0
  18. package/lib/loading/index.js +22 -0
  19. package/lib/loading/index.module.less +8 -0
  20. package/lib/txp/codesandbox/code-editor/index.d.ts +42 -0
  21. package/lib/txp/codesandbox/code-editor/index.js +152 -0
  22. package/lib/txp/codesandbox/index.d.ts +19 -0
  23. package/lib/txp/codesandbox/index.js +121 -0
  24. package/lib/txp/codesandbox/index.style.d.ts +7 -0
  25. package/lib/txp/codesandbox/index.style.js +28 -0
  26. package/lib/txp/codesandbox/postSandboxCode.d.ts +2 -0
  27. package/lib/txp/codesandbox/postSandboxCode.js +36 -0
  28. package/lib/txp/demo/index.d.ts +13 -0
  29. package/lib/txp/demo/index.js +54 -0
  30. package/lib/txp/demo/index.style.d.ts +2 -0
  31. package/lib/txp/demo/index.style.js +14 -0
  32. package/lib/txp/hooks/useAsync.d.ts +14 -0
  33. package/lib/txp/hooks/useAsync.js +37 -0
  34. package/lib/txp/hooks/useTestUrl.d.ts +1 -0
  35. package/lib/txp/hooks/useTestUrl.js +12 -0
  36. package/lib/txp/index.d.ts +3 -0
  37. package/lib/txp/index.js +3 -0
  38. package/lib/txp/sandbox/index.d.ts +7 -0
  39. package/lib/txp/sandbox/index.js +32 -0
  40. package/lib/txp/sandbox/index.style.d.ts +17 -0
  41. package/lib/txp/sandbox/index.style.js +20 -0
  42. package/lib/txp/services/index.d.ts +4 -0
  43. package/lib/txp/services/index.js +12 -0
  44. package/lib/txp/services/interface.d.ts +9 -0
  45. package/lib/txp/services/interface.js +1 -0
  46. package/lib/txp/utils/theme-vars.d.ts +1032 -0
  47. package/lib/txp/utils/theme-vars.js +1032 -0
  48. package/package.json +77 -0
@@ -0,0 +1,121 @@
1
+ import { Button, Drawer, Tooltip } from 'antd';
2
+ import _ from 'lodash';
3
+ import React, { useCallback, useEffect, useRef, useState } from 'react';
4
+ import { CodeSandboxOutlined } from '@ant-design/icons';
5
+ import { Demo } from '../demo';
6
+ import { CodeEditor } from './code-editor';
7
+ import { Contain, Content, DrawerContain, Main, Right } from './index.style';
8
+ import { postSandboxCode } from './postSandboxCode';
9
+ export const CodeSandbox = _ref => {
10
+ let {
11
+ txpCode,
12
+ code,
13
+ title,
14
+ show,
15
+ pageName = 'playground',
16
+ baseUri
17
+ } = _ref;
18
+ const iframeRef = useRef();
19
+ const [ts, setTs] = useState(Date.now());
20
+ const apiRef = useRef();
21
+ useEffect(() => {
22
+ const handle = event => {
23
+ var _event$data;
24
+ const type = event === null || event === void 0 || (_event$data = event.data) === null || _event$data === void 0 ? void 0 : _event$data.type;
25
+ if (type === 'sandbox-reload') {
26
+ setTs(Date.now());
27
+ }
28
+ };
29
+ window.addEventListener('message', handle);
30
+ return () => {
31
+ window.removeEventListener('message', handle);
32
+ };
33
+ }, [show]);
34
+ const postCode = useCallback(_.debounce(newCode => {
35
+ if (iframeRef.current) {
36
+ postSandboxCode(iframeRef.current, newCode, title);
37
+ }
38
+ }, 500), [show]);
39
+ return /*#__PURE__*/React.createElement(Contain, null, /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(Main, null, /*#__PURE__*/React.createElement(CodeEditor, {
40
+ baseUri: baseUri,
41
+ txpCode: txpCode,
42
+ height: `calc(90vh - 103px + 48px)`,
43
+ ref: apiRef,
44
+ show: show,
45
+ title: title,
46
+ code: code,
47
+ onChange: newCode => {
48
+ postCode(newCode);
49
+ }
50
+ })), /*#__PURE__*/React.createElement(Right, null, /*#__PURE__*/React.createElement(Demo, {
51
+ baseUri: baseUri,
52
+ txpCode: txpCode,
53
+ ts: ts,
54
+ ref: iframeRef,
55
+ comId: pageName,
56
+ style: {
57
+ boxShadow: '0 8px 24px -2px rgb(0 0 0 / 26%)',
58
+ minWidth: 300
59
+ }
60
+ }))));
61
+ };
62
+ export const checkShowCodeSandbox = code => {
63
+ if (code) {
64
+ return code.includes('import') && code.includes('from') && code.includes('export default');
65
+ }
66
+ return false;
67
+ };
68
+ export const CodeSandboxButton = _ref2 => {
69
+ let {
70
+ code,
71
+ title,
72
+ txpCode,
73
+ tooltip = '在线编辑预览效果!',
74
+ pageName,
75
+ baseUri
76
+ } = _ref2;
77
+ const [show, setShow] = useState(false);
78
+ const ref = useRef();
79
+ return /*#__PURE__*/React.createElement(DrawerContain, {
80
+ ref: ref
81
+ }, /*#__PURE__*/React.createElement(Tooltip, {
82
+ title: tooltip
83
+ }, /*#__PURE__*/React.createElement(Button, {
84
+ type: "link",
85
+ icon: /*#__PURE__*/React.createElement(CodeSandboxOutlined, {
86
+ style: {
87
+ width: 14,
88
+ height: 14
89
+ }
90
+ }),
91
+ style: {
92
+ display: 'flex',
93
+ alignItems: 'center',
94
+ justifyContent: 'center'
95
+ },
96
+ onClick: () => setShow(true)
97
+ })), /*#__PURE__*/React.createElement(Drawer, {
98
+ getContainer: () => ref.current,
99
+ title: `${title}`,
100
+ height: "90vh",
101
+ contentWrapperStyle: {
102
+ padding: 0
103
+ },
104
+ style: {
105
+ padding: 0
106
+ },
107
+ placement: "bottom",
108
+ destroyOnClose: true,
109
+ open: show,
110
+ onClose: () => {
111
+ setShow(false);
112
+ }
113
+ }, /*#__PURE__*/React.createElement(CodeSandbox, {
114
+ baseUri: baseUri,
115
+ txpCode: txpCode,
116
+ pageName: pageName,
117
+ show: show,
118
+ title: title,
119
+ code: code
120
+ })));
121
+ };
@@ -0,0 +1,7 @@
1
+ /// <reference types="react" />
2
+ export declare const Contain: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
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
+ 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
+ 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 Right: 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 DrawerContain: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
@@ -0,0 +1,28 @@
1
+ import styled from 'styled-components';
2
+ export const Contain = styled.div`
3
+ position: relative;
4
+ height: 100%;
5
+ `;
6
+ export const Bottom = styled.div`
7
+ border-right: 1px solid #efefef;
8
+ `;
9
+ export const Content = styled.div`
10
+ display: flex;
11
+ flex-direction: row;
12
+ flex-grow: 1;
13
+ height: 100%;
14
+ `;
15
+ export const Main = styled.div`
16
+ flex-grow: 1;
17
+ `;
18
+ export const Right = styled.div`
19
+ display: flex;
20
+ align-items: center;
21
+ justify-content: center;
22
+ width: 400px;
23
+ `;
24
+ export const DrawerContain = styled.div`
25
+ .ant-drawer-body {
26
+ padding: 0;
27
+ }
28
+ `;
@@ -0,0 +1,2 @@
1
+ import { DemoRef } from '../demo';
2
+ export declare const postSandboxCode: (iframe: DemoRef, code: string, title: string) => void;
@@ -0,0 +1,36 @@
1
+ import * as Base64 from 'js-base64';
2
+ export const postSandboxCode = (iframe, code, title) => {
3
+ if (iframe) {
4
+ iframe.postMsg({
5
+ type: 'addJs',
6
+ payload: {
7
+ code: Base64.encode(`RJSEventChannel.emit('sandbox-jsrun', '${Base64.encode(`
8
+ var exports = {};
9
+ function require(id) {
10
+ if(typeof id === 'string' && id.startsWith('@tuya-miniapp/icons/')) {
11
+ const comName = id.split('/').pop()
12
+ return __ctx__['@tuya-miniapp/icons'][comName]
13
+ }
14
+ return __ctx__[id]
15
+ };
16
+ var React = __ctx__.React;
17
+ try {
18
+ ${code};
19
+ __ctx__.___setTitle___("${title}");
20
+ __ctx__.___setContent___(__ctx__.react.createElement(__ctx__.__loading__));
21
+ setTimeout(() => {
22
+ if(exports.default) {
23
+ const childrenNode = __ctx__.react.createElement(exports.default)
24
+ if(__ctx__.React.isValidElement(childrenNode)) {
25
+ __ctx__.___setContent___(childrenNode);
26
+ }
27
+ }
28
+ }, 500)
29
+ } catch (e) {
30
+ console.log(e)
31
+ };
32
+ `)}')`)
33
+ }
34
+ });
35
+ }
36
+ };
@@ -0,0 +1,13 @@
1
+ import React from 'react';
2
+ export interface DemoProps {
3
+ comId?: string;
4
+ onInit?(el: HTMLIFrameElement): void;
5
+ style?: React.CSSProperties;
6
+ ts?: number;
7
+ txpCode: string;
8
+ baseUri: string;
9
+ }
10
+ export interface DemoRef {
11
+ postMsg(data: any): void;
12
+ }
13
+ export declare const Demo: React.ForwardRefExoticComponent<DemoProps & React.RefAttributes<DemoRef>>;
@@ -0,0 +1,54 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import React, { useImperativeHandle, useRef } from 'react';
3
+ import { Sandbox } from '../sandbox';
4
+ import { themeVars } from '../utils/theme-vars';
5
+ import { Contain } from './index.style';
6
+ import { createCompnentUri } from '../services';
7
+ export const Demo = /*#__PURE__*/React.forwardRef((_ref, parentRef) => {
8
+ let {
9
+ comId,
10
+ onInit,
11
+ style,
12
+ ts,
13
+ txpCode,
14
+ baseUri
15
+ } = _ref;
16
+ let path = `${createCompnentUri(baseUri, txpCode, `index.html`)}?t=${ts || Date.now()}`;
17
+ path += `#!/pages/${comId}/index`;
18
+ const ref = useRef();
19
+ useImperativeHandle(parentRef, () => ({
20
+ postMsg: data => {
21
+ const el = ref.current;
22
+ if (el) {
23
+ el.contentWindow.window.postMessage(data, '*');
24
+ }
25
+ }
26
+ }), [comId]);
27
+ const sendMessage = (type, payload) => {
28
+ const el = ref.current;
29
+ if (el) {
30
+ el.contentWindow.window.postMessage({
31
+ type,
32
+ payload
33
+ }, '*');
34
+ }
35
+ };
36
+ return /*#__PURE__*/React.createElement(Contain, {
37
+ style: style
38
+ }, /*#__PURE__*/React.createElement(Sandbox, {
39
+ ref: el => {
40
+ if (el) {
41
+ ref.current = el;
42
+ sendMessage('theme', {
43
+ theme: 'light',
44
+ vars: _objectSpread({}, themeVars['light'] || {})
45
+ });
46
+ onInit && onInit(el);
47
+ }
48
+ },
49
+ src: path,
50
+ style: {
51
+ borderRadius: 0
52
+ }
53
+ }));
54
+ });
@@ -0,0 +1,2 @@
1
+ /// <reference types="react" />
2
+ export declare const Contain: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, never>> & string;
@@ -0,0 +1,14 @@
1
+ import styled from 'styled-components';
2
+ export const Contain = styled.div`
3
+ position: -webkit-sticky;
4
+ position: sticky;
5
+ top: 0;
6
+ overflow-y: auto;
7
+ display: flex;
8
+ flex-direction: column;
9
+ border-radius: 32px;
10
+ overflow: hidden;
11
+ background-color: #fff;
12
+ border-radius: 8px;
13
+ padding: 8px;
14
+ `;
@@ -0,0 +1,14 @@
1
+ export type UseAsyncOps<T> = {
2
+ manual: boolean;
3
+ onSuccess?(result?: T): any;
4
+ onError?(err?: Error): any;
5
+ };
6
+ export type UseAsyncResult<A extends any[], T> = {
7
+ data: T;
8
+ loading: boolean;
9
+ setData(data: T): void;
10
+ run: (...args: A) => Promise<T>;
11
+ };
12
+ export declare function useAsync<A extends any[], T = any>(run: (...args: A) => Promise<T>): UseAsyncResult<A, T>;
13
+ export declare function useAsync<A extends any[], T = any>(run: (...args: A) => Promise<T>, deps: any[]): UseAsyncResult<A, T>;
14
+ export declare function useAsync<A extends any[], T = any>(run: (...args: A) => Promise<T>, deps: any[], ops: UseAsyncOps<T>): UseAsyncResult<A, T>;
@@ -0,0 +1,37 @@
1
+ import { useEffect, useState } from 'react';
2
+ export function useAsync(run) {
3
+ let deps = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
4
+ let ops = arguments.length > 2 ? arguments[2] : undefined;
5
+ const [data, setData] = useState();
6
+ const [loading, setLoading] = useState();
7
+ const main = async function () {
8
+ try {
9
+ setLoading(true);
10
+ const result = await run(...arguments);
11
+ if (ops !== null && ops !== void 0 && ops.onSuccess) {
12
+ await (ops === null || ops === void 0 ? void 0 : ops.onSuccess(result));
13
+ }
14
+ setData(result);
15
+ return result;
16
+ } catch (error) {
17
+ if (ops !== null && ops !== void 0 && ops.onError) {
18
+ await (ops === null || ops === void 0 ? void 0 : ops.onError(error));
19
+ } else {
20
+ console.log(`[useAsync] error`, error);
21
+ }
22
+ } finally {
23
+ setLoading(false);
24
+ }
25
+ };
26
+ useEffect(() => {
27
+ if (!(ops !== null && ops !== void 0 && ops.manual)) {
28
+ main();
29
+ }
30
+ }, [ops === null || ops === void 0 ? void 0 : ops.manual, ...deps]);
31
+ return {
32
+ data,
33
+ loading,
34
+ run: main,
35
+ setData
36
+ };
37
+ }
@@ -0,0 +1 @@
1
+ export declare const useTestUri: (src: string) => import("./useAsync").UseAsyncResult<[], boolean | undefined>;
@@ -0,0 +1,12 @@
1
+ import { useAsync } from './useAsync';
2
+ import axios from 'axios';
3
+ export const useTestUri = src => useAsync(async () => {
4
+ if (src) {
5
+ try {
6
+ await axios.get(src);
7
+ return true;
8
+ } catch (error) {
9
+ return false;
10
+ }
11
+ }
12
+ }, [src]);
@@ -0,0 +1,3 @@
1
+ import { CodeSandboxButton, CodeSandbox } from './codesandbox';
2
+ export * from './codesandbox';
3
+ export { CodeSandbox, CodeSandboxButton };
@@ -0,0 +1,3 @@
1
+ import { CodeSandboxButton, CodeSandbox } from './codesandbox';
2
+ export * from './codesandbox';
3
+ export { CodeSandbox, CodeSandboxButton };
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ export interface SandboxProps {
3
+ src: string;
4
+ style?: React.CSSProperties;
5
+ sandboxId?: string;
6
+ }
7
+ export declare const Sandbox: React.ForwardRefExoticComponent<SandboxProps & React.RefAttributes<HTMLIFrameElement>>;
@@ -0,0 +1,32 @@
1
+ import { useTestUri } from '../hooks/useTestUrl';
2
+ import React from 'react';
3
+ import { SandboxEmpty, SandboxFrame, SandboxSkeleton } from './index.style';
4
+ export const Sandbox = /*#__PURE__*/React.forwardRef((_ref, ref) => {
5
+ let {
6
+ src,
7
+ style,
8
+ sandboxId
9
+ } = _ref;
10
+ const {
11
+ data: hasUri,
12
+ loading
13
+ } = useTestUri(src);
14
+ let content = /*#__PURE__*/React.createElement(SandboxEmpty, {
15
+ description: "\u6682\u65E0\u6F14\u793A"
16
+ });
17
+ if (loading) {
18
+ content = /*#__PURE__*/React.createElement(SandboxSkeleton, {
19
+ paragraph: {
20
+ rows: 12
21
+ }
22
+ });
23
+ } else if (hasUri) {
24
+ content = /*#__PURE__*/React.createElement(SandboxFrame, {
25
+ id: sandboxId,
26
+ style: style,
27
+ ref: ref,
28
+ src: src
29
+ });
30
+ }
31
+ return content;
32
+ });
@@ -0,0 +1,17 @@
1
+ /// <reference types="react" />
2
+ export declare const SandboxFrame: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("react").DetailedHTMLProps<import("react").IframeHTMLAttributes<HTMLIFrameElement>, HTMLIFrameElement>, never>> & string;
3
+ export declare const SandboxEmpty: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("antd").EmptyProps & {
4
+ children?: import("react").ReactNode;
5
+ }, never>> & string & Omit<import("react").FC<import("antd").EmptyProps> & {
6
+ PRESENTED_IMAGE_DEFAULT: import("react").ReactNode;
7
+ PRESENTED_IMAGE_SIMPLE: import("react").ReactNode;
8
+ }, keyof import("react").Component<any, {}, any>>;
9
+ export declare const SandboxSkeleton: import("styled-components/dist/types").IStyledComponentBase<"web", import("styled-components").FastOmit<import("antd").SkeletonProps & {
10
+ children?: import("react").ReactNode;
11
+ }, never>> & string & Omit<import("react").FC<import("antd").SkeletonProps> & {
12
+ Button: import("react").FC<import("antd/es/skeleton/Button").SkeletonButtonProps>;
13
+ Avatar: import("react").FC<import("antd/es/skeleton/Avatar").AvatarProps>;
14
+ Input: import("react").FC<import("antd/es/skeleton/Input").SkeletonInputProps>;
15
+ Image: import("react").FC<import("antd/es/skeleton/Image").SkeletonImageProps>;
16
+ Node: import("react").FC<import("antd/es/skeleton/Node").SkeletonNodeProps>;
17
+ }, keyof import("react").Component<any, {}, any>>;
@@ -0,0 +1,20 @@
1
+ import { Empty, Skeleton } from 'antd';
2
+ import styled from 'styled-components';
3
+ export const SandboxFrame = styled.iframe`
4
+ border: 0;
5
+ width: 100%;
6
+ height: 522px;
7
+ border-bottom-left-radius: 32px;
8
+ border-bottom-right-radius: 32px;
9
+ `;
10
+ export const SandboxEmpty = styled(Empty)`
11
+ border: 0;
12
+ width: 100%;
13
+ height: 522px;
14
+ padding-top: 140px;
15
+ `;
16
+ export const SandboxSkeleton = styled(Skeleton)`
17
+ border: 0;
18
+ width: 100%;
19
+ height: 522px;
20
+ `;
@@ -0,0 +1,4 @@
1
+ import { Components } from './interface';
2
+ export declare const getComponents: (baseUri: string, src: string) => Promise<Components>;
3
+ export declare const createCompnentUri: (baseUri: string, code: string, name: string) => string;
4
+ export declare const getComponentResource: <T = any>(baseUri: string, src: string, readmeName: string) => Promise<T>;
@@ -0,0 +1,12 @@
1
+ import axios from 'axios';
2
+ export const getComponents = async (baseUri, src) => {
3
+ const res = await getComponentResource(baseUri, src, 'components.json');
4
+ return res;
5
+ };
6
+ export const createCompnentUri = (baseUri, code, name) => {
7
+ return `${baseUri}/static/txp-ray-${code}/${name}`;
8
+ };
9
+ export const getComponentResource = async (baseUri, src, readmeName) => {
10
+ const res = await axios.get(createCompnentUri(baseUri, src, `${readmeName}`) + `?t=${Date.now()}`);
11
+ return res.data;
12
+ };
@@ -0,0 +1,9 @@
1
+ export interface Components {
2
+ [name: string]: {
3
+ props: string;
4
+ name: string;
5
+ updateTime: number;
6
+ readmeName: string;
7
+ category: string;
8
+ };
9
+ }
@@ -0,0 +1 @@
1
+ export {};