/
EventHandler.js
540 lines (467 loc) · 17.5 KB
/
EventHandler.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
define([
'summernote/core/agent',
'summernote/core/func',
'summernote/core/dom',
'summernote/core/async',
'summernote/core/key',
'summernote/core/list',
'summernote/editing/History',
'summernote/module/Editor',
'summernote/module/Toolbar',
'summernote/module/Statusbar',
'summernote/module/Popover',
'summernote/module/Handle',
'summernote/module/Fullscreen',
'summernote/module/Codeview',
'summernote/module/DragAndDrop',
'summernote/module/Clipboard',
'summernote/module/LinkDialog',
'summernote/module/ImageDialog',
'summernote/module/HelpDialog'
], function (agent, func, dom, async, key, list, History,
Editor, Toolbar, Statusbar, Popover, Handle, Fullscreen, Codeview,
DragAndDrop, Clipboard, LinkDialog, ImageDialog, HelpDialog) {
/**
* @class EventHandler
*
* EventHandler
* - TODO: new instance per a editor
*/
var EventHandler = function () {
var self = this;
/**
* Modules
*/
var modules = this.modules = {
editor: new Editor(this),
toolbar: new Toolbar(this),
statusbar: new Statusbar(this),
popover: new Popover(this),
handle: new Handle(this),
fullscreen: new Fullscreen(this),
codeview: new Codeview(this),
dragAndDrop: new DragAndDrop(this),
clipboard: new Clipboard(this),
linkDialog: new LinkDialog(this),
imageDialog: new ImageDialog(this),
helpDialog: new HelpDialog(this)
};
/**
* invoke module's method
*
* @param {String} moduleAndMethod - ex) 'editor.redo'
* @param {...*} arguments - arguments of method
* @return {*}
*/
this.invoke = function () {
var moduleAndMethod = list.head(list.from(arguments));
var args = list.tail(list.from(arguments));
var splits = moduleAndMethod.split('.');
var hasSeparator = splits.length > 1;
var moduleName = hasSeparator && list.head(splits);
var methodName = hasSeparator ? list.last(splits) : list.head(splits);
var module = this.getModule(moduleName);
var method = module[methodName];
return method && method.apply(module, args);
};
/**
* returns module
*
* @param {String} moduleName - name of module
* @return {Module} - defaults is editor
*/
this.getModule = function (moduleName) {
return this.modules[moduleName] || this.modules.editor;
};
/**
* @param {jQuery} $holder
* @param {Object} callbacks
* @param {String} eventNamespace
* @returns {Function}
*/
var bindCustomEvent = this.bindCustomEvent = function ($holder, callbacks, eventNamespace) {
return function () {
var callback = callbacks[func.namespaceToCamel(eventNamespace, 'on')];
if (callback) {
callback.apply($holder[0], arguments);
}
return $holder.trigger('summernote.' + eventNamespace, arguments);
};
};
/**
* insert Images from file array.
*
* @private
* @param {Object} layoutInfo
* @param {File[]} files
*/
this.insertImages = function (layoutInfo, files) {
var $editor = layoutInfo.editor(),
$editable = layoutInfo.editable(),
$holder = layoutInfo.holder();
var callbacks = $editable.data('callbacks');
var options = $editor.data('options');
// If onImageUpload options setted
if (callbacks.onImageUpload) {
bindCustomEvent($holder, callbacks, 'image.upload')(files);
// else insert Image as dataURL
} else {
$.each(files, function (idx, file) {
var filename = file.name;
if (options.maximumImageFileSize && options.maximumImageFileSize < file.size) {
bindCustomEvent($holder, callbacks, 'image.upload.error')(options.langInfo.image.maximumFileSizeError);
} else {
async.readFileAsDataURL(file).then(function (sDataURL) {
modules.editor.insertImage($editable, sDataURL, filename);
}).fail(function () {
bindCustomEvent($holder, callbacks, 'image.upload.error')(options.langInfo.image.maximumFileSizeError);
});
}
});
}
};
var commands = {
/**
* @param {Object} layoutInfo
*/
showLinkDialog: function (layoutInfo) {
modules.linkDialog.show(layoutInfo);
},
/**
* @param {Object} layoutInfo
*/
showImageDialog: function (layoutInfo) {
modules.imageDialog.show(layoutInfo);
},
/**
* @param {Object} layoutInfo
*/
showHelpDialog: function (layoutInfo) {
modules.helpDialog.show(layoutInfo);
},
/**
* @param {Object} layoutInfo
*/
fullscreen: function (layoutInfo) {
modules.fullscreen.toggle(layoutInfo);
},
/**
* @param {Object} layoutInfo
*/
codeview: function (layoutInfo) {
modules.codeview.toggle(layoutInfo);
}
};
var hMousedown = function (event) {
//preventDefault Selection for FF, IE8+
if (dom.isImg(event.target)) {
event.preventDefault();
}
};
var hKeyupAndMouseup = function (event) {
var layoutInfo = dom.makeLayoutInfo(event.currentTarget || event.target);
modules.editor.removeBogus(layoutInfo.editable());
hToolbarAndPopoverUpdate(event);
};
/**
* update sytle info
* @param {Object} styleInfo
* @param {Object} layoutInfo
*/
this.updateStyleInfo = function (styleInfo, layoutInfo) {
if (!styleInfo) {
return;
}
var isAirMode = layoutInfo.editor().data('options').airMode;
if (!isAirMode) {
modules.toolbar.update(layoutInfo.toolbar(), styleInfo);
}
modules.popover.update(layoutInfo.popover(), styleInfo, isAirMode);
modules.handle.update(layoutInfo.handle(), styleInfo, isAirMode);
};
var hToolbarAndPopoverUpdate = function (event) {
var target = event.target;
// delay for range after mouseup
setTimeout(function () {
var layoutInfo = dom.makeLayoutInfo(target);
var styleInfo = modules.editor.currentStyle(target);
self.updateStyleInfo(styleInfo, layoutInfo);
}, 0);
};
var hScroll = function (event) {
var layoutInfo = dom.makeLayoutInfo(event.currentTarget || event.target);
//hide popover and handle when scrolled
modules.popover.hide(layoutInfo.popover());
modules.handle.hide(layoutInfo.handle());
};
var hToolbarAndPopoverMousedown = function (event) {
// prevent default event when insertTable (FF, Webkit)
var $btn = $(event.target).closest('[data-event]');
if ($btn.length) {
event.preventDefault();
}
};
var hToolbarAndPopoverClick = function (event) {
var $btn = $(event.target).closest('[data-event]');
if (!$btn.length) {
return;
}
var eventName = $btn.attr('data-event'),
value = $btn.attr('data-value'),
hide = $btn.attr('data-hide');
var layoutInfo = dom.makeLayoutInfo(event.target);
// before command: detect control selection element($target)
var $target;
if ($.inArray(eventName, ['resize', 'floatMe', 'removeMedia', 'imageShape']) !== -1) {
var $selection = layoutInfo.handle().find('.note-control-selection');
$target = $($selection.data('target'));
}
// If requested, hide the popover when the button is clicked.
// Useful for things like showHelpDialog.
if (hide) {
$btn.parents('.popover').hide();
}
if ($.isFunction($.summernote.pluginEvents[eventName])) {
$.summernote.pluginEvents[eventName](event, modules.editor, layoutInfo, value);
} else if (modules.editor[eventName]) { // on command
var $editable = layoutInfo.editable();
$editable.focus();
modules.editor[eventName]($editable, value, $target);
event.preventDefault();
} else if (commands[eventName]) {
commands[eventName].call(this, layoutInfo);
event.preventDefault();
}
// after command
if ($.inArray(eventName, ['backColor', 'foreColor']) !== -1) {
var options = layoutInfo.editor().data('options', options);
var module = options.airMode ? modules.popover : modules.toolbar;
module.updateRecentColor(list.head($btn), eventName, value);
}
hToolbarAndPopoverUpdate(event);
};
var PX_PER_EM = 18;
var hDimensionPickerMove = function (event, options) {
var $picker = $(event.target.parentNode); // target is mousecatcher
var $dimensionDisplay = $picker.next();
var $catcher = $picker.find('.note-dimension-picker-mousecatcher');
var $highlighted = $picker.find('.note-dimension-picker-highlighted');
var $unhighlighted = $picker.find('.note-dimension-picker-unhighlighted');
var posOffset;
// HTML5 with jQuery - e.offsetX is undefined in Firefox
if (event.offsetX === undefined) {
var posCatcher = $(event.target).offset();
posOffset = {
x: event.pageX - posCatcher.left,
y: event.pageY - posCatcher.top
};
} else {
posOffset = {
x: event.offsetX,
y: event.offsetY
};
}
var dim = {
c: Math.ceil(posOffset.x / PX_PER_EM) || 1,
r: Math.ceil(posOffset.y / PX_PER_EM) || 1
};
$highlighted.css({ width: dim.c + 'em', height: dim.r + 'em' });
$catcher.attr('data-value', dim.c + 'x' + dim.r);
if (3 < dim.c && dim.c < options.insertTableMaxSize.col) {
$unhighlighted.css({ width: dim.c + 1 + 'em'});
}
if (3 < dim.r && dim.r < options.insertTableMaxSize.row) {
$unhighlighted.css({ height: dim.r + 1 + 'em'});
}
$dimensionDisplay.html(dim.c + ' x ' + dim.r);
};
/**
* bind KeyMap on keydown
*
* @param {Object} layoutInfo
* @param {Object} keyMap
*/
this.bindKeyMap = function (layoutInfo, keyMap) {
var $editor = layoutInfo.editor();
var $editable = layoutInfo.editable();
$editable.on('keydown', function (event) {
var keys = [];
// modifier
if (event.metaKey) { keys.push('CMD'); }
if (event.ctrlKey && !event.altKey) { keys.push('CTRL'); }
if (event.shiftKey) { keys.push('SHIFT'); }
// keycode
var keyName = key.nameFromCode[event.keyCode];
if (keyName) {
keys.push(keyName);
}
var pluginEvent;
var keyString = keys.join('+');
var eventName = keyMap[keyString];
if (eventName) {
// FIXME Summernote doesn't support event pipeline yet.
// - Plugin -> Base Code
pluginEvent = $.summernote.pluginEvents[keyString];
if ($.isFunction(pluginEvent)) {
if (pluginEvent(event, modules.editor, layoutInfo)) {
return false;
}
}
pluginEvent = $.summernote.pluginEvents[eventName];
if ($.isFunction(pluginEvent)) {
pluginEvent(event, modules.editor, layoutInfo);
} else if (modules.editor[eventName]) {
modules.editor[eventName]($editable, $editor.data('options'));
event.preventDefault();
} else if (commands[eventName]) {
commands[eventName].call(this, layoutInfo);
event.preventDefault();
}
} else if (key.isEdit(event.keyCode)) {
modules.editor.afterCommand($editable);
}
});
};
/**
* attach eventhandler
*
* @param {Object} layoutInfo - layout Informations
* @param {Object} options - user options include custom event handlers
*/
this.attach = function (layoutInfo, options) {
// handlers for editable
if (options.shortcuts) {
this.bindKeyMap(layoutInfo, options.keyMap[agent.isMac ? 'mac' : 'pc']);
}
layoutInfo.editable().on('mousedown', hMousedown);
layoutInfo.editable().on('keyup mouseup', hKeyupAndMouseup);
layoutInfo.editable().on('scroll', hScroll);
// handler for clipboard
modules.clipboard.attach(layoutInfo, options);
// handler for handle and popover
modules.handle.attach(layoutInfo, options);
layoutInfo.popover().on('click', hToolbarAndPopoverClick);
layoutInfo.popover().on('mousedown', hToolbarAndPopoverMousedown);
// handler for drag and drop
modules.dragAndDrop.attach(layoutInfo, options);
// handlers for frame mode (toolbar, statusbar)
if (!options.airMode) {
// handler for toolbar
layoutInfo.toolbar().on('click', hToolbarAndPopoverClick);
layoutInfo.toolbar().on('mousedown', hToolbarAndPopoverMousedown);
// handler for statusbar
modules.statusbar.attach(layoutInfo, options);
}
// handler for table dimension
var $catcherContainer = options.airMode ? layoutInfo.popover() :
layoutInfo.toolbar();
var $catcher = $catcherContainer.find('.note-dimension-picker-mousecatcher');
$catcher.css({
width: options.insertTableMaxSize.col + 'em',
height: options.insertTableMaxSize.row + 'em'
}).on('mousemove', function (event) {
hDimensionPickerMove(event, options);
});
// save options on editor
layoutInfo.editor().data('options', options);
// ret styleWithCSS for backColor / foreColor clearing with 'inherit'.
if (!agent.isMSIE) {
// [workaround] for Firefox
// - protect FF Error: NS_ERROR_FAILURE: Failure
setTimeout(function () {
document.execCommand('styleWithCSS', 0, options.styleWithSpan);
}, 0);
}
// History
var history = new History(layoutInfo.editable());
layoutInfo.editable().data('NoteHistory', history);
// All editor status will be saved on editable with jquery's data
// for support multiple editor with singleton object.
layoutInfo.editable().data('callbacks', {
onInit: options.onInit,
onFocus: options.onFocus,
onBlur: options.onBlur,
onKeydown: options.onKeydown,
onKeyup: options.onKeyup,
onMousedown: options.onMousedown,
onEnter: options.onEnter,
onPaste: options.onPaste,
onBeforeCommand: options.onBeforeCommand,
onChange: options.onChange,
onImageUpload: options.onImageUpload,
onImageUploadError: options.onImageUploadError,
onMediaDelete: options.onMediaDelete,
onToolbarClick: options.onToolbarClick
});
var styleInfo = modules.editor.styleFromNode(layoutInfo.editable());
this.updateStyleInfo(styleInfo, layoutInfo);
};
/**
* attach jquery custom event
*
* @param {Object} layoutInfo - layout Informations
*/
this.attachCustomEvent = function (layoutInfo, options) {
var $holder = layoutInfo.holder();
var $editable = layoutInfo.editable();
var callbacks = $editable.data('callbacks');
$editable.focus(bindCustomEvent($holder, callbacks, 'focus'));
$editable.blur(bindCustomEvent($holder, callbacks, 'blur'));
$editable.keydown(function (event) {
if (event.keyCode === key.code.ENTER) {
bindCustomEvent($holder, callbacks, 'enter').call(this, event);
}
bindCustomEvent($holder, callbacks, 'keydown').call(this, event);
});
$editable.keyup(bindCustomEvent($holder, callbacks, 'keyup'));
$editable.on('mousedown', bindCustomEvent($holder, callbacks, 'mousedown'));
$editable.on('mouseup', bindCustomEvent($holder, callbacks, 'mouseup'));
$editable.on('scroll', bindCustomEvent($holder, callbacks, 'scroll'));
$editable.on('paste', bindCustomEvent($holder, callbacks, 'paste'));
// [workaround] IE doesn't have input events for contentEditable
// - see: https://goo.gl/4bfIvA
var changeEventName = agent.isMSIE ? 'DOMCharacterDataModified DOMSubtreeModified DOMNodeInserted' : 'input';
$editable.on(changeEventName, function () {
bindCustomEvent($holder, callbacks, 'change')($editable.html(), $editable);
});
if (!options.airMode) {
layoutInfo.toolbar().click(bindCustomEvent($holder, callbacks, 'toolbar.click'));
layoutInfo.popover().click(bindCustomEvent($holder, callbacks, 'popover.click'));
}
// Textarea: auto filling the code before form submit.
if (dom.isTextarea(list.head($holder))) {
$holder.closest('form').submit(function (e) {
layoutInfo.holder().val(layoutInfo.holder().code());
bindCustomEvent($holder, callbacks, 'submit').call(this, e, $holder.code());
});
}
// textarea auto sync
if (dom.isTextarea(list.head($holder)) && options.textareaAutoSync) {
$holder.on('summernote.change', function () {
layoutInfo.holder().val(layoutInfo.holder().code());
});
}
// fire init event
bindCustomEvent($holder, callbacks, 'init')(layoutInfo);
// fire plugin init event
for (var i = 0, len = $.summernote.plugins.length; i < len; i++) {
if ($.isFunction($.summernote.plugins[i].init)) {
$.summernote.plugins[i].init(layoutInfo);
}
}
};
this.detach = function (layoutInfo, options) {
layoutInfo.holder().off();
layoutInfo.editable().off();
layoutInfo.popover().off();
layoutInfo.handle().off();
layoutInfo.dialog().off();
if (!options.airMode) {
layoutInfo.dropzone().off();
layoutInfo.toolbar().off();
layoutInfo.statusbar().off();
}
};
};
return EventHandler;
});