From d7cbcdfb40e2ff489824b865be507910d7b60f30 Mon Sep 17 00:00:00 2001 From: Martavis Parker <47053705+martavis@users.noreply.github.com> Date: Tue, 5 Oct 2021 12:56:47 -0700 Subject: [PATCH] Fixed query editor horizontal scroll (#2378) * much better solution to backspace problem * fixed query horiztonal scrolling * changes file --- changes/issue-2267-editor-horizontal-scroll | 1 + frontend/components/FleetAce/FleetAce.tsx | 113 ++++++++++---------- frontend/components/FleetAce/_styles.scss | 4 + 3 files changed, 61 insertions(+), 57 deletions(-) create mode 100644 changes/issue-2267-editor-horizontal-scroll diff --git a/changes/issue-2267-editor-horizontal-scroll b/changes/issue-2267-editor-horizontal-scroll new file mode 100644 index 0000000000..6fc65d0c1b --- /dev/null +++ b/changes/issue-2267-editor-horizontal-scroll @@ -0,0 +1 @@ +- Fixed query editor horizontal scrolling issue on page reload \ No newline at end of file diff --git a/frontend/components/FleetAce/FleetAce.tsx b/frontend/components/FleetAce/FleetAce.tsx index 3a1fa0d32c..cea3d8a653 100644 --- a/frontend/components/FleetAce/FleetAce.tsx +++ b/frontend/components/FleetAce/FleetAce.tsx @@ -1,7 +1,6 @@ -import React, { useCallback, useRef } from "react"; +import React, { useCallback, useEffect, useRef, useState } from "react"; import AceEditor from "react-ace"; import ReactAce from "react-ace/lib/ace"; -import { Ace } from "ace-builds"; import { IAceEditor } from "react-ace/lib/types"; import classnames from "classnames"; import "ace-builds/src-noconflict/mode-sql"; @@ -9,6 +8,8 @@ import "ace-builds/src-noconflict/ext-linking"; import "ace-builds/src-noconflict/ext-language_tools"; import { noop } from "lodash"; +import Spinner from "components/loaders/Spinner"; + import "./mode"; import "./theme"; @@ -48,19 +49,19 @@ const FleetAce = ({ handleSubmit = noop, }: IFleetAceProps) => { const editorRef = useRef(null); - const wrapperClass = classnames(wrapperClassName, { + const wrapperClass = classnames(wrapperClassName, baseClass, { [`${baseClass}__wrapper--error`]: !!error, }); - const fixCursorPosition = ( - startPosition: Ace.Point | undefined, - offset: number - ) => { - if (startPosition) { - const newColumn = startPosition.column + offset; - editorRef.current?.editor.moveCursorTo(startPosition.row, newColumn); - } - }; + const [isReady, setIsReady] = useState(false); + + useEffect(() => { + setTimeout(() => { + setIsReady(true); + }, 0); + + return () => editorRef?.current?.editor.destroy(); + }, []); const fixHotkeys = (editor: IAceEditor) => { editor.commands.removeCommand("gotoline"); @@ -69,25 +70,19 @@ const FleetAce = ({ }; const handleDelete = (deleteCommand: string) => { - let cursorOffset = 0; const currentText = editorRef.current?.editor.getValue(); const selectedText = editorRef.current?.editor.getSelectedText(); - const selectedStartPosition = editorRef.current?.editor - .getSelection() - .getCursor(); if (selectedText) { const remainingText = currentText?.replace(selectedText, ""); if (typeof remainingText !== "undefined") { onChange && onChange(remainingText); + editorRef.current?.editor.navigateLeft(); editorRef.current?.editor.clearSelection(); } } else { - cursorOffset = -1; editorRef.current?.editor.execCommand(deleteCommand); } - - fixCursorPosition(selectedStartPosition, cursorOffset); }; const renderLabel = useCallback(() => { @@ -116,44 +111,48 @@ const FleetAce = ({ return (
{renderLabel()} - handleDelete("del"), - }, - { - name: "backspaceSelection", - bindKey: { win: "Backspace", mac: "Backspace" }, - exec: () => handleDelete("backspace"), - }, - ]} - /> + {isReady ? ( + handleDelete("del"), + }, + { + name: "backspaceSelection", + bindKey: { win: "Backspace", mac: "Backspace" }, + exec: () => handleDelete("backspace"), + }, + ]} + /> + ) : ( + + )} {renderHint()}
); diff --git a/frontend/components/FleetAce/_styles.scss b/frontend/components/FleetAce/_styles.scss index bae5a44ff4..ef2803f428 100644 --- a/frontend/components/FleetAce/_styles.scss +++ b/frontend/components/FleetAce/_styles.scss @@ -44,4 +44,8 @@ font-family: "SourceCodePro", $monospace; } } + + .card { + margin: 0; + } }