Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Garrett/plugin toggle state #145

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
53 changes: 29 additions & 24 deletions src/components/CompiledOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { gzipSize } from "../gzip";
import { Wrapper, Code, Config } from "./styles";
import { useDebounce } from "../utils/useDebounce";
import Transition from "./Transitions";
import { PluginPanel } from "./PluginPanel";

import { plugins, presets } from "../plugins";
import VizOutput from "./AST/Viz";
Expand Down Expand Up @@ -73,10 +74,16 @@ export function CompiledOutput({

useEffect(saveConfig, [source, config, debouncedPlugin]);

// Bind config and stringConfig changes to each other
useEffect(() => {
setStringConfig(JSON.stringify(config, null, "\t"));
}, [config]);

useEffect(() => {
config = JSON.parse(stringConfig);
onConfigChange(config);
}, [stringConfig])

useEffect(() => {
try {
let sconfig = JSON.parse(stringConfig);
Expand Down Expand Up @@ -106,21 +113,6 @@ export function CompiledOutput({
return timeTravel.slice(0, timeTravelIndex).map(t => t.pluginAlias);
}, [timeTravel, timeTravelIndex, configOpts]);

function displayAvailablePlugins() {
return Object.keys(plugins).map(pluginName => {
return (
<Segment key={pluginName}>
<Checkbox
toggle
name={pluginName}
type="checkbox"
onChange={handlePluginChange}
label={pluginName}
/>
</Segment>
);
});
}

function displayAvailablePresets() {
return Object.keys(presets).map(presetName => {
Expand Down Expand Up @@ -174,7 +166,6 @@ export function CompiledOutput({
}

function handleStringConfigChange(configText) {

setStringConfig(configText);

let sConfig = {};
Expand All @@ -187,7 +178,7 @@ export function CompiledOutput({
onConfigChange(sConfig);
}

const sourceCode = compiled ?.code ?? "";
const sourceCode = compiled?.code ?? "";
return (
<>
<SplitPane minSize={40} defaultSize={300}>
Expand Down Expand Up @@ -243,9 +234,9 @@ export function CompiledOutput({
*/
if (timeTravel !== null) {
setDisplayAtIndex(
`${timeTravel[timeTravelIndex - 1] ?.currentNode}`
`${timeTravel[timeTravelIndex - 1]?.currentNode}`
);
setTimeTravelCode(`${timeTravel[timeTravelIndex - 1] ?.code}`);
setTimeTravelCode(`${timeTravel[timeTravelIndex - 1]?.code}`);
if (timeTravelIndex !== timeTravel.length) {
setTimeTravelIndex(timeTravelIndex + 1);
}
Expand All @@ -255,8 +246,22 @@ export function CompiledOutput({
/>
</Menu.Menu>
</Menu>
<Segment.Group id="plugins">{displayAvailablePlugins()}</Segment.Group>
<Segment.Group id="plugins">{displayAvailablePresets()}</Segment.Group>
{/* <Segment.Group id="plugins">{displayAvailablePlugins()}</Segment.Group>
<Segment.Group id="plugins">{displayAvailablePresets()}</Segment.Group> */}
<Segment.Group piled>
<PluginPanel
pluginList={plugins}
onChange={handlePluginChange}
stringConfig={stringConfig}
/>
</Segment.Group>
<Segment.Group piled>
<PluginPanel
pluginList={presets}
onChange={handlePresetChange}
stringConfig={stringConfig}
/>
</Segment.Group>
<Wrapper>
<Config
value={stringConfig}
Expand Down Expand Up @@ -289,7 +294,7 @@ export function CompiledOutput({
</Menu.Menu>
<Menu.Menu position="right">
<Menu.Item>
{compiled ?.size}b, {gzip}b
{compiled?.size}b, {gzip}b
</Menu.Item>
<Menu.Item onClick={removeConfig}>
<Icon name="close" />
Expand All @@ -307,11 +312,11 @@ export function CompiledOutput({
) : (
<Code
value={
timeTravelCode !== undefined ? timeTravelCode : compiled ?.code
timeTravelCode !== undefined ? timeTravelCode : compiled?.code
}
docName="result.js"
config={{ readOnly: true, lineWrapping: true }}
isError={compiled ?.error ?? false}
isError={compiled?.error ?? false}
/>
)}

Expand Down
24 changes: 24 additions & 0 deletions src/components/PluginPanel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react";
import { Checkbox, Segment } from "semantic-ui-react";
import {PluginToggle} from "./PluginToggle";

export function PluginPanel({
pluginList,
onChange,
stringConfig,
}) {
return (
<>
{Object.keys(pluginList).map(pluginName => {
return (
<PluginToggle
pluginName={pluginName}
onChange={onChange}
stringConfig={stringConfig}
/>
);
})}

</>
)
}
26 changes: 26 additions & 0 deletions src/components/PluginToggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, {useState, useEffect} from "react";
import {Segment, Checkbox} from "semantic-ui-react";

export function PluginToggle({
pluginName,
onChange,
stringConfig,
}) {
const [checked, setChecked] = useState(false);
useEffect(() => {
// Crude implementation
setChecked(stringConfig.includes(pluginName));
}, [stringConfig]);
return (
<Segment key={pluginName}>
<Checkbox
toggle
name={pluginName}
type="checkbox"
onChange={onChange}
label={pluginName}
checked={checked}
/>
</Segment>
);
}