Skip to content

Commit

Permalink
Separate ignored commands add custom commands
Browse files Browse the repository at this point in the history
* Add mixed fraction type
* Added definite integral command
  • Loading branch information
kvnvelasco committed Jan 29, 2019
1 parent 4fed466 commit 39c1b5e
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 44 deletions.
74 changes: 52 additions & 22 deletions build/mathquill.js
Original file line number Diff line number Diff line change
Expand Up @@ -1565,19 +1565,14 @@ var saneKeyboardEvents = (function() {
};
}());
Controller.open(function(_) {
var COMMAND_CONFIGURATION_TYPE_MAP = {
symbol: 'vanillaSymbol',
variable: 'variable',
nonSymbola: 'nonSymbolaSymbol',
binary: 'binary'
};

_.initializeLatexGrammar = function () {
this.cursor.options.autoCommands = {};
this.cursor.grammarDicts = {
latexCmds: Object.assign({}, LatexCmds),
charCmds: Object.assign({}, CharCmds),
textCommands: {}
textCommands: {},
ignoredCharacters: {}
};
// Initialize the grammar processors for various symbols
// by loading up the default configuration
Expand All @@ -1592,6 +1587,9 @@ Controller.open(function(_) {
// Process injected commands into autocommands
var options = this.cursor.options;
var commands = options.commands || [];
var ignoredCharacters = options.ignoredCharacters || [];
// We're going to create an index for ignored character lookup
ignoredCharacters.forEach(char => this.cursor.grammarDicts.ignoredCharacters[char] = true);
this.extendLatexGrammar(commands);
};

Expand Down Expand Up @@ -1631,8 +1629,7 @@ Controller.open(function(_) {
}


var grammarType = type || symbolDefinition.type || 'symbol';
var processor = grammarProcessors[COMMAND_CONFIGURATION_TYPE_MAP[grammarType]];
var processor = grammarProcessors.vanillaSymbol;
Object.assign(
latexCmds,
processor(symbolDefinition)
Expand Down Expand Up @@ -2676,18 +2673,9 @@ var GLOBALLY_DISABLED_INPUT = [
function symbolFactory(binder) {
return function loadDynamicSymbol(symbolDefinition) {
var symbols = {};
// In cases when the skip flag is set to true
// we will define a command that has no output { latex: '', htmlEntity: '' }
// effectively ignoring the input
if(symbolDefinition.skip)
symbolDefinition = {
name: symbolDefinition.name,
latex: '',
htmlEntity: '',
match: symbolDefinition.match
};

var boundSymbol = binder(symbolDefinition);
var boundSymbol;
if (symbolDefinition.useInternalSymbolDef) boundSymbol = LatexCmds[symbolDefinition.name];
else boundSymbol = binder(symbolDefinition);

if (symbolDefinition.match)
symbolDefinition.match.forEach(function (match) {
Expand All @@ -2704,6 +2692,9 @@ function symbolFactory(binder) {
var latexWithoutBs = symbolDefinition.latex.replace('\\', '');
symbols[latexWithoutBs] = boundSymbol;
}
if(symbolDefinition.htmlEntity)
// all html entities should match the command by default
symbols[symbolDefinition.htmlEntity] = boundSymbol;
return symbols;
}
}
Expand Down Expand Up @@ -3195,8 +3186,10 @@ var MathBlock = P(MathElement, function(_, super_) {
ch = ch || '';

var cons;
if (cursor.grammarDicts.ignoredCharacters[ch])
return null;
// exclude f because it gets a dedicated command with more spacing
if (ch.match(/^[a-eg-zA-Z]$/))
else if (ch.match(/^[a-eg-zA-Z]$/))
return Letter(ch);
else if (/^\d$/.test(ch))
return Digit(ch);
Expand All @@ -3207,6 +3200,7 @@ var MathBlock = P(MathElement, function(_, super_) {
};
_.write = function(cursor, ch) {
var cmd = this.chToCmd(cursor, ch);
if(!cmd) return;
if (cursor.selection) cmd.replaces(cursor.replaceSelection());
cmd.createLeftOf(cursor.show());
};
Expand Down Expand Up @@ -5379,6 +5373,42 @@ CharCmds['*'] = LatexCmds.times;
// Patched latex for % symbol, it should not contain \\ in the beginning.
LatexCmds['%'] = bind(NonSymbolaSymbol, '%', '%');

var MixedFraction =
LatexCmds.mixed = P(MathCommand, function(_, super_) {
_.ctrlSeq = '\\frac';
_.htmlTemplate =
'<span>'
+ '<span>&0</span>'
+ '<span class="mq-fraction mq-non-leaf">'
+ '<span class="mq-numerator">&1</span>'
+ '<span class="mq-denominator">&2</span>'
+ '<span style="display:inline-block;width:0">&#8203;</span>'
+ '</span>'
+ '</span>';
_.text_template = ['frac[', ']', '(', ')', '(', ')'];
_.latex = function() {
var blocks = this.blocks;
return blocks[0].latex() + '\\frac{' + blocks[1].latex() + '}' + '{' + blocks[2].latex() + '}'
};
})

var Defint =
LatexCmds.defint = P(MathCommand, function(_, super_) {
_.ctrlSeq = '\\int';
_.htmlTemplate =
'<span>'
+ '<big>&int;</big>'
+ '<span class="mq-supsub mq-non-leaf mq-limit">'
+ '<span class="mq-sup"><span>&0</span></span>'
+ '<span class="mq-sub"><span>&1</span></span>'
+ '<span style="display:inline-block;width:0">&#8203;</span>'
+ '</span>'
+ '</span>';
_.text_template = ['int[', ']', '(', ')'];
_.latex = function() {
return '\\int_{' + this.ends[L].latex() + '}^{' + this.ends[R].latex() + '}'
};
})
var nCr = LatexCmds.nCr = P(MathCommand, function(_, super_) {
_.ctrlSeq = '\\nCr';
_.htmlTemplate =
Expand Down
5 changes: 4 additions & 1 deletion src/commands/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,10 @@ var MathBlock = P(MathElement, function(_, super_) {
ch = ch || '';

var cons;
if (cursor.grammarDicts.ignoredCharacters[ch])
return null;
// exclude f because it gets a dedicated command with more spacing
if (ch.match(/^[a-eg-zA-Z]$/))
else if (ch.match(/^[a-eg-zA-Z]$/))
return Letter(ch);
else if (/^\d$/.test(ch))
return Digit(ch);
Expand All @@ -490,6 +492,7 @@ var MathBlock = P(MathElement, function(_, super_) {
};
_.write = function(cursor, ch) {
var cmd = this.chToCmd(cursor, ch);
if(!cmd) return;
if (cursor.selection) cmd.replaces(cursor.replaceSelection());
cmd.createLeftOf(cursor.show());
};
Expand Down
36 changes: 36 additions & 0 deletions src/commands/mathspace/mathspaceCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,42 @@ CharCmds['*'] = LatexCmds.times;
// Patched latex for % symbol, it should not contain \\ in the beginning.
LatexCmds['%'] = bind(NonSymbolaSymbol, '%', '%');

var MixedFraction =
LatexCmds.mixed = P(MathCommand, function(_, super_) {
_.ctrlSeq = '\\frac';
_.htmlTemplate =
'<span>'
+ '<span>&0</span>'
+ '<span class="mq-fraction mq-non-leaf">'
+ '<span class="mq-numerator">&1</span>'
+ '<span class="mq-denominator">&2</span>'
+ '<span style="display:inline-block;width:0">&#8203;</span>'
+ '</span>'
+ '</span>';
_.text_template = ['frac[', ']', '(', ')', '(', ')'];
_.latex = function() {
var blocks = this.blocks;
return blocks[0].latex() + '\\frac{' + blocks[1].latex() + '}' + '{' + blocks[2].latex() + '}'
};
})

var Defint =
LatexCmds.defint = P(MathCommand, function(_, super_) {
_.ctrlSeq = '\\int';
_.htmlTemplate =
'<span>'
+ '<big>&int;</big>'
+ '<span class="mq-supsub mq-non-leaf mq-limit">'
+ '<span class="mq-sup"><span>&0</span></span>'
+ '<span class="mq-sub"><span>&1</span></span>'
+ '<span style="display:inline-block;width:0">&#8203;</span>'
+ '</span>'
+ '</span>';
_.text_template = ['int[', ']', '(', ')'];
_.latex = function() {
return '\\int_{' + this.ends[L].latex() + '}^{' + this.ends[R].latex() + '}'
};
})
var nCr = LatexCmds.nCr = P(MathCommand, function(_, super_) {
_.ctrlSeq = '\\nCr';
_.htmlTemplate =
Expand Down
18 changes: 6 additions & 12 deletions src/configuration/symbols/symbols.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,9 @@
function symbolFactory(binder) {
return function loadDynamicSymbol(symbolDefinition) {
var symbols = {};
// In cases when the skip flag is set to true
// we will define a command that has no output { latex: '', htmlEntity: '' }
// effectively ignoring the input
if(symbolDefinition.skip)
symbolDefinition = {
name: symbolDefinition.name,
latex: '',
htmlEntity: '',
match: symbolDefinition.match
};

var boundSymbol = binder(symbolDefinition);
var boundSymbol;
if (symbolDefinition.useInternalSymbolDef) boundSymbol = LatexCmds[symbolDefinition.name];
else boundSymbol = binder(symbolDefinition);

if (symbolDefinition.match)
symbolDefinition.match.forEach(function (match) {
Expand All @@ -56,6 +47,9 @@ function symbolFactory(binder) {
var latexWithoutBs = symbolDefinition.latex.replace('\\', '');
symbols[latexWithoutBs] = boundSymbol;
}
if(symbolDefinition.htmlEntity)
// all html entities should match the command by default
symbols[symbolDefinition.htmlEntity] = boundSymbol;
return symbols;
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/services/commands.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
Controller.open(function(_) {
var COMMAND_CONFIGURATION_TYPE_MAP = {
symbol: 'vanillaSymbol',
variable: 'variable',
nonSymbola: 'nonSymbolaSymbol',
binary: 'binary'
};

_.initializeLatexGrammar = function () {
this.cursor.options.autoCommands = {};
this.cursor.grammarDicts = {
latexCmds: Object.assign({}, LatexCmds),
charCmds: Object.assign({}, CharCmds),
textCommands: {}
textCommands: {},
ignoredCharacters: {}
};
// Initialize the grammar processors for various symbols
// by loading up the default configuration
Expand All @@ -26,6 +21,9 @@ Controller.open(function(_) {
// Process injected commands into autocommands
var options = this.cursor.options;
var commands = options.commands || [];
var ignoredCharacters = options.ignoredCharacters || [];
// We're going to create an index for ignored character lookup
ignoredCharacters.forEach(char => this.cursor.grammarDicts.ignoredCharacters[char] = true);
this.extendLatexGrammar(commands);
};

Expand Down Expand Up @@ -65,8 +63,7 @@ Controller.open(function(_) {
}


var grammarType = type || symbolDefinition.type || 'symbol';
var processor = grammarProcessors[COMMAND_CONFIGURATION_TYPE_MAP[grammarType]];
var processor = grammarProcessors.vanillaSymbol;
Object.assign(
latexCmds,
processor(symbolDefinition)
Expand Down

0 comments on commit 39c1b5e

Please sign in to comment.