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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import { Vue, Component, Prop, Model, Watch } from 'vue-property-decorator';
import './app-rich-text-editor.less';
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 {
/**
* 传入值
*/
@Prop() value?: any;
/**
* 监听value值
*/
@Watch('value', { immediate: true, deep: true })
oncurrentContent(newval: any, val: any) {
if (newval) {
if(this.editor){
tinymceCode.remove('#' + this.id);
}
this.init(newval);
}
}
/**
* 输入name
*/
@Prop() name?: string;
/**
* 输入高度
*/
@Prop() height?: any;
/**
* 是否禁用
*/
@Prop() disabled?: any;
/**
* 当前语言,默认中文
*/
@Prop() langu?: string;
/**
* 上传文件路径
*/
public uploadUrl = '/' + Environment.BaseUrl + Environment.UploadFile;
/**
* 下载路径
*/
public downloadUrl = '/' + Environment.BaseUrl + Environment.ExportFile;
/**
* 当前富文本
*/
public editor: any = null;
/**
* 当前富文本id
*/
id: string = this.$util.createUUID();
/**
* 表单状态
*
* @type {Subject<any>}
* @memberof AppRichTextEditor
*/
@Prop() public formState?: Subject<any>;
/**
* 生命周期
*
* @memberof AppRichTextEditor
*/
public created() {
if(this.formState) {
this.formState.subscribe(({ type, data }) => {
if (Object.is('load', type)) {
if (!this.value) {
if(this.editor){
tinymceCode.remove('#' + this.id);
}
this.init(this.value);
}
}
});
}
}
/**
* 初始化富文本
*/
public mounted() {
this.init('');
}
/**
* 销毁富文本
*/
public destoryed(){
tinymceCode.remove(this.editor);
}
/**
* 初始化富文本
* @param val
*/
public init(val: any) {
let richtexteditor = this;
tinymceCode.init({
selector: '#' + this.id,
height: this.height,
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',
language_url: './assets/tinymce/langs/' + (this.langu ? this.langu : 'zh_CN') + '.js',
setup: (editor: any) => {
this.editor = editor;
editor.on('blur', () => {
const content = editor.getContent();
this.$emit('change', content);
});
},
images_upload_handler: (bolbinfo: any, success: any, failure: any) => {
const formData = new FormData();
formData.append('file', bolbinfo.blob(), bolbinfo.filename());
const _url = richtexteditor.uploadUrl;
richtexteditor.uploadFile(_url, formData).subscribe((response: any) => {
if (response.ret === 0 && response.files.length > 0) {
const id: string = response.files[0].id;
const url: string = `${richtexteditor.downloadUrl}?fileid=${id}`
success(url);
}
}, (error: any) => {
console.log(error);
});
},
init_instance_callback: (editor: any) => {
this.editor = editor;
let value = (this.value && this.value.length > 0) ? this.value : '';
if (this.editor) {
this.editor.setContent(value);
}
if (this.disabled) {
this.editor.setMode('readonly');
}
}
});
}
/**
* 上传文件
* @param url
* @param formData
*/
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;
}
/**
* 渲染组件
*/
public render() {
return (
<textarea id={this.id}></textarea>
);
}
}