jin-edit.vue 8.0 KB

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