1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
| <template> <div> <vue-ueditor-wrap ref="editor" v-model="editHtml" @ready="ready" :config="config" :destroy="true" @beforeInit="addCustomButtom" style="line-height: 1"> </vue-ueditor-wrap> </div> </template>
<script> import VueUeditorWrap from 'vue-ueditor-wrap' import { mapGetters } from 'vuex'
const URL = process.env.NODE_ENV === 'production' ? './ueditor/' : '/ueditor/'
export default { name: 'UE', components: { VueUeditorWrap }, props: { value: {} }, data () { return { editor: null, editHtml: '', // 配置 config: { // 编辑器不自动被内容撑高 autoHeightEnabled: false, // 初始容器高度 initialFrameHeight: 115, // 初始容器宽度 initialFrameWidth: '100%', // 上传文件接口(这个地址是我为了方便各位体验文件上传功能搭建的临时接口,请勿在生产环境使用!!!) serverUrl: 'http: UEDITOR_HOME_URL: URL, enableAutoSave: false, elementPathEnabled: false, wordCount: false, toolbars: [ ['source', 'undo', 'redo', 'bold', 'italic', 'underline', 'fontborder', 'strikethrough', 'removeformat', 'forecolor', 'backcolor', 'selectall', 'link' ] ], langPath: URL + 'lang/', imageScaleEnabled: false, listDefaultPaddingLeft: '10' } } }, computed: { ...mapGetters([ 'language' ]), lang () { return localStorage.getItem('localeLanguage') || 'en-US' } }, watch: { value: { handler (val) { this.editHtml = val }, deep: true, immediate: true }, editHtml: { handler (val) { this.$emit('input', val) }, deep: true } }, created () { this.config.lang = this.lang }, methods: { ready (editorInstance) { this.editor = editorInstance }, addCustomButtom (editorId) { const _this = this this.config.lang = this.lang window.UE.registerUI('test-button', function (editor, uiName) { editor.registerCommand(uiName, { execCommand: function () { _this.axiosRequestImgUp(editor) } })
var btn = new window.UE.ui.Button({ name: uiName, title: _this.$t('uploadImg'), cssRules: `background-image: url('./ueditor/themes/default/images/icons.png') !important;background-position: -380px 0px;`, onclick: function () { editor.execCommand(uiName) } })
editor.addListener('selectionchange', function () { var state = editor.queryCommandState(uiName) if (state === -1) { btn.setDisabled(true) btn.setChecked(false) } else { btn.setDisabled(false) btn.setChecked(state) } }) return btn }, -1 , editorId ) }, axiosRequestImgUp (editor) { const _this = this var input = document.createElement('input') input.type = 'file' input.style.display = 'none' input.accept = '.png, .jpg, .bmp' document.body.appendChild(input) input.click() input.addEventListener('change', (e) => { const file = e.target.files[0] const size = file.size const isLt2M = size / 1024 / 1024 < 2 const isZero = size > 0 const fileType = file.type if (!isZero || !isLt2M || fileType.indexOf('image') === -1) { _this.$yrtoast(this.$t('fileaccept'), 2000, 'error') return } const formData = new FormData() formData.append('file', file) formData.append('type', 2) _this.$store.dispatch('upload', formData).then(res => { if (res.code === '10000') { const imgUrl = res.data.fullUrl editor.execCommand('inserthtml', `<img src="${imgUrl}" title="${file.name}" />`) } else { _this.$yrtoast(_this.$t('errorCode')[res.code], 1000, 'error') } }).catch(() => { document.body.removeChild(input) return false }) }) } } } </script>
<style lang="less" scoped> div { width: 100%; } </style>
|