| 776 |
lars |
1 |
/* ===================================================
|
|
|
2 |
* bootstrap-markdown.js v2.9.0
|
|
|
3 |
* http://github.com/toopay/bootstrap-markdown
|
|
|
4 |
* ===================================================
|
|
|
5 |
* Copyright 2013-2015 Taufan Aditya
|
|
|
6 |
*
|
|
|
7 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
8 |
* you may not use this file except in compliance with the License.
|
|
|
9 |
* You may obtain a copy of the License at
|
|
|
10 |
*
|
|
|
11 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
12 |
*
|
|
|
13 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
14 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
15 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
16 |
* See the License for the specific language governing permissions and
|
|
|
17 |
* limitations under the License.
|
|
|
18 |
* ========================================================== */
|
|
|
19 |
|
|
|
20 |
!function ($) {
|
|
|
21 |
|
|
|
22 |
"use strict"; // jshint ;_;
|
|
|
23 |
|
|
|
24 |
/* MARKDOWN CLASS DEFINITION
|
|
|
25 |
* ========================== */
|
|
|
26 |
|
|
|
27 |
var Markdown = function (element, options) {
|
|
|
28 |
// @TODO : remove this BC on next major release
|
|
|
29 |
// @see : https://github.com/toopay/bootstrap-markdown/issues/109
|
|
|
30 |
var opts = ['autofocus', 'savable', 'hideable', 'width',
|
|
|
31 |
'height', 'resize', 'iconlibrary', 'language',
|
|
|
32 |
'footer', 'fullscreen', 'hiddenButtons', 'disabledButtons'];
|
|
|
33 |
$.each(opts,function(_, opt){
|
|
|
34 |
if (typeof $(element).data(opt) !== 'undefined') {
|
|
|
35 |
options = typeof options == 'object' ? options : {}
|
|
|
36 |
options[opt] = $(element).data(opt)
|
|
|
37 |
}
|
|
|
38 |
});
|
|
|
39 |
// End BC
|
|
|
40 |
|
|
|
41 |
// Class Properties
|
|
|
42 |
this.$ns = 'bootstrap-markdown';
|
|
|
43 |
this.$element = $(element);
|
|
|
44 |
this.$editable = {el:null, type:null,attrKeys:[], attrValues:[], content:null};
|
|
|
45 |
this.$options = $.extend(true, {}, $.fn.markdown.defaults, options, this.$element.data('options'));
|
|
|
46 |
this.$oldContent = null;
|
|
|
47 |
this.$isPreview = false;
|
|
|
48 |
this.$isFullscreen = false;
|
|
|
49 |
this.$editor = null;
|
|
|
50 |
this.$textarea = null;
|
|
|
51 |
this.$handler = [];
|
|
|
52 |
this.$callback = [];
|
|
|
53 |
this.$nextTab = [];
|
|
|
54 |
|
|
|
55 |
this.showEditor();
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
Markdown.prototype = {
|
|
|
59 |
|
|
|
60 |
constructor: Markdown
|
|
|
61 |
|
|
|
62 |
, __alterButtons: function(name,alter) {
|
|
|
63 |
var handler = this.$handler, isAll = (name == 'all'),that = this;
|
|
|
64 |
|
|
|
65 |
$.each(handler,function(k,v) {
|
|
|
66 |
var halt = true;
|
|
|
67 |
if (isAll) {
|
|
|
68 |
halt = false;
|
|
|
69 |
} else {
|
|
|
70 |
halt = v.indexOf(name) < 0;
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
if (halt === false) {
|
|
|
74 |
alter(that.$editor.find('button[data-handler="'+v+'"]'));
|
|
|
75 |
}
|
|
|
76 |
});
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
, __buildButtons: function(buttonsArray, container) {
|
|
|
80 |
var i,
|
|
|
81 |
ns = this.$ns,
|
|
|
82 |
handler = this.$handler,
|
|
|
83 |
callback = this.$callback;
|
|
|
84 |
|
|
|
85 |
for (i=0;i<buttonsArray.length;i++) {
|
|
|
86 |
// Build each group container
|
|
|
87 |
var y, btnGroups = buttonsArray[i];
|
|
|
88 |
for (y=0;y<btnGroups.length;y++) {
|
|
|
89 |
// Build each button group
|
|
|
90 |
var z,
|
|
|
91 |
buttons = btnGroups[y].data,
|
|
|
92 |
btnGroupContainer = $('<div/>', {
|
|
|
93 |
'class': 'btn-group'
|
|
|
94 |
});
|
|
|
95 |
|
|
|
96 |
for (z=0;z<buttons.length;z++) {
|
|
|
97 |
var button = buttons[z],
|
|
|
98 |
buttonContainer, buttonIconContainer,
|
|
|
99 |
buttonHandler = ns+'-'+button.name,
|
|
|
100 |
buttonIcon = this.__getIcon(button.icon),
|
|
|
101 |
btnText = button.btnText ? button.btnText : '',
|
|
|
102 |
btnClass = button.btnClass ? button.btnClass : 'btn',
|
|
|
103 |
tabIndex = button.tabIndex ? button.tabIndex : '-1',
|
|
|
104 |
hotkey = typeof button.hotkey !== 'undefined' ? button.hotkey : '',
|
|
|
105 |
hotkeyCaption = typeof jQuery.hotkeys !== 'undefined' && hotkey !== '' ? ' ('+hotkey+')' : '';
|
|
|
106 |
|
|
|
107 |
// Construct the button object
|
|
|
108 |
buttonContainer = $('<button></button>');
|
|
|
109 |
buttonContainer.text(' ' + this.__localize(btnText)).addClass('btn-default btn-sm').addClass(btnClass);
|
|
|
110 |
if(btnClass.match(/btn\-(primary|success|info|warning|danger|link)/)){
|
|
|
111 |
buttonContainer.removeClass('btn-default');
|
|
|
112 |
}
|
|
|
113 |
buttonContainer.attr({
|
|
|
114 |
'type': 'button',
|
|
|
115 |
'title': this.__localize(button.title) + hotkeyCaption,
|
|
|
116 |
'tabindex': tabIndex,
|
|
|
117 |
'data-provider': ns,
|
|
|
118 |
'data-handler': buttonHandler,
|
|
|
119 |
'data-hotkey': hotkey
|
|
|
120 |
});
|
|
|
121 |
if (button.toggle === true){
|
|
|
122 |
buttonContainer.attr('data-toggle', 'button');
|
|
|
123 |
}
|
|
|
124 |
buttonIconContainer = $('<span/>');
|
|
|
125 |
buttonIconContainer.addClass(buttonIcon);
|
|
|
126 |
buttonIconContainer.prependTo(buttonContainer);
|
|
|
127 |
|
|
|
128 |
// Attach the button object
|
|
|
129 |
btnGroupContainer.append(buttonContainer);
|
|
|
130 |
|
|
|
131 |
// Register handler and callback
|
|
|
132 |
handler.push(buttonHandler);
|
|
|
133 |
callback.push(button.callback);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
// Attach the button group into container dom
|
|
|
137 |
container.append(btnGroupContainer);
|
|
|
138 |
}
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
return container;
|
|
|
142 |
}
|
|
|
143 |
, __setListener: function() {
|
|
|
144 |
// Set size and resizable Properties
|
|
|
145 |
var hasRows = typeof this.$textarea.attr('rows') !== 'undefined',
|
|
|
146 |
maxRows = this.$textarea.val().split("\n").length > 5 ? this.$textarea.val().split("\n").length : '5',
|
|
|
147 |
rowsVal = hasRows ? this.$textarea.attr('rows') : maxRows;
|
|
|
148 |
|
|
|
149 |
this.$textarea.attr('rows',rowsVal);
|
|
|
150 |
if (this.$options.resize) {
|
|
|
151 |
this.$textarea.css('resize',this.$options.resize);
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
this.$textarea
|
|
|
155 |
.on('focus', $.proxy(this.focus, this))
|
|
|
156 |
.on('keypress', $.proxy(this.keypress, this))
|
|
|
157 |
.on('keyup', $.proxy(this.keyup, this))
|
|
|
158 |
.on('change', $.proxy(this.change, this))
|
|
|
159 |
.on('select', $.proxy(this.select, this));
|
|
|
160 |
|
|
|
161 |
if (this.eventSupported('keydown')) {
|
|
|
162 |
this.$textarea.on('keydown', $.proxy(this.keydown, this));
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
// Re-attach markdown data
|
|
|
166 |
this.$textarea.data('markdown',this);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
, __handle: function(e) {
|
|
|
170 |
var target = $(e.currentTarget),
|
|
|
171 |
handler = this.$handler,
|
|
|
172 |
callback = this.$callback,
|
|
|
173 |
handlerName = target.attr('data-handler'),
|
|
|
174 |
callbackIndex = handler.indexOf(handlerName),
|
|
|
175 |
callbackHandler = callback[callbackIndex];
|
|
|
176 |
|
|
|
177 |
// Trigger the focusin
|
|
|
178 |
$(e.currentTarget).focus();
|
|
|
179 |
|
|
|
180 |
callbackHandler(this);
|
|
|
181 |
|
|
|
182 |
// Trigger onChange for each button handle
|
|
|
183 |
this.change(this);
|
|
|
184 |
|
|
|
185 |
// Unless it was the save handler,
|
|
|
186 |
// focusin the textarea
|
|
|
187 |
if (handlerName.indexOf('cmdSave') < 0) {
|
|
|
188 |
this.$textarea.focus();
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
e.preventDefault();
|
|
|
192 |
}
|
|
|
193 |
|
|
|
194 |
, __localize: function(string) {
|
|
|
195 |
var messages = $.fn.markdown.messages,
|
|
|
196 |
language = this.$options.language;
|
|
|
197 |
if (
|
|
|
198 |
typeof messages !== 'undefined' &&
|
|
|
199 |
typeof messages[language] !== 'undefined' &&
|
|
|
200 |
typeof messages[language][string] !== 'undefined'
|
|
|
201 |
) {
|
|
|
202 |
return messages[language][string];
|
|
|
203 |
}
|
|
|
204 |
return string;
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
, __getIcon: function(src) {
|
|
|
208 |
return typeof src == 'object' ? src[this.$options.iconlibrary] : src;
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
, setFullscreen: function(mode) {
|
|
|
212 |
var $editor = this.$editor,
|
|
|
213 |
$textarea = this.$textarea;
|
|
|
214 |
|
|
|
215 |
if (mode === true) {
|
|
|
216 |
$editor.addClass('md-fullscreen-mode');
|
|
|
217 |
$('body').addClass('md-nooverflow');
|
|
|
218 |
this.$options.onFullscreen(this);
|
|
|
219 |
} else {
|
|
|
220 |
$editor.removeClass('md-fullscreen-mode');
|
|
|
221 |
$('body').removeClass('md-nooverflow');
|
|
|
222 |
|
|
|
223 |
if (this.$isPreview == true) this.hidePreview().showPreview()
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
this.$isFullscreen = mode;
|
|
|
227 |
$textarea.focus();
|
|
|
228 |
}
|
|
|
229 |
|
|
|
230 |
, showEditor: function() {
|
|
|
231 |
var instance = this,
|
|
|
232 |
textarea,
|
|
|
233 |
ns = this.$ns,
|
|
|
234 |
container = this.$element,
|
|
|
235 |
originalHeigth = container.css('height'),
|
|
|
236 |
originalWidth = container.css('width'),
|
|
|
237 |
editable = this.$editable,
|
|
|
238 |
handler = this.$handler,
|
|
|
239 |
callback = this.$callback,
|
|
|
240 |
options = this.$options,
|
|
|
241 |
editor = $( '<div/>', {
|
|
|
242 |
'class': 'md-editor',
|
|
|
243 |
click: function() {
|
|
|
244 |
instance.focus();
|
|
|
245 |
}
|
|
|
246 |
});
|
|
|
247 |
|
|
|
248 |
// Prepare the editor
|
|
|
249 |
if (this.$editor === null) {
|
|
|
250 |
// Create the panel
|
|
|
251 |
var editorHeader = $('<div/>', {
|
|
|
252 |
'class': 'md-header btn-toolbar'
|
|
|
253 |
});
|
|
|
254 |
|
|
|
255 |
// Merge the main & additional button groups together
|
|
|
256 |
var allBtnGroups = [];
|
|
|
257 |
if (options.buttons.length > 0) allBtnGroups = allBtnGroups.concat(options.buttons[0]);
|
|
|
258 |
if (options.additionalButtons.length > 0) {
|
|
|
259 |
// iterate the additional button groups
|
|
|
260 |
$.each(options.additionalButtons[0], function(idx, buttonGroup){
|
|
|
261 |
|
|
|
262 |
// see if the group name of the addional group matches an existing group
|
|
|
263 |
var matchingGroups = $.grep(allBtnGroups, function(allButtonGroup, allIdx){
|
|
|
264 |
return allButtonGroup.name === buttonGroup.name;
|
|
|
265 |
});
|
|
|
266 |
|
|
|
267 |
// if it matches add the addional buttons to that group, if not just add it to the all buttons group
|
|
|
268 |
if(matchingGroups.length > 0) {
|
|
|
269 |
matchingGroups[0].data = matchingGroups[0].data.concat(buttonGroup.data);
|
|
|
270 |
} else {
|
|
|
271 |
allBtnGroups.push(options.additionalButtons[0][idx]);
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
});
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
// Reduce and/or reorder the button groups
|
|
|
278 |
if (options.reorderButtonGroups.length > 0) {
|
|
|
279 |
allBtnGroups = allBtnGroups
|
|
|
280 |
.filter(function(btnGroup) {
|
|
|
281 |
return options.reorderButtonGroups.indexOf(btnGroup.name) > -1;
|
|
|
282 |
})
|
|
|
283 |
.sort(function(a, b) {
|
|
|
284 |
if (options.reorderButtonGroups.indexOf(a.name) < options.reorderButtonGroups.indexOf(b.name)) return -1;
|
|
|
285 |
if (options.reorderButtonGroups.indexOf(a.name) > options.reorderButtonGroups.indexOf(b.name)) return 1;
|
|
|
286 |
return 0;
|
|
|
287 |
});
|
|
|
288 |
}
|
|
|
289 |
|
|
|
290 |
// Build the buttons
|
|
|
291 |
if (allBtnGroups.length > 0) {
|
|
|
292 |
editorHeader = this.__buildButtons([allBtnGroups], editorHeader);
|
|
|
293 |
}
|
|
|
294 |
|
|
|
295 |
if (options.fullscreen.enable) {
|
|
|
296 |
editorHeader.append('<div class="md-controls"><a class="md-control md-control-fullscreen" href="#"><span class="'+this.__getIcon(options.fullscreen.icons.fullscreenOn)+'"></span></a></div>').on('click', '.md-control-fullscreen', function(e) {
|
|
|
297 |
e.preventDefault();
|
|
|
298 |
instance.setFullscreen(true);
|
|
|
299 |
});
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
editor.append(editorHeader);
|
|
|
303 |
|
|
|
304 |
// Wrap the textarea
|
|
|
305 |
if (container.is('textarea')) {
|
|
|
306 |
container.before(editor);
|
|
|
307 |
textarea = container;
|
|
|
308 |
textarea.addClass('md-input');
|
|
|
309 |
editor.append(textarea);
|
|
|
310 |
} else {
|
|
|
311 |
var rawContent = (typeof toMarkdown == 'function') ? toMarkdown(container.html()) : container.html(),
|
|
|
312 |
currentContent = $.trim(rawContent);
|
|
|
313 |
|
|
|
314 |
// This is some arbitrary content that could be edited
|
|
|
315 |
textarea = $('<textarea/>', {
|
|
|
316 |
'class': 'md-input',
|
|
|
317 |
'val' : currentContent
|
|
|
318 |
});
|
|
|
319 |
|
|
|
320 |
editor.append(textarea);
|
|
|
321 |
|
|
|
322 |
// Save the editable
|
|
|
323 |
editable.el = container;
|
|
|
324 |
editable.type = container.prop('tagName').toLowerCase();
|
|
|
325 |
editable.content = container.html();
|
|
|
326 |
|
|
|
327 |
$(container[0].attributes).each(function(){
|
|
|
328 |
editable.attrKeys.push(this.nodeName);
|
|
|
329 |
editable.attrValues.push(this.nodeValue);
|
|
|
330 |
});
|
|
|
331 |
|
|
|
332 |
// Set editor to blocked the original container
|
|
|
333 |
container.replaceWith(editor);
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
var editorFooter = $('<div/>', {
|
|
|
337 |
'class': 'md-footer'
|
|
|
338 |
}),
|
|
|
339 |
createFooter = false,
|
|
|
340 |
footer = '';
|
|
|
341 |
// Create the footer if savable
|
|
|
342 |
if (options.savable) {
|
|
|
343 |
createFooter = true;
|
|
|
344 |
var saveHandler = 'cmdSave';
|
|
|
345 |
|
|
|
346 |
// Register handler and callback
|
|
|
347 |
handler.push(saveHandler);
|
|
|
348 |
callback.push(options.onSave);
|
|
|
349 |
|
|
|
350 |
editorFooter.append('<button class="btn btn-success" data-provider="'
|
|
|
351 |
+ ns
|
|
|
352 |
+ '" data-handler="'
|
|
|
353 |
+ saveHandler
|
|
|
354 |
+ '"><i class="icon icon-white icon-ok"></i> '
|
|
|
355 |
+ this.__localize('Save')
|
|
|
356 |
+ '</button>');
|
|
|
357 |
|
|
|
358 |
|
|
|
359 |
}
|
|
|
360 |
|
|
|
361 |
footer = typeof options.footer === 'function' ? options.footer(this) : options.footer;
|
|
|
362 |
|
|
|
363 |
if ($.trim(footer) !== '') {
|
|
|
364 |
createFooter = true;
|
|
|
365 |
editorFooter.append(footer);
|
|
|
366 |
}
|
|
|
367 |
|
|
|
368 |
if (createFooter) editor.append(editorFooter);
|
|
|
369 |
|
|
|
370 |
// Set width
|
|
|
371 |
if (options.width && options.width !== 'inherit') {
|
|
|
372 |
if (jQuery.isNumeric(options.width)) {
|
|
|
373 |
editor.css('display', 'table');
|
|
|
374 |
textarea.css('width', options.width + 'px');
|
|
|
375 |
} else {
|
|
|
376 |
editor.addClass(options.width);
|
|
|
377 |
}
|
|
|
378 |
}
|
|
|
379 |
|
|
|
380 |
// Set height
|
|
|
381 |
if (options.height && options.height !== 'inherit') {
|
|
|
382 |
if (jQuery.isNumeric(options.height)) {
|
|
|
383 |
var height = options.height;
|
|
|
384 |
if (editorHeader) height = Math.max(0, height - editorHeader.outerHeight());
|
|
|
385 |
if (editorFooter) height = Math.max(0, height - editorFooter.outerHeight());
|
|
|
386 |
textarea.css('height', height + 'px');
|
|
|
387 |
} else {
|
|
|
388 |
editor.addClass(options.height);
|
|
|
389 |
}
|
|
|
390 |
}
|
|
|
391 |
|
|
|
392 |
// Reference
|
|
|
393 |
this.$editor = editor;
|
|
|
394 |
this.$textarea = textarea;
|
|
|
395 |
this.$editable = editable;
|
|
|
396 |
this.$oldContent = this.getContent();
|
|
|
397 |
|
|
|
398 |
this.__setListener();
|
|
|
399 |
|
|
|
400 |
// Set editor attributes, data short-hand API and listener
|
|
|
401 |
this.$editor.attr('id',(new Date()).getTime());
|
|
|
402 |
this.$editor.on('click', '[data-provider="bootstrap-markdown"]', $.proxy(this.__handle, this));
|
|
|
403 |
|
|
|
404 |
if (this.$element.is(':disabled') || this.$element.is('[readonly]')) {
|
|
|
405 |
this.$editor.addClass('md-editor-disabled');
|
|
|
406 |
this.disableButtons('all');
|
|
|
407 |
}
|
|
|
408 |
|
|
|
409 |
if (this.eventSupported('keydown') && typeof jQuery.hotkeys === 'object') {
|
|
|
410 |
editorHeader.find('[data-provider="bootstrap-markdown"]').each(function() {
|
|
|
411 |
var $button = $(this),
|
|
|
412 |
hotkey = $button.attr('data-hotkey');
|
|
|
413 |
if (hotkey.toLowerCase() !== '') {
|
|
|
414 |
textarea.bind('keydown', hotkey, function() {
|
|
|
415 |
$button.trigger('click');
|
|
|
416 |
return false;
|
|
|
417 |
});
|
|
|
418 |
}
|
|
|
419 |
});
|
|
|
420 |
}
|
|
|
421 |
|
|
|
422 |
if (options.initialstate === 'preview') {
|
|
|
423 |
this.showPreview();
|
|
|
424 |
} else if (options.initialstate === 'fullscreen' && options.fullscreen.enable) {
|
|
|
425 |
this.setFullscreen(true);
|
|
|
426 |
}
|
|
|
427 |
|
|
|
428 |
} else {
|
|
|
429 |
this.$editor.show();
|
|
|
430 |
}
|
|
|
431 |
|
|
|
432 |
if (options.autofocus) {
|
|
|
433 |
this.$textarea.focus();
|
|
|
434 |
this.$editor.addClass('active');
|
|
|
435 |
}
|
|
|
436 |
|
|
|
437 |
if (options.fullscreen.enable && options.fullscreen !== false) {
|
|
|
438 |
this.$editor.append('<div class="md-fullscreen-controls">'
|
|
|
439 |
+ '<a href="#" class="exit-fullscreen" title="Exit fullscreen"><span class="' + this.__getIcon(options.fullscreen.icons.fullscreenOff) + '">'
|
|
|
440 |
+ '</span></a>'
|
|
|
441 |
+ '</div>');
|
|
|
442 |
this.$editor.on('click', '.exit-fullscreen', function(e) {
|
|
|
443 |
e.preventDefault();
|
|
|
444 |
instance.setFullscreen(false);
|
|
|
445 |
});
|
|
|
446 |
}
|
|
|
447 |
|
|
|
448 |
// hide hidden buttons from options
|
|
|
449 |
this.hideButtons(options.hiddenButtons);
|
|
|
450 |
|
|
|
451 |
// disable disabled buttons from options
|
|
|
452 |
this.disableButtons(options.disabledButtons);
|
|
|
453 |
|
|
|
454 |
// Trigger the onShow hook
|
|
|
455 |
options.onShow(this);
|
|
|
456 |
|
|
|
457 |
return this;
|
|
|
458 |
}
|
|
|
459 |
|
|
|
460 |
, parseContent: function(val) {
|
|
|
461 |
var content;
|
|
|
462 |
|
|
|
463 |
// parse with supported markdown parser
|
|
|
464 |
var val = val || this.$textarea.val();
|
|
|
465 |
|
|
|
466 |
if (this.$options.parser) {
|
|
|
467 |
content = this.$options.parser(val);
|
|
|
468 |
} else if (typeof markdown == 'object') {
|
|
|
469 |
content = markdown.toHTML(val);
|
|
|
470 |
} else if (typeof marked == 'function') {
|
|
|
471 |
content = marked(val);
|
|
|
472 |
} else {
|
|
|
473 |
content = val;
|
|
|
474 |
}
|
|
|
475 |
|
|
|
476 |
return content;
|
|
|
477 |
}
|
|
|
478 |
|
|
|
479 |
, showPreview: function() {
|
|
|
480 |
var options = this.$options,
|
|
|
481 |
container = this.$textarea,
|
|
|
482 |
afterContainer = container.next(),
|
|
|
483 |
replacementContainer = $('<div/>',{'class':'md-preview','data-provider':'markdown-preview'}),
|
|
|
484 |
content,
|
|
|
485 |
callbackContent;
|
|
|
486 |
|
|
|
487 |
if (this.$isPreview == true) {
|
|
|
488 |
// Avoid sequenced element creation on missused scenario
|
|
|
489 |
// @see https://github.com/toopay/bootstrap-markdown/issues/170
|
|
|
490 |
return this;
|
|
|
491 |
}
|
|
|
492 |
|
|
|
493 |
// Give flag that tell the editor enter preview mode
|
|
|
494 |
this.$isPreview = true;
|
|
|
495 |
// Disable all buttons
|
|
|
496 |
this.disableButtons('all').enableButtons('cmdPreview');
|
|
|
497 |
|
|
|
498 |
// Try to get the content from callback
|
|
|
499 |
callbackContent = options.onPreview(this);
|
|
|
500 |
// Set the content based from the callback content if string otherwise parse value from textarea
|
|
|
501 |
content = typeof callbackContent == 'string' ? callbackContent : this.parseContent();
|
|
|
502 |
|
|
|
503 |
// Build preview element
|
|
|
504 |
replacementContainer.html(content);
|
|
|
505 |
|
|
|
506 |
if (afterContainer && afterContainer.attr('class') == 'md-footer') {
|
|
|
507 |
// If there is footer element, insert the preview container before it
|
|
|
508 |
replacementContainer.insertBefore(afterContainer);
|
|
|
509 |
} else {
|
|
|
510 |
// Otherwise, just append it after textarea
|
|
|
511 |
container.parent().append(replacementContainer);
|
|
|
512 |
}
|
|
|
513 |
|
|
|
514 |
// Set the preview element dimensions
|
|
|
515 |
replacementContainer.css({
|
|
|
516 |
width: container.outerWidth() + 'px',
|
|
|
517 |
height: container.outerHeight() + 'px'
|
|
|
518 |
});
|
|
|
519 |
|
|
|
520 |
if (this.$options.resize) {
|
|
|
521 |
replacementContainer.css('resize',this.$options.resize);
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
// Hide the last-active textarea
|
|
|
525 |
container.hide();
|
|
|
526 |
|
|
|
527 |
// Attach the editor instances
|
|
|
528 |
replacementContainer.data('markdown',this);
|
|
|
529 |
|
|
|
530 |
if (this.$element.is(':disabled') || this.$element.is('[readonly]')) {
|
|
|
531 |
this.$editor.addClass('md-editor-disabled');
|
|
|
532 |
this.disableButtons('all');
|
|
|
533 |
}
|
|
|
534 |
|
|
|
535 |
return this;
|
|
|
536 |
}
|
|
|
537 |
|
|
|
538 |
, hidePreview: function() {
|
|
|
539 |
// Give flag that tell the editor quit preview mode
|
|
|
540 |
this.$isPreview = false;
|
|
|
541 |
|
|
|
542 |
// Obtain the preview container
|
|
|
543 |
var container = this.$editor.find('div[data-provider="markdown-preview"]');
|
|
|
544 |
|
|
|
545 |
// Remove the preview container
|
|
|
546 |
container.remove();
|
|
|
547 |
|
|
|
548 |
// Enable all buttons
|
|
|
549 |
this.enableButtons('all');
|
|
|
550 |
// Disable configured disabled buttons
|
|
|
551 |
this.disableButtons(this.$options.disabledButtons);
|
|
|
552 |
|
|
|
553 |
// Back to the editor
|
|
|
554 |
this.$textarea.show();
|
|
|
555 |
this.__setListener();
|
|
|
556 |
|
|
|
557 |
return this;
|
|
|
558 |
}
|
|
|
559 |
|
|
|
560 |
, isDirty: function() {
|
|
|
561 |
return this.$oldContent != this.getContent();
|
|
|
562 |
}
|
|
|
563 |
|
|
|
564 |
, getContent: function() {
|
|
|
565 |
return this.$textarea.val();
|
|
|
566 |
}
|
|
|
567 |
|
|
|
568 |
, setContent: function(content) {
|
|
|
569 |
this.$textarea.val(content);
|
|
|
570 |
|
|
|
571 |
return this;
|
|
|
572 |
}
|
|
|
573 |
|
|
|
574 |
, findSelection: function(chunk) {
|
|
|
575 |
var content = this.getContent(), startChunkPosition;
|
|
|
576 |
|
|
|
577 |
if (startChunkPosition = content.indexOf(chunk), startChunkPosition >= 0 && chunk.length > 0) {
|
|
|
578 |
var oldSelection = this.getSelection(), selection;
|
|
|
579 |
|
|
|
580 |
this.setSelection(startChunkPosition,startChunkPosition+chunk.length);
|
|
|
581 |
selection = this.getSelection();
|
|
|
582 |
|
|
|
583 |
this.setSelection(oldSelection.start,oldSelection.end);
|
|
|
584 |
|
|
|
585 |
return selection;
|
|
|
586 |
} else {
|
|
|
587 |
return null;
|
|
|
588 |
}
|
|
|
589 |
}
|
|
|
590 |
|
|
|
591 |
, getSelection: function() {
|
|
|
592 |
|
|
|
593 |
var e = this.$textarea[0];
|
|
|
594 |
|
|
|
595 |
return (
|
|
|
596 |
|
|
|
597 |
('selectionStart' in e && function() {
|
|
|
598 |
var l = e.selectionEnd - e.selectionStart;
|
|
|
599 |
return { start: e.selectionStart, end: e.selectionEnd, length: l, text: e.value.substr(e.selectionStart, l) };
|
|
|
600 |
}) ||
|
|
|
601 |
|
|
|
602 |
/* browser not supported */
|
|
|
603 |
function() {
|
|
|
604 |
return null;
|
|
|
605 |
}
|
|
|
606 |
|
|
|
607 |
)();
|
|
|
608 |
|
|
|
609 |
}
|
|
|
610 |
|
|
|
611 |
, setSelection: function(start,end) {
|
|
|
612 |
|
|
|
613 |
var e = this.$textarea[0];
|
|
|
614 |
|
|
|
615 |
return (
|
|
|
616 |
|
|
|
617 |
('selectionStart' in e && function() {
|
|
|
618 |
e.selectionStart = start;
|
|
|
619 |
e.selectionEnd = end;
|
|
|
620 |
return;
|
|
|
621 |
}) ||
|
|
|
622 |
|
|
|
623 |
/* browser not supported */
|
|
|
624 |
function() {
|
|
|
625 |
return null;
|
|
|
626 |
}
|
|
|
627 |
|
|
|
628 |
)();
|
|
|
629 |
|
|
|
630 |
}
|
|
|
631 |
|
|
|
632 |
, replaceSelection: function(text) {
|
|
|
633 |
|
|
|
634 |
var e = this.$textarea[0];
|
|
|
635 |
|
|
|
636 |
return (
|
|
|
637 |
|
|
|
638 |
('selectionStart' in e && function() {
|
|
|
639 |
e.value = e.value.substr(0, e.selectionStart) + text + e.value.substr(e.selectionEnd, e.value.length);
|
|
|
640 |
// Set cursor to the last replacement end
|
|
|
641 |
e.selectionStart = e.value.length;
|
|
|
642 |
return this;
|
|
|
643 |
}) ||
|
|
|
644 |
|
|
|
645 |
/* browser not supported */
|
|
|
646 |
function() {
|
|
|
647 |
e.value += text;
|
|
|
648 |
return jQuery(e);
|
|
|
649 |
}
|
|
|
650 |
|
|
|
651 |
)();
|
|
|
652 |
}
|
|
|
653 |
|
|
|
654 |
, getNextTab: function() {
|
|
|
655 |
// Shift the nextTab
|
|
|
656 |
if (this.$nextTab.length === 0) {
|
|
|
657 |
return null;
|
|
|
658 |
} else {
|
|
|
659 |
var nextTab, tab = this.$nextTab.shift();
|
|
|
660 |
|
|
|
661 |
if (typeof tab == 'function') {
|
|
|
662 |
nextTab = tab();
|
|
|
663 |
} else if (typeof tab == 'object' && tab.length > 0) {
|
|
|
664 |
nextTab = tab;
|
|
|
665 |
}
|
|
|
666 |
|
|
|
667 |
return nextTab;
|
|
|
668 |
}
|
|
|
669 |
}
|
|
|
670 |
|
|
|
671 |
, setNextTab: function(start,end) {
|
|
|
672 |
// Push new selection into nextTab collections
|
|
|
673 |
if (typeof start == 'string') {
|
|
|
674 |
var that = this;
|
|
|
675 |
this.$nextTab.push(function(){
|
|
|
676 |
return that.findSelection(start);
|
|
|
677 |
});
|
|
|
678 |
} else if (typeof start == 'number' && typeof end == 'number') {
|
|
|
679 |
var oldSelection = this.getSelection();
|
|
|
680 |
|
|
|
681 |
this.setSelection(start,end);
|
|
|
682 |
this.$nextTab.push(this.getSelection());
|
|
|
683 |
|
|
|
684 |
this.setSelection(oldSelection.start,oldSelection.end);
|
|
|
685 |
}
|
|
|
686 |
|
|
|
687 |
return;
|
|
|
688 |
}
|
|
|
689 |
|
|
|
690 |
, __parseButtonNameParam: function (names) {
|
|
|
691 |
return typeof names == 'string' ?
|
|
|
692 |
names.split(' ') :
|
|
|
693 |
names;
|
|
|
694 |
|
|
|
695 |
}
|
|
|
696 |
|
|
|
697 |
, enableButtons: function(name) {
|
|
|
698 |
var buttons = this.__parseButtonNameParam(name),
|
|
|
699 |
that = this;
|
|
|
700 |
|
|
|
701 |
$.each(buttons, function(i, v) {
|
|
|
702 |
that.__alterButtons(buttons[i], function (el) {
|
|
|
703 |
el.removeAttr('disabled');
|
|
|
704 |
});
|
|
|
705 |
});
|
|
|
706 |
|
|
|
707 |
return this;
|
|
|
708 |
}
|
|
|
709 |
|
|
|
710 |
, disableButtons: function(name) {
|
|
|
711 |
var buttons = this.__parseButtonNameParam(name),
|
|
|
712 |
that = this;
|
|
|
713 |
|
|
|
714 |
$.each(buttons, function(i, v) {
|
|
|
715 |
that.__alterButtons(buttons[i], function (el) {
|
|
|
716 |
el.attr('disabled','disabled');
|
|
|
717 |
});
|
|
|
718 |
});
|
|
|
719 |
|
|
|
720 |
return this;
|
|
|
721 |
}
|
|
|
722 |
|
|
|
723 |
, hideButtons: function(name) {
|
|
|
724 |
var buttons = this.__parseButtonNameParam(name),
|
|
|
725 |
that = this;
|
|
|
726 |
|
|
|
727 |
$.each(buttons, function(i, v) {
|
|
|
728 |
that.__alterButtons(buttons[i], function (el) {
|
|
|
729 |
el.addClass('hidden');
|
|
|
730 |
});
|
|
|
731 |
});
|
|
|
732 |
|
|
|
733 |
return this;
|
|
|
734 |
}
|
|
|
735 |
|
|
|
736 |
, showButtons: function(name) {
|
|
|
737 |
var buttons = this.__parseButtonNameParam(name),
|
|
|
738 |
that = this;
|
|
|
739 |
|
|
|
740 |
$.each(buttons, function(i, v) {
|
|
|
741 |
that.__alterButtons(buttons[i], function (el) {
|
|
|
742 |
el.removeClass('hidden');
|
|
|
743 |
});
|
|
|
744 |
});
|
|
|
745 |
|
|
|
746 |
return this;
|
|
|
747 |
}
|
|
|
748 |
|
|
|
749 |
, eventSupported: function(eventName) {
|
|
|
750 |
var isSupported = eventName in this.$element;
|
|
|
751 |
if (!isSupported) {
|
|
|
752 |
this.$element.setAttribute(eventName, 'return;');
|
|
|
753 |
isSupported = typeof this.$element[eventName] === 'function';
|
|
|
754 |
}
|
|
|
755 |
return isSupported;
|
|
|
756 |
}
|
|
|
757 |
|
|
|
758 |
, keyup: function (e) {
|
|
|
759 |
var blocked = false;
|
|
|
760 |
switch(e.keyCode) {
|
|
|
761 |
case 40: // down arrow
|
|
|
762 |
case 38: // up arrow
|
|
|
763 |
case 16: // shift
|
|
|
764 |
case 17: // ctrl
|
|
|
765 |
case 18: // alt
|
|
|
766 |
break;
|
|
|
767 |
|
|
|
768 |
case 9: // tab
|
|
|
769 |
var nextTab;
|
|
|
770 |
if (nextTab = this.getNextTab(),nextTab !== null) {
|
|
|
771 |
// Get the nextTab if exists
|
|
|
772 |
var that = this;
|
|
|
773 |
setTimeout(function(){
|
|
|
774 |
that.setSelection(nextTab.start,nextTab.end);
|
|
|
775 |
},500);
|
|
|
776 |
|
|
|
777 |
blocked = true;
|
|
|
778 |
} else {
|
|
|
779 |
// The next tab memory contains nothing...
|
|
|
780 |
// check the cursor position to determine tab action
|
|
|
781 |
var cursor = this.getSelection();
|
|
|
782 |
|
|
|
783 |
if (cursor.start == cursor.end && cursor.end == this.getContent().length) {
|
|
|
784 |
// The cursor already reach the end of the content
|
|
|
785 |
blocked = false;
|
|
|
786 |
} else {
|
|
|
787 |
// Put the cursor to the end
|
|
|
788 |
this.setSelection(this.getContent().length,this.getContent().length);
|
|
|
789 |
|
|
|
790 |
blocked = true;
|
|
|
791 |
}
|
|
|
792 |
}
|
|
|
793 |
|
|
|
794 |
break;
|
|
|
795 |
|
|
|
796 |
case 13: // enter
|
|
|
797 |
blocked = false;
|
|
|
798 |
break;
|
|
|
799 |
case 27: // escape
|
|
|
800 |
if (this.$isFullscreen) this.setFullscreen(false);
|
|
|
801 |
blocked = false;
|
|
|
802 |
break;
|
|
|
803 |
|
|
|
804 |
default:
|
|
|
805 |
blocked = false;
|
|
|
806 |
}
|
|
|
807 |
|
|
|
808 |
if (blocked) {
|
|
|
809 |
e.stopPropagation();
|
|
|
810 |
e.preventDefault();
|
|
|
811 |
}
|
|
|
812 |
|
|
|
813 |
this.$options.onChange(this);
|
|
|
814 |
}
|
|
|
815 |
|
|
|
816 |
, change: function(e) {
|
|
|
817 |
this.$options.onChange(this);
|
|
|
818 |
return this;
|
|
|
819 |
}
|
|
|
820 |
, select: function (e) {
|
|
|
821 |
this.$options.onSelect(this);
|
|
|
822 |
return this;
|
|
|
823 |
}
|
|
|
824 |
, focus: function (e) {
|
|
|
825 |
var options = this.$options,
|
|
|
826 |
isHideable = options.hideable,
|
|
|
827 |
editor = this.$editor;
|
|
|
828 |
|
|
|
829 |
editor.addClass('active');
|
|
|
830 |
|
|
|
831 |
// Blur other markdown(s)
|
|
|
832 |
$(document).find('.md-editor').each(function(){
|
|
|
833 |
if ($(this).attr('id') !== editor.attr('id')) {
|
|
|
834 |
var attachedMarkdown;
|
|
|
835 |
|
|
|
836 |
if (attachedMarkdown = $(this).find('textarea').data('markdown'),
|
|
|
837 |
attachedMarkdown === null) {
|
|
|
838 |
attachedMarkdown = $(this).find('div[data-provider="markdown-preview"]').data('markdown');
|
|
|
839 |
}
|
|
|
840 |
|
|
|
841 |
if (attachedMarkdown) {
|
|
|
842 |
attachedMarkdown.blur();
|
|
|
843 |
}
|
|
|
844 |
}
|
|
|
845 |
});
|
|
|
846 |
|
|
|
847 |
// Trigger the onFocus hook
|
|
|
848 |
options.onFocus(this);
|
|
|
849 |
|
|
|
850 |
return this;
|
|
|
851 |
}
|
|
|
852 |
|
|
|
853 |
, blur: function (e) {
|
|
|
854 |
var options = this.$options,
|
|
|
855 |
isHideable = options.hideable,
|
|
|
856 |
editor = this.$editor,
|
|
|
857 |
editable = this.$editable;
|
|
|
858 |
|
|
|
859 |
if (editor.hasClass('active') || this.$element.parent().length === 0) {
|
|
|
860 |
editor.removeClass('active');
|
|
|
861 |
|
|
|
862 |
if (isHideable) {
|
|
|
863 |
// Check for editable elements
|
|
|
864 |
if (editable.el !== null) {
|
|
|
865 |
// Build the original element
|
|
|
866 |
var oldElement = $('<'+editable.type+'/>'),
|
|
|
867 |
content = this.getContent(),
|
|
|
868 |
currentContent = this.parseContent(content);
|
|
|
869 |
|
|
|
870 |
$(editable.attrKeys).each(function(k,v) {
|
|
|
871 |
oldElement.attr(editable.attrKeys[k],editable.attrValues[k]);
|
|
|
872 |
});
|
|
|
873 |
|
|
|
874 |
// Get the editor content
|
|
|
875 |
oldElement.html(currentContent);
|
|
|
876 |
|
|
|
877 |
editor.replaceWith(oldElement);
|
|
|
878 |
} else {
|
|
|
879 |
editor.hide();
|
|
|
880 |
}
|
|
|
881 |
}
|
|
|
882 |
|
|
|
883 |
// Trigger the onBlur hook
|
|
|
884 |
options.onBlur(this);
|
|
|
885 |
}
|
|
|
886 |
|
|
|
887 |
return this;
|
|
|
888 |
}
|
|
|
889 |
|
|
|
890 |
};
|
|
|
891 |
|
|
|
892 |
/* MARKDOWN PLUGIN DEFINITION
|
|
|
893 |
* ========================== */
|
|
|
894 |
|
|
|
895 |
var old = $.fn.markdown;
|
|
|
896 |
|
|
|
897 |
$.fn.markdown = function (option) {
|
|
|
898 |
return this.each(function () {
|
|
|
899 |
var $this = $(this)
|
|
|
900 |
, data = $this.data('markdown')
|
|
|
901 |
, options = typeof option == 'object' && option;
|
|
|
902 |
if (!data) $this.data('markdown', (data = new Markdown(this, options)))
|
|
|
903 |
})
|
|
|
904 |
};
|
|
|
905 |
|
|
|
906 |
$.fn.markdown.messages = {};
|
|
|
907 |
|
|
|
908 |
$.fn.markdown.defaults = {
|
|
|
909 |
/* Editor Properties */
|
|
|
910 |
autofocus: false,
|
|
|
911 |
hideable: false,
|
|
|
912 |
savable: false,
|
|
|
913 |
width: 'inherit',
|
|
|
914 |
height: 'inherit',
|
|
|
915 |
resize: 'none',
|
|
|
916 |
iconlibrary: 'glyph',
|
|
|
917 |
language: 'en',
|
|
|
918 |
initialstate: 'editor',
|
|
|
919 |
parser: null,
|
|
|
920 |
|
|
|
921 |
/* Buttons Properties */
|
|
|
922 |
buttons: [
|
|
|
923 |
[{
|
|
|
924 |
name: 'groupFont',
|
|
|
925 |
data: [{
|
|
|
926 |
name: 'cmdBold',
|
|
|
927 |
hotkey: 'Ctrl+B',
|
|
|
928 |
title: 'Bold',
|
|
|
929 |
icon: { glyph: 'glyphicon glyphicon-bold', fa: 'fa fa-bold', 'fa-3': 'icon-bold' },
|
|
|
930 |
callback: function(e){
|
|
|
931 |
// Give/remove ** surround the selection
|
|
|
932 |
var chunk, cursor, selected = e.getSelection(), content = e.getContent();
|
|
|
933 |
|
|
|
934 |
if (selected.length === 0) {
|
|
|
935 |
// Give extra word
|
|
|
936 |
chunk = e.__localize('strong text');
|
|
|
937 |
} else {
|
|
|
938 |
chunk = selected.text;
|
|
|
939 |
}
|
|
|
940 |
|
|
|
941 |
// transform selection and set the cursor into chunked text
|
|
|
942 |
if (content.substr(selected.start-2,2) === '**'
|
|
|
943 |
&& content.substr(selected.end,2) === '**' ) {
|
|
|
944 |
e.setSelection(selected.start-2,selected.end+2);
|
|
|
945 |
e.replaceSelection(chunk);
|
|
|
946 |
cursor = selected.start-2;
|
|
|
947 |
} else {
|
|
|
948 |
e.replaceSelection('**'+chunk+'**');
|
|
|
949 |
cursor = selected.start+2;
|
|
|
950 |
}
|
|
|
951 |
|
|
|
952 |
// Set the cursor
|
|
|
953 |
e.setSelection(cursor,cursor+chunk.length);
|
|
|
954 |
}
|
|
|
955 |
},{
|
|
|
956 |
name: 'cmdItalic',
|
|
|
957 |
title: 'Italic',
|
|
|
958 |
hotkey: 'Ctrl+I',
|
|
|
959 |
icon: { glyph: 'glyphicon glyphicon-italic', fa: 'fa fa-italic', 'fa-3': 'icon-italic' },
|
|
|
960 |
callback: function(e){
|
|
|
961 |
// Give/remove * surround the selection
|
|
|
962 |
var chunk, cursor, selected = e.getSelection(), content = e.getContent();
|
|
|
963 |
|
|
|
964 |
if (selected.length === 0) {
|
|
|
965 |
// Give extra word
|
|
|
966 |
chunk = e.__localize('emphasized text');
|
|
|
967 |
} else {
|
|
|
968 |
chunk = selected.text;
|
|
|
969 |
}
|
|
|
970 |
|
|
|
971 |
// transform selection and set the cursor into chunked text
|
|
|
972 |
if (content.substr(selected.start-1,1) === '_'
|
|
|
973 |
&& content.substr(selected.end,1) === '_' ) {
|
|
|
974 |
e.setSelection(selected.start-1,selected.end+1);
|
|
|
975 |
e.replaceSelection(chunk);
|
|
|
976 |
cursor = selected.start-1;
|
|
|
977 |
} else {
|
|
|
978 |
e.replaceSelection('_'+chunk+'_');
|
|
|
979 |
cursor = selected.start+1;
|
|
|
980 |
}
|
|
|
981 |
|
|
|
982 |
// Set the cursor
|
|
|
983 |
e.setSelection(cursor,cursor+chunk.length);
|
|
|
984 |
}
|
|
|
985 |
},{
|
|
|
986 |
name: 'cmdHeading',
|
|
|
987 |
title: 'Heading',
|
|
|
988 |
hotkey: 'Ctrl+H',
|
|
|
989 |
icon: { glyph: 'glyphicon glyphicon-header', fa: 'fa fa-header', 'fa-3': 'icon-font' },
|
|
|
990 |
callback: function(e){
|
|
|
991 |
// Append/remove ### surround the selection
|
|
|
992 |
var chunk, cursor, selected = e.getSelection(), content = e.getContent(), pointer, prevChar;
|
|
|
993 |
|
|
|
994 |
if (selected.length === 0) {
|
|
|
995 |
// Give extra word
|
|
|
996 |
chunk = e.__localize('heading text');
|
|
|
997 |
} else {
|
|
|
998 |
chunk = selected.text + '\n';
|
|
|
999 |
}
|
|
|
1000 |
|
|
|
1001 |
// transform selection and set the cursor into chunked text
|
|
|
1002 |
if ((pointer = 4, content.substr(selected.start-pointer,pointer) === '### ')
|
|
|
1003 |
|| (pointer = 3, content.substr(selected.start-pointer,pointer) === '###')) {
|
|
|
1004 |
e.setSelection(selected.start-pointer,selected.end);
|
|
|
1005 |
e.replaceSelection(chunk);
|
|
|
1006 |
cursor = selected.start-pointer;
|
|
|
1007 |
} else if (selected.start > 0 && (prevChar = content.substr(selected.start-1,1), !!prevChar && prevChar != '\n')) {
|
|
|
1008 |
e.replaceSelection('\n\n### '+chunk);
|
|
|
1009 |
cursor = selected.start+6;
|
|
|
1010 |
} else {
|
|
|
1011 |
// Empty string before element
|
|
|
1012 |
e.replaceSelection('### '+chunk);
|
|
|
1013 |
cursor = selected.start+4;
|
|
|
1014 |
}
|
|
|
1015 |
|
|
|
1016 |
// Set the cursor
|
|
|
1017 |
e.setSelection(cursor,cursor+chunk.length);
|
|
|
1018 |
}
|
|
|
1019 |
}]
|
|
|
1020 |
},{
|
|
|
1021 |
name: 'groupLink',
|
|
|
1022 |
data: [{
|
|
|
1023 |
name: 'cmdUrl',
|
|
|
1024 |
title: 'URL/Link',
|
|
|
1025 |
hotkey: 'Ctrl+L',
|
|
|
1026 |
icon: { glyph: 'glyphicon glyphicon-link', fa: 'fa fa-link', 'fa-3': 'icon-link' },
|
|
|
1027 |
callback: function(e){
|
|
|
1028 |
// Give [] surround the selection and prepend the link
|
|
|
1029 |
var chunk, cursor, selected = e.getSelection(), content = e.getContent(), link;
|
|
|
1030 |
|
|
|
1031 |
if (selected.length === 0) {
|
|
|
1032 |
// Give extra word
|
|
|
1033 |
chunk = e.__localize('enter link description here');
|
|
|
1034 |
} else {
|
|
|
1035 |
chunk = selected.text;
|
|
|
1036 |
}
|
|
|
1037 |
|
|
|
1038 |
link = prompt(e.__localize('Insert Hyperlink'),'http://');
|
|
|
1039 |
|
|
|
1040 |
if (link !== null && link !== '' && link !== 'http://' && link.substr(0,4) === 'http') {
|
|
|
1041 |
var sanitizedLink = $('<div>'+link+'</div>').text();
|
|
|
1042 |
|
|
|
1043 |
// transform selection and set the cursor into chunked text
|
|
|
1044 |
e.replaceSelection('['+chunk+']('+sanitizedLink+')');
|
|
|
1045 |
cursor = selected.start+1;
|
|
|
1046 |
|
|
|
1047 |
// Set the cursor
|
|
|
1048 |
e.setSelection(cursor,cursor+chunk.length);
|
|
|
1049 |
}
|
|
|
1050 |
}
|
|
|
1051 |
},{
|
|
|
1052 |
name: 'cmdImage',
|
|
|
1053 |
title: 'Image',
|
|
|
1054 |
hotkey: 'Ctrl+G',
|
|
|
1055 |
icon: { glyph: 'glyphicon glyphicon-picture', fa: 'fa fa-picture-o', 'fa-3': 'icon-picture' },
|
|
|
1056 |
callback: function(e){
|
|
|
1057 |
// Give ![] surround the selection and prepend the image link
|
|
|
1058 |
var chunk, cursor, selected = e.getSelection(), content = e.getContent(), link;
|
|
|
1059 |
|
|
|
1060 |
if (selected.length === 0) {
|
|
|
1061 |
// Give extra word
|
|
|
1062 |
chunk = e.__localize('enter image description here');
|
|
|
1063 |
} else {
|
|
|
1064 |
chunk = selected.text;
|
|
|
1065 |
}
|
|
|
1066 |
|
|
|
1067 |
link = prompt(e.__localize('Insert Image Hyperlink'),'http://');
|
|
|
1068 |
|
|
|
1069 |
if (link !== null && link !== '' && link !== 'http://' && link.substr(0,4) === 'http') {
|
|
|
1070 |
var sanitizedLink = $('<div>'+link+'</div>').text();
|
|
|
1071 |
|
|
|
1072 |
// transform selection and set the cursor into chunked text
|
|
|
1073 |
e.replaceSelection('+'")');
|
|
|
1074 |
cursor = selected.start+2;
|
|
|
1075 |
|
|
|
1076 |
// Set the next tab
|
|
|
1077 |
e.setNextTab(e.__localize('enter image title here'));
|
|
|
1078 |
|
|
|
1079 |
// Set the cursor
|
|
|
1080 |
e.setSelection(cursor,cursor+chunk.length);
|
|
|
1081 |
}
|
|
|
1082 |
}
|
|
|
1083 |
}]
|
|
|
1084 |
},{
|
|
|
1085 |
name: 'groupMisc',
|
|
|
1086 |
data: [{
|
|
|
1087 |
name: 'cmdList',
|
|
|
1088 |
hotkey: 'Ctrl+U',
|
|
|
1089 |
title: 'Unordered List',
|
|
|
1090 |
icon: { glyph: 'glyphicon glyphicon-list', fa: 'fa fa-list', 'fa-3': 'icon-list-ul' },
|
|
|
1091 |
callback: function(e){
|
|
|
1092 |
// Prepend/Give - surround the selection
|
|
|
1093 |
var chunk, cursor, selected = e.getSelection(), content = e.getContent();
|
|
|
1094 |
|
|
|
1095 |
// transform selection and set the cursor into chunked text
|
|
|
1096 |
if (selected.length === 0) {
|
|
|
1097 |
// Give extra word
|
|
|
1098 |
chunk = e.__localize('list text here');
|
|
|
1099 |
|
|
|
1100 |
e.replaceSelection('- '+chunk);
|
|
|
1101 |
// Set the cursor
|
|
|
1102 |
cursor = selected.start+2;
|
|
|
1103 |
} else {
|
|
|
1104 |
if (selected.text.indexOf('\n') < 0) {
|
|
|
1105 |
chunk = selected.text;
|
|
|
1106 |
|
|
|
1107 |
e.replaceSelection('- '+chunk);
|
|
|
1108 |
|
|
|
1109 |
// Set the cursor
|
|
|
1110 |
cursor = selected.start+2;
|
|
|
1111 |
} else {
|
|
|
1112 |
var list = [];
|
|
|
1113 |
|
|
|
1114 |
list = selected.text.split('\n');
|
|
|
1115 |
chunk = list[0];
|
|
|
1116 |
|
|
|
1117 |
$.each(list,function(k,v) {
|
|
|
1118 |
list[k] = '- '+v;
|
|
|
1119 |
});
|
|
|
1120 |
|
|
|
1121 |
e.replaceSelection('\n\n'+list.join('\n'));
|
|
|
1122 |
|
|
|
1123 |
// Set the cursor
|
|
|
1124 |
cursor = selected.start+4;
|
|
|
1125 |
}
|
|
|
1126 |
}
|
|
|
1127 |
|
|
|
1128 |
// Set the cursor
|
|
|
1129 |
e.setSelection(cursor,cursor+chunk.length);
|
|
|
1130 |
}
|
|
|
1131 |
},
|
|
|
1132 |
{
|
|
|
1133 |
name: 'cmdListO',
|
|
|
1134 |
hotkey: 'Ctrl+O',
|
|
|
1135 |
title: 'Ordered List',
|
|
|
1136 |
icon: { glyph: 'glyphicon glyphicon-th-list', fa: 'fa fa-list-ol', 'fa-3': 'icon-list-ol' },
|
|
|
1137 |
callback: function(e) {
|
|
|
1138 |
|
|
|
1139 |
// Prepend/Give - surround the selection
|
|
|
1140 |
var chunk, cursor, selected = e.getSelection(), content = e.getContent();
|
|
|
1141 |
|
|
|
1142 |
// transform selection and set the cursor into chunked text
|
|
|
1143 |
if (selected.length === 0) {
|
|
|
1144 |
// Give extra word
|
|
|
1145 |
chunk = e.__localize('list text here');
|
|
|
1146 |
e.replaceSelection('1. '+chunk);
|
|
|
1147 |
// Set the cursor
|
|
|
1148 |
cursor = selected.start+3;
|
|
|
1149 |
} else {
|
|
|
1150 |
if (selected.text.indexOf('\n') < 0) {
|
|
|
1151 |
chunk = selected.text;
|
|
|
1152 |
|
|
|
1153 |
e.replaceSelection('1. '+chunk);
|
|
|
1154 |
|
|
|
1155 |
// Set the cursor
|
|
|
1156 |
cursor = selected.start+3;
|
|
|
1157 |
} else {
|
|
|
1158 |
var list = [];
|
|
|
1159 |
|
|
|
1160 |
list = selected.text.split('\n');
|
|
|
1161 |
chunk = list[0];
|
|
|
1162 |
|
|
|
1163 |
$.each(list,function(k,v) {
|
|
|
1164 |
list[k] = '1. '+v;
|
|
|
1165 |
});
|
|
|
1166 |
|
|
|
1167 |
e.replaceSelection('\n\n'+list.join('\n'));
|
|
|
1168 |
|
|
|
1169 |
// Set the cursor
|
|
|
1170 |
cursor = selected.start+5;
|
|
|
1171 |
}
|
|
|
1172 |
}
|
|
|
1173 |
|
|
|
1174 |
// Set the cursor
|
|
|
1175 |
e.setSelection(cursor,cursor+chunk.length);
|
|
|
1176 |
}
|
|
|
1177 |
},
|
|
|
1178 |
{
|
|
|
1179 |
name: 'cmdCode',
|
|
|
1180 |
hotkey: 'Ctrl+K',
|
|
|
1181 |
title: 'Code',
|
|
|
1182 |
icon: { glyph: 'glyphicon glyphicon-asterisk', fa: 'fa fa-code', 'fa-3': 'icon-code' },
|
|
|
1183 |
callback: function(e) {
|
|
|
1184 |
// Give/remove ** surround the selection
|
|
|
1185 |
var chunk, cursor, selected = e.getSelection(), content = e.getContent();
|
|
|
1186 |
|
|
|
1187 |
if (selected.length === 0) {
|
|
|
1188 |
// Give extra word
|
|
|
1189 |
chunk = e.__localize('code text here');
|
|
|
1190 |
} else {
|
|
|
1191 |
chunk = selected.text;
|
|
|
1192 |
}
|
|
|
1193 |
|
|
|
1194 |
// transform selection and set the cursor into chunked text
|
|
|
1195 |
if (content.substr(selected.start-4,4) === '```\n'
|
|
|
1196 |
&& content.substr(selected.end,4) === '\n```') {
|
|
|
1197 |
e.setSelection(selected.start-4, selected.end+4);
|
|
|
1198 |
e.replaceSelection(chunk);
|
|
|
1199 |
cursor = selected.start-4;
|
|
|
1200 |
} else if (content.substr(selected.start-1,1) === '`'
|
|
|
1201 |
&& content.substr(selected.end,1) === '`') {
|
|
|
1202 |
e.setSelection(selected.start-1,selected.end+1);
|
|
|
1203 |
e.replaceSelection(chunk);
|
|
|
1204 |
cursor = selected.start-1;
|
|
|
1205 |
} else if (content.indexOf('\n') > -1) {
|
|
|
1206 |
e.replaceSelection('```\n'+chunk+'\n```');
|
|
|
1207 |
cursor = selected.start+4;
|
|
|
1208 |
} else {
|
|
|
1209 |
e.replaceSelection('`'+chunk+'`');
|
|
|
1210 |
cursor = selected.start+1;
|
|
|
1211 |
}
|
|
|
1212 |
|
|
|
1213 |
// Set the cursor
|
|
|
1214 |
e.setSelection(cursor,cursor+chunk.length);
|
|
|
1215 |
}
|
|
|
1216 |
},
|
|
|
1217 |
{
|
|
|
1218 |
name: 'cmdQuote',
|
|
|
1219 |
hotkey: 'Ctrl+Q',
|
|
|
1220 |
title: 'Quote',
|
|
|
1221 |
icon: { glyph: 'glyphicon glyphicon-comment', fa: 'fa fa-quote-left', 'fa-3': 'icon-quote-left' },
|
|
|
1222 |
callback: function(e) {
|
|
|
1223 |
// Prepend/Give - surround the selection
|
|
|
1224 |
var chunk, cursor, selected = e.getSelection(), content = e.getContent();
|
|
|
1225 |
|
|
|
1226 |
// transform selection and set the cursor into chunked text
|
|
|
1227 |
if (selected.length === 0) {
|
|
|
1228 |
// Give extra word
|
|
|
1229 |
chunk = e.__localize('quote here');
|
|
|
1230 |
|
|
|
1231 |
e.replaceSelection('> '+chunk);
|
|
|
1232 |
|
|
|
1233 |
// Set the cursor
|
|
|
1234 |
cursor = selected.start+2;
|
|
|
1235 |
} else {
|
|
|
1236 |
if (selected.text.indexOf('\n') < 0) {
|
|
|
1237 |
chunk = selected.text;
|
|
|
1238 |
|
|
|
1239 |
e.replaceSelection('> '+chunk);
|
|
|
1240 |
|
|
|
1241 |
// Set the cursor
|
|
|
1242 |
cursor = selected.start+2;
|
|
|
1243 |
} else {
|
|
|
1244 |
var list = [];
|
|
|
1245 |
|
|
|
1246 |
list = selected.text.split('\n');
|
|
|
1247 |
chunk = list[0];
|
|
|
1248 |
|
|
|
1249 |
$.each(list,function(k,v) {
|
|
|
1250 |
list[k] = '> '+v;
|
|
|
1251 |
});
|
|
|
1252 |
|
|
|
1253 |
e.replaceSelection('\n\n'+list.join('\n'));
|
|
|
1254 |
|
|
|
1255 |
// Set the cursor
|
|
|
1256 |
cursor = selected.start+4;
|
|
|
1257 |
}
|
|
|
1258 |
}
|
|
|
1259 |
|
|
|
1260 |
// Set the cursor
|
|
|
1261 |
e.setSelection(cursor,cursor+chunk.length);
|
|
|
1262 |
}
|
|
|
1263 |
}]
|
|
|
1264 |
},{
|
|
|
1265 |
name: 'groupUtil',
|
|
|
1266 |
data: [{
|
|
|
1267 |
name: 'cmdPreview',
|
|
|
1268 |
toggle: true,
|
|
|
1269 |
hotkey: 'Ctrl+P',
|
|
|
1270 |
title: 'Preview',
|
|
|
1271 |
btnText: 'Preview',
|
|
|
1272 |
btnClass: 'btn btn-primary btn-sm',
|
|
|
1273 |
icon: { glyph: 'glyphicon glyphicon-search', fa: 'fa fa-search', 'fa-3': 'icon-search' },
|
|
|
1274 |
callback: function(e){
|
|
|
1275 |
// Check the preview mode and toggle based on this flag
|
|
|
1276 |
var isPreview = e.$isPreview,content;
|
|
|
1277 |
|
|
|
1278 |
if (isPreview === false) {
|
|
|
1279 |
// Give flag that tell the editor enter preview mode
|
|
|
1280 |
e.showPreview();
|
|
|
1281 |
} else {
|
|
|
1282 |
e.hidePreview();
|
|
|
1283 |
}
|
|
|
1284 |
}
|
|
|
1285 |
}]
|
|
|
1286 |
}]
|
|
|
1287 |
],
|
|
|
1288 |
additionalButtons:[], // Place to hook more buttons by code
|
|
|
1289 |
reorderButtonGroups:[],
|
|
|
1290 |
hiddenButtons:[], // Default hidden buttons
|
|
|
1291 |
disabledButtons:[], // Default disabled buttons
|
|
|
1292 |
footer: '',
|
|
|
1293 |
fullscreen: {
|
|
|
1294 |
enable: true,
|
|
|
1295 |
icons: {
|
|
|
1296 |
fullscreenOn: {
|
|
|
1297 |
fa: 'fa fa-expand',
|
|
|
1298 |
glyph: 'glyphicon glyphicon-fullscreen',
|
|
|
1299 |
'fa-3': 'icon-resize-full'
|
|
|
1300 |
},
|
|
|
1301 |
fullscreenOff: {
|
|
|
1302 |
fa: 'fa fa-compress',
|
|
|
1303 |
glyph: 'glyphicon glyphicon-fullscreen',
|
|
|
1304 |
'fa-3': 'icon-resize-small'
|
|
|
1305 |
}
|
|
|
1306 |
}
|
|
|
1307 |
},
|
|
|
1308 |
|
|
|
1309 |
/* Events hook */
|
|
|
1310 |
onShow: function (e) {},
|
|
|
1311 |
onPreview: function (e) {},
|
|
|
1312 |
onSave: function (e) {},
|
|
|
1313 |
onBlur: function (e) {},
|
|
|
1314 |
onFocus: function (e) {},
|
|
|
1315 |
onChange: function(e) {},
|
|
|
1316 |
onFullscreen: function(e) {},
|
|
|
1317 |
onSelect: function (e) {}
|
|
|
1318 |
};
|
|
|
1319 |
|
|
|
1320 |
$.fn.markdown.Constructor = Markdown;
|
|
|
1321 |
|
|
|
1322 |
|
|
|
1323 |
/* MARKDOWN NO CONFLICT
|
|
|
1324 |
* ==================== */
|
|
|
1325 |
|
|
|
1326 |
$.fn.markdown.noConflict = function () {
|
|
|
1327 |
$.fn.markdown = old;
|
|
|
1328 |
return this;
|
|
|
1329 |
};
|
|
|
1330 |
|
|
|
1331 |
/* MARKDOWN GLOBAL FUNCTION & DATA-API
|
|
|
1332 |
* ==================================== */
|
|
|
1333 |
var initMarkdown = function(el) {
|
|
|
1334 |
var $this = el;
|
|
|
1335 |
|
|
|
1336 |
if ($this.data('markdown')) {
|
|
|
1337 |
$this.data('markdown').showEditor();
|
|
|
1338 |
return;
|
|
|
1339 |
}
|
|
|
1340 |
|
|
|
1341 |
$this.markdown()
|
|
|
1342 |
};
|
|
|
1343 |
|
|
|
1344 |
var blurNonFocused = function(e) {
|
|
|
1345 |
var $activeElement = $(document.activeElement);
|
|
|
1346 |
|
|
|
1347 |
// Blur event
|
|
|
1348 |
$(document).find('.md-editor').each(function(){
|
|
|
1349 |
var $this = $(this),
|
|
|
1350 |
focused = $activeElement.closest('.md-editor')[0] === this,
|
|
|
1351 |
attachedMarkdown = $this.find('textarea').data('markdown') ||
|
|
|
1352 |
$this.find('div[data-provider="markdown-preview"]').data('markdown');
|
|
|
1353 |
|
|
|
1354 |
if (attachedMarkdown && !focused) {
|
|
|
1355 |
attachedMarkdown.blur();
|
|
|
1356 |
}
|
|
|
1357 |
})
|
|
|
1358 |
};
|
|
|
1359 |
|
|
|
1360 |
$(document)
|
|
|
1361 |
.on('click.markdown.data-api', '[data-provide="markdown-editable"]', function (e) {
|
|
|
1362 |
initMarkdown($(this));
|
|
|
1363 |
e.preventDefault();
|
|
|
1364 |
})
|
|
|
1365 |
.on('click focusin', function (e) {
|
|
|
1366 |
blurNonFocused(e);
|
|
|
1367 |
})
|
|
|
1368 |
.ready(function(){
|
|
|
1369 |
$('textarea[data-provide="markdown"]').each(function(){
|
|
|
1370 |
initMarkdown($(this));
|
|
|
1371 |
})
|
|
|
1372 |
});
|
|
|
1373 |
|
|
|
1374 |
}(window.jQuery);
|