Skip to content

Commit

Permalink
fix: export usePreviewSandboxQuery & add builtin sandboxQuery (#163)
Browse files Browse the repository at this point in the history
* fix: export usePreviewSandboxQuery

* fix: update ui

* fix: update ui
  • Loading branch information
wwsun authored May 24, 2024
1 parent 22a2e9b commit 163eced
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 14 deletions.
14 changes: 11 additions & 3 deletions packages/designer/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ export interface IDesignerContext<S = any> {
* 沙箱查询实例
*/
sandboxQuery: DndQuery;
/**
* 预览沙箱查询实例
*/
previewSandboxQuery: DndQuery;
/**
* 远程服务
*/
remoteServices?: Record<string, S>;
remoteServices: Record<string, S>;
}

const [DesignerProvider, useDesigner] = createContext<IDesignerContext>({
Expand All @@ -19,9 +23,13 @@ const [DesignerProvider, useDesigner] = createContext<IDesignerContext>({
export { DesignerProvider };

export const useSandboxQuery = () => {
return useDesigner()?.sandboxQuery;
return useDesigner().sandboxQuery;
};

export const usePreviewSandboxQuery = () => {
return useDesigner().previewSandboxQuery;
};

export const useRemoteServices = () => {
return useDesigner()?.remoteServices;
return useDesigner().remoteServices;
};
26 changes: 23 additions & 3 deletions packages/designer/src/designer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@ import { TangoEngineProvider, ITangoEngineContext } from '@music163/tango-contex
import zhCN from 'antd/lib/locale/zh_CN';
import { DesignerProvider, IDesignerContext } from './context';
import defaultTheme from './themes/default';
import { DndQuery } from './dnd';
import { DESIGN_SANDBOX_ID, PREVIEW_SANDBOX_ID } from './helpers';

export interface DesignerProps extends IDesignerContext, ITangoEngineContext {
const builtinSandboxQuery = new DndQuery({
context: `#${DESIGN_SANDBOX_ID}`,
});

const builtinPreviewSandboxQuery = new DndQuery({
context: `#${PREVIEW_SANDBOX_ID}`,
});

export interface DesignerProps extends Partial<IDesignerContext>, ITangoEngineContext {
/**
* 主题包
*/
Expand All @@ -20,13 +30,23 @@ export interface DesignerProps extends IDesignerContext, ITangoEngineContext {
* @returns
*/
export function Designer(props: DesignerProps) {
const { engine, config, theme: themeProp, sandboxQuery, remoteServices = {}, children } = props;
const {
engine,
config,
theme: themeProp,
sandboxQuery = builtinSandboxQuery,
previewSandboxQuery = builtinPreviewSandboxQuery,
remoteServices = {},
children,
} = props;
const theme = useMemo(() => extendTheme(themeProp, defaultTheme), [themeProp]);
return (
<SystemProvider theme={theme} prefix="--tango">
<ConfigProvider locale={zhCN}>
<TangoEngineProvider value={{ engine, config }}>
<DesignerProvider value={{ sandboxQuery, remoteServices }}>{children}</DesignerProvider>
<DesignerProvider value={{ sandboxQuery, previewSandboxQuery, remoteServices }}>
{children}
</DesignerProvider>
</TangoEngineProvider>
</ConfigProvider>
</SystemProvider>
Expand Down
7 changes: 6 additions & 1 deletion packages/designer/src/dnd/dnd-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export const DRAGGABLE_SELECTOR = `[${SLOT.dnd}]`;
interface DndQueryOptions {
/**
* DOM 查询上下文选择器
* TODO: 是不是可以合并成一个 API
*/
context?: string;
/**
Expand Down Expand Up @@ -63,6 +62,9 @@ export class DndQuery {
return false;
}

/**
* 沙箱内的 window 对象
*/
get window() {
if (this.context && 'defaultView' in this.context) {
return (this.context as unknown as Document).defaultView;
Expand All @@ -71,6 +73,9 @@ export class DndQuery {
return window;
}

/**
* 沙箱内的全局滚动偏移
*/
get scrollTop() {
if (this.context && 'documentElement' in this.context) {
return (this.context as unknown as Document).documentElement.scrollTop;
Expand Down
16 changes: 14 additions & 2 deletions packages/designer/src/helpers/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ import {
parseDndId,
} from '@music163/tango-helpers';

// -----------
// CONSTANTS
// -----------

export const DRAG_GHOST_ID = 'dragGhost';

export const DESIGN_SANDBOX_ID = 'sandbox-container';

export const PREVIEW_SANDBOX_ID = 'preview-sandbox-container';

// -----------
// DOM Helpers
// -----------

export function buildQueryBySlotId(id: string) {
return `[${SLOT.dnd}="${id}"]`;
}
Expand All @@ -15,8 +29,6 @@ export function getElement(selector: Selector) {
return $(selector).get(0);
}

export const DRAG_GHOST_ID = 'dragGhost';

export const getDragGhostElement = () => getElement(`#${DRAG_GHOST_ID}`);

export function setElementStyle(selector: Selector, style: any) {
Expand Down
6 changes: 4 additions & 2 deletions packages/designer/src/sandbox/sandbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useSandboxQuery } from '../context';
import { DndQuery, useDnd } from '../dnd';
import { Navigator } from './navigator';
import { SelectionToolsProps } from '../simulator/selection';
import { DESIGN_SANDBOX_ID, PREVIEW_SANDBOX_ID } from '../helpers';

interface ISandboxEventHandlerConfig {
sandboxQuery?: DndQuery;
Expand Down Expand Up @@ -62,6 +63,7 @@ function useSandbox({
const workspace = useWorkspace();
const designer = useDesigner();
const sandboxQuery = useSandboxQuery();

const isPreview = isPreviewProp ?? designer.isPreview;

// 组件不一定会立即刷新,因此 isActive 需要实时获取
Expand Down Expand Up @@ -140,7 +142,7 @@ const PreviewSandbox = observer(

return (
<Box display={display} width="100%" height="100%">
<CodeSandbox ref={ref} iframeId="preview-sandbox-container" {...sandboxProps} {...rest} />
<CodeSandbox ref={ref} iframeId={PREVIEW_SANDBOX_ID} {...sandboxProps} {...rest} />
</Box>
);
},
Expand Down Expand Up @@ -188,7 +190,7 @@ const DesignSandbox = observer(

return (
<Box display={display} width="100%" height="100%">
<CodeSandbox ref={ref} {...sandboxProps} {...rest} />
<CodeSandbox ref={ref} iframeId={DESIGN_SANDBOX_ID} {...sandboxProps} {...rest} />
</Box>
);
},
Expand Down
2 changes: 1 addition & 1 deletion packages/designer/src/toolbar/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const PreviewTool = observer(() => {
designer.setActiveView('design');
}
}}
tooltip="预览"
tooltip={designer.isPreview ? '切换到设计模式' : '切换到预览模式'}
>
<EyeOutlined />
</ToggleButton>
Expand Down
2 changes: 1 addition & 1 deletion packages/designer/src/toolbar/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function Toolbar({ children }: ToolbarProps) {
<Group display="flex" alignItems="center" gap="m">
{centerTools}
</Group>
<Group display="flex" alignItems="center" gap="m">
<Group display="flex" alignItems="center" justifyContent="flex-end" gap="m">
{rightTools}
</Group>
</Box>
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const actionStyle = css`
border-radius: var(--tango-radii-s);
color: var(--tango-colors-text2);
background-color: transparent;
white-space: nowrap;
&:hover {
background-color: var(--tango-colors-fill2);
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/toggle-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const buttonStyle = css`
background-color: var(--tango-colors-custom-toolbarButtonBg, rgba(223, 223, 223, 0.08));
color: var(--tango-colors-custom-toolbarButtonTextColor, #999);
border: 0;
border-radius: var(--tango-radii-m);
border-radius: var(--tango-radii-s);
&:hover {
color: var(--tango-colors-custom-toolbarButtonTextColorHover, #fff);
Expand Down

0 comments on commit 163eced

Please sign in to comment.