From 3a1c87fcb0dee762521a1cd602aec09ba4d55741 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 3 May 2024 14:38:01 +0200 Subject: [PATCH] Message Context Menu: Fix showing Copy Selected Text on wrong messages (#4512) --- src/components/middle/message/helpers/copyOptions.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/components/middle/message/helpers/copyOptions.ts b/src/components/middle/message/helpers/copyOptions.ts index 4d4a80adc..a3be2ec60 100644 --- a/src/components/middle/message/helpers/copyOptions.ts +++ b/src/components/middle/message/helpers/copyOptions.ts @@ -4,6 +4,7 @@ import { ApiMediaFormat } from '../../../../api/types'; import { getMessageContact, + getMessageHtmlId, getMessageMediaHash, getMessagePhoto, getMessageText, @@ -76,6 +77,7 @@ export function getMessageCopyOptions( selection?.anchorNode?.parentNode && (selection.anchorNode.parentNode as HTMLElement).closest('.Message .content-inner') && selection.toString().replace(/(?:\r\n|\r|\n)/g, '') !== '' + && checkMessageHasSelection(message) )); options.push({ @@ -121,7 +123,12 @@ export function getMessageCopyOptions( return options; } - +function checkMessageHasSelection(message: ApiMessage): boolean { + const selection = window.getSelection(); + const selectionParentNode = (selection?.anchorNode?.parentNode as HTMLElement); + const selectedMessageElement = selectionParentNode?.closest('.Message.message-list-item'); + return getMessageHtmlId(message.id) === selectedMessageElement?.id; +} function getCopyLabel(hasSelection: boolean): string { if (hasSelection) { return 'lng_context_copy_selected';