Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Fix editing losing emote-ness and rainbow-ness of messages #6931

Merged
merged 3 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/editor/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { walkDOMDepthFirst } from "./dom";
import { checkBlockNode } from "../HtmlUtils";
import { getPrimaryPermalinkEntity } from "../utils/permalinks/Permalinks";
import { PartCreator, Type } from "./parts";
import { Part, PartCreator, Type } from "./parts";
import SdkConfig from "../SdkConfig";
import { textToHtmlRainbow } from "../utils/colour";

function parseAtRoomMentions(text: string, partCreator: PartCreator) {
const ATROOM = "@room";
Expand Down Expand Up @@ -213,12 +214,12 @@ function prefixQuoteLines(isFirstNode, parts, partCreator) {
}
}

function parseHtmlMessage(html: string, partCreator: PartCreator, isQuotedMessage: boolean) {
function parseHtmlMessage(html: string, partCreator: PartCreator, isQuotedMessage: boolean): Part[] {
// no nodes from parsing here should be inserted in the document,
// as scripts in event handlers, etc would be executed then.
// we're only taking text, so that is fine
const rootNode = new DOMParser().parseFromString(html, "text/html").body;
const parts = [];
const parts: Part[] = [];
let lastNode;
let inQuote = isQuotedMessage;
const state: IState = {
Expand All @@ -233,7 +234,7 @@ function parseHtmlMessage(html: string, partCreator: PartCreator, isQuotedMessag
inQuote = true;
}

const newParts = [];
const newParts: Part[] = [];
if (lastNode && (checkBlockNode(lastNode) || checkBlockNode(n))) {
newParts.push(partCreator.newline());
}
Expand Down Expand Up @@ -288,7 +289,7 @@ function parseHtmlMessage(html: string, partCreator: PartCreator, isQuotedMessag
return parts;
}

export function parsePlainTextMessage(body: string, partCreator: PartCreator, isQuotedMessage?: boolean) {
export function parsePlainTextMessage(body: string, partCreator: PartCreator, isQuotedMessage?: boolean): Part[] {
const lines = body.split(/\r\n|\r|\n/g); // split on any new-line combination not just \n, collapses \r\n
return lines.reduce((parts, line, i) => {
if (isQuotedMessage) {
Expand All @@ -300,19 +301,31 @@ export function parsePlainTextMessage(body: string, partCreator: PartCreator, is
parts.push(partCreator.newline());
}
return parts;
}, []);
}, [] as Part[]);
}

export function parseEvent(event: MatrixEvent, partCreator: PartCreator, { isQuotedMessage = false } = {}) {
const content = event.getContent();
let parts;
let parts: Part[];
const isEmote = content.msgtype === "m.emote";
let isRainbow = false;

if (content.format === "org.matrix.custom.html") {
parts = parseHtmlMessage(content.formatted_body || "", partCreator, isQuotedMessage);
if (content.body && content.formatted_body && textToHtmlRainbow(content.body) === content.formatted_body) {
isRainbow = true;
}
} else {
parts = parsePlainTextMessage(content.body || "", partCreator, isQuotedMessage);
}
if (content.msgtype === "m.emote") {

if (isEmote && isRainbow) {
parts.unshift(partCreator.plain("/rainbowme "));
} else if (isRainbow) {
parts.unshift(partCreator.plain("/rainbow "));
} else if (isEmote) {
parts.unshift(partCreator.plain("/me "));
}

return parts;
}
3 changes: 2 additions & 1 deletion src/editor/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ export default class EditorModel {
}

public clone(): EditorModel {
return new EditorModel(this._parts, this._partCreator, this.updateCallback);
const clonedParts = this.parts.map(p => this.partCreator.deserializePart(p.serialize()));
return new EditorModel(clonedParts, this._partCreator, this.updateCallback);
}

private insertPart(index: number, part: Part): void {
Expand Down