forked from derobins/wmd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wmd.js
2378 lines (1923 loc) · 62.8 KB
/
wmd.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var Attacklab = Attacklab || {};
Attacklab.wmdBase = function(){
// A few handy aliases for readability.
var wmd = top.Attacklab;
var doc = top.document;
var re = top.RegExp;
var nav = top.navigator;
// Some namespaces.
wmd.Util = {};
wmd.Position = {};
wmd.Command = {};
wmd.Global = {};
var util = wmd.Util;
var position = wmd.Position;
var command = wmd.Command;
var global = wmd.Global;
// Used to work around some browser bugs where we can't use feature testing.
global.isIE = /msie/.test(nav.userAgent.toLowerCase());
global.isIE_5or6 = /msie 6/.test(nav.userAgent.toLowerCase()) || /msie 5/.test(nav.userAgent.toLowerCase());
global.isIE_7plus = global.isIE && !global.isIE_5or6;
global.isOpera = /opera/.test(nav.userAgent.toLowerCase());
global.isKonqueror = /konqueror/.test(nav.userAgent.toLowerCase());
// -------------------------------------------------------------------
// YOUR CHANGES GO HERE
//
// I've tried to localize the things you are likely to change to
// this area.
// -------------------------------------------------------------------
// The text that appears on the upper part of the dialog box when
// entering links.
var imageDialogText = "<p style='margin-top: 0px'><b>Enter the image URL.</b></p><p>You can also add a title, which will be displayed as a tool tip.</p><p>Example:<br />http://wmd-editor.com/images/cloud1.jpg \"Optional title\"</p>";
var linkDialogText = "<p style='margin-top: 0px'><b>Enter the web address.</b></p><p>You can also add a title, which will be displayed as a tool tip.</p><p>Example:<br />http://wmd-editor.com/ \"Optional title\"</p>";
// The default text that appears in the dialog input box when entering
// links.
var imageDefaultText = "http://";
var linkDefaultText = "http://";
// The location of your button images relative to the base directory.
var imageDirectory = "images/";
// Some intervals in ms. These can be adjusted to reduce the control's load.
var previewPollInterval = 500;
var pastePollInterval = 100;
// The link and title for the help button
var helpLink = "http://wmd-editor.com/";
var helpHoverTitle = "WMD website";
var helpTarget = "_blank";
// -------------------------------------------------------------------
// END OF YOUR CHANGES
// -------------------------------------------------------------------
// A collection of the important regions on the page.
// Cached so we don't have to keep traversing the DOM.
wmd.PanelCollection = function(){
this.buttonBar = doc.getElementById("wmd-button-bar");
this.preview = doc.getElementById("wmd-preview");
this.output = doc.getElementById("wmd-output");
this.input = doc.getElementById("wmd-input");
};
// This PanelCollection object can't be filled until after the page
// has loaded.
wmd.panels = undefined;
// Internet explorer has problems with CSS sprite buttons that use HTML
// lists. When you click on the background image "button", IE will
// select the non-existent link text and discard the selection in the
// textarea. The solution to this is to cache the textarea selection
// on the button's mousedown event and set a flag. In the part of the
// code where we need to grab the selection, we check for the flag
// and, if it's set, use the cached area instead of querying the
// textarea.
//
// This ONLY affects Internet Explorer (tested on versions 6, 7
// and 8) and ONLY on button clicks. Keyboard shortcuts work
// normally since the focus never leaves the textarea.
wmd.ieCachedRange = null; // cached textarea selection
wmd.ieRetardedClick = false; // flag
// Returns true if the DOM element is visible, false if it's hidden.
// Checks if display is anything other than none.
util.isVisible = function (elem) {
if (window.getComputedStyle) {
// Most browsers
return window.getComputedStyle(elem, null).getPropertyValue("display") !== "none";
}
else if (elem.currentStyle) {
// IE
return elem.currentStyle["display"] !== "none";
}
};
// Adds a listener callback to a DOM element which is fired on a specified
// event.
util.addEvent = function(elem, event, listener){
if (elem.attachEvent) {
// IE only. The "on" is mandatory.
elem.attachEvent("on" + event, listener);
}
else {
// Other browsers.
elem.addEventListener(event, listener, false);
}
};
// Removes a listener callback from a DOM element which is fired on a specified
// event.
util.removeEvent = function(elem, event, listener){
if (elem.detachEvent) {
// IE only. The "on" is mandatory.
elem.detachEvent("on" + event, listener);
}
else {
// Other browsers.
elem.removeEventListener(event, listener, false);
}
};
// Converts \r\n and \r to \n.
util.fixEolChars = function(text){
text = text.replace(/\r\n/g, "\n");
text = text.replace(/\r/g, "\n");
return text;
};
// Extends a regular expression. Returns a new RegExp
// using pre + regex + post as the expression.
// Used in a few functions where we have a base
// expression and we want to pre- or append some
// conditions to it (e.g. adding "$" to the end).
// The flags are unchanged.
//
// regex is a RegExp, pre and post are strings.
util.extendRegExp = function(regex, pre, post){
if (pre === null || pre === undefined)
{
pre = "";
}
if(post === null || post === undefined)
{
post = "";
}
var pattern = regex.toString();
var flags = "";
// Replace the flags with empty space and store them.
// Technically, this can match incorrect flags like "gmm".
var result = pattern.match(/\/([gim]*)$/);
if (result === null) {
flags = result[0];
}
else {
flags = "";
}
// Remove the flags and slash delimiters from the regular expression.
pattern = pattern.replace(/(^\/|\/[gim]*$)/g, "");
pattern = pre + pattern + post;
return new RegExp(pattern, flags);
}
// Sets the image for a button passed to the WMD editor.
// Returns a new element with the image attached.
// Adds several style properties to the image.
util.createImage = function(img){
var imgPath = imageDirectory + img;
var elem = doc.createElement("img");
elem.className = "wmd-button";
elem.src = imgPath;
return elem;
};
// This simulates a modal dialog box and asks for the URL when you
// click the hyperlink or image buttons.
//
// text: The html for the input box.
// defaultInputText: The default value that appears in the input box.
// makeLinkMarkdown: The function which is executed when the prompt is dismissed, either via OK or Cancel
util.prompt = function(text, defaultInputText, makeLinkMarkdown){
// These variables need to be declared at this level since they are used
// in multiple functions.
var dialog; // The dialog box.
var background; // The background beind the dialog box.
var input; // The text box where you enter the hyperlink.
if (defaultInputText === undefined) {
defaultInputText = "";
}
// Used as a keydown event handler. Esc dismisses the prompt.
// Key code 27 is ESC.
var checkEscape = function(key){
var code = (key.charCode || key.keyCode);
if (code === 27) {
close(true);
}
};
// Dismisses the hyperlink input box.
// isCancel is true if we don't care about the input text.
// isCancel is false if we are going to keep the text.
var close = function(isCancel){
util.removeEvent(doc.body, "keydown", checkEscape);
var text = input.value;
if (isCancel){
text = null;
}
else{
// Fixes common pasting errors.
text = text.replace('http://http://', 'http://');
text = text.replace('http://https://', 'https://');
text = text.replace('http://ftp://', 'ftp://');
if (text.indexOf('http://') === -1 && text.indexOf('ftp://') === -1 && text.indexOf('https://') === -1) {
text = 'http://' + text;
}
}
dialog.parentNode.removeChild(dialog);
background.parentNode.removeChild(background);
makeLinkMarkdown(text);
return false;
};
// Creates the background behind the hyperlink text entry box.
// Most of this has been moved to CSS but the div creation and
// browser-specific hacks remain here.
var createBackground = function(){
background = doc.createElement("div");
background.className = "wmd-prompt-background";
style = background.style;
style.position = "absolute";
style.top = "0";
style.zIndex = "1000";
// Some versions of Konqueror don't support transparent colors
// so we make the whole window transparent.
//
// Is this necessary on modern konqueror browsers?
if (global.isKonqueror){
style.backgroundColor = "transparent";
}
else if (global.isIE){
style.filter = "alpha(opacity=50)";
}
else {
style.opacity = "0.5";
}
var pageSize = position.getPageSize();
style.height = pageSize[1] + "px";
if(global.isIE){
style.left = doc.documentElement.scrollLeft;
style.width = doc.documentElement.clientWidth;
}
else {
style.left = "0";
style.width = "100%";
}
doc.body.appendChild(background);
};
// Create the text input box form/window.
var createDialog = function(){
// The main dialog box.
dialog = doc.createElement("div");
dialog.className = "wmd-prompt-dialog";
dialog.style.padding = "10px;";
dialog.style.position = "fixed";
dialog.style.width = "400px";
dialog.style.zIndex = "1001";
// The dialog text.
var question = doc.createElement("div");
question.innerHTML = text;
question.style.padding = "5px";
dialog.appendChild(question);
// The web form container for the text box and buttons.
var form = doc.createElement("form");
form.onsubmit = function(){ return close(false); };
style = form.style;
style.padding = "0";
style.margin = "0";
style.cssFloat = "left";
style.width = "100%";
style.textAlign = "center";
style.position = "relative";
dialog.appendChild(form);
// The input text box
input = doc.createElement("input");
input.type = "text";
input.value = defaultInputText;
style = input.style;
style.display = "block";
style.width = "80%";
style.marginLeft = style.marginRight = "auto";
form.appendChild(input);
// The ok button
var okButton = doc.createElement("input");
okButton.type = "button";
okButton.onclick = function(){ return close(false); };
okButton.value = "OK";
style = okButton.style;
style.margin = "10px";
style.display = "inline";
style.width = "7em";
// The cancel button
var cancelButton = doc.createElement("input");
cancelButton.type = "button";
cancelButton.onclick = function(){ return close(true); };
cancelButton.value = "Cancel";
style = cancelButton.style;
style.margin = "10px";
style.display = "inline";
style.width = "7em";
// The order of these buttons is different on macs.
if (/mac/.test(nav.platform.toLowerCase())) {
form.appendChild(cancelButton);
form.appendChild(okButton);
}
else {
form.appendChild(okButton);
form.appendChild(cancelButton);
}
util.addEvent(doc.body, "keydown", checkEscape);
dialog.style.top = "50%";
dialog.style.left = "50%";
dialog.style.display = "block";
if(global.isIE_5or6){
dialog.style.position = "absolute";
dialog.style.top = doc.documentElement.scrollTop + 200 + "px";
dialog.style.left = "50%";
}
doc.body.appendChild(dialog);
// This has to be done AFTER adding the dialog to the form if you
// want it to be centered.
dialog.style.marginTop = -(position.getHeight(dialog) / 2) + "px";
dialog.style.marginLeft = -(position.getWidth(dialog) / 2) + "px";
};
createBackground();
// Why is this in a zero-length timeout?
// Is it working around a browser bug?
top.setTimeout(function(){
createDialog();
var defTextLen = defaultInputText.length;
if (input.selectionStart !== undefined) {
input.selectionStart = 0;
input.selectionEnd = defTextLen;
}
else if (input.createTextRange) {
var range = input.createTextRange();
range.collapse(false);
range.moveStart("character", -defTextLen);
range.moveEnd("character", defTextLen);
range.select();
}
input.focus();
}, 0);
};
// UNFINISHED
// The assignment in the while loop makes jslint cranky.
// I'll change it to a better loop later.
position.getTop = function(elem, isInner){
var result = elem.offsetTop;
if (!isInner) {
while (elem = elem.offsetParent) {
result += elem.offsetTop;
}
}
return result;
};
position.getHeight = function (elem) {
return elem.offsetHeight || elem.scrollHeight;
};
position.getWidth = function (elem) {
return elem.offsetWidth || elem.scrollWidth;
};
position.getPageSize = function(){
var scrollWidth, scrollHeight;
var innerWidth, innerHeight;
// It's not very clear which blocks work with which browsers.
if(self.innerHeight && self.scrollMaxY){
scrollWidth = doc.body.scrollWidth;
scrollHeight = self.innerHeight + self.scrollMaxY;
}
else if(doc.body.scrollHeight > doc.body.offsetHeight){
scrollWidth = doc.body.scrollWidth;
scrollHeight = doc.body.scrollHeight;
}
else{
scrollWidth = doc.body.offsetWidth;
scrollHeight = doc.body.offsetHeight;
}
if(self.innerHeight){
// Non-IE browser
innerWidth = self.innerWidth;
innerHeight = self.innerHeight;
}
else if(doc.documentElement && doc.documentElement.clientHeight){
// Some versions of IE (IE 6 w/ a DOCTYPE declaration)
innerWidth = doc.documentElement.clientWidth;
innerHeight = doc.documentElement.clientHeight;
}
else if(doc.body){
// Other versions of IE
innerWidth = doc.body.clientWidth;
innerHeight = doc.body.clientHeight;
}
var maxWidth = Math.max(scrollWidth, innerWidth);
var maxHeight = Math.max(scrollHeight, innerHeight);
return [maxWidth, maxHeight, innerWidth, innerHeight];
};
// Watches the input textarea, polling at an interval and runs
// a callback function if anything has changed.
wmd.inputPoller = function(callback, interval){
var pollerObj = this;
var inputArea = wmd.panels.input;
// Stored start, end and text. Used to see if there are changes to the input.
var lastStart;
var lastEnd;
var markdown;
var killHandle; // Used to cancel monitoring on destruction.
// Checks to see if anything has changed in the textarea.
// If so, it runs the callback.
this.tick = function(){
if (!util.isVisible(inputArea)) {
return;
}
// Update the selection start and end, text.
if (inputArea.selectionStart || inputArea.selectionStart === 0) {
var start = inputArea.selectionStart;
var end = inputArea.selectionEnd;
if (start != lastStart || end != lastEnd) {
lastStart = start;
lastEnd = end;
if (markdown != inputArea.value) {
markdown = inputArea.value;
return true;
}
}
}
return false;
};
var doTickCallback = function(){
if (!util.isVisible(inputArea)) {
return;
}
// If anything has changed, call the function.
if (pollerObj.tick()) {
callback();
}
};
// Set how often we poll the textarea for changes.
var assignInterval = function(){
// previewPollInterval is set at the top of the namespace.
killHandle = top.setInterval(doTickCallback, interval);
};
this.destroy = function(){
top.clearInterval(killHandle);
};
assignInterval();
};
// Handles pushing and popping TextareaStates for undo/redo commands.
// I should rename the stack variables to list.
wmd.undoManager = function(callback){
var undoObj = this;
var undoStack = []; // A stack of undo states
var stackPtr = 0; // The index of the current state
var mode = "none";
var lastState; // The last state
var poller;
var timer; // The setTimeout handle for cancelling the timer
var inputStateObj;
// Set the mode for later logic steps.
var setMode = function(newMode, noSave){
if (mode != newMode) {
mode = newMode;
if (!noSave) {
saveState();
}
}
if (!global.isIE || mode != "moving") {
timer = top.setTimeout(refreshState, 1);
}
else {
inputStateObj = null;
}
};
var refreshState = function(){
inputStateObj = new wmd.TextareaState();
poller.tick();
timer = undefined;
};
this.setCommandMode = function(){
mode = "command";
saveState();
timer = top.setTimeout(refreshState, 0);
};
this.canUndo = function(){
return stackPtr > 1;
};
this.canRedo = function(){
if (undoStack[stackPtr + 1]) {
return true;
}
return false;
};
// Removes the last state and restores it.
this.undo = function(){
if (undoObj.canUndo()) {
if (lastState) {
// What about setting state -1 to null or checking for undefined?
lastState.restore();
lastState = null;
}
else {
undoStack[stackPtr] = new wmd.TextareaState();
undoStack[--stackPtr].restore();
if (callback) {
callback();
}
}
}
mode = "none";
wmd.panels.input.focus();
refreshState();
};
// Redo an action.
this.redo = function(){
if (undoObj.canRedo()) {
undoStack[++stackPtr].restore();
if (callback) {
callback();
}
}
mode = "none";
wmd.panels.input.focus();
refreshState();
};
// Push the input area state to the stack.
var saveState = function(){
var currState = inputStateObj || new wmd.TextareaState();
if (!currState) {
return false;
}
if (mode == "moving") {
if (!lastState) {
lastState = currState;
}
return;
}
if (lastState) {
if (undoStack[stackPtr - 1].text != lastState.text) {
undoStack[stackPtr++] = lastState;
}
lastState = null;
}
undoStack[stackPtr++] = currState;
undoStack[stackPtr + 1] = null;
if (callback) {
callback();
}
};
var handleCtrlYZ = function(event){
var handled = false;
if (event.ctrlKey || event.metaKey) {
// IE and Opera do not support charCode.
var keyCode = event.charCode || event.keyCode;
var keyCodeChar = String.fromCharCode(keyCode);
switch (keyCodeChar) {
case "y":
undoObj.redo();
handled = true;
break;
case "z":
if (!event.shiftKey) {
undoObj.undo();
}
else {
undoObj.redo();
}
handled = true;
break;
}
}
if (handled) {
if (event.preventDefault) {
event.preventDefault();
}
if (top.event) {
top.event.returnValue = false;
}
return;
}
};
// Set the mode depending on what is going on in the input area.
var handleModeChange = function(event){
if (!event.ctrlKey && !event.metaKey) {
var keyCode = event.keyCode;
if ((keyCode >= 33 && keyCode <= 40) || (keyCode >= 63232 && keyCode <= 63235)) {
// 33 - 40: page up/dn and arrow keys
// 63232 - 63235: page up/dn and arrow keys on safari
setMode("moving");
}
else if (keyCode == 8 || keyCode == 46 || keyCode == 127) {
// 8: backspace
// 46: delete
// 127: delete
setMode("deleting");
}
else if (keyCode == 13) {
// 13: Enter
setMode("newlines");
}
else if (keyCode == 27) {
// 27: escape
setMode("escape");
}
else if ((keyCode < 16 || keyCode > 20) && keyCode != 91) {
// 16-20 are shift, etc.
// 91: left window key
// I think this might be a little messed up since there are
// a lot of nonprinting keys above 20.
setMode("typing");
}
}
};
var setEventHandlers = function(){
util.addEvent(wmd.panels.input, "keypress", function(event){
// keyCode 89: y
// keyCode 90: z
if ((event.ctrlKey || event.metaKey) && (event.keyCode == 89 || event.keyCode == 90)) {
event.preventDefault();
}
});
var handlePaste = function(){
if (global.isIE || (inputStateObj && inputStateObj.text != wmd.panels.input.value)) {
if (timer == undefined) {
mode = "paste";
saveState();
refreshState();
}
}
};
// pastePollInterval is specified at the beginning of this namespace.
poller = new wmd.inputPoller(handlePaste, pastePollInterval);
util.addEvent(wmd.panels.input, "keydown", handleCtrlYZ);
util.addEvent(wmd.panels.input, "keydown", handleModeChange);
util.addEvent(wmd.panels.input, "mousedown", function(){
setMode("moving");
});
wmd.panels.input.onpaste = handlePaste;
wmd.panels.input.ondrop = handlePaste;
};
var init = function(){
setEventHandlers();
refreshState();
saveState();
};
this.destroy = function(){
if (poller) {
poller.destroy();
}
};
init();
};
// I think my understanding of how the buttons and callbacks are stored in the array is incomplete.
wmd.editor = function(previewRefreshCallback){
if (!previewRefreshCallback) {
previewRefreshCallback = function(){};
}
var inputBox = wmd.panels.input;
var offsetHeight = 0;
var editObj = this;
var mainDiv;
var mainSpan;
var div; // This name is pretty ambiguous. I should rename this.
// Used to cancel recurring events from setInterval.
var creationHandle;
var undoMgr; // The undo manager
// Perform the button's action.
var doClick = function(button){
inputBox.focus();
if (button.textOp) {
if (undoMgr) {
undoMgr.setCommandMode();
}
var state = new wmd.TextareaState();
if (!state) {
return;
}
var chunks = state.getChunks();
// Some commands launch a "modal" prompt dialog. Javascript
// can't really make a modal dialog box and the WMD code
// will continue to execute while the dialog is displayed.
// This prevents the dialog pattern I'm used to and means
// I can't do something like this:
//
// var link = CreateLinkDialog();
// makeMarkdownLink(link);
//
// Instead of this straightforward method of handling a
// dialog I have to pass any code which would execute
// after the dialog is dismissed (e.g. link creation)
// in a function parameter.
//
// Yes this is awkward and I think it sucks, but there's
// no real workaround. Only the image and link code
// create dialogs and require the function pointers.
var fixupInputArea = function(){
inputBox.focus();
if (chunks) {
state.setChunks(chunks);
}
state.restore();
previewRefreshCallback();
};
var useDefaultText = true;
var noCleanup = button.textOp(chunks, fixupInputArea, useDefaultText);
if(!noCleanup) {
fixupInputArea();
}
}
if (button.execute) {
button.execute(editObj);
}
};
var setUndoRedoButtonStates = function(){
if(undoMgr){
setupButton(document.getElementById("wmd-undo-button"), undoMgr.canUndo());
setupButton(document.getElementById("wmd-redo-button"), undoMgr.canRedo());
}
};
var setupButton = function(button, isEnabled) {
var normalYShift = "0px";
var disabledYShift = "-20px";
var highlightYShift = "-40px";
if(isEnabled) {
button.style.backgroundPosition = button.XShift + " " + normalYShift;
button.onmouseover = function(){
this.style.backgroundPosition = this.XShift + " " + highlightYShift;
};
button.onmouseout = function(){
this.style.backgroundPosition = this.XShift + " " + normalYShift;
};
// IE tries to select the background image "button" text (it's
// implemented in a list item) so we have to cache the selection
// on mousedown.
if(global.isIE) {
button.onmousedown = function() {
wmd.ieRetardedClick = true;
wmd.ieCachedRange = document.selection.createRange();
};
}
if (!button.isHelp)
{
button.onclick = function() {
if (this.onmouseout) {
this.onmouseout();
}
doClick(this);
return false;
}
}
}
else {
button.style.backgroundPosition = button.XShift + " " + disabledYShift;
button.onmouseover = button.onmouseout = button.onclick = function(){};
}
}
var makeSpritedButtonRow = function(){
var buttonBar = document.getElementById("wmd-button-bar");
var normalYShift = "0px";
var disabledYShift = "-20px";
var highlightYShift = "-40px";
var buttonRow = document.createElement("ul");
buttonRow.id = "wmd-button-row";
buttonRow = buttonBar.appendChild(buttonRow);
var boldButton = document.createElement("li");
boldButton.className = "wmd-button";
boldButton.id = "wmd-bold-button";
boldButton.title = "Strong <strong> Ctrl+B";
boldButton.XShift = "0px";
boldButton.textOp = command.doBold;
setupButton(boldButton, true);
buttonRow.appendChild(boldButton);
var italicButton = document.createElement("li");
italicButton.className = "wmd-button";
italicButton.id = "wmd-italic-button";
italicButton.title = "Emphasis <em> Ctrl+I";
italicButton.XShift = "-20px";
italicButton.textOp = command.doItalic;
setupButton(italicButton, true);
buttonRow.appendChild(italicButton);
var spacer1 = document.createElement("li");
spacer1.className = "wmd-spacer";
spacer1.id = "wmd-spacer1";
buttonRow.appendChild(spacer1);
var linkButton = document.createElement("li");
linkButton.className = "wmd-button";
linkButton.id = "wmd-link-button";
linkButton.title = "Hyperlink <a> Ctrl+L";
linkButton.XShift = "-40px";
linkButton.textOp = function(chunk, postProcessing, useDefaultText){
return command.doLinkOrImage(chunk, postProcessing, false);
};
setupButton(linkButton, true);
buttonRow.appendChild(linkButton);
var quoteButton = document.createElement("li");
quoteButton.className = "wmd-button";
quoteButton.id = "wmd-quote-button";
quoteButton.title = "Blockquote <blockquote> Ctrl+Q";
quoteButton.XShift = "-60px";
quoteButton.textOp = command.doBlockquote;
setupButton(quoteButton, true);
buttonRow.appendChild(quoteButton);
var codeButton = document.createElement("li");
codeButton.className = "wmd-button";
codeButton.id = "wmd-code-button";
codeButton.title = "Code Sample <pre><code> Ctrl+K";
codeButton.XShift = "-80px";
codeButton.textOp = command.doCode;
setupButton(codeButton, true);
buttonRow.appendChild(codeButton);
var imageButton = document.createElement("li");
imageButton.className = "wmd-button";
imageButton.id = "wmd-image-button";
imageButton.title = "Image <img> Ctrl+G";
imageButton.XShift = "-100px";
imageButton.textOp = function(chunk, postProcessing, useDefaultText){
return command.doLinkOrImage(chunk, postProcessing, true);
};
setupButton(imageButton, true);
buttonRow.appendChild(imageButton);
var spacer2 = document.createElement("li");
spacer2.className = "wmd-spacer";
spacer2.id = "wmd-spacer2";
buttonRow.appendChild(spacer2);
var olistButton = document.createElement("li");
olistButton.className = "wmd-button";
olistButton.id = "wmd-olist-button";
olistButton.title = "Numbered List <ol> Ctrl+O";
olistButton.XShift = "-120px";
olistButton.textOp = function(chunk, postProcessing, useDefaultText){
command.doList(chunk, postProcessing, true, useDefaultText);