This repository has been archived by the owner on Jan 6, 2021. It is now read-only.
forked from taalendigitaal/summernote-audio
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathsummernote-file.js
332 lines (293 loc) · 10.3 KB
/
summernote-file.js
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/**
* Copyright 2019 [nobsod | Mathieu Coingt].
* Website: https://www.nobsod.fr
* Email: dev@nobsod.fr
* License: MIT
*
* Fork from summernote-audio : /~https://github.com/taalendigitaal/summernote-audio
*/
(function (factory) {
/* Global define */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(window.jQuery);
}
}(function ($) {
$.extend(true, $.summernote.lang, {
'en-US': {
file: {
file: 'File',
btn: 'File',
insert: 'Insert File',
selectFromFiles: 'Select from files',
url: 'File URL',
maximumFileSize: 'Maximum file size',
maximumFileSizeError: 'Maximum file size exceeded.'
}
},
'fr-FR': {
file: {
file: 'Fichier',
btn: 'Fichier',
insert: 'Insérer un fichier',
selectFromFiles: 'Sélectionner depuis les fichiers',
url: 'URL du fichier',
maximumFileSize: 'Poids maximum du fichier',
maximumFileSizeError: 'Poids maximum dépassé.'
}
},
'cs-CZ': {
file: {
file: 'Soubor',
btn: 'Soubor',
insert: 'Vložit soubor',
selectFromFiles: 'Vybrat ze souborů',
url: 'URL souboru',
maximumFileSize: 'Maximální velikost souboru',
maximumFileSizeError: 'Maximální velikost souboru překročena.'
}
},
});
$.extend($.summernote.options, {
file: {
icon: '<i class="note-icon-file"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" width="14px" height="14px"><path d="M 16 3.59375 L 15.28125 4.28125 L 8.28125 11.28125 L 9.71875 12.71875 L 15 7.4375 L 15 24 L 17 24 L 17 7.4375 L 22.28125 12.71875 L 23.71875 11.28125 L 16.71875 4.28125 L 16 3.59375 z M 7 26 L 7 28 L 25 28 L 25 26 L 7 26 z" style="fill:none;stroke:#111111;stroke-width:3;stroke-linecap:round;"/></svg></i>'
},
callbacks: {
onFileUpload: null,
onFileUploadError: null,
onFileLinkInsert: null
}
});
$.extend($.summernote.plugins, {
/**
* @param {Object} context - context object has status of editor.
*/
'file': function (context) {
let self = this,
// ui has renders to build ui elements
// for e.g. you can create a button with 'ui.button'
ui = $.summernote.ui,
$note = context.layoutInfo.note,
// contentEditable element
$editor = context.layoutInfo.editor,
$editable = context.layoutInfo.editable,
$toolbar = context.layoutInfo.toolbar,
// options holds the Options Information from Summernote and what we extended above.
options = context.options,
// lang holds the Language Information from Summernote and what we extended above.
lang = options.langInfo;
context.memo('button.file', function () {
// Here we create a button
let button = ui.button({
// icon for button
contents: options.file.icon,
// tooltip for button
tooltip: lang.file.file,
click: function (e) {
context.invoke('file.show');
}
});
return button.render();
});
this.initialize = function () {
// This is how we can add a Modal Dialog to allow users to interact with the Plugin.
// get the correct container for the plugin how it's attached to the document DOM.
let $container = options.dialogsInBody ? $(document.body) : $editor;
let fileLimitation = '';
if (options.maximumFileSize) {
let unit = Math.floor(Math.log(options.maximumFileSize) / Math.log(1024));
let readableSize = (options.maximumFileSize / Math.pow(1024, unit)).toFixed(2) * 1 +
' ' + ' KMGTP'[unit] + 'B';
fileLimitation = '<small>' + lang.file.maximumFileSize + ' : ' + readableSize + '</small>';
}
// Build the Body HTML of the Dialog.
let body = [
'<div class="form-group note-form-group note-group-select-from-files">',
'<label class="note-form-label">' + lang.file.selectFromFiles + '</label>',
'<input class="note-file-input note-form-control note-input" ',
' type="file" name="files" accept="*/*">',
'</div>',
fileLimitation,
'<div class="form-group note-group-image-url" style="overflow:auto;">',
'<label class="note-form-label">' + lang.file.url + '</label>',
'<input class="note-file-url form-control note-form-control note-input ',
' col-md-12" type="text">',
'</div>'
].join('');
// Build the Footer HTML of the Dialog.
let footer = '<button href="#" class="btn btn-primary note-file-btn">' + lang.file.insert + '</button>';
this.$dialog = ui.dialog({
// Set the title for the Dialog. Note: We don't need to build the markup for the Modal
// Header, we only need to set the Title.
title: lang.file.insert,
// Set the Body of the Dialog.
body: body,
// Set the Footer of the Dialog.
footer: footer
// This adds the Modal to the DOM.
}).render().appendTo($container);
};
this.destroy = function () {
ui.hideDialog(this.$dialog);
this.$dialog.remove();
};
this.bindEnterKey = function ($input, $btn) {
$input.on('keypress', function (event) {
if (event.keyCode === 13)
$btn.trigger('click');
});
};
this.bindLabels = function () {
self.$dialog.find('.form-control:first').focus().select();
self.$dialog.find('label').on('click', function () {
$(this).parent().find('.form-control:first').focus();
});
};
/**
* @method readFileAsDataURL
*
* read contents of file as representing URL
*
* @param {File} file
* @return {Promise} - then: dataUrl
*
* @todo this method already exists in summernote.js so we should use that one
*/
this.readFileAsDataURL = function (file) {
return $.Deferred(function (deferred) {
$.extend(new FileReader(), {
onload: function (e) {
let dataURL = e.target.result;
deferred.resolve(dataURL);
},
onerror: function (err) {
deferred.reject(err);
}
}).readAsDataURL(file);
}).promise();
};
this.createFile = function (url) {
// IMG url patterns (jpg, jpeg, png, gif, svg, webp)
let imgRegExp = /^.+.(jpg|jpeg|png|gif|svg|webp)$/;
let imgBase64RegExp = /^data:(image\/jpeg|image\/png|image\/gif|image\/svg|image\/webp).+$/;
// AUDIO url patterns (mp3, ogg, oga)
let audioRegExp = /^.+.(mp3|ogg|oga)$/;
let audioBase64RegExp = /^data:(audio\/mpeg|audio\/ogg).+$/;
// VIDEO url patterns (mp4, ogc, webm)
let videoRegExp = /^.+.(mp4|ogv|webm)$/;
let videoBase64RegExp = /^data:(video\/mpeg|video\/mp4|video\/ogv|video\/webm).+$/;
let $file;
if (url.match(imgRegExp) || url.match(imgBase64RegExp)) {
$file = $('<img>')
.attr('src', url)
;
} else if (url.match(audioRegExp) || url.match(audioBase64RegExp)) {
$file = $('<audio controls>')
.attr('src', url)
;
} else if (url.match(videoRegExp) || url.match(videoBase64RegExp)) {
$file = $('<video controls>')
.attr('src', url)
;
} else {
//We can't use this type of file. You have to implement onFileUpload into your Summernote
console.log('File type not supported. Please define "onFileUpload" callback in Summernote.');
return false;
}
$file.addClass('note-file-clip');
return $file;
};
this.insertFile = function (src, param) {
let $file = self.createFile(src);
if (!$file) {
context.triggerEvent('file.upload.error');
}
context.invoke('editor.beforeCommand');
if (typeof param === 'string') {
$file.attr('data-filename', param);
}
$file.show();
context.invoke('editor.insertNode', $file[0]);
context.invoke('editor.afterCommand');
};
this.insertFilesAsDataURL = function (files) {
$.each(files, function (idx, file) {
let filename = file.name;
if (options.maximumFileSize && options.maximumFileSize < file.size) {
context.triggerEvent('file.upload.error', lang.file.maximumFileSizeError);
} else {
self.readFileAsDataURL(file).then(function (dataURL) {
return self.insertFile(dataURL, filename);
}).fail(function () {
context.triggerEvent('file.upload.error');
});
}
});
};
this.show = function (data) {
context.invoke('editor.saveRange');
this.showFileDialog().then(function (data) {
// [workaround] hide dialog before restore range for IE range focus
ui.hideDialog(self.$dialog);
context.invoke('editor.restoreRange');
if (typeof data === 'string') { // file url
// If onFileLinkInsert set
if (options.callbacks.onFileLinkInsert) {
context.triggerEvent('file.link.insert', data);
} else {
self.insertFile(data);
}
} else { // array of files
// If onFileUpload set
if (options.callbacks.onFileUpload) {
context.triggerEvent('file.upload', data);
} else {
// else insert File as dataURL
self.insertFilesAsDataURL(data);
}
}
}).fail(function () {
context.invoke('editor.restoreRange');
});
};
this.showFileDialog = function () {
return $.Deferred(function (deferred) {
let $fileInput = self.$dialog.find('.note-file-input');
let $fileUrl = self.$dialog.find('.note-file-url');
let $fileBtn = self.$dialog.find('.note-file-btn');
ui.onDialogShown(self.$dialog, function () {
context.triggerEvent('dialog.shown');
// Cloning FileInput to clear element.
$fileInput.replaceWith($fileInput.clone().on('change', function (event) {
deferred.resolve(event.target.files || event.target.value);
}).val(''));
$fileBtn.click(function (e) {
e.preventDefault();
deferred.resolve($fileUrl.val());
});
$fileUrl.on('keyup paste', function () {
let url = $fileUrl.val();
ui.toggleBtn($fileBtn, url);
}).val('');
self.bindEnterKey($fileUrl, $fileBtn);
self.bindLabels();
});
ui.onDialogHidden(self.$dialog, function () {
$fileInput.off('change');
$fileUrl.off('keyup paste keypress');
$fileBtn.off('click');
if (deferred.state() === 'pending')
deferred.reject();
});
ui.showDialog(self.$dialog);
});
};
}
});
}));