Skip to content

Instantly share code, notes, and snippets.

@hizhengfu
Last active December 1, 2017 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hizhengfu/3186a978ba9a003a09119f95bcbbc512 to your computer and use it in GitHub Desktop.
Save hizhengfu/3186a978ba9a003a09119f95bcbbc512 to your computer and use it in GitHub Desktop.
vuejs整合tinymce本组件除开语言文件完全基于npm,不需要指定id,支持v-model.
本组件除开语言文件完全基于npm,不需要指定id,支持v-model.
1.安装tinymce npm install tinymce
2.下载语言文件到组件目录
3.在webpack文件中增加如下配置,主要langs路径要与项目一致
```js
{
from: 'node_modules/tinymce/plugins',
to: 'plugins'
},
{
from: 'node_modules/tinymce/skins',
to: 'skins'
},
{
from: 'node_modules/tinymce/themes',
to: 'themes'
},
{
from: 'src/components/form/tinymce/langs',
to: 'langs'
}
```
4.使用下面定义的组件
```vue
<template>
<div v-loading="loading" text="加载组件中...">
<textarea class='tinymce-textarea' ref="tinymceEditer"></textarea>
<div v-if="preview" v-html="value"></div>
</div>
</template>
<script>
import tinymce from 'tinymce';
/**
*
* @param menubar 菜单栏 具体用法参加tinymce
* @param plugins 插件
* @param toolbar 工具栏
* */
export default {
name: 'tinymce-editor',
props: {
value: {},
preview: {type: Boolean, default: false},
menubar: {default: 'edit insert view format table tools'},
height: {type: Number, default: 400},
language: {
type: String,
default: 'zh_CN.GB2312'
},
plugins: {
type: Array,
default() {
return [
'advlist autolink lists link image charmap print preview hr anchor pagebreak imagetools',
'searchreplace visualblocks visualchars code fullpage',
'insertdatetime media nonbreaking save table contextmenu directionality',
'emoticons paste textcolor colorpicker textpattern imagetools codesample'
]
}
},
toolbar: {
default() {
return 'newnote print preview | undo redo | insert | styleselect | forecolor backcolor bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image emoticons media codesample'
}
}
},
data() {
return {
loading: true,
currentValue: '',
editor: null
};
},
methods: {
init() {
let self = this;
tinymce.init({
target: this.$refs.tinymceEditer,
branding: false,
// theme: 'inlite',
elementpath: false,
height: this.height,
language: this.language,
menubar: this.menubar,
plugins: this.plugins,
toolbar: this.toolbar,
autosave_interval: '20s',
image_advtab: true,
table_default_styles: {
width: '100%',
borderCollapse: 'collapse'
},
//文件浏览
/*file_browser_callback(field_name, url, type, win) {
},*/
setup(editor) {
this.editor = editor;
editor.on('init', (e) => {
self.loading = false;
let content = self.value;
if (content) {
editor.setContent(content);
}
});
editor.on('Change', (e) => {
self.currentValue = editor.getContent({format: 'raw'})
self.$emit('input', self.currentValue);
});
}
});
}
},
mounted() {
this.init();
},
watch: {
value(v) {
if (this.editor && this.currentValue != v) {
this.editor.setContent(v);
}
}
},
destroyed() {
if (this.editor)
this.editor.destroy();
}
};
</script>
```
6.结束 基本所有功能可用
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment