Skip to content

Commit d11a9fc

Browse files
hohllAlexandreClose
authored andcommitted
LUTECE-2226 : Upgrade MarkitUp front editor library
1 parent d841173 commit d11a9fc

File tree

1 file changed

+128
-45
lines changed

1 file changed

+128
-45
lines changed

webapp/js/editors/markitup/jquery.markitup.js

Lines changed: 128 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// v 1.1.x
44
// Dual licensed under the MIT and GPL licenses.
55
// ----------------------------------------------------------------------------
6-
// Copyright (C) 2007-2011 Jay Salvat
6+
// Copyright (C) 2007-2012 Jay Salvat
77
// http://markitup.jaysalvat.com/
88
// ----------------------------------------------------------------------------
99
// Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -26,19 +26,26 @@
2626
// ----------------------------------------------------------------------------
2727
(function($) {
2828
$.fn.markItUp = function(settings, extraSettings) {
29-
var options, ctrlKey, shiftKey, altKey;
30-
ctrlKey = shiftKey = altKey = false;
31-
29+
var method, params, options, ctrlKey, shiftKey, altKey; ctrlKey = shiftKey = altKey = false;
30+
31+
if (typeof settings == 'string') {
32+
method = settings;
33+
params = extraSettings;
34+
}
35+
3236
options = { id: '',
3337
nameSpace: '',
3438
root: '',
39+
previewHandler: false,
3540
previewInWindow: '', // 'width=800, height=600, resizable=yes, scrollbars=yes'
41+
previewInElement: '',
3642
previewAutoRefresh: true,
3743
previewPosition: 'after',
3844
previewTemplatePath: '~/templates/preview.html',
3945
previewParser: false,
4046
previewParserPath: '',
4147
previewParserVar: 'data',
48+
previewParserAjaxType: 'POST',
4249
resizeHandle: true,
4350
beforeInsert: '',
4451
afterInsert: '',
@@ -53,13 +60,42 @@
5360
// compute markItUp! path
5461
if (!options.root) {
5562
$('script').each(function(a, tag) {
56-
miuScript = $(tag).get(0).src.match(/(.*)jquery\.markitup(\.pack)?\.js$/);
63+
var miuScript = $(tag).get(0).src.match(/(.*)jquery\.markitup(\.pack)?\.js$/);
5764
if (miuScript !== null) {
5865
options.root = miuScript[1];
5966
}
6067
});
6168
}
6269

70+
// Quick patch to keep compatibility with jQuery 1.9
71+
var uaMatch = function(ua) {
72+
ua = ua.toLowerCase();
73+
74+
var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
75+
/(webkit)[ \/]([\w.]+)/.exec(ua) ||
76+
/(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
77+
/(msie) ([\w.]+)/.exec(ua) ||
78+
ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
79+
[];
80+
81+
return {
82+
browser: match[ 1 ] || "",
83+
version: match[ 2 ] || "0"
84+
};
85+
};
86+
var matched = uaMatch( navigator.userAgent );
87+
var browser = {};
88+
89+
if (matched.browser) {
90+
browser[matched.browser] = true;
91+
browser.version = matched.version;
92+
}
93+
if (browser.chrome) {
94+
browser.webkit = true;
95+
} else if (browser.webkit) {
96+
browser.safari = true;
97+
}
98+
6399
return this.each(function() {
64100
var $$, textarea, levels, scrollPosition, caretPosition, caretOffset,
65101
clicked, hash, header, footer, previewWindow, template, iFrame, abort;
@@ -73,6 +109,20 @@
73109
options.previewParserPath = localize(options.previewParserPath);
74110
options.previewTemplatePath = localize(options.previewTemplatePath);
75111

112+
if (method) {
113+
switch(method) {
114+
case 'remove':
115+
remove();
116+
break;
117+
case 'insert':
118+
markup(params);
119+
break;
120+
default:
121+
$.error('Method ' + method + ' does not exist on jQuery.markItUp');
122+
}
123+
return;
124+
}
125+
76126
// apply the computed path to ~/
77127
function localize(data, inText) {
78128
if (inText) {
@@ -106,29 +156,29 @@
106156
footer = $('<div class="markItUpFooter"></div>').insertAfter($$);
107157

108158
// add the resize handle after textarea
109-
if (options.resizeHandle === true && $.browser.safari !== true) {
159+
if (options.resizeHandle === true && browser.safari !== true) {
110160
resizeHandle = $('<div class="markItUpResizeHandle"></div>')
111161
.insertAfter($$)
112-
.bind("mousedown", function(e) {
162+
.bind("mousedown.markItUp", function(e) {
113163
var h = $$.height(), y = e.clientY, mouseMove, mouseUp;
114164
mouseMove = function(e) {
115165
$$.css("height", Math.max(20, e.clientY+h-y)+"px");
116166
return false;
117167
};
118168
mouseUp = function(e) {
119-
$("html").unbind("mousemove", mouseMove).unbind("mouseup", mouseUp);
169+
$("html").unbind("mousemove.markItUp", mouseMove).unbind("mouseup.markItUp", mouseUp);
120170
return false;
121171
};
122-
$("html").bind("mousemove", mouseMove).bind("mouseup", mouseUp);
172+
$("html").bind("mousemove.markItUp", mouseMove).bind("mouseup.markItUp", mouseUp);
123173
});
124174
footer.append(resizeHandle);
125175
}
126176

127177
// listen key events
128-
$$.keydown(keyPressed).keyup(keyPressed);
178+
$$.bind('keydown.markItUp', keyPressed).bind('keyup', keyPressed);
129179

130180
// bind an event to catch external calls
131-
$$.bind("insertion", function(e, settings) {
181+
$$.bind("insertion.markItUp", function(e, settings) {
132182
if (settings.target !== false) {
133183
get();
134184
}
@@ -138,9 +188,13 @@
138188
});
139189

140190
// remember the last focus
141-
$$.focus(function() {
191+
$$.bind('focus.markItUp', function() {
142192
$.markItUp.focused = this;
143193
});
194+
195+
if (options.previewInElement) {
196+
refreshPreview();
197+
}
144198
}
145199

146200
// recursively build header with dropMenus from markupset
@@ -149,7 +203,7 @@
149203
$('li:hover > ul', ul).css('display', 'block');
150204
$.each(markupSet, function() {
151205
var button = this, t = '', title, li, j;
152-
title = (button.key) ? (button.name||'')+' [Ctrl+'+button.key+']' : (button.name||'');
206+
button.title ? title = (button.key) ? (button.title||'')+' [Ctrl+'+button.key+']' : (button.title||'') : title = (button.key) ? (button.name||'')+' [Ctrl+'+button.key+']' : (button.name||'');
153207
key = (button.key) ? 'accesskey="'+button.key+'"' : '';
154208
if (button.separator) {
155209
li = $('<li class="markItUpSeparator">'+(button.separator||'')+'</li>').appendTo(ul);
@@ -158,29 +212,28 @@
158212
for (j = levels.length -1; j >= 0; j--) {
159213
t += levels[j]+"-";
160214
}
161-
li = $('<li class="markItUpButton markItUpButton'+t+(i)+' '+(button.className||'')+'"><a href="" '+key+' title="'+title+'">'+(button.name||'')+'</a></li>')
162-
.bind("contextmenu", function() { // prevent contextmenu on mac and allow ctrl+click
215+
li = $('<li class="markItUpButton markItUpButton'+t+(i)+' '+(button.className||'')+'"><a href="#" '+key+' title="'+title+'">'+(button.name||'')+'</a></li>')
216+
.bind("contextmenu.markItUp", function() { // prevent contextmenu on mac and allow ctrl+click
163217
return false;
164-
}).click(function() {
165-
return false;
166-
}).bind("focusin", function(){
218+
}).bind('click.markItUp', function(e) {
219+
e.preventDefault();
220+
}).bind("focusin.markItUp", function(){
167221
$$.focus();
168-
}).mouseup(function() {
222+
}).bind('mouseup', function(e) {
169223
if (button.call) {
170-
eval(button.call)();
224+
eval(button.call)(e); // Pass the mouseup event to custom delegate
171225
}
172226
setTimeout(function() { markup(button) },1);
173227
return false;
174-
}).hover(function() {
228+
}).bind('mouseenter.markItUp', function() {
175229
$('> ul', this).show();
176230
$(document).one('click', function() { // close dropmenu if click outside
177231
$('ul ul', header).hide();
178232
}
179233
);
180-
}, function() {
234+
}).bind('mouseleave.markItUp', function() {
181235
$('> ul', this).hide();
182-
}
183-
).appendTo(ul);
236+
}).appendTo(ul);
184237
if (button.dropMenu) {
185238
levels.push(i);
186239
$(li).addClass('markItUpDropMenu').append(dropMenus(button.dropMenu));
@@ -249,9 +302,13 @@
249302
} else {
250303
string = string || selection;
251304

252-
var lines = selection.split(/\r?\n/), blocks = [];
305+
var lines = [string], blocks = [];
306+
307+
if (multiline === true) {
308+
lines = string.split(/\r?\n/);
309+
}
253310

254-
for (var l=0; l < lines.length; l++) {
311+
for (var l = 0; l < lines.length; l++) {
255312
line = lines[l];
256313
var trailingSpaces;
257314
if (trailingSpaces = line.match(/ *$/)) {
@@ -267,10 +324,12 @@
267324
block = openBlockWith + block + closeBlockWith;
268325

269326
return { block:block,
327+
openBlockWith:openBlockWith,
270328
openWith:openWith,
271329
replaceWith:replaceWith,
272330
placeHolder:placeHolder,
273-
closeWith:closeWith
331+
closeWith:closeWith,
332+
closeBlockWith:closeBlockWith
274333
};
275334
}
276335

@@ -307,9 +366,10 @@
307366
lines[i] = "";
308367
}
309368
}
369+
310370
string = { block:lines.join('\n')};
311371
start = caretPosition;
312-
len = string.block.length + (($.browser.opera) ? n-1 : 0);
372+
len = string.block.length + ((browser.opera) ? n-1 : 0);
313373
} else if (ctrlKey === true) {
314374
string = build(selection);
315375
start = caretPosition + string.openWith.length;
@@ -330,8 +390,8 @@
330390
if ((selection === '' && string.replaceWith === '')) {
331391
caretOffset += fixOperaBug(string.block);
332392

333-
start = caretPosition + string.openWith.length;
334-
len = string.block.length - string.openWith.length - string.closeWith.length;
393+
start = caretPosition + string.openBlockWith.length + string.openWith.length;
394+
len = string.block.length - string.openBlockWith.length - string.openWith.length - string.closeWith.length - string.closeBlockWith.length;
335395

336396
caretOffset = $$.val().substring(caretPosition, $$.val().length).length;
337397
caretOffset -= fixOperaBug($$.val().substring(0, caretPosition));
@@ -359,21 +419,24 @@
359419
if (previewWindow && options.previewAutoRefresh) {
360420
refreshPreview();
361421
}
422+
423+
// Triggers an input event to allow other scripts to react.
424+
textarea.dispatchEvent(new Event('input'));
362425

363426
// reinit keyevent
364427
shiftKey = altKey = ctrlKey = abort = false;
365428
}
366429

367430
// Substract linefeed in Opera
368431
function fixOperaBug(string) {
369-
if ($.browser.opera) {
432+
if (browser.opera) {
370433
return string.length - string.replace(/\n*/g, '').length;
371434
}
372435
return 0;
373436
}
374437
// Substract linefeed in IE
375438
function fixIeBug(string) {
376-
if ($.browser.msie) {
439+
if (browser.msie) {
377440
return string.length - string.replace(/\r*/g, '').length;
378441
}
379442
return 0;
@@ -393,7 +456,7 @@
393456
function set(start, len) {
394457
if (textarea.createTextRange){
395458
// quick fix to make it work on Opera 9.5
396-
if ($.browser.opera && $.browser.version >= 9.5 && len == 0) {
459+
if (browser.opera && browser.version >= 9.5 && len == 0) {
397460
return false;
398461
}
399462
range = textarea.createTextRange();
@@ -415,7 +478,7 @@
415478
scrollPosition = textarea.scrollTop;
416479
if (document.selection) {
417480
selection = document.selection.createRange().text;
418-
if ($.browser.msie) { // ie
481+
if (browser.msie) { // ie
419482
var range = document.selection.createRange(), rangeCopy = range.duplicate();
420483
rangeCopy.moveToElementText(textarea);
421484
caretPosition = -1;
@@ -436,7 +499,11 @@
436499

437500
// open preview window
438501
function preview() {
439-
if (!previewWindow || previewWindow.closed) {
502+
if (typeof options.previewHandler === 'function') {
503+
previewWindow = true;
504+
} else if (options.previewInElement) {
505+
previewWindow = $(options.previewInElement);
506+
} else if (!previewWindow || previewWindow.closed) {
440507
if (options.previewInWindow) {
441508
previewWindow = window.open('', 'preview', options.previewInWindow);
442509
$(window).unload(function() {
@@ -472,18 +539,21 @@
472539
renderPreview();
473540
}
474541

475-
function renderPreview() {
542+
function renderPreview() {
476543
var phtml;
544+
var parsedData = $$.val();
477545
if (options.previewParser && typeof options.previewParser === 'function') {
478-
var data = options.previewParser( $$.val() );
479-
writeInPreview( localize(data, 1) );
546+
parsedData = options.previewParser(parsedData);
547+
}
548+
if (options.previewHandler && typeof options.previewHandler === 'function') {
549+
options.previewHandler(parsedData);
480550
} else if (options.previewParserPath !== '') {
481551
$.ajax({
482-
type: 'POST',
552+
type: options.previewParserAjaxType,
483553
dataType: 'text',
484554
global: false,
485555
url: options.previewParserPath,
486-
data: options.previewParserVar+'='+encodeURIComponent($$.val()),
556+
data: options.previewParserVar+'='+encodeURIComponent(parsedData),
487557
success: function(data) {
488558
writeInPreview( localize(data, 1) );
489559
}
@@ -495,7 +565,7 @@
495565
dataType: 'text',
496566
global: false,
497567
success: function(data) {
498-
writeInPreview( localize(data, 1).replace(/<!-- content -->/g, $$.val()) );
568+
writeInPreview( localize(data, 1).replace(/<!-- content -->/g, parsedData) );
499569
}
500570
});
501571
}
@@ -504,7 +574,9 @@
504574
}
505575

506576
function writeInPreview(data) {
507-
if (previewWindow.document) {
577+
if (options.previewInElement) {
578+
$(options.previewInElement).html(data);
579+
} else if (previewWindow && previewWindow.document) {
508580
try {
509581
sp = previewWindow.document.documentElement.scrollTop
510582
} catch(e) {
@@ -525,7 +597,7 @@
525597

526598
if (e.type === 'keydown') {
527599
if (ctrlKey === true) {
528-
li = $('a[accesskey="'+String.fromCharCode(e.keyCode)+'"]', header).parent('li');
600+
li = $('a[accesskey="'+((e.keyCode == 13) ? '\\n' : String.fromCharCode(e.keyCode))+'"]', header).parent('li');
529601
if (li.length !== 0) {
530602
ctrlKey = false;
531603
setTimeout(function() {
@@ -566,14 +638,25 @@
566638
}
567639
}
568640

641+
function remove() {
642+
$$.unbind(".markItUp").removeClass('markItUpEditor');
643+
$$.parent('div').parent('div.markItUp').parent('div').replaceWith($$);
644+
645+
var relativeRef = $$.parent('div').parent('div.markItUp').parent('div');
646+
if (relativeRef.length) {
647+
relativeRef.replaceWith($$);
648+
}
649+
650+
$$.data('markItUp', null);
651+
}
652+
569653
init();
570654
});
571655
};
572656

573657
$.fn.markItUpRemove = function() {
574658
return this.each(function() {
575-
var $$ = $(this).unbind().removeClass('markItUpEditor');
576-
$$.parent('div').parent('div.markItUp').parent('div').replaceWith($$);
659+
$(this).markItUp('remove');
577660
}
578661
);
579662
};

0 commit comments

Comments
 (0)