-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathng2-summernote.ts
215 lines (183 loc) · 6.72 KB
/
ng2-summernote.ts
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
import {
Component,
Input,
Output,
ElementRef,
EventEmitter,
NgZone,
Inject,
forwardRef
} from '@angular/core';
import {NG_VALUE_ACCESSOR} from '@angular/forms';
import {Http, Headers, RequestOptions} from '@angular/http';
import 'rxjs/add/operator/toPromise';
declare var $: any;
@Component({
selector: 'ng2-summernote',
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => Ng2Summernote),
multi: true
}
],
template: `<div class="summernote"></div>`,
})
export class Ng2Summernote {
@Input() height: number;
@Input() minHeight: number;
@Input() maxHeight: number;
@Input() placeholder: string;
@Input() focus: boolean;
@Input() airMode: boolean;
@Input() dialogsInBody: string;
@Input() editable: boolean;
@Input() lang: string;
@Input() disableResizeEditor: string;
@Input() serverImgUp: boolean;
@Input() config: any;
/** URL for upload server images */
@Input() hostUpload: string;
/** Uploaded images server folder */
@Input() uploadFolder: string = "";
@Output() change = new EventEmitter<any>();
private _config: any;
private _value: any;
constructor (
@Inject(ElementRef) private _elementRef: ElementRef,
private _zone: NgZone,
private _http: Http
) {}
get value(): any { return this._value; };
@Input() set value(v) {
if (v !== this._value) {
this._value = v;
this._onChangeCallback(v);
}
}
ngAfterViewInit () {}
/**
* Value update process
*/
updateValue (value: any) {
this._zone.run(() => {
this._value = value;
this.onChange(value);
this._onTouchedCallback();
this.change.emit(value);
});
}
ngOnDestroy () {}
private _imageUpload(dataUpload: any) {
if (dataUpload.editable) {
let data = new FormData();
data.append("file", dataUpload.files[0]);
data.append("action", "upload");
data.append("image", "resizeNoThumb");
data.append("folder", this.uploadFolder);
$.post({
data: data,
type: "POST",
url: this.hostUpload,
cache: false,
contentType: false,
processData: false,
success: (uploadedImg: any) => {
let insertImg = $('<img style="width: 100%;" src="' + uploadedImg.data[0].fileName + '" />');
$(this._elementRef.nativeElement).find('.summernote').summernote('insertNode', insertImg[0]);
console.log("Uploaded image: " + uploadedImg.data[0]);
},
error: (err: any) => { this._errHandle(err) }
});
}
}
private _mediaDelete(fileUrl: string) {
let data: any = JSON.stringify({
action: "del",
file: fileUrl
});
let headers = new Headers({
'Accept': '*/*',
'Content-Type': 'application/json'
});
let options = new RequestOptions({headers: headers});
return this._http.post(this.hostUpload, data, options)
.toPromise()
.then((response: any) => response)
.catch((err: any) => Promise.reject(err.message || err));
}
/**
* Set logical varibles from text input values
*
* @param any variable, logic varible for setting
* @param boolean defaultValue, this value will be set if variable is not set
*
* @return boolean variable, finally setted variable value
*/
private _setLogicVars(variable: any, defaultVal?: boolean) {
variable = typeof variable !== 'undefined' ? true : false;
if (!variable && defaultVal) variable = defaultVal;
return variable;
}
/**
* Hanle error in console
*/
private _errHandle(err: any) {
console.error("Error");
console.log(err);
}
/**
* Implements ControlValueAccessor
*/
writeValue (value: any) {
if (typeof value === 'string' || value) {
this._value = value;
this.height = Number(this.height);
this.editable = this._setLogicVars(this.editable, true);
this.lang = $.summernote.lang[this.lang] ? this.lang : 'en-US'
this._config = this.config || {
height: this.height || 200,
minHeight: Number(this.minHeight) || this.height || 200,
maxHeight: Number(this.maxHeight) || this.height || 500,
placeholder: this.placeholder || 'Text...',
focus: this._setLogicVars(this.focus, false),
airMode: this._setLogicVars(this.airMode, false),
dialogsInBody: this._setLogicVars(this.dialogsInBody, false),
editable: this.editable,
lang: this.lang,
disableResizeEditor: this._setLogicVars(this.disableResizeEditor, false)
};
this._config.callbacks = {
onChange: (evt: any) => {
this.updateValue(evt);
},
onInit: (evt: any) => {}
};
if (typeof this.serverImgUp !== 'undefined') {
this._config.callbacks.onImageUpload = (files: string) => {
this._imageUpload({files: files, editable: this.editable});
};
this._config.callbacks.onMediaDelete = (target: [any]) => {
let fileUrl: string;
let attributes: any = target[0].attributes;
for (let i = 0; i < attributes.length; i++) {
if (attributes[i].name == "src") {
fileUrl = attributes[i].value;
}
}
this._mediaDelete(fileUrl)
.then((resp: any) => { console.log(resp.json().data) })
.catch((err: any) => { this._errHandle(err) });
};
}
$(this._elementRef.nativeElement).find('.summernote').summernote(this._config);
$(this._elementRef.nativeElement).find('.summernote').summernote('code', value);
}
}
onChange (_: any) {}
onTouched () {}
registerOnChange (fn: any) { this.onChange = fn; }
registerOnTouched (fn: any) { this.onTouched = fn; }
_onChangeCallback (_: any) {}
_onTouchedCallback () {}
}