-
Notifications
You must be signed in to change notification settings - Fork 51
/
mathquill_modified.js
executable file
·2484 lines (2188 loc) · 74.7 KB
/
mathquill_modified.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
/**
* Copyright 2010 Jay and Han (laughinghan@gmail.com)
* License, Usage and Readme at http://mathquill.com
*/
(function($){ //takes in the jQuery function as an argument
var _, //temp variable of prototypes
undefined,
jQueryDataKey = '[[mathquill internal data]]';
/*************************************************
* Abstract base classes of blocks and commands.
************************************************/
/**
* MathElement is the core Math DOM tree node prototype.
* Both MathBlock's and MathCommand's descend from it.
*/
function MathElement(){}
_ = MathElement.prototype;
_.prev = 0;
_.next = 0;
_.parent = 0;
_.firstChild = 0;
_.lastChild = 0;
_.eachChild = function(fn) {
for (var child = this.firstChild; child; child = child.next)
if (fn.call(this, child) === false) break;
return this;
};
_.foldChildren = function(fold, fn) {
this.eachChild(function(child) {
fold = fn.call(this, fold, child);
});
return fold;
};
_.keydown = function(e) {
return this.parent.keydown(e);
};
_.textInput = function(ch) {
return this.parent.textInput(ch);
};
/**
* Commands and operators, like subscripts, exponents, or fractions.
* Descendant commands are organized into blocks.
* May be passed a MathFragment that's being replaced.
*/
function MathCommand(cmd, html_template, text_template, replacedFragment) {
if (!arguments.length) return;
var self = this; // minifier optimization
self.cmd = cmd;
if (html_template) self.html_template = html_template;
if (text_template) self.text_template = text_template;
self.jQ = $(self.html_template[0]).data(jQueryDataKey, {cmd: self});
self.initBlocks(replacedFragment);
}
_ = MathCommand.prototype = new MathElement;
_.initBlocks = function(replacedFragment) {
var self = this;
//single-block commands
if (self.html_template.length === 1) {
self.firstChild =
self.lastChild =
self.jQ.data(jQueryDataKey).block =
(replacedFragment && replacedFragment.blockify()) || new MathBlock;
self.firstChild.parent = self;
self.firstChild.jQ = self.jQ.append(self.firstChild.jQ);
return;
}
//otherwise, the succeeding elements of html_template should be child blocks
var newBlock, prev, num_blocks = self.html_template.length;
this.firstChild = newBlock = prev =
(replacedFragment && replacedFragment.blockify()) || new MathBlock;
newBlock.parent = self;
newBlock.jQ = $(self.html_template[1])
.data(jQueryDataKey, {block: newBlock})
.append(newBlock.jQ)
.appendTo(self.jQ);
newBlock.blur();
for (var i = 2; i < num_blocks; i += 1) {
newBlock = new MathBlock;
newBlock.parent = self;
newBlock.prev = prev;
prev.next = newBlock;
prev = newBlock;
newBlock.jQ = $(self.html_template[i])
.data(jQueryDataKey, {block: newBlock})
.appendTo(self.jQ);
newBlock.blur();
}
self.lastChild = newBlock;
};
_.latex = function() {
return this.foldChildren(this.cmd, function(latex, child) {
return latex + '{' + (child.latex() || ' ') + '}';
});
};
_.text = function() {
var i = 0;
return this.foldChildren(this.text_template[i], function(text, child) {
i += 1;
var child_text = child.text();
if (text && this.text_template[i] === '('
&& child_text[0] === '(' && child_text.slice(-1) === ')')
return text + child_text.slice(1, -1) + this.text_template[i];
return text + child.text() + (this.text_template[i] || '');
});
};
_.remove = function() {
var self = this,
prev = self.prev,
next = self.next,
parent = self.parent;
if (prev)
prev.next = next;
else
parent.firstChild = next;
if (next)
next.prev = prev;
else
parent.lastChild = prev;
self.jQ.remove();
return self;
};
_.respace = $.noop; //placeholder for context-sensitive spacing
_.placeCursor = function(cursor) {
//append the cursor to the first empty child, or if none empty, the last one
cursor.appendTo(this.foldChildren(this.firstChild, function(prev, child) {
return prev.isEmpty() ? prev : child;
}));
};
_.isEmpty = function() {
return this.foldChildren(true, function(isEmpty, child) {
return isEmpty && child.isEmpty();
});
};
/**
* Lightweight command without blocks or children.
*/
function Symbol(cmd, html, text) {
MathCommand.call(this, cmd, [ html ],
[ text || (cmd && cmd.length > 1 ? cmd.slice(1) : cmd) ]);
}
_ = Symbol.prototype = new MathCommand;
_.initBlocks = $.noop;
_.latex = function(){ return this.cmd; };
_.text = function(){ return this.text_template; };
_.placeCursor = $.noop;
_.isEmpty = function(){ return true; };
/**
* Children and parent of MathCommand's. Basically partitions all the
* symbols and operators that descend (in the Math DOM tree) from
* ancestor operators.
*/
function MathBlock(){}
_ = MathBlock.prototype = new MathElement;
_.latex = function() {
return this.foldChildren('', function(latex, child) {
return latex + child.latex();
});
};
_.text = function() {
return this.firstChild === this.lastChild ?
this.firstChild.text() :
this.foldChildren('(', function(text, child) {
return text + child.text();
}) + ')';
};
_.isEmpty = function() {
return this.firstChild === 0 && this.lastChild === 0;
};
_.focus = function() {
this.jQ.addClass('hasCursor');
if (this.isEmpty())
this.jQ.removeClass('empty');
return this;
};
_.blur = function() {
this.jQ.removeClass('hasCursor');
if (this.isEmpty())
this.jQ.addClass('empty');
return this;
};
/**
* An entity outside the Math DOM tree with one-way pointers (so it's only
* a "view" of part of the tree, not an actual node/entity in the tree)
* that delimit a list of symbols and operators.
*/
function MathFragment(parent, prev, next) {
if (!arguments.length) return;
var self = this;
self.parent = parent;
self.prev = prev || 0; //so you can do 'new MathFragment(block)' without
self.next = next || 0; //ending up with this.prev or this.next === undefined
self.jQinit(self.fold($(), function(jQ, child){ return child.jQ.add(jQ); }));
}
_ = MathFragment.prototype;
_.remove= MathCommand.prototype.remove;
_.jQinit = function(children) {
this.jQ = children;
};
_.each = function(fn) {
for (var el = this.prev.next || this.parent.firstChild; el !== this.next; el = el.next)
if (fn.call(this, el) === false) break;
return this;
};
_.fold = function(fold, fn) {
this.each(function(el) {
fold = fn.call(this, fold, el);
});
return fold;
};
_.latex = function() {
return this.fold('', function(latex, el){ return latex + el.latex(); });
};
_.blockify = function() {
var self = this,
prev = self.prev,
next = self.next,
parent = self.parent,
newBlock = new MathBlock,
newFirstChild = newBlock.firstChild = prev.next || parent.firstChild,
newLastChild = newBlock.lastChild = next.prev || parent.lastChild;
if (prev)
prev.next = next;
else
parent.firstChild = next;
if (next)
next.prev = prev;
else
parent.lastChild = prev;
newFirstChild.prev = self.prev = 0;
newLastChild.next = self.next = 0;
self.parent = newBlock;
self.each(function(el){ el.parent = newBlock; });
newBlock.jQ = self.jQ;
return newBlock;
};
/*********************************************
* Root math elements with event delegation.
********************************************/
function createRoot(jQ, root, textbox, editable) {
var contents = jQ.contents().detach();
if (!textbox)
jQ.addClass('mathquill-rendered-math');
root.jQ = jQ.data(jQueryDataKey, {
block: root,
revert: function() {
jQ.empty().unbind('.mathquill')
.removeClass('mathquill-rendered-math mathquill-editable mathquill-textbox')
.append(contents);
}
});
var cursor = root.cursor = new Cursor(root);
root.renderLatex(contents.text());
if (!editable) //if static, quit once we render the LaTeX
return;
root.textarea = $('<span class="textarea"><textarea></textarea></span>')
.prependTo(jQ.addClass('mathquill-editable'));
var textarea = root.textarea.children();
if (textbox)
jQ.addClass('mathquill-textbox');
textarea.focus(function(e) {
if (!cursor.parent)
cursor.appendTo(root);
cursor.parent.jQ.addClass('hasCursor');
if (cursor.selection) {
cursor.selection.jQ.removeClass('blur');
setTimeout(function(){ cursor.selectLatex(); });
} else
cursor.show();
e.stopPropagation();
}).blur(function(e) {
cursor.hide().parent.blur();
if (cursor.selection)
cursor.selection.jQ.addClass('blur');
e.stopPropagation();
});
//trigger virtual textInput event (see Wiki page "Keyboard Events")
function textInput() {
if (skipTextInput) return;
var text = textarea.val();
if (!text) return;
textarea.val('');
// textarea can contain more than one character
// when typing quickly on slower platforms;
// so process each character separately
for (var i=0; i<text.length; i++) {
cursor.parent.textInput(text[i]);
}
}
var lastKeydn = {}, skipTextInput = false; //see Wiki page "Keyboard Events"
jQ.bind('focus.mathquill blur.mathquill', function(e) {
textarea.trigger(e);
}).bind('keydown.mathquill', function(e) { //see Wiki page "Keyboard Events"
lastKeydn.evt = e;
lastKeydn.happened = true;
lastKeydn.returnValue = cursor.parent.keydown(e);
if (lastKeydn.returnValue)
return true;
else {
e.stopImmediatePropagation();
return false;
}
}).bind('keypress.mathquill', function(e) {
//on auto-repeated key events, keypress may get triggered but not keydown
// (see Wiki page "Keyboard Events")
if (lastKeydn.happened)
lastKeydn.happened = false;
else
lastKeydn.returnValue = cursor.parent.keydown(lastKeydn.evt);
//prevent default and cancel keypress if keydown returned false,
//even in browsers where that doesn't automatically happen
// (see Wiki page "Keyboard Events")
if (!lastKeydn.returnValue)
return false;
//after keypress event, trigger virtual textInput event if text was
//input to textarea
// (see Wiki page "Keyboard Events")
skipTextInput = false;
setTimeout(textInput);
}).bind('mousedown.mathquill', function(e) {
cursor.seek($(e.target), e.pageX, e.pageY).blink = $.noop;
anticursor = new Cursor(root);
anticursor.jQ = anticursor._jQ = $();
if (cursor.next)
anticursor.insertBefore(cursor.next);
else
anticursor.appendTo(cursor.parent);
jQ.mousemove(mousemove);
$(document).mousemove(docmousemove).mouseup(mouseup);
setTimeout(function(){textarea.focus();});
}).bind('cut', function() {
if (cursor.selection)
cursor.deleteSelection();
}).bind('copy', function() {
skipTextInput = true;
}).bind('paste', function() {
skipTextInput = true;
setTimeout(function() {
cursor.writeLatex(textarea.val()).clearSelection();
});
}).bind('selectstart.mathquill', function(e) {
if (e.target != textarea[0])
e.preventDefault();
e.stopPropagation();
}).blur();
function mousemove(e) {
cursor.seek($(e.target), e.pageX, e.pageY);
if (cursor.prev === anticursor.prev && cursor.parent === anticursor.parent)
cursor.clearSelection();
else
cursor.selectFrom(anticursor);
return false;
}
function docmousemove(e) {
delete e.target;
return mousemove(e);
}
function mouseup(e) {
anticursor = undefined;
cursor.blink = blink;
if (!cursor.selection) cursor.show();
jQ.unbind('mousemove', mousemove);
$(document).unbind('mousemove', docmousemove).unbind('mouseup', mouseup);
}
var anticursor, blink = cursor.blink;
}
function RootMathBlock(){}
_ = RootMathBlock.prototype = new MathBlock;
_.latex = function() {
return MathBlock.prototype.latex.call(this).replace(/(\\[a-z]+) (?![a-z])/ig,'$1');
};
_.text = function() {
return this.foldChildren('', function(text, child) {
return text + child.text();
});
};
_.renderLatex = function(latex) {
this.jQ.children().slice(1).remove();
this.firstChild = this.lastChild = 0;
this.cursor.appendTo(this).writeLatex(latex);
this.blur();
};
_.keydown = function(e)
{
this.skipTextInput = true;
e.ctrlKey = e.ctrlKey || e.metaKey;
switch ((e.originalEvent && e.originalEvent.keyIdentifier) || e.which) {
case 8: //backspace
case 'Backspace':
case 'U+0008':
if (e.ctrlKey)
while (this.cursor.prev || this.cursor.selection)
this.cursor.backspace();
else
this.cursor.backspace();
break;
case 27: //may as well be the same as tab until we figure out what to do with it
case 'Esc':
case 'U+001B':
case 9: //tab
case 'Tab':
case 'U+0009':
if (e.ctrlKey) break;
var parent = this.cursor.parent;
if (e.shiftKey) { //shift+Tab = go one block left if it exists, else escape left.
if (parent === this) //cursor is in root editable, continue default
break;
else if (parent.prev) //go one block left
this.cursor.appendTo(parent.prev);
else //get out of the block
this.cursor.insertBefore(parent.parent);
}
else { //plain Tab = go one block right if it exists, else escape right.
if (parent === this) //cursor is in root editable, continue default
return this.skipTextInput = true;
else if (parent.next) //go one block right
this.cursor.prependTo(parent.next);
else //get out of the block
this.cursor.insertAfter(parent.parent);
}
this.cursor.clearSelection();
break;
case 13: //enter
case 'Enter':
e.preventDefault();
return true;
case 35: //end
case 'End':
if (e.shiftKey)
while (this.cursor.next || (e.ctrlKey && this.cursor.parent !== this))
this.cursor.selectRight();
else //move to the end of the root block or the current block.
this.cursor.clearSelection().appendTo(e.ctrlKey ? this : this.cursor.parent);
break;
case 36: //home
case 'Home':
if (e.shiftKey)
while (this.cursor.prev || (e.ctrlKey && this.cursor.parent !== this))
this.cursor.selectLeft();
else //move to the start of the root block or the current block.
this.cursor.clearSelection().prependTo(e.ctrlKey ? this : this.cursor.parent);
break;
case 37: //left
case 'Left':
if (e.ctrlKey) break;
if (e.shiftKey)
this.cursor.selectLeft();
else
this.cursor.moveLeft();
break;
case 38: //up
case 'Up':
if (e.ctrlKey) break;
if (e.shiftKey) {
if (this.cursor.prev)
while (this.cursor.prev)
this.cursor.selectLeft();
else
this.cursor.selectLeft();
}
else if (this.cursor.parent.prev)
this.cursor.clearSelection().appendTo(this.cursor.parent.prev);
else if (this.cursor.prev)
this.cursor.clearSelection().prependTo(this.cursor.parent);
else if (this.cursor.parent !== this)
this.cursor.clearSelection().insertBefore(this.cursor.parent.parent);
break;
case 39: //right
case 'Right':
if (e.ctrlKey) break;
if (e.shiftKey)
this.cursor.selectRight();
else
this.cursor.moveRight();
break;
case 40: //down
case 'Down':
if (e.ctrlKey) break;
if (e.shiftKey) {
if (this.cursor.next)
while (this.cursor.next)
this.cursor.selectRight();
else
this.cursor.selectRight();
}
else if (this.cursor.parent.next)
this.cursor.clearSelection().prependTo(this.cursor.parent.next);
else if (this.cursor.next)
this.cursor.clearSelection().appendTo(this.cursor.parent);
else if (this.cursor.parent !== this)
this.cursor.clearSelection().insertAfter(this.cursor.parent.parent);
break;
case 46: //delete
case 'Del':
case 'U+007F':
if (e.ctrlKey)
while (this.cursor.next || this.cursor.selection)
this.cursor.deleteForward();
else
this.cursor.deleteForward();
break;
case 65: //the 'A' key, as in Ctrl+A Select All
case 'A':
case 'U+0041':
if (e.ctrlKey && !e.shiftKey && !e.altKey) {
if (this !== this.cursor.root) //so not stopPropagation'd at RootMathCommand
return this.parent.keydown(e);
this.cursor.clearSelection().appendTo(this);
while (this.cursor.prev)
this.cursor.selectLeft();
e.preventDefault();
return false;
}
else
this.skipTextInput = false;
return true;
default:
this.skipTextInput = false;
return true;
}
return false;
};
_.textInput = function(ch) {
if (!this.skipTextInput)
this.cursor.write(ch);
};
function RootMathCommand(cursor) {
MathCommand.call(this, '$');
this.firstChild.cursor = cursor;
this.firstChild.textInput = function(ch) {
if (this.skipTextInput) return;
if (ch !== '$' || cursor.parent !== this)
cursor.write(ch);
else if (this.isEmpty()) {
cursor.insertAfter(this.parent).backspace()
.insertNew(new VanillaSymbol('\\$','$')).show();
}
else if (!cursor.next)
cursor.insertAfter(this.parent);
else if (!cursor.prev)
cursor.insertBefore(this.parent);
else
cursor.write(ch);
};
}
_ = RootMathCommand.prototype = new MathCommand;
_.html_template = ['<span class="mathquill-rendered-math"></span>'];
_.initBlocks = function() {
this.firstChild =
this.lastChild =
this.jQ.data(jQueryDataKey).block =
new RootMathBlock;
this.firstChild.parent = this;
this.firstChild.jQ = this.jQ;
};
_.latex = function() {
return '$' + this.firstChild.latex() + '$';
};
function RootTextBlock(){}
_ = RootTextBlock.prototype = new MathBlock;
_.renderLatex = function(latex) {
var self = this, cursor = self.cursor;
self.jQ.children().slice(1).remove();
self.firstChild = self.lastChild = 0;
cursor.show().appendTo(self);
latex = latex.match(/(?:\\\$|[^$])+|\$(?:\\\$|[^$])*\$|\$(?:\\\$|[^$])*$/g) || '';
for (var i = 0; i < latex.length; i += 1) {
var chunk = latex[i];
if (chunk[0] === '$') {
if (chunk[-1+chunk.length] === '$' && chunk[-2+chunk.length] !== '\\')
chunk = chunk.slice(1, -1);
else
chunk = chunk.slice(1);
var root = new RootMathCommand(cursor);
cursor.insertNew(root);
root.firstChild.renderLatex(chunk);
cursor.show().insertAfter(root);
}
else {
for (var j = 0; j < chunk.length; j += 1)
this.cursor.insertNew(new VanillaSymbol(chunk[j]));
}
}
};
_.keydown = RootMathBlock.prototype.keydown;
_.textInput = function(ch) {
if (this.skipTextInput) return;
this.cursor.deleteSelection();
if (ch === '$')
this.cursor.insertNew(new RootMathCommand(this.cursor));
else
this.cursor.insertNew(new VanillaSymbol(ch));
};
/***************************
* Commands and Operators.
**************************/
var CharCmds = {}, LatexCmds = {}; //single character commands, LaTeX commands
function proto(parent, child) { //shorthand for prototyping
child.prototype = parent.prototype;
return child;
}
function SupSub(cmd, html, text, replacedFragment) {
MathCommand.call(this, cmd, [ html ], [ text ], replacedFragment);
}
_ = SupSub.prototype = new MathCommand;
_.latex = function() {
var latex = this.firstChild.latex();
if (latex.length === 1)
return this.cmd + latex;
else
return this.cmd + '{' + (latex || ' ') + '}';
};
_.redraw = function() {
this.respace();
if (this.next)
this.next.respace();
if (this.prev)
this.prev.respace();
};
_.respace = function() {
if (
this.prev.cmd === '\\int ' || (
this.prev instanceof SupSub && this.prev.cmd != this.cmd &&
this.prev.prev && this.prev.prev.cmd === '\\int '
)
) {
if (!this.limit) {
this.limit = true;
this.jQ.addClass('limit');
}
}
else {
if (this.limit) {
this.limit = false;
this.jQ.removeClass('limit');
}
}
if (this.respaced = this.prev instanceof SupSub && this.prev.cmd != this.cmd && !this.prev.respaced) {
if (this.limit && this.cmd === '_') {
this.jQ.css({
left: -.25-this.prev.jQ.outerWidth()/+this.jQ.css('fontSize').slice(0,-2)+'em',
marginRight: .1-Math.min(this.jQ.outerWidth(), this.prev.jQ.outerWidth())/+this.jQ.css('fontSize').slice(0,-2)+'em' //1px adjustment very important!
});
}
else {
this.jQ.css({
left: -this.prev.jQ.outerWidth()/+this.jQ.css('fontSize').slice(0,-2)+'em',
marginRight: .1-Math.min(this.jQ.outerWidth(), this.prev.jQ.outerWidth())/+this.jQ.css('fontSize').slice(0,-2)+'em' //1px adjustment very important!
});
}
}
else if (this.limit && this.cmd === '_') {
this.jQ.css({
left: '-.25em',
marginRight: ''
});
}
else {
this.jQ.css({
left: '',
marginRight: ''
});
}
return this;
};
LatexCmds.subscript = LatexCmds._ = proto(SupSub, function(replacedFragment) {
SupSub.call(this, '_', '<sub></sub>', '_', replacedFragment);
});
LatexCmds.superscript =
LatexCmds.supscript =
LatexCmds['^'] = proto(SupSub, function(replacedFragment) {
SupSub.call(this, '^', '<sup></sup>', '**', replacedFragment);
});
function Fraction(replacedFragment) {
MathCommand.call(this, '\\frac', undefined, undefined, replacedFragment);
this.jQ.append('<span style="width:0"> </span>');
}
_ = Fraction.prototype = new MathCommand;
_.html_template = [
'<span class="fraction"></span>',
'<span class="numerator"></span>',
'<span class="denominator"></span>'
];
_.text_template = ['(', '/', ')'];
LatexCmds.frac = LatexCmds.fraction = Fraction;
function LiveFraction() {
Fraction.apply(this, arguments);
}
_ = LiveFraction.prototype = new Fraction;
_.placeCursor = function(cursor) {
if (this.firstChild.isEmpty()) {
var prev = this.prev;
while (prev &&
!(
prev instanceof BinaryOperator ||
prev instanceof TextBlock ||
prev instanceof BigSymbol
) //lookbehind for operator
)
prev = prev.prev;
if (prev instanceof BigSymbol && prev.next instanceof SupSub) {
prev = prev.next;
if (prev.next instanceof SupSub && prev.next.cmd != prev.cmd)
prev = prev.next;
}
if (prev !== this.prev) {
var newBlock = new MathFragment(this.parent, prev, this).blockify();
newBlock.jQ = this.firstChild.jQ.empty().removeClass('empty').append(newBlock.jQ).data(jQueryDataKey, { block: newBlock });
newBlock.next = this.lastChild;
newBlock.parent = this;
this.firstChild = this.lastChild.prev = newBlock;
}
}
cursor.appendTo(this.lastChild);
};
CharCmds['/'] = LiveFraction;
function SquareRoot(replacedFragment) {
MathCommand.call(this, '\\sqrt', undefined, undefined, replacedFragment);
}
_ = SquareRoot.prototype = new MathCommand;
_.html_template = [
'<span><span class="sqrt-prefix">√</span></span>',
'<span class="sqrt-stem"></span>'
];
_.text_template = ['sqrt(', ')'];
_.redraw = function() {
var block = this.lastChild.jQ, height = block.outerHeight(true);
block.css({
borderTopWidth: height/28+1 // NOTE: Formula will need to change if our font isn't Symbola
}).prev().css({
fontSize: .9*height/+block.css('fontSize').slice(0,-2)+'em'
});
};
LatexCmds.sqrt = LatexCmds['√'] = SquareRoot;
function NthRoot(replacedFragment) {
SquareRoot.call(this, replacedFragment);
this.jQ = this.firstChild.jQ.detach().add(this.jQ);
}
_ = NthRoot.prototype = new SquareRoot;
_.html_template = [
'<span><span class="sqrt-prefix">√</span></span>',
'<sup class="nthroot"></sup>',
'<span class="sqrt-stem"></span>'
];
_.text_template = ['sqrt[', '](', ')'];
_.latex = function() {
return '\\sqrt['+this.firstChild.latex()+']{'+this.lastChild.latex()+'}';
};
LatexCmds.nthroot = NthRoot;
// Round/Square/Curly/Angle Brackets (aka Parens/Brackets/Braces)
function Bracket(open, close, cmd, end, replacedFragment) {
MathCommand.call(this, '\\left'+cmd,
['<span><span class="paren">'+open+'</span><span></span><span class="paren">'+close+'</span></span>'],
[open, close],
replacedFragment);
this.end = '\\right'+end;
}
_ = Bracket.prototype = new MathCommand;
_.initBlocks = function(replacedFragment) {
this.firstChild = this.lastChild =
(replacedFragment && replacedFragment.blockify()) || new MathBlock;
this.firstChild.parent = this;
this.firstChild.jQ = this.jQ.children(':eq(1)')
.data(jQueryDataKey, {block: this.firstChild})
.append(this.firstChild.jQ);
};
_.latex = function() {
return this.cmd + this.firstChild.latex() + this.end;
};
_.redraw = function() {
var block = this.firstChild.jQ;
block.prev().add(block.next()).css('fontSize', block.outerHeight()/(+block.css('fontSize').slice(0,-2)*1.02)+'em');
};
LatexCmds.lbrace = CharCmds['{'] = proto(Bracket, function(replacedFragment) {
Bracket.call(this, '{', '}', '\\{', '\\}', replacedFragment);
});
LatexCmds.langle = LatexCmds.lang = proto(Bracket, function(replacedFragment) {
Bracket.call(this,'⟨','⟩','\\langle ','\\rangle ',replacedFragment);
});
// Closing bracket matching opening bracket above
function CloseBracket(open, close, cmd, end, replacedFragment) {
Bracket.apply(this, arguments);
}
_ = CloseBracket.prototype = new Bracket;
_.placeCursor = function(cursor) {
//if I'm at the end of my parent who is a matching open-paren, and I was not passed
// a selection fragment, get rid of me and put cursor after my parent
if (!this.next && this.parent.parent && this.parent.parent.end === this.end && this.firstChild.isEmpty())
cursor.backspace().insertAfter(this.parent.parent);
else
this.firstChild.blur();
};
LatexCmds.rbrace = CharCmds['}'] = proto(CloseBracket, function(replacedFragment) {
CloseBracket.call(this, '{','}','\\{','\\}',replacedFragment);
});
LatexCmds.rangle = LatexCmds.rang = proto(CloseBracket, function(replacedFragment) {
CloseBracket.call(this,'⟨','⟩','\\langle ','\\rangle ',replacedFragment);
});
function Paren(open, close, replacedFragment) {
Bracket.call(this, open, close, open, close, replacedFragment);
}
Paren.prototype = Bracket.prototype;
LatexCmds.lparen = CharCmds['('] = proto(Paren, function(replacedFragment) {
Paren.call(this, '(', ')', replacedFragment);
});
LatexCmds.lbrack = LatexCmds.lbracket = CharCmds['['] = proto(Paren, function(replacedFragment) {
Paren.call(this, '[', ']', replacedFragment);
});
function CloseParen(open, close, replacedFragment) {
CloseBracket.call(this, open, close, open, close, replacedFragment);
}
CloseParen.prototype = CloseBracket.prototype;
LatexCmds.rparen = CharCmds[')'] = proto(CloseParen, function(replacedFragment) {
CloseParen.call(this, '(', ')', replacedFragment);
});
LatexCmds.rbrack = LatexCmds.rbracket = CharCmds[']'] = proto(CloseParen, function(replacedFragment) {
CloseParen.call(this, '[', ']', replacedFragment);
});
function Pipes(replacedFragment) {
Paren.call(this, '|', '|', replacedFragment);
}
_ = Pipes.prototype = new Paren;
_.placeCursor = function(cursor) {
if (!this.next && this.parent.parent && this.parent.parent.end === this.end && this.firstChild.isEmpty())
cursor.backspace().insertAfter(this.parent.parent);
else
cursor.appendTo(this.firstChild);
};
LatexCmds.lpipe = LatexCmds.rpipe = CharCmds['|'] = Pipes;
function TextBlock(replacedText) {
if (replacedText instanceof MathFragment)
this.replacedText = replacedText.remove().jQ.text();
else if (typeof replacedText === 'string')
this.replacedText = replacedText;
MathCommand.call(this, '\\text');
}
_ = TextBlock.prototype = new MathCommand;
_.html_template = ['<span class="text"></span>'];
_.text_template = ['"', '"'];
_.initBlocks = function() {
this.firstChild =
this.lastChild =
this.jQ.data(jQueryDataKey).block = new InnerTextBlock;
this.firstChild.parent = this;
this.firstChild.jQ = this.jQ.append(this.firstChild.jQ);
};
_.placeCursor = function(cursor) {
(this.cursor = cursor).appendTo(this.firstChild);
if (this.replacedText)
for (var i = 0; i < this.replacedText.length; i += 1)
this.write(this.replacedText.charAt(i));
};
_.write = function(ch) {
this.cursor.insertNew(new VanillaSymbol(ch));
};
_.keydown = function(e) {
//backspace and delete and ends of block don't unwrap
if (!this.cursor.selection &&
(
(e.which === 8 && !this.cursor.prev) ||
(e.which === 46 && !this.cursor.next)
)
) {
if (this.isEmpty())
this.cursor.insertAfter(this);
return false;
}
return this.parent.keydown(e);
};
_.textInput = function(ch) {
this.cursor.deleteSelection();
if (ch !== '$')
this.write(ch);
else if (this.isEmpty())
this.cursor.insertAfter(this).backspace().insertNew(new VanillaSymbol('\\$','$'));
else if (!this.cursor.next)
this.cursor.insertAfter(this);
else if (!this.cursor.prev)
this.cursor.insertBefore(this);
else { //split apart
var next = new TextBlock(new MathFragment(this.firstChild, this.cursor.prev));
next.placeCursor = function(cursor) // ********** REMOVEME HACK **********
{
this.prev = 0;
delete this.placeCursor;
this.placeCursor(cursor);
};
next.firstChild.focus = function(){ return this; };
this.cursor.insertAfter(this).insertNew(next);
next.prev = this;
this.cursor.insertBefore(next);
delete next.firstChild.focus;
}
};
function InnerTextBlock(){}
_ = InnerTextBlock.prototype = new MathBlock;
_.blur = function() {
this.jQ.removeClass('hasCursor');
if (this.isEmpty()) {
var textblock = this.parent, cursor = textblock.cursor;