From 5b530792e5724e109db131a482ddc04a8b3b9aa1 Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Wed, 24 Apr 2024 22:45:36 +0900 Subject: [PATCH 1/8] Initial implementation of auto upload on paste or drop --- .../ckeditor/plugins/rx_upload/plugin.js | 116 ++++++++++++++++++ modules/editor/skins/ckeditor/editor.html | 2 +- 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 common/js/plugins/ckeditor/ckeditor/plugins/rx_upload/plugin.js diff --git a/common/js/plugins/ckeditor/ckeditor/plugins/rx_upload/plugin.js b/common/js/plugins/ckeditor/ckeditor/plugins/rx_upload/plugin.js new file mode 100644 index 000000000..a36b27000 --- /dev/null +++ b/common/js/plugins/ckeditor/ckeditor/plugins/rx_upload/plugin.js @@ -0,0 +1,116 @@ +/** + * Upload plugin for Rhymix + */ +CKEDITOR.plugins.add('rx_upload', { + init: function(editor) { + + /** + * The main event handler for paste and drop. + */ + editor.on('paste', function(event) { + + // Check if the pasted data contains any files. + const method = event.data.method; + const dataTransfer = event.data.dataTransfer; + const files_count = dataTransfer.getFilesCount(); + if (!files_count) { + return; + } + + // Prevent the default plugin from touching these files. + event.stop(); + + // Read and upload each file. + for (let i = 0; i < files_count; i++) { + uploadFile(dataTransfer.getFile(i)); + } + }); + + /** + * Upload function. + */ + const uploadFile = function(file) { + + // Get the editor sequence. + const editor_container = $(editor.container.$).parents('.rx_ckeditor'); + const upload_container = editor_container.next('.xefu-container'); + const editor_sequence = editor_container.data('editorSequence'); + + // Generate the form data. + const form = new FormData(); + form.append('mid', window.editor_mid ? window.editor_mid : window.current_mid); + form.append('act', 'procFileUpload'); + form.append('editor_sequence', editor_sequence); + form.append('Filedata', file); + + // Upload! + $.ajax({ + url: window.request_uri, + type: 'POST', + contentType: false, + processData: false, + cache: false, + data: form, + dataType: 'json', + success: function(data) { + insertFile(upload_container, data); + reloadFileList(upload_container, data); + }, + error: function(jqXHR) { + alert('Upload error: ' + jqXHR.responseText); + } + }); + }; + + /** + * Insert file into editor. + */ + const insertFile = function(container, data) { + if (data.error != 0) { + alert(data.message); + return; + } + + let temp_code = ''; + if(/\.(gif|jpe?g|png|webp)$/i.test(data.source_filename)) { + temp_code += '' + data.source_filename + ''; + } + else if(/\.(mp3|wav|ogg|flac|aac)$/i.test(data.source_filename)) { + temp_code += '