| 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287 | 1×
1×
1×
1×
3259×
3259×
3259×
1×
3259×
3259×
3259×
1×
22×
22×
22×
22×
21×
21×
1×
1×
21×
21×
21×
21×
21×
6×
21×
21×
21×
21×
21×
21×
21×
21×
21×
21×
21×
21×
1×
9×
9×
5×
4×
1×
12×
12×
12×
12×
12×
12×
12×
6×
6×
6×
6×
5×
5×
5×
12×
12×
12×
9×
9×
9×
9×
3×
3×
3×
9×
9×
8×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
4×
5×
5×
1×
5×
5×
1×
6492×
3234×
3258×
3258×
3258×
3258×
21×
21×
1×
21×
21×
21×
21×
21×
13×
13×
21×
21×
4×
13×
4×
4×
5×
5×
4×
4×
4×
21×
21×
21×
21×
21×
1×
21×
21×
18×
3×
3×
2×
2×
2×
2×
2×
2×
2×
2×
1×
1×
1×
1×
21×
21×
21×
13×
13×
21×
21×
21×
13×
21×
21×
21×
21×
21×
14×
14×
14×
3×
11×
11×
11×
21×
6×
6×
11×
6×
13×
21×
21×
1×
35×
35×
35×
1×
21×
21×
21×
21×
21×
31×
31×
25×
25×
3×
21×
3×
3×
1×
71387×
1×
1×
| define(["require", "exports", "../base/constant", "@syncfusion/ej2-inputs", "../../common/export-styles", "@syncfusion/ej2-base", "@syncfusion/ej2-splitbuttons"], function (require, exports, events, ej2_inputs_1, export_styles_1, ej2_base_1, ej2_splitbuttons_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ImportExport = (function () {
function ImportExport(parent) {
ej2_base_1.initializeTelemetryFeature('ImportExport', 'RichTextEditor');
this.parent = parent;
this.addEventListener();
}
ImportExport.prototype.addEventListener = function () {
this.parent.on(events.onImport, this.onImport, this);
this.parent.on(events.onExport, this.onExport, this);
this.parent.on(events.destroy, this.destroy, this);
};
ImportExport.prototype.onImport = function () {
var _this = this;
var actionBegin = {
cancel: false,
requestType: 'Import'
};
this.parent.trigger(events.actionBegin, actionBegin, function (actionBeginArgs) {
if (!actionBeginArgs.cancel) {
Eif (ej2_base_1.isNullOrUndefined(_this.uploaderObj)) {
_this.uploaderObj = new ej2_inputs_1.Uploader({
allowedExtensions: '.doc,.docx,.rtf,.dot,.dotx,.docm,.dotm',
asyncSettings: {
saveUrl: _this.parent.importWord.serviceUrl
},
uploading: function (e) {
Eif (!_this.parent.isServerRendered) {
_this.parent.trigger(events.wordImporting, e);
}
},
success: function (args) {
var wordHtml = args.e.currentTarget.response;
var importContainer = _this.parent.createElement('div');
importContainer.innerHTML = wordHtml;
var tableElement = importContainer.querySelectorAll('table:not(.e-rte-table):not(.e-rte-paste-table)');
for (var i = 0; i < tableElement.length; i++) {
tableElement[i].classList.add('e-rte-paste-table');
}
var preparedHtml = importContainer.innerHTML;
importContainer.remove();
_this.applyWordCleanupOnImport(preparedHtml);
_this.parent.trigger(events.actionComplete, { requestType: 'Import' });
_this.parent.notify(events.count, {});
}
});
}
_this.parent.setProperties({ enableXhtml: true }, true);
var uploadParentEle = _this.parent.createElement('div', { className: 'e-import-uploadwrap e-droparea' + _this.parent.getCssClass(true) });
var uploadEle = _this.parent.createElement('input', {
id: _this.rteID + '_upload', attrs: { type: 'File', name: 'UploadFiles' }, className: _this.parent.getCssClass()
});
uploadParentEle.appendChild(uploadEle);
_this.uploaderObj.appendTo(uploadEle);
_this.uploaderObj.element.click();
_this.uploaderObj.element.closest('.e-upload').style.display = 'none';
}
});
};
ImportExport.prototype.getExportProgressButton = function (member) {
var toolbarItem = this.parent.element.querySelector("#" + this.parent.element.id + "_toolbar_" + member);
if (!ej2_base_1.isNullOrUndefined(toolbarItem)) {
return ej2_base_1.getComponent(toolbarItem, ej2_splitbuttons_1.ProgressButton);
}
return null;
};
ImportExport.prototype.onExport = function (args) {
var _this = this;
var filename;
var serviceUrl;
this.parent.setProperties({ enableXhtml: true }, true);
var rteHtmlData = this.parent.getHtml();
var html;
if (args.member === 'ExportWord') {
filename = this.parent.exportWord.fileName;
serviceUrl = this.parent.exportWord.serviceUrl;
html = "<html><head><style>" + (this.parent.exportWord.stylesheet + export_styles_1.EXPORT_STYLES) + "</style></head><body><div class=\"e-content\">" + rteHtmlData + "</div></body></html>";
}
else if (args.member === 'ExportPdf') {
filename = this.parent.exportPdf.fileName;
serviceUrl = this.parent.exportPdf.serviceUrl;
html = "<html><head><style>" + (this.parent.exportPdf.stylesheet + export_styles_1.EXPORT_STYLES) + "</style></head><body><div class=\"e-content\">" + rteHtmlData + "</div></body></html>";
}
var actionBegin = {
requestType: args.member,
exportValue: html,
cancel: false
};
this.parent.trigger(events.actionBegin, actionBegin, function (actionBeginArgs) {
if (!actionBeginArgs.cancel) {
var exportArgs = {
exportType: (args.member === 'ExportPdf' ? 'PDF' : 'Word')
};
_this.parent.trigger(events.documentExporting, exportArgs);
var headers = {
'Content-Type': 'application/json'
};
if (exportArgs.currentRequest) {
for (var _i = 0, _a = exportArgs.currentRequest; _i < _a.length; _i++) {
var h = _a[_i];
Object.assign(headers, h);
}
}
var payload = exportArgs.customFormData
? { html: actionBeginArgs.exportValue, formData: exportArgs.customFormData }
: { html: actionBeginArgs.exportValue };
fetch(serviceUrl, {
method: 'POST',
headers: headers,
body: JSON.stringify(payload)
})
.then(function (response) {
if (!response.ok) {
throw new Error("HTTP error! Status: " + response.status);
}
return response.blob().then(function (blob) { return ({ blob: blob, filename: filename }); });
})
.then(function (_a) {
var blob = _a.blob, filename = _a.filename;
var url = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
var btn = _this.getExportProgressButton(args.member);
Eif (!ej2_base_1.isNullOrUndefined(btn)) {
btn.progressComplete();
}
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
_this.parent.trigger(events.actionComplete, { requestType: args.member });
_this.parent.contentModule.getEditPanel().focus();
})
.catch(function (error) {
var btn = _this.getExportProgressButton(args.member);
if (!ej2_base_1.isNullOrUndefined(btn)) {
btn.progressComplete();
}
console.error('Fetch error:', error);
_this.parent.contentModule.getEditPanel().focus();
});
}
});
};
ImportExport.prototype.destroy = function () {
if (this.parent.isDestroyed) {
return;
}
this.parent.off(events.onImport, this.onImport);
this.parent.off(events.onExport, this.onExport);
this.parent.off(events.destroy, this.destroy);
if (this.uploaderObj && !this.uploaderObj.isDestroyed) {
this.uploaderObj.destroy();
this.uploaderObj = null;
}
};
ImportExport.prototype.applyWordCleanupOnImport = function (html) {
var _this = this;
var container = this.parent.createElement('div');
container.innerHTML = html;
var lists = container.querySelectorAll('ol, ul');
lists.forEach(function (listElement) {
var paragraphs = [];
for (var i = 0; i < listElement.children.length; i++) {
var child = listElement.children[i];
if (child.tagName.toLowerCase() === 'p') {
paragraphs.push(child);
}
}
paragraphs.forEach(function (p) {
var li = _this.parent.createElement('li');
for (var i = 0; i < p.attributes.length; i++) {
var attr = p.attributes[i];
li.setAttribute(attr.name, attr.value);
}
while (p.firstChild) {
li.appendChild(p.firstChild);
}
p.parentElement.replaceChild(li, p);
});
});
var processedContainer = this.buildNestedListStructure(container);
this.extractBulletMarkersFromLists(processedContainer);
this.cleanupRtlStyles(processedContainer);
var processedHtml = processedContainer.innerHTML;
this.parent.executeCommand('insertHTML', processedHtml);
};
ImportExport.prototype.cleanupRtlStyles = function (container) {
var isRtlMode = this.parent.element && this.parent.element.classList.contains('e-rtl');
if (!isRtlMode) {
return;
}
var allElements = container.querySelectorAll('*[style*="text-align"], *[style*="text-indent"]');
for (var i = 0; i < allElements.length; i++) {
var element = allElements[i];
var styleAttr = element.getAttribute('style') || '';
var newStyle = styleAttr;
newStyle = newStyle.replace(/text-align:\s*left\s*;?/gi, '');
newStyle = newStyle.replace(/text-align:\s*start\s*;?/gi, '');
newStyle = newStyle.replace(/text-indent:\s*-[^;]*;?/gi, '');
newStyle = newStyle.replace(/;\s*;/g, ';').replace(/;\s*$/, '').trim();
if (newStyle && newStyle !== styleAttr) {
element.setAttribute('style', newStyle);
}
else Eif (!newStyle && styleAttr) {
element.removeAttribute('style');
}
}
};
ImportExport.prototype.buildNestedListStructure = function (container) {
var _this = this;
var lists = container.querySelectorAll('ol, ul');
lists.forEach(function (list) {
var items = [];
for (var i = 0; i < list.children.length; i++) {
var child = list.children[i];
Eif (child.tagName.toLowerCase() === 'li') {
items.push(child);
}
}
var _loop_1 = function (i) {
var currentItem = items[i];
var currentMargin = _this.getMarginValue(currentItem);
var nestedItems = [];
var j = i + 1;
while (j < items.length) {
var nextItem = items[j];
var nextMargin = _this.getMarginValue(nextItem);
if (nextMargin <= currentMargin) {
break;
}
Eif (nextMargin > currentMargin) {
nestedItems.push(nextItem);
j++;
}
}
if (nestedItems.length > 0) {
var nestedList_1 = _this.parent.createElement(list.tagName.toLowerCase());
nestedItems.forEach(function (nestedItem) {
nestedList_1.appendChild(nestedItem);
});
currentItem.appendChild(nestedList_1);
}
};
for (var i = 0; i < items.length; i++) {
_loop_1(i);
}
});
return container;
};
ImportExport.prototype.getMarginValue = function (element) {
var style = element.getAttribute('style') || '';
var marginMatch = style.match(/margin-left:\s*([0-9.]+)pt/);
return marginMatch ? parseFloat(marginMatch[1]) : 0;
};
ImportExport.prototype.extractBulletMarkersFromLists = function (container) {
var listItems = container.querySelectorAll('li');
listItems.forEach(function (item) {
var spans = [];
var markerSpans = [];
for (var i = 0; i < item.children.length; i++) {
var child = item.children[i];
if (child.tagName.toLowerCase() === 'span') {
spans.push(child);
if (!child.hasAttribute('lang')) {
markerSpans.push(child);
}
}
}
markerSpans.forEach(function (span) {
Eif (span.remove) {
span.remove();
}
});
});
};
ImportExport.prototype.getModuleName = function () {
return 'importExport';
};
return ImportExport;
}());
exports.ImportExport = ImportExport;
});
|