diff --git a/packages/designer/src/context.ts b/packages/designer/src/context.ts index 886586e5..d99f3517 100644 --- a/packages/designer/src/context.ts +++ b/packages/designer/src/context.ts @@ -6,10 +6,14 @@ export interface IDesignerContext { * 沙箱查询实例 */ sandboxQuery: DndQuery; + /** + * 预览沙箱查询实例 + */ + previewSandboxQuery: DndQuery; /** * 远程服务 */ - remoteServices?: Record; + remoteServices: Record; } const [DesignerProvider, useDesigner] = createContext({ @@ -19,9 +23,13 @@ const [DesignerProvider, useDesigner] = createContext({ 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; }; diff --git a/packages/designer/src/designer.tsx b/packages/designer/src/designer.tsx index 3f6a46ce..e868942b 100644 --- a/packages/designer/src/designer.tsx +++ b/packages/designer/src/designer.tsx @@ -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, ITangoEngineContext { /** * 主题包 */ @@ -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 ( - {children} + + {children} + diff --git a/packages/designer/src/dnd/dnd-query.ts b/packages/designer/src/dnd/dnd-query.ts index 7a11d8ef..7460f463 100644 --- a/packages/designer/src/dnd/dnd-query.ts +++ b/packages/designer/src/dnd/dnd-query.ts @@ -12,7 +12,6 @@ export const DRAGGABLE_SELECTOR = `[${SLOT.dnd}]`; interface DndQueryOptions { /** * DOM 查询上下文选择器 - * TODO: 是不是可以合并成一个 API */ context?: string; /** @@ -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; @@ -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; diff --git a/packages/designer/src/helpers/dom.ts b/packages/designer/src/helpers/dom.ts index 12f4a040..b3b49a1c 100644 --- a/packages/designer/src/helpers/dom.ts +++ b/packages/designer/src/helpers/dom.ts @@ -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}"]`; } @@ -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) { diff --git a/packages/designer/src/sandbox/sandbox.tsx b/packages/designer/src/sandbox/sandbox.tsx index e8c9a076..21f7a508 100644 --- a/packages/designer/src/sandbox/sandbox.tsx +++ b/packages/designer/src/sandbox/sandbox.tsx @@ -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; @@ -62,6 +63,7 @@ function useSandbox({ const workspace = useWorkspace(); const designer = useDesigner(); const sandboxQuery = useSandboxQuery(); + const isPreview = isPreviewProp ?? designer.isPreview; // 组件不一定会立即刷新,因此 isActive 需要实时获取 @@ -140,7 +142,7 @@ const PreviewSandbox = observer( return ( - + ); }, @@ -188,7 +190,7 @@ const DesignSandbox = observer( return ( - + ); },