jin-edit.vue 8.3 KB

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