jin-edit.vue 8.4 KB

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