【wangeditor】富文本實現placeholder功能


效果

 

Template
 1 <template>
 2   <div>
 3     <div class="wangEditor">
 4       <div :id="editorId" ref="toolbar" class="wangEditor-toolbar"></div>
 5       <div ref="wangEditor" class="wangEditor-text">
 6       <!--    <span class="wangEditor-deleteIcon el-icon-circle-close" @click="reset"></span>-->
 7       </div>
 8     </div>
 9     <div :style="{position: 'absolute',left: '15px',top: '6px'}">
10       <input
11         ref="placeHolder"
12         type="text"
13         :placeholder="placeholder"
14         :style="{border: 'none', background: '#fff', outline: 'none', width: '100%', color: '#ccc', margin: '0', height: '20px'}"
15         @mousedown="hidePlace"
16       >
17     </div>
18   </div>
19 </template>
數據
import E from 'wangeditor'
 1     props: {
 2       value: {
 3         type: String,
 4         default: ''
 5       },
 6       editorId: {
 7         type: String,
 8         default: ''
 9       },
10       disableTag: {
11         type: Boolean,
12         default: false
13       },
14       placeholder: {
15         type: String,
16         default: ''
17       }
18     },
19     data () {
20       return {
21         editor: null,
22         info_: null
23       }
24     },
監聽
 1     watch: {
 2       // value為父組件的傳值,
 3       value: function (value) {
 4         const vm = this;
 5         if (value !== this.editor.txt.html()) {
 6           vm.editor.txt.html(vm.value)
 7         }
 8         this.$refs.placeHolder.style.display = this.editor.txt.text().length !== 0 ? 'none' : 'block'
 9         vm.$emit('change', value)
10       }
11     },
詳解
1. mounted的時候,父組件的數據回顯,有數據的時候要將placeholder輸入框隱藏掉
1     mounted () {
2       this.setEditor(this.editorId);
3       // this.editor.txt.html(this.value)
4       this.$refs.placeHolder.style.display = this.editor.txt.text().length !== 0 ? 'none' : 'block'
5     },
2. 定義editor的時候,對應的焦點和失焦事件要處理placeholder的顯隱
 1     methods: {
 2       // 隱藏input輸入框
 3       hidePlace () {
 4         this.$refs.placeHolder.style.display = 'none'
 5       },
 6       setEditor (id) {
 7         const vm = this;
 8         vm.editor = new E(vm.$refs.toolbar, vm.$refs.wangEditor);
 9         // 配置菜單
10         vm.editor.customConfig.menus = [
11           'link' // 插入鏈接
12         ];
13         vm.editor.customConfig.zIndex = 10;
14         // 焦點和失焦時候,工具欄的顯示與隱藏
15         const toolbar = document.getElementById(id);
16         // 焦點的時候,輸入框隱藏
17         vm.editor.customConfig.onfocus = function () {
18           vm.$refs.placeHolder.style.display = 'none'
19           setTimeout(() => {
20             // console.log('???', toolbar.nextSibling.nextSibling);
21             toolbar.style.display = 'block';
22             toolbar.nextSibling.nextSibling.style.borderColor = '#1c61fc'
23           }, 100);
24         };
25         // 失焦的時候,editor有內容則輸入框隱藏,否則出現
26         vm.editor.customConfig.onblur = function () {
27           vm.$refs.placeHolder.style.display = vm.editor.txt.text().length !== 0 ? 'none' : 'block'
28           setTimeout(() => {
29             toolbar.style.display = 'none';
30             // 將當前頁面的所有富文本處理,第一個邊框為灰色,其他的邊框都無色
31             const richDom = document.getElementsByClassName('wangEditor-text')
32             for (var i = 0; i < richDom.length; i++) {
33               if (i === 0) {
34                 richDom[i].style.borderColor = '#cdcdcd'
35               } else {
36                 richDom[i].style.borderColor = 'transparent'
37               }
38             }
39           }, 100);
40         };
41         // editor  change事件,同步內容
42         vm.editor.customConfig.onchange = (html) => {
43           vm.info_ = html; // 綁定當前組件的值
44           vm.$emit('change', vm.info_); // 將內容同步到父組件中
45         };
46         const ED = new E('.wangEditor')
47         ED.customConfig.zIndex = 100
48         // 創建富文本編輯器
49         vm.editor.create();
50         vm.editor.selection.getSelectionText();
51         vm.editor.txt.html(vm.value);
52         // 啟用與禁用
53         if (vm.disableTag) {
54           vm.editor.$textElem.attr('contenteditable', false)
55         } else {
56           vm.editor.$textElem.attr('contenteditable', true)
57         }
58         // 啟用與禁用時的文字顏色
59         const editorDom = document.getElementsByClassName('w-e-text')
60         for (var i = 0; i < editorDom.length; i++) {
61           // console.log(editorDom[i].contentEditable);
62           if (editorDom[i].contentEditable === 'false') {
63             editorDom[i].style.color = '#606266'
64           } else {
65             editorDom[i].style.color = '#606266'
66           }
67         }
68       },
69       // 清空內容
70       reset () {
71         this.editor.txt.clear();
72         this.$emit('change', '')
73       }
74     }
 
        
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM