From 65be041f770d1f6abbe21df60629676ca6cd78d6 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 8 Jan 2022 22:01:01 +0000 Subject: [PATCH] auto scroll to selected line --- src/components/EditorTextArea.js | 34 ++++++++++++++++++++------------ src/style/themes.js | 6 +----- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/components/EditorTextArea.js b/src/components/EditorTextArea.js index 43ee125..51ce037 100644 --- a/src/components/EditorTextArea.js +++ b/src/components/EditorTextArea.js @@ -16,7 +16,7 @@ export default function EditorTextArea({ const [monaco, setMonaco] = useState(); const [selected, toggleSelected] = useSelectedLine(); const editorAreaRef = useRef(); - useLineNumberMagic(editorAreaRef, selected, toggleSelected, editor, monaco); + useLineNumberMagic(editorAreaRef, selected, toggleSelected, forcedContent, editor, monaco); usePlaceholderText(actualContent, editor, monaco); function beforeMount(monaco) { @@ -112,6 +112,7 @@ function useLineNumberMagic( editorAreaRef, selected, toggleSelected, + forcedContent, editor, monaco ) { @@ -134,27 +135,34 @@ function useLineNumberMagic( }, [editorAreaRef, toggleSelected]); // apply a 'highlighed' decoration to the selected lines + // and scroll them into view if not already useEffect(() => { - if (!editor || !monaco) { + if (!editor || !monaco || selected[0] === -1) { return; } - const newDecorations = []; - if (selected[0] !== -1) { - newDecorations.push({ - range: new monaco.Range(selected[0], 1, selected[1], 1), - options: { - isWholeLine: true, - linesDecorationsClassName: 'highlighted-line', - }, - }); - } + // apply a decoration + const newDecorations = [{ + range: new monaco.Range(selected[0], 1, selected[1], 1), + options: { + isWholeLine: true, + linesDecorationsClassName: 'highlighted-line', + }, + }]; const decorations = editor.deltaDecorations([], newDecorations); + // scroll the selected line into view + if (selected[0] === selected[1]) { + editor.revealLineInCenterIfOutsideViewport(selected[0]); + } else { + editor.revealLinesInCenterIfOutsideViewport(selected[0], selected[1]); + } + + // cleanup by removing the decorations return () => { editor.deltaDecorations(decorations, []); }; - }, [editor, monaco, selected]); + }, [editor, monaco, selected, forcedContent]); } function useSelectedLine() { diff --git a/src/style/themes.js b/src/style/themes.js index d2e95ee..ef8ba11 100644 --- a/src/style/themes.js +++ b/src/style/themes.js @@ -157,7 +157,7 @@ export function makeMonacoTheme(theme) { }, { token: 'identifier', - foreground: theme.editor.function.substring(1), + foreground: theme.editor.primary.substring(1), }, { token: 'type', @@ -167,10 +167,6 @@ export function makeMonacoTheme(theme) { token: 'comment', foreground: theme.editor.comment.substring(1), }, - { - token: 'identifier.java', - foreground: theme.editor.primary.substring(1), - }, ], colors: { 'editor.background': theme.editor.background,