diff --git a/.changeset/extract-buildReplacementContent.md b/.changeset/extract-buildReplacementContent.md new file mode 100644 index 000000000..79ee19aae --- /dev/null +++ b/.changeset/extract-buildReplacementContent.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Extract buildReplacementContent so message edits assemble the replacement the same way in the composer and the editor. diff --git a/src/app/features/room/RoomInput.tsx b/src/app/features/room/RoomInput.tsx index 932c6cbb8..cbcae8035 100644 --- a/src/app/features/room/RoomInput.tsx +++ b/src/app/features/room/RoomInput.tsx @@ -113,6 +113,7 @@ import type { EditorButtonId } from '$state/settings'; import { settingsAtom } from '$state/settings'; import { matchesShortcut } from '../../keyboard/shortcuts'; import { getEditedEvent, getMentionContent, getThreadReplyEvents } from '$utils/room'; +import { buildReplacementContent } from './buildReplacementContent'; import { htmlToMarkdown } from '$plugins/markdown'; import { Command, SHRUG, TABLEFLIP, UNFLIP, useCommands } from '$hooks/useCommands'; import { mobileOrTablet } from '$utils/user-agent'; @@ -1051,33 +1052,9 @@ export const RoomInput = forwardRef( const eventId = editingEvent.getId(); if (!eventId) return; - const msgtype = oldContent.msgtype ?? MsgType.Text; - const newContent: IContent = { - msgtype, - body: plainText, - }; - const rawPmp = currentContent['com.beeper.per_message_profile'] ?? oldContent['com.beeper.per_message_profile']; - const pmpDisplayname = - rawPmp !== null && - typeof rawPmp === 'object' && - 'displayname' in rawPmp && - typeof rawPmp.displayname === 'string' && - rawPmp.displayname.length > 0 - ? rawPmp.displayname - : undefined; - - if (pmpDisplayname) { - const bodyPrefix = `${pmpDisplayname}: `; - if (!plainText.startsWith(bodyPrefix)) plainText = bodyPrefix + plainText; - - const htmlPrefix = `${sanitizeText(pmpDisplayname)}: `; - if (!customHtml.startsWith(htmlPrefix)) customHtml = htmlPrefix + customHtml; - newContent.body = plainText; - newContent['com.beeper.per_message_profile'] = rawPmp; - } const mentionData = getMentions(mx, roomId, editor); const previousMentions = currentContent['m.mentions']; @@ -1092,50 +1069,21 @@ export const RoomInput = forwardRef( }); } const mMentions = getMentionContent(Array.from(mentionData.users), mentionData.room); - newContent['m.mentions'] = mMentions; - const content: IContent = { - ...oldContent, - 'm.relates_to': { - event_id: eventId, - rel_type: RelationType.Replace, - }, - body: `* ${plainText}`, - 'm.mentions': mMentions, - 'm.new_content': newContent, - }; + const linkPreviews = + getLinks(editor.children)?.map((matchedUrl) => ({ + matched_url: matchedUrl, + })) ?? []; - if (pmpDisplayname || !customHtmlEqualsPlainText(customHtml, plainText)) { - newContent.format = 'org.matrix.custom.html'; - newContent.formatted_body = customHtml; - content.format = 'org.matrix.custom.html'; - content.formatted_body = `* ${customHtml}`; - } else { - delete content.format; - delete content.formatted_body; - } - - if (oldContent.info !== undefined && oldContent.msgtype !== MsgType.Text) { - const filename = 'filename' in oldContent ? oldContent.filename : oldContent.body; - content.filename = filename; - newContent.filename = filename; - content.info = oldContent.info; - newContent.info = oldContent.info; - if (oldContent.file !== undefined) newContent.file = oldContent.file; - if (oldContent.url !== undefined) newContent.url = oldContent.url; - - const spoilerKey = 'page.codeberg.everypizza.msc4193.spoiler'; - if (oldContent[spoilerKey] !== undefined) { - content[spoilerKey] = oldContent[spoilerKey]; - newContent[spoilerKey] = oldContent[spoilerKey]; - } - } - - const linkPreviews = getLinks(editor.children)?.map((matchedUrl) => ({ - matched_url: matchedUrl, - })); - content['com.beeper.linkpreviews'] = linkPreviews ?? []; - newContent['com.beeper.linkpreviews'] = linkPreviews ?? []; + const content = buildReplacementContent( + oldContent, + plainText, + customHtml, + eventId, + mMentions, + linkPreviews, + rawPmp + ); await mx.sendMessage(roomId, content as RoomMessageEventContent); onCancelEdit?.(); diff --git a/src/app/features/room/buildReplacementContent.ts b/src/app/features/room/buildReplacementContent.ts new file mode 100644 index 000000000..869f5de3c --- /dev/null +++ b/src/app/features/room/buildReplacementContent.ts @@ -0,0 +1,94 @@ +import type { IContent, IMentions } from '$types/matrix-sdk'; +import { MsgType, RelationType } from '$types/matrix-sdk'; +import { customHtmlEqualsPlainText } from '$components/editor'; +import { sanitizeText } from '$utils/sanitize'; + +/** + * Unified from RoomInput and MessageEditor, which had drifted: this keeps + * RoomInput's `delete content.format` branch that MessageEditor lacked. + */ +export function buildReplacementContent( + oldContent: IContent, + plainText: string, + customHtml: string, + eventId: string, + mMentions: IMentions, + linkPreviews: { matched_url: string }[], + perMessageProfile: unknown +): IContent { + const pmpDisplayname = + perMessageProfile !== null && + typeof perMessageProfile === 'object' && + 'displayname' in perMessageProfile && + typeof perMessageProfile.displayname === 'string' && + perMessageProfile.displayname.length > 0 + ? perMessageProfile.displayname + : undefined; + + let adjustedPlainText = plainText; + let adjustedCustomHtml = customHtml; + + if (pmpDisplayname) { + const bodyPrefix = `${pmpDisplayname}: `; + if (!adjustedPlainText.startsWith(bodyPrefix)) + adjustedPlainText = bodyPrefix + adjustedPlainText; + + const htmlPrefix = `${sanitizeText(pmpDisplayname)}: `; + if (!adjustedCustomHtml.startsWith(htmlPrefix)) + adjustedCustomHtml = htmlPrefix + adjustedCustomHtml; + } + + const msgtype = oldContent.msgtype ?? MsgType.Text; + + const newContent: IContent = { + msgtype, + body: adjustedPlainText, + 'm.mentions': mMentions, + }; + + if (pmpDisplayname) { + newContent['com.beeper.per_message_profile'] = perMessageProfile; + } + + const content: IContent = { + ...oldContent, + 'm.relates_to': { + event_id: eventId, + rel_type: RelationType.Replace, + }, + body: `* ${adjustedPlainText}`, + 'm.mentions': mMentions, + 'm.new_content': newContent, + }; + + if (!customHtmlEqualsPlainText(adjustedCustomHtml, adjustedPlainText)) { + newContent.format = 'org.matrix.custom.html'; + newContent.formatted_body = adjustedCustomHtml; + content.format = 'org.matrix.custom.html'; + content.formatted_body = `* ${adjustedCustomHtml}`; + } else { + delete content.format; + delete content.formatted_body; + } + + if (oldContent.info !== undefined && oldContent.msgtype !== MsgType.Text) { + const filename = 'filename' in oldContent ? (oldContent.filename as string) : oldContent.body; + content.filename = filename; + newContent.filename = filename; + content.info = oldContent.info; + newContent.info = oldContent.info; + if (oldContent.file !== undefined) newContent.file = oldContent.file; + if (oldContent.url !== undefined) newContent.url = oldContent.url; + + const spoilerKey = 'page.codeberg.everypizza.msc4193.spoiler'; + if (oldContent[spoilerKey] !== undefined) { + content[spoilerKey] = oldContent[spoilerKey]; + newContent[spoilerKey] = oldContent[spoilerKey]; + } + } + + content['com.beeper.linkpreviews'] = linkPreviews; + newContent['com.beeper.linkpreviews'] = linkPreviews; + + return content; +} diff --git a/src/app/features/room/message/MessageEditor.tsx b/src/app/features/room/message/MessageEditor.tsx index 8f86fb0d3..fc2fd1def 100644 --- a/src/app/features/room/message/MessageEditor.tsx +++ b/src/app/features/room/message/MessageEditor.tsx @@ -21,12 +21,11 @@ import type { IContent, IMentions, MatrixEvent, - ReplacementEvent, Room, RoomMessageEventContent, RoomMessageTextEventContent, } from '$types/matrix-sdk'; -import { RelationType, MsgType } from '$types/matrix-sdk'; +import { MsgType } from '$types/matrix-sdk'; import { isKeyHotkey } from 'is-hotkey'; import type { AutocompleteQuery } from '$components/editor'; import { @@ -63,6 +62,7 @@ import { useMatrixClient } from '$hooks/useMatrixClient'; import { useDismissOnBack } from '$utils/androidBack'; import { nicknamesAtom } from '$state/nicknames'; import { getEditedEvent, getMentionContent, trimReplyFromFormattedBody } from '$utils/room'; +import { buildReplacementContent } from '../buildReplacementContent'; import { mobileOrTablet } from '$utils/user-agent'; import { useComposingCheck } from '$hooks/useComposingCheck'; import { floatingEditor } from '$styles/overrides/Composer.css'; @@ -75,7 +75,6 @@ import type { HTMLReactParserOptions } from 'html-react-parser'; import { useMediaAuthentication } from '$hooks/useMediaAuthentication'; import type { Opts as LinkifyOpts } from 'linkifyjs'; import type { GetContentCallback } from '$types/matrix/room'; -import { sanitizeText } from '$utils/sanitize'; import type { BundleContent } from '$components/message'; import { readdAngleBracketsForHiddenPreviews, @@ -261,11 +260,6 @@ export const MessageEditor = as<'div', MessageEditorProps>( } } - const newContent: IContent = { - msgtype, - body: plainText, - }; - const evtId = mEvent.getId(); const evtTimeline = evtId ? room.getTimelineForEvent(evtId) : undefined; const editedEvent = @@ -277,36 +271,6 @@ export const MessageEditor = as<'div', MessageEditorProps>( editedEvent?.getContent()?.['m.new_content']?.['com.beeper.per_message_profile'] ?? mEvent.getContent()?.['com.beeper.per_message_profile']; - const pmpDisplayname = - rawPmp !== null && - typeof rawPmp === 'object' && - 'displayname' in rawPmp && - typeof rawPmp.displayname === 'string' && - rawPmp.displayname.length > 0 - ? (rawPmp.displayname as string) - : undefined; - - if (pmpDisplayname) { - const bodyPrefix = `${pmpDisplayname}: `; - if (!plainText.startsWith(bodyPrefix)) { - plainText = bodyPrefix + plainText; - } - - const escapedName = sanitizeText(pmpDisplayname); - const htmlPrefix = `${escapedName}: `; - if (!customHtml.startsWith(htmlPrefix)) { - customHtml = htmlPrefix + customHtml; - } - - newContent['com.beeper.per_message_profile'] = rawPmp; - } - - const contentBody: IContent & Omit, 'm.relates_to'> = { - msgtype, - body: `* ${plainText}`, - 'm.new_content': newContent, - }; - const mentionData = getMentions(mx, roomId, editor); prevMentions?.user_ids?.forEach((prevMentionId) => { @@ -314,49 +278,21 @@ export const MessageEditor = as<'div', MessageEditorProps>( }); const mMentions = getMentionContent(Array.from(mentionData.users), mentionData.room); - newContent['m.mentions'] = mMentions; - contentBody['m.mentions'] = mMentions; - const links = getLinks(editor.children); + const linkPreviews = + getLinks(editor.children)?.map((matchedUrl) => ({ + matched_url: matchedUrl, + })) ?? []; - if (pmpDisplayname || !customHtmlEqualsPlainText(customHtml, plainText)) { - newContent.format = 'org.matrix.custom.html'; - newContent.formatted_body = customHtml; - contentBody.format = 'org.matrix.custom.html'; - contentBody.formatted_body = `* ${customHtml}`; - } - - const content: IContent = { - ...oldContent, - 'm.relates_to': { - event_id: eventId, - rel_type: RelationType.Replace, - }, - }; - content.body = contentBody.body; - content.format = contentBody.format; - content.formatted_body = contentBody.formatted_body; - content['m.new_content'] = newContent; - if (oldContent.info !== undefined && oldContent.msgtype !== 'm.text') { - content.filename = 'filename' in oldContent ? oldContent.filename : oldContent.body; - content['m.new_content'].filename = - 'filename' in oldContent ? oldContent.filename : oldContent.body; - content.info = oldContent.info; - content['m.new_content'].info = oldContent.info; - - if (oldContent.file !== undefined) content['m.new_content'].file = oldContent.file; - if (oldContent.url !== undefined) content['m.new_content'].url = oldContent.url; - - if (oldContent['page.codeberg.everypizza.msc4193.spoiler'] !== undefined) { - content['page.codeberg.everypizza.msc4193.spoiler'] = - oldContent['page.codeberg.everypizza.msc4193.spoiler']; - content['m.new_content']['page.codeberg.everypizza.msc4193.spoiler'] = - oldContent['page.codeberg.everypizza.msc4193.spoiler']; - } - } - content['com.beeper.linkpreviews'] = []; - links?.forEach((link) => content['com.beeper.linkpreviews'].push({ matched_url: link })); - content['m.new_content']['com.beeper.linkpreviews'] = content['com.beeper.linkpreviews']; + const content = buildReplacementContent( + oldContent, + plainText, + customHtml, + eventId, + mMentions, + linkPreviews, + rawPmp + ); return mx.sendMessage(roomId, content as RoomMessageEventContent); }, [mx, editor, roomId, mEvent, getPrevBodyAndFormattedBody, room])