jin-edit.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <template>
  2. <view class="container" :style="{height: height}">
  3. <editor class="ql-container" :placeholder="placeholder" :show-img-size="true" :show-img-toolbar="true"
  4. :show-img-resize="true" @ready="onEditorReady" id="editor" @statuschange="statuschange" @focus="editFocus"
  5. @blur="editBlur" ref="editot"></editor>
  6. <!-- 操作工具 -->
  7. <view class="tool-view">
  8. <!-- 文字相关操作 -->
  9. <view class="font-more" :style="{ height: showMoreTool ? '100rpx' : 0 }">
  10. <jinIcon class="single" type="&#xe6e7;" font-size="44rpx" title="加粗" @click="setBold"
  11. :color="showBold ? activeColor : '#666666'"></jinIcon>
  12. <jinIcon class="single" type="&#xe6fe;" font-size="44rpx" title="斜体" @click="setItalic"
  13. :color="showItalic ? activeColor : '#666666'"></jinIcon>
  14. <jinIcon class="single" type="&#xe6f8;" font-size="44rpx" title="分割线" @click="setIns"
  15. :color="showIns ? activeColor : '#666666'"></jinIcon>
  16. <jinIcon class="single" type="&#xe6e3;" font-size="44rpx" title="标题" @click="setHeader"
  17. :color="showHeader ? activeColor : '#666666'"></jinIcon>
  18. <jinIcon class="single" type="&#xe6f1;" font-size="44rpx" title="居中" @click="setCenter"
  19. :color="showCenter ? activeColor : '#666666'"></jinIcon>
  20. <jinIcon class="single" type="&#xe6ed;" font-size="44rpx" title="居右" @click="setRight"
  21. :color="showRight ? activeColor : '#666666'"></jinIcon>
  22. </view>
  23. <view class="setting-layer-mask" v-if="showSettingLayer" @click="showSetting"></view>
  24. <view class="setting-layer" v-if="showSettingLayer">
  25. <view class="single" @click="release(true)">
  26. <jinIcon class="icon" type="&#xe639;"></jinIcon>
  27. <view>公开发布</view>
  28. </view>
  29. <view class="single" @click="release(false)">
  30. <jinIcon class="icon" type="&#xe655;"></jinIcon>
  31. <view>暂时保存</view>
  32. </view>
  33. </view>
  34. <view class="tool">
  35. <jinIcon class="single" type="&#xe6f3;" font-size="44rpx" title="插入图片" @click="insertImage"></jinIcon>
  36. <jinIcon class="single" type="&#xe6f9;" font-size="44rpx" title="修改文字样式" @click="showMore"
  37. :color="showMoreTool ? activeColor : '#666666'"></jinIcon>
  38. <jinIcon class="single" type="&#xe6eb;" font-size="44rpx" title="分割线" @click="insertDivider"></jinIcon>
  39. <jinIcon class="single" type="&#xe6e8;" font-size="44rpx" title="撤销" @click="undo"></jinIcon>
  40. <jinIcon class="single" type="&#xe705;" font-size="44rpx" title="重做" @click="redo"></jinIcon>
  41. <jinIcon class="single" type="&#xeb8a;" font-size="44rpx" title="设置" @click="release(true)">
  42. </jinIcon>
  43. </view>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import {
  49. mapState,
  50. mapMutations
  51. } from 'vuex';
  52. import jinIcon from './jin-icons.vue';
  53. export default {
  54. props: {
  55. // 点击图片时显示图片大小控件
  56. showImgSize: {
  57. type: Boolean,
  58. default: false
  59. },
  60. // 点击图片时显示工具栏控件
  61. showImgToolbar: {
  62. type: Boolean,
  63. default: false
  64. },
  65. // 点击图片时显示修改尺寸控件
  66. showImgResize: {
  67. type: Boolean,
  68. default: false
  69. },
  70. // 占位符
  71. placeholder: {
  72. type: String,
  73. default: '开始输入...'
  74. },
  75. // 图片上传的地址
  76. uploadFileUrl: {
  77. type: String,
  78. default: '#'
  79. },
  80. // 上传文件时的name
  81. fileKeyName: {
  82. type: String,
  83. default: 'file'
  84. },
  85. // 上传图片时,http请求的header
  86. header: {
  87. type: Object
  88. },
  89. // 初始化html
  90. html: {
  91. type: String
  92. },
  93. // 整个控件的高度
  94. height: {
  95. type: String,
  96. default: '100vh'
  97. }
  98. },
  99. computed: {
  100. ...mapState(['baseURL'])
  101. },
  102. data() {
  103. return {
  104. showMoreTool: false,
  105. showBold: false,
  106. showItalic: false,
  107. showIns: false,
  108. showHeader: false,
  109. showCenter: false,
  110. showRight: false,
  111. showSettingLayer: false,
  112. activeColor: '#F56C6C'
  113. };
  114. },
  115. components: {
  116. jinIcon
  117. },
  118. mounted() {},
  119. methods: {
  120. onEditorReady(e) {
  121. uni.createSelectorQuery()
  122. .in(this)
  123. .select('.ql-container')
  124. .fields({
  125. size: true,
  126. context: true
  127. }, res => {
  128. this.editorCtx = res.context;
  129. this.editorCtx.setContents({
  130. html: this.html
  131. })
  132. })
  133. .exec();
  134. },
  135. undo() {
  136. this.editorCtx.undo();
  137. },
  138. // 插入图片
  139. insertImage() {
  140. uni.chooseImage({
  141. count: 9, //默认9
  142. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  143. sourceType: ['album', 'camera'], //从相册选择
  144. success: async (res) => {
  145. var tempFilePaths = res.tempFilePaths;
  146. uni.showLoading({
  147. title: '正在上传中...'
  148. })
  149. for (let temp of tempFilePaths) {
  150. console.log(88, temp);
  151. // 图片上传服务器
  152. await uni.uploadFile({
  153. url: this.baseURL + this.uploadFileUrl,
  154. filePath: temp,
  155. name: this.fileKeyName,
  156. header: this.header,
  157. success: res => {
  158. console.log(res, 'shangchuan')
  159. // 上传完成后处理
  160. let bb
  161. console.log(res, '上传完成后处理')
  162. if ("string" === typeof res.data) {
  163. bb = JSON.parse(res.data).data
  164. } else {
  165. bb = res.data.data
  166. }
  167. this.editorCtx.insertImage({
  168. src: bb.url, // 此处需要将图片地址切换成服务器返回的真实图片地址
  169. alt: '图片',
  170. success: function(e) {}
  171. });
  172. uni.hideLoading()
  173. },
  174. fail: res => {
  175. console.log(res, 'fail');
  176. }
  177. });
  178. }
  179. }
  180. });
  181. },
  182. /// 插入分割线
  183. insertDivider() {
  184. this.editorCtx.insertDivider();
  185. },
  186. redo() {
  187. this.editorCtx.redo();
  188. },
  189. showMore() {
  190. this.showMoreTool = !this.showMoreTool;
  191. this.editorCtx.setContents()
  192. },
  193. setBold() {
  194. this.showBold = !this.showBold;
  195. this.editorCtx.format('bold');
  196. },
  197. setItalic() {
  198. this.showItalic = !this.showItalic;
  199. this.editorCtx.format('italic');
  200. },
  201. checkStatus(name, detail, obj) {
  202. if (detail.hasOwnProperty(name)) {
  203. this[obj] = true;
  204. } else {
  205. this[obj] = false;
  206. }
  207. },
  208. statuschange(e) {
  209. var detail = e.detail;
  210. this.checkStatus('bold', detail, 'showBold');
  211. this.checkStatus('italic', detail, 'showItalic');
  212. this.checkStatus('ins', detail, 'showIns');
  213. this.checkStatus('header', detail, 'showHeader');
  214. if (detail.hasOwnProperty('align')) {
  215. if (detail.align == 'center') {
  216. this.showCenter = true;
  217. this.showRight = false;
  218. } else if (detail.align == 'right') {
  219. this.showCenter = false;
  220. this.showRight = true;
  221. } else {
  222. this.showCenter = false;
  223. this.showRight = false;
  224. }
  225. } else {
  226. this.showCenter = false;
  227. this.showRight = false;
  228. }
  229. },
  230. setIns() {
  231. this.showIns = !this.showIns;
  232. this.editorCtx.format('ins');
  233. },
  234. setHeader() {
  235. this.showHeader = !this.showHeader;
  236. this.editorCtx.format('header', this.showHeader ? 'H2' : false);
  237. },
  238. setCenter() {
  239. this.showCenter = !this.showCenter;
  240. this.editorCtx.format('align', this.showCenter ? 'center' : false);
  241. },
  242. setRight() {
  243. this.showRight = !this.showRight;
  244. this.editorCtx.format('align', this.showRight ? 'right' : false);
  245. },
  246. showSetting() {
  247. this.showSettingLayer = !this.showSettingLayer;
  248. },
  249. async editFocus(e) {},
  250. editBlur(e) {},
  251. release(isPublic) {
  252. // this.showSettingLayer = false;
  253. this.editorCtx.getContents({
  254. success: res => {
  255. Object.assign(res, {
  256. isPublic: isPublic
  257. })
  258. this.$emit('editOk', res);
  259. }
  260. })
  261. },
  262. }
  263. };
  264. </script>
  265. <style scoped>
  266. .container {
  267. box-sizing: border-box;
  268. display: flex;
  269. flex-direction: column;
  270. height: 100%;
  271. box-sizing: border-box;
  272. /* padding-top: 30rpx; */
  273. }
  274. .ql-container {
  275. line-height: 150%;
  276. font-size: 34rpx;
  277. width: 100%;
  278. background: #fff;
  279. width: calc(100% - 60rpx);
  280. margin: 0 auto;
  281. flex: 1;
  282. box-sizing: border-box;
  283. margin-top: 30rpx;
  284. /* padding-bottom: 5rpx; */
  285. }
  286. .tool-view {
  287. width: 100%;
  288. background: #eee;
  289. /* margin-top: 20px; */
  290. }
  291. .tool {
  292. height: 100rpx;
  293. display: flex;
  294. align-items: center;
  295. justify-content: space-around;
  296. width: 100%;
  297. }
  298. .font-more {
  299. display: flex;
  300. align-items: center;
  301. justify-content: space-around;
  302. width: 100%;
  303. background: rgb(235, 235, 235);
  304. overflow: hidden;
  305. transition: all 0.15s;
  306. }
  307. .setting-layer {
  308. position: absolute;
  309. bottom: 100rpx;
  310. background: #fff;
  311. width: 250rpx;
  312. right: 20rpx;
  313. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  314. border-radius: 8rpx;
  315. }
  316. .setting-layer .single {
  317. height: 80rpx;
  318. font-size: 32rpx;
  319. padding: 0 30rpx;
  320. display: flex;
  321. align-items: center;
  322. line-height: 80rpx;
  323. flex-direction: row;
  324. color: #666;
  325. }
  326. .setting-layer .single .icon {
  327. margin-right: 20rpx;
  328. }
  329. .setting-layer-mask {
  330. position: fixed;
  331. left: 0;
  332. top: 0;
  333. width: 100vw;
  334. height: 100vh;
  335. background: transparent;
  336. }
  337. </style>