@yimoka/designable-react 0.0.0-stage → 0.1.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.
Files changed (142) hide show
  1. package/README.md +6 -2
  2. package/dist/src/adapter/UIAdapterProvider.d.ts +7 -0
  3. package/dist/src/adapter/UIAdapterProvider.js +10 -0
  4. package/dist/src/adapter/context-core.d.ts +2 -0
  5. package/dist/src/adapter/context-core.js +3 -0
  6. package/dist/src/adapter/context.d.ts +6 -0
  7. package/dist/src/adapter/context.js +9 -0
  8. package/dist/src/adapter/default-components.d.ts +8 -0
  9. package/dist/src/adapter/default-components.js +38 -0
  10. package/dist/src/adapter/default.d.ts +2 -0
  11. package/dist/src/adapter/default.js +9 -0
  12. package/dist/src/adapter/index.d.ts +3 -0
  13. package/dist/src/adapter/index.js +3 -0
  14. package/dist/src/adapter/types.d.ts +63 -0
  15. package/dist/src/adapter/types.js +1 -0
  16. package/dist/src/containers/Designer.d.ts +3 -0
  17. package/dist/src/containers/Designer.js +63 -0
  18. package/dist/src/containers/Sandbox.d.ts +4 -0
  19. package/dist/src/containers/Sandbox.js +3 -0
  20. package/dist/src/containers/SandboxComponent.d.ts +3 -0
  21. package/dist/src/containers/SandboxComponent.js +15 -0
  22. package/dist/src/containers/Simulator.d.ts +3 -0
  23. package/dist/src/containers/Simulator.js +70 -0
  24. package/dist/src/containers/Viewport.d.ts +6 -0
  25. package/dist/src/containers/Viewport.js +71 -0
  26. package/dist/src/containers/Workbench.d.ts +2 -0
  27. package/dist/src/containers/Workbench.js +9 -0
  28. package/dist/src/containers/Workspace.d.ts +7 -0
  29. package/dist/src/containers/Workspace.js +39 -0
  30. package/dist/src/containers/index.d.ts +6 -0
  31. package/dist/src/containers/index.js +6 -0
  32. package/dist/src/containers/sandbox-types.d.ts +7 -0
  33. package/dist/src/containers/sandbox-types.js +1 -0
  34. package/dist/src/containers/sandbox-utils.d.ts +6 -0
  35. package/dist/src/containers/sandbox-utils.js +40 -0
  36. package/dist/src/containers/useSandbox.d.ts +3 -0
  37. package/dist/src/containers/useSandbox.js +77 -0
  38. package/dist/src/context.d.ts +7 -0
  39. package/dist/src/context.js +6 -0
  40. package/dist/src/hooks/index.d.ts +26 -0
  41. package/dist/src/hooks/index.js +26 -0
  42. package/dist/src/hooks/useComponents.d.ts +2 -0
  43. package/dist/src/hooks/useComponents.js +3 -0
  44. package/dist/src/hooks/useCurrentNode.d.ts +2 -0
  45. package/dist/src/hooks/useCurrentNode.js +7 -0
  46. package/dist/src/hooks/useCurrentNodeSelected.d.ts +1 -0
  47. package/dist/src/hooks/useCurrentNodeSelected.js +7 -0
  48. package/dist/src/hooks/useCursor.d.ts +2 -0
  49. package/dist/src/hooks/useCursor.js +5 -0
  50. package/dist/src/hooks/useDesigner.d.ts +3 -0
  51. package/dist/src/hooks/useDesigner.js +15 -0
  52. package/dist/src/hooks/useHistory.d.ts +2 -0
  53. package/dist/src/hooks/useHistory.js +5 -0
  54. package/dist/src/hooks/useHover.d.ts +2 -0
  55. package/dist/src/hooks/useHover.js +5 -0
  56. package/dist/src/hooks/useLayout.d.ts +2 -0
  57. package/dist/src/hooks/useLayout.js +8 -0
  58. package/dist/src/hooks/useNodeIdProps.d.ts +2 -0
  59. package/dist/src/hooks/useNodeIdProps.js +17 -0
  60. package/dist/src/hooks/useOperation.d.ts +2 -0
  61. package/dist/src/hooks/useOperation.js +5 -0
  62. package/dist/src/hooks/useOutline.d.ts +2 -0
  63. package/dist/src/hooks/useOutline.js +5 -0
  64. package/dist/src/hooks/useOutlineDragon.d.ts +2 -0
  65. package/dist/src/hooks/useOutlineDragon.js +5 -0
  66. package/dist/src/hooks/usePosition.d.ts +1 -0
  67. package/dist/src/hooks/usePosition.js +2 -0
  68. package/dist/src/hooks/usePrefix.d.ts +1 -0
  69. package/dist/src/hooks/usePrefix.js +5 -0
  70. package/dist/src/hooks/useRegistry.d.ts +2 -0
  71. package/dist/src/hooks/useRegistry.js +6 -0
  72. package/dist/src/hooks/useScreen.d.ts +2 -0
  73. package/dist/src/hooks/useScreen.js +2 -0
  74. package/dist/src/hooks/useSelected.d.ts +1 -0
  75. package/dist/src/hooks/useSelected.js +5 -0
  76. package/dist/src/hooks/useSelection.d.ts +2 -0
  77. package/dist/src/hooks/useSelection.js +5 -0
  78. package/dist/src/hooks/useTheme.d.ts +1 -0
  79. package/dist/src/hooks/useTheme.js +2 -0
  80. package/dist/src/hooks/useTree.d.ts +2 -0
  81. package/dist/src/hooks/useTree.js +5 -0
  82. package/dist/src/hooks/useTreeNode.d.ts +2 -0
  83. package/dist/src/hooks/useTreeNode.js +3 -0
  84. package/dist/src/hooks/useValidNodeOffsetRect.d.ts +2 -0
  85. package/dist/src/hooks/useValidNodeOffsetRect.js +81 -0
  86. package/dist/src/hooks/useViewport.d.ts +2 -0
  87. package/dist/src/hooks/useViewport.js +5 -0
  88. package/dist/src/hooks/useViewportDragon.d.ts +3 -0
  89. package/dist/src/hooks/useViewportDragon.js +6 -0
  90. package/dist/src/hooks/useWorkbench.d.ts +2 -0
  91. package/dist/src/hooks/useWorkbench.js +5 -0
  92. package/dist/src/hooks/useWorkspace.d.ts +2 -0
  93. package/dist/src/hooks/useWorkspace.js +17 -0
  94. package/dist/src/index.d.ts +8 -0
  95. package/dist/src/index.js +8 -0
  96. package/dist/src/locales/global.d.ts +17 -0
  97. package/dist/src/locales/global.js +16 -0
  98. package/dist/src/locales/icons.d.ts +157 -0
  99. package/dist/src/locales/icons.js +156 -0
  100. package/dist/src/locales/index.d.ts +5 -0
  101. package/dist/src/locales/index.js +7 -0
  102. package/dist/src/locales/operations.d.ts +57 -0
  103. package/dist/src/locales/operations.js +56 -0
  104. package/dist/src/locales/panels.d.ts +19 -0
  105. package/dist/src/locales/panels.js +18 -0
  106. package/dist/src/shared/globals.d.ts +5 -0
  107. package/dist/src/shared/globals.js +5 -0
  108. package/dist/src/types.d.ts +36 -0
  109. package/dist/src/types.js +1 -0
  110. package/dist/src/widgets/AuxToolWidget/Cover.d.ts +2 -0
  111. package/dist/src/widgets/AuxToolWidget/Cover.js +48 -0
  112. package/dist/src/widgets/AuxToolWidget/Helpers.d.ts +6 -0
  113. package/dist/src/widgets/AuxToolWidget/Helpers.js +74 -0
  114. package/dist/src/widgets/AuxToolWidget/Insertion.d.ts +2 -0
  115. package/dist/src/widgets/AuxToolWidget/Insertion.js +99 -0
  116. package/dist/src/widgets/AuxToolWidget/Selection.d.ts +7 -0
  117. package/dist/src/widgets/AuxToolWidget/Selection.js +61 -0
  118. package/dist/src/widgets/AuxToolWidget/index.d.ts +5 -0
  119. package/dist/src/widgets/AuxToolWidget/index.js +35 -0
  120. package/dist/src/widgets/DesignerToolsWidget/index.d.ts +9 -0
  121. package/dist/src/widgets/DesignerToolsWidget/index.js +179 -0
  122. package/dist/src/widgets/DroppableWidget/index.d.ts +9 -0
  123. package/dist/src/widgets/DroppableWidget/index.js +20 -0
  124. package/dist/src/widgets/EmptyWidget/index.d.ts +5 -0
  125. package/dist/src/widgets/EmptyWidget/index.js +25 -0
  126. package/dist/src/widgets/GhostWidget/index.d.ts +2 -0
  127. package/dist/src/widgets/GhostWidget/index.js +47 -0
  128. package/dist/src/widgets/HistoryWidget/index.d.ts +2 -0
  129. package/dist/src/widgets/HistoryWidget/index.js +409 -0
  130. package/dist/src/widgets/IconWidget/index.d.ts +26 -0
  131. package/dist/src/widgets/IconWidget/index.js +203 -0
  132. package/dist/src/widgets/NodePathWidget/index.d.ts +6 -0
  133. package/dist/src/widgets/NodePathWidget/index.js +32 -0
  134. package/dist/src/widgets/NodeTitleWidget/index.d.ts +6 -0
  135. package/dist/src/widgets/NodeTitleWidget/index.js +13 -0
  136. package/dist/src/widgets/TextWidget/index.d.ts +10 -0
  137. package/dist/src/widgets/TextWidget/index.js +36 -0
  138. package/dist/src/widgets/ViewToolsWidget/index.d.ts +11 -0
  139. package/dist/src/widgets/ViewToolsWidget/index.js +76 -0
  140. package/dist/src/widgets/index.d.ts +11 -0
  141. package/dist/src/widgets/index.js +11 -0
  142. package/package.json +61 -4
package/README.md CHANGED
@@ -1,3 +1,7 @@
1
- # Temporary Holding Version
1
+ # @yimoka/designable-react
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ ```sh
4
+ npm install --save-exact @yimoka/designable-react@0.1.1
5
+ ```
6
+
7
+ 公开入口见 package.json 的 exports。宿主依赖及支持的版本范围见 peerDependencies。
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ import type { IUIAdapter } from "./types.js";
3
+ export interface IUIAdapterProviderProps {
4
+ adapter?: Partial<IUIAdapter>;
5
+ children?: React.ReactNode;
6
+ }
7
+ export declare const UIAdapterProvider: React.FC<IUIAdapterProviderProps>;
@@ -0,0 +1,10 @@
1
+ import React, { useMemo } from 'react';
2
+ import { UIAdapterContext } from "./context-core.js";
3
+ import { defaultAdapter } from "./default.js";
4
+ export const UIAdapterProvider = ({ adapter, children, }) => {
5
+ const mergedAdapter = useMemo(() => ({
6
+ ...defaultAdapter,
7
+ ...adapter,
8
+ }), [adapter]);
9
+ return (React.createElement(UIAdapterContext.Provider, { value: mergedAdapter }, children));
10
+ };
@@ -0,0 +1,2 @@
1
+ import type { IUIAdapter } from "./types.js";
2
+ export declare const UIAdapterContext: import("react").Context<IUIAdapter>;
@@ -0,0 +1,3 @@
1
+ import { createContext } from 'react';
2
+ import { defaultAdapter } from "./default.js";
3
+ export const UIAdapterContext = createContext(defaultAdapter);
@@ -0,0 +1,6 @@
1
+ import type { IUIAdapter } from "./types.js";
2
+ export { UIAdapterContext } from "./context-core.js";
3
+ export type { IUIAdapterProviderProps } from "./UIAdapterProvider.js";
4
+ export { UIAdapterProvider } from "./UIAdapterProvider.js";
5
+ export declare const useUIAdapter: () => IUIAdapter;
6
+ export declare const useUIComponent: <K extends keyof IUIAdapter>(name: K) => IUIAdapter[K];
@@ -0,0 +1,9 @@
1
+ import { useContext } from 'react';
2
+ import { UIAdapterContext } from "./context-core.js";
3
+ export { UIAdapterContext } from "./context-core.js";
4
+ export { UIAdapterProvider } from "./UIAdapterProvider.js";
5
+ export const useUIAdapter = () => useContext(UIAdapterContext);
6
+ export const useUIComponent = (name) => {
7
+ const adapter = useUIAdapter();
8
+ return adapter[name];
9
+ };
@@ -0,0 +1,8 @@
1
+ import React from 'react';
2
+ import type { IBreadcrumbProps, IButtonGroupProps, IButtonProps, IEmptyProps, IInputNumberProps, ITooltipProps } from "./types.js";
3
+ export declare const DefaultButton: React.FC<IButtonProps>;
4
+ export declare const DefaultButtonGroup: React.FC<IButtonGroupProps>;
5
+ export declare const DefaultInputNumber: React.FC<IInputNumberProps>;
6
+ export declare const DefaultBreadcrumb: React.FC<IBreadcrumbProps>;
7
+ export declare const DefaultTooltip: React.FC<ITooltipProps>;
8
+ export declare const DefaultEmpty: React.FC<IEmptyProps>;
@@ -0,0 +1,38 @@
1
+ import React from 'react';
2
+ export const DefaultButton = ({ ariaCurrent, ariaLabel, ariaPressed, type = 'default', size = 'middle', disabled, onClick, children, className, style, }) => (React.createElement("button", { "aria-current": ariaCurrent, "aria-label": ariaLabel, "aria-pressed": ariaPressed, className: className, "data-size": size, "data-type": type, disabled: disabled, style: style, type: "button", onClick: onClick }, children));
3
+ export const DefaultButtonGroup = ({ children, className, style, size, }) => (React.createElement("div", { className: className, "data-size": size, style: { display: 'inline-flex', ...style } }, children));
4
+ export const DefaultInputNumber = ({ value, onChange, size = 'middle', style, className, onPressEnter, disabled, min, max, step, }) => (React.createElement("input", { className: className, "data-size": size, disabled: disabled, max: max, min: min, step: step, style: style, type: "number", value: value ?? '', onChange: (e) => {
5
+ const val = e.target.value;
6
+ const nextValue = Number(val);
7
+ onChange?.(val === '' || !Number.isFinite(nextValue) ? null : nextValue);
8
+ }, onKeyDown: (e) => {
9
+ if (e.key === 'Enter') {
10
+ onPressEnter?.();
11
+ }
12
+ } }));
13
+ export const DefaultBreadcrumb = ({ items = [], className, style, separator = '/', }) => (React.createElement("nav", { className: className, style: style },
14
+ React.createElement("ol", { style: { display: 'flex', listStyle: 'none', margin: 0, padding: 0 } }, items.map((item, index) => (React.createElement("li", { key: item.key ?? index, style: { display: 'flex', alignItems: 'center' } },
15
+ React.createElement("a", { href: "#", style: { cursor: 'pointer' }, onClick: (e) => {
16
+ e.preventDefault();
17
+ item.onClick?.(e);
18
+ }, onMouseEnter: item.onMouseEnter }, item.title),
19
+ index < items.length - 1 && (React.createElement("span", { style: { margin: '0 4px' } }, separator))))))));
20
+ export const DefaultTooltip = ({ title, children, }) => {
21
+ if (!children)
22
+ return null;
23
+ const titleStr = typeof title === 'string' ? title : undefined;
24
+ const cloneProps = { title: titleStr };
25
+ return React.cloneElement(children, cloneProps);
26
+ };
27
+ export const DefaultEmpty = ({ description = '暂无数据', image, className, style, children, }) => (React.createElement("div", { className: className, style: {
28
+ display: 'flex',
29
+ flexDirection: 'column',
30
+ alignItems: 'center',
31
+ justifyContent: 'center',
32
+ padding: '32px 0',
33
+ color: '#999',
34
+ ...style,
35
+ } },
36
+ image && React.createElement("div", { style: { marginBottom: 8 } }, image),
37
+ React.createElement("div", null, description),
38
+ children));
@@ -0,0 +1,2 @@
1
+ import type { IUIAdapter } from "./types.js";
2
+ export declare const defaultAdapter: IUIAdapter;
@@ -0,0 +1,9 @@
1
+ import { DefaultBreadcrumb, DefaultButton, DefaultButtonGroup, DefaultEmpty, DefaultInputNumber, DefaultTooltip, } from "./default-components.js";
2
+ export const defaultAdapter = {
3
+ Button: DefaultButton,
4
+ ButtonGroup: DefaultButtonGroup,
5
+ InputNumber: DefaultInputNumber,
6
+ Breadcrumb: DefaultBreadcrumb,
7
+ Tooltip: DefaultTooltip,
8
+ Empty: DefaultEmpty,
9
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./context.js";
2
+ export * from "./default.js";
3
+ export * from "./types.js";
@@ -0,0 +1,3 @@
1
+ export * from "./context.js";
2
+ export * from "./default.js";
3
+ export * from "./types.js";
@@ -0,0 +1,63 @@
1
+ import type React from 'react';
2
+ export interface IButtonProps {
3
+ ariaCurrent?: React.AriaAttributes['aria-current'];
4
+ ariaLabel?: string;
5
+ ariaPressed?: boolean;
6
+ type?: 'primary' | 'default' | 'text' | 'link';
7
+ size?: 'small' | 'middle' | 'large';
8
+ disabled?: boolean;
9
+ onClick?: (e: React.MouseEvent) => void;
10
+ children?: React.ReactNode;
11
+ className?: string;
12
+ style?: React.CSSProperties;
13
+ }
14
+ export interface IButtonGroupProps {
15
+ children?: React.ReactNode;
16
+ className?: string;
17
+ style?: React.CSSProperties;
18
+ size?: 'small' | 'middle' | 'large';
19
+ }
20
+ export interface IInputNumberProps {
21
+ value?: number | string;
22
+ onChange?: (value: number | null) => void;
23
+ size?: 'small' | 'middle' | 'large';
24
+ style?: React.CSSProperties;
25
+ className?: string;
26
+ onPressEnter?: () => void;
27
+ disabled?: boolean;
28
+ min?: number;
29
+ max?: number;
30
+ step?: number;
31
+ }
32
+ export interface IBreadcrumbItem {
33
+ title: React.ReactNode;
34
+ onClick?: (e: React.MouseEvent) => void;
35
+ onMouseEnter?: (e: React.MouseEvent) => void;
36
+ key?: string | number;
37
+ }
38
+ export interface IBreadcrumbProps {
39
+ items?: IBreadcrumbItem[];
40
+ className?: string;
41
+ style?: React.CSSProperties;
42
+ separator?: React.ReactNode;
43
+ }
44
+ export interface ITooltipProps {
45
+ title?: React.ReactNode;
46
+ children?: React.ReactElement;
47
+ placement?: 'top' | 'bottom' | 'left' | 'right';
48
+ }
49
+ export interface IEmptyProps {
50
+ description?: React.ReactNode;
51
+ image?: React.ReactNode;
52
+ className?: string;
53
+ style?: React.CSSProperties;
54
+ children?: React.ReactNode;
55
+ }
56
+ export interface IUIAdapter {
57
+ Button: React.FC<IButtonProps>;
58
+ ButtonGroup: React.FC<IButtonGroupProps>;
59
+ InputNumber: React.FC<IInputNumberProps>;
60
+ Breadcrumb: React.FC<IBreadcrumbProps>;
61
+ Tooltip: React.FC<ITooltipProps>;
62
+ Empty: React.FC<IEmptyProps>;
63
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { FC, PropsWithChildren } from 'react';
2
+ import type { IDesignerProps } from "../types.js";
3
+ export declare const Designer: FC<PropsWithChildren<IDesignerProps>>;
@@ -0,0 +1,63 @@
1
+ import { forEach } from 'lodash-es';
2
+ import React, { useContext, useEffect, useLayoutEffect, useRef } from 'react';
3
+ import { DesignerEngineContext, DesignerLayoutContext, } from "../context.js";
4
+ import { DESIGNABLE_ENGINE_KEY } from "../shared/globals.js";
5
+ import { GhostWidget } from "../widgets/index.js";
6
+ export const Designer = (props) => {
7
+ const { engine, theme = 'light', prefixCls = 'dn-', position = 'fixed', variables, children, } = props;
8
+ const existingEngine = useContext(DesignerEngineContext);
9
+ const engineRef = useRef(null);
10
+ useEffect(() => {
11
+ if (engine) {
12
+ engine.mount();
13
+ Reflect.set(globalThis, DESIGNABLE_ENGINE_KEY, engine);
14
+ engineRef.current = engine;
15
+ }
16
+ return () => {
17
+ if (engine) {
18
+ engine.unmount();
19
+ if (Reflect.get(globalThis, DESIGNABLE_ENGINE_KEY) === engine) {
20
+ Reflect.deleteProperty(globalThis, DESIGNABLE_ENGINE_KEY);
21
+ }
22
+ if (engineRef.current === engine) {
23
+ engineRef.current = null;
24
+ }
25
+ }
26
+ };
27
+ }, [engine]);
28
+ useLayoutEffect(() => {
29
+ if (!variables)
30
+ return;
31
+ const { style } = document.documentElement;
32
+ const snapshots = new Map();
33
+ forEach(variables, (value, key) => {
34
+ const varName = `--${key}`;
35
+ snapshots.set(varName, {
36
+ priority: style.getPropertyPriority(varName),
37
+ value: style.getPropertyValue(varName),
38
+ });
39
+ style.setProperty(varName, value);
40
+ });
41
+ return () => {
42
+ snapshots.forEach((snapshot, varName) => {
43
+ if (snapshot.value) {
44
+ style.setProperty(varName, snapshot.value, snapshot.priority);
45
+ }
46
+ else {
47
+ style.removeProperty(varName);
48
+ }
49
+ });
50
+ };
51
+ }, [variables]);
52
+ if (existingEngine) {
53
+ throw new Error('There can only be one Designable Engine Context in the React Tree');
54
+ }
55
+ return (React.createElement(DesignerLayoutContext.Provider, { value: {
56
+ theme,
57
+ prefixCls,
58
+ position,
59
+ } },
60
+ React.createElement(DesignerEngineContext.Provider, { value: engine },
61
+ children,
62
+ React.createElement(GhostWidget, null))));
63
+ };
@@ -0,0 +1,4 @@
1
+ export type { ISandboxProps } from "./sandbox-types.js";
2
+ export { Sandbox } from "./SandboxComponent.js";
3
+ export { useSandbox } from "./useSandbox.js";
4
+ export { isSafeSandboxAssetUrl, renderSandboxContent, renderSandboxScriptAssets, renderSandboxStyleAssets, useSandboxScope, } from "./sandbox-utils.js";
@@ -0,0 +1,3 @@
1
+ export { Sandbox } from "./SandboxComponent.js";
2
+ export { useSandbox } from "./useSandbox.js";
3
+ export { isSafeSandboxAssetUrl, renderSandboxContent, renderSandboxScriptAssets, renderSandboxStyleAssets, useSandboxScope, } from "./sandbox-utils.js";
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+ import type { ISandboxProps } from "./sandbox-types.js";
3
+ export declare const Sandbox: React.FC<ISandboxProps>;
@@ -0,0 +1,15 @@
1
+ import React from 'react';
2
+ import { useSandbox } from "./useSandbox.js";
3
+ export const Sandbox = (props) => {
4
+ const { cssAssets, jsAssets, scope, style, ...iframeProps } = props;
5
+ void cssAssets;
6
+ void jsAssets;
7
+ void scope;
8
+ const ref = useSandbox(props);
9
+ return (React.createElement("iframe", { ref: ref, sandbox: "allow-scripts allow-same-origin", style: {
10
+ border: 'none',
11
+ width: '100%',
12
+ height: '100%',
13
+ ...style,
14
+ }, title: "Designable Sandbox", ...iframeProps }));
15
+ };
@@ -0,0 +1,3 @@
1
+ import type { FC, HTMLAttributes } from 'react';
2
+ export type ISimulatorProps = HTMLAttributes<HTMLDivElement>;
3
+ export declare const Simulator: FC<ISimulatorProps>;
@@ -0,0 +1,70 @@
1
+ import { observer } from '@formily/reactive-react';
2
+ import { requestIdle, ScreenType } from '@yimoka/designable-core';
3
+ import React from 'react';
4
+ import { useScreen } from "../hooks/index.js";
5
+ const normalizeSize = (value, fallback) => (value === undefined ? fallback : value);
6
+ const toStyleSize = (value) => (typeof value === 'number' ? value : value);
7
+ const getSimulatorType = (type) => {
8
+ switch (type) {
9
+ case ScreenType.Mobile:
10
+ return 'mobile';
11
+ case ScreenType.Responsive:
12
+ return 'responsive';
13
+ case ScreenType.PC:
14
+ default:
15
+ return 'pc';
16
+ }
17
+ };
18
+ const getMobileSize = (flip) => ({
19
+ height: flip ? 375 : 667,
20
+ width: flip ? 667 : 375,
21
+ });
22
+ const isFixedDeviceSimulator = (simulatorType) => (simulatorType === 'mobile' || simulatorType === 'responsive');
23
+ const getSimulatorWidth = (screen, simulatorType) => (simulatorType === 'mobile'
24
+ ? getMobileSize(screen?.flip).width
25
+ : simulatorType === 'responsive'
26
+ ? normalizeSize(screen?.width, '100%')
27
+ : '100%');
28
+ const getSimulatorHeight = (screen, simulatorType) => (simulatorType === 'mobile'
29
+ ? getMobileSize(screen?.flip).height
30
+ : simulatorType === 'responsive'
31
+ ? normalizeSize(screen?.height, '100%')
32
+ : '100%');
33
+ const getSimulatorScreenStyle = (screen, simulatorType) => {
34
+ const isFixedDevice = isFixedDeviceSimulator(simulatorType);
35
+ const width = getSimulatorWidth(screen, simulatorType);
36
+ const height = getSimulatorHeight(screen, simulatorType);
37
+ return {
38
+ background: screen?.background || '#fff',
39
+ boxShadow: isFixedDevice ? '0 2px 12px rgba(0, 0, 0, 0.12)' : undefined,
40
+ height: toStyleSize(height),
41
+ margin: isFixedDevice ? '24px auto' : 0,
42
+ maxHeight: isFixedDevice ? 'calc(100% - 48px)' : '100%',
43
+ maxWidth: isFixedDevice ? 'calc(100% - 48px)' : '100%',
44
+ overflow: 'auto',
45
+ width: toStyleSize(width),
46
+ };
47
+ };
48
+ const getSimulatorContainerStyle = (style, simulatorType) => {
49
+ const isFixedDevice = isFixedDeviceSimulator(simulatorType);
50
+ return {
51
+ boxSizing: 'border-box',
52
+ height: '100%',
53
+ overflow: 'auto',
54
+ padding: isFixedDevice ? '1px 0' : 0,
55
+ width: '100%',
56
+ ...style,
57
+ };
58
+ };
59
+ export const Simulator = observer((props) => {
60
+ const screen = useScreen();
61
+ const { children, ...rest } = props;
62
+ const simulatorType = getSimulatorType(screen?.type);
63
+ const containerStyle = getSimulatorContainerStyle(rest.style, simulatorType);
64
+ const screenStyle = getSimulatorScreenStyle(screen, simulatorType);
65
+ return (React.createElement("div", { ...rest, "data-simulator": simulatorType, style: containerStyle },
66
+ React.createElement("div", { "data-simulator-screen": simulatorType, style: screenStyle }, children)));
67
+ }, {
68
+ scheduler: requestIdle,
69
+ });
70
+ Simulator.displayName = 'Simulator';
@@ -0,0 +1,6 @@
1
+ import type { FC, HTMLAttributes, ReactNode } from 'react';
2
+ export interface IViewportProps extends Omit<HTMLAttributes<HTMLDivElement>, 'placeholder'> {
3
+ placeholder?: ReactNode;
4
+ dragTipsDirection?: 'left' | 'right';
5
+ }
6
+ export declare const Viewport: FC<IViewportProps>;
@@ -0,0 +1,71 @@
1
+ import { requestIdle } from '@yimoka/designable-core';
2
+ import cls from 'classnames';
3
+ import React, { useLayoutEffect, useRef, useState } from 'react';
4
+ import { usePrefix, useViewport } from "../hooks/index.js";
5
+ import { AuxToolWidget } from "../widgets/index.js";
6
+ export const Viewport = ({ placeholder, dragTipsDirection, className, style, children, ...props }) => {
7
+ const [loaded, setLoaded] = useState(false);
8
+ const [isFrame, setIsFrame] = useState(false);
9
+ const prefix = usePrefix('viewport');
10
+ const viewport = useViewport();
11
+ const ref = useRef(null);
12
+ const viewportRef = useRef(undefined);
13
+ useLayoutEffect(() => {
14
+ const container = ref.current;
15
+ if (!container || !viewport)
16
+ return;
17
+ let disposed = false;
18
+ const frameElement = container.querySelector('iframe');
19
+ requestIdle(() => {
20
+ if (disposed)
21
+ return;
22
+ setLoaded(false);
23
+ });
24
+ viewportRef.current = viewport;
25
+ const markLoaded = (nextIsFrame) => {
26
+ requestIdle(() => {
27
+ if (disposed)
28
+ return;
29
+ setIsFrame(nextIsFrame);
30
+ setLoaded(true);
31
+ });
32
+ };
33
+ if (frameElement) {
34
+ const handleLoad = () => {
35
+ if (disposed)
36
+ return;
37
+ viewport.onMount(frameElement, frameElement.contentWindow);
38
+ markLoaded(true);
39
+ };
40
+ frameElement.addEventListener('load', handleLoad);
41
+ if (frameElement.contentDocument?.readyState === 'complete') {
42
+ handleLoad();
43
+ }
44
+ return () => {
45
+ disposed = true;
46
+ frameElement.removeEventListener('load', handleLoad);
47
+ viewport.onUnmount();
48
+ if (viewportRef.current === viewport) {
49
+ viewportRef.current = undefined;
50
+ }
51
+ };
52
+ }
53
+ viewport.onMount(container, globalThis);
54
+ markLoaded(false);
55
+ return () => {
56
+ disposed = true;
57
+ viewport.onUnmount();
58
+ if (viewportRef.current === viewport) {
59
+ viewportRef.current = undefined;
60
+ }
61
+ };
62
+ }, [viewport]);
63
+ return (React.createElement("div", { ...props, ref: ref, className: cls(prefix, className), style: {
64
+ opacity: loaded ? 1 : 0,
65
+ overflow: isFrame ? 'hidden' : 'overlay',
66
+ position: 'relative',
67
+ ...style,
68
+ } },
69
+ children,
70
+ React.createElement(AuxToolWidget, null)));
71
+ };
@@ -0,0 +1,2 @@
1
+ import type { FC, PropsWithChildren } from 'react';
2
+ export declare const Workbench: FC<PropsWithChildren>;
@@ -0,0 +1,9 @@
1
+ import { observer } from '@formily/reactive-react';
2
+ import React from 'react';
3
+ import { useWorkbench } from "../hooks/index.js";
4
+ import { Workspace } from "./Workspace.js";
5
+ export const Workbench = observer((props) => {
6
+ const workbench = useWorkbench();
7
+ return (React.createElement(Workspace, { id: workbench?.currentWorkspace?.id }, props.children));
8
+ });
9
+ Workbench.displayName = 'Workbench';
@@ -0,0 +1,7 @@
1
+ import type { FC, PropsWithChildren } from 'react';
2
+ export interface IWorkspaceProps {
3
+ id?: string;
4
+ title?: string;
5
+ description?: string;
6
+ }
7
+ export declare const Workspace: FC<PropsWithChildren<IWorkspaceProps>>;
@@ -0,0 +1,39 @@
1
+ import React, { Fragment, useEffect, useMemo, useRef } from 'react';
2
+ import { WorkspaceContext } from "../context.js";
3
+ import { useDesigner } from "../hooks/index.js";
4
+ const detachWorkspaceViewports = (workspace) => {
5
+ workspace?.viewport.detachEvents();
6
+ workspace?.outline.detachEvents();
7
+ };
8
+ export const Workspace = ({ id, title, description, children, }) => {
9
+ const oldIdRef = useRef(undefined);
10
+ const designer = useDesigner();
11
+ const workspaceId = id ?? 'index';
12
+ const workspace = useMemo(() => ({
13
+ id: workspaceId,
14
+ title,
15
+ description,
16
+ }), [workspaceId, title, description]);
17
+ useEffect(() => {
18
+ if (!designer)
19
+ return;
20
+ const oldId = oldIdRef.current;
21
+ if (oldId && oldId !== workspaceId) {
22
+ detachWorkspaceViewports(designer.workbench.findWorkspaceById(oldId));
23
+ }
24
+ oldIdRef.current = workspaceId;
25
+ return () => {
26
+ if (oldIdRef.current !== workspaceId)
27
+ return;
28
+ detachWorkspaceViewports(designer.workbench.findWorkspaceById(workspaceId));
29
+ oldIdRef.current = undefined;
30
+ };
31
+ }, [designer, workspaceId]);
32
+ useEffect(() => {
33
+ if (!designer)
34
+ return;
35
+ designer.workbench.ensureWorkspace(workspace);
36
+ }, [designer, workspace]);
37
+ return (React.createElement(Fragment, null,
38
+ React.createElement(WorkspaceContext.Provider, { value: workspace }, children)));
39
+ };
@@ -0,0 +1,6 @@
1
+ export * from "./Designer.js";
2
+ export * from "./Sandbox.js";
3
+ export * from "./Simulator.js";
4
+ export * from "./Viewport.js";
5
+ export * from "./Workbench.js";
6
+ export * from "./Workspace.js";
@@ -0,0 +1,6 @@
1
+ export * from "./Designer.js";
2
+ export * from "./Sandbox.js";
3
+ export * from "./Simulator.js";
4
+ export * from "./Viewport.js";
5
+ export * from "./Workbench.js";
6
+ export * from "./Workspace.js";
@@ -0,0 +1,7 @@
1
+ import type React from 'react';
2
+ export interface ISandboxProps {
3
+ style?: React.CSSProperties;
4
+ cssAssets?: string[];
5
+ jsAssets?: string[];
6
+ scope?: unknown;
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import type React from 'react';
2
+ export declare const isSafeSandboxAssetUrl: (value: string) => boolean;
3
+ export declare const renderSandboxStyleAssets: (assets: string[]) => string;
4
+ export declare const renderSandboxScriptAssets: (assets: string[]) => string;
5
+ export declare const useSandboxScope: () => unknown;
6
+ export declare const renderSandboxContent: (render: (scope?: unknown) => React.ReactElement) => void;
@@ -0,0 +1,40 @@
1
+ import { isFunction } from 'lodash-es';
2
+ import { DESIGNABLE_SANDBOX_SCOPE_KEY } from "../shared/globals.js";
3
+ const escapeHtmlAttribute = (value) => (value
4
+ .replace(/&/g, '&amp;')
5
+ .replace(/"/g, '&quot;')
6
+ .replace(/</g, '&lt;')
7
+ .replace(/>/g, '&gt;'));
8
+ export const isSafeSandboxAssetUrl = (value) => {
9
+ const url = value.trim();
10
+ if (!url)
11
+ return false;
12
+ if (/["'<>`]/.test(url))
13
+ return false;
14
+ if (/^(javascript|data|vbscript):/i.test(url))
15
+ return false;
16
+ return /^(https?:)?\/\//i.test(url)
17
+ || url.startsWith('/')
18
+ || url.startsWith('./')
19
+ || url.startsWith('../');
20
+ };
21
+ export const renderSandboxStyleAssets = (assets) => assets
22
+ .filter(isSafeSandboxAssetUrl)
23
+ .map(css => `<link media="all" rel="stylesheet" href="${escapeHtmlAttribute(css.trim())}" />`)
24
+ .join('\n');
25
+ export const renderSandboxScriptAssets = (assets) => assets
26
+ .filter(isSafeSandboxAssetUrl)
27
+ .map(js => `<script src="${escapeHtmlAttribute(js.trim())}" type="text/javascript"></script>`)
28
+ .join('\n');
29
+ export const useSandboxScope = () => Reflect.get(globalThis, DESIGNABLE_SANDBOX_SCOPE_KEY);
30
+ export const renderSandboxContent = (render) => {
31
+ if (isFunction(render)) {
32
+ const root = document.getElementById('__SANDBOX_ROOT__');
33
+ if (root) {
34
+ import('react-dom/client').then(({ createRoot }) => {
35
+ const rootInstance = createRoot(root);
36
+ rootInstance.render(render(useSandboxScope()));
37
+ });
38
+ }
39
+ }
40
+ };
@@ -0,0 +1,3 @@
1
+ import type React from 'react';
2
+ import type { ISandboxProps } from "./sandbox-types.js";
3
+ export declare const useSandbox: (props: React.PropsWithChildren<ISandboxProps>) => React.RefObject<HTMLIFrameElement | null>;