Browse Source

Some formatting fixes

master
Eduard Kuzmenko 3 years ago
parent
commit
d9a8da9382
  1. 62
      src/lib/richtextprocessor.ts

62
src/lib/richtextprocessor.ts

@ -492,6 +492,7 @@ namespace RichTextProcessor {
nasty?: { nasty?: {
i: number, i: number,
usedLength: number, usedLength: number,
text: string,
lastEntity?: MessageEntity lastEntity?: MessageEntity
}, },
voodoo?: boolean voodoo?: boolean
@ -501,17 +502,19 @@ namespace RichTextProcessor {
return fragment; return fragment;
} }
const entities = options.entities ??= parseEntities(text); const nasty = options.nasty ??= {
i: 0,
usedLength: 0,
text
};
const entities = options.entities ??= parseEntities(nasty.text);
const passEntities = options.passEntities ??= {}; const passEntities = options.passEntities ??= {};
const contextSite = options.contextSite ??= 'Telegram'; const contextSite = options.contextSite ??= 'Telegram';
const contextExternal = contextSite !== 'Telegram'; const contextExternal = contextSite !== 'Telegram';
const nasty = options.nasty ??= {
i: 0,
usedLength: 0
};
const textLength = text.length; const textLength = nasty.text.length;
const length = entities.length; const length = entities.length;
let lastElement: HTMLElement | DocumentFragment; let lastElement: HTMLElement | DocumentFragment;
for(; nasty.i < length; ++nasty.i) { for(; nasty.i < length; ++nasty.i) {
@ -537,12 +540,12 @@ namespace RichTextProcessor {
const startOffset = entity.offset; const startOffset = entity.offset;
const endOffset = startOffset + entity.length; const endOffset = startOffset + entity.length;
const endPartOffset = Math.min(endOffset, nextEntity?.offset ?? 0xFFFF); const endPartOffset = Math.min(endOffset, nextEntity?.offset ?? 0xFFFF);
const fullEntityText = text.slice(startOffset, endOffset); const fullEntityText = nasty.text.slice(startOffset, endOffset);
const sliced = text.slice(startOffset, endPartOffset); const sliced = nasty.text.slice(startOffset, endPartOffset);
const partText = sliced; let partText = sliced;
if(nasty.usedLength < startOffset) { if(nasty.usedLength < startOffset) {
(lastElement || fragment).append(text.slice(nasty.usedLength, startOffset)); (lastElement || fragment).append(nasty.text.slice(nasty.usedLength, startOffset));
} }
if(lastElement) { if(lastElement) {
@ -704,12 +707,10 @@ namespace RichTextProcessor {
case 'messageEntityCaret': { case 'messageEntityCaret': {
element = document.createElement('span'); element = document.createElement('span');
element.className = 'composer-sel'; element.className = 'composer-sel';
// const html = '<span class="composer-sel"></span>';
// pushPartsAfterSort.push({part: html, offset: entity.offset});
break; break;
} }
// /* case 'messageEntityLinebreak': { // case 'messageEntityLinebreak': {
// if(options.noLinebreaks) { // if(options.noLinebreaks) {
// insertPart(entity, ' '); // insertPart(entity, ' ');
// } else { // } else {
@ -717,7 +718,7 @@ namespace RichTextProcessor {
// } // }
// break; // break;
// } */ // }
case 'messageEntityUrl': case 'messageEntityUrl':
case 'messageEntityTextUrl': { case 'messageEntityTextUrl': {
@ -834,10 +835,11 @@ namespace RichTextProcessor {
case 'messageEntitySpoiler': { case 'messageEntitySpoiler': {
if(options.noTextFormat) { if(options.noTextFormat) {
const before = text.slice(0, entity.offset); const before = nasty.text.slice(0, entity.offset);
const spoilerBefore = text.slice(entity.offset, entity.offset + entity.length); const spoilerBefore = nasty.text.slice(entity.offset, entity.offset + entity.length);
const after = text.slice(entity.offset + entity.length); const spoilerAfter = partText = spoiler(spoilerBefore)/* '▚'.repeat(entity.length) */;
text = before + spoiler(spoilerBefore)/* '▚'.repeat(entity.length) */ + after; const after = nasty.text.slice(entity.offset + entity.length);
nasty.text = before + spoilerAfter + after;
} else if(options.wrappingDraft) { } else if(options.wrappingDraft) {
element = document.createElement('span'); element = document.createElement('span');
element.style.fontFamily = 'spoiler'; element.style.fontFamily = 'spoiler';
@ -856,15 +858,23 @@ namespace RichTextProcessor {
} }
} }
if(element && !usedText) { if(!usedText) {
// @ts-ignore if(element) {
element[property] = partText; // @ts-ignore
element[property] = partText;
} else {
(element || fragment).append(partText);
}
}
if(element && !element.parentElement) {
(lastElement || fragment).append(element);
} }
while(nextEntity && nextEntity.offset < (endOffset - 1)) { while(nextEntity && nextEntity.offset < (endOffset - 1)) {
++nasty.i; ++nasty.i;
(element || fragment).append(wrapRichText(text, { (element || fragment).append(wrapRichText(nasty.text, {
...options, ...options,
voodoo: true voodoo: true
})); }));
@ -872,9 +882,9 @@ namespace RichTextProcessor {
nextEntity = entities[nasty.i + 1]; nextEntity = entities[nasty.i + 1];
} }
if(!element?.parentElement) { // if(!element?.parentElement) {
(lastElement || fragment).append(element ?? partText); // (lastElement || fragment).append(element ?? partText);
} // }
if(entity.length > partText.length && element) { if(entity.length > partText.length && element) {
lastElement = element; lastElement = element;
@ -892,7 +902,7 @@ namespace RichTextProcessor {
} }
if(nasty.usedLength < textLength) { if(nasty.usedLength < textLength) {
(lastElement || fragment).append(text.slice(nasty.usedLength)); (lastElement || fragment).append(nasty.text.slice(nasty.usedLength));
} }
return fragment; return fragment;

Loading…
Cancel
Save