editor.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <view class="container">
  3. <view class="page-body">
  4. <view class="wrapper">
  5. <view class="toolbar" @tap="format">
  6. <view :class="formats.bold ? 'ql-active' : ''" class="iconfont icon-zitijiacu" data-name="bold"></view>
  7. <view :class="formats.italic ? 'ql-active' : ''" class="iconfont icon-zitixieti" data-name="italic"></view>
  8. <view :class="formats.underline ? 'ql-active' : ''" class="iconfont icon-zitixiahuaxian" data-name="underline"></view>
  9. <view :class="formats.strike ? 'ql-active' : ''" class="iconfont icon-zitishanchuxian" data-name="strike"></view>
  10. <view :class="formats.align === 'left' ? 'ql-active' : ''" class="iconfont icon-zuoduiqi" data-name="align" data-value="left"></view>
  11. <view :class="formats.align === 'center' ? 'ql-active' : ''" class="iconfont icon-juzhongduiqi" data-name="align" data-value="center"></view>
  12. <view :class="formats.align === 'right' ? 'ql-active' : ''" class="iconfont icon-youduiqi" data-name="align" data-value="right"></view>
  13. <view :class="formats.align === 'justify' ? 'ql-active' : ''" class="iconfont icon-zuoyouduiqi" data-name="align" data-value="justify"></view>
  14. <view :class="formats.lineHeight ? 'ql-active' : ''" class="iconfont icon-line-height" data-name="lineHeight" data-value="2"></view>
  15. <view :class="formats.letterSpacing ? 'ql-active' : ''" class="iconfont icon-Character-Spacing" data-name="letterSpacing" data-value="2em"></view>
  16. <view :class="formats.marginTop ? 'ql-active' : ''" class="iconfont icon-722bianjiqi_duanqianju" data-name="marginTop" data-value="20px"></view>
  17. <view :class="formats.previewarginBottom ? 'ql-active' : ''" class="iconfont icon-723bianjiqi_duanhouju" data-name="marginBottom" data-value="20px"></view>
  18. <view class="iconfont icon-clearedformat" @tap="removeFormat"></view>
  19. <view :class="formats.fontFamily ? 'ql-active' : ''" class="iconfont icon-font" data-name="fontFamily" data-value="Pacifico"></view>
  20. <view :class="formats.fontSize === '24px' ? 'ql-active' : ''" class="iconfont icon-fontsize" data-name="fontSize" data-value="24px"></view>
  21. <view :class="formats.color === '#0000ff' ? 'ql-active' : ''" class="iconfont icon-text_color" data-name="color" data-value="#0000ff"></view>
  22. <view
  23. :class="formats.backgroundColor === '#00ff00' ? 'ql-active' : ''"
  24. class="iconfont icon-fontbgcolor"
  25. data-name="backgroundColor"
  26. data-value="#00ff00"
  27. ></view>
  28. <view class="iconfont icon-date" @tap="insertDate"></view>
  29. <view class="iconfont icon--checklist" data-name="list" data-value="check"></view>
  30. <view :class="formats.list === 'ordered' ? 'ql-active' : ''" class="iconfont icon-youxupailie" data-name="list" data-value="ordered"></view>
  31. <view :class="formats.list === 'bullet' ? 'ql-active' : ''" class="iconfont icon-wuxupailie" data-name="list" data-value="bullet"></view>
  32. <view class="iconfont icon-undo" @tap="undo"></view>
  33. <view class="iconfont icon-redo" @tap="redo"></view>
  34. <view class="iconfont icon-outdent" data-name="indent" data-value="-1"></view>
  35. <view class="iconfont icon-indent" data-name="indent" data-value="+1"></view>
  36. <view class="iconfont icon-fengexian" @tap="insertDivider"></view>
  37. <view class="iconfont icon-charutupian" @tap="insertImage"></view>
  38. <view :class="formats.header === 1 ? 'ql-active' : ''" class="iconfont icon-format-header-1" data-name="header" :data-value="1"></view>
  39. <view :class="formats.script === 'sub' ? 'ql-active' : ''" class="iconfont icon-zitixiabiao" data-name="script" data-value="sub"></view>
  40. <view :class="formats.script === 'super' ? 'ql-active' : ''" class="iconfont icon-zitishangbiao" data-name="script" data-value="super"></view>
  41. <view class="iconfont icon-shanchu" @tap="clear"></view>
  42. <view :class="formats.direction === 'rtl' ? 'ql-active' : ''" class="iconfont icon-direction-rtl" data-name="direction" data-value="rtl"></view>
  43. </view>
  44. <editor
  45. ref="dialogInput"
  46. id="editor"
  47. class="ql-container"
  48. placeholder="开始输入..."
  49. @blur="editInput"
  50. showImgSize
  51. showImgToolbar
  52. showImgResize
  53. @statuschange="onStatusChange"
  54. :read-only="readOnly"
  55. @ready="onEditorReady"
  56. ></editor>
  57. </view>
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. import webUrl from '@/common/config.js';
  63. import qiniuUploader from '@/common/qiniuUploader.js';
  64. // import { getEnToken } from '@/access/common.js';
  65. export default {
  66. data() {
  67. return {
  68. qn_token: '',
  69. readOnly: false,
  70. formats: {},
  71. goods_detail: '',
  72. uploadtoken: '',
  73. goodsDescription: ''
  74. };
  75. },
  76. props: {
  77. html: {
  78. type: String,
  79. default: ''
  80. }
  81. },
  82. watch: {
  83. html(val) {
  84. if (val) {
  85. this.editorCtx.setContents({
  86. html: this.html
  87. });
  88. }
  89. }
  90. },
  91. created(options) {
  92. this.getQiniuToken();
  93. },
  94. methods: {
  95. getQiniuToken() {
  96. this.$u.api
  97. .uploadToken({
  98. bucket: webUrl.QINIU_KEY,
  99. key: 0
  100. })
  101. .then(res => {
  102. this.qn_token = res.data;
  103. });
  104. },
  105. editInput(e) {
  106. this.editorCtx.getContents({
  107. success:(res)=>{
  108. this.$emit('editInput', res.html);
  109. }
  110. })
  111. },
  112. readOnlyChange() {
  113. this.readOnly = !this.readOnly;
  114. },
  115. onEditorReady() {
  116. uni.createSelectorQuery()
  117. .in(this)
  118. .select('#editor')
  119. .context(res => {
  120. if (!res) return;
  121. this.editorCtx = res.context;
  122. if (this.html) {
  123. this.editorCtx.setContents({
  124. html: this.html,
  125. success: () => {
  126. uni.hideKeyboard();
  127. this.$refs.dialogInput.quill.blur();
  128. }
  129. });
  130. }
  131. })
  132. .exec();
  133. },
  134. undo() {
  135. this.editorCtx.undo();
  136. },
  137. redo() {
  138. this.editorCtx.redo();
  139. },
  140. format(e) {
  141. let { name, value } = e.target.dataset;
  142. if (!name) return;
  143. // console.log('format', name, value)
  144. this.editorCtx.format(name, value);
  145. },
  146. onStatusChange(e) {
  147. const formats = e.detail;
  148. this.formats = formats;
  149. },
  150. insertDivider() {
  151. this.editorCtx.insertDivider({
  152. success: function() {
  153. console.log('insert divider success');
  154. }
  155. });
  156. },
  157. clear() {
  158. this.editorCtx.clear({
  159. success: function(res) {
  160. console.log('clear success');
  161. }
  162. });
  163. },
  164. removeFormat() {
  165. this.editorCtx.removeFormat();
  166. },
  167. insertDate() {
  168. const date = new Date();
  169. const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`;
  170. this.editorCtx.insertText({
  171. text: formatDate
  172. });
  173. },
  174. // 上传图片
  175. insertImage() {
  176. uni.chooseImage({
  177. count: 9,
  178. success: async res => {
  179. res.tempFilePaths.forEach(imgPath => {
  180. this.upImg(imgPath);
  181. });
  182. }
  183. });
  184. },
  185. async upImg(imgPath) {
  186. // const imgPath = res.tempFilePaths[0]; //选择图片的路径
  187. const imgName = imgPath.split('/')[imgPath.split('/').length - 1];
  188. const key = `${this.$store.state.enToken}/${imgName}`; //图片和企业token拼接 为自定义文件名
  189. let domain = webUrl.QN_UP; //文件上传地址
  190. let token = this.qn_token; //token为七牛云的token一般由后台接口提供
  191. let filePath = imgPath; //为需要上传的文件
  192. uni.showLoading();
  193. qiniuUploader.upload(
  194. filePath,
  195. res => {
  196. //图片上传完成后返回值
  197. // 压缩图片
  198. uni.hideLoading();
  199. const uploadPicUrl = `${webUrl.QINIU_URL}/${res.key}?imageView2/2/w/750/h/750`;
  200. // console.log(uploadPicUrl);
  201. this.editorCtx.insertImage({
  202. src: uploadPicUrl,
  203. width:'100%',
  204. alt: '图像',
  205. success: function() {
  206. console.log('图像上传成功');
  207. }
  208. });
  209. // this.$emit('uploadSuccess', imgData);
  210. },
  211. error => {
  212. uni.hideLoading();
  213. // resolve(error)
  214. },
  215. {
  216. region: 'SCN', // (必须填写正确)ECN, SCN, NCN, NA, ASG,分别对应七牛的:华东,华南,华北,北美,新加坡 5 个区域
  217. domain: domain, // // bucket 域名,下载资源时用到。如果设置,会在 success callback 的 res 参数加上可以直接使用的 ImageURL 字 段。否则需要自己拼接
  218. key: key, // [非必须]自定义文件 key。如果不设置,默认为使用微信小程序 API 的临时文件名
  219. // 以下方法三选一即可,优先级为:uptoken > uptokenURL > uptokenFunc
  220. uptoken: token // 由其他程序生成七牛 uptoken
  221. },
  222. res => {
  223. uni.hideLoading();
  224. //上传进度
  225. if (res.progress === 100) {
  226. // resolve(keys);
  227. }
  228. }
  229. );
  230. }
  231. }
  232. };
  233. </script>
  234. <style>
  235. @import './editor-icon.css';
  236. .wrapper {
  237. padding: 5px;
  238. }
  239. .iconfont {
  240. display: inline-block;
  241. padding: 22rpx;
  242. /* width: 24px; */
  243. /* height: 24px; */
  244. cursor: pointer;
  245. font-size: 20px;
  246. }
  247. .toolbar {
  248. box-sizing: border-box;
  249. border-bottom: 0;
  250. font-family: 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif;
  251. }
  252. .ql-container {
  253. box-sizing: border-box;
  254. padding: 12px 15px;
  255. width: 100%;
  256. min-height: 30vh;
  257. height: auto;
  258. background: #fff;
  259. font-size: 16px;
  260. line-height: 1.5;
  261. border: 1px solid #eee;
  262. }
  263. .ql-active {
  264. color: #06c;
  265. }
  266. </style>