From 3e93752d53de8bd4cdbdd60d5bf9eb71d8d40b99 Mon Sep 17 00:00:00 2001 From: alex <48489896+devnaumov@users.noreply.github.com> Date: Thu, 25 Jan 2024 08:47:47 +0100 Subject: [PATCH] CB-4134 adjust cursor position (#2299) * CB-4134 adjust cursor position * CB-4134 pass controlled cursor to the editor * CB-4134 do not trim value * CB-4134 revert const history * CB-4134 review fixes * CB-4134 remove onUpdate method --------- Co-authored-by: Evgenia Bezborodova <139753579+EvgeniaBzzz@users.noreply.github.com> --- .../core-blocks/src/FormControls/Textarea.tsx | 15 ++++++- .../src/IReactCodemirrorProps.ts | 10 ++++- .../src/ReactCodemirror.tsx | 44 +++++++++++++++---- .../SQLCodeEditorPanel/SQLCodeEditorPanel.tsx | 6 ++- .../useSQLCodeEditorPanel.ts | 18 +++----- .../src/SqlEditor/useSqlEditor.ts | 3 +- 6 files changed, 68 insertions(+), 28 deletions(-) diff --git a/webapp/packages/core-blocks/src/FormControls/Textarea.tsx b/webapp/packages/core-blocks/src/FormControls/Textarea.tsx index 58c9795dff..8e879faf66 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, useLayoutEffect, 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; + useLayoutEffect(() => { + if (cursorInitiallyAtEnd && typeof value === 'string') { + const position = value.length; + textareaRef.current?.setSelectionRange(position, position); + } + }, [cursorInitiallyAtEnd]); + return ( - + {children}