jin-edit.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template>
  2. <view class="container" :style="{
  3. paddingBottom: showMoreTool ? '220rpx' : '120rpx'
  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="showSetting"></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="release(true)"></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. file_type: 'img',
  85. type: 'news'
  86. },
  87. // 初始化html
  88. html: {
  89. type: String
  90. }
  91. },
  92. computed: {
  93. },
  94. data() {
  95. return {
  96. showMoreTool: false,
  97. showBold: false,
  98. showItalic: false,
  99. showIns: false,
  100. showHeader: false,
  101. showCenter: false,
  102. showRight: false,
  103. showSettingLayer: false,
  104. activeColor: '#F56C6C'
  105. };
  106. },
  107. components: {
  108. jinIcon
  109. },
  110. created() {
  111. },
  112. methods: {
  113. onEditorReady(e) {
  114. uni.createSelectorQuery()
  115. .in(this)
  116. .select('.ql-container')
  117. .fields({
  118. size: true,
  119. context: true
  120. }, res => {
  121. this.editorCtx = res.context;
  122. this.editorCtx.setContents({
  123. html: this.html
  124. })
  125. })
  126. .exec();
  127. },
  128. undo() {
  129. this.editorCtx.undo();
  130. },
  131. // 插入图片
  132. insertImage() {
  133. uni.chooseImage({
  134. count: 9, //默认9
  135. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  136. sourceType: ['album', 'camera'], //从相册选择
  137. success: async (res) => {
  138. var tempFilePaths = res.tempFilePaths;
  139. uni.showLoading({
  140. title: '正在上传中...'
  141. })
  142. for (let temp of tempFilePaths) {
  143. // 图片上传服务器
  144. await uni.uploadFile({
  145. url: this.uploadFileUrl,
  146. filePath: temp,
  147. name: this.fileKeyName,
  148. header: {
  149. },
  150. formData:{
  151. file_type: 'img',
  152. type: 'news'
  153. },
  154. success: res => {
  155. console.log(temp,'pppppp蹄片上传')
  156. let url=JSON.parse(res.data).data.url
  157. // temp=JSON.parse(res.data).data.url
  158. // 上传完成后处理
  159. this.editorCtx.insertImage({
  160. src: url, // 此处需要将图片地址切换成服务器返回的真实图片地址
  161. alt: '图片',
  162. success: function(e) {}
  163. });
  164. uni.hideLoading()
  165. },
  166. });
  167. }
  168. }
  169. });
  170. },
  171. insertDivider() {
  172. this.editorCtx.insertDivider();
  173. },
  174. redo() {
  175. this.editorCtx.redo();
  176. },
  177. showMore() {
  178. this.showMoreTool = !this.showMoreTool;
  179. this.editorCtx.setContents()
  180. },
  181. setBold() {
  182. this.showBold = !this.showBold;
  183. this.editorCtx.format('bold');
  184. },
  185. setItalic() {
  186. this.showItalic = !this.showItalic;
  187. this.editorCtx.format('italic');
  188. },
  189. checkStatus(name, detail, obj) {
  190. if (detail.hasOwnProperty(name)) {
  191. this[obj] = true;
  192. } else {
  193. this[obj] = false;
  194. }
  195. },
  196. statuschange(e) {
  197. var detail = e.detail;
  198. this.checkStatus('bold', detail, 'showBold');
  199. this.checkStatus('italic', detail, 'showItalic');
  200. this.checkStatus('ins', detail, 'showIns');
  201. this.checkStatus('header', detail, 'showHeader');
  202. if (detail.hasOwnProperty('align')) {
  203. if (detail.align == 'center') {
  204. this.showCenter = true;
  205. this.showRight = false;
  206. } else if (detail.align == 'right') {
  207. this.showCenter = false;
  208. this.showRight = true;
  209. } else {
  210. this.showCenter = false;
  211. this.showRight = false;
  212. }
  213. } else {
  214. this.showCenter = false;
  215. this.showRight = false;
  216. }
  217. },
  218. setIns() {
  219. this.showIns = !this.showIns;
  220. this.editorCtx.format('ins');
  221. },
  222. setHeader() {
  223. this.showHeader = !this.showHeader;
  224. this.editorCtx.format('header', this.showHeader ? 'H2' : false);
  225. },
  226. setCenter() {
  227. this.showCenter = !this.showCenter;
  228. this.editorCtx.format('align', this.showCenter ? 'center' : false);
  229. },
  230. setRight() {
  231. this.showRight = !this.showRight;
  232. this.editorCtx.format('align', this.showRight ? 'right' : false);
  233. },
  234. showSetting() {
  235. this.showSettingLayer = !this.showSettingLayer;
  236. },
  237. async editFocus() {
  238. },
  239. editBlur() {
  240. },
  241. release(isPublic) {
  242. this.showSettingLayer = false;
  243. this.editorCtx.getContents({
  244. success: res => {
  245. Object.assign(res, {
  246. isPublic: isPublic
  247. })
  248. this.$emit('editOk', res);
  249. }
  250. })
  251. },
  252. }
  253. };
  254. </script>
  255. <style scoped>
  256. .container {
  257. padding: 30rpx 0;
  258. box-sizing: border-box;
  259. padding-bottom: 120rpx;
  260. }
  261. .ql-container {
  262. line-height: 160%;
  263. font-size: 34rpx;
  264. width: calc(100% - 60rpx);
  265. height: auto;
  266. margin: 0 auto;
  267. }
  268. .tool-view {
  269. width: 100vw;
  270. position: fixed;
  271. bottom: 0;
  272. left: 0;
  273. }
  274. .tool {
  275. height: 100rpx;
  276. display: flex;
  277. align-items: center;
  278. justify-content: space-around;
  279. width: 100%;
  280. background: #eee;
  281. }
  282. .font-more {
  283. position: absolute;
  284. left: 0;
  285. bottom: 100rpx;
  286. display: flex;
  287. align-items: center;
  288. justify-content: space-around;
  289. width: 100%;
  290. background: rgb(235, 235, 235);
  291. overflow: hidden;
  292. transition: all 0.15s;
  293. }
  294. .setting-layer {
  295. position: absolute;
  296. bottom: 100rpx;
  297. background: #fff;
  298. width: 250rpx;
  299. right: 20rpx;
  300. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  301. border-radius: 8rpx;
  302. }
  303. .setting-layer .single {
  304. height: 80rpx;
  305. font-size: 32rpx;
  306. padding: 0 30rpx;
  307. display: flex;
  308. align-items: center;
  309. line-height: 80rpx;
  310. flex-direction: row;
  311. color: #666;
  312. }
  313. .setting-layer .single .icon {
  314. margin-right: 20rpx;
  315. }
  316. .setting-layer-mask {
  317. position: fixed;
  318. left: 0;
  319. top: 0;
  320. width: 100vw;
  321. height: 100vh;
  322. background: transparent;
  323. }
  324. </style>