Skip to content

Commit

Permalink
Merge branch 'master' into add-user-to-context
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Potoms <[email protected]>
  • Loading branch information
Janpot authored Feb 9, 2024
2 parents 172d2c3 + d698ed1 commit 68aab7f
Show file tree
Hide file tree
Showing 32 changed files with 647 additions and 231 deletions.
2 changes: 1 addition & 1 deletion packages/toolpad-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
"lodash-es": "4.17.21",
"markdown-to-jsx": "7.4.1",
"mime": "4.0.1",
"monaco-editor": "0.45.0",
"mysql2": "3.9.1",
"nanoid": "5.0.5",
"node-fetch": "2.7.0",
Expand Down Expand Up @@ -158,7 +159,6 @@
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import": "2.29.1",
"formidable": "3.5.1",
"monaco-editor": "0.45.0",
"react-transition-group": "4.4.5",
"webpack": "5.90.1"
},
Expand Down
4 changes: 3 additions & 1 deletion packages/toolpad-app/src/canvas/ToolpadBridge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ type Commands<T extends Record<string, Function>> = T & {
[COMMAND_HANDLERS]: Partial<T>;
};

function createCommands<T extends Record<string, Function>>(initial: Partial<T> = {}): Commands<T> {
export function createCommands<T extends Record<string, Function>>(
initial: Partial<T> = {},
): Commands<T> {
return new Proxy(
{
[COMMAND_HANDLERS]: initial,
Expand Down
6 changes: 2 additions & 4 deletions packages/toolpad-app/src/canvas/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import { CanvasEventsContext } from '@mui/toolpad-core/runtime';
import { FlowDirection, SlotType } from '@mui/toolpad-core';
import { update } from '@mui/toolpad-utils/immutability';
import { useNonNullableContext } from '@mui/toolpad-utils/react';
import ToolpadApp from '../runtime/ToolpadApp';
import { queryClient } from '../runtime/api';
import { AppCanvasState, NodeInfo, PageViewState, SlotsState } from '../types';
import {
getRelativeBoundingRect,
getRelativeOuterRect,
rectContainsPoint,
} from '../utils/geometry';
import { CanvasHooks, CanvasHooksContext } from '../runtime/CanvasHooksContext';
import { ToolpadBridge, bridge, setCommandHandler } from './ToolpadBridge';
import { AppHostContext } from '../runtime/AppHostContext';
import { AppHostContext, ToolpadApp, CanvasHooks, CanvasHooksContext } from '../runtime';

const handleScreenUpdate = throttle(
() => {
Expand All @@ -25,7 +23,7 @@ const handleScreenUpdate = throttle(
{ trailing: true },
);

function updateNodeInfo(nodeInfo: NodeInfo, rootElm: Element): NodeInfo {
export function updateNodeInfo(nodeInfo: NodeInfo, rootElm: Element): NodeInfo {
const nodeElm = rootElm.querySelector(`[data-toolpad-node-id="${nodeInfo.nodeId}"]`);

if (!nodeElm) {
Expand Down
7 changes: 5 additions & 2 deletions packages/toolpad-app/src/components/FlexFill.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { styled } from '@mui/material';
import { StyledComponent } from '@emotion/styled';
import { styled, BoxProps } from '@mui/material';

export default styled('div')({ flex: 1 });
const FlexFill: StyledComponent<BoxProps> = styled('div')({ flex: 1 });

export default FlexFill;
7 changes: 5 additions & 2 deletions packages/toolpad-app/src/components/Pre.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { styled } from '@mui/material';
import { StyledComponent } from '@emotion/styled';
import { styled, BoxProps } from '@mui/material';

export default styled('pre')({
const Pre: StyledComponent<BoxProps> = styled('pre')({
margin: 0,
fontFamily: 'Consolas, Menlo, Monaco, "Andale Mono", "Ubuntu Mono", monospace',
});

export default Pre;
59 changes: 32 additions & 27 deletions packages/toolpad-app/src/components/resizablePanels.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,38 @@
import { styled } from '@mui/material';
import { PanelResizeHandle as PanelResizeHandleOrig } from 'react-resizable-panels';
import {
PanelResizeHandle as PanelResizeHandleOrig,
PanelResizeHandleProps,
} from 'react-resizable-panels';

export * from 'react-resizable-panels';

const RESIZE_HANDLE_OFFSET = 3;

export const PanelResizeHandle = styled(PanelResizeHandleOrig)(({ theme }) => ({
backgroundClip: 'padding-box',
zIndex: 1,
display: 'flex',
backgroundColor: theme.palette.divider,
borderStyle: 'solid',
borderColor: 'rgba(255, 255, 255, 0)',
'&:hover': {
borderColor: theme.palette.divider,
},
transition: 'all 250ms ease',
'&[data-panel-group-direction="horizontal"]': {
width: 2 * RESIZE_HANDLE_OFFSET + 1,
marginLeft: -RESIZE_HANDLE_OFFSET,
marginRight: -RESIZE_HANDLE_OFFSET,
borderLeftWidth: RESIZE_HANDLE_OFFSET,
borderRightWidth: RESIZE_HANDLE_OFFSET,
},
'&[data-panel-group-direction="vertical"]': {
height: 2 * RESIZE_HANDLE_OFFSET + 1,
marginTop: -RESIZE_HANDLE_OFFSET,
marginBottom: -RESIZE_HANDLE_OFFSET,
borderTopWidth: RESIZE_HANDLE_OFFSET,
borderBottomWidth: RESIZE_HANDLE_OFFSET,
},
}));
export const PanelResizeHandle: React.FC<PanelResizeHandleProps> = styled(PanelResizeHandleOrig)(
({ theme }) => ({
backgroundClip: 'padding-box',
zIndex: 1,
display: 'flex',
backgroundColor: theme.palette.divider,
borderStyle: 'solid',
borderColor: 'rgba(255, 255, 255, 0)',
'&:hover': {
borderColor: theme.palette.divider,
},
transition: 'all 250ms ease',
'&[data-panel-group-direction="horizontal"]': {
width: 2 * RESIZE_HANDLE_OFFSET + 1,
marginLeft: -RESIZE_HANDLE_OFFSET,
marginRight: -RESIZE_HANDLE_OFFSET,
borderLeftWidth: RESIZE_HANDLE_OFFSET,
borderRightWidth: RESIZE_HANDLE_OFFSET,
},
'&[data-panel-group-direction="vertical"]': {
height: 2 * RESIZE_HANDLE_OFFSET + 1,
marginTop: -RESIZE_HANDLE_OFFSET,
marginBottom: -RESIZE_HANDLE_OFFSET,
borderTopWidth: RESIZE_HANDLE_OFFSET,
borderBottomWidth: RESIZE_HANDLE_OFFSET,
},
}),
);
82 changes: 82 additions & 0 deletions packages/toolpad-app/src/entrypoint.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import Button from '@mui/material/Button';
import * as React from 'react';
import * as ReactDOM from 'react-dom/client';
import { ToolpadComponents } from '@mui/toolpad-core';
import createCache from '@emotion/cache';
import { CacheProvider } from '@emotion/react';
import { Box } from '@mui/material';
import { RuntimeState } from './runtime/types';
import {
AppHost,
AppHostContext,
ToolpadApp as RuntimeToolpadApp,
ToolpadAppProps,
componentsStore,
pageComponentsStore,
} from './runtime';

const IS_PREVIEW = process.env.NODE_ENV !== 'production';
const IS_CUSTOM_SERVER = process.env.TOOLPAD_CUSTOM_SERVER === 'true';

const cache = createCache({
key: 'css',
prepend: true,
});

// See https://github.com/emotion-js/emotion/issues/1105#issuecomment-1058225197
cache.compat = true;

/**
* This allows us to hot update the components when a file is added/removed
*/
export function setComponents(
newComponents: ToolpadComponents,
pageComponents: Record<string, React.ComponentType>,
) {
componentsStore.setState(newComponents);
pageComponentsStore.setState(pageComponents);
}

interface RootProps {
initialState: RuntimeState;
base: string;
ToolpadApp: React.ComponentType<ToolpadAppProps>;
}

const IS_RENDERED_IN_CANVAS =
typeof window === 'undefined'
? false
: !!(window.frameElement as HTMLIFrameElement)?.dataset?.toolpadCanvas;

const appHost: AppHost = {
isPreview: IS_PREVIEW,
isCustomServer: IS_CUSTOM_SERVER,
isCanvas: IS_RENDERED_IN_CANVAS,
};

function Root({ ToolpadApp, initialState, base }: RootProps) {
return (
<React.StrictMode>
<CacheProvider value={cache}>
{/* For some reason this helps with https://github.com/vitejs/vite/issues/12423 */}
<Button sx={{ display: 'none' }} />
<AppHostContext.Provider value={appHost}>
<ToolpadApp basename={base} state={initialState} />
</AppHostContext.Provider>
<Box data-testid="page-ready-marker" sx={{ display: 'none' }} />
</CacheProvider>
</React.StrictMode>
);
}

export interface InitParams {
initialState: RuntimeState;
base: string;
ToolpadApp?: React.ComponentType<ToolpadAppProps>;
}

export function init({ ToolpadApp = RuntimeToolpadApp, initialState, base }: InitParams) {
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<Root base={base} ToolpadApp={ToolpadApp} initialState={initialState} />,
);
}
6 changes: 6 additions & 0 deletions packages/toolpad-app/src/exports/editor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as React from 'react';
import ToolpadEditor from '../toolpad/Toolpad';

export default function ToolpadEditorVite({ basename }: { basename: string }) {
return <ToolpadEditor basename={`${basename}/editor`} appUrl={basename} />;
}
1 change: 1 addition & 0 deletions packages/toolpad-app/src/exports/entrypoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../entrypoint';
4 changes: 4 additions & 0 deletions packages/toolpad-app/src/runtime/AppHostContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import * as React from 'react';

export interface RouterProps {
children?: React.ReactNode;
}

export interface AppHost {
isPreview: boolean;
isCustomServer: boolean;
Expand Down
1 change: 1 addition & 0 deletions packages/toolpad-app/src/runtime/CanvasHooksContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface CanvasHooks {
componentConfig: ComponentConfig,
elm: Element | undefined,
) => () => void;
overlayRef?: (elm: HTMLDivElement) => void;
}

export const CanvasHooksContext = React.createContext<CanvasHooks>({});
Loading

0 comments on commit 68aab7f

Please sign in to comment.