-
Notifications
You must be signed in to change notification settings - Fork 74
/
bindCommands.js
101 lines (95 loc) · 3.25 KB
/
bindCommands.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
'use strict';
var crossvent = require('crossvent');
var commands = {
markdown: {
boldOrItalic: require('./markdown/boldOrItalic'),
linkOrImageOrAttachment: require('./markdown/linkOrImageOrAttachment'),
blockquote: require('./markdown/blockquote'),
codeblock: require('./markdown/codeblock'),
heading: require('./markdown/heading'),
list: require('./markdown/list'),
hr: require('./markdown/hr')
},
html: {
boldOrItalic: require('./html/boldOrItalic'),
linkOrImageOrAttachment: require('./html/linkOrImageOrAttachment'),
blockquote: require('./html/blockquote'),
codeblock: require('./html/codeblock'),
heading: require('./html/heading'),
list: require('./html/list'),
hr: require('./html/hr')
}
};
commands.wysiwyg = commands.html;
function bindCommands (surface, options, editor) {
bind('bold', 'cmd+b', bold);
bind('italic', 'cmd+i', italic);
bind('quote', 'cmd+j', router('blockquote'));
bind('code', 'cmd+e', code);
bind('ol', 'cmd+o', ol);
bind('ul', 'cmd+u', ul);
bind('heading', 'cmd+d', router('heading'));
editor.showLinkDialog = fabricator(bind('link', 'cmd+k', linkOrImageOrAttachment('link')));
editor.showImageDialog = fabricator(bind('image', 'cmd+g', linkOrImageOrAttachment('image')));
editor.linkOrImageOrAttachment = linkOrImageOrAttachment;
if (options.attachments) {
editor.showAttachmentDialog = fabricator(bind('attachment', 'cmd+shift+k', linkOrImageOrAttachment('attachment')));
}
if (options.hr) { bind('hr', 'cmd+n', router('hr')); }
function fabricator (el) {
return function open () {
crossvent.fabricate(el, 'click');
};
}
function bold (mode, chunks) {
commands[mode].boldOrItalic(chunks, 'bold');
}
function italic (mode, chunks) {
commands[mode].boldOrItalic(chunks, 'italic');
}
function code (mode, chunks) {
commands[mode].codeblock(chunks, { fencing: options.fencing });
}
function ul (mode, chunks) {
commands[mode].list(chunks, false);
}
function ol (mode, chunks) {
commands[mode].list(chunks, true);
}
function linkOrImageOrAttachment (type, autoUpload) {
return function linkOrImageOrAttachmentInvoke (mode, chunks) {
commands[mode].linkOrImageOrAttachment.call(this, chunks, {
editor: editor,
mode: mode,
type: type,
surface: surface,
prompts: options.prompts,
upload: options[type + 's'],
classes: options.classes,
mergeHtmlAndAttachment: options.mergeHtmlAndAttachment || mergeHtmlAndAttachment,
autoUpload: autoUpload
});
};
}
function bind (id, combo, fn) {
return editor.addCommandButton(id, combo, suppress(fn));
}
function mergeHtmlAndAttachment (chunks, link) {
var linkText = chunks.selection || link.title;
return {
before: chunks.before,
selection: '<a href="' + link.href + '">' + linkText + '</a>',
after: chunks.after,
};
}
function router (method) {
return function routed (mode, chunks) { commands[mode][method].call(this, chunks); };
}
function stop (e) {
e.preventDefault(); e.stopPropagation();
}
function suppress (fn) {
return function suppressor (e, mode, chunks) { stop(e); fn.call(this, mode, chunks); };
}
}
module.exports = bindCommands;