jin-edit.vue 8.5 KB

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