app-rich-text-editor.vue 11.1 KB
Newer Older
ibizdev's avatar
ibizdev committed
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
<template>
  <textarea :id="id"></textarea>
</template>
<script lang = 'ts'>
import { Vue, Component, Prop, Model, Watch } from 'vue-property-decorator';
import { Subject } from 'rxjs';
import { Environment } from '@/environments/environment';
import axios from 'axios';

import tinymce from "tinymce/tinymce";
import 'tinymce/themes/modern';
import 'tinymce/plugins/link';
import 'tinymce/plugins/paste';
import 'tinymce/plugins/table';
import 'tinymce/plugins/image';
import 'tinymce/plugins/imagetools';
import 'tinymce/plugins/codesample';
import 'tinymce/plugins/code';
import 'tinymce/plugins/fullscreen';
import 'tinymce/plugins/preview';

const tinymceCode:any = tinymce;



@Component({})
export default class AppRichTextEditor extends Vue {

    /**
     * 传入值
ibizdev's avatar
ibizdev committed
31 32 33
     * 
     * @type {*}
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
34 35 36 37 38
     */
    @Prop() value?: any;
    
    /**
     * 输入name
ibizdev's avatar
ibizdev committed
39 40 41
     * 
     * @type {string}
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
42 43 44 45 46
     */
    @Prop() name?: string;

    /**
     * 输入高度
ibizdev's avatar
ibizdev committed
47 48 49
     * 
     * @type {*}
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
50 51 52 53 54
     */
    @Prop() height?: any;

    /**
     * 是否禁用
ibizdev's avatar
ibizdev committed
55 56 57
     * 
     * @type {boolean}
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
58
     */
ibizdev's avatar
ibizdev committed
59
    @Prop() disabled?: boolean;
ibizdev's avatar
ibizdev committed
60 61

    /**
ibizdev's avatar
ibizdev committed
62 63 64 65
     * 表单状态
     *
     * @type {Subject<any>}
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
66
     */
ibizdev's avatar
ibizdev committed
67
    @Prop() public formState?: Subject<any>;
ibizdev's avatar
ibizdev committed
68 69 70

    /**
     * 上传文件路径
ibizdev's avatar
ibizdev committed
71 72 73
     * 
     * @type {string}
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
74
     */
75
    public uploadUrl = Environment.UploadFile;
ibizdev's avatar
ibizdev committed
76 77 78

    /**
     * 下载路径
ibizdev's avatar
ibizdev committed
79 80 81
     * 
     * @type {string}
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
82 83 84 85 86
     */
    public downloadUrl =  Environment.BaseUrl + Environment.ExportFile;

    /**
     * 当前富文本
ibizdev's avatar
ibizdev committed
87 88 89
     * 
     * @type {*}
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
90 91 92 93 94
     */
    public editor: any = null;

    /**
     *  当前富文本id
ibizdev's avatar
ibizdev committed
95 96 97
     * 
     * @type {string}
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
98
     */
ibizdev's avatar
ibizdev committed
99
    public id: string = this.$util.createUUID();
ibizdev's avatar
ibizdev committed
100 101

    /**
ibizdev's avatar
ibizdev committed
102 103 104
     * 当前语言,默认中文
     * 
     * @type {*}
ibizdev's avatar
ibizdev committed
105 106
     * @memberof AppRichTextEditor
     */
107
    public langu: any = localStorage.getItem('local') ? localStorage.getItem('local') : 'zh-CN' ;
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

    /**
     * 上传params
     *
     * @type {Array<any>}
     * @memberof AppRichTextEditor
     */
    public upload_params: Array<any> = [];

    /**
     * 导出params
     *
     * @type {Array<any>}
     * @memberof AppRichTextEditor
     */
    public export_params: Array<any> = [];

    /**
     * 上传参数
     *
     * @type {string}
     * @memberof AppRichTextEditor
     */
    @Prop() public uploadparams?: any;

    /**
     * 下载参数
     *
     * @type {string}
     * @memberof AppRichTextEditor
     */
    @Prop() public exportparams?: any;

    /**
     * 视图参数
     *
     * @type {*}
     * @memberof AppRichTextEditor
     */
    @Prop() public viewparams!: any;

    /**
     * 视图上下文
     *
     * @type {*}
     * @memberof AppRichTextEditor
     */
    @Prop() public context!: any;

    /**
     * 表单数据
     *
     * @type {string}
     * @memberof AppRichTextEditor
     */
    @Prop() public data!: string;
ibizdev's avatar
ibizdev committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    
    /**
     * 语言映射文件
     * 
     * @type {*}
     * @memberof AppRichTextEditor
     */
    public languMap:any = {
        'zh-CN': 'zh_CN',
        'en-US': 'en_US',
    };

    /**
     * 是否处于激活状态
     * 
     * @type {boolean}
     * @memberof AppRichTextEditor
     */
    public isActived:boolean = true;

    /**
     * 是否需要初始化
     * 
     * @type {boolean}
     * @memberof AppRichTextEditor
     */
    public isNeedInit:boolean = false;
ibizdev's avatar
ibizdev committed
191 192 193 194 195 196 197 198 199 200

    /**
     * 生命周期
     *
     * @memberof AppRichTextEditor
     */
    public created() {
        if(this.formState) {
            this.formState.subscribe(({ type, data }) => {
                if (Object.is('load', type)) {
201
                    this.getParams();
ibizdev's avatar
ibizdev committed
202
                    if (!this.value) {
ibizdev's avatar
ibizdev committed
203
                        this.init();
ibizdev's avatar
ibizdev committed
204 205 206 207 208
                    }
                }
            });
        }
    }
ibizdev's avatar
ibizdev committed
209 210 211 212 213 214 215 216 217 218 219 220 221
    
    /**
     * 生命周期:激活
     *
     * @memberof AppRichTextEditor
     */
    public activated(){
        this.isActived = true;
        if(this.isNeedInit){
            this.init();
            this.isNeedInit = false;
        }
    }
ibizdev's avatar
ibizdev committed
222 223

    /**
ibizdev's avatar
ibizdev committed
224 225 226 227 228 229 230 231 232 233 234 235
     * 生命周期:缓存
     *
     * @memberof AppRichTextEditor
     */
    public deactivated(){
        this.isActived = false;
    }

    /**
     * 生命周期:初始化富文本
     * 
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
236 237
     */
    public mounted() {
ibizdev's avatar
ibizdev committed
238
        this.init();
ibizdev's avatar
ibizdev committed
239 240 241
    }
    
    /**
ibizdev's avatar
ibizdev committed
242 243 244
     * 生命周期:销毁富文本
     * 
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
245 246
     */
    public destoryed(){
ibizdev's avatar
ibizdev committed
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
        if(this.editor){
            tinymceCode.remove('#' + this.id);
        }
    }

    /**
     * 监听value值
     * 
     * @memberof AppRichTextEditor
     */
    @Watch('value', { immediate: true, deep: true })
    oncurrentContent(newval: any, val: any) {
        if (newval) {
            this.init();
        }
262
        this.getParams();
ibizdev's avatar
ibizdev committed
263 264 265 266 267 268 269 270 271 272 273 274 275
    }

    /**
     * 监听语言变化
     */
    @Watch('$i18n.locale')
    onLocaleChange(newval: any, val: any) {
        this.langu = newval;
        if(this.isActived){
            this.init();
        }else{
            this.isNeedInit = true;
        }
ibizdev's avatar
ibizdev committed
276 277 278 279
    }

    /**
     * 初始化富文本
ibizdev's avatar
ibizdev committed
280 281
     * 
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
282
     */
ibizdev's avatar
ibizdev committed
283 284
    public init() {
        this.destoryed();
ibizdev's avatar
ibizdev committed
285 286
        let richtexteditor = this;
        tinymceCode.init({
ibizdev's avatar
ibizdev committed
287
            selector: '#' + richtexteditor.id,
ibizdev's avatar
ibizdev committed
288
            width: 'calc( 100% - 2px )',
ibizdev's avatar
ibizdev committed
289
            height: richtexteditor.height,
ibizdev's avatar
ibizdev committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
            min_height: 400,
            branding: false,
            plugins: ['link', 'paste', 'table', 'image', 'codesample', 'code', 'fullscreen', 'preview'],
            codesample_languages: [
                { text: 'HTML/XML', value: 'markup' },
                { text: 'JavaScript', value: 'javascript' },
                { text: 'CSS', value: 'css' },
                { text: 'PHP', value: 'php' },
                { text: 'Ruby', value: 'ruby' },
                { text: 'Python', value: 'python' },
                { text: 'Java', value: 'java' },
                { text: 'C', value: 'c' },
                { text: 'C#', value: 'csharp' },
                { text: 'C++', value: 'cpp' }
            ],
            paste_data_images: true,
            codesample_content_css: 'assets/tinymce/prism.css',
            skin_url: './assets/tinymce/skins/lightgray',
ibizdev's avatar
ibizdev committed
308 309
            language_url: './assets/tinymce/langs/' + richtexteditor.languMap[richtexteditor.langu] + '.js',
            language:richtexteditor.languMap[richtexteditor.langu],
ibizdev's avatar
ibizdev committed
310
            setup: (editor: any) => {
ibizdev's avatar
ibizdev committed
311
                richtexteditor.editor = editor;
ibizdev's avatar
ibizdev committed
312 313
                editor.on('blur', () => {
                    const content = editor.getContent();
ibizdev's avatar
ibizdev committed
314
                    richtexteditor.$emit('change', content);
ibizdev's avatar
ibizdev committed
315 316 317 318 319
                });
            },
            images_upload_handler: (bolbinfo: any, success: any, failure: any) => {
                const formData = new FormData();
                formData.append('file', bolbinfo.blob(), bolbinfo.filename());
320 321 322 323 324 325 326 327 328 329 330
                let _url = richtexteditor.uploadUrl;
                if (this.upload_params.length > 0 ) {
                    _url +='?';
                    this.upload_params.forEach((item:any,i:any)=>{
                        _url += `${Object.keys(item)[0]}=${Object.values(item)[0]}`;
                        if(i<this.upload_params.length-1){
                            _url += '&';
                        }
                    })    
                }
                this.uploadUrl = _url;
ibizdev's avatar
ibizdev committed
331
                richtexteditor.uploadFile(_url, formData).subscribe((file: any) => {
332
                    let downloadUrl =   richtexteditor.downloadUrl;
333 334 335 336 337
                    if (file.filename) {
                        const id: string = file.fileid;
                        const url: string = `${downloadUrl}/${id}`
                        success(url);
                    }
338 339 340 341 342 343 344 345 346
                    if (this.export_params.length > 0) {
                        downloadUrl +='?';
                        this.export_params.forEach((item:any,i:any)=>{
                            downloadUrl += `${Object.keys(item)[0]}=${Object.values(item)[0]}`;
                            if(i<this.export_params.length-1){
                                downloadUrl += '&';
                            }
                        })
                    }
ibizdev's avatar
ibizdev committed
347 348 349 350 351 352
                }, (error: any) => {
                    console.log(error);
                    failure('HTTP Error: ' + error.status);
                });
            },
            init_instance_callback: (editor: any) => {
ibizdev's avatar
ibizdev committed
353 354 355 356
                richtexteditor.editor = editor;
                let value = (richtexteditor.value && richtexteditor.value.length > 0) ? richtexteditor.value : '';
                if (richtexteditor.editor) {
                    richtexteditor.editor.setContent(value);
ibizdev's avatar
ibizdev committed
357
                }
ibizdev's avatar
ibizdev committed
358 359
                if (richtexteditor.disabled) {
                    richtexteditor.editor.setMode('readonly');
ibizdev's avatar
ibizdev committed
360 361 362 363 364 365 366
                }
            }
        });
    }

    /**
     * 上传文件
ibizdev's avatar
ibizdev committed
367 368 369 370
     * 
     * @param url 路径
     * @param formData 文件对象 
     * @memberof AppRichTextEditor
ibizdev's avatar
ibizdev committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
     */
    public uploadFile(url: string, formData: any) {
        let _this = this;
        const subject: Subject<any> = new Subject<any>();
        axios({
            method: 'post',
            url: url,
            data: formData,
            headers: { 'Content-Type': 'image/png', 'Accept': 'application/json' },
        }).then((response: any) => {
            if (response.status === 200) {
                subject.next(response.data);
            } else {
                subject.error(response);
            }
        }).catch((response: any) => {
            subject.error(response);
        });
        return subject;
    }
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

    /**
     *获取上传,导出参数
     *
     *@memberof AppRichTextEditor
     */
    public getParams(){
        let uploadparams: any = JSON.parse(JSON.stringify(this.uploadparams));
        let exportparams: any = JSON.parse(JSON.stringify(this.exportparams));

        let upload_params: Array<string> = [];
        let export_params: Array<string> = [];

        let param:any = this.viewparams;
        let context:any = this.context;
        let _data:any = JSON.parse(this.data);

        if (this.uploadparams && !Object.is(this.uploadparams, '')) {
            upload_params = this.$util.computedNavData(_data,param,context,uploadparams);    
        }
        if (this.exportparams && !Object.is(this.exportparams, '')) {
            export_params = this.$util.computedNavData(_data,param,context,exportparams);
        }
        
        this.upload_params = [];
        this.export_params = [];

        for (const item in upload_params) {
            this.upload_params.push({
                [item]:upload_params[item]
            })
        }
        for (const item in export_params) {
            this.export_params.push({
                [item]:export_params[item]
            })
        }
    }
ibizdev's avatar
ibizdev committed
429 430 431 432
}
</script>
<style lang="less">
</style>