From 345c3c26116019fc2fb18562cd1346f79b3ad3e4 Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Tue, 23 Oct 2018 15:50:55 +0200 Subject: [PATCH] W.I.P. implementing TinyMCE editor and deprecating CKEditor: - Add d-n-d image upload --- api/js/etemplate/et2_widget_htmlarea.js | 27 +++++++++--------- api/src/Etemplate/Widget/Vfs.php | 37 +++++++++++++++---------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/api/js/etemplate/et2_widget_htmlarea.js b/api/js/etemplate/et2_widget_htmlarea.js index d4e09e0d87..5cf2e7f35c 100644 --- a/api/js/etemplate/et2_widget_htmlarea.js +++ b/api/js/etemplate/et2_widget_htmlarea.js @@ -98,6 +98,18 @@ var et2_htmlarea = (function(){ "use strict"; return et2_inputWidget.extend([et2 doLoadingFinished: function() { this._super.apply(this, arguments); if(this.mode == 'ascii' || this.editor != null) return; + var imageUpload = ''; + + if (this.options.imageUpload[0] !== '/' && this.options.imageUpload.substr(0, 4) != 'http') + { + imageUpload = egw.ajaxUrl("EGroupware\\Api\\Etemplate\\Widget\\Vfs::ajax_htmlarea_upload")+ + '&request_id='+this.getInstanceManager().etemplate_exec_id+'&widget_id='+this.options.imageUpload+'&type=htmlarea'; + imageUpload = imageUpload.substr(egw.webserverUrl.length+1); + } + else + { + imageUpload = this.options.imageUpload.substr(egw.webserverUrl.length+1); + } // default settings for initialization var settings = { @@ -113,9 +125,9 @@ var et2_htmlarea = (function(){ "use strict"; return et2_inputWidget.extend([et2 paste_data_images: true, browser_spellcheck: true, contextmenu: false, - images_upload_url: this.options.imageUpload, + images_upload_url: imageUpload, file_picker_callback: jQuery.proxy(this._file_picker_callback, this), - images_upload_handler: jQuery.proxy(this._images_upload_handler, this), + images_upload_handler: this.options.images_upload_handler, init_instance_callback : jQuery.proxy(this._instanceIsReady, this), plugins: [ "print fullpage searchreplace autolink directionality "+ @@ -189,17 +201,6 @@ var et2_htmlarea = (function(){ "use strict"; return et2_inputWidget.extend([et2 vfsSelect.click(); }, - /** - * - * @param {type} _blobInfo image blob info - * @param {type} _success success callback - * @param {type} _failure failure callback - * @returns {} - */ - _images_upload_handler: function(_blobInfo, _success, _failure) { - if (typeof this.images_upload_handler == 'function') return this.images_upload_handler.call(arguments, this); - }, - /** * Callback when instance is ready * diff --git a/api/src/Etemplate/Widget/Vfs.php b/api/src/Etemplate/Widget/Vfs.php index 8ce6bd63de..75d56929f9 100644 --- a/api/src/Etemplate/Widget/Vfs.php +++ b/api/src/Etemplate/Widget/Vfs.php @@ -140,12 +140,14 @@ class Vfs extends File } /** - * Upload via dragging images into ckeditor + * Upload via dragging images into ckeditor/htmlarea(tinymce) */ public static function ajax_htmlarea_upload() { $request_id = urldecode($_REQUEST['request_id']); + $type = $_REQUEST['type']; $widget_id = $_REQUEST['widget_id']; + $file = $type == 'htmlarea' ? $_FILES['file'] : $_FILES['upload']; if(!self::$request = Etemplate\Request::read($request_id)) { $error = lang("Could not read session"); @@ -156,7 +158,7 @@ class Vfs extends File // Can't use callback $error = lang("Could not get template for file upload, callback skipped"); } - elseif (!isset($_FILES['upload'])) + elseif (!isset($file)) { $error = lang('No _FILES[upload] found!'); } @@ -164,14 +166,14 @@ class Vfs extends File { $data = self::$request->content[$widget_id]; $path = self::store_file($path = (!is_array($data) && $data[0] == '/' ? $data : - self::get_vfs_path($data['to_app'].':'.$data['to_id'])).'/', $_FILES['upload']); + self::get_vfs_path($data['to_app'].':'.$data['to_id'])).'/', $file); // store temp. vfs-path like links to be able to move it to correct location after entry is stored if (!$data['to_id'] || is_array($data['to_id'])) { Api\Link::link($data['to_app'], $data['to_id'], Api\Link::VFS_APPNAME, array( - 'name' => $_FILES['upload']['name'], - 'type' => $_FILES['upload']['type'], + 'name' => $file['name'], + 'type' => $file['upload']['type'], 'tmp_name' => Api\Vfs::PREFIX.$path, )); self::$request->content = array_merge(self::$request->content, array($widget_id => $data)); @@ -180,17 +182,24 @@ class Vfs extends File // switch regular JSON response handling off Api\Json\Request::isJSONRequest(false); - $file = array( - "uploaded" => (int)empty($error), - "fileName" => Api\Html::htmlspecialchars($_FILES['upload']['name']), - "url" => Api\Framework::link(Api\Vfs::download_url($path)), - "error" => array( - "message" => $error, - ) - ); + if ($type == 'htmlarea') + { + $result = array ('location' => Api\Framework::link(Api\Vfs::download_url($path))); + } + else + { + $result = array( + "uploaded" => (int)empty($error), + "fileName" => Api\Html::htmlspecialchars($file['name']), + "url" => Api\Framework::link(Api\Vfs::download_url($path)), + "error" => array( + "message" => $error, + ) + ); + } header('Content-Type: application/json; charset=utf-8'); - echo json_encode($file); + echo json_encode($result); exit; }