| 
									
										
										
										
											2025-05-03 23:39:30 +03:00
										 |  |  | import { Command, FileRepository, Model, type NodeAttributes, type Writer } from "ckeditor5"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface FileUploadOpts { | 
					
						
							|  |  |  |     file: File[]; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-05-03 23:13:01 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default class FileUploadCommand extends Command { | 
					
						
							|  |  |  | 	refresh() { | 
					
						
							|  |  |  | 		this.isEnabled = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * Executes the command. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * @fires execute | 
					
						
							|  |  |  | 	 * @param {Object} options Options for the executed command. | 
					
						
							|  |  |  | 	 * @param {File|Array.<File>} options.file The file or an array of files to upload. | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2025-05-03 23:39:30 +03:00
										 |  |  | 	execute( options: FileUploadOpts ) { | 
					
						
							| 
									
										
										
										
											2025-05-03 23:13:01 +03:00
										 |  |  | 		const editor = this.editor; | 
					
						
							|  |  |  | 		const model = editor.model; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const fileRepository = editor.plugins.get( FileRepository ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		model.change( writer => { | 
					
						
							|  |  |  | 			const filesToUpload = options.file; | 
					
						
							|  |  |  | 			for ( const file of filesToUpload ) { | 
					
						
							|  |  |  | 				uploadFile( writer, model, fileRepository, file ); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} ); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * 	Handles uploading single file. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-05-03 23:39:30 +03:00
										 |  |  | function uploadFile( writer: Writer, model: Model, fileRepository: FileRepository, file: File ) { | 
					
						
							| 
									
										
										
										
											2025-05-03 23:13:01 +03:00
										 |  |  | 	const loader = fileRepository.createLoader( file ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Do not throw when upload adapter is not set. FileRepository will log an error anyway.
 | 
					
						
							|  |  |  | 	if ( !loader ) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	insertFileLink( writer, model, { href: '', uploadId: loader.id }, file ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-03 23:39:30 +03:00
										 |  |  | function insertFileLink( writer: Writer, model: Model, attributes: NodeAttributes = {}, file: File ) { | 
					
						
							| 
									
										
										
										
											2025-05-03 23:13:01 +03:00
										 |  |  | 	const placeholder = writer.createElement( 'reference', attributes ); | 
					
						
							|  |  |  | 	model.insertContent( placeholder, model.document.selection ); | 
					
						
							|  |  |  | 	writer.insertText( ' ', placeholder, 'after' ); | 
					
						
							|  |  |  | } |