forked from RichardSquires/summernote-link-editor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsummernote-linkEditor.js
452 lines (430 loc) · 23.5 KB
/
summernote-linkEditor.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/* https://github.com/RichardSquires/summernote-link-editor
* Version: 1.0.0
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require('jquery'));
} else {
factory(window.jQuery);
}
}(function ($) {
$.extend(true, $.summernote.lang, {
'en-US': {
linkEditor: {
email: "Email",
address: "Email address",
subject: "Subject line",
body: "Body text"
},
'de-DE': {
linkEditor: {
email: "E-Mail",
address: "E-Mail Adresse",
subject: "Betreffzeile",
body: "Nachricht (kurzer Text)"
}
},
}
});
$.extend($.summernote.options, {
linkEditor: {
tabsEnabled: ['link', 'email']
}
});
$.extend($.summernote.plugins, {
'linkEditor': function (context) {
var self = this,
ui = $.summernote.ui,
$note = context.layoutInfo.note,
$editor = context.layoutInfo.editor,
$editable = context.layoutInfo.editable,
options = context.options,
lang = options.langInfo;
/*override keyboard mapping for links*/
if(shouldInitialize()) {
options.keyMap.pc['CTRL+K'] = 'linkEditor.show';
options.keyMap.mac['CMD+K'] = 'linkEditor.show';
}
context.memo('button.advLink', function () {
var button = ui.button({
contents: ui.icon(options.icons.link),
container: context.layoutInfo.editor[0],/*fix to allow tooltip to show*/
tooltip: lang.link.link,
click: function () {
context.invoke('linkEditor.show');
}
});
return button.render();
});
context.memo('button.editAdvLink', function () {
var button = ui.button({
contents: ui.icon(options.icons.link),
container: context.layoutInfo.editor[0],/*fix to allow tooltip to show*/
tooltip: lang.link.edit,
click: function () {
context.invoke('linkEditor.show');
}
});
return button.render();
});
this.initialize = function () {
var showLinkTab = options.linkEditor.tabsEnabled.indexOf("link") > -1;
var showEmailTab = options.linkEditor.tabsEnabled.indexOf("email") > -1;
/*create dialog*/
var $container = options.dialogsInBody ? $(document.body) : $editor;
/*header*/
var header =
/*dialog nav*/
'<div class="note-header">' +
'<ul class="nav note-nav nav-tabs note-nav-tabs">';
if(showLinkTab)
{
header += '<li class="nav-item note-nav-item note-nav-advlink-link">' +
'<a class="nav-link note-nav-link note-nav-advlink-link" data-toggle="link" href="JavaScript:void(null);">' + lang.link.insert + '</a>' +
'</li>';
}
if(showEmailTab)
{
header += '<li class="nav-item note-nav-item note-nav-advlink-email">' +
'<a class="nav-link note-nav-link note-nav-advlink-email" data-toggle="email" href="JavaScript:void(null);">' + lang.linkEditor.email + '</a>' +
'</li>';
}
/*close header tags*/
header += '</ul>' +
'</div>';
/*dialog contents*/
var linkTab =
/*link tab*/
'<div class="tab-content note-tab-content note-tab-link">' +
/*link title attribute*/
'<div class="note-form-group form-group note-group-link-title">' +
'<div class="input-group note-input-group col-xs-12 col-sm-9">' +
'<label class="control-label note-form-label col-sm-3">' + lang.link.textToDisplay + '</label>' +
'<input class="note-link-title form-control note-form-control note-input" type="text">' +
'</div>' +
'</div>' +
/*link href*/
'<div class="note-form-group form-group note-group-link-href">' +
'<div class="input-group note-input-group col-xs-12 col-sm-9">' +
'<label class="control-label note-form-label col-xs-3">' + lang.link.url + '</label>' +
'<input class="note-link-href form-control note-form-control note-input" type="text">' +
'</div>' +
'</div>' +
/*link target open in new window checkbox*/
'<div class="note-form-group form-group note-group-link-target-protocol" >' +
'<div class="input-group note-input-group checkbox col-xs-12 col-sm-9">' +
'<label>' +
'<input class="note-link-target" role="checkbox" type="checkbox" checked="" aria-checked="true" />' +
lang.link.openInNewWindow +
'</label>' +
'</div>' +
/*protocol checkbox*/
'<div class="input-group note-input-group checkbox col-xs-12 col-sm-9">' +
'<label>' +
'<input class="note-link-protocol" role="checkbox" type="checkbox" checked="" aria-checked="true" />' +
lang.link.useProtocol +
'</label>' +
'</div>' +
'</div>' +
/* closing div for tab content*/
'</div>';
var emailTab =
/*email tab*/
'<div class="tab-content note-tab-content note-tab-email">' +
/*email address*/
'<div class="note-form-group form-group note-group-link-email">' +
'<div class="input-group note-input-group col-xs-12 col-sm-9">' +
'<label class="control-label note-form-label col-sm-3">' + lang.linkEditor.address + '</label>' +
'<input class="note-link-email form-control note-form-control note-input" type="text">' +
'</div>' +
'</div>' +
/*email subject*/
'<div class="note-form-group form-group note-group-link-subject">' +
'<div class="input-group note-input-group col-xs-12 col-sm-9">' +
'<label class="control-label note-form-label col-xs-3">' + lang.linkEditor.subject + '</label>' +
'<input class="note-link-subject form-control note-form-control note-input" type="text">' +
'</div>' +
'</div>' +
/*email body*/
'<div class="note-form-group form-group note-group-link-target-protocol" >' +
'<div class="input-group note-input-group checkbox col-xs-12 col-sm-9">' +
'<label class="control-label note-form-label col-xs-3">' + lang.linkEditor.body + '</label>' +
'<textarea class="note-link-body form-control note-form-control note-input" style="resize: none;" rows="6">' +
'</textarea>' +
'</div>' +
'</div>' +
/* closing div for tab content*/
'</div>';
var body = "";
if(showLinkTab) { body += linkTab; }
if(showEmailTab) { body += emailTab; }
/*footer button*/
var footerButton = '<button href="#" class="btn btn-primary note-btn note-btn-primary note-advLink-btn" disabled>' + lang.link.insert + '</button>';
/*dialog*/
this.$dialog = ui.dialog({
className: 'adv-link-dialog',
title: lang.link.insert,
body: header + body,
footer: footerButton,
callback: function($dialog) {
var $tabs = $dialog.find('.note-tab-content');
var $links = $dialog.find('.note-nav-link');
/*hide all tabs */
$tabs.hide();
/*Tab switching function*/
var tabSwitch = function($link) {
var tab = $link.data('toggle');
$tabs.hide();
$tabs.removeClass('active-tab-link');
$links.removeClass('active');
var $selTab = $dialog.find('.note-tab-' + tab);
$selTab.show();
$selTab.addClass('active-tab-link');
$link.addClass('active');
};
$links.each(function(index, link) {
var $link = $(link);
/*run for first link */
if(index === 0) { tabSwitch($link); }
/*attach on click */
$link.click(function() {
tabSwitch($link);
});
});
/*generate email link on the fly */
var $title = $dialog.find('.note-link-title');
var $url = $dialog.find('.note-link-href');
var $email = $dialog.find('.note-link-email');
var $subject = $dialog.find('.note-link-subject');
var $body = $dialog.find('.note-link-body');
var autoFillUrl = function() {
/*check fields have been filled out */
if($email.val().length === 0
&& $subject.val().length === 0
&& $body.val().length === 0)
{
/*if not cancel processing and wipe any existing mailto link */
if($url.val().indexOf('mailto:') > -1) { $url.val(''); }
return;
}
var currentUrl = $url.val();
/*create link */
if(currentUrl == '' || !currentUrl.startsWith('mailto:')) {
currentUrl = 'mailto:'.concat(encodeURIComponent($email.val()));
if($subject.val().length > 0)
{
currentUrl += '?subject='.concat(encodeURIComponent($subject.val()));
}
if($body.val().length > 0)
{
var paramJoin = currentUrl.indexOf('?') > -1 ? '&' : '?';
currentUrl += paramJoin.concat(encodeURIComponent($body.val()));
}
} else {
/*else update link (preserves pasted ) */
/*update or add subject */
var subjectWithVal = 'subject='.concat(encodeURIComponent($subject.val()));
var subjectRegEx = /(subject=.*?.&)/i /*subject param has surrounding parameters */
var subjectAtEndRegEx = /(subject=.*?$)/i
if(subjectRegEx.test(currentUrl)) {
subjectWithVal = subjectWithVal.concat("&");
currentUrl = currentUrl.replace(subjectRegEx, subjectWithVal);
} else if (subjectAtEndRegEx.test(currentUrl)) {
currentUrl = currentUrl.replace(subjectAtEndRegEx, subjectWithVal);
} else if ($subject.val().length > 0) {
var joinChar = currentUrl.indexOf('?') === -1 ? '?' : '&';
currentUrl += joinChar.concat(subjectWithVal);
}
/*update or add body */
var bodyWithVal = 'body='.concat(encodeURIComponent($body.val()));
var bodyRegEx = /(body=.*?.&)/i; /*body param has surrounding parameters */
var bodyAtEndRegEx = /(body=.*?$)/i;
if(bodyRegEx.test(currentUrl)) {
bodyWithVal = bodyWithVal.concat("&");
currentUrl = currentUrl.replace(bodyRegEx, bodyWithVal);
} else if (bodyAtEndRegEx.test(currentUrl)) {
currentUrl = currentUrl.replace(bodyAtEndRegEx, bodyWithVal);
} else if ($body.val().length > 0) {
var joinChar = currentUrl.indexOf('?') === -1 ? '?' : '&';
currentUrl += joinChar.concat(bodyWithVal);
}
/*Update email */
var emailInLink = /(:.*?\?)//*email to has surrounding parameters */
var endChar = '?';
if (currentUrl.indexOf('?') == -1) {
/*email to has no parameters */
emailInLink = /(:.*)/
endChar = '';
}
currentUrl = currentUrl.replace(emailInLink, ":".concat(encodeURIComponent($email.val()),endChar))
}
$url.val(currentUrl);
/*Keep link text up to date with email if was empty to begin with*/
var newText = $email.val()
var oldText = $title.val();
if(!oldText
|| oldText === newText.substring(0,newText.length - 1)
|| oldText.substring(0, oldText.length - 1) === newText
) {
$title.val(newText);
}
/*All changes ready trigger validation for link text */
$title.trigger('input');
};
$email.on('input paste propertychange', autoFillUrl);
$subject.on('input paste propertychange', autoFillUrl);
$body.on('input paste propertychange', autoFillUrl);
/*bind url to title inputs and enable/disable save button*/
var checkFormValid = function() {
var isDisabled = $title.val().length === 0 || $url.val().length === 0
$dialog.find('.note-btn.note-advLink-btn').prop('disabled', isDisabled)
}
$url.on('input paste propertychange',function() {
var newText = $url.val()
var oldText = $title.val();
if(!$title.val()
|| $title.val() === newText.substring(0,newText.length - 1)
|| oldText.substring(0, oldText.length - 1) === newText
) {
$title.val(newText);
}
checkFormValid();
});
$url.on('input paste propertychange',function() {
var newText = $url.val()
var oldText = $title.val();
if(!oldText
|| oldText === newText.substring(0,newText.length - 1)
|| oldText.substring(0, oldText.length - 1) === newText
) {
$title.val(newText);
}
checkFormValid();
});
$title.on('input paste propertychange', checkFormValid);
}
}).render().appendTo($container);
};
this.destroy = function () {
ui.hideDialog(this.$dialog);
this.$dialog.remove();
};
this.bindEnterKey = function ($input, $btn) {
$input.on('keypress', function (e) {
if (e.keyCode === 13) $btn.trigger('click');
});
};
this.show = function () {
ui.showDialog(self.$dialog);
/**function from built-in link editor, structure returned:
* range (summernote range)
* text
* url
* isNewWindow (bool)
*/
var linkInfo = context.invoke('editor.getLinkInfo');
this.showImageAttributesDialog(linkInfo).then(function (linkInfo) {
ui.hideDialog(self.$dialog);
/*update editable area*/
/*validate url (mirrors standard linking for consistency - v0.8.18)*/
if (linkInfo.url && typeof linkInfo.url === 'string') {
linkInfo.url = linkInfo.url.trim();
if (options.onCreateLink) {
linkUrl = options.onCreateLink(linkUrl);
} else if(linkInfo.checkProtocol) {
/*if url doesn't have any protocol and not even a relative or a label, use http:// as default*/
linkInfo.url = /^([A-Za-z][A-Za-z0-9+-.]*\:|#|\/)/.test(linkInfo.url) ? linkInfo.url : options.defaultProtocol.concat(linkInfo.url);
}
}
/*Use existing summernote insertion API to update/create links
*(linkInfo needs to be consistent with system setup)
*/
$note.summernote('createLink', linkInfo);
});
};
this.showImageAttributesDialog = function (linkInfo) {
return $.Deferred(function (deferred) {
var $title = self.$dialog.find('.note-link-title'),
$url = self.$dialog.find('.note-link-href'),
$newWindow = self.$dialog.find('.note-link-target'),
$defaultProtocol = self.$dialog.find('.note-link-protocol'),
$email = self.$dialog.find('.note-link-email'),
$subject = self.$dialog.find('.note-link-subject'),
$body = self.$dialog.find('.note-link-body'),
$editBtn = self.$dialog.find('.note-advLink-btn');
/*dialog ui functions*/
ui.onDialogShown(self.$dialog, function () {
context.triggerEvent('dialog.shown');
$editBtn.off();
$editBtn.one('click', function (e) {
e.preventDefault();
deferred.resolve({
range: linkInfo.range,
text: $title.val(),
url: $url.val(),
isNewWindow: $newWindow.prop('checked'),
checkProtocol: $defaultProtocol.prop('checked')
}).then(function (linkInfo) {
/*synch changes*/
context.triggerEvent('change', $editable.html());
});
});
/*setup form data from html*/
$title.val(linkInfo.text);
if(linkInfo.url) {
$url.val(linkInfo.url);
$newWindow.prop('checked', linkInfo.isNewWindow);
/*process mailto link */
if(linkInfo.url.indexOf('mailto:') > -1) {
var urlParts = linkInfo.url.split(/(mailto:|\?|&)/gi);
for (let partIndex = 0; partIndex < urlParts.length; partIndex++) {
const part = urlParts[partIndex];
const testPart = part.toLowerCase().trim();
if(testPart === "mailto:") {
$email.val(decodeURIComponent(urlParts[partIndex+1]));
continue;
}
if(testPart.startsWith('subject=')) {
var subject = part.replace('subject=', '');
$subject.val(decodeURIComponent(subject));
continue;
}
if(testPart.startsWith('body=')) {
var body = part.replace('body=', '');
$body.val(decodeURIComponent(body));
continue;
}
}
}
} else {
/*if there is no link then reset form*/
$url.val("");
$newWindow.prop('checked', true);
$email.val("");
$subject.val("");
$body.val("");
}
/*Validate link editor form on load */
$title.trigger('input');
self.bindEnterKey($editBtn);
});
ui.onDialogHidden(self.$dialog, function () {
$editBtn.off('click');
if (deferred.state() === 'pending') deferred.reject();
});
ui.showDialog(self.$dialog);
});
};
//events for this plugin
this.events = {
}
/*check we have enough tabs to be useful*/
function shouldInitialize() {
return options.linkEditor && options.linkEditor.tabsEnabled && options.linkEditor.tabsEnabled.length > 0;
}
this.shouldInitialize = shouldInitialize;
}
});
}));