Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix-edit #1477

Merged
merged 3 commits into from
Oct 23, 2023
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
3 changes: 3 additions & 0 deletions src/app/components/editor/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const parseBlockquoteNode = (node: Element): BlockQuoteElement => {
}
if (isTag(child)) {
if (child.name === 'br') {
lineHolder.push({ text: '' });
appendLine();
return;
}
Expand Down Expand Up @@ -202,6 +203,7 @@ const parseListNode = (node: Element): OrderedListElement | UnorderedListElement
}
if (isTag(child)) {
if (child.name === 'br') {
lineHolder.push({ text: '' });
appendLine();
return;
}
Expand Down Expand Up @@ -260,6 +262,7 @@ export const domToEditorInput = (domNodes: ChildNode[]): Descendant[] => {
}
if (isTag(node)) {
if (node.name === 'br') {
lineHolder.push({ text: '' });
appendLine();
return;
}
Expand Down
10 changes: 6 additions & 4 deletions src/app/organisms/room/RoomTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,8 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
if (document.hasFocus()) {
scrollToBottomRef.current.count += 1;
scrollToBottomRef.current.smooth = true;
} else if (!unreadInfo) {
setUnreadInfo(getRoomUnreadInfo(room));
}
setTimeline((ct) => ({
...ct,
Expand Down Expand Up @@ -919,7 +921,7 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
if (!replyEvt) return;
const editedReply = getEditedEvent(replyId, replyEvt, room.getUnfilteredTimelineSet());
const { body, formatted_body: formattedBody }: Record<string, string> =
editedReply?.getContent()['m.new.content'] ?? replyEvt.getContent();
editedReply?.getContent()['m.new_content'] ?? replyEvt.getContent();
const senderId = replyEvt.getSender();
if (senderId && typeof body === 'string') {
setReplyDraft({
Expand Down Expand Up @@ -982,7 +984,7 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
renderText: (mEventId, mEvent, timelineSet) => {
const editedEvent = getEditedEvent(mEventId, mEvent, timelineSet);
const { body, formatted_body: customBody }: Record<string, unknown> =
editedEvent?.getContent()['m.new.content'] ?? mEvent.getContent();
editedEvent?.getContent()['m.new_content'] ?? mEvent.getContent();

if (typeof body !== 'string') return null;
return (
Expand All @@ -1002,7 +1004,7 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
renderEmote: (mEventId, mEvent, timelineSet) => {
const editedEvent = getEditedEvent(mEventId, mEvent, timelineSet);
const { body, formatted_body: customBody } =
editedEvent?.getContent()['m.new.content'] ?? mEvent.getContent();
editedEvent?.getContent()['m.new_content'] ?? mEvent.getContent();
const senderId = mEvent.getSender() ?? '';

const senderDisplayName =
Expand All @@ -1027,7 +1029,7 @@ export function RoomTimeline({ room, eventId, roomInputRef, editor }: RoomTimeli
renderNotice: (mEventId, mEvent, timelineSet) => {
const editedEvent = getEditedEvent(mEventId, mEvent, timelineSet);
const { body, formatted_body: customBody }: Record<string, unknown> =
editedEvent?.getContent()['m.new.content'] ?? mEvent.getContent();
editedEvent?.getContent()['m.new_content'] ?? mEvent.getContent();

if (typeof body !== 'string') return null;
return (
Expand Down
31 changes: 20 additions & 11 deletions src/app/organisms/room/message/MessageEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,22 @@ export const MessageEditor = as<'div', MessageEditorProps>(
const [autocompleteQuery, setAutocompleteQuery] =
useState<AutocompleteQuery<AutocompletePrefix>>();

const getPrevBodyAndFormattedBody = useCallback(() => {
const getPrevBodyAndFormattedBody = useCallback((): [
string | undefined,
string | undefined
] => {
const evtId = mEvent.getId()!;
const evtTimeline = room.getTimelineForEvent(evtId);
const editedEvent =
evtTimeline && getEditedEvent(evtId, mEvent, evtTimeline.getTimelineSet());

const { body, formatted_body: customHtml }: Record<string, unknown> =
editedEvent?.getContent()['m.new.content'] ?? mEvent.getContent();
editedEvent?.getContent()['m.new_content'] ?? mEvent.getContent();

return [body, customHtml];
return [
typeof body === 'string' ? body : undefined,
typeof customHtml === 'string' ? customHtml : undefined,
];
}, [room, mEvent]);

const [saveState, save] = useAsyncCallback(
Expand All @@ -78,14 +84,17 @@ export const MessageEditor = as<'div', MessageEditorProps>(
const [prevBody, prevCustomHtml] = getPrevBodyAndFormattedBody();

if (plainText === '') return undefined;
if (
typeof prevCustomHtml === 'string' &&
trimReplyFromFormattedBody(prevCustomHtml) === customHtml
) {
return undefined;
}
if (!prevCustomHtml && typeof prevBody === 'string' && prevBody === plainText) {
return undefined;
if (prevBody) {
if (prevCustomHtml && trimReplyFromFormattedBody(prevCustomHtml) === customHtml) {
return undefined;
}
if (
!prevCustomHtml &&
prevBody === plainText &&
customHtmlEqualsPlainText(customHtml, plainText)
) {
return undefined;
}
}

const newContent: IContent = {
Expand Down