Skip to content

Commit

Permalink
Add support for resizing panes in the playground (#2581)
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheeguerin authored Oct 18, 2023
1 parent f39f3a7 commit 9d2cf85
Show file tree
Hide file tree
Showing 13 changed files with 515 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/playground",
"comment": "Add resizable panes for the editor and output",
"type": "none"
}
],
"packageName": "@typespec/playground"
}
52 changes: 43 additions & 9 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/playground/definitions/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module "*.module.css";
8 changes: 5 additions & 3 deletions packages/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
},
"scripts": {
"clean": "rimraf ./dist ./dist-dev ./temp ./typespecContents.json",
"build": "tsc -p .",
"watch": "tsc -p . --watch",
"build": "tsc -p . && npm run copy-css",
"watch": "npm run copy-css && tsc -p . --watch",
"copy-css": "copyfiles -u 1 src/**/*.module.css dist/src",
"preview": "npm run build && vite preview",
"start": "vite",
"e2e": "cross-env PW_EXPERIMENTAL_TS_ESM=1 playwright test -c e2e ",
Expand Down Expand Up @@ -89,6 +90,7 @@
"rimraf": "~5.0.1",
"rollup-plugin-visualizer": "~5.9.2",
"typescript": "~5.2.2",
"vite": "^4.4.9"
"vite": "^4.4.9",
"copyfiles": "~2.4.1"
}
}
2 changes: 1 addition & 1 deletion packages/playground/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export { createBrowserHost } from "./browser-host.js";
export { registerMonacoDefaultWorkers } from "./monaco-worker.js";
export { registerMonacoLanguage } from "./services.js";
export { createUrlStateStorage } from "./state-storage.js";
export { PlaygroundSample } from "./types.js";
export type { PlaygroundSample } from "./types.js";
export { resolveLibraries as filterEmitters } from "./utils.js";
3 changes: 2 additions & 1 deletion packages/playground/src/react/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Playground, PlaygroundProps } from "./playground.js";
export { Playground } from "./playground.js";
export type { PlaygroundProps } from "./playground.js";
export { createReactPlayground, renderReactPlayground } from "./standalone.js";
export * from "./types.js";
61 changes: 30 additions & 31 deletions packages/playground/src/react/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { useMonacoModel } from "./editor.js";
import { Footer } from "./footer.js";
import { useAsyncMemo, useControllableValue } from "./hooks.js";
import { OutputView } from "./output-view.js";
import Pane from "./split-pane/pane.js";
import { SplitPane } from "./split-pane/split-pane.js";
import { CompilationState, FileOutputViewer } from "./types.js";
import { TypeSpecEditor } from "./typespec-editor.js";

Expand Down Expand Up @@ -208,45 +210,42 @@ export const Playground: FunctionComponent<PlaygroundProps> = (props) => {
<div
css={{
display: "grid",
gridTemplateColumns: "repeat(2, 1fr)",
gridTemplateColumns: "1",
gridTemplateRows: "1fr auto",
gridTemplateAreas: '"typespeceditor output"\n "footer footer"',
gridTemplateAreas: '"typespeceditor"\n "footer"',
width: "100%",
height: "100%",
overflow: "hidden",
fontFamily: `"Segoe UI", Tahoma, Geneva, Verdana, sans-serif`,
}}
>
<div css={{ gridArea: "typespeceditor", width: "100%", height: "100%", overflow: "hidden" }}>
<EditorCommandBar
libraries={libraries}
selectedEmitter={selectedEmitter}
onSelectedEmitterChange={onSelectedEmitterChange}
compilerOptions={compilerOptions}
onCompilerOptionsChange={onCompilerOptionsChange}
samples={props.samples}
selectedSampleName={selectedSampleName}
onSelectedSampleNameChange={onSelectedSampleNameChange}
saveCode={saveCode}
newIssue={props?.links?.githubIssueUrl ? newIssue : undefined}
documentationUrl={props.links?.documentationUrl}
/>
<TypeSpecEditor model={typespecModel} actions={typespecEditorActions} />
</div>
<div
css={{
gridArea: "output",
overflow: "hidden",
display: "flex",
flexDirection: "column",
borderLeft: "1px solid #c5c5c5",
}}
<SplitPane
initialSizes={["50%", "50%"]}
css={{ gridArea: "typespeceditor", width: "100%", height: "100%", overflow: "hidden" }}
>
<OutputView
compilationState={compilationState}
viewers={props.emitterViewers?.[selectedEmitter]}
/>
</div>
<Pane>
<EditorCommandBar
libraries={libraries}
selectedEmitter={selectedEmitter}
onSelectedEmitterChange={onSelectedEmitterChange}
compilerOptions={compilerOptions}
onCompilerOptionsChange={onCompilerOptionsChange}
samples={props.samples}
selectedSampleName={selectedSampleName}
onSelectedSampleNameChange={onSelectedSampleNameChange}
saveCode={saveCode}
newIssue={props?.links?.githubIssueUrl ? newIssue : undefined}
documentationUrl={props.links?.documentationUrl}
/>
<TypeSpecEditor model={typespecModel} actions={typespecEditorActions} />
</Pane>
<Pane>
<OutputView
compilationState={compilationState}
viewers={props.emitterViewers?.[selectedEmitter]}
/>
</Pane>
</SplitPane>
<Footer />
</div>
);
Expand Down
1 change: 1 addition & 0 deletions packages/playground/src/react/split-pane/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { SplitPane, SplitPaneProps } from "./split-pane.js";
13 changes: 13 additions & 0 deletions packages/playground/src/react/split-pane/pane.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { HTMLAttributes, PropsWithChildren } from "react";

export interface PaneProps {
maxSize?: number | string;
minSize?: number | string;
}

export default function Pane({
children,
...props
}: PropsWithChildren<HTMLAttributes<HTMLDivElement> & PaneProps>) {
return <div {...props}>{children}</div>;
}
29 changes: 29 additions & 0 deletions packages/playground/src/react/split-pane/sash-content.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { mergeClasses } from "@fluentui/react-components";
import { ReactNode } from "react";
import style from "./split-pane.module.css";

export interface SashContentProps {
className?: string;
dragging?: boolean;
children?: ReactNode;
}

export const SashContent: React.FunctionComponent<SashContentProps> = ({
className,
children,
dragging,
...others
}: SashContentProps) => {
return (
<div
className={mergeClasses(
style["sash-content"],
dragging && style["sash-content-dragging"],
className
)}
{...others}
>
{children}
</div>
);
};
53 changes: 53 additions & 0 deletions packages/playground/src/react/split-pane/sash.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { mergeClasses } from "@fluentui/react-components";
import { ReactNode, useState } from "react";
import style from "./split-pane.module.css";

export interface SashProps {
className?: string;
style: React.CSSProperties;
render: (dragging: boolean) => ReactNode;
onReset: () => void;
onDragStart: React.MouseEventHandler<HTMLDivElement>;
onDragging: React.MouseEventHandler<HTMLDivElement>;
onDragEnd: React.MouseEventHandler<HTMLDivElement>;
}

export const Sash = ({
className,
render,
onDragStart,
onDragging,
onDragEnd,
onReset,
...others
}: SashProps) => {
const [draging, setDrag] = useState(false);

const handleMouseMove = (e: any) => {
onDragging(e);
};

const handleMouseUp = (e: any) => {
setDrag(false);
onDragEnd(e);
window.removeEventListener("mousemove", handleMouseMove);
window.removeEventListener("mouseup", handleMouseUp);
};

return (
<div
className={mergeClasses(style["sash"], className)}
onMouseDown={(e) => {
setDrag(true);
onDragStart(e);

window.addEventListener("mousemove", handleMouseMove);
window.addEventListener("mouseup", handleMouseUp);
}}
onDoubleClick={onReset}
{...others}
>
{render(draging)}
</div>
);
};
Loading

0 comments on commit 9d2cf85

Please sign in to comment.