From 3bf7fcd46b13fedc2ca61a75f3f4abf171df7201 Mon Sep 17 00:00:00 2001 From: Corebreaker Date: Tue, 1 Sep 2020 14:06:08 +0200 Subject: [PATCH 1/3] Add a button in the toolbar to reset the editor --- ui/frontend/AdvancedResetMenu.tsx | 37 +++++++++++++++++++++++++++++++ ui/frontend/Header.tsx | 29 ++++++++++++++++++++++++ ui/frontend/actions.ts | 15 +++++++++++++ ui/frontend/index.scss | 2 +- ui/frontend/reducers/code.ts | 12 ++++++++++ 5 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 ui/frontend/AdvancedResetMenu.tsx diff --git a/ui/frontend/AdvancedResetMenu.tsx b/ui/frontend/AdvancedResetMenu.tsx new file mode 100644 index 000000000..b4d4ccfaa --- /dev/null +++ b/ui/frontend/AdvancedResetMenu.tsx @@ -0,0 +1,37 @@ +import React, { useCallback } from 'react'; +import { useDispatch, useSelector } from 'react-redux'; + +import ButtonMenuItem from './ButtonMenuItem'; +import MenuGroup from './MenuGroup'; + +import * as selectors from './selectors'; +import * as actions from './actions'; + +interface AdvancedResetMenuProps { + close: () => void; +} + +const AdvancedResetMenu: React.SFC = props => { + const dispatch = useDispatch(); + const resetToMinimal = useCallback(() => { + dispatch(actions.resetEditorToMinimal()); + props.close(); + }, [dispatch, props]); + const resetToHello = useCallback(() => { + dispatch(actions.resetEditorToHello()); + props.close(); + }, [dispatch, props]); + + return ( + + +
Reset the editor content with just an empty main function.
+
+ +
Reset the editor content with an "Hello World" executable.
+
+
+ ); +}; + +export default AdvancedResetMenu; diff --git a/ui/frontend/Header.tsx b/ui/frontend/Header.tsx index 88ab34ef0..80bc45e25 100644 --- a/ui/frontend/Header.tsx +++ b/ui/frontend/Header.tsx @@ -11,6 +11,7 @@ import ModeMenu from './ModeMenu'; import PopButton from './PopButton'; import { SegmentedButton, SegmentedButtonSet, SegmentedLink } from './SegmentedButton'; import ToolsMenu from './ToolsMenu'; +import AdvancedResetMenu from './AdvancedResetMenu'; import * as actions from './actions'; import * as selectors from './selectors'; @@ -30,6 +31,12 @@ const Header: React.SFC = () => ( + + + + + + @@ -126,6 +133,28 @@ const AdvancedOptionsMenuButton: React.SFC = () => { return ; } +const ResetButton: React.SFC = () => { + const dispatch = useDispatch(); + const resetAction = useCallback(() => dispatch(actions.resetEditorToEmpty()), [dispatch]); + + return ( + + Reset + + ); +}; + +const AdvancedResetMenuButton: React.SFC = () => { + const Button = React.forwardRef void }>(({ toggle }, ref) => ( + + } /> + + )); + Button.displayName = 'AdvancedResetMenuButton.Button'; + + return ; +} + const ShareButton: React.SFC = () => { const dispatch = useDispatch(); const gistSave = useCallback(() => dispatch(actions.performGistSave()), [dispatch]); diff --git a/ui/frontend/actions.ts b/ui/frontend/actions.ts index 55c59ff4c..2b48be169 100644 --- a/ui/frontend/actions.ts +++ b/ui/frontend/actions.ts @@ -118,6 +118,9 @@ export enum ActionType { RequestVersionsLoad = 'REQUEST_VERSIONS_LOAD', VersionsLoadSucceeded = 'VERSIONS_LOAD_SUCCEEDED', NotificationSeen = 'NOTIFICATION_SEEN', + ResetEditorToEmpty = 'RESET_EDITOR_TO_EMPTY', + ResetEditorToMinimal = 'RESET_EDITOR_TO_MINIMAL', + ResetEditorToHelloWorld = 'RESET_EDITOR_TO_HELLOWORLD', } const setPage = (page: Page) => @@ -420,6 +423,15 @@ export const performCompileToMir = export const performCompileToNightlyWasm = performAndSwitchPrimaryAction(performCompileToNightlyWasmOnly, PrimaryActionCore.Wasm); +export const resetEditorToEmpty = () => + createAction(ActionType.ResetEditorToEmpty); + +export const resetEditorToMinimal = () => + createAction(ActionType.ResetEditorToMinimal); + +export const resetEditorToHello = () => + createAction(ActionType.ResetEditorToHelloWorld); + export const editCode = (code: string) => createAction(ActionType.EditCode, { code }); @@ -822,4 +834,7 @@ export type Action = | ReturnType | ReturnType | ReturnType + | ReturnType + | ReturnType + | ReturnType ; diff --git a/ui/frontend/index.scss b/ui/frontend/index.scss index e06a60f24..b3a68cc02 100644 --- a/ui/frontend/index.scss +++ b/ui/frontend/index.scss @@ -460,7 +460,7 @@ $header-transition: 0.2s ease-in-out; margin-right: 0; } - &--channel-mode { + &--reset { margin-right: auto; } } diff --git a/ui/frontend/reducers/code.ts b/ui/frontend/reducers/code.ts index b64bacb39..ba02d47c4 100644 --- a/ui/frontend/reducers/code.ts +++ b/ui/frontend/reducers/code.ts @@ -4,6 +4,9 @@ const DEFAULT: State = `fn main() { println!("Hello, world!"); }`; +const EMPTY_MAIN: State = `fn main() { +}`; + export type State = string; export default function code(state = DEFAULT, action: Action): State { @@ -28,6 +31,15 @@ export default function code(state = DEFAULT, action: Action): State { case ActionType.FormatSucceeded: return action.code; + case ActionType.ResetEditorToEmpty: + return ''; + + case ActionType.ResetEditorToMinimal: + return EMPTY_MAIN; + + case ActionType.ResetEditorToHelloWorld: + return DEFAULT; + default: return state; } From ec9f6fabb6bc759395c83873605db3f5c01b2f1f Mon Sep 17 00:00:00 2001 From: Corebreaker Date: Tue, 1 Sep 2020 14:22:43 +0200 Subject: [PATCH 2/3] Fix errors detected by travis --- ui/frontend/AdvancedResetMenu.tsx | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ui/frontend/AdvancedResetMenu.tsx b/ui/frontend/AdvancedResetMenu.tsx index b4d4ccfaa..4b543e89d 100644 --- a/ui/frontend/AdvancedResetMenu.tsx +++ b/ui/frontend/AdvancedResetMenu.tsx @@ -1,10 +1,9 @@ import React, { useCallback } from 'react'; -import { useDispatch, useSelector } from 'react-redux'; +import { useDispatch } from 'react-redux'; import ButtonMenuItem from './ButtonMenuItem'; import MenuGroup from './MenuGroup'; -import * as selectors from './selectors'; import * as actions from './actions'; interface AdvancedResetMenuProps { @@ -28,7 +27,7 @@ const AdvancedResetMenu: React.SFC = props => {
Reset the editor content with just an empty main function.
-
Reset the editor content with an "Hello World" executable.
+
Reset the editor content with an "Hello World" executable.
); From 40794d4f76225151bd9d27b0cc4889943050000b Mon Sep 17 00:00:00 2001 From: Corebreaker Date: Tue, 1 Sep 2020 14:26:11 +0200 Subject: [PATCH 3/3] Fix errors detected by travis --- ui/frontend/AdvancedResetMenu.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/frontend/AdvancedResetMenu.tsx b/ui/frontend/AdvancedResetMenu.tsx index 4b543e89d..4bf4c7003 100644 --- a/ui/frontend/AdvancedResetMenu.tsx +++ b/ui/frontend/AdvancedResetMenu.tsx @@ -27,7 +27,7 @@ const AdvancedResetMenu: React.SFC = props => {
Reset the editor content with just an empty main function.
-
Reset the editor content with an "Hello World" executable.
+
Reset the editor content with an "Hello World" executable.
);