123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- <template>
- <div :class="{ fullscreen: fullscreen }" class="tinymce-container" :style="{ width: containerWidth }">
- <textarea :id="tinymceId" class="tinymce-textarea" />
- <!-- <div class="editor-custom-btn-container"> -->
- <!-- <editorImage color="#1890ff" class="editor-upload-btn" @successCBK="imageSuccessCBK" /> -->
- <!-- </div> -->
- </div>
- </template>
- <script>
- /**
- * docs:
- * https://panjiachen.github.io/vue-element-admin-site/feature/component/rich-editor.html#tinymce
- */
- // import editorImage from './components/EditorImage';
- // import editorImage from '../../views/propertyManagement/common/upImage.vue';
- import plugins from './plugins';
- import toolbar from './toolbar';
- import load from './dynamicLoadScript';
- // why use this cdn, detail see https://github.com/PanJiaChen/tinymce-all-in-one
- const tinymceCDN = 'https://cdn.jsdelivr.net/npm/tinymce-all-in-one@4.9.3/tinymce.min.js';
- export default {
- name: 'Tinymce',
- // components: { editorImage },
- props: {
- id: {
- type: String,
- default: function () {
- return 'vue-tinymce-' + +new Date() + ((Math.random() * 1000).toFixed(0) + '');
- }
- },
- value: {
- type: String,
- default: ''
- },
- toolbar: {
- type: Array,
- required: false,
- default() {
- return [];
- }
- },
- height: {
- type: [Number, String],
- required: false,
- default: 360
- },
- width: {
- type: [Number, String],
- required: false,
- default: '100%'
- }
- },
- data() {
- return {
- times: null,
- hasChange: false,
- hasInit: false,
- tinymceId: this.id,
- fullscreen: false,
- languageTypeList: {
- en: 'en',
- zh: 'zh_CN',
- es: 'es_MX',
- ja: 'ja'
- },
- thisImg: ''
- };
- },
- computed: {
- language() {
- return this.languageTypeList[this.$store.getters.language];
- },
- containerWidth() {
- const width = this.width;
- if (/^[\d]+(\.[\d]+)?$/.test(width)) {
- // matches `100`, `'100'`
- return `${width}px`;
- }
- return width;
- }
- },
- watch: {
- value(val) {
- // console.log('tinymceValue============', val);
- if (!this.hasChange && this.hasInit) {
- this.$nextTick(() => {
- window.tinymce.get(this.tinymceId).setContent(val || '');
- });
- }
- },
- language() {
- this.destroyTinymce();
- this.$nextTick(() => this.initTinymce());
- }
- },
- mounted() {
- this.init();
- },
- activated() {
- if (window.tinymce) {
- this.initTinymce();
- }
- },
- deactivated() {
- this.destroyTinymce();
- },
- destroyed() {
- this.destroyTinymce();
- },
- methods: {
- init() {
- // dynamic load tinymce from cdn
- load(tinymceCDN, (err) => {
- if (err) {
- this.$message.error(err.message);
- return;
- }
- this.initTinymce();
- });
- },
- onPaste(event) {
- const items = (event.clipboardData || window.clipboardData).items;
- if (items[0].type.indexOf('image') !== -1) {
- const file = items[0].getAsFile();
- const formData = new FormData();
- formData.append('file', file);
- // 上传图片
- this.$http.post('/sc-community/upload/uploadFile', formData).then((res) => {
- if (res.status == 0) {
- let content = window.tinymce.get(this.tinymceId).getContent();
- let newContent = content.replace(/<img *src="data.*?\/>/g, `<img src='${res.data}' />`);
- window.tinymce.get(this.tinymceId).setContent(newContent);
- // resolve && resolve();
- } else {
- this.$message.error(res.msg);
- }
- });
- }
- // else {
- // this.$message({type:'warning',message:'只能复制纯文本或图片',duration:3000});
- // }
- return;
- new Promise((resolve) => {
- const items = (event.clipboardData || window.clipboardData).items;
- if (items[0].type.indexOf('image') !== -1) {
- const file = items[0].getAsFile();
- const formData = new FormData();
- formData.append('file', file);
- // 上传图片
- this.$http.post('/sc-community/upload/uploadFile', formData).then((res) => {
- if (res.status === 0) {
- let img = `<img class="wscnph" src="${res.data}" />`;
- window.tinymce.get(this.tinymceId).insertContent(img);
- resolve && resolve();
- } else {
- this.$message.error(res.msg);
- }
- });
- } else {
- this.$message.error('只能复制文字图片,样式无法复制请手动排版');
- }
- }).then(() => {
- let content = window.tinymce.get(this.tinymceId).getContent();
- let newContent = content.replace(/<img *src="data.*?\/>/g, '');
- window.tinymce.get(this.tinymceId).setContent(newContent);
- });
- },
- initTinymce() {
- const _this = this;
- window.tinymce.init({
- // fontsize_formats: '8pt 10pt 12pt 13pt 14pt 15pt 16pt 17pt 18pt 19pt 20pt 24pt 36pt',
- language: 'zh_CN',
- selector: `#${this.tinymceId}`,
- //状态栏指的是编辑器最底下、左侧显示dom信息、右侧显示Tiny版权链接和调整大小的那一条。默认是显示的,设为false可将其隐藏
- statusbar: false,
- height: this.height,
- branding: false, //去掉底部文本
- body_class: 'panel-body ',
- object_resizing: false,
- toolbar: this.toolbar.length > 0 ? this.toolbar : toolbar,
- menubar: false,
- plugins: plugins,
- //只保留标签
- valid_elements: 'img[src],span[style],div,em,p,strong,blockquote',
- end_container_on_empty_block: true,
- powerpaste_word_import: 'clean',
- code_dialog_height: 450,
- code_dialog_width: '100%',
- default_link_target: '_blank',
- link_title: false,
- init_instance_callback: (editor) => {
- if (_this.value) {
- editor.setContent(_this.value);
- }
- _this.hasInit = true;
- editor.on('NodeChange Change KeyUp SetContent', () => {
- this.times = null;
- this.hasChange = true;
- // const val = editor.getContent().replace(/<p><img\s?src="data.*?<\/p>/g, '')
- this.$emit('input', editor.getContent());
- });
- editor.on('paste', (evt) => {
- // 监听粘贴事件
- this.onPaste(evt);
- });
- },
- setup(editor) {
- editor.on('FullscreenStateChanged', (e) => {
- _this.fullscreen = e.state;
- });
- },
- convert_urls: false,
- paste_data_images: false // 粘贴的同时能把内容里的图片自动上传
- // images_upload_handler(blobInfo, success, failure, progress) {
- // }
- });
- },
- destroyTinymce() {
- const tinymce = window.tinymce.get(this.tinymceId);
- if (this.fullscreen) {
- tinymce.execCommand('mceFullScreen');
- }
- if (tinymce) {
- tinymce.destroy();
- }
- },
- setContent(value) {
- window.tinymce.get(this.tinymceId).setContent(value);
- },
- getContent() {
- window.tinymce.get(this.tinymceId).getContent();
- },
- deleteImage(val) {
- let content = window.tinymce.get(this.tinymceId).getContent();
- let reg = new RegExp(`<img class="wscnph" src="${val.response.data}" />`, 'g');
- let newContent = content.replace(reg, '');
- window.tinymce.get(this.tinymceId).setContent(newContent);
- },
- imageSuccessCBK(arr) {
- // if (arr) {
- // // 获取内容,允许上传图片的线上大小,个数
- // let content = window.tinymce.get(this.tinymceId).getContent();
- // let img = [];
- // content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, function (match, capture) {
- // img.push(capture);
- // });
- // // if (img.length >= 3) {
- // // return this.$message.warning('最多插入三张图片');
- // // }
- // arr.forEach((v) => window.tinymce.get(this.tinymceId).insertContent(`<img class="wscnph" src="${v.url}" />`));
- // }
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- /deep/ .wscnph {
- width: 200px !important;
- height: 100px;
- vertical-align: middle;
- }
- .tinymce-container {
- position: relative;
- line-height: normal;
- }
- .tinymce-container {
- ::v-deep {
- .mce-fullscreen {
- z-index: 10000;
- }
- }
- }
- .tinymce-textarea {
- visibility: hidden;
- z-index: -1;
- }
- .editor-custom-btn-container {
- position: absolute;
- right: 4px;
- top: 4px;
- /*z-index: 2005;*/
- }
- .fullscreen .editor-custom-btn-container {
- z-index: 10000;
- position: fixed;
- }
- .editor-upload-btn {
- display: inline-block;
- }
- ::v-deep #mceu_28-body {
- display: none !important;
- }
- /deep/ .cell,
- .el-tooltip {
- &:hover {
- display: none;
- }
- }
- </style>
|