suneditor 2.47.5 → 2.47.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/LICENSE.txt +20 -20
  2. package/README.md +1625 -1613
  3. package/dist/css/suneditor.min.css +1 -1
  4. package/dist/suneditor.min.js +2 -2
  5. package/example.md +586 -586
  6. package/package.json +71 -71
  7. package/src/assets/css/suneditor-contents.css +561 -561
  8. package/src/assets/css/suneditor.css +2 -2
  9. package/src/assets/defaultIcons.js +103 -103
  10. package/src/lang/Lang.d.ts +143 -143
  11. package/src/lang/ckb.d.ts +4 -4
  12. package/src/lang/ckb.js +187 -187
  13. package/src/lang/cs.d.ts +4 -4
  14. package/src/lang/cs.js +187 -187
  15. package/src/lang/da.d.ts +4 -4
  16. package/src/lang/da.js +191 -191
  17. package/src/lang/de.d.ts +4 -4
  18. package/src/lang/de.js +188 -188
  19. package/src/lang/en.d.ts +4 -4
  20. package/src/lang/en.js +187 -187
  21. package/src/lang/es.d.ts +4 -4
  22. package/src/lang/es.js +187 -187
  23. package/src/lang/fa.d.ts +4 -4
  24. package/src/lang/fa.js +187 -187
  25. package/src/lang/fr.d.ts +4 -4
  26. package/src/lang/fr.js +188 -188
  27. package/src/lang/he.d.ts +4 -4
  28. package/src/lang/he.js +188 -188
  29. package/src/lang/hu.d.ts +5 -5
  30. package/src/lang/hu.js +188 -188
  31. package/src/lang/index.d.ts +25 -24
  32. package/src/lang/index.js +30 -27
  33. package/src/lang/it.d.ts +4 -4
  34. package/src/lang/it.js +188 -188
  35. package/src/lang/ja.d.ts +4 -4
  36. package/src/lang/ja.js +187 -187
  37. package/src/lang/km.d.ts +5 -0
  38. package/src/lang/km.js +188 -0
  39. package/src/lang/ko.d.ts +4 -4
  40. package/src/lang/ko.js +187 -187
  41. package/src/lang/lv.d.ts +4 -4
  42. package/src/lang/lv.js +187 -187
  43. package/src/lang/nl.d.ts +4 -4
  44. package/src/lang/nl.js +187 -187
  45. package/src/lang/pl.d.ts +4 -4
  46. package/src/lang/pl.js +187 -187
  47. package/src/lang/pt_br.d.ts +4 -4
  48. package/src/lang/pt_br.js +189 -189
  49. package/src/lang/ro.d.ts +4 -4
  50. package/src/lang/ro.js +187 -187
  51. package/src/lang/ru.d.ts +4 -4
  52. package/src/lang/ru.js +187 -187
  53. package/src/lang/se.d.ts +4 -4
  54. package/src/lang/se.js +191 -191
  55. package/src/lang/tr.d.ts +5 -5
  56. package/src/lang/tr.js +191 -191
  57. package/src/lang/ua.d.ts +5 -5
  58. package/src/lang/ua.js +188 -188
  59. package/src/lang/ur.d.ts +4 -4
  60. package/src/lang/ur.js +187 -187
  61. package/src/lang/zh_cn.d.ts +4 -4
  62. package/src/lang/zh_cn.js +187 -187
  63. package/src/lib/constructor.js +0 -0
  64. package/src/lib/context.d.ts +42 -42
  65. package/src/lib/context.js +0 -0
  66. package/src/lib/core.d.ts +1135 -1135
  67. package/src/lib/core.js +9 -3
  68. package/src/lib/history.d.ts +48 -48
  69. package/src/lib/history.js +218 -218
  70. package/src/lib/util.d.ts +677 -677
  71. package/src/lib/util.js +1 -1
  72. package/src/options.d.ts +621 -620
  73. package/src/plugins/CommandPlugin.d.ts +7 -7
  74. package/src/plugins/DialogPlugin.d.ts +19 -19
  75. package/src/plugins/FileBrowserPlugin.d.ts +29 -29
  76. package/src/plugins/Module.d.ts +14 -14
  77. package/src/plugins/Plugin.d.ts +41 -41
  78. package/src/plugins/SubmenuPlugin.d.ts +7 -7
  79. package/src/plugins/command/blockquote.d.ts +4 -4
  80. package/src/plugins/command/blockquote.js +46 -46
  81. package/src/plugins/dialog/audio.d.ts +4 -4
  82. package/src/plugins/dialog/audio.js +557 -558
  83. package/src/plugins/dialog/image.d.ts +4 -4
  84. package/src/plugins/dialog/image.js +1126 -1127
  85. package/src/plugins/dialog/link.d.ts +4 -4
  86. package/src/plugins/dialog/link.js +223 -223
  87. package/src/plugins/dialog/math.d.ts +4 -4
  88. package/src/plugins/dialog/math.js +300 -300
  89. package/src/plugins/dialog/video.d.ts +4 -4
  90. package/src/plugins/dialog/video.js +989 -989
  91. package/src/plugins/fileBrowser/imageGallery.d.ts +4 -4
  92. package/src/plugins/fileBrowser/imageGallery.js +64 -64
  93. package/src/plugins/index.d.ts +79 -79
  94. package/src/plugins/index.js +32 -32
  95. package/src/plugins/modules/_anchor.js +461 -461
  96. package/src/plugins/modules/_colorPicker.d.ts +59 -59
  97. package/src/plugins/modules/_colorPicker.js +0 -0
  98. package/src/plugins/modules/_notice.d.ts +20 -20
  99. package/src/plugins/modules/_notice.js +72 -72
  100. package/src/plugins/modules/_selectMenu.js +118 -118
  101. package/src/plugins/modules/component.d.ts +24 -24
  102. package/src/plugins/modules/component.js +80 -80
  103. package/src/plugins/modules/dialog.d.ts +27 -27
  104. package/src/plugins/modules/dialog.js +174 -174
  105. package/src/plugins/modules/fileBrowser.d.ts +41 -41
  106. package/src/plugins/modules/fileBrowser.js +377 -377
  107. package/src/plugins/modules/fileManager.d.ts +66 -66
  108. package/src/plugins/modules/fileManager.js +325 -325
  109. package/src/plugins/modules/index.d.ts +10 -10
  110. package/src/plugins/modules/index.js +8 -8
  111. package/src/plugins/modules/resizing.d.ts +153 -153
  112. package/src/plugins/modules/resizing.js +902 -902
  113. package/src/plugins/submenu/align.d.ts +4 -4
  114. package/src/plugins/submenu/align.js +160 -160
  115. package/src/plugins/submenu/font.d.ts +4 -4
  116. package/src/plugins/submenu/font.js +123 -123
  117. package/src/plugins/submenu/fontColor.d.ts +4 -4
  118. package/src/plugins/submenu/fontColor.js +0 -0
  119. package/src/plugins/submenu/fontSize.d.ts +4 -4
  120. package/src/plugins/submenu/fontSize.js +112 -112
  121. package/src/plugins/submenu/formatBlock.d.ts +4 -4
  122. package/src/plugins/submenu/formatBlock.js +273 -273
  123. package/src/plugins/submenu/hiliteColor.d.ts +4 -4
  124. package/src/plugins/submenu/hiliteColor.js +0 -0
  125. package/src/plugins/submenu/horizontalRule.d.ts +4 -4
  126. package/src/plugins/submenu/horizontalRule.js +98 -98
  127. package/src/plugins/submenu/lineHeight.d.ts +4 -4
  128. package/src/plugins/submenu/lineHeight.js +104 -104
  129. package/src/plugins/submenu/list.d.ts +4 -4
  130. package/src/plugins/submenu/list.js +456 -456
  131. package/src/plugins/submenu/paragraphStyle.d.ts +4 -4
  132. package/src/plugins/submenu/paragraphStyle.js +135 -135
  133. package/src/plugins/submenu/table.d.ts +4 -4
  134. package/src/plugins/submenu/template.d.ts +4 -4
  135. package/src/plugins/submenu/template.js +71 -71
  136. package/src/plugins/submenu/textStyle.d.ts +4 -4
  137. package/src/plugins/submenu/textStyle.js +167 -167
  138. package/src/suneditor.d.ts +9 -9
  139. package/src/suneditor.js +75 -75
  140. package/src/suneditor_build.js +20 -17
  141. package/README_V3_TEMP.md +0 -630
@@ -1,559 +1,558 @@
1
- /*
2
- * wysiwyg web editor
3
- *
4
- * suneditor.js
5
- * Copyright 2017 JiHong Lee.
6
- * MIT license.
7
- */
8
- 'use strict';
9
-
10
- import dialog from '../modules/dialog';
11
- import component from '../modules/component';
12
- import fileManager from '../modules/fileManager';
13
-
14
- export default {
15
- name: 'audio',
16
- display: 'dialog',
17
- add: function (core) {
18
- core.addModule([dialog, component, fileManager]);
19
-
20
- const context = core.context;
21
- const contextAudio = context.audio = {
22
- _infoList: [], // @Override fileManager
23
- _infoIndex: 0, // @Override fileManager
24
- _uploadFileLength: 0, // @Override fileManager
25
- focusElement: null, // @Override dialog // This element has focus when the dialog is opened.
26
- targetSelect: null,
27
- _origin_w: core.options.audioWidth,
28
- _origin_h: core.options.audioHeight,
29
- _linkValue: '',
30
- // @require @Override component
31
- _element: null,
32
- _cover: null,
33
- _container: null,
34
- };
35
-
36
- /** dialog */
37
- let audio_dialog = this.setDialog(core);
38
- contextAudio.modal = audio_dialog;
39
- contextAudio.audioInputFile = audio_dialog.querySelector('._se_audio_files');
40
- contextAudio.audioUrlFile = audio_dialog.querySelector('.se-input-url');
41
- contextAudio.focusElement = contextAudio.audioInputFile || contextAudio.audioUrlFile;
42
- contextAudio.preview = audio_dialog.querySelector('.se-link-preview');
43
-
44
- /** controller */
45
- let audio_controller = this.setController(core);
46
- contextAudio.controller = audio_controller;
47
-
48
- /** add event listeners */
49
- audio_dialog.querySelector('form').addEventListener('submit', this.submit.bind(core));
50
- if (contextAudio.audioInputFile) audio_dialog.querySelector('.se-dialog-files-edge-button').addEventListener('click', this._removeSelectedFiles.bind(contextAudio.audioInputFile, contextAudio.audioUrlFile, contextAudio.preview));
51
- if (contextAudio.audioInputFile && contextAudio.audioUrlFile) contextAudio.audioInputFile.addEventListener('change', this._fileInputChange.bind(contextAudio));
52
- audio_controller.addEventListener('click', this.onClick_controller.bind(core));
53
- if (contextAudio.audioUrlFile) contextAudio.audioUrlFile.addEventListener('input', this._onLinkPreview.bind(contextAudio.preview, contextAudio, core.options.linkProtocol));
54
-
55
- /** append html */
56
- context.dialog.modal.appendChild(audio_dialog);
57
-
58
- /** append controller */
59
- context.element.relative.appendChild(audio_controller);
60
-
61
- /** empty memory */
62
- audio_dialog = null, audio_controller = null;
63
- },
64
-
65
- /** HTML - dialog */
66
- setDialog: function (core) {
67
- const option = core.options;
68
- const lang = core.lang;
69
- const dialog = core.util.createElement('DIV');
70
-
71
- dialog.className = 'se-dialog-content';
72
- dialog.style.display = 'none';
73
- let html = '' +
74
- '<form method="post" enctype="multipart/form-data">' +
75
- '<div class="se-dialog-header">' +
76
- '<button type="button" data-command="close" class="se-btn se-dialog-close" title="' + lang.dialogBox.close + '" aria-label="' + lang.dialogBox.close + '">' +
77
- core.icons.cancel +
78
- '</button>' +
79
- '<span class="se-modal-title">' + lang.dialogBox.audioBox.title + '</span>' +
80
- '</div>' +
81
- '<div class="se-dialog-body">';
82
-
83
- if (option.audioFileInput) {
84
- html += '' +
85
- '<div class="se-dialog-form">' +
86
- '<label>' + lang.dialogBox.audioBox.file + '</label>' +
87
- '<div class="se-dialog-form-files">' +
88
- '<input class="se-input-form _se_audio_files" type="file" accept="' + option.audioAccept + '"' + (option.audioMultipleFile ? ' multiple="multiple"' : '') + '/>' +
89
- '<button type="button" data-command="filesRemove" class="se-btn se-dialog-files-edge-button se-file-remove" title="' + lang.controller.remove + '" aria-label="' + lang.controller.remove + '">' + core.icons.cancel + '</button>' +
90
- '</div>' +
91
- '</div>';
92
- }
93
-
94
- if (option.audioUrlInput) {
95
- html += '' +
96
- '<div class="se-dialog-form">' +
97
- '<label>' + lang.dialogBox.audioBox.url + '</label>' +
98
- '<input class="se-input-form se-input-url" type="text" />' +
99
- '<pre class="se-link-preview"></pre>' +
100
- '</div>';
101
- }
102
-
103
- html += '' +
104
- '</div>' +
105
- '<div class="se-dialog-footer">' +
106
- '<button type="submit" class="se-btn-primary" title="' + lang.dialogBox.submitButton + '" aria-label="' + lang.dialogBox.submitButton + '"><span>' + lang.dialogBox.submitButton + '</span></button>' +
107
- '</div>' +
108
- '</form>';
109
-
110
- dialog.innerHTML = html;
111
-
112
- return dialog;
113
- },
114
-
115
- /** HTML - controller */
116
- setController: function (core) {
117
- const lang = core.lang;
118
- const icons = core.icons;
119
- const link_btn = core.util.createElement('DIV');
120
-
121
- link_btn.className = 'se-controller se-controller-link';
122
- link_btn.innerHTML = '' +
123
- '<div class="se-arrow se-arrow-up"></div>' +
124
- '<div class="link-content">' +
125
- '<div class="se-btn-group">' +
126
- '<button type="button" data-command="update" tabindex="-1" class="se-tooltip">' +
127
- icons.edit +
128
- '<span class="se-tooltip-inner"><span class="se-tooltip-text">' + lang.controller.edit + '</span></span>' +
129
- '</button>' +
130
- '<button type="button" data-command="delete" tabindex="-1" class="se-tooltip">' +
131
- icons.delete +
132
- '<span class="se-tooltip-inner"><span class="se-tooltip-text">' + lang.controller.remove + '</span></span>' +
133
- '</button>' +
134
- '</div>' +
135
- '</div>';
136
-
137
- return link_btn;
138
- },
139
-
140
- // Disable url input when uploading files
141
- _fileInputChange: function () {
142
- if (!this.audioInputFile.value) {
143
- this.audioUrlFile.removeAttribute('disabled');
144
- this.preview.style.textDecoration = '';
145
- } else {
146
- this.audioUrlFile.setAttribute('disabled', true);
147
- this.preview.style.textDecoration = 'line-through';
148
- }
149
- },
150
-
151
- // Disable url input when uploading files
152
- _removeSelectedFiles: function (urlInput, preview) {
153
- this.value = '';
154
- if (urlInput) {
155
- urlInput.removeAttribute('disabled');
156
- preview.style.textDecoration = '';
157
- }
158
- },
159
-
160
- // create new audio tag
161
- _createAudioTag: function () {
162
- const oAudio = this.util.createElement('AUDIO');
163
- this.plugins.audio._setTagAttrs.call(this, oAudio);
164
-
165
- const w = this.context.audio._origin_w;
166
- const h = this.context.audio._origin_h;
167
- oAudio.setAttribute('origin-size', w + ',' + h);
168
- oAudio.style.cssText = (w ? ('width:' + w + '; ') : '') + (h ? ('height:' + h + ';') : '');
169
-
170
- return oAudio;
171
- },
172
-
173
- _setTagAttrs: function (element) {
174
- element.setAttribute('controls', true);
175
-
176
- const attrs = this.options.audioTagAttrs;
177
- if (!attrs) return;
178
-
179
- for (let key in attrs) {
180
- if (!this.util.hasOwn(attrs, key)) continue;
181
- element.setAttribute(key, attrs[key]);
182
- }
183
- },
184
-
185
- _onLinkPreview: function (context, protocol, e) {
186
- const value = e.target.value.trim();
187
- context._linkValue = this.textContent = !value ? '' : (protocol && value.indexOf('://') === -1 && value.indexOf('#') !== 0) ? protocol + value : value.indexOf('://') === -1 ? '/' + value : value;
188
- },
189
-
190
- /**
191
- * @Required @Override fileManager
192
- */
193
- fileTags: ['audio'],
194
-
195
- /**
196
- * @Override core, fileManager, resizing
197
- * @description It is called from core.selectComponent.
198
- * @param {Element} element Target element
199
- */
200
- select: function (element) {
201
- this.plugins.audio.onModifyMode.call(this, element);
202
- },
203
-
204
- /**
205
- * @Override fileManager, resizing
206
- */
207
- destroy: function (element) {
208
- element = element || this.context.audio._element;
209
- const container = this.util.getParentElement(element, this.util.isComponent) || element;
210
- const dataIndex = element.getAttribute('data-index') * 1;
211
-
212
- if (typeof this.functions.onAudioDeleteBefore === 'function' && (this.functions.onAudioDeleteBefore(element, container, dataIndex, this) === false)) return;
213
-
214
- const focusEl = (container.previousElementSibling || container.nextElementSibling);
215
- const emptyDiv = container.parentNode;
216
- this.util.removeItem(container);
217
- this.plugins.audio.init.call(this);
218
- this.controllersOff();
219
-
220
- if (emptyDiv !== this.context.element.wysiwyg) this.util.removeItemAllParents(emptyDiv, function (current) { return current.childNodes.length === 0; }, null);
221
-
222
- // focus
223
- this.focusEdge(focusEl);
224
-
225
- // fileManager event
226
- this.plugins.fileManager.deleteInfo.call(this, 'audio', dataIndex, this.functions.onAudioUpload);
227
-
228
- // history stack
229
- this.history.push(false);
230
- },
231
-
232
- /**
233
- * @Override fileManager
234
- */
235
- checkFileInfo: function () {
236
- this.plugins.fileManager.checkInfo.call(this, 'audio', ['audio'], this.functions.onAudioUpload, this.plugins.audio.updateCover.bind(this), false);
237
- },
238
-
239
- /**
240
- * @Override fileManager
241
- */
242
- resetFileInfo: function () {
243
- this.plugins.fileManager.resetInfo.call(this, 'audio', this.functions.onAudioUpload);
244
- },
245
-
246
- /**
247
- * @Required @Override dialog
248
- */
249
- on: function (update) {
250
- const contextAudio = this.context.audio;
251
-
252
- if (!update) {
253
- this.plugins.audio.init.call(this);
254
- if (contextAudio.audioInputFile && this.options.audioMultipleFile) contextAudio.audioInputFile.setAttribute('multiple', 'multiple');
255
- } else if (contextAudio._element) {
256
- this.context.dialog.updateModal = true;
257
- contextAudio._linkValue = contextAudio.preview.textContent = contextAudio.audioUrlFile.value = contextAudio._element.src;
258
- if (contextAudio.audioInputFile && this.options.audioMultipleFile) contextAudio.audioInputFile.removeAttribute('multiple');
259
- } else {
260
- if (contextAudio.audioInputFile && this.options.audioMultipleFile) contextAudio.audioInputFile.removeAttribute('multiple');
261
- }
262
- },
263
-
264
- /**
265
- * @Required @Override dialog
266
- */
267
- open: function () {
268
- this.plugins.dialog.open.call(this, 'audio', 'audio' === this.currentControllerName);
269
- },
270
-
271
- submit: function (e) {
272
- const contextAudio = this.context.audio;
273
-
274
- e.preventDefault();
275
- e.stopPropagation();
276
-
277
- try {
278
- if (contextAudio.audioInputFile && contextAudio.audioInputFile.files.length > 0) {
279
- this.showLoading();
280
- this.plugins.audio.submitAction.call(this, contextAudio.audioInputFile.files);
281
- } else if (contextAudio.audioUrlFile && contextAudio._linkValue.length > 0) {
282
- this.showLoading();
283
- this.plugins.audio.setupUrl.call(this, contextAudio._linkValue);
284
- }
285
- } catch (error) {
286
- this.closeLoading();
287
- throw Error('[SUNEDITOR.audio.submit.fail] cause : "' + error.message + '"');
288
- } finally {
289
- this.plugins.dialog.close.call(this);
290
- }
291
-
292
- return false;
293
- },
294
-
295
- submitAction: function (fileList) {
296
- if (fileList.length === 0) return;
297
-
298
- let fileSize = 0;
299
- let files = [];
300
- for (let i = 0, len = fileList.length; i < len; i++) {
301
- if (/audio/i.test(fileList[i].type)) {
302
- files.push(fileList[i]);
303
- fileSize += fileList[i].size;
304
- }
305
- }
306
-
307
- const limitSize = this.options.audioUploadSizeLimit;
308
- if (limitSize > 0) {
309
- let infoSize = 0;
310
- const audiosInfo = this.context.audio._infoList;
311
- for (let i = 0, len = audiosInfo.length; i < len; i++) {
312
- infoSize += audiosInfo[i].size * 1;
313
- }
314
-
315
- if ((fileSize + infoSize) > limitSize) {
316
- this.closeLoading();
317
- const err = '[SUNEDITOR.audioUpload.fail] Size of uploadable total audios: ' + (limitSize/1000) + 'KB';
318
- if (typeof this.functions.onAudioUploadError !== 'function' || this.functions.onAudioUploadError(err, { 'limitSize': limitSize, 'currentSize': infoSize, 'uploadSize': fileSize }, this)) {
319
- this.functions.noticeOpen(err);
320
- }
321
- return;
322
- }
323
- }
324
-
325
- const contextAudio = this.context.audio;
326
- contextAudio._uploadFileLength = files.length;
327
-
328
- const info = {
329
- isUpdate: this.context.dialog.updateModal,
330
- element: contextAudio._element
331
- };
332
-
333
- if (typeof this.functions.onAudioUploadBefore === 'function') {
334
- const result = this.functions.onAudioUploadBefore(files, info, this, function (data) {
335
- if (data && this._w.Array.isArray(data.result)) {
336
- this.plugins.audio.register.call(this, info, data);
337
- } else {
338
- this.plugins.audio.upload.call(this, info, data);
339
- }
340
- }.bind(this));
341
-
342
- if (typeof result === 'undefined') return;
343
- if (!result) {
344
- this.closeLoading();
345
- return;
346
- }
347
- if (typeof result === 'object' && result.length > 0) files = result;
348
- }
349
-
350
- this.plugins.audio.upload.call(this, info, files);
351
- },
352
-
353
- error: function (message, response) {
354
- this.closeLoading();
355
- if (typeof this.functions.onAudioUploadError !== 'function' || this.functions.onAudioUploadError(message, response, this)) {
356
- this.functions.noticeOpen(message);
357
- throw Error('[SUNEDITOR.plugin.audio.exception] response: ' + message);
358
- }
359
- },
360
-
361
- upload: function (info, files) {
362
- if (!files) {
363
- this.closeLoading();
364
- return;
365
- }
366
- if (typeof files === 'string') {
367
- this.plugins.audio.error.call(this, files, null);
368
- return;
369
- }
370
-
371
- const audioUploadUrl = this.options.audioUploadUrl;
372
- const filesLen = this.context.dialog.updateModal ? 1 : files.length;
373
-
374
- // create formData
375
- const formData = new FormData();
376
- for (let i = 0; i < filesLen; i++) {
377
- formData.append('file-' + i, files[i]);
378
- }
379
-
380
- // server upload
381
- this.plugins.fileManager.upload.call(this, audioUploadUrl, this.options.audioUploadHeader, formData, this.plugins.audio.callBack_upload.bind(this, info), this.functions.onAudioUploadError);
382
- },
383
-
384
- callBack_upload: function (info, xmlHttp) {
385
- if (typeof this.functions.audioUploadHandler === 'function') {
386
- this.functions.audioUploadHandler(xmlHttp, info, this);
387
- } else {
388
- const response = JSON.parse(xmlHttp.responseText);
389
- if (response.errorMessage) {
390
- this.plugins.audio.error.call(this, response.errorMessage, response);
391
- } else {
392
- this.plugins.audio.register.call(this, info, response);
393
- }
394
- }
395
- },
396
-
397
- register: function (info, response) {
398
- const fileList = response.result;
399
-
400
- for (let i = 0, len = fileList.length, file, oAudio; i < len; i++) {
401
- if (info.isUpdate) oAudio = info.element;
402
- else oAudio = this.plugins.audio._createAudioTag.call(this);
403
-
404
- file = { name: fileList[i].name, size: fileList[i].size };
405
- this.plugins.audio.create_audio.call(this, oAudio, fileList[i].url, file, info.isUpdate);
406
- }
407
-
408
- this.closeLoading();
409
- },
410
-
411
- setupUrl: function (src) {
412
- try {
413
- if (src.length === 0) return false;
414
- this.plugins.audio.create_audio.call(this, this.plugins.audio._createAudioTag.call(this), src, null, this.context.dialog.updateModal);
415
- } catch (error) {
416
- throw Error('[SUNEDITOR.audio.audio.fail] cause : "' + error.message + '"');
417
- } finally {
418
- this.closeLoading();
419
- }
420
- },
421
-
422
- create_audio: function (element, src, file, isUpdate) {
423
- const contextAudio = this.context.audio;
424
-
425
- // create new tag
426
- if (!isUpdate) {
427
- element.src = src;
428
- const cover = this.plugins.component.set_cover.call(this, element);
429
- const container = this.plugins.component.set_container.call(this, cover, '');
430
- if (!this.insertComponent(container, false, true, !this.options.mediaAutoSelect)) {
431
- this.focus();
432
- return;
433
- }
434
- if (!this.options.mediaAutoSelect) {
435
- const line = this.appendFormatTag(container, null);
436
- if (line) this.setRange(line, 0, line, 0);
437
- }
438
- } // update
439
- else {
440
- if (contextAudio._element) element = contextAudio._element;
441
- if (element && element.src !== src) {
442
- element.src = src;
443
- this.selectComponent(element, 'audio');
444
- } else {
445
- this.selectComponent(element, 'audio');
446
- return;
447
- }
448
- }
449
-
450
- this.plugins.fileManager.setInfo.call(this, 'audio', element, this.functions.onAudioUpload, file, false);
451
- if (isUpdate) this.history.push(false);
452
- },
453
-
454
- updateCover: function (element) {
455
- const contextAudio = this.context.audio;
456
- this.plugins.audio._setTagAttrs.call(this, element);
457
-
458
- // find component element
459
- let existElement = (this.util.isRangeFormatElement(element.parentNode) || this.util.isWysiwygDiv(element.parentNode)) ?
460
- element : this.util.getFormatElement(element) || element;
461
-
462
- // clone element
463
- const prevElement = element;
464
- contextAudio._element = element = element.cloneNode(false);
465
- const cover = this.plugins.component.set_cover.call(this, element);
466
- const container = this.plugins.component.set_container.call(this, cover, 'se-audio-container');
467
-
468
- try {
469
- if (this.util.getParentElement(prevElement, this.util.isNotCheckingNode)) {
470
- prevElement.parentNode.replaceChild(container, prevElement);
471
- } else if (this.util.isListCell(existElement)) {
472
- const refer = this.util.getParentElement(prevElement, function (current) { return current.parentNode === existElement; });
473
- existElement.insertBefore(container, refer);
474
- this.util.removeItem(prevElement);
475
- this.util.removeEmptyNode(refer, null, true);
476
- } else if (this.util.isFormatElement(existElement)) {
477
- const refer = this.util.getParentElement(prevElement, function (current) { return current.parentNode === existElement; });
478
- existElement = this.util.splitElement(existElement, refer);
479
- existElement.parentNode.insertBefore(container, existElement);
480
- this.util.removeItem(prevElement);
481
- this.util.removeEmptyNode(existElement, null, true);
482
- if (existElement.children.length === 0) existElement.innerHTML = this.util.htmlRemoveWhiteSpace(existElement.innerHTML);
483
- } else {
484
- existElement.parentNode.replaceChild(container, existElement);
485
- }
486
- } catch (error) {
487
- console.warn('[SUNEDITOR.audio.error] Maybe the audio tag is nested.', error);
488
- }
489
-
490
- this.plugins.fileManager.setInfo.call(this, 'audio', element, this.functions.onAudioUpload, null, false);
491
- this.plugins.audio.init.call(this);
492
- },
493
-
494
- /**
495
- * @Required @Override fileManager, resizing
496
- */
497
- onModifyMode: function (selectionTag) {
498
- const contextAudio = this.context.audio;
499
-
500
- this.setControllerPosition(contextAudio.controller, selectionTag, 'bottom', {left: 0, top: 0});
501
- this.controllersOn(contextAudio.controller, selectionTag, this.plugins.audio.onControllerOff.bind(this, selectionTag), 'audio');
502
-
503
- this.util.addClass(selectionTag, 'active');
504
- contextAudio._element = selectionTag;
505
- contextAudio._cover = this.util.getParentElement(selectionTag, 'FIGURE');
506
- contextAudio._container = this.util.getParentElement(selectionTag, this.util.isComponent);
507
- },
508
-
509
- /**
510
- * @Required @Override fileManager, resizing
511
- */
512
- openModify: function (notOpen) {
513
- if (this.context.audio.audioUrlFile) {
514
- const contextAudio = this.context.audio;
515
- contextAudio._linkValue = contextAudio.preview.textContent = contextAudio.audioUrlFile.value = contextAudio._element.src;
516
- }
517
- if (!notOpen) this.plugins.dialog.open.call(this, 'audio', true);
518
- },
519
-
520
- onClick_controller: function (e) {
521
- e.stopPropagation();
522
-
523
- const command = e.target.getAttribute('data-command');
524
- if (!command) return;
525
-
526
- e.preventDefault();
527
-
528
- if (/update/.test(command)) {
529
- this.plugins.audio.openModify.call(this, false);
530
- }
531
- else { /** delete */
532
- this.plugins.audio.destroy.call(this, this.context.audio._element);
533
- }
534
-
535
- this.controllersOff();
536
- },
537
-
538
- onControllerOff: function (selectionTag) {
539
- this.util.removeClass(selectionTag, 'active');
540
- this.context.audio.controller.style.display = 'none';
541
- },
542
-
543
- /**
544
- * @Required @Override dialog
545
- */
546
- init: function () {
547
- if (this.context.dialog.updateModal) return;
548
- const contextAudio = this.context.audio;
549
-
550
- if (contextAudio.audioInputFile) contextAudio.audioInputFile.value = '';
551
- if (contextAudio.audioUrlFile) contextAudio._linkValue = contextAudio.preview.textContent = contextAudio.audioUrlFile.value = '';
552
- if (contextAudio.audioInputFile && contextAudio.audioUrlFile) {
553
- contextAudio.audioUrlFile.removeAttribute('disabled');
554
- contextAudio.preview.style.textDecoration = '';
555
- }
556
-
557
- contextAudio._element = null;
558
- }
1
+ /*
2
+ * wysiwyg web editor
3
+ *
4
+ * suneditor.js
5
+ * Copyright 2017 JiHong Lee.
6
+ * MIT license.
7
+ */
8
+ 'use strict';
9
+
10
+ import dialog from '../modules/dialog';
11
+ import component from '../modules/component';
12
+ import fileManager from '../modules/fileManager';
13
+
14
+ export default {
15
+ name: 'audio',
16
+ display: 'dialog',
17
+ add: function (core) {
18
+ core.addModule([dialog, component, fileManager]);
19
+
20
+ const context = core.context;
21
+ const contextAudio = context.audio = {
22
+ _infoList: [], // @Override fileManager
23
+ _infoIndex: 0, // @Override fileManager
24
+ _uploadFileLength: 0, // @Override fileManager
25
+ focusElement: null, // @Override dialog // This element has focus when the dialog is opened.
26
+ targetSelect: null,
27
+ _origin_w: core.options.audioWidth,
28
+ _origin_h: core.options.audioHeight,
29
+ _linkValue: '',
30
+ // @require @Override component
31
+ _element: null,
32
+ _cover: null,
33
+ _container: null,
34
+ };
35
+
36
+ /** dialog */
37
+ let audio_dialog = this.setDialog(core);
38
+ contextAudio.modal = audio_dialog;
39
+ contextAudio.audioInputFile = audio_dialog.querySelector('._se_audio_files');
40
+ contextAudio.audioUrlFile = audio_dialog.querySelector('.se-input-url');
41
+ contextAudio.focusElement = contextAudio.audioInputFile || contextAudio.audioUrlFile;
42
+ contextAudio.preview = audio_dialog.querySelector('.se-link-preview');
43
+
44
+ /** controller */
45
+ let audio_controller = this.setController(core);
46
+ contextAudio.controller = audio_controller;
47
+
48
+ /** add event listeners */
49
+ audio_dialog.querySelector('form').addEventListener('submit', this.submit.bind(core));
50
+ if (contextAudio.audioInputFile) audio_dialog.querySelector('.se-dialog-files-edge-button').addEventListener('click', this._removeSelectedFiles.bind(contextAudio.audioInputFile, contextAudio.audioUrlFile, contextAudio.preview));
51
+ if (contextAudio.audioInputFile && contextAudio.audioUrlFile) contextAudio.audioInputFile.addEventListener('change', this._fileInputChange.bind(contextAudio));
52
+ audio_controller.addEventListener('click', this.onClick_controller.bind(core));
53
+ if (contextAudio.audioUrlFile) contextAudio.audioUrlFile.addEventListener('input', this._onLinkPreview.bind(contextAudio.preview, contextAudio, core.options.linkProtocol));
54
+
55
+ /** append html */
56
+ context.dialog.modal.appendChild(audio_dialog);
57
+
58
+ /** append controller */
59
+ context.element.relative.appendChild(audio_controller);
60
+
61
+ /** empty memory */
62
+ audio_dialog = null, audio_controller = null;
63
+ },
64
+
65
+ /** HTML - dialog */
66
+ setDialog: function (core) {
67
+ const option = core.options;
68
+ const lang = core.lang;
69
+ const dialog = core.util.createElement('DIV');
70
+
71
+ dialog.className = 'se-dialog-content';
72
+ dialog.style.display = 'none';
73
+ let html = '' +
74
+ '<form method="post" enctype="multipart/form-data">' +
75
+ '<div class="se-dialog-header">' +
76
+ '<button type="button" data-command="close" class="se-btn se-dialog-close" title="' + lang.dialogBox.close + '" aria-label="' + lang.dialogBox.close + '">' +
77
+ core.icons.cancel +
78
+ '</button>' +
79
+ '<span class="se-modal-title">' + lang.dialogBox.audioBox.title + '</span>' +
80
+ '</div>' +
81
+ '<div class="se-dialog-body">';
82
+
83
+ if (option.audioFileInput) {
84
+ html += '' +
85
+ '<div class="se-dialog-form">' +
86
+ '<label>' + lang.dialogBox.audioBox.file + '</label>' +
87
+ '<div class="se-dialog-form-files">' +
88
+ '<input class="se-input-form _se_audio_files" type="file" accept="' + option.audioAccept + '"' + (option.audioMultipleFile ? ' multiple="multiple"' : '') + '/>' +
89
+ '<button type="button" data-command="filesRemove" class="se-btn se-dialog-files-edge-button se-file-remove" title="' + lang.controller.remove + '" aria-label="' + lang.controller.remove + '">' + core.icons.cancel + '</button>' +
90
+ '</div>' +
91
+ '</div>';
92
+ }
93
+
94
+ if (option.audioUrlInput) {
95
+ html += '' +
96
+ '<div class="se-dialog-form">' +
97
+ '<label>' + lang.dialogBox.audioBox.url + '</label>' +
98
+ '<input class="se-input-form se-input-url" type="text" />' +
99
+ '<pre class="se-link-preview"></pre>' +
100
+ '</div>';
101
+ }
102
+
103
+ html += '' +
104
+ '</div>' +
105
+ '<div class="se-dialog-footer">' +
106
+ '<button type="submit" class="se-btn-primary" title="' + lang.dialogBox.submitButton + '" aria-label="' + lang.dialogBox.submitButton + '"><span>' + lang.dialogBox.submitButton + '</span></button>' +
107
+ '</div>' +
108
+ '</form>';
109
+
110
+ dialog.innerHTML = html;
111
+
112
+ return dialog;
113
+ },
114
+
115
+ /** HTML - controller */
116
+ setController: function (core) {
117
+ const lang = core.lang;
118
+ const icons = core.icons;
119
+ const link_btn = core.util.createElement('DIV');
120
+
121
+ link_btn.className = 'se-controller se-controller-link';
122
+ link_btn.innerHTML = '' +
123
+ '<div class="se-arrow se-arrow-up"></div>' +
124
+ '<div class="link-content">' +
125
+ '<div class="se-btn-group">' +
126
+ '<button type="button" data-command="update" tabindex="-1" class="se-tooltip">' +
127
+ icons.edit +
128
+ '<span class="se-tooltip-inner"><span class="se-tooltip-text">' + lang.controller.edit + '</span></span>' +
129
+ '</button>' +
130
+ '<button type="button" data-command="delete" tabindex="-1" class="se-tooltip">' +
131
+ icons.delete +
132
+ '<span class="se-tooltip-inner"><span class="se-tooltip-text">' + lang.controller.remove + '</span></span>' +
133
+ '</button>' +
134
+ '</div>' +
135
+ '</div>';
136
+
137
+ return link_btn;
138
+ },
139
+
140
+ // Disable url input when uploading files
141
+ _fileInputChange: function () {
142
+ if (!this.audioInputFile.value) {
143
+ this.audioUrlFile.removeAttribute('disabled');
144
+ this.preview.style.textDecoration = '';
145
+ } else {
146
+ this.audioUrlFile.setAttribute('disabled', true);
147
+ this.preview.style.textDecoration = 'line-through';
148
+ }
149
+ },
150
+
151
+ // Disable url input when uploading files
152
+ _removeSelectedFiles: function (urlInput, preview) {
153
+ this.value = '';
154
+ if (urlInput) {
155
+ urlInput.removeAttribute('disabled');
156
+ preview.style.textDecoration = '';
157
+ }
158
+ },
159
+
160
+ // create new audio tag
161
+ _createAudioTag: function () {
162
+ const oAudio = this.util.createElement('AUDIO');
163
+ this.plugins.audio._setTagAttrs.call(this, oAudio);
164
+
165
+ const w = this.context.audio._origin_w;
166
+ const h = this.context.audio._origin_h;
167
+ oAudio.setAttribute('origin-size', w + ',' + h);
168
+ oAudio.style.cssText = (w ? ('width:' + w + '; ') : '') + (h ? ('height:' + h + ';') : '');
169
+
170
+ return oAudio;
171
+ },
172
+
173
+ _setTagAttrs: function (element) {
174
+ element.setAttribute('controls', true);
175
+
176
+ const attrs = this.options.audioTagAttrs;
177
+ if (!attrs) return;
178
+
179
+ for (let key in attrs) {
180
+ if (!this.util.hasOwn(attrs, key)) continue;
181
+ element.setAttribute(key, attrs[key]);
182
+ }
183
+ },
184
+
185
+ _onLinkPreview: function (context, protocol, e) {
186
+ const value = e.target.value.trim();
187
+ context._linkValue = this.textContent = !value ? '' : (protocol && value.indexOf('://') === -1 && value.indexOf('#') !== 0) ? protocol + value : value.indexOf('://') === -1 ? '/' + value : value;
188
+ },
189
+
190
+ /**
191
+ * @Required @Override fileManager
192
+ */
193
+ fileTags: ['audio'],
194
+
195
+ /**
196
+ * @Override core, fileManager, resizing
197
+ * @description It is called from core.selectComponent.
198
+ * @param {Element} element Target element
199
+ */
200
+ select: function (element) {
201
+ this.plugins.audio.onModifyMode.call(this, element);
202
+ },
203
+
204
+ /**
205
+ * @Override fileManager, resizing
206
+ */
207
+ destroy: function (element) {
208
+ element = element || this.context.audio._element;
209
+ const container = this.util.getParentElement(element, this.util.isComponent) || element;
210
+ const dataIndex = element.getAttribute('data-index') * 1;
211
+
212
+ if (typeof this.functions.onAudioDeleteBefore === 'function' && (this.functions.onAudioDeleteBefore(element, container, dataIndex, this) === false)) return;
213
+
214
+ const focusEl = (container.previousElementSibling || container.nextElementSibling);
215
+ const emptyDiv = container.parentNode;
216
+ this.util.removeItem(container);
217
+ this.plugins.audio.init.call(this);
218
+ this.controllersOff();
219
+
220
+ if (emptyDiv !== this.context.element.wysiwyg) this.util.removeItemAllParents(emptyDiv, function (current) { return current.childNodes.length === 0; }, null);
221
+
222
+ // focus
223
+ this.focusEdge(focusEl);
224
+
225
+ // fileManager event
226
+ this.plugins.fileManager.deleteInfo.call(this, 'audio', dataIndex, this.functions.onAudioUpload);
227
+
228
+ // history stack
229
+ this.history.push(false);
230
+ },
231
+
232
+ /**
233
+ * @Override fileManager
234
+ */
235
+ checkFileInfo: function () {
236
+ this.plugins.fileManager.checkInfo.call(this, 'audio', ['audio'], this.functions.onAudioUpload, this.plugins.audio.updateCover.bind(this), false);
237
+ },
238
+
239
+ /**
240
+ * @Override fileManager
241
+ */
242
+ resetFileInfo: function () {
243
+ this.plugins.fileManager.resetInfo.call(this, 'audio', this.functions.onAudioUpload);
244
+ },
245
+
246
+ /**
247
+ * @Required @Override dialog
248
+ */
249
+ on: function (update) {
250
+ const contextAudio = this.context.audio;
251
+
252
+ if (!update) {
253
+ this.plugins.audio.init.call(this);
254
+ if (contextAudio.audioInputFile && this.options.audioMultipleFile) contextAudio.audioInputFile.setAttribute('multiple', 'multiple');
255
+ } else if (contextAudio._element) {
256
+ this.context.dialog.updateModal = true;
257
+ contextAudio._linkValue = contextAudio.preview.textContent = contextAudio.audioUrlFile.value = contextAudio._element.src;
258
+ if (contextAudio.audioInputFile && this.options.audioMultipleFile) contextAudio.audioInputFile.removeAttribute('multiple');
259
+ } else {
260
+ if (contextAudio.audioInputFile && this.options.audioMultipleFile) contextAudio.audioInputFile.removeAttribute('multiple');
261
+ }
262
+ },
263
+
264
+ /**
265
+ * @Required @Override dialog
266
+ */
267
+ open: function () {
268
+ this.plugins.dialog.open.call(this, 'audio', 'audio' === this.currentControllerName);
269
+ },
270
+
271
+ submit: function (e) {
272
+ const contextAudio = this.context.audio;
273
+
274
+ e.preventDefault();
275
+ e.stopPropagation();
276
+
277
+ try {
278
+ if (contextAudio.audioInputFile && contextAudio.audioInputFile.files.length > 0) {
279
+ this.showLoading();
280
+ this.plugins.audio.submitAction.call(this, contextAudio.audioInputFile.files);
281
+ } else if (contextAudio.audioUrlFile && contextAudio._linkValue.length > 0) {
282
+ this.showLoading();
283
+ this.plugins.audio.setupUrl.call(this, contextAudio._linkValue);
284
+ }
285
+ } catch (error) {
286
+ this.closeLoading();
287
+ throw Error('[SUNEDITOR.audio.submit.fail] cause : "' + error.message + '"');
288
+ } finally {
289
+ this.plugins.dialog.close.call(this);
290
+ }
291
+
292
+ return false;
293
+ },
294
+
295
+ submitAction: function (fileList) {
296
+ if (fileList.length === 0) return;
297
+
298
+ let fileSize = 0;
299
+ let files = [];
300
+ for (let i = 0, len = fileList.length; i < len; i++) {
301
+ if (/audio/i.test(fileList[i].type)) {
302
+ files.push(fileList[i]);
303
+ fileSize += fileList[i].size;
304
+ }
305
+ }
306
+
307
+ const limitSize = this.options.audioUploadSizeLimit;
308
+ if (limitSize > 0) {
309
+ let infoSize = 0;
310
+ const audiosInfo = this.context.audio._infoList;
311
+ for (let i = 0, len = audiosInfo.length; i < len; i++) {
312
+ infoSize += audiosInfo[i].size * 1;
313
+ }
314
+
315
+ if ((fileSize + infoSize) > limitSize) {
316
+ this.closeLoading();
317
+ const err = '[SUNEDITOR.audioUpload.fail] Size of uploadable total audios: ' + (limitSize/1000) + 'KB';
318
+ if (typeof this.functions.onAudioUploadError !== 'function' || this.functions.onAudioUploadError(err, { 'limitSize': limitSize, 'currentSize': infoSize, 'uploadSize': fileSize }, this)) {
319
+ this.functions.noticeOpen(err);
320
+ }
321
+ return;
322
+ }
323
+ }
324
+
325
+ const contextAudio = this.context.audio;
326
+ contextAudio._uploadFileLength = files.length;
327
+
328
+ const info = {
329
+ isUpdate: this.context.dialog.updateModal,
330
+ element: contextAudio._element
331
+ };
332
+
333
+ if (typeof this.functions.onAudioUploadBefore === 'function') {
334
+ const result = this.functions.onAudioUploadBefore(files, info, this, function (data) {
335
+ if (data && this._w.Array.isArray(data.result)) {
336
+ this.plugins.audio.register.call(this, info, data);
337
+ } else {
338
+ this.plugins.audio.upload.call(this, info, data);
339
+ }
340
+ }.bind(this));
341
+
342
+ if (typeof result === 'undefined') return;
343
+ if (!result) {
344
+ this.closeLoading();
345
+ return;
346
+ }
347
+ if (typeof result === 'object' && result.length > 0) files = result;
348
+ }
349
+
350
+ this.plugins.audio.upload.call(this, info, files);
351
+ },
352
+
353
+ error: function (message, response) {
354
+ this.closeLoading();
355
+ if (typeof this.functions.onAudioUploadError !== 'function' || this.functions.onAudioUploadError(message, response, this)) {
356
+ this.functions.noticeOpen(message);
357
+ throw Error('[SUNEDITOR.plugin.audio.exception] response: ' + message);
358
+ }
359
+ },
360
+
361
+ upload: function (info, files) {
362
+ if (!files) {
363
+ this.closeLoading();
364
+ return;
365
+ }
366
+ if (typeof files === 'string') {
367
+ this.plugins.audio.error.call(this, files, null);
368
+ return;
369
+ }
370
+
371
+ const audioUploadUrl = this.options.audioUploadUrl;
372
+ const filesLen = this.context.dialog.updateModal ? 1 : files.length;
373
+
374
+ // create formData
375
+ const formData = new FormData();
376
+ for (let i = 0; i < filesLen; i++) {
377
+ formData.append('file-' + i, files[i]);
378
+ }
379
+
380
+ // server upload
381
+ this.plugins.fileManager.upload.call(this, audioUploadUrl, this.options.audioUploadHeader, formData, this.plugins.audio.callBack_upload.bind(this, info), this.functions.onAudioUploadError);
382
+ },
383
+
384
+ callBack_upload: function (info, xmlHttp) {
385
+ if (typeof this.functions.audioUploadHandler === 'function') {
386
+ this.functions.audioUploadHandler(xmlHttp, info, this);
387
+ } else {
388
+ const response = JSON.parse(xmlHttp.responseText);
389
+ if (response.errorMessage) {
390
+ this.plugins.audio.error.call(this, response.errorMessage, response);
391
+ } else {
392
+ this.plugins.audio.register.call(this, info, response);
393
+ }
394
+ }
395
+ },
396
+
397
+ register: function (info, response) {
398
+ const fileList = response.result;
399
+
400
+ for (let i = 0, len = fileList.length, file, oAudio; i < len; i++) {
401
+ if (info.isUpdate) oAudio = info.element;
402
+ else oAudio = this.plugins.audio._createAudioTag.call(this);
403
+
404
+ file = { name: fileList[i].name, size: fileList[i].size };
405
+ this.plugins.audio.create_audio.call(this, oAudio, fileList[i].url, file, info.isUpdate);
406
+ }
407
+
408
+ this.closeLoading();
409
+ },
410
+
411
+ setupUrl: function (src) {
412
+ try {
413
+ if (src.length === 0) return false;
414
+ this.plugins.audio.create_audio.call(this, this.plugins.audio._createAudioTag.call(this), src, null, this.context.dialog.updateModal);
415
+ } catch (error) {
416
+ throw Error('[SUNEDITOR.audio.audio.fail] cause : "' + error.message + '"');
417
+ } finally {
418
+ this.closeLoading();
419
+ }
420
+ },
421
+
422
+ create_audio: function (element, src, file, isUpdate) {
423
+ const contextAudio = this.context.audio;
424
+
425
+ // create new tag
426
+ if (!isUpdate) {
427
+ element.src = src;
428
+ const cover = this.plugins.component.set_cover.call(this, element);
429
+ const container = this.plugins.component.set_container.call(this, cover, '');
430
+ if (!this.insertComponent(container, false, true, !this.options.mediaAutoSelect)) {
431
+ this.focus();
432
+ return;
433
+ }
434
+ if (!this.options.mediaAutoSelect) {
435
+ const line = this.appendFormatTag(container, null);
436
+ if (line) this.setRange(line, 0, line, 0);
437
+ }
438
+ } // update
439
+ else {
440
+ if (contextAudio._element) element = contextAudio._element;
441
+ if (element && element.src !== src) {
442
+ element.src = src;
443
+ this.selectComponent(element, 'audio');
444
+ } else {
445
+ this.selectComponent(element, 'audio');
446
+ return;
447
+ }
448
+ }
449
+
450
+ this.plugins.fileManager.setInfo.call(this, 'audio', element, this.functions.onAudioUpload, file, false);
451
+ if (isUpdate) this.history.push(false);
452
+ },
453
+
454
+ updateCover: function (element) {
455
+ const contextAudio = this.context.audio;
456
+ this.plugins.audio._setTagAttrs.call(this, element);
457
+
458
+ const elementParent = element.parentNode;
459
+ let existElement = (this.util.isRangeFormatElement(elementParent) || this.util.isWysiwygDiv(elementParent)) ? element : elementParent || element;
460
+
461
+ // clone element
462
+ const prevElement = element;
463
+ contextAudio._element = element = element.cloneNode(false);
464
+ const cover = this.plugins.component.set_cover.call(this, element);
465
+ const container = this.plugins.component.set_container.call(this, cover, 'se-audio-container');
466
+
467
+ try {
468
+ if (this.util.getParentElement(prevElement, this.util.isNotCheckingNode)) {
469
+ prevElement.parentNode.replaceChild(container, prevElement);
470
+ } else if (this.util.isListCell(existElement)) {
471
+ const refer = this.util.getParentElement(prevElement, function (current) { return current.parentNode === existElement; });
472
+ existElement.insertBefore(container, refer);
473
+ this.util.removeItem(prevElement);
474
+ this.util.removeEmptyNode(refer, null, true);
475
+ } else if (this.util.isFormatElement(existElement)) {
476
+ const refer = this.util.getParentElement(prevElement, function (current) { return current.parentNode === existElement; });
477
+ existElement = this.util.splitElement(existElement, refer);
478
+ existElement.parentNode.insertBefore(container, existElement);
479
+ this.util.removeItem(prevElement);
480
+ this.util.removeEmptyNode(existElement, null, true);
481
+ if (existElement.children.length === 0) existElement.innerHTML = this.util.htmlRemoveWhiteSpace(existElement.innerHTML);
482
+ } else {
483
+ existElement.parentNode.replaceChild(container, existElement);
484
+ }
485
+ } catch (error) {
486
+ console.warn('[SUNEDITOR.audio.error] Maybe the audio tag is nested.', error);
487
+ }
488
+
489
+ this.plugins.fileManager.setInfo.call(this, 'audio', element, this.functions.onAudioUpload, null, false);
490
+ this.plugins.audio.init.call(this);
491
+ },
492
+
493
+ /**
494
+ * @Required @Override fileManager, resizing
495
+ */
496
+ onModifyMode: function (selectionTag) {
497
+ const contextAudio = this.context.audio;
498
+
499
+ this.setControllerPosition(contextAudio.controller, selectionTag, 'bottom', {left: 0, top: 0});
500
+ this.controllersOn(contextAudio.controller, selectionTag, this.plugins.audio.onControllerOff.bind(this, selectionTag), 'audio');
501
+
502
+ this.util.addClass(selectionTag, 'active');
503
+ contextAudio._element = selectionTag;
504
+ contextAudio._cover = this.util.getParentElement(selectionTag, 'FIGURE');
505
+ contextAudio._container = this.util.getParentElement(selectionTag, this.util.isComponent);
506
+ },
507
+
508
+ /**
509
+ * @Required @Override fileManager, resizing
510
+ */
511
+ openModify: function (notOpen) {
512
+ if (this.context.audio.audioUrlFile) {
513
+ const contextAudio = this.context.audio;
514
+ contextAudio._linkValue = contextAudio.preview.textContent = contextAudio.audioUrlFile.value = contextAudio._element.src;
515
+ }
516
+ if (!notOpen) this.plugins.dialog.open.call(this, 'audio', true);
517
+ },
518
+
519
+ onClick_controller: function (e) {
520
+ e.stopPropagation();
521
+
522
+ const command = e.target.getAttribute('data-command');
523
+ if (!command) return;
524
+
525
+ e.preventDefault();
526
+
527
+ if (/update/.test(command)) {
528
+ this.plugins.audio.openModify.call(this, false);
529
+ }
530
+ else { /** delete */
531
+ this.plugins.audio.destroy.call(this, this.context.audio._element);
532
+ }
533
+
534
+ this.controllersOff();
535
+ },
536
+
537
+ onControllerOff: function (selectionTag) {
538
+ this.util.removeClass(selectionTag, 'active');
539
+ this.context.audio.controller.style.display = 'none';
540
+ },
541
+
542
+ /**
543
+ * @Required @Override dialog
544
+ */
545
+ init: function () {
546
+ if (this.context.dialog.updateModal) return;
547
+ const contextAudio = this.context.audio;
548
+
549
+ if (contextAudio.audioInputFile) contextAudio.audioInputFile.value = '';
550
+ if (contextAudio.audioUrlFile) contextAudio._linkValue = contextAudio.preview.textContent = contextAudio.audioUrlFile.value = '';
551
+ if (contextAudio.audioInputFile && contextAudio.audioUrlFile) {
552
+ contextAudio.audioUrlFile.removeAttribute('disabled');
553
+ contextAudio.preview.style.textDecoration = '';
554
+ }
555
+
556
+ contextAudio._element = null;
557
+ }
559
558
  };