wang-edit.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <template>
  2. <div class="text-edit">
  3. <div id="myeditor"></div>
  4. </div>
  5. </template>
  6. <script type="text/ecmascript-6">
  7. import WangEditor from 'wangeditor';
  8. export default {
  9. name: 'wang-edit',
  10. // components: { WangEditor },
  11. props: {
  12. text: {
  13. type: String
  14. }
  15. },
  16. data() {
  17. return {
  18. editor: null,
  19. isPaste: false,
  20. info_: '', // 当前修改的文本
  21. currentImg: '' // 当前粘贴图片
  22. };
  23. },
  24. computed: {},
  25. mounted() {
  26. this.createEditor();
  27. this.editor.txt.html(this.text);
  28. },
  29. methods: {
  30. dataURLtoBlob(dataurl) {
  31. var arr = dataurl.split(',');
  32. var mime = arr[0].match(/:(.*?);/)[1];
  33. var bstr = atob(arr[1]);
  34. var n = bstr.length;
  35. var u8arr = new Uint8Array(n);
  36. while (n--) {
  37. u8arr[n] = bstr.charCodeAt(n);
  38. }
  39. return new Blob([u8arr], { type: mime });
  40. },
  41. createEditor() {
  42. var that = this;
  43. this.editor = new WangEditor('#myeditor');
  44. console.log(this.editor.customConfig);
  45. this.editor.customConfig.pasteFilterStyle = true; // 带样式粘贴
  46. this.editor.customConfig.pasteIgnoreImg = true; // 忽略粘贴内容中的图片
  47. this.editor.customConfig.showLinkImg = false;
  48. this.editor.customConfig.menus = [
  49. 'head',
  50. 'bold',
  51. 'fontSize',
  52. 'fontName',
  53. 'italic',
  54. 'underline',
  55. 'strikeThrough',
  56. 'indent',
  57. 'lineHeight',
  58. 'foreColor',
  59. 'backColor',
  60. // 'link',
  61. // 'list',
  62. 'todo',
  63. 'justify',
  64. 'quote',
  65. // 'emoticon',
  66. 'image',
  67. // 'video',
  68. // 'table',
  69. // 'code',
  70. 'splitLine',
  71. 'undo',
  72. 'redo'
  73. ];
  74. // 自定义上传图片
  75. this.editor.customConfig.customUploadImg = function (files, insert) {
  76. // 粘贴截图会触发customUploadImg方法,不走自定义粘贴
  77. that.isPaste = true;
  78. that.upPic(files[0], (res) => {
  79. insert(res);
  80. that.isPaste = false;
  81. });
  82. };
  83. // 粘贴网络图片(不包括截图)时,使用pasteTextHandle自定义过滤原始图片
  84. this.editor.customConfig.pasteTextHandle = function (content) {
  85. return content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi, '');
  86. };
  87. // 文本实时更新
  88. this.editor.customConfig.onchange = (html) => {
  89. that.info_ = html;
  90. that.$emit('change', that.info_); // 将内容同步到父组件中
  91. };
  92. // 自定义粘贴截图图片
  93. window.addEventListener('paste', function (e) {
  94. function dataURLtoBlob(dataurl) {
  95. // base64转blob方法
  96. var arr = dataurl.split(',');
  97. var mime = arr[0].match(/:(.*?);/)[1];
  98. var bstr = atob(arr[1]);
  99. var n = bstr.length;
  100. var u8arr = new Uint8Array(n);
  101. while (n--) {
  102. u8arr[n] = bstr.charCodeAt(n);
  103. }
  104. return new Blob([u8arr], { type: mime });
  105. }
  106. var userAgent = navigator.userAgent; // 取得浏览器的userAgent字符串
  107. var isIE11 =
  108. userAgent.indexOf('Trident') > -1 &&
  109. userAgent.indexOf('rv:11.0') > -1;
  110. if (isIE11) {
  111. // IE11粘贴图片需要注意:
  112. // 1.粘贴截图返回base64,使用querySelectorAll获取base64地址转为blob
  113. // 2.插入图片方法insertImage
  114. // 3.使用base64图片标签正则表达式匹配并删除原base64图片
  115. setTimeout(() => {
  116. // 获取最新一张粘贴的base64图片地址并转化为blob文件
  117. var imgList = document.querySelectorAll('.w-e-text img');
  118. var len = imgList.length;
  119. var src_str = imgList[len - 1].src;
  120. var file = dataURLtoBlob(src_str);
  121. // 获取现在编辑器所有的内容,匹配所有的base64图片并删除
  122. let currentHtml = that.editor.txt.html();
  123. that.editor.txt.html(
  124. currentHtml.replace(
  125. /<img [^>]*src=['"](data:+)([^'"]+)[^>]*>/gi,
  126. ''
  127. )
  128. );
  129. that.upPic(file, (res) => {
  130. // 图片转oss插入编辑器
  131. that.editor.cmd.do('insertImage', res);
  132. });
  133. }, 0);
  134. } else {
  135. // chrome浏览器粘贴图片需要注意:
  136. // 1.使用clipboardData获取粘贴图片file文件
  137. // 2.插入图片方法insertHtml
  138. // 3.使用wangEditor自带的pasteTextHandle方法过滤原始图片
  139. if (that.isPaste) {
  140. // 粘贴截图时会触发customUploadImg方法
  141. return false;
  142. }
  143. let clipboardData = e.clipboardData;
  144. let types;
  145. let items;
  146. let item;
  147. items = clipboardData.items;
  148. types = clipboardData.types || [];
  149. for (let i = 0; i < types.length; i++) {
  150. if (types[i] === 'Files') {
  151. item = items[i];
  152. break;
  153. }
  154. }
  155. if (item && item.type.match(/^image\//i)) {
  156. // 筛选图片文件自定义上传
  157. that.upPic(item.getAsFile(), (res) => {
  158. that.editor.cmd.do(
  159. 'insertHtml',
  160. '<img src="' + res + '" style="max-width:100%;"/>'
  161. );
  162. });
  163. }
  164. }
  165. });
  166. this.editor.create();
  167. },
  168. upPic(file, fun) {
  169. this.httpUploadImg({ prefix: 'other', file }).then(
  170. (res) => {
  171. fun(res);
  172. },
  173. (err) => {
  174. console.log(err);
  175. }
  176. );
  177. }
  178. },
  179. watch: {
  180. text(value) {
  181. if (value !== this.editor.txt.html()) {
  182. this.editor.txt.html(value);
  183. }
  184. }
  185. }
  186. };
  187. </script>
  188. <style lang="stylus" rel="stylesheet/stylus" scoped>
  189. @import '~assets/main.styl';
  190. * {
  191. box-sizing: border-box;
  192. }
  193. .text-edit {
  194. z-index: 1;
  195. }
  196. >>>.w-e-text-container {
  197. height: 720px !important;
  198. }
  199. </style>