From 6c2cd8132abd276b4f3dd92affae5c324acd168f Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Wed, 19 Jan 2022 07:11:39 +0400 Subject: [PATCH] Fix pasting styles and comments Fallback to plain text if length of rich and plain are different --- src/components/inputField.ts | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/components/inputField.ts b/src/components/inputField.ts index 61a92939..6d9eb1e4 100644 --- a/src/components/inputField.ts +++ b/src/components/inputField.ts @@ -23,9 +23,16 @@ let init = () => { e.preventDefault(); let text: string, entities: MessageEntity[]; + // @ts-ignore + let plainText: string = (e.originalEvent || e).clipboardData.getData('text/plain'); + let usePlainText = true; + // @ts-ignore let html: string = (e.originalEvent || e).clipboardData.getData('text/html'); if(html.trim()) { + html = html.replace(//, ''); + html = html.replace(//, ''); + const match = html.match(/([\s\S]*)<\/body>/); if(match) { html = match[1].trim(); @@ -47,16 +54,19 @@ let init = () => { } const richValue = getRichValue(span, true); - text = richValue.value; - entities = richValue.entities; - - let entities2 = RichTextProcessor.parseEntities(text); - entities2 = entities2.filter(e => e._ === 'messageEntityEmoji' || e._ === 'messageEntityLinebreak'); - RichTextProcessor.mergeEntities(entities, entities2); - } else { - // @ts-ignore - text = (e.originalEvent || e).clipboardData.getData('text/plain'); - + if(richValue.value.replace(/\s/g, '').length === plainText.replace(/\s/g, '').length) { + text = richValue.value; + entities = richValue.entities; + usePlainText = false; + + let entities2 = RichTextProcessor.parseEntities(text); + entities2 = entities2.filter(e => e._ === 'messageEntityEmoji' || e._ === 'messageEntityLinebreak'); + RichTextProcessor.mergeEntities(entities, entities2); + } + } + + if(usePlainText) { + text = plainText; entities = RichTextProcessor.parseEntities(text); entities = entities.filter(e => e._ === 'messageEntityEmoji' || e._ === 'messageEntityLinebreak'); }