{"id":74,"date":"2021-01-04T11:30:29","date_gmt":"2021-01-04T11:30:29","guid":{"rendered":"https:\/\/blog.gwlin.com\/?p=74"},"modified":"2023-03-31T23:19:00","modified_gmt":"2023-03-31T23:19:00","slug":"summernote \u4e0a\u4f20\u6587\u4ef6\u63d2\u4ef6","status":"publish","type":"post","link":"https:\/\/www.gwlin.com\/blog\/posts\/74","title":{"rendered":"summernote \u4e0a\u4f20\u6587\u4ef6\u63d2\u4ef6"},"content":{"rendered":"\n<p>\u516c\u53f8\u7ba1\u7406\u540e\u53f0\u7684\u7f16\u8f91\u5668\u4e0d\u597d\u7528\uff0c\u88ab\u516c\u53f8\u8fd0\u8425\u7f20\u7740\u4e0d\u653e\uff0c\u8981\u6539\u3002\u4e8e\u662f\u627e\u4e86\u4e00\u5708\uff0c\u8c8c\u4f3csummernote\u624d\u597d\u4f7f\u4e00\u70b9\u3002\u4f46\u8fd9\u4e2a\u7f16\u8f91\u5668\u6ca1\u6709\u4e0a\u4f20\u529f\u80fd\uff0c\u4e8e\u662f\u505a\u4e86\u4e00\u4e2a\u3002\u6211\u672c\u6765\u662f\u5199PHP\u540e\u7aef\u7684\u3002\u3002\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\"  lang=\"javascript\" class=\"language-javascript\">(function(factory) {\n  \/* global define *\/\n  if (typeof define === &#039;function&#039; &amp;&amp; define.amd) {\n    \/\/ AMD. Register as an anonymous module.\n    define([&#039;jquery&#039;], factory);\n  } else if (typeof module === &#039;object&#039; &amp;&amp; module.exports) {\n    \/\/ Node\/CommonJS\n    module.exports = factory(require(&#039;jquery&#039;));\n  } else {\n    \/\/ Browser globals\n    factory(window.jQuery);\n  }\n}(function($) {\n\n  $.extend($.summernote.options, {\n    upload: {\n      uploadUrl:&#039;&#039;,\n      token:&#039;&#039;,\n    }\n  });\n\n  \/\/ Extends plugins for adding hello.\n  \/\/  - plugin is external module for customizing.\n  $.extend($.summernote.plugins, {\n    \/**\n     * @param {Object} context - context object has status of editor.\n     *\/\n    &#039;upload&#039;: function(context) {\n      var self = this;\n\n      \/\/ ui has renders to build ui elements.\n      \/\/  - you can create a button with `ui.button`\n      var ui = $.summernote.ui;\n\n      \/\/ add hello button\n      context.memo(&#039;button.upload&#039;, function() {\n        \/\/ create button\n        var button = ui.button({\n          contents: &#039;&lt;i class=&quot;fa fa-file-archive-o&quot;\/&gt; \u4e0a\u4f20&#039;,\n          tooltip: &#039;\u4e0a\u4f20\u56fe\u7247\uff0c\u97f3\u9891\uff0c\u89c6\u9891\u7b49&#039;,\n          click: function() {\n            self.$panel.click()\n          }\n        });\n\n        \/\/ create jQuery object from button instance.\n        return button.render();\n      });\n\n      \/\/ This events will be attached when editor is initialized.\n      this.events = {\n        \/\/ This will be called after modules are initialized.\n        &#039;summernote.init&#039;: function(we, e) {\n          \/\/console.log(&#039;summernote initialized&#039;, we, e);\n        },\n        \/\/ This will be called when user releases a key on editable.\n        &#039;summernote.keyup&#039;: function(we, e) {\n          \/\/console.log(&#039;summernote keyup&#039;, we, e);\n        }\n      };\n\n      \/\/ This method will be called when editor is initialized by $(&#039;..&#039;).summernote();\n      \/\/ You can create elements for plugin\n      this.initialize = function() {\n        this.$panel=$(&#039;&lt;input type=&quot;file&quot; multiple&gt;&#039;).css({&#039;display&#039;:&#039;none&#039;});\n        this.$panel.change(function(){\n          var $this=$(this);\n          var files=$this[0].files;\n          console.log(&#039;files&#039;,files);\n          for(var i =0;i&lt;files.length;i++){\n            var fd=new FormData();\n            var file=files[i];\n            console.log(file);\n            fd.append(&#039;file&#039;,file);\n            fd.append(&#039;_token&#039;,context.options.upload.token);\n            $.ajax({\n              url: context.options.upload.uploadUrl,\n              type: &#039;POST&#039;,\n              cache: false,\n              data: fd,\n              processData: false,\n              contentType: false,\n              success:function(res){\n                console.log(res);\n                if(res.status){\n                  var fileType=file.type.split(&#039;\/&#039;);\n                  switch (fileType[0]) {\n                    case &#039;image&#039;:\n                      context.invoke(&#039;editor.insertImage&#039;, res.data.src);\n                      break;\n                    case &#039;audio&#039;:\n                      var audio=document.createElement(&#039;audio&#039;);\n                      audio.src=res.data.src;\n                      audio.controls=true;\n                      context.invoke(&#039;editor.insertNode&#039;, audio);\n                      break;\n                    case &#039;video&#039;:\n                      var video=document.createElement(&#039;video&#039;);\n                      video.src=res.data.src;\n                      video.controls=true;\n                      video.style.height=&#039;auto&#039;;\n                      video.style.maxWidth=&#039;100%&#039;;\n                      context.invoke(&#039;editor.insertNode&#039;, video);\n                      break;\n                    default:\n                      context.invoke(&#039;editor.createLink&#039;, {url:res.data.src,text:file.name});\n                  }\n                }\n              },\n              error:function(){\n\n              },\n              complete:function(){\n                $this.val(&#039;&#039;)\n              }\n            });\n          }\n        });\n      };\n\n      \/\/ This methods will be called when editor is destroyed by $(&#039;..&#039;).summernote(&#039;destroy&#039;);\n      \/\/ You should remove elements on `initialize`.\n      this.destroy = function() {\n        this.$panel.remove();\n        this.$panel = null;\n      };\n    }\n  });\n}));<\/code><\/pre>\n\n\n\n<p>\u63d2\u4ef6\u7684\u4e0a\u4f20\u53c2\u6570\u914d\u5408\u4e86\u516c\u53f8\u7684laravel\u6846\u67b6\uff0c\u7528\u7684\u65f6\u5019\u6ce8\u610f\u4fee\u6539\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\"  lang=\"javascript\" class=\"language-javascript\">$.extend($.summernote.options, {\n\n    upload: {\n\n      uploadUrl:&#039;&#039;,\n\n      token:&#039;&#039;,\n\n    }\n\n  });<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u53ef\u4ee5\u4fee\u6539\u4f20\u5165\u53c2\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\"  lang=\"javascript\" class=\"language-javascript\">var fd=new FormData();\n\nvar file=files[i];\n\nconsole.log(file);\n\nfd.append(&#039;file&#039;,file);\n\nfd.append(&#039;_token&#039;,context.options.upload.token);\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u662fajax\u7684\u4e0a\u4f20\u53c2\u6570\u3002 <\/p>\n\n\n\n<p><a href=\"https:\/\/summernote.org\/plugins\">summernote\u7684\u6587\u6863<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u516c\u53f8\u7ba1\u7406\u540e\u53f0\u7684\u7f16\u8f91\u5668\u4e0d\u597d\u7528\uff0c\u88ab\u516c\u53f8\u8fd0\u8425\u7f20\u7740\u4e0d\u653e\uff0c\u8981\u6539\u3002\u4e8e\u662f\u627e\u4e86\u4e00\u5708\uff0c\u8c8c\u4f3csummernote\u624d\u597d\u4f7f\u4e00\u70b9\u3002\u4f46\u8fd9\u4e2a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[179],"tags":[191],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-notes","tag-javascript"],"_links":{"self":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":0,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gwlin.com\/blog\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}