jin-edit.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. @input="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="confirmContent"></jinIcon><!-- @click="showSetting" -->
  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. const req = require('../../../utils/request.js');
  53. import jinIcon from './jin-icons.vue';
  54. export default {
  55. data() {
  56. return {
  57. showMoreTool: false,
  58. showBold: false,
  59. showItalic: false,
  60. showIns: false,
  61. showHeader: false,
  62. showCenter: false,
  63. showRight: false,
  64. showSettingLayer: false,
  65. activeColor: '#F56C6C',
  66. };
  67. },
  68. components: {
  69. jinIcon
  70. },
  71. props: {
  72. // 点击图片时显示图片大小控件
  73. showImgSize: {
  74. type: Boolean,
  75. default: false
  76. },
  77. // 点击图片时显示工具栏控件
  78. showImgToolbar: {
  79. type: Boolean,
  80. default: false
  81. },
  82. // 点击图片时显示修改尺寸控件
  83. showImgResize: {
  84. type: Boolean,
  85. default: false
  86. },
  87. // 占位符
  88. placeholder: {
  89. type: String,
  90. default: '开始输入...'
  91. },
  92. // 图片上传的地址
  93. uploadFileUrl: {
  94. type: String,
  95. default: '#'
  96. },
  97. // 上传文件时的name
  98. fileKeyName: {
  99. type: String,
  100. default: 'file'
  101. },
  102. // 上传图片时,http请求的header
  103. header: {
  104. type: Object
  105. },
  106. // 初始化html
  107. html: {
  108. type: String,
  109. }
  110. },
  111. watch: {
  112. },
  113. computed:{
  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. let that = this;
  139. uni.chooseImage({
  140. count: 8,
  141. sizeType: ['original', 'compressed'],
  142. sourceType: ['album', 'camera'],
  143. success: function({
  144. tempFilePaths
  145. }) {
  146. var promise = Promise.all(tempFilePaths.map(tempFilePath => {
  147. return new Promise(function(resolve, reject) {
  148. uni.showLoading({
  149. title: "正在上传中"
  150. })
  151. req.uploadFile('/api/nocheck/upload', tempFilePath, res => {
  152. that.editorCtx.insertImage({
  153. src: res.src, // 此处需要将图片地址切换成服务器返回的真实图片地址
  154. alt: '图片',
  155. success: function(e) {}
  156. });
  157. uni.hideLoading()
  158. });
  159. });
  160. }));
  161. promise.then(function(results) {
  162. console.log(results);
  163. }).catch(function(err) {
  164. console.log(err);
  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() {
  236. },
  237. editBlur(res) {
  238. this.$emit('editBlur', res);
  239. },
  240. release(isPublic) {
  241. this.showSettingLayer = false;
  242. this.editorCtx.getContents({
  243. success: res => {
  244. Object.assign(res, {
  245. isPublic: isPublic
  246. })
  247. this.$emit('editOk', res);
  248. }
  249. })
  250. },
  251. confirmContent(){
  252. this.editorCtx.getContents({
  253. success: res => {
  254. this.$emit('editOk', res);
  255. }
  256. })
  257. }
  258. }
  259. };
  260. </script>
  261. <style scoped>
  262. .ql-editor.ql-blank:before {
  263. /* 此处设置 placeholder 样式 */
  264. color: rgba(204, 204, 204, 1);
  265. font-style: normal;
  266. }
  267. .container {
  268. padding: 30rpx 0;
  269. box-sizing: border-box;
  270. padding-bottom: 120rpx;
  271. }
  272. .ql-container {
  273. line-height: 160%;
  274. font-size: 34rpx;
  275. width: calc(100% - 60rpx);
  276. height: auto;
  277. margin: 0 auto;
  278. font-style: normal !important;
  279. }
  280. #editor{
  281. font-style: normal !important;
  282. }
  283. .tool-view{
  284. width: 100vw;
  285. position: fixed;
  286. bottom: 0;
  287. left: 0;
  288. }
  289. .tool {
  290. height: 100rpx;
  291. display: flex;
  292. align-items: center;
  293. justify-content: space-around;
  294. width: 100%;
  295. background: #eee;
  296. }
  297. .font-more {
  298. position: absolute;
  299. left: 0;
  300. bottom: 100rpx;
  301. display: flex;
  302. align-items: center;
  303. justify-content: space-around;
  304. width: 100%;
  305. background: rgb(235, 235, 235);
  306. overflow: hidden;
  307. transition: all 0.15s;
  308. }
  309. .setting-layer {
  310. position: absolute;
  311. bottom: 100rpx;
  312. background: #fff;
  313. width: 250rpx;
  314. right: 20rpx;
  315. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  316. border-radius: 8rpx;
  317. }
  318. .setting-layer .single {
  319. height: 80rpx;
  320. font-size: 32rpx;
  321. padding: 0 30rpx;
  322. display: flex;
  323. align-items: center;
  324. line-height: 80rpx;
  325. flex-direction: row;
  326. color: #666;
  327. }
  328. .setting-layer .single .icon {
  329. margin-right: 20rpx;
  330. }
  331. .setting-layer-mask{
  332. position: fixed;
  333. left: 0;
  334. top: 0;
  335. width: 100vw;
  336. height: 100vh;
  337. background: transparent;
  338. }
  339. </style>