From 6f4474beab68c778153c362510fe0483fbeb2153 Mon Sep 17 00:00:00 2001 From: naumov Date: Sun, 14 Jan 2024 15:42:21 +0100 Subject: [PATCH 1/6] CB-4134 adjust cursor position --- .../core-blocks/src/FormControls/Textarea.tsx | 15 ++++- .../SQLCodeEditorPanel/SQLCodeEditorPanel.tsx | 58 +++++++++++++++++++ .../src/SqlDataSource/BaseSqlDataSource.ts | 10 ++-- .../src/SqlDataSource/ISqlDataSource.ts | 6 +- .../LocalStorage/LocalStorageSqlDataSource.ts | 5 +- .../src/SqlEditor/useSqlEditor.ts | 2 +- 6 files changed, 83 insertions(+), 13 deletions(-) diff --git a/webapp/packages/core-blocks/src/FormControls/Textarea.tsx b/webapp/packages/core-blocks/src/FormControls/Textarea.tsx index 58c9795dff..d6bf36f63b 100644 --- a/webapp/packages/core-blocks/src/FormControls/Textarea.tsx +++ b/webapp/packages/core-blocks/src/FormControls/Textarea.tsx @@ -6,7 +6,7 @@ * you may not use this file except in compliance with the License. */ import { observer } from 'mobx-react-lite'; -import { useCallback, useContext } from 'react'; +import { useCallback, useContext, useEffect, useRef } from 'react'; import { filterLayoutFakeProps, getLayoutProps } from '../Containers/filterLayoutFakeProps'; import type { ILayoutSizeProps } from '../Containers/ILayoutSizeProps'; @@ -23,6 +23,7 @@ type BaseProps = Omit, 'onChan description?: string; labelTooltip?: string; embedded?: boolean; + cursorInitiallyAtEnd?: boolean; }; type ControlledProps = BaseProps & { @@ -54,9 +55,11 @@ export const Textarea: TextareaType = observer(function Textarea({ description, labelTooltip, embedded, + cursorInitiallyAtEnd, onChange = () => {}, ...rest }: ControlledProps | ObjectProps) { + const textareaRef = useRef(null); const layoutProps = getLayoutProps(rest); rest = filterLayoutFakeProps(rest); const styles = useS(textareaStyle); @@ -79,13 +82,21 @@ export const Textarea: TextareaType = observer(function Textarea({ const value = state ? state[name] : controlledValue; + useEffect(() => { + if (cursorInitiallyAtEnd && typeof value === 'string') { + const position = value.trim().length; + textareaRef.current?.setSelectionRange(position, position); + } + }, []); + return ( - + {children}