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

Commit 9096adc

Browse files
floriandurosAmy Walker
authored and
Amy Walker
committed
Fix edit a reply in rich text editor (#9615)
Fix edit a reply in rich text editor
1 parent df84c02 commit 9096adc

File tree

4 files changed

+68
-15
lines changed

4 files changed

+68
-15
lines changed

src/components/views/rooms/wysiwyg_composer/EditWysiwygComposer.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ interface EditWysiwygComposerProps {
4444

4545
export function EditWysiwygComposer({ editorStateTransfer, className, ...props }: EditWysiwygComposerProps) {
4646
const initialContent = useInitialContent(editorStateTransfer);
47-
const isReady = !editorStateTransfer || Boolean(initialContent);
47+
const isReady = !editorStateTransfer || initialContent !== undefined;
4848

49-
const { editMessage, endEditing, onChange, isSaveDisabled } = useEditing(initialContent, editorStateTransfer);
49+
const { editMessage, endEditing, onChange, isSaveDisabled } = useEditing(editorStateTransfer, initialContent);
5050

5151
return isReady && <WysiwygComposer
5252
className={classNames("mx_EditWysiwygComposer", className)}

src/components/views/rooms/wysiwyg_composer/hooks/useEditing.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import EditorStateTransfer from "../../../../../utils/EditorStateTransfer";
2222
import { endEditing } from "../utils/editing";
2323
import { editMessage } from "../utils/message";
2424

25-
export function useEditing(initialContent: string, editorStateTransfer: EditorStateTransfer) {
25+
export function useEditing(editorStateTransfer: EditorStateTransfer, initialContent?: string) {
2626
const roomContext = useRoomContext();
2727
const mxClient = useMatrixClientContext();
2828

@@ -34,7 +34,7 @@ export function useEditing(initialContent: string, editorStateTransfer: EditorSt
3434
}, [initialContent]);
3535

3636
const editMessageMemoized = useCallback(() =>
37-
editMessage(content, { roomContext, mxClient, editorStateTransfer }),
37+
content !== undefined && editMessage(content, { roomContext, mxClient, editorStateTransfer }),
3838
[content, roomContext, mxClient, editorStateTransfer],
3939
);
4040

src/components/views/rooms/wysiwyg_composer/hooks/useInitialContent.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import { CommandPartCreator, Part } from "../../../../../editor/parts";
2424
import SettingsStore from "../../../../../settings/SettingsStore";
2525
import EditorStateTransfer from "../../../../../utils/EditorStateTransfer";
2626

27+
function getFormattedContent(editorStateTransfer: EditorStateTransfer): string {
28+
return editorStateTransfer.getEvent().getContent().formatted_body?.replace(/<mx-reply>.*<\/mx-reply>/, '') || '';
29+
}
30+
2731
function parseEditorStateTransfer(
2832
editorStateTransfer: EditorStateTransfer,
2933
room: Room,
@@ -42,7 +46,7 @@ function parseEditorStateTransfer(
4246
// const restoredParts = this.restoreStoredEditorState(partCreator);
4347

4448
if (editorStateTransfer.getEvent().getContent().format === 'org.matrix.custom.html') {
45-
return editorStateTransfer.getEvent().getContent().formatted_body || "";
49+
return getFormattedContent(editorStateTransfer);
4650
}
4751

4852
parts = parseEvent(editorStateTransfer.getEvent(), partCreator, {
@@ -59,7 +63,7 @@ export function useInitialContent(editorStateTransfer: EditorStateTransfer) {
5963
const roomContext = useRoomContext();
6064
const mxClient = useMatrixClientContext();
6165

62-
return useMemo<string>(() => {
66+
return useMemo<string | undefined>(() => {
6367
if (editorStateTransfer && roomContext.room) {
6468
return parseEditorStateTransfer(editorStateTransfer, roomContext.room, mxClient);
6569
}

test/components/views/rooms/wysiwyg_composer/EditWysiwygComposer-test.tsx

+58-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
import "@testing-library/jest-dom";
1818
import React from "react";
19-
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
19+
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
2020

2121
import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext";
2222
import RoomContext from "../../../../../src/contexts/RoomContext";
@@ -96,6 +96,53 @@ describe('EditWysiwygComposer', () => {
9696
await waitFor(() =>
9797
expect(screen.getByRole('textbox')).toContainHTML(mockEvent.getContent()['body']));
9898
});
99+
100+
it('Should ignore when formatted_body is not filled', async () => {
101+
// When
102+
const mockEvent = mkEvent({
103+
type: "m.room.message",
104+
room: 'myfakeroom',
105+
user: 'myfakeuser',
106+
content: {
107+
"msgtype": "m.text",
108+
"body": "Replying to this",
109+
"format": "org.matrix.custom.html",
110+
},
111+
event: true,
112+
});
113+
114+
const editorStateTransfer = new EditorStateTransfer(mockEvent);
115+
customRender(false, editorStateTransfer);
116+
117+
// Then
118+
await waitFor(() => expect(screen.getByRole('textbox')).toHaveAttribute('contentEditable', "true"));
119+
});
120+
121+
it('Should strip <mx-reply> tag from initial content', async () => {
122+
// When
123+
const mockEvent = mkEvent({
124+
type: "m.room.message",
125+
room: 'myfakeroom',
126+
user: 'myfakeuser',
127+
content: {
128+
"msgtype": "m.text",
129+
"body": "Replying to this",
130+
"format": "org.matrix.custom.html",
131+
"formatted_body": '<mx-reply>Reply</mx-reply>My content',
132+
},
133+
event: true,
134+
});
135+
136+
const editorStateTransfer = new EditorStateTransfer(mockEvent);
137+
customRender(false, editorStateTransfer);
138+
await waitFor(() => expect(screen.getByRole('textbox')).toHaveAttribute('contentEditable', "true"));
139+
140+
// Then
141+
await waitFor(() => {
142+
expect(screen.getByRole('textbox')).not.toContainHTML("<mx-reply>Reply</mx-reply>");
143+
expect(screen.getByRole('textbox')).toContainHTML("My content");
144+
});
145+
});
99146
});
100147

101148
describe('Edit and save actions', () => {
@@ -180,14 +227,16 @@ describe('EditWysiwygComposer', () => {
180227
expect(screen.getByRole('textbox')).not.toHaveFocus();
181228

182229
// When we send an action that would cause us to get focus
183-
defaultDispatcher.dispatch({
184-
action: Action.FocusEditMessageComposer,
185-
context: null,
186-
});
187-
// (Send a second event to exercise the clearTimeout logic)
188-
defaultDispatcher.dispatch({
189-
action: Action.FocusEditMessageComposer,
190-
context: null,
230+
act(() => {
231+
defaultDispatcher.dispatch({
232+
action: Action.FocusEditMessageComposer,
233+
context: null,
234+
});
235+
// (Send a second event to exercise the clearTimeout logic)
236+
defaultDispatcher.dispatch({
237+
action: Action.FocusEditMessageComposer,
238+
context: null,
239+
});
191240
});
192241

193242
// Wait for event dispatch to happen

0 commit comments

Comments
 (0)