-
Notifications
You must be signed in to change notification settings - Fork 6
/
summernote-ace-plugin.js
210 lines (188 loc) · 8.78 KB
/
summernote-ace-plugin.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
(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($) {
// Extends lang for code plugin.
$.extend(true, $.summernote.lang, {
'en-US': {
aceCodeEditor: {
aceCodeEditor: 'Code'
}
},
'zh-CN': {
aceCodeEditor: {
aceCodeEditor: '代码'
}
}
});
// Extends plugins for code plugin.
$.extend($.summernote.plugins, {
/**
* @param {Object} context - context object has status of editor.
*/
'aceCodeEditor': function(context) {
var self = this;
// ui has renders to build ui elements.
// - you can create a button with `ui.button`
var ui = $.summernote.ui;
var $editor = context.layoutInfo.editor;
var options = context.options;
var lang = options.langInfo;
this.editor = '';
this.getOpts = function() {
var opts = {
aceTheme: context.options.ace.aceTheme || 'ace/theme/twilight',
aceMode: context.options.ace.aceMode || 'c_cpp',
aceLineHeight: context.options.ace.aceLineHeight || '18px',
aceFontSize: context.options.ace.aceFontSize || '12px',
aceModeSelectorLabel: context.options.ace.aceModeSelectorLabel || 'select your language',
aceCodeInputAreaLabel: context.options.ace.aceCodeInputAreaLabel || 'input your code',
aceCodeSubmitBtnLabel: context.options.ace.aceCodeSubmitBtnLabel || 'Insert',
aceModalTitle: context.options.ace.aceModalTitle || 'Insert Code',
aceModes: context.options.ace.aceModes || [
'c/c++', 'java', 'javascript', 'perl', 'python', 'php', 'ruby',
'sh', 'golang', 'julia', 'rust', 'scala', 'haskell', 'lisp', 'lua', 'sql',
'coffee', 'typescript'
],
};
return opts;
};
this.showInsertCodeDialog = function(opts) {
this.editor.focus();
return $.Deferred(function(deferred) {
var $submitBtn = $('#aceInsertCode');
var $modeSelector = $('#modeSelector');
var mode = opts.aceMode;
var theme = opts.aceTheme;
var lineHeight = opts.aceLineHeight;
var fontSize = opts.aceFontSize;
ui.onDialogShown(self.$dialog, function() {
context.triggerEvent('dialog.shown');
$modeSelector.one('change', function(e) {
mode = e.target.value;
if (mode === 'c/c++') {
mode = 'c_cpp';
}
self.editor.session.setMode('ace/mode/' + mode);
});
$submitBtn.one('click', function(event) {
event.preventDefault();
deferred.resolve({
code: self.editor.getSession().getValue(),
aceMode: mode,
aceTheme: theme,
aceLineHeight: lineHeight,
aceFontSize: fontSize,
});
self.$dialog.modal('hide');
});
});
ui.onDialogHidden(self.$dialog, function() {
$submitBtn = null;
$modeSelector = null;
if (deferred.state() === 'pending') {
deferred.reject();
}
});
ui.showDialog(self.$dialog);
}).promise();
};
this.createNode = function(code) {
var $display = $('<pre />')
$display.html(code);
return $display[0];
};
this.show = function() {
var opts = self.getOpts();
context.invoke('editor.saveRange');
this.showInsertCodeDialog(opts).then(function(result) {
context.invoke('editor.restoreRange');
var codeNode = self.createNode(result.code);
var display = ace.edit(codeNode);
display.setTheme(result.aceTheme);
display.session.setMode('ace/mode/' + result.aceMode);
display.getSession().setValue(result.code);
display.getSession().setUseWorker(false);
display.setReadOnly(true);
display.$blockScrolling = Infinity;
display.setShowPrintMargin(false);
display.setHighlightActiveLine(false);
display.renderer.$cursorLayer.element.style.display = "none";
display.renderer.$gutterLineHighlight.style.display = "none";
display.renderer.$gutterLayer.$showFoldWidgets = false;
var lineNumber = display.session.getLength();
codeNode.style.lineHeight = result.aceLineHeight;
codeNode.style.fontSize = result.aceFontSize;
codeNode.style.height = parseInt(codeNode.style.lineHeight) * lineNumber + 5 + 'px';
context.invoke('editor.insertNode', codeNode);
});
};
// add code button
context.memo('button.aceCodeEditor', function() {
// create button
var button = ui.button({
contents: '<i class="fa fa-code"/>' + lang.aceCodeEditor.aceCodeEditor,
tooltip: lang.aceCodeEditor.aceCodeEditor,
click: function() {
self.show();
}
});
// create jQuery object from button instance.
var $code = button.render();
return $code;
});
this.createCodeDialog = function() {
var $box = $('<div />');
var $selectGroup = $('<div class="form-group" />');
var $textGroup = $('<div class="form-group" />');
var $select = $('<select class="form-control ace-plugin-select" id="modeSelector" />');
var _opts = this.getOpts();
var modes = _opts.aceModes;
for (var i = 0; i < modes.length; i++) {
$select.append('<option value="' + modes[i] + '">' + modes[i] + '</option>');
}
var $label = $('<label />');
$label.html(_opts.aceModeSelectorLabel);
$box.append($selectGroup.append($label));
$box.append($selectGroup.append($select));
var $label = $('<label />');
$label.html(_opts.aceCodeInputAreaLabel);
var $textarea = $('<div class="ace-plugin-code" id="aceCodeEditor" />');
$box.append($textGroup.append($label));
$box.append($textGroup.append($textarea));
return $box.html();
};
this.initialize = function() {
var opts = this.getOpts();
var $container = options.dialogsInBody ? $(document.body) : $editor;
var footer = '<button type="button" class="btn btn-primary" id="aceInsertCode">' +
opts.aceCodeSubmitBtnLabel + '</button>';
this.$dialog = ui.dialog({
className: 'code-dialog',
title: opts.aceModalTitle,
fade: options.dialogsFade,
body: self.createCodeDialog(),
footer: footer
}).render().appendTo($container);
this.editor = ace.edit('aceCodeEditor');
this.editor.setTheme(opts.aceTheme);
this.editor.session.setMode('ace/mode/' + opts.aceMode);
this.editor.$blockScrolling = Infinity;
this.editor.setShowPrintMargin(false);
};
this.destroy = function() {
ui.hideDialog(this.$dialog);
this.$dialog.remove();
};
}
});
}));