bgneal@312
|
1 // ----------------------------------------------------------------------------
|
bgneal@312
|
2 // markItUp! Universal MarkUp Engine, JQuery plugin
|
bgneal@312
|
3 // v 1.1.x
|
bgneal@312
|
4 // Dual licensed under the MIT and GPL licenses.
|
bgneal@312
|
5 // ----------------------------------------------------------------------------
|
bgneal@312
|
6 // Copyright (C) 2007-2010 Jay Salvat
|
bgneal@312
|
7 // http://markitup.jaysalvat.com/
|
bgneal@312
|
8 // ----------------------------------------------------------------------------
|
bgneal@312
|
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
bgneal@312
|
10 // of this software and associated documentation files (the "Software"), to deal
|
bgneal@312
|
11 // in the Software without restriction, including without limitation the rights
|
bgneal@312
|
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
bgneal@312
|
13 // copies of the Software, and to permit persons to whom the Software is
|
bgneal@312
|
14 // furnished to do so, subject to the following conditions:
|
bgneal@312
|
15 //
|
bgneal@312
|
16 // The above copyright notice and this permission notice shall be included in
|
bgneal@312
|
17 // all copies or substantial portions of the Software.
|
bgneal@312
|
18 //
|
bgneal@312
|
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
bgneal@312
|
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
bgneal@312
|
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
bgneal@312
|
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
bgneal@312
|
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
bgneal@312
|
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
bgneal@312
|
25 // THE SOFTWARE.
|
bgneal@312
|
26 // ----------------------------------------------------------------------------
|
bgneal@312
|
27 (function($) {
|
bgneal@312
|
28 $.fn.markItUp = function(settings, extraSettings) {
|
bgneal@312
|
29 var options, ctrlKey, shiftKey, altKey;
|
bgneal@312
|
30 ctrlKey = shiftKey = altKey = false;
|
bgneal@312
|
31
|
bgneal@312
|
32 options = { id: '',
|
bgneal@312
|
33 nameSpace: '',
|
bgneal@312
|
34 root: '',
|
bgneal@312
|
35 previewInWindow: '', // 'width=800, height=600, resizable=yes, scrollbars=yes'
|
bgneal@312
|
36 previewAutoRefresh: true,
|
bgneal@312
|
37 previewPosition: 'after',
|
bgneal@312
|
38 previewTemplatePath: '~/templates/preview.html',
|
bgneal@312
|
39 previewParserPath: '',
|
bgneal@312
|
40 previewParserVar: 'data',
|
bgneal@312
|
41 resizeHandle: true,
|
bgneal@312
|
42 beforeInsert: '',
|
bgneal@312
|
43 afterInsert: '',
|
bgneal@312
|
44 onEnter: {},
|
bgneal@312
|
45 onShiftEnter: {},
|
bgneal@312
|
46 onCtrlEnter: {},
|
bgneal@312
|
47 onTab: {},
|
bgneal@312
|
48 markupSet: [ { /* set */ } ]
|
bgneal@312
|
49 };
|
bgneal@312
|
50 $.extend(options, settings, extraSettings);
|
bgneal@312
|
51
|
bgneal@312
|
52 // compute markItUp! path
|
bgneal@312
|
53 if (!options.root) {
|
bgneal@312
|
54 $('script').each(function(a, tag) {
|
bgneal@312
|
55 miuScript = $(tag).get(0).src.match(/(.*)jquery\.markitup(\.pack)?\.js$/);
|
bgneal@312
|
56 if (miuScript !== null) {
|
bgneal@312
|
57 options.root = miuScript[1];
|
bgneal@312
|
58 }
|
bgneal@312
|
59 });
|
bgneal@312
|
60 }
|
bgneal@312
|
61
|
bgneal@312
|
62 return this.each(function() {
|
bgneal@312
|
63 var $$, textarea, levels, scrollPosition, caretPosition, caretOffset,
|
bgneal@312
|
64 clicked, hash, header, footer, previewWindow, template, iFrame, abort;
|
bgneal@312
|
65 $$ = $(this);
|
bgneal@312
|
66 textarea = this;
|
bgneal@312
|
67 levels = [];
|
bgneal@312
|
68 abort = false;
|
bgneal@312
|
69 scrollPosition = caretPosition = 0;
|
bgneal@312
|
70 caretOffset = -1;
|
bgneal@312
|
71
|
bgneal@312
|
72 options.previewParserPath = localize(options.previewParserPath);
|
bgneal@312
|
73 options.previewTemplatePath = localize(options.previewTemplatePath);
|
bgneal@312
|
74
|
bgneal@312
|
75 // apply the computed path to ~/
|
bgneal@312
|
76 function localize(data, inText) {
|
bgneal@312
|
77 if (inText) {
|
bgneal@312
|
78 return data.replace(/("|')~\//g, "$1"+options.root);
|
bgneal@312
|
79 }
|
bgneal@312
|
80 return data.replace(/^~\//, options.root);
|
bgneal@312
|
81 }
|
bgneal@312
|
82
|
bgneal@312
|
83 // init and build editor
|
bgneal@312
|
84 function init() {
|
bgneal@312
|
85 id = ''; nameSpace = '';
|
bgneal@312
|
86 if (options.id) {
|
bgneal@312
|
87 id = 'id="'+options.id+'"';
|
bgneal@312
|
88 } else if ($$.attr("id")) {
|
bgneal@312
|
89 id = 'id="markItUp'+($$.attr("id").substr(0, 1).toUpperCase())+($$.attr("id").substr(1))+'"';
|
bgneal@312
|
90
|
bgneal@312
|
91 }
|
bgneal@312
|
92 if (options.nameSpace) {
|
bgneal@312
|
93 nameSpace = 'class="'+options.nameSpace+'"';
|
bgneal@312
|
94 }
|
bgneal@312
|
95 $$.wrap('<div '+nameSpace+'></div>');
|
bgneal@312
|
96 $$.wrap('<div '+id+' class="markItUp"></div>');
|
bgneal@312
|
97 $$.wrap('<div class="markItUpContainer"></div>');
|
bgneal@312
|
98 $$.addClass("markItUpEditor");
|
bgneal@312
|
99
|
bgneal@312
|
100 // add the header before the textarea
|
bgneal@312
|
101 header = $('<div class="markItUpHeader"></div>').insertBefore($$);
|
bgneal@312
|
102 $(dropMenus(options.markupSet)).appendTo(header);
|
bgneal@312
|
103
|
bgneal@312
|
104 // add the footer after the textarea
|
bgneal@312
|
105 footer = $('<div class="markItUpFooter"></div>').insertAfter($$);
|
bgneal@312
|
106
|
bgneal@312
|
107 // add the resize handle after textarea
|
bgneal@312
|
108 if (options.resizeHandle === true && $.browser.safari !== true) {
|
bgneal@312
|
109 resizeHandle = $('<div class="markItUpResizeHandle"></div>')
|
bgneal@312
|
110 .insertAfter($$)
|
bgneal@312
|
111 .bind("mousedown", function(e) {
|
bgneal@312
|
112 var h = $$.height(), y = e.clientY, mouseMove, mouseUp;
|
bgneal@312
|
113 mouseMove = function(e) {
|
bgneal@312
|
114 $$.css("height", Math.max(20, e.clientY+h-y)+"px");
|
bgneal@312
|
115 return false;
|
bgneal@312
|
116 };
|
bgneal@312
|
117 mouseUp = function(e) {
|
bgneal@312
|
118 $("html").unbind("mousemove", mouseMove).unbind("mouseup", mouseUp);
|
bgneal@312
|
119 return false;
|
bgneal@312
|
120 };
|
bgneal@312
|
121 $("html").bind("mousemove", mouseMove).bind("mouseup", mouseUp);
|
bgneal@312
|
122 });
|
bgneal@312
|
123 footer.append(resizeHandle);
|
bgneal@312
|
124 }
|
bgneal@312
|
125
|
bgneal@312
|
126 // listen key events
|
bgneal@312
|
127 $$.keydown(keyPressed).keyup(keyPressed);
|
bgneal@312
|
128
|
bgneal@312
|
129 // bind an event to catch external calls
|
bgneal@312
|
130 $$.bind("insertion", function(e, settings) {
|
bgneal@312
|
131 if (settings.target !== false) {
|
bgneal@312
|
132 get();
|
bgneal@312
|
133 }
|
bgneal@312
|
134 if (textarea === $.markItUp.focused) {
|
bgneal@312
|
135 markup(settings);
|
bgneal@312
|
136 }
|
bgneal@312
|
137 });
|
bgneal@312
|
138
|
bgneal@312
|
139 // remember the last focus
|
bgneal@312
|
140 $$.focus(function() {
|
bgneal@312
|
141 $.markItUp.focused = this;
|
bgneal@312
|
142 });
|
bgneal@312
|
143 }
|
bgneal@312
|
144
|
bgneal@312
|
145 // recursively build header with dropMenus from markupset
|
bgneal@312
|
146 function dropMenus(markupSet) {
|
bgneal@312
|
147 var ul = $('<ul></ul>'), i = 0;
|
bgneal@312
|
148 $('li:hover > ul', ul).css('display', 'block');
|
bgneal@312
|
149 $.each(markupSet, function() {
|
bgneal@312
|
150 var button = this, t = '', title, li, j;
|
bgneal@312
|
151 title = (button.key) ? (button.name||'')+' [Ctrl+'+button.key+']' : (button.name||'');
|
bgneal@312
|
152 key = (button.key) ? 'accesskey="'+button.key+'"' : '';
|
bgneal@312
|
153 if (button.separator) {
|
bgneal@312
|
154 li = $('<li class="markItUpSeparator">'+(button.separator||'')+'</li>').appendTo(ul);
|
bgneal@312
|
155 } else {
|
bgneal@312
|
156 i++;
|
bgneal@312
|
157 for (j = levels.length -1; j >= 0; j--) {
|
bgneal@312
|
158 t += levels[j]+"-";
|
bgneal@312
|
159 }
|
bgneal@312
|
160 li = $('<li class="markItUpButton markItUpButton'+t+(i)+' '+(button.className||'')+'"><a href="" '+key+' title="'+title+'">'+(button.name||'')+'</a></li>')
|
bgneal@312
|
161 .bind("contextmenu", function() { // prevent contextmenu on mac and allow ctrl+click
|
bgneal@312
|
162 return false;
|
bgneal@312
|
163 }).click(function() {
|
bgneal@312
|
164 return false;
|
bgneal@312
|
165 }).focusin(function(){
|
bgneal@312
|
166 $$.focus();
|
bgneal@312
|
167 }).mousedown(function() {
|
bgneal@312
|
168 if (button.call) {
|
bgneal@312
|
169 eval(button.call)();
|
bgneal@312
|
170 }
|
bgneal@312
|
171 setTimeout(function() { markup(button) },1);
|
bgneal@312
|
172 return false;
|
bgneal@312
|
173 }).hover(function() {
|
bgneal@312
|
174 $('> ul', this).show();
|
bgneal@312
|
175 $(document).one('click', function() { // close dropmenu if click outside
|
bgneal@312
|
176 $('ul ul', header).hide();
|
bgneal@312
|
177 }
|
bgneal@312
|
178 );
|
bgneal@312
|
179 }, function() {
|
bgneal@312
|
180 $('> ul', this).hide();
|
bgneal@312
|
181 }
|
bgneal@312
|
182 ).appendTo(ul);
|
bgneal@312
|
183 if (button.dropMenu) {
|
bgneal@312
|
184 levels.push(i);
|
bgneal@312
|
185 $(li).addClass('markItUpDropMenu').append(dropMenus(button.dropMenu));
|
bgneal@312
|
186 }
|
bgneal@312
|
187 }
|
bgneal@312
|
188 });
|
bgneal@312
|
189 levels.pop();
|
bgneal@312
|
190 return ul;
|
bgneal@312
|
191 }
|
bgneal@312
|
192
|
bgneal@312
|
193 // markItUp! markups
|
bgneal@312
|
194 function magicMarkups(string) {
|
bgneal@312
|
195 if (string) {
|
bgneal@312
|
196 string = string.toString();
|
bgneal@312
|
197 string = string.replace(/\(\!\(([\s\S]*?)\)\!\)/g,
|
bgneal@312
|
198 function(x, a) {
|
bgneal@312
|
199 var b = a.split('|!|');
|
bgneal@312
|
200 if (altKey === true) {
|
bgneal@312
|
201 return (b[1] !== undefined) ? b[1] : b[0];
|
bgneal@312
|
202 } else {
|
bgneal@312
|
203 return (b[1] === undefined) ? "" : b[0];
|
bgneal@312
|
204 }
|
bgneal@312
|
205 }
|
bgneal@312
|
206 );
|
bgneal@312
|
207 // [![prompt]!], [![prompt:!:value]!]
|
bgneal@312
|
208 string = string.replace(/\[\!\[([\s\S]*?)\]\!\]/g,
|
bgneal@312
|
209 function(x, a) {
|
bgneal@312
|
210 var b = a.split(':!:');
|
bgneal@312
|
211 if (abort === true) {
|
bgneal@312
|
212 return false;
|
bgneal@312
|
213 }
|
bgneal@312
|
214 value = prompt(b[0], (b[1]) ? b[1] : '');
|
bgneal@312
|
215 if (value === null) {
|
bgneal@312
|
216 abort = true;
|
bgneal@312
|
217 }
|
bgneal@312
|
218 return value;
|
bgneal@312
|
219 }
|
bgneal@312
|
220 );
|
bgneal@312
|
221 return string;
|
bgneal@312
|
222 }
|
bgneal@312
|
223 return "";
|
bgneal@312
|
224 }
|
bgneal@312
|
225
|
bgneal@312
|
226 // prepare action
|
bgneal@312
|
227 function prepare(action) {
|
bgneal@312
|
228 if ($.isFunction(action)) {
|
bgneal@312
|
229 action = action(hash);
|
bgneal@312
|
230 }
|
bgneal@312
|
231 return magicMarkups(action);
|
bgneal@312
|
232 }
|
bgneal@312
|
233
|
bgneal@312
|
234 // build block to insert
|
bgneal@312
|
235 function build(string) {
|
bgneal@312
|
236 openWith = prepare(clicked.openWith);
|
bgneal@312
|
237 placeHolder = prepare(clicked.placeHolder);
|
bgneal@312
|
238 replaceWith = prepare(clicked.replaceWith);
|
bgneal@312
|
239 closeWith = prepare(clicked.closeWith);
|
bgneal@312
|
240 if (replaceWith !== "") {
|
bgneal@312
|
241 block = openWith + replaceWith + closeWith;
|
bgneal@312
|
242 } else if (selection === '' && placeHolder !== '') {
|
bgneal@312
|
243 block = openWith + placeHolder + closeWith;
|
bgneal@312
|
244 } else {
|
bgneal@312
|
245 block = openWith + (string||selection) + closeWith;
|
bgneal@312
|
246 }
|
bgneal@312
|
247 return { block:block,
|
bgneal@312
|
248 openWith:openWith,
|
bgneal@312
|
249 replaceWith:replaceWith,
|
bgneal@312
|
250 placeHolder:placeHolder,
|
bgneal@312
|
251 closeWith:closeWith
|
bgneal@312
|
252 };
|
bgneal@312
|
253 }
|
bgneal@312
|
254
|
bgneal@312
|
255 // define markup to insert
|
bgneal@312
|
256 function markup(button) {
|
bgneal@312
|
257 var len, j, n, i;
|
bgneal@312
|
258 hash = clicked = button;
|
bgneal@312
|
259 get();
|
bgneal@312
|
260
|
bgneal@312
|
261 $.extend(hash, { line:"",
|
bgneal@312
|
262 root:options.root,
|
bgneal@312
|
263 textarea:textarea,
|
bgneal@312
|
264 selection:(selection||''),
|
bgneal@312
|
265 caretPosition:caretPosition,
|
bgneal@312
|
266 ctrlKey:ctrlKey,
|
bgneal@312
|
267 shiftKey:shiftKey,
|
bgneal@312
|
268 altKey:altKey
|
bgneal@312
|
269 }
|
bgneal@312
|
270 );
|
bgneal@312
|
271 // callbacks before insertion
|
bgneal@312
|
272 prepare(options.beforeInsert);
|
bgneal@312
|
273 prepare(clicked.beforeInsert);
|
bgneal@312
|
274 if (ctrlKey === true && shiftKey === true) {
|
bgneal@312
|
275 prepare(clicked.beforeMultiInsert);
|
bgneal@312
|
276 }
|
bgneal@312
|
277 $.extend(hash, { line:1 });
|
bgneal@312
|
278
|
bgneal@312
|
279 if (ctrlKey === true && shiftKey === true) {
|
bgneal@312
|
280 lines = selection.split(/\r?\n/);
|
bgneal@312
|
281 for (j = 0, n = lines.length, i = 0; i < n; i++) {
|
bgneal@312
|
282 if ($.trim(lines[i]) !== '') {
|
bgneal@312
|
283 $.extend(hash, { line:++j, selection:lines[i] } );
|
bgneal@312
|
284 lines[i] = build(lines[i]).block;
|
bgneal@312
|
285 } else {
|
bgneal@312
|
286 lines[i] = "";
|
bgneal@312
|
287 }
|
bgneal@312
|
288 }
|
bgneal@312
|
289 string = { block:lines.join('\n')};
|
bgneal@312
|
290 start = caretPosition;
|
bgneal@312
|
291 len = string.block.length + (($.browser.opera) ? n-1 : 0);
|
bgneal@312
|
292 } else if (ctrlKey === true) {
|
bgneal@312
|
293 string = build(selection);
|
bgneal@312
|
294 start = caretPosition + string.openWith.length;
|
bgneal@312
|
295 len = string.block.length - string.openWith.length - string.closeWith.length;
|
bgneal@312
|
296 len -= fixIeBug(string.block);
|
bgneal@312
|
297 } else if (shiftKey === true) {
|
bgneal@312
|
298 string = build(selection);
|
bgneal@312
|
299 start = caretPosition;
|
bgneal@312
|
300 len = string.block.length;
|
bgneal@312
|
301 len -= fixIeBug(string.block);
|
bgneal@312
|
302 } else {
|
bgneal@312
|
303 string = build(selection);
|
bgneal@312
|
304 start = caretPosition + string.block.length ;
|
bgneal@312
|
305 len = 0;
|
bgneal@312
|
306 start -= fixIeBug(string.block);
|
bgneal@312
|
307 }
|
bgneal@312
|
308 if ((selection === '' && string.replaceWith === '')) {
|
bgneal@312
|
309 caretOffset += fixOperaBug(string.block);
|
bgneal@312
|
310
|
bgneal@312
|
311 start = caretPosition + string.openWith.length;
|
bgneal@312
|
312 len = string.block.length - string.openWith.length - string.closeWith.length;
|
bgneal@312
|
313
|
bgneal@312
|
314 caretOffset = $$.val().substring(caretPosition, $$.val().length).length;
|
bgneal@312
|
315 caretOffset -= fixOperaBug($$.val().substring(0, caretPosition));
|
bgneal@312
|
316 }
|
bgneal@312
|
317 $.extend(hash, { caretPosition:caretPosition, scrollPosition:scrollPosition } );
|
bgneal@312
|
318
|
bgneal@312
|
319 if (string.block !== selection && abort === false) {
|
bgneal@312
|
320 insert(string.block);
|
bgneal@312
|
321 set(start, len);
|
bgneal@312
|
322 } else {
|
bgneal@312
|
323 caretOffset = -1;
|
bgneal@312
|
324 }
|
bgneal@312
|
325 get();
|
bgneal@312
|
326
|
bgneal@312
|
327 $.extend(hash, { line:'', selection:selection });
|
bgneal@312
|
328
|
bgneal@312
|
329 // callbacks after insertion
|
bgneal@312
|
330 if (ctrlKey === true && shiftKey === true) {
|
bgneal@312
|
331 prepare(clicked.afterMultiInsert);
|
bgneal@312
|
332 }
|
bgneal@312
|
333 prepare(clicked.afterInsert);
|
bgneal@312
|
334 prepare(options.afterInsert);
|
bgneal@312
|
335
|
bgneal@312
|
336 // refresh preview if opened
|
bgneal@312
|
337 if (previewWindow && options.previewAutoRefresh) {
|
bgneal@312
|
338 refreshPreview();
|
bgneal@312
|
339 }
|
bgneal@312
|
340
|
bgneal@312
|
341 // reinit keyevent
|
bgneal@312
|
342 shiftKey = altKey = ctrlKey = abort = false;
|
bgneal@312
|
343 }
|
bgneal@312
|
344
|
bgneal@312
|
345 // Substract linefeed in Opera
|
bgneal@312
|
346 function fixOperaBug(string) {
|
bgneal@312
|
347 if ($.browser.opera) {
|
bgneal@312
|
348 return string.length - string.replace(/\n*/g, '').length;
|
bgneal@312
|
349 }
|
bgneal@312
|
350 return 0;
|
bgneal@312
|
351 }
|
bgneal@312
|
352 // Substract linefeed in IE
|
bgneal@312
|
353 function fixIeBug(string) {
|
bgneal@312
|
354 if ($.browser.msie) {
|
bgneal@312
|
355 return string.length - string.replace(/\r*/g, '').length;
|
bgneal@312
|
356 }
|
bgneal@312
|
357 return 0;
|
bgneal@312
|
358 }
|
bgneal@312
|
359
|
bgneal@312
|
360 // add markup
|
bgneal@312
|
361 function insert(block) {
|
bgneal@312
|
362 if (document.selection) {
|
bgneal@312
|
363 var newSelection = document.selection.createRange();
|
bgneal@312
|
364 newSelection.text = block;
|
bgneal@312
|
365 } else {
|
bgneal@312
|
366 textarea.value = textarea.value.substring(0, caretPosition) + block + textarea.value.substring(caretPosition + selection.length, textarea.value.length);
|
bgneal@312
|
367 }
|
bgneal@312
|
368 }
|
bgneal@312
|
369
|
bgneal@312
|
370 // set a selection
|
bgneal@312
|
371 function set(start, len) {
|
bgneal@312
|
372 if (textarea.createTextRange){
|
bgneal@312
|
373 // quick fix to make it work on Opera 9.5
|
bgneal@312
|
374 if ($.browser.opera && $.browser.version >= 9.5 && len == 0) {
|
bgneal@312
|
375 return false;
|
bgneal@312
|
376 }
|
bgneal@312
|
377 range = textarea.createTextRange();
|
bgneal@312
|
378 range.collapse(true);
|
bgneal@312
|
379 range.moveStart('character', start);
|
bgneal@312
|
380 range.moveEnd('character', len);
|
bgneal@312
|
381 range.select();
|
bgneal@312
|
382 } else if (textarea.setSelectionRange ){
|
bgneal@312
|
383 textarea.setSelectionRange(start, start + len);
|
bgneal@312
|
384 }
|
bgneal@312
|
385 textarea.scrollTop = scrollPosition;
|
bgneal@312
|
386 textarea.focus();
|
bgneal@312
|
387 }
|
bgneal@312
|
388
|
bgneal@312
|
389 // get the selection
|
bgneal@312
|
390 function get() {
|
bgneal@312
|
391 textarea.focus();
|
bgneal@312
|
392
|
bgneal@312
|
393 scrollPosition = textarea.scrollTop;
|
bgneal@312
|
394 if (document.selection) {
|
bgneal@312
|
395 selection = document.selection.createRange().text;
|
bgneal@312
|
396 if ($.browser.msie) { // ie
|
bgneal@312
|
397 var range = document.selection.createRange(), rangeCopy = range.duplicate();
|
bgneal@312
|
398 rangeCopy.moveToElementText(textarea);
|
bgneal@312
|
399 caretPosition = -1;
|
bgneal@312
|
400 while(rangeCopy.inRange(range)) {
|
bgneal@312
|
401 rangeCopy.moveStart('character');
|
bgneal@312
|
402 caretPosition ++;
|
bgneal@312
|
403 }
|
bgneal@312
|
404 } else { // opera
|
bgneal@312
|
405 caretPosition = textarea.selectionStart;
|
bgneal@312
|
406 }
|
bgneal@312
|
407 } else { // gecko & webkit
|
bgneal@312
|
408 caretPosition = textarea.selectionStart;
|
bgneal@312
|
409 selection = textarea.value.substring(caretPosition, textarea.selectionEnd);
|
bgneal@312
|
410 }
|
bgneal@312
|
411 return selection;
|
bgneal@312
|
412 }
|
bgneal@312
|
413
|
bgneal@312
|
414 // open preview window
|
bgneal@312
|
415 function preview() {
|
bgneal@312
|
416 if (!previewWindow || previewWindow.closed) {
|
bgneal@312
|
417 if (options.previewInWindow) {
|
bgneal@312
|
418 previewWindow = window.open('', 'preview', options.previewInWindow);
|
bgneal@312
|
419 $(window).unload(function() {
|
bgneal@312
|
420 previewWindow.close();
|
bgneal@312
|
421 });
|
bgneal@312
|
422 } else {
|
bgneal@312
|
423 iFrame = $('<iframe class="markItUpPreviewFrame"></iframe>');
|
bgneal@312
|
424 if (options.previewPosition == 'after') {
|
bgneal@312
|
425 iFrame.insertAfter(footer);
|
bgneal@312
|
426 } else {
|
bgneal@312
|
427 iFrame.insertBefore(header);
|
bgneal@312
|
428 }
|
bgneal@312
|
429 previewWindow = iFrame[iFrame.length - 1].contentWindow || frame[iFrame.length - 1];
|
bgneal@312
|
430 }
|
bgneal@312
|
431 } else if (altKey === true) {
|
bgneal@312
|
432 if (iFrame) {
|
bgneal@312
|
433 iFrame.remove();
|
bgneal@312
|
434 } else {
|
bgneal@312
|
435 previewWindow.close();
|
bgneal@312
|
436 }
|
bgneal@312
|
437 previewWindow = iFrame = false;
|
bgneal@312
|
438 }
|
bgneal@312
|
439 if (!options.previewAutoRefresh) {
|
bgneal@312
|
440 refreshPreview();
|
bgneal@312
|
441 }
|
bgneal@312
|
442 if (options.previewInWindow) {
|
bgneal@312
|
443 previewWindow.focus();
|
bgneal@312
|
444 }
|
bgneal@312
|
445 }
|
bgneal@312
|
446
|
bgneal@312
|
447 // refresh Preview window
|
bgneal@312
|
448 function refreshPreview() {
|
bgneal@312
|
449 renderPreview();
|
bgneal@312
|
450 }
|
bgneal@312
|
451
|
bgneal@312
|
452 function renderPreview() {
|
bgneal@312
|
453 var phtml;
|
bgneal@312
|
454 if (options.previewParserPath !== '') {
|
bgneal@312
|
455 $.ajax( {
|
bgneal@312
|
456 type: 'POST',
|
bgneal@312
|
457 url: options.previewParserPath,
|
bgneal@312
|
458 data: options.previewParserVar+'='+encodeURIComponent($$.val()),
|
bgneal@312
|
459 success: function(data) {
|
bgneal@312
|
460 writeInPreview( localize(data, 1) );
|
bgneal@312
|
461 }
|
bgneal@312
|
462 } );
|
bgneal@312
|
463 } else {
|
bgneal@312
|
464 if (!template) {
|
bgneal@312
|
465 $.ajax( {
|
bgneal@312
|
466 url: options.previewTemplatePath,
|
bgneal@312
|
467 success: function(data) {
|
bgneal@312
|
468 writeInPreview( localize(data, 1).replace(/<!-- content -->/g, $$.val()) );
|
bgneal@312
|
469 }
|
bgneal@312
|
470 } );
|
bgneal@312
|
471 }
|
bgneal@312
|
472 }
|
bgneal@312
|
473 return false;
|
bgneal@312
|
474 }
|
bgneal@312
|
475
|
bgneal@312
|
476 function writeInPreview(data) {
|
bgneal@312
|
477 if (previewWindow.document) {
|
bgneal@312
|
478 try {
|
bgneal@312
|
479 sp = previewWindow.document.documentElement.scrollTop
|
bgneal@312
|
480 } catch(e) {
|
bgneal@312
|
481 sp = 0;
|
bgneal@312
|
482 }
|
bgneal@312
|
483 previewWindow.document.open();
|
bgneal@312
|
484 previewWindow.document.write(data);
|
bgneal@312
|
485 previewWindow.document.close();
|
bgneal@312
|
486 previewWindow.document.documentElement.scrollTop = sp;
|
bgneal@312
|
487 }
|
bgneal@312
|
488 }
|
bgneal@312
|
489
|
bgneal@312
|
490 // set keys pressed
|
bgneal@312
|
491 function keyPressed(e) {
|
bgneal@312
|
492 shiftKey = e.shiftKey;
|
bgneal@312
|
493 altKey = e.altKey;
|
bgneal@312
|
494 ctrlKey = (!(e.altKey && e.ctrlKey)) ? e.ctrlKey : false;
|
bgneal@312
|
495
|
bgneal@312
|
496 if (e.type === 'keydown') {
|
bgneal@312
|
497 if (ctrlKey === true) {
|
bgneal@312
|
498 li = $("a[accesskey="+String.fromCharCode(e.keyCode)+"]", header).parent('li');
|
bgneal@312
|
499 if (li.length !== 0) {
|
bgneal@312
|
500 ctrlKey = false;
|
bgneal@312
|
501 setTimeout(function() {
|
bgneal@312
|
502 li.triggerHandler('mousedown');
|
bgneal@312
|
503 },1);
|
bgneal@312
|
504 return false;
|
bgneal@312
|
505 }
|
bgneal@312
|
506 }
|
bgneal@312
|
507 if (e.keyCode === 13 || e.keyCode === 10) { // Enter key
|
bgneal@312
|
508 if (ctrlKey === true) { // Enter + Ctrl
|
bgneal@312
|
509 ctrlKey = false;
|
bgneal@312
|
510 markup(options.onCtrlEnter);
|
bgneal@312
|
511 return options.onCtrlEnter.keepDefault;
|
bgneal@312
|
512 } else if (shiftKey === true) { // Enter + Shift
|
bgneal@312
|
513 shiftKey = false;
|
bgneal@312
|
514 markup(options.onShiftEnter);
|
bgneal@312
|
515 return options.onShiftEnter.keepDefault;
|
bgneal@312
|
516 } else { // only Enter
|
bgneal@312
|
517 markup(options.onEnter);
|
bgneal@312
|
518 return options.onEnter.keepDefault;
|
bgneal@312
|
519 }
|
bgneal@312
|
520 }
|
bgneal@312
|
521 if (e.keyCode === 9) { // Tab key
|
bgneal@312
|
522 if (shiftKey == true || ctrlKey == true || altKey == true) {
|
bgneal@312
|
523 return false;
|
bgneal@312
|
524 }
|
bgneal@312
|
525 if (caretOffset !== -1) {
|
bgneal@312
|
526 get();
|
bgneal@312
|
527 caretOffset = $$.val().length - caretOffset;
|
bgneal@312
|
528 set(caretOffset, 0);
|
bgneal@312
|
529 caretOffset = -1;
|
bgneal@312
|
530 return false;
|
bgneal@312
|
531 } else {
|
bgneal@312
|
532 markup(options.onTab);
|
bgneal@312
|
533 return options.onTab.keepDefault;
|
bgneal@312
|
534 }
|
bgneal@312
|
535 }
|
bgneal@312
|
536 }
|
bgneal@312
|
537 }
|
bgneal@312
|
538
|
bgneal@312
|
539 init();
|
bgneal@312
|
540 });
|
bgneal@312
|
541 };
|
bgneal@312
|
542
|
bgneal@312
|
543 $.fn.markItUpRemove = function() {
|
bgneal@312
|
544 return this.each(function() {
|
bgneal@312
|
545 var $$ = $(this).unbind().removeClass('markItUpEditor');
|
bgneal@312
|
546 $$.parent('div').parent('div.markItUp').parent('div').replaceWith($$);
|
bgneal@312
|
547 }
|
bgneal@312
|
548 );
|
bgneal@312
|
549 };
|
bgneal@312
|
550
|
bgneal@312
|
551 $.markItUp = function(settings) {
|
bgneal@312
|
552 var options = { target:false };
|
bgneal@312
|
553 $.extend(options, settings);
|
bgneal@312
|
554 if (options.target) {
|
bgneal@312
|
555 return $(options.target).each(function() {
|
bgneal@312
|
556 $(this).focus();
|
bgneal@312
|
557 $(this).trigger('insertion', [options]);
|
bgneal@312
|
558 });
|
bgneal@312
|
559 } else {
|
bgneal@312
|
560 $('textarea').trigger('insertion', [options]);
|
bgneal@312
|
561 }
|
bgneal@312
|
562 };
|
bgneal@312
|
563 })(jQuery);
|