Permalink
Browse files

Add additional publicapi handlers for

Add hacked support for typing sin/cos/etc without the backslash
Add fake intervalopenleft/right commands for (2,3] style interval notation
Change nthroot output to \nthroot to fix cut-and-paste
Add support for typing <= for less than or equals
  • Loading branch information...
unknown unknown
unknown authored and unknown committed Feb 27, 2012
1 parent 1d4b700 commit cb40b41dffde2deb5db61649dda8930347fb1e18
Showing with 114 additions and 4 deletions.
  1. +10 −1 src/commands.js
  2. +67 −1 src/publicapi.js
  3. +37 −2 src/symbols.js
View
@@ -255,7 +255,9 @@ _.html_template = [
];
_.text_template = ['sqrt[', '](', ')'];
_.latex = function() {
- return '\\sqrt['+this.firstChild.latex()+']{'+this.lastChild.latex()+'}';
+ //return '\\sqrt['+this.firstChild.latex()+']{'+this.lastChild.latex()+'}';
+ //This fixes cut-and-paste
+ return '\\nthroot{'+this.firstChild.latex()+'}{'+this.lastChild.latex()+'}';
};
LatexCmds.nthroot = NthRoot;
@@ -288,6 +290,13 @@ _.redraw = function() {
scale(this.bracketjQs, min(1 + .2*(height - 1), 1.2), 1.05*height);
};
+LatexCmds.intervalopenleft = proto(Bracket, function(replacedFragment) {
+ Bracket.call(this, '(', ']', '(', ']', replacedFragment);
+});
+LatexCmds.intervalopenright = proto(Bracket, function(replacedFragment) {
+ Bracket.call(this, '[', ')', '[', ')', replacedFragment);
+});
+
LatexCmds.lbrace = CharCmds['{'] = proto(Bracket, function(replacedFragment) {
Bracket.call(this, '{', '}', '\\{', '\\}', replacedFragment);
});
View
@@ -47,6 +47,38 @@ $.fn.mathquill = function(cmd, latex) {
if (cursor)
cursor.writeLatex(latex).parent.blur();
});
+ case 'writefunc':
+ if (arguments.length > 1)
+ return this.each(function() {
+ var data = $(this).data(jQueryDataKey),
+ block = data && data.block,
+ cursor = block && block.cursor;
+
+ if (cursor) {
+ if (cursor.selection) {
+ cursor.writeLatex(latex+'\\left('+cursor.selection.latex()+'\\right)').parent.blur();
+ } else {
+ cursor.writeLatex(latex+'\\left(\\right)').parent.blur();
+ }
+ cursor.moveLeft();
+ }
+ });
+ case 'writesimpfunc':
+ if (arguments.length > 1)
+ return this.each(function() {
+ var data = $(this).data(jQueryDataKey),
+ block = data && data.block,
+ cursor = block && block.cursor;
+
+ if (cursor) {
+ if (cursor.selection) {
+ cursor.writeLatex(latex+'{'+cursor.selection.latex()+'}').parent.blur();
+ } else {
+ cursor.writeLatex(latex).parent.blur();
+ }
+ cursor.moveLeft();
+ }
+ });
case 'cmd':
if (arguments.length > 1)
return this.each(function() {
@@ -70,7 +102,41 @@ $.fn.mathquill = function(cmd, latex) {
cursor.hide().parent.blur();
}
});
- default:
+ case 'writeint':
+ if (arguments.length > 1)
+ return this.each(function() {
+ var data = $(this).data(jQueryDataKey),
+ block = data && data.block,
+ cursor = block && block.cursor;
+
+ if (cursor) {
+ var hascomma = false;
+ if (cursor.selection && cursor.selection.latex().match(/,/)) {
+ hascomma = true;
+ }
+ cursor.show();
+ if (/^\\[a-z]+$/i.test(latex)) {
+ if (cursor.selection) {
+ //gotta do cursor before cursor.selection is mutated by 'new cmd(cursor.selection)'
+ cursor.prev = cursor.selection.prev;
+ cursor.next = cursor.selection.next;
+ }
+
+
+ cursor.insertCmd(latex.slice(1), cursor.selection);
+ delete cursor.selection;
+ }
+ else {
+ cursor.insertCh(latex);
+ }
+ if (!hascomma) {
+ cursor.insertCh(',');
+ cursor.moveLeft();
+ }
+ cursor.hide().parent.blur();
+ }
+ });
+ default:
var textbox = cmd === 'textbox',
editable = textbox || cmd === 'editable',
RootBlock = textbox ? RootTextBlock : RootMathBlock;
View
@@ -18,7 +18,32 @@ _.text = function() {
text += '*';
return text;
};
-
+_.insertAt = function(c) {
+ var lookback = this.cmd;
+ d = c.prev;
+ while (d && d instanceof Variable) {
+ lookback = d.cmd + lookback;
+ d = d.prev;
+ }
+ if (lookback.match(/(sqrt|root|sin|cos|tan|sec|csc|cot|log|ln)/)) {
+ for (var i=0;i<lookback.length-1;i++) {
+ c.selectLeft();
+ }
+ if (lookback=='root') {
+ lookback = 'nthroot{3}';
+ }
+ c.writeLatex('\\'+lookback);
+ if (lookback.match(/(sqrt|root)/)) {
+ c.moveLeft();
+ }
+ } else if (lookback.match(/^(pi|oo)$/)) {
+ if (lookback=='oo') { lookback = 'infty';}
+ c.selectLeft();
+ c.writeLatex('\\'+lookback);
+ } else {
+ MathCommand.prototype.insertAt.apply(this,arguments);
+ }
+}
function VanillaSymbol(ch, html) {
Symbol.call(this, ch, '<span>'+(html || ch)+'</span>');
}
@@ -139,7 +164,17 @@ LatexCmds.forall = proto(Symbol, function(replacedFragment, latex) {
function BinaryOperator(cmd, html, text) {
Symbol.call(this, cmd, '<span class="binary-operator">'+html+'</span>', text);
}
-BinaryOperator.prototype = new Symbol; //so instanceof will work
+_ = BinaryOperator.prototype = new Symbol; //so instanceof will work
+_.insertAt = function(af) {
+ var ae = af.prev.cmd + this.cmd;
+ if(ae==="<="){
+ af.backspace().insertNew(new BinaryOperator("\\le ","&le;"));
+ } else if(ae===">="){
+ af.backspace().insertNew(new BinaryOperator("\\ge ","&ge;"));
+ } else {
+ MathCommand.prototype.insertAt.apply(this,arguments);
+ }
+}
function PlusMinus(cmd, html) {
VanillaSymbol.apply(this, arguments);

0 comments on commit cb40b41

Please sign in to comment.