admin-fe/src/components/Tinymce/index.vue

211 lines
5.5 KiB
Vue
Raw Normal View History

2017-04-18 07:09:13 +00:00
<template>
<div :class="{fullscreen:fullscreen}" class="tinymce-container editor-container">
<textarea :id="tinymceId" class="tinymce-textarea"/>
2017-08-30 06:41:10 +00:00
<div class="editor-custom-btn-container">
<editorImage color="#1890ff" class="editor-upload-btn" @successCBK="imageSuccessCBK"/>
2018-02-01 03:07:07 +00:00
</div>
2017-04-18 07:09:13 +00:00
</div>
</template>
<script>
2017-08-30 06:41:10 +00:00
import editorImage from './components/editorImage'
2018-02-01 03:07:07 +00:00
import plugins from './plugins'
import toolbar from './toolbar'
2017-08-30 06:41:10 +00:00
2017-08-22 07:43:34 +00:00
export default {
name: 'Tinymce',
2017-08-30 06:41:10 +00:00
components: { editorImage },
2017-08-22 07:43:34 +00:00
props: {
id: {
type: String,
2018-09-17 10:37:12 +00:00
default: function() {
return 'vue-tinymce-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '')
}
2017-08-22 07:43:34 +00:00
},
value: {
type: String,
default: ''
},
toolbar: {
type: Array,
required: false,
default() {
2018-02-01 03:07:07 +00:00
return []
2017-08-22 07:43:34 +00:00
}
},
menubar: {
type: String,
2018-02-01 03:07:07 +00:00
default: 'file edit insert view format table'
2017-08-22 07:43:34 +00:00
},
height: {
type: Number,
required: false,
default: 360
}
},
2019-02-22 18:06:48 +00:00
data: function() {
2017-09-01 02:04:08 +00:00
return {
hasChange: false,
hasInit: false,
tinymceId: this.id,
2018-08-22 06:42:12 +00:00
fullscreen: false,
languageTypeList: {
'en': 'en',
'zh': 'zh_CN'
}
}
},
computed: {
language() {
return this.languageTypeList[this.$store.getters.language]
2017-09-01 02:04:08 +00:00
}
},
2017-08-22 07:43:34 +00:00
watch: {
value(val) {
if (!this.hasChange && this.hasInit) {
this.$nextTick(() =>
window.tinymce.get(this.tinymceId).setContent(val || ''))
2017-08-22 07:43:34 +00:00
}
2018-08-22 06:42:12 +00:00
},
language() {
this.destroyTinymce()
this.$nextTick(() => this.initTinymce())
2017-08-22 07:43:34 +00:00
}
},
mounted() {
2017-10-31 03:06:41 +00:00
this.initTinymce()
},
activated() {
this.initTinymce()
},
deactivated() {
this.destroyTinymce()
},
destroyed() {
this.destroyTinymce()
},
2017-10-31 03:06:41 +00:00
methods: {
initTinymce() {
const _this = this
window.tinymce.init({
2018-08-22 06:42:12 +00:00
language: this.language,
2017-10-31 03:06:41 +00:00
selector: `#${this.tinymceId}`,
height: this.height,
body_class: 'panel-body ',
object_resizing: false,
2018-02-01 03:07:07 +00:00
toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,
2017-10-31 03:06:41 +00:00
menubar: this.menubar,
2018-02-01 03:07:07 +00:00
plugins: plugins,
2017-10-31 03:06:41 +00:00
end_container_on_empty_block: true,
powerpaste_word_import: 'clean',
code_dialog_height: 450,
code_dialog_width: 1000,
advlist_bullet_styles: 'square',
advlist_number_styles: 'default',
2018-02-01 03:07:07 +00:00
imagetools_cors_hosts: ['www.tinymce.com', 'codepen.io'],
2017-10-31 03:06:41 +00:00
default_link_target: '_blank',
link_title: false,
nonbreaking_force_tab: true, // inserting nonbreaking space &nbsp; need Nonbreaking Space Plugin
2017-10-31 03:06:41 +00:00
init_instance_callback: editor => {
if (_this.value) {
editor.setContent(_this.value)
}
_this.hasInit = true
2018-03-07 01:57:45 +00:00
editor.on('NodeChange Change KeyUp SetContent', () => {
2017-10-31 03:06:41 +00:00
this.hasChange = true
2018-03-08 08:51:09 +00:00
this.$emit('input', editor.getContent())
2017-10-31 03:06:41 +00:00
})
},
setup(editor) {
editor.on('FullscreenStateChanged', (e) => {
_this.fullscreen = e.state
})
2017-08-22 07:43:34 +00:00
}
2017-10-31 08:04:30 +00:00
// 整合七牛上传
// images_dataimg_filter(img) {
// setTimeout(() => {
// const $image = $(img);
// $image.removeAttr('width');
// $image.removeAttr('height');
// if ($image[0].height && $image[0].width) {
// $image.attr('data-wscntype', 'image');
// $image.attr('data-wscnh', $image[0].height);
// $image.attr('data-wscnw', $image[0].width);
// $image.addClass('wscnph');
// }
// }, 0);
// return img
// },
// images_upload_handler(blobInfo, success, failure, progress) {
// progress(0);
// const token = _this.$store.getters.token;
// getToken(token).then(response => {
// const url = response.data.qiniu_url;
// const formData = new FormData();
// formData.append('token', response.data.qiniu_token);
// formData.append('key', response.data.qiniu_key);
// formData.append('file', blobInfo.blob(), url);
// upload(formData).then(() => {
// success(url);
// progress(100);
// })
// }).catch(err => {
// failure('出现未知问题,刷新页面,或者联系程序员')
// console.log(err);
// });
// },
2017-10-31 03:06:41 +00:00
})
},
destroyTinymce() {
const tinymce = window.tinymce.get(this.tinymceId)
if (this.fullscreen) {
tinymce.execCommand('mceFullScreen')
}
if (tinymce) {
tinymce.destroy()
2017-11-17 09:57:17 +00:00
}
2017-10-31 03:06:41 +00:00
},
setContent(value) {
window.tinymce.get(this.tinymceId).setContent(value)
},
getContent() {
window.tinymce.get(this.tinymceId).getContent()
},
2017-08-30 06:41:10 +00:00
imageSuccessCBK(arr) {
const _this = this
arr.forEach(v => {
2017-09-01 02:04:08 +00:00
window.tinymce.get(_this.tinymceId).insertContent(`<img class="wscnph" src="${v.url}" >`)
2017-08-30 06:41:10 +00:00
})
}
2017-07-06 09:56:17 +00:00
}
2017-08-22 07:43:34 +00:00
}
2017-04-18 07:09:13 +00:00
</script>
<style scoped>
.tinymce-container {
2018-03-19 09:19:13 +00:00
position: relative;
2018-12-20 07:01:22 +00:00
line-height: normal;
2018-03-19 09:19:13 +00:00
}
.tinymce-container>>>.mce-fullscreen {
z-index: 10000;
2017-04-18 07:09:13 +00:00
}
.tinymce-textarea {
visibility: hidden;
z-index: -1;
}
2017-08-30 06:41:10 +00:00
.editor-custom-btn-container {
position: absolute;
2018-02-01 03:07:07 +00:00
right: 4px;
top: 4px;
2017-08-30 06:41:10 +00:00
/*z-index: 2005;*/
}
.fullscreen .editor-custom-btn-container {
z-index: 10000;
position: fixed;
}
2017-08-30 06:41:10 +00:00
.editor-upload-btn {
display: inline-block;
}
2017-04-18 07:09:13 +00:00
</style>