Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feature(website): Toggle Tree Style #2563

Merged
merged 11 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions website/playground/pnpm-lock.yaml

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

8 changes: 6 additions & 2 deletions website/playground/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "react-tabs/style/react-tabs.css";
import init, { run } from "../pkg/rome_playground";
import { useEffect, useState } from "react";
import { IndentStyle } from "./types";
import { IndentStyle, TreeStyle } from "./types";
import { formatWithPrettier, usePlaygroundState, useWindowSize } from "./utils";
import DesktopPlayground from "./DesktopPlayground";
import { MobilePlayground } from "./MobilePlayground";
Expand All @@ -23,7 +23,7 @@ function App() {
);

const [loadingState, setLoadingState] = useState(LoadingState.Loading);
const playgroundState = usePlaygroundState();
const [playgroundState, setPlaygroundState] = usePlaygroundState();
const { width } = useWindowSize();

switch (loadingState) {
Expand All @@ -45,6 +45,7 @@ function App() {
isTypeScript,
isJsx,
sourceType,
treeStyle,
} = playgroundState;

const romeOutput = run(
Expand All @@ -55,6 +56,7 @@ function App() {
isTypeScript,
isJsx,
sourceType,
treeStyle === TreeStyle.Json,
);
const prettierOutput = formatWithPrettier(
code,
Expand All @@ -70,6 +72,7 @@ function App() {
if (width && width < 480) {
return (
<MobilePlayground
setPlaygroundState={setPlaygroundState}
playgroundState={playgroundState}
prettierOutput={prettierOutput}
romeOutput={romeOutput}
Expand All @@ -78,6 +81,7 @@ function App() {
}
return (
<DesktopPlayground
setPlaygroundState={setPlaygroundState}
playgroundState={playgroundState}
prettierOutput={prettierOutput}
romeOutput={romeOutput}
Expand Down
30 changes: 23 additions & 7 deletions website/playground/src/DesktopPlayground.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { PlaygroundProps } from "./types";
import CodeEditor from "@uiw/react-textarea-code-editor";
import { getLanguage } from "./utils";
import { createSetter, getLanguage } from "./utils";
import { Tab, TabList, TabPanel, Tabs } from "react-tabs";
import { SettingsMenu } from "./SettingsMenu";
import TreeView from "./TreeView";
import ReactJson from "react-json-view";

export default function DesktopPlayground(
{
playgroundState: { code, setCode, ...settings },
setPlaygroundState,
playgroundState: { code, treeStyle, ...settings },
prettierOutput,
romeOutput: { cst, ast, formatted_code, formatter_ir, errors },
}: PlaygroundProps,
Expand All @@ -17,15 +19,21 @@ export default function DesktopPlayground(
return (
<div className="divide-y divide-slate-300">
<h1 className="p-4 text-xl">Rome Playground</h1>
<SettingsMenu settings={settings} />
<SettingsMenu
settings={settings}
setPlaygroundState={setPlaygroundState}
/>
<div className="box-border flex h-screen divide-x divide-slate-300">
<div className="w-1/2 p-5">
<CodeEditor
value={code}
language={language}
placeholder="Enter some code here"
onChange={(evn) => {
setCode(evn.target.value);
setPlaygroundState((state) => ({
...state,
code: evn.target.value,
}));
}}
style={{
fontSize: 12,
Expand Down Expand Up @@ -76,16 +84,24 @@ export default function DesktopPlayground(
/>
</TabPanel>
<TabPanel>
<TreeView tree={JSON.parse(cst)} />
<TreeView
treeStyle={treeStyle}
setPlaygroundState={setPlaygroundState}
tree={cst}
/>
</TabPanel>
<TabPanel>
<TreeView tree={JSON.parse(ast)} />
<TreeView
treeStyle={treeStyle}
setPlaygroundState={setPlaygroundState}
tree={ast}
/>
</TabPanel>
<TabPanel>
<pre className="h-screen overflow-scroll">{formatter_ir}</pre>
</TabPanel>
<TabPanel>
<TreeView tree={prettierOutput.ir} />
<ReactJson src={prettierOutput.ir} />
</TabPanel>
<TabPanel>
<pre className="h-screen overflow-scroll whitespace-pre-wrap text-red-500 text-xs">
Expand Down
30 changes: 23 additions & 7 deletions website/playground/src/MobilePlayground.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Tab, TabList, TabPanel, Tabs } from "react-tabs";
import CodeEditor from "@uiw/react-textarea-code-editor";
import { getLanguage } from "./utils";
import { createSetter, getLanguage } from "./utils";
import { PlaygroundProps } from "./types";
import { SettingsMenu } from "./SettingsMenu";
import TreeView from "./TreeView";
import ReactJson from "react-json-view";

export function MobilePlayground(
{
playgroundState: { code, setCode, ...settings },
setPlaygroundState,
playgroundState: { code, treeStyle, ...settings },
prettierOutput,
romeOutput: { cst, ast, formatted_code, formatter_ir, errors },
}: PlaygroundProps,
Expand Down Expand Up @@ -36,7 +38,10 @@ export function MobilePlayground(
language={language}
placeholder="Enter some code here"
onChange={(evn) => {
setCode(evn.target.value);
setPlaygroundState((state) => ({
...state,
code: evn.target.value,
}));
}}
style={{
fontSize: 12,
Expand All @@ -47,7 +52,10 @@ export function MobilePlayground(
/>
</TabPanel>
<TabPanel>
<SettingsMenu settings={settings} />
<SettingsMenu
setPlaygroundState={setPlaygroundState}
settings={settings}
/>
</TabPanel>
<TabPanel>
<h1>Rome</h1>
Expand Down Expand Up @@ -78,16 +86,24 @@ export function MobilePlayground(
/>
</TabPanel>
<TabPanel>
<TreeView tree={JSON.parse(cst)} />
<TreeView
tree={cst}
treeStyle={treeStyle}
setPlaygroundState={setPlaygroundState}
/>
</TabPanel>
<TabPanel>
<TreeView tree={JSON.parse(ast)} />
<TreeView
tree={ast}
treeStyle={treeStyle}
setPlaygroundState={setPlaygroundState}
/>
</TabPanel>
<TabPanel>
<pre className="h-screen overflow-y-scroll">{formatter_ir}</pre>
</TabPanel>
<TabPanel>
<TreeView tree={prettierOutput.ir} />
<ReactJson src={prettierOutput.ir} />
</TabPanel>
<TabPanel>
<pre className="h-screen overflow-y-scroll whitespace-pre-wrap text-red-500 text-xs">
Expand Down
34 changes: 18 additions & 16 deletions website/playground/src/SettingsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,55 @@ import LineWidthInput from "./LineWidthInput";
import IndentStyleSelect from "./IndentStyleSelect";
import QuoteStyleSelect from "./QuoteStyleSelect";
import SourceTypeSelect from "./SourceTypeSelect";
import { PlaygroundSettings } from "./types";
import { PlaygroundSettings, PlaygroundState } from "./types";
import { Dispatch, SetStateAction } from "react";
import { createSetter } from "./utils";

interface Props { settings: PlaygroundSettings }
interface Props {
settings: PlaygroundSettings;
setPlaygroundState: Dispatch<SetStateAction<PlaygroundState>>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaReiser I'm using the complicated type signature here because I'm making use of the setPlaygroundState(state => ...) pattern

}

export function SettingsMenu(
{
setPlaygroundState,
settings: {
lineWidth,
setLineWidth,
indentWidth,
setIndentWidth,
indentStyle,
setIndentStyle,
quoteStyle,
setQuoteStyle,
sourceType,
setSourceType,
isTypeScript,
setIsTypeScript,
isJsx,
setIsJsx,
},
}: Props,
) {
return (
<div>
<div className="flex flex-col sm:flex-row">
<LineWidthInput lineWidth={lineWidth} setLineWidth={setLineWidth} />
<LineWidthInput
lineWidth={lineWidth}
setLineWidth={createSetter(setPlaygroundState, "lineWidth")}
/>
<IndentStyleSelect
indentWidth={indentWidth}
setIndentWidth={setIndentWidth}
setIndentWidth={createSetter(setPlaygroundState, "indentWidth")}
indentStyle={indentStyle}
setIndentStyle={setIndentStyle}
setIndentStyle={createSetter(setPlaygroundState, "indentStyle")}
/>
</div>
<div className="flex flex-col sm:flex-row">
<QuoteStyleSelect
quoteStyle={quoteStyle}
setQuoteStyle={setQuoteStyle}
setQuoteStyle={createSetter(setPlaygroundState, "quoteStyle")}
/>
<SourceTypeSelect
isTypeScript={isTypeScript}
setIsTypeScript={setIsTypeScript}
setIsTypeScript={createSetter(setPlaygroundState, "isTypeScript")}
isJsx={isJsx}
setIsJsx={setIsJsx}
setIsJsx={createSetter(setPlaygroundState, "isJsx")}
sourceType={sourceType}
setSourceType={setSourceType}
setSourceType={createSetter(setPlaygroundState, "sourceType")}
/>
</div>
</div>
Expand Down
54 changes: 54 additions & 0 deletions website/playground/src/TreeStyleSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { classNames } from "./utils";
import { TreeStyle } from "./types";
import { Dispatch, SetStateAction } from "react";

interface Props {
treeStyle: TreeStyle;
setTreeStyle: (treeStyle: TreeStyle) => void;
}

export default function TreeStyleSelect({ treeStyle, setTreeStyle }: Props) {
return (
<div className="group p-0.5 rounded-lg flex bg-gray-200 mb-4 m-2 w-fit">
<button
type="button"
onClick={() => setTreeStyle(TreeStyle.Json)}
className={classNames(
"flex focus-visible:ring-2 focus-visible:ring-teal-500 focus-visible:ring-offset-2 rounded-md focus:outline-none focus-visible:ring-offset-gray-100",
treeStyle === TreeStyle.Json &&
"bg-white shadow-sm ring-1 ring-black ring-opacity-5"
)}
>
<span
className={classNames(
"p-1.5 lg:pl-2.5 lg:pr-3.5 rounded-md flex items-center text-sm font-medium",
treeStyle === TreeStyle.Json &&
"bg-white shadow-sm ring-1 ring-black ring-opacity-5"
)}
>
<span
className={classNames(
"text-gray-900",
treeStyle === TreeStyle.Json
? "text-gray-900"
: "text-gray-600 group-hover:text-gray-900"
)}
>
JSON
</span>
</span>
</button>
<button
type="button"
onClick={() => setTreeStyle(TreeStyle.Text)}
className={classNames(
"ml-0.5 p-1.5 lg:pl-2.5 lg:pr-3.5 rounded-md flex items-center text-sm text-gray-600 font-medium focus-visible:ring-2 focus-visible:ring-teal-500 focus-visible:ring-offset-2 focus:outline-none focus-visible:ring-offset-gray-100",
treeStyle === TreeStyle.Text &&
"bg-white shadow-sm ring-1 ring-black ring-opacity-5"
)}
>
<span className="text-gray-900">Text</span>
</button>
</div>
);
}
Loading