You've already forked matrix-react-sdk
mirror of
https://github.com/matrix-org/matrix-react-sdk.git
synced 2025-11-07 10:46:24 +03:00
Only send HTML when using RTE when necessary
When there are no styled blocks or inline styles applied within blocks, just send text instead of HTML. Also, don't add <br /> for the last <p> (the last block). Fixes https://github.com/vector-im/riot-web/issues/3147
This commit is contained in:
@@ -512,9 +512,30 @@ export default class MessageComposerInput extends React.Component {
|
||||
}
|
||||
|
||||
if (this.state.isRichtextEnabled) {
|
||||
contentHTML = HtmlUtils.processHtmlForSending(
|
||||
RichText.contentStateToHTML(contentState),
|
||||
);
|
||||
// We should only send HTML if any block is styled or contains inline style
|
||||
let shouldSendHTML = false;
|
||||
const blocks = contentState.getBlocksAsArray();
|
||||
if (blocks.some((block) => block.getType() !== 'unstyled')) {
|
||||
shouldSendHTML = true;
|
||||
} else {
|
||||
const characterLists = blocks.map((block) => block.getCharacterList());
|
||||
// For each block of characters, determine if any inline styles are applied
|
||||
// and if yes, send HTML
|
||||
characterLists.forEach((characters) => {
|
||||
const numberOfStylesForCharacters = characters.map(
|
||||
(character) => character.getStyle().toArray().length,
|
||||
).toArray();
|
||||
// If any character has more than 0 inline styles applied, send HTML
|
||||
if (numberOfStylesForCharacters.some((styles) => styles > 0)) {
|
||||
shouldSendHTML = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
if (shouldSendHTML) {
|
||||
contentHTML = HtmlUtils.processHtmlForSending(
|
||||
RichText.contentStateToHTML(contentState),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
const md = new Markdown(contentText);
|
||||
if (md.isPlainText()) {
|
||||
@@ -536,8 +557,8 @@ export default class MessageComposerInput extends React.Component {
|
||||
}
|
||||
|
||||
this.historyManager.addItem(
|
||||
this.state.isRichtextEnabled ? contentHTML : contentState.getPlainText(),
|
||||
this.state.isRichtextEnabled ? 'html' : 'markdown');
|
||||
contentHTML ? contentHTML : contentText,
|
||||
contentHTML ? 'html' : 'markdown');
|
||||
|
||||
let sendMessagePromise;
|
||||
if (contentHTML) {
|
||||
|
||||
Reference in New Issue
Block a user