TagSpaces is an offline, open source, document manager with tagging support
chore: improving copy and paste from menu in textfields
Ilian Sapundshievc887318272fe
diff --git a/src/renderer/components/TsTextField.tsx b/src/renderer/components/TsTextField.tsxindex b2c35e766..137a443ef 100644--- a/src/renderer/components/TsTextField.tsx+++ b/src/renderer/components/TsTextField.tsx@@ -42,7 +42,9 @@ function TsTextField(props: TSTextFieldProps) {const theme = useTheme();const { t } = useTranslation();const desktopMode = useSelector(isDesktopMode);- //const textFieldRef = useRef(null);+ const textFieldRef = React.useRef<HTMLInputElement | HTMLTextAreaElement>(+ null,+ );const [contextMenu, setContextMenu] = React.useState<{mouseX: number;@@ -86,14 +88,31 @@ function TsTextField(props: TSTextFieldProps) {if (updateValue) {navigator.clipboard.readText().then((clipText) => {const value = retrieveValue ? retrieveValue() : '';- const selection = document.getSelection()?.toString();- let result = '';- if (selection && value?.includes(selection)) {- result = value.replace(selection, clipText);+ const inputElement = textFieldRef.current;+ if (inputElement) {+ const selectionStart = inputElement.selectionStart || 0;+ const selectionEnd = inputElement.selectionEnd || 0;++ // Insert clipboard text at cursor position, replacing any selected text+ const result =+ value.substring(0, selectionStart) ++ clipText ++ value.substring(selectionEnd);+ const newCursorPosition = selectionStart + clipText.length;++ updateValue(result);++ // Move cursor to after the pasted text after the DOM has updated+ setTimeout(() => {+ if (textFieldRef.current) {+ textFieldRef.current.selectionStart =+ textFieldRef.current.selectionEnd = newCursorPosition;+ }+ }, 0);} else {- result = value + clipText;+ // Fallback if ref is not available+ updateValue(value + clipText);}- updateValue(result);handleClose();});}@@ -107,6 +126,7 @@ function TsTextField(props: TSTextFieldProps) {</FormHelperText>)}<TextField+ inputRef={textFieldRef}onContextMenu={handleContextMenu}margin="dense"size={desktopMode ? 'small' : 'medium'}diff --git a/src/renderer/components/chat/ChatView.tsx b/src/renderer/components/chat/ChatView.tsxindex 6c9e3e6ff..e0137c96d 100644--- a/src/renderer/components/chat/ChatView.tsx+++ b/src/renderer/components/chat/ChatView.tsx@@ -548,6 +548,11 @@ function ChatView() {minRows={1}maxRows={6}value={chatMsg.current}+ retrieveValue={() => chatMsg.current}+ updateValue={(value) => {+ chatMsg.current = value;+ forceUpdate();+ }}inputRef={textInputRef}sx={{'& .MuiInputBase-root': { padding: '4px' },diff --git a/src/renderer/components/chat/PromptEditDialog.tsx b/src/renderer/components/chat/PromptEditDialog.tsxindex 80f416bcb..1b8eb9b49 100644--- a/src/renderer/components/chat/PromptEditDialog.tsx+++ b/src/renderer/components/chat/PromptEditDialog.tsx@@ -83,6 +83,10 @@ function PromptEditDialog(props: PromptEditDialogProps) {label={t('core:promptTitle')}value={dialogTitle}onChange={(e) => onTitleChange(e.target.value)}+ retrieveValue={() => dialogTitle}+ updateValue={(value) => {+ onTitleChange(value);+ }}fullWidthplaceholder={t('core:promptTitlePlaceholder')}/>@@ -90,6 +94,10 @@ function PromptEditDialog(props: PromptEditDialogProps) {label={t('core:promptContent')}value={dialogContent}onChange={(e) => onContentChange(e.target.value)}+ retrieveValue={() => dialogContent}+ updateValue={(value) => {+ onContentChange(value);+ }}fullWidthmultilineminRows={6}