Browse Source

Split text message by 4096 symbols

Fix paste to input linebreaks
master
morethanwords 4 years ago
parent
commit
b260c4a5a7
  1. 7
      src/components/chat/input.ts
  2. 19
      src/lib/appManagers/appMessagesManager.ts
  3. 51
      src/lib/utils.ts

7
src/components/chat/input.ts

@ -257,8 +257,11 @@ export class ChatInput { @@ -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);

19
src/lib/appManagers/appMessagesManager.ts

@ -13,7 +13,7 @@ import { RichTextProcessor } from "../richtextprocessor"; @@ -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 { @@ -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) {

51
src/lib/utils.ts

@ -530,3 +530,54 @@ export function positionElementByIndex(element: HTMLElement, container: HTMLElem @@ -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;

Loading…
Cancel
Save