table.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template lang="html">
  2. <div class="editor cont">
  3. <div ref="toolbar" class="toolbar">
  4. </div>
  5. <div ref="editor" class="text">
  6. </div>
  7. </div>
  8. </template>
  9. <script>
  10. import E from "wangeditor";
  11. import "../../common/aws-sdk-2.235.1.min";
  12. import { Loading } from 'element-ui';
  13. // import 'wangeditor/release/wangEditor.min.css'
  14. export default {
  15. name: "editoritem",
  16. data() {
  17. return {
  18. // uploadPath,
  19. editor: null,
  20. info_: null,
  21. };
  22. },
  23. model: {
  24. prop: "value",
  25. event: "change",
  26. },
  27. props: {
  28. value: {
  29. type: String,
  30. default: "",
  31. },
  32. isClear: {
  33. type: Boolean,
  34. default: false,
  35. },
  36. placeholder: {
  37. type: String,
  38. default: "请输入正文"
  39. }
  40. },
  41. watch: {
  42. isClear(val) {
  43. // 触发清除文本域内容
  44. if (val) {
  45. this.editor.txt.clear();
  46. this.info_ = null;
  47. }
  48. },
  49. value: function (value) {
  50. if (value !== this.editor.txt.html()) {
  51. this.editor.txt.html(this.value);
  52. }
  53. },
  54. //value为编辑框输入的内容,这里我监听了一下值,当父组件调用得时候,如果给value赋值了,子组件将会显示父组件赋给的值
  55. },
  56. mounted() {
  57. this.seteditor();
  58. this.editor.txt.html(this.value);
  59. },
  60. methods: {
  61. seteditor() {
  62. this.editor = new E(this.$refs.toolbar, this.$refs.editor);
  63. // 关闭菜单栏fixed
  64. this.editor.config.menuFixed = false;
  65. // 普通的自定义菜单
  66. this.editor.config.menus = [
  67. // "head", //标题
  68. // "bold", //加粗
  69. // "fontSize", //字体大小
  70. // "fontName", //字体
  71. // "italic", //斜体
  72. // "underline", //下划线
  73. // "strikeThrough", //删除线
  74. // "indent", //缩进
  75. // "lineHeight", //行高
  76. // "foreColor",
  77. // "backColor",
  78. // "link",
  79. // "list",
  80. // "todo",
  81. // "justify",
  82. // "quote",
  83. // "emoticon",
  84. // "image",
  85. // "video",
  86. "table",
  87. // "code",
  88. // "splitLine",
  89. // "undo",
  90. // "redo",
  91. ];
  92. // 带格式粘贴
  93. this.editor.config.pasteFilterStyle = false;
  94. //忽略粘贴内容中的图片
  95. this.editor.config.pasteIgnoreImg = false;
  96. this.editor.config.showLinkImg = false;
  97. this.editor.config.placeholder = this.placeholder;
  98. var that = this;
  99. this.editor.config.customUploadImg = function (files, insert) {
  100. const loading = Loading.service({
  101. lock: true,
  102. background: 'rgba(0, 0, 0, 0.7)'
  103. });
  104. // 图片自定义上传方法
  105. for (var i = 0; i < files.length; i++) {
  106. var file = files[i];
  107. var credentials = {
  108. accessKeyId: "AKIATLPEDU37QV5CHLMH",
  109. secretAccessKey: "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",
  110. }; //秘钥形式的登录上传
  111. window.AWS.config.update(credentials);
  112. window.AWS.config.region = "cn-northwest-1"; //设置区域
  113. var bucket = new window.AWS.S3({ params: { Bucket: "ccrb" } }); //选择桶
  114. if (file) {
  115. var params = {
  116. Key:
  117. file.name.split(".")[0] +
  118. new Date().getTime() +
  119. "." +
  120. file.name.split(".")[file.name.split(".").length - 1],
  121. ContentType: file.type,
  122. Body: file,
  123. "Access-Control-Allow-Credentials": "*",
  124. ACL: "public-read",
  125. }; //key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
  126. var options = {
  127. partSize: 2048 * 1024 * 1024,
  128. queueSize: 2,
  129. leavePartsOnError: true,
  130. };
  131. bucket
  132. .upload(params, options)
  133. .on("httpUploadProgress", function (evt) {
  134. //这里可以写进度条
  135. // console.log("Uploaded : " + parseInt((evt.loaded * 80) / evt.total) + '%');
  136. })
  137. .send(function (err, data) {
  138. loading.close();
  139. if (err) {
  140. that.$message.error("上传失败");
  141. } else {
  142. //上传成功处理
  143. insert(data.Location);
  144. }
  145. });
  146. }
  147. }
  148. };
  149. //配置 自定义处理粘贴的文本内容
  150. this.editor.config.pasteTextHandle = function (content) {
  151. if (content == '' && !content) return ''
  152. var str = content
  153. str = str.replace(/<xml>[\s\S]*?<\/xml>/ig, '')
  154. str = str.replace(/<style>[\s\S]*?<\/style>/ig, '')
  155. str = str.replace(/<\/?[^>]*>/g, '')
  156. str = str.replace(/[ | ]*\n/g, '\n')
  157. str = str.replace(/&nbsp;/ig, '')
  158. // console.log('****', content)
  159. // console.log('****', str)
  160. return str
  161. };
  162. this.editor.config.onchange = (html) => {
  163. this.info_ = html; // 绑定当前逐渐地值
  164. this.$emit("change", this.info_); // 将内容同步到父组件中
  165. };
  166. // 创建富文本编辑器
  167. this.editor.create();
  168. },
  169. },
  170. };
  171. </script>
  172. <style lang="css" scoped>
  173. .editor {
  174. width: 100%;
  175. margin: 10px auto;
  176. position: relative;
  177. z-index: 0;
  178. }
  179. .toolbar {
  180. border: 1px solid #ccc;
  181. }
  182. .text {
  183. border: 1px solid #ccc;
  184. height: 400px;
  185. overflow: auto;
  186. }
  187. .editor >>> .w-e-toolbar .w-e-menu{
  188. display: flex;
  189. width: auto;
  190. align-items: center;
  191. margin-left: 10px;
  192. padding: 0 10px;
  193. }
  194. .editor >>> .w-e-toolbar {
  195. background: #f1f1f1;
  196. }
  197. .editor >>> .w-e-icon-table2::after {
  198. content: '插入表格';
  199. margin-left: 5px;
  200. }
  201. /* table 样式 */
  202. .cont>>>table {
  203. border-top: 1px solid #ccc;
  204. border-left: 1px solid #ccc;
  205. }
  206. .cont>>>table td,
  207. .cont>>>table th {
  208. border-bottom: 1px solid #ccc;
  209. border-right: 1px solid #ccc;
  210. padding: 20px 5px;
  211. }
  212. .cont>>>table th {
  213. border-bottom: 2px solid #ccc;
  214. text-align: center;
  215. }
  216. /* blockquote 样式 */
  217. .cont>>>blockquote {
  218. display: block;
  219. border-left: 8px solid #d0e5f2;
  220. padding: 5px 10px;
  221. margin: 10px 0;
  222. line-height: 1.4;
  223. font-size: 100%;
  224. background-color: #f1f1f1;
  225. }
  226. /* code 样式 */
  227. .cont>>>code {
  228. display: inline-block;
  229. *display: inline;
  230. *zoom: 1;
  231. background-color: #f1f1f1;
  232. border-radius: 3px;
  233. padding: 3px 5px;
  234. margin: 0 3px;
  235. }
  236. .cont>>>pre code {
  237. display: block;
  238. }
  239. /* ul ol 样式 */
  240. .cont>>>ul,
  241. ol {
  242. margin: 10px 0 10px 20px;
  243. }
  244. </style>