mirror of
https://github.com/element-hq/element-web.git
synced 2024-12-19 10:54:09 +03:00
Remove remaining reply fallbacks code (#28610)
* Remove remaining reply fallbacks code as MSC2781 has been merged Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> * Update tests Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
parent
00aadf1580
commit
84709df3c9
7 changed files with 18 additions and 74 deletions
|
@ -43,25 +43,6 @@ import { attachMentions, attachRelation } from "./SendMessageComposer";
|
||||||
import { filterBoolean } from "../../../utils/arrays";
|
import { filterBoolean } from "../../../utils/arrays";
|
||||||
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
||||||
|
|
||||||
function getHtmlReplyFallback(mxEvent: MatrixEvent): string {
|
|
||||||
const html = mxEvent.getContent().formatted_body;
|
|
||||||
if (!html) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
const rootNode = new DOMParser().parseFromString(html, "text/html").body;
|
|
||||||
const mxReply = rootNode.querySelector("mx-reply");
|
|
||||||
return (mxReply && mxReply.outerHTML) || "";
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTextReplyFallback(mxEvent: MatrixEvent): string {
|
|
||||||
const body: string = mxEvent.getContent().body;
|
|
||||||
const lines = body.split("\n").map((l) => l.trim());
|
|
||||||
if (lines.length > 2 && lines[0].startsWith("> ") && lines[1].length === 0) {
|
|
||||||
return `${lines[0]}\n\n`;
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
// exported for tests
|
// exported for tests
|
||||||
export function createEditContent(
|
export function createEditContent(
|
||||||
model: EditorModel,
|
model: EditorModel,
|
||||||
|
@ -72,15 +53,6 @@ export function createEditContent(
|
||||||
if (isEmote) {
|
if (isEmote) {
|
||||||
model = stripEmoteCommand(model);
|
model = stripEmoteCommand(model);
|
||||||
}
|
}
|
||||||
const isReply = !!editedEvent.replyEventId;
|
|
||||||
let plainPrefix = "";
|
|
||||||
let htmlPrefix = "";
|
|
||||||
|
|
||||||
if (isReply) {
|
|
||||||
plainPrefix = getTextReplyFallback(editedEvent);
|
|
||||||
htmlPrefix = getHtmlReplyFallback(editedEvent);
|
|
||||||
}
|
|
||||||
|
|
||||||
const body = textSerialize(model);
|
const body = textSerialize(model);
|
||||||
|
|
||||||
const newContent: RoomMessageEventContent = {
|
const newContent: RoomMessageEventContent = {
|
||||||
|
@ -89,19 +61,18 @@ export function createEditContent(
|
||||||
};
|
};
|
||||||
const contentBody: RoomMessageTextEventContent & Omit<ReplacementEvent<RoomMessageEventContent>, "m.relates_to"> = {
|
const contentBody: RoomMessageTextEventContent & Omit<ReplacementEvent<RoomMessageEventContent>, "m.relates_to"> = {
|
||||||
"msgtype": newContent.msgtype,
|
"msgtype": newContent.msgtype,
|
||||||
"body": `${plainPrefix} * ${body}`,
|
"body": `* ${body}`,
|
||||||
"m.new_content": newContent,
|
"m.new_content": newContent,
|
||||||
};
|
};
|
||||||
|
|
||||||
const formattedBody = htmlSerializeIfNeeded(model, {
|
const formattedBody = htmlSerializeIfNeeded(model, {
|
||||||
forceHTML: isReply,
|
|
||||||
useMarkdown: SettingsStore.getValue("MessageComposerInput.useMarkdown"),
|
useMarkdown: SettingsStore.getValue("MessageComposerInput.useMarkdown"),
|
||||||
});
|
});
|
||||||
if (formattedBody) {
|
if (formattedBody) {
|
||||||
newContent.format = "org.matrix.custom.html";
|
newContent.format = "org.matrix.custom.html";
|
||||||
newContent.formatted_body = formattedBody;
|
newContent.formatted_body = formattedBody;
|
||||||
contentBody.format = newContent.format;
|
contentBody.format = newContent.format;
|
||||||
contentBody.formatted_body = `${htmlPrefix} * ${formattedBody}`;
|
contentBody.formatted_body = `* ${formattedBody}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build the mentions properties for both the content and new_content.
|
// Build the mentions properties for both the content and new_content.
|
||||||
|
|
|
@ -193,7 +193,6 @@ export function createMessageContent(
|
||||||
body: body,
|
body: body,
|
||||||
};
|
};
|
||||||
const formattedBody = htmlSerializeIfNeeded(model, {
|
const formattedBody = htmlSerializeIfNeeded(model, {
|
||||||
forceHTML: !!replyToEvent,
|
|
||||||
useMarkdown: SettingsStore.getValue("MessageComposerInput.useMarkdown"),
|
useMarkdown: SettingsStore.getValue("MessageComposerInput.useMarkdown"),
|
||||||
});
|
});
|
||||||
if (formattedBody) {
|
if (formattedBody) {
|
||||||
|
|
|
@ -27,28 +27,6 @@ function attachRelation(content: IContent, relation?: IEventRelation): void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHtmlReplyFallback(mxEvent: MatrixEvent): string {
|
|
||||||
const html = mxEvent.getContent().formatted_body;
|
|
||||||
if (!html) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
const rootNode = new DOMParser().parseFromString(html, "text/html").body;
|
|
||||||
const mxReply = rootNode.querySelector("mx-reply");
|
|
||||||
return (mxReply && mxReply.outerHTML) || "";
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTextReplyFallback(mxEvent: MatrixEvent): string {
|
|
||||||
const body = mxEvent.getContent().body;
|
|
||||||
if (typeof body !== "string") {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
const lines = body.split("\n").map((l) => l.trim());
|
|
||||||
if (lines.length > 2 && lines[0].startsWith("> ") && lines[1].length === 0) {
|
|
||||||
return `${lines[0]}\n\n`;
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
interface CreateMessageContentParams {
|
interface CreateMessageContentParams {
|
||||||
relation?: IEventRelation;
|
relation?: IEventRelation;
|
||||||
replyToEvent?: MatrixEvent;
|
replyToEvent?: MatrixEvent;
|
||||||
|
@ -63,8 +41,6 @@ export async function createMessageContent(
|
||||||
{ relation, replyToEvent, editedEvent }: CreateMessageContentParams,
|
{ relation, replyToEvent, editedEvent }: CreateMessageContentParams,
|
||||||
): Promise<RoomMessageEventContent> {
|
): Promise<RoomMessageEventContent> {
|
||||||
const isEditing = isMatrixEvent(editedEvent);
|
const isEditing = isMatrixEvent(editedEvent);
|
||||||
const isReply = isEditing ? Boolean(editedEvent.replyEventId) : isMatrixEvent(replyToEvent);
|
|
||||||
const isReplyAndEditing = isEditing && isReply;
|
|
||||||
|
|
||||||
const isEmote = message.startsWith(EMOTE_PREFIX);
|
const isEmote = message.startsWith(EMOTE_PREFIX);
|
||||||
if (isEmote) {
|
if (isEmote) {
|
||||||
|
@ -82,12 +58,10 @@ export async function createMessageContent(
|
||||||
// if we're editing rich text, the message content is pure html
|
// if we're editing rich text, the message content is pure html
|
||||||
// BUT if we're not, the message content will be plain text where we need to convert the mentions
|
// BUT if we're not, the message content will be plain text where we need to convert the mentions
|
||||||
const body = isHTML ? await richToPlain(message, false) : convertPlainTextToBody(message);
|
const body = isHTML ? await richToPlain(message, false) : convertPlainTextToBody(message);
|
||||||
const bodyPrefix = (isReplyAndEditing && getTextReplyFallback(editedEvent)) || "";
|
|
||||||
const formattedBodyPrefix = (isReplyAndEditing && getHtmlReplyFallback(editedEvent)) || "";
|
|
||||||
|
|
||||||
const content = {
|
const content = {
|
||||||
msgtype: isEmote ? MsgType.Emote : MsgType.Text,
|
msgtype: isEmote ? MsgType.Emote : MsgType.Text,
|
||||||
body: isEditing ? `${bodyPrefix} * ${body}` : body,
|
body: isEditing ? `* ${body}` : body,
|
||||||
} as RoomMessageTextEventContent & ReplacementEvent<RoomMessageTextEventContent>;
|
} as RoomMessageTextEventContent & ReplacementEvent<RoomMessageTextEventContent>;
|
||||||
|
|
||||||
// TODO markdown support
|
// TODO markdown support
|
||||||
|
@ -97,7 +71,7 @@ export async function createMessageContent(
|
||||||
|
|
||||||
if (formattedBody) {
|
if (formattedBody) {
|
||||||
content.format = "org.matrix.custom.html";
|
content.format = "org.matrix.custom.html";
|
||||||
content.formatted_body = isEditing ? `${formattedBodyPrefix} * ${formattedBody}` : formattedBody;
|
content.formatted_body = isEditing ? `* ${formattedBody}` : formattedBody;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isEditing) {
|
if (isEditing) {
|
||||||
|
|
Loading…
Reference in a new issue