From 4166c09bd4eb39b40464cb21718d89c4121005c2 Mon Sep 17 00:00:00 2001 From: Snarling <84951833+Snarling@users.noreply.github.com> Date: Fri, 24 Feb 2023 07:37:29 -0500 Subject: [PATCH] More script editor options (#386) * Options are responsive again (fix from previous changes) * Better height control on the monaco container using flexbox. * Added options for tab size, auto-detect indentation per-file, font family, and font ligatures. --- src/ScriptEditor/ui/Editor.tsx | 11 ++-- src/ScriptEditor/ui/Options.ts | 8 ++- src/ScriptEditor/ui/OptionsModal.tsx | 79 +++++++++++++++++------- src/ScriptEditor/ui/ScriptEditorRoot.tsx | 33 ++++++---- src/Settings/Settings.ts | 11 +++- 5 files changed, 99 insertions(+), 43 deletions(-) diff --git a/src/ScriptEditor/ui/Editor.tsx b/src/ScriptEditor/ui/Editor.tsx index 235dd2972..d228ee05b 100644 --- a/src/ScriptEditor/ui/Editor.tsx +++ b/src/ScriptEditor/ui/Editor.tsx @@ -5,8 +5,6 @@ import { useEffect, useRef } from "react"; export type Monaco = typeof monaco; type EditorProps = { - /** css height of editor */ - height: string; /** Editor options */ options: monaco.editor.IEditorOptions; /** Function to be ran prior to mounting editor */ @@ -17,7 +15,7 @@ type EditorProps = { onChange: (newCode?: string) => void; }; -export function Editor({ height, options, beforeMount, onMount, onChange }: EditorProps) { +export function Editor({ options, beforeMount, onMount, onChange }: EditorProps) { const containerDiv = useRef(null); const editor = useRef(null); const subscription = useRef(null); @@ -43,12 +41,11 @@ export function Editor({ height, options, beforeMount, onMount, onChange }: Edit // Unmounting return () => { - editor.current?.dispose(); - const model = editor.current?.getModel(); - model?.dispose(); subscription.current?.dispose(); + editor.current?.getModel()?.dispose(); + editor.current?.dispose(); }; }, []); - return
; + return
; } diff --git a/src/ScriptEditor/ui/Options.ts b/src/ScriptEditor/ui/Options.ts index 2e7e84bf6..f985fb552 100644 --- a/src/ScriptEditor/ui/Options.ts +++ b/src/ScriptEditor/ui/Options.ts @@ -1,8 +1,12 @@ export type WordWrapOptions = "on" | "off" | "bounded" | "wordWrapColumn"; -export interface Options { +export type Options = { theme: string; insertSpaces: boolean; + tabSize: number; + detectIndentation: boolean; + fontFamily: string; fontSize: number; + fontLigatures: boolean; wordWrap: WordWrapOptions; vim: boolean; -} +}; diff --git a/src/ScriptEditor/ui/OptionsModal.tsx b/src/ScriptEditor/ui/OptionsModal.tsx index 1e0d622f4..fba5f247d 100644 --- a/src/ScriptEditor/ui/OptionsModal.tsx +++ b/src/ScriptEditor/ui/OptionsModal.tsx @@ -3,7 +3,6 @@ import { Options, WordWrapOptions } from "./Options"; import { Modal } from "../../ui/React/Modal"; import Button from "@mui/material/Button"; -import Box from "@mui/material/Box"; import Typography from "@mui/material/Typography"; import Select from "@mui/material/Select"; import Switch from "@mui/material/Switch"; @@ -24,7 +23,11 @@ interface IProps { export function OptionsModal(props: IProps): React.ReactElement { const [theme, setTheme] = useState(props.options.theme); const [insertSpaces, setInsertSpaces] = useState(props.options.insertSpaces); + const [tabSize, setTabSize] = useState(props.options.tabSize); + const [detectIndentation, setDetectIndentation] = useState(props.options.detectIndentation); + const [fontFamily, setFontFamily] = useState(props.options.fontFamily); const [fontSize, setFontSize] = useState(props.options.fontSize); + const [fontLigatures, setFontLigatures] = useState(props.options.fontLigatures); const [wordWrap, setWordWrap] = useState(props.options.wordWrap); const [vim, setVim] = useState(props.options.vim); const [themeEditorOpen, setThemeEditorOpen] = useState(false); @@ -33,23 +36,33 @@ export function OptionsModal(props: IProps): React.ReactElement { props.save({ theme, insertSpaces, + tabSize, + detectIndentation, + fontFamily, fontSize, + fontLigatures, wordWrap, vim, }); props.onClose(); } - function onFontChange(event: React.ChangeEvent): void { - const f = parseFloat(event.target.value); - if (isNaN(f)) return; - setFontSize(f); + function onFontSizeChange(event: React.ChangeEvent): void { + const n = parseInt(event.target.value); + if (!Number.isFinite(n) || n < 1) return; + setFontSize(n); + } + + function onTabSizeChange(event: React.ChangeEvent): void { + const n = parseInt(event.target.value); + if (!Number.isFinite(n) || n < 1) return; + setTabSize(n); } return ( setThemeEditorOpen(false)} /> - +
Theme: - - +
- - Use whitespace over tabs: - setInsertSpaces(event.target.checked)} checked={insertSpaces} /> - +
+ Indent using tabs: + setInsertSpaces(e.target.checked)} checked={insertSpaces} /> +
- - Word Wrap: +
+ Tab size: + +
+ +
+ Auto-detect indentation: + setDetectIndentation(e.target.checked)} checked={detectIndentation} /> +
+ +
+ Word wrap: - +
- - Enable vim mode: - setVim(event.target.checked)} checked={vim} /> - +
+ Enable vim mode: + setVim(e.target.checked)} checked={vim} /> +
+ +
+ Font family: + setFontFamily(e.target.value)} /> +
+ +
+ Font size: + +
+ +
+ Enable font ligatures: + setFontLigatures(e.target.checked)} checked={fontLigatures} /> +
- - -