diff --git a/WP-Editor.MD.php b/WP-Editor.MD.php index ec80b761..fbb4bf4a 100644 --- a/WP-Editor.MD.php +++ b/WP-Editor.MD.php @@ -24,7 +24,10 @@ } //引入资源模板 -require WP_EDITORMD_PLUGIN_PATH . '/editormd_class.php'; +require WP_EDITORMD_PLUGIN_PATH . '/editormd_class.php'; + +//引入粘贴 +require WP_EDITORMD_PLUGIN_PATH . '/imagepaste.php'; //引入设置页面 require WP_EDITORMD_PLUGIN_PATH . '/editormd_options.php'; @@ -39,6 +42,7 @@ add_action('admin_init', array($editormd, 'editormd_jetpack_markdown_posting_always_on'), 11); add_action('plugins_loaded', array($editormd, 'editormd_init_languages')); add_action('plugins_loaded', array($editormd, 'editormd_jetpack_markdown_load_textdomain')); +add_action('admin_notices', array($editormd, 'imagepaste_admin_notices')); add_filter('quicktags_settings', array($editormd, 'quicktags_settings'), $editorId = 'content');// 删除编辑器的快捷按钮标签 add_filter('pre_option_' . WPCom_Markdown::POST_OPTION, '__return_true'); add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($editormd, 'jetpack_markdown_settings_link'));//添加插件设置链接 diff --git a/editormd_class.php b/editormd_class.php index 1860450f..042e94f9 100644 --- a/editormd_class.php +++ b/editormd_class.php @@ -143,13 +143,34 @@ public function load_editormd() document.getElementById("htmlDom").remove(); }; //Emoji表情自定义服务器地址 - editormd.emoji = { + editormd.emoji = { path : 'https:' === document.location.protocol ? "https://staticfile.qnssl.com/emoji-cheat-sheet/1.0.0/" : "http://cdn.staticfile.org/emoji-cheat-sheet/1.0.0/", ext : ".png" }; - // - - }); + //TODO 图片粘贴上传 + jQuery(document).on('paste', function(event) { + var data = { + action: "imagepaste_action", + dataurl: results.dataURL, + filename: results.filename, + name: results.name + }; + new jQuery.ajax({ + url: ajaxurl, + type: "post", + data: data, + success: function (request) { + var obj = eval("(" + request + ")"); + if (obj.error) { + alert(obj.error) + } else { + var id = iframe.contents().find("body").attr("id"); + EditorMD.insertValue(''));//HTML + } + } + }) + }) + }) //]]> 0) { + echo "

"; + foreach ($errors as $message) echo 'Image Paste: '.$message.'
'; + echo "

"; + } + } //编辑器快捷按键 - function quicktags_settings($qtInit) + public function quicktags_settings($qtInit) { $qtInit['buttons'] = ' '; return $qtInit; diff --git a/imagepaste.php b/imagepaste.php new file mode 100644 index 00000000..a0475ccf --- /dev/null +++ b/imagepaste.php @@ -0,0 +1,100 @@ +''); + $upload = wp_upload_dir(); + $uploadUrl=$upload['url']; + $uploadDir=$upload['path']; + + list($data,$image)=explode(';',$_REQUEST['dataurl']); + list($field,$type)=explode(':',$data); + list($encoding,$content)=explode(',',$image); + if ($type=='image/png') $extension='png'; + + $name=md5($_REQUEST['dataurl']); + if (!$extension) { + $result['error']="Could not determine image extension type"; + } else { + $file=$uploadDir.'/'.$name.'.'.$extension; + $fileUrl=$uploadUrl.'/'.$name.'.'.$extension; + file_put_contents($file,base64_decode($content)); + if (defined('W3TC')) { + $result['w3tc']=1; + ob_start(); + $w3tc=new ImagepasteW3tc(); + //$result['w3tcconfig']=$w3tc->config; + if ($w3tc->engine=='rscf') $w3tc->upload($uploadDir,$uploadUrl,$name.'.'.$extension); + $result['log']=$w3tc->log; + $result['output']=ob_get_clean(); + } else $result['w3tc']=0; + + $result['url']=$fileUrl; + $result['elementid']=$_REQUEST['elementid']; + } + echo json_encode($result); + die(); // this is required to return a proper result +} + +class ImagepasteW3tc { + var $log=array(); + + function __construct() { + $this->config=array(); + $file=W3TC_CACHE_CONFIG_DIR.'/master.php'; + if (file_exists($file)) $this->config=include(W3TC_CACHE_CONFIG_DIR.'/master.php'); + $this->engine=$this->config['cdn.engine']; + $this->username=$this->config['cdn.rscf.user']; + $this->apikey=$this->config['cdn.rscf.key']; + $this->container=$this->config['cdn.rscf.container']; + } + + function upload($uploadDir,$uploadUrl,$file) { + if (!file_exists(W3TC_LIB_DIR.'/CF/cloudfiles.php')) return; + + require_once(W3TC_LIB_DIR.'/CF/cloudfiles.php'); + + $prefix=str_replace(home_url().'/','',$uploadUrl); + + // Lets connect to Rackspace + $authentication = new CF_Authentication($this->username, $this->apikey); + $authentication->authenticate(); + $connection = null; + try { + $connection = new CF_Connection($authentication); + } + catch(AuthenticationException $e) { + $this->log('Error 1: '.$e->getMessage()); + } + + $container = null; + try + { + $container = $connection->get_container($this->container); + $container->make_public(); + $this->log('Connected to container '.$this->container); + } + catch(Exception $e) { + $this->log('Error 2: '.$e->getMessage()); + } + + try { + $object = $container->create_object($prefix.'/'.$file); + $object->load_from_filename($uploadDir.'/'.$file); + $this->log('Copied '.$file.' to '.$object->public_uri(), 'info'); + } catch(Exception $e) { + $this->log('Error 3: '.$e->getMessage()); + } + } + + function log($s) { + $this->log[]=is_array($s) ? print_r($s,true) : $s; + } +} diff --git a/js/jquery.paste_image_reader.js b/js/jquery.paste_image_reader.js new file mode 100755 index 00000000..98f6ca53 --- /dev/null +++ b/js/jquery.paste_image_reader.js @@ -0,0 +1,84 @@ +(function ($) { + var defaults; + $.event.fix = (function (originalFix) { + return function (event) { + event = originalFix.apply(this, arguments); + if (event.type.indexOf("copy") === 0 || event.type.indexOf("paste") === 0) { + event.clipboardData = event.originalEvent.clipboardData + } + return event + } + })($.event.fix); + defaults = {callback: $.noop, matchType: /image.*/}; + return $.fn.pasteImageReader = function (options) { + if (typeof options === "function") { + options = {callback: options} + } + options = $.extend({}, defaults, options); + return this.each(function () { + var $this, element; + element = this; + $this = $(this); + return $this.bind("paste", function (event) { + var clipboardData, found; + found = false; + if (jQuery.browser.chrome) { + clipboardData = event.clipboardData; + return Array.prototype.forEach.call(clipboardData.types, function (type, i) { + var file, reader; + if (found) { + return + } + if (type.match(options.matchType) || clipboardData.items[i].type.match(options.matchType)) { + file = clipboardData.items[i].getAsFile(); + reader = new FileReader(); + reader.onload = function (evt) { + return options.callback.call(element, { + dataURL: evt.target.result, + event: evt, + file: file, + name: file.name + }) + }; + reader.readAsDataURL(file); + return found = true + } + }) + } else { + if (jQuery.browser.mozilla) { + var that = this; + setTimeout(function () { + images = jQuery(that).find("img"); + for (i = 0; i < images.length; i++) { + if (jQuery(images[i]).attr("src").substr(0, 5) == "data:") { + jQuery(images[i]).attr("id", "imagepaste_temp_" + i); + var data = { + action: "imagepaste_action", + dataurl: jQuery(images[i]).attr("src"), + elementid: "imagepaste_temp_" + i + }; + jQuery("#publishing-action #publish").attr("disabled", true); + new jQuery.ajax({ + url: ajaxurl, + type: "post", + data: data, + success: function (request) { + jQuery("#publishing-action #publish").removeAttr("disabled"); + var obj = eval("(" + request + ")"); + var target = jQuery("html").find("iframe").contents().find("body").find("#" + obj.elementid); + target.attr("data-mce-src", obj.url); + target.attr("src", obj.url); + target.attr("id", null) + } + }) + } + } + }, 1) + } else { + console.log("not supported") + } + } + }) + }) + } +})(jQuery); \ No newline at end of file