import { Button, ButtonGroup, Callout, Checkbox, FormGroup, InputGroup, Radio, RadioGroup, } from '@blueprintjs/core'; import { observer } from 'mobx-react'; import * as React from 'react'; import { IPackageManager } from '../npm'; import { AppState } from '../state'; export enum SettingItemType { EnvVars = 'environmentVariables', Flags = 'executionFlags', } interface ExecutionSettingsProps { appState: AppState; } interface ExecutionSettingsState { [setting: string]: Record; } /** * Settings content to manage execution-related preferences. * * @class ExecutionSettings * @extends {React.Component} */ @observer export class ExecutionSettings extends React.Component< ExecutionSettingsProps, ExecutionSettingsState > { constructor(props: ExecutionSettingsProps) { super(props); this.state = { executionFlags: Object.assign( {}, ...props.appState.executionFlags.map((flag, idx) => { return { [idx]: flag }; }), ), environmentVariables: Object.assign( {}, ...props.appState.environmentVariables.map((envVar, idx) => { return { [idx]: envVar }; }), ), }; this.handleDeleteDataChange = this.handleDeleteDataChange.bind(this); this.handleElectronLoggingChange = this.handleElectronLoggingChange.bind( this, ); this.handleSettingsItemChange = this.handleSettingsItemChange.bind(this); this.addNewSettingsItem = this.addNewSettingsItem.bind(this); } public componentDidMount() { const { environmentVariables, executionFlags } = this.state; if (Object.keys(executionFlags).length === 0) { this.addNewSettingsItem(SettingItemType.Flags); } if (Object.keys(environmentVariables).length === 0) { this.addNewSettingsItem(SettingItemType.EnvVars); } } public componentDidUpdate() { const { appState } = this.props; for (const type of Object.values(SettingItemType)) { const values = Object.values(this.state[type]); appState[type] = values.filter((v) => v !== ''); } } /** * Handles a change on whether or not the user data dir should be deleted * after a run. * * @param {React.ChangeEvent} event */ public handleDeleteDataChange(event: React.FormEvent) { const { checked } = event.currentTarget; this.props.appState.isKeepingUserDataDirs = checked; } /** * Handles a change on whether or not electron should log more things * * @param {React.ChangeEvent} event */ public handleElectronLoggingChange(event: React.FormEvent) { const { checked } = event.currentTarget; this.props.appState.isEnablingElectronLogging = checked; } /** * Handles a change in the execution flags or environment variables * run with the Electron executable. * * @param {React.ChangeEvent} event * @param {SettingItemType} type */ public handleSettingsItemChange( event: React.ChangeEvent, type: SettingItemType, ) { const { name, value } = event.currentTarget; this.setState((prevState) => ({ [type]: { ...prevState[type], [name]: value, }, })); } /** * Adds a new settings item input field. * * @param {SettingItemType} type */ private addNewSettingsItem(type: SettingItemType) { const array = Object.entries(this.state[type]); this.setState((prevState) => ({ [type as any]: { ...prevState[type], [array.length]: '', }, })); } /** * Handle a change to the package manager used to install modules when running * Fiddles; * * @param {React.FormEvent} event */ private handlePMChange = (event: React.FormEvent) => { const { appState } = this.props; const { value } = event.currentTarget; appState.packageManager = value as IPackageManager; }; public renderDeleteItem(idx: string, type: SettingItemType): JSX.Element { const updated = this.state[type]; const removeFn = () => { if (Object.keys(updated).length === 1) { updated[idx] = ''; } else { delete updated[idx]; } this.setState({ [type]: updated }); }; return (
{this.renderEnvironmentVariables()}
Electron Fiddle will install packages on runtime if they are imported within your fiddle with require. It uses{' '} npm {' '} as its package manager by default, but{' '} Yarn {' '} is also available. ); } }