@@ -6,13 +6,26 @@ import { getCKEditorConfig } from "../utils/ckeditorConfigs";
66import { MainEditor } from "./MainEditor" ;
77import DOMPurify from "dompurify" ;
88
9+ const FILE_SIZE_LIMIT = 1048576 ; // Binary bytes for 1MB
10+
911interface EditorProps {
1012 element : HTMLElement ;
1113 widgetProps : RichTextContainerProps ;
1214}
1315
1416type 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+
1629export 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 (
0 commit comments