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

Commit 6c4e945

Browse files
committed
Use react lazy to load rte component
1 parent 910aa0b commit 6c4e945

File tree

5 files changed

+70
-24
lines changed

5 files changed

+70
-24
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2022 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React, { ComponentProps, lazy, Suspense } from "react";
18+
19+
const SendComposer = lazy(() => import("./SendWysiwygComposer"));
20+
const EditComposer = lazy(() => import("./EditWysiwygComposer"));
21+
22+
export function DynamicImportSendWysiwygComposer(props: ComponentProps<typeof SendComposer>) {
23+
return (
24+
<Suspense fallback={<div />}>
25+
<SendComposer {...props} />
26+
</Suspense>
27+
);
28+
}
29+
30+
export function DynamicImportEditWysiwygComposer(props: ComponentProps<typeof EditComposer>) {
31+
return (
32+
<Suspense fallback={<div />}>
33+
<EditComposer {...props} />
34+
</Suspense>
35+
);
36+
}

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

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,31 @@ export function EditWysiwygComposer({ editorStateTransfer, className, ...props }
4949

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

52+
if (!isReady) {
53+
return null;
54+
}
55+
5256
return (
53-
isReady && (
54-
<WysiwygComposer
55-
className={classNames("mx_EditWysiwygComposer", className)}
56-
initialContent={initialContent}
57-
onChange={onChange}
58-
onSend={editMessage}
59-
{...props}
60-
>
61-
{(ref) => (
62-
<>
63-
<Content disabled={props.disabled} ref={ref} />
64-
<EditionButtons
65-
onCancelClick={endEditing}
66-
onSaveClick={editMessage}
67-
isSaveDisabled={isSaveDisabled}
68-
/>
69-
</>
70-
)}
71-
</WysiwygComposer>
72-
)
57+
<WysiwygComposer
58+
className={classNames("mx_EditWysiwygComposer", className)}
59+
initialContent={initialContent}
60+
onChange={onChange}
61+
onSend={editMessage}
62+
{...props}
63+
>
64+
{(ref) => (
65+
<>
66+
<Content disabled={props.disabled} ref={ref} />
67+
<EditionButtons
68+
onCancelClick={endEditing}
69+
onSaveClick={editMessage}
70+
isSaveDisabled={isSaveDisabled}
71+
/>
72+
</>
73+
)}
74+
</WysiwygComposer>
7375
);
7476
}
77+
78+
// Needed for React.lazy
79+
export default EditWysiwygComposer;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,6 @@ export function SendWysiwygComposer({
7272
</Composer>
7373
);
7474
}
75+
76+
// Needed for React.lazy
77+
export default SendWysiwygComposer;

src/components/views/rooms/wysiwyg_composer/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
export { SendWysiwygComposer } from "./SendWysiwygComposer";
18-
export { EditWysiwygComposer } from "./EditWysiwygComposer";
17+
export {
18+
DynamicImportSendWysiwygComposer as SendWysiwygComposer,
19+
DynamicImportEditWysiwygComposer as EditWysiwygComposer,
20+
} from "./DynamicImportWysiwygComposer";
1921
export { sendMessage } from "./utils/message";

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import defaultDispatcher from "../../../../../src/dispatcher/dispatcher";
2424
import { Action } from "../../../../../src/dispatcher/actions";
2525
import { IRoomState } from "../../../../../src/components/structures/RoomView";
2626
import { createTestClient, flushPromises, getRoomContext, mkEvent, mkStubRoom } from "../../../../test-utils";
27-
import { SendWysiwygComposer } from "../../../../../src/components/views/rooms/wysiwyg_composer";
27+
import { SendWysiwygComposer } from "../../../../../src/components/views/rooms/wysiwyg_composer/";
2828
import { aboveLeftOf } from "../../../../../src/components/structures/ContextMenu";
2929
import { ComposerInsertPayload, ComposerType } from "../../../../../src/dispatcher/payloads/ComposerInsertPayload";
3030
import { setSelection } from "../../../../../src/components/views/rooms/wysiwyg_composer/utils/selection";
@@ -106,7 +106,7 @@ describe("SendWysiwygComposer", () => {
106106
customRender(jest.fn(), jest.fn(), false, true);
107107

108108
// Then
109-
expect(screen.getByTestId("WysiwygComposer")).toBeTruthy();
109+
waitFor(() => expect(screen.getByTestId("WysiwygComposer")).toBeTruthy());
110110
});
111111

112112
it("Should render PlainTextComposer when isRichTextEnabled is at false", () => {

0 commit comments

Comments
 (0)