diff --git a/src/components/chat/input.ts b/src/components/chat/input.ts index 16c1b127..ba0b5c02 100644 --- a/src/components/chat/input.ts +++ b/src/components/chat/input.ts @@ -257,8 +257,11 @@ export class ChatInput { // @ts-ignore let text = (e.originalEvent || e).clipboardData.getData('text/plain'); - // console.log('messageInput paste', text); - text = RichTextProcessor.wrapEmojiText(text); + let entities = RichTextProcessor.parseEntities(text); + //console.log('messageInput paste', text, entities); + entities = entities.filter(e => e._ == 'messageEntityEmoji' || e._ == 'messageEntityLinebreak'); + //text = RichTextProcessor.wrapEmojiText(text); + text = RichTextProcessor.wrapRichText(text, {entities}); // console.log('messageInput paste after', text); diff --git a/src/lib/appManagers/appMessagesManager.ts b/src/lib/appManagers/appMessagesManager.ts index a60859aa..e5d8a107 100644 --- a/src/lib/appManagers/appMessagesManager.ts +++ b/src/lib/appManagers/appMessagesManager.ts @@ -13,7 +13,7 @@ import { RichTextProcessor } from "../richtextprocessor"; import $rootScope from "../rootScope"; import searchIndexManager from '../searchIndexManager'; import AppStorage from '../storage'; -import { copy, deepEqual, getObjectKeysAndSort, langPack, limitSymbols, listMergeSorted, safeReplaceObject, tsNow } from "../utils"; +import { copy, deepEqual, getObjectKeysAndSort, langPack, limitSymbols, listMergeSorted, safeReplaceObject, splitStringByLength, tsNow } from "../utils"; //import { telegramMeWebService } from "../mtproto/mtproto"; import apiUpdatesManager from "./apiUpdatesManager"; import appChatsManager from "./appChatsManager"; @@ -738,19 +738,28 @@ export class AppMessagesManager { clearDraft: true, webPage: any }> = {}) { - if(typeof(text) != 'string') { + if(typeof(text) != 'string' || !text.length) { return; } + const MAX_LENGTH = 4096; + if(text.length > MAX_LENGTH) { + const splitted = splitStringByLength(text, MAX_LENGTH); + text = splitted[0]; + + for(let i = 1; i < splitted.length; ++i) { + //setTimeout(() => { + this.sendText(peerID, splitted[i], options); + //}, i); + } + } + peerID = appPeersManager.getPeerMigratedTo(peerID) || peerID; var entities = options.entities || []; if(!options.viaBotID) { text = RichTextProcessor.parseMarkdown(text, entities); } - if(!text.length) { - return; - } var sendEntites = this.getInputEntities(entities); if(!sendEntites.length) { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index c7826afd..8a86f301 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -530,3 +530,54 @@ export function positionElementByIndex(element: HTMLElement, container: HTMLElem return true; } + +export function splitStringByLength(str: string, maxLength: number) { + if(str.length < maxLength) return [str]; + let length = 0, lastSliceStartIndex = 0, arrayIndex = 0; + const delimiter = ' ';//'\n'; + const out: string[] = []; + + const cut = (end?: number) => { + let part = str.slice(lastSliceStartIndex, end); + const _arrayIndex = arrayIndex++; + if(part.length > maxLength) { + let overflowPart = part.slice(maxLength); + const splitted = splitStringByLength(overflowPart, maxLength); + splitted.forEach(part => { + out[arrayIndex++] = part; + }); + + part = part.slice(0, maxLength); + } + + lastSliceStartIndex = end; + length = 0; + out[_arrayIndex] = (out[_arrayIndex] || '') + part; + }; + + let lastIndex = 0; + do { + let index = str.indexOf(delimiter, lastIndex); + if(index === -1) { + if(lastIndex != (str.length - 1)) { + cut(); + } + + break; + } + + index += delimiter.length; + + const partLength = index - lastIndex; + if((length + partLength) > maxLength) { + cut(length); + } + + lastIndex = index; + length += partLength; + } while(true); + + return out; +} + +(window as any).splitStringByLength = splitStringByLength;