Composer: Fix extra div> text in message input (#2429)

This commit is contained in:
Alexander Zinchuk 2023-01-30 15:55:48 +01:00
parent d1824caa8a
commit 4c6a9216f6

View File

@ -122,11 +122,12 @@ export default function useMentionTooltip(
>${getUserFirstOrLastName(user)}</a>`;
const containerEl = document.querySelector<HTMLDivElement>(inputSelector)!;
const fixedHtmlBeforeSelection = cleanWebkitNewLines(htmlBeforeSelection);
const atIndex = htmlBeforeSelection.lastIndexOf('@');
const atIndex = fixedHtmlBeforeSelection.lastIndexOf('@');
if (atIndex !== -1) {
const newHtml = `${htmlBeforeSelection.substr(0, atIndex)}${insertedHtml}&nbsp;`;
const htmlAfterSelection = containerEl.innerHTML.substring(htmlBeforeSelection.length);
const newHtml = `${fixedHtmlBeforeSelection.substr(0, atIndex)}${insertedHtml}&nbsp;`;
const htmlAfterSelection = cleanWebkitNewLines(containerEl.innerHTML).substring(fixedHtmlBeforeSelection.length);
onUpdateHtml(`${newHtml}${htmlAfterSelection}`);
requestAnimationFrame(() => {
@ -151,6 +152,12 @@ function getUsernameFilter(html: string) {
return username ? username[0].trim() : undefined;
}
// Webkit replaces the line break with the `<div><br /></div>` or `<div></div>` code.
// It is necessary to clean the html to a single form before processing.
function cleanWebkitNewLines(html: string) {
return html.replace(/<div>(<br>|<br\s?\/>)?<\/div>/gi, '<br>');
}
function canSuggestInlineBots(html: string) {
return html.startsWith('@');
}