Skip to content

Commit 72c4201

Browse files
authored
[WC-1621]: Rich Text image upload performance drop (#313)
2 parents e3775fe + bea8cb5 commit 72c4201

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

packages/pluggableWidgets/rich-text-web/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66

77
## [Unreleased]
88

9+
### Fixed
10+
11+
- Added 1MB file size limit for pasted and dropped images.
12+
913
## [2.1.2] - 2023-01-25
1014

1115
### Fixed

packages/pluggableWidgets/rich-text-web/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "rich-text-web",
33
"widgetName": "RichText",
4-
"version": "2.1.2",
4+
"version": "2.1.3",
55
"description": "Rich inline or toolbar text editing",
66
"copyright": "© Mendix Technology BV 2022. All rights reserved.",
77
"repository": {

packages/pluggableWidgets/rich-text-web/src/components/Editor.tsx

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,26 @@ import { getCKEditorConfig } from "../utils/ckeditorConfigs";
66
import { MainEditor } from "./MainEditor";
77
import DOMPurify from "dompurify";
88

9+
const FILE_SIZE_LIMIT = 1048576; // Binary bytes for 1MB
10+
911
interface EditorProps {
1012
element: HTMLElement;
1113
widgetProps: RichTextContainerProps;
1214
}
1315

1416
type EditorHookProps = CKEditorHookProps<never>;
1517

18+
interface CKEditorEvent {
19+
data: any;
20+
listenerData: any;
21+
name: string;
22+
sender: { [key: string]: any };
23+
24+
cancel(): void;
25+
removeListener(): void;
26+
stop(): void;
27+
}
28+
1629
export class Editor extends Component<EditorProps> {
1730
widgetProps: RichTextContainerProps;
1831
editor: CKEditorInstance | null;
@@ -31,6 +44,8 @@ export class Editor extends Component<EditorProps> {
3144
this.editorHookProps = this.getNewEditorHookProps();
3245
this.onChange = debounce(this.onChange.bind(this), 500);
3346
this.onKeyPress = this.onKeyPress.bind(this);
47+
this.onPasteContent = this.onPasteContent.bind(this);
48+
this.onDropContent = this.onDropContent.bind(this);
3449
}
3550

3651
setNewRenderProps(): void {
@@ -115,6 +130,39 @@ export class Editor extends Component<EditorProps> {
115130
this.widgetProps.onKeyPress?.execute();
116131
}
117132

133+
onPasteContent(event: CKEditorEvent): void {
134+
if (event.data.dataTransfer.isFileTransfer()) {
135+
for (let i = 0; i < event.data.dataTransfer.getFilesCount(); i++) {
136+
if (event.data.dataTransfer.getFile(i).size > FILE_SIZE_LIMIT) {
137+
this.editor.showNotification(
138+
`The image ${
139+
event.data.dataTransfer.getFile(i).name
140+
} is larger than the 1MB limit. Please choose a smaller image and try again.`,
141+
"warning"
142+
);
143+
event.cancel();
144+
break;
145+
}
146+
}
147+
}
148+
}
149+
onDropContent(event: CKEditorEvent): void {
150+
if (event.data.dataTransfer.isFileTransfer()) {
151+
for (let i = 0; i < event.data.dataTransfer.getFilesCount(); i++) {
152+
if (event.data.dataTransfer.getFile(i).size > FILE_SIZE_LIMIT) {
153+
this.editor.showNotification(
154+
`The image ${
155+
event.data.dataTransfer.getFile(i).name
156+
} is larger than the 1MB limit. Please choose a smaller image and try again.`,
157+
"warning"
158+
);
159+
event.cancel();
160+
break;
161+
}
162+
}
163+
}
164+
}
165+
118166
// onChange is wrapped in debounce, so, we always need to check
119167
// weather we sill have editor.
120168
onChange(_event: CKEditorEventPayload<"change">): void {
@@ -132,12 +180,16 @@ export class Editor extends Component<EditorProps> {
132180
if (this.editor && !this.editor.readOnly) {
133181
this.editor.on("change", this.onChange);
134182
this.editor.on("key", this.onKeyPress);
183+
this.editor.on("paste", this.onPasteContent);
184+
this.editor.on("drop", this.onDropContent);
135185
}
136186
}
137187

138188
removeListeners(): void {
139189
this.editor?.removeListener("change", this.onChange);
140190
this.editor?.removeListener("key", this.onKeyPress);
191+
this.editor?.removeListener("paste", this.onPasteContent);
192+
this.editor?.removeListener("drop", this.onDropContent);
141193
}
142194

143195
updateEditorState(

packages/pluggableWidgets/rich-text-web/src/package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<package xmlns="http://www.mendix.com/package/1.0/">
3-
<clientModule name="RichText" version="2.1.2" xmlns="http://www.mendix.com/clientModule/1.0/">
3+
<clientModule name="RichText" version="2.1.3" xmlns="http://www.mendix.com/clientModule/1.0/">
44
<widgetFiles>
55
<widgetFile path="RichText.xml" />
66
</widgetFiles>

0 commit comments

Comments
 (0)