Skip to content

Commit

Permalink
Refine categories for smart shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
arnog committed Apr 8, 2019
1 parent d81fce2 commit 10616a5
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 139 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,20 @@ The `value` key is required an indicate the shortcut substitution.
The `mode` key, if present, indicate in which mode this shortcut should apply, either `'math'` or `'text'`. If the key is not present the shortcut apply in both modes.

The `'after'` key, if present, indicate in what context the shortcut should apply. One or more values can be specified, separated by a '+' sign. If any of the values match, the shortcut will be applicable. Possible values are:
- `'space'`
- `'space'` A spacing command, such as `\quad`
- `'nothing'` The begining of a group
- `'surd'` A square root or n-th root
- `'frac'` A fraction
- `'function'` A function such as `\sin` or `f`
- `'letter'` A letter, such as `x` or `n`
- `'digit'` `0` through `9`
- `'binop'` A binary operator, such as `+`
- `'relop'` A relational operator, such as `=`
- `'punct'` A punctuation mark, such as `,`
- `'array'` An array, such as a matrix or cases statement
- `'openfence'` An opening fence, such as `(`
- `'closefence'` A closing fence such as `}`
- `'text'` Some plain text



Expand Down
2 changes: 1 addition & 1 deletion dist/mathlive.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mathlive.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/src/addons/auto-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function createMathMLNode(latex, options) {
"</math>";
} catch (e) {
console.error( 'Could not convert\'' + latex + '\' to MathML with ', e );
span.innerText = latex;
span.textContent = latex;
}
span.className = 'sr-only';
return span;
Expand Down
5 changes: 3 additions & 2 deletions dist/src/core/mathAtom.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const makeOrd = Span.makeOrd;
const makeInner = Span.makeInner;
const makeHlist = Span.makeHlist;
const makeVlist = Span.makeVlist;
const GREEK_REGEX = /\u0393|\u0394|\u0398|\u039b|\u039E|\u03A0|\u03A3|\u03a5|\u03a6|\u03a8|\u03a9|[\u03b1-\u03c9]|\u03d1|\u03d5|\u03d6|\u03f1|\u03f5/;
export const GREEK_REGEX = /\u0393|\u0394|\u0398|\u039b|\u039E|\u03A0|\u03A3|\u03a5|\u03a6|\u03a8|\u03a9|[\u03b1-\u03c9]|\u03d1|\u03d5|\u03d6|\u03f1|\u03f5/;

// TeX by default auto-italicize latin letters and lowercase greek letters
const AUTO_ITALIC_REGEX = /^([A-Za-z]|[\u03b1-\u03c9]|\u03d1|\u03d5|\u03d6|\u03f1|\u03f5)$/;
Expand Down Expand Up @@ -1789,7 +1789,8 @@ function makeRoot(parseMode, body) {
export default {
MathAtom,
decompose,
makeRoot
makeRoot,
GREEK_REGEX
}


Expand Down
110 changes: 45 additions & 65 deletions dist/src/editor/editor-shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* @private
*/

import Definitions from '../core/definitions.js';

/**
* The index of this array is a keystroke combination as returned by the key
* field of a JavaScript keyboard event as documented here:
Expand Down Expand Up @@ -300,7 +302,7 @@ const INLINE_SHORTCUTS = {
'jj': '\\imaginaryJ',
'ee': {
mode: 'math',
after: 'digit+symbol+fence',
after: 'nothing+digit+function+frac+surd+binop+relop+punct+array+openfence+closefence+space',
value: '\\exponentialE',
},

Expand All @@ -313,7 +315,7 @@ const INLINE_SHORTCUTS = {
// '&#8734;': '\\infty',
'oo': {
mode: 'math',
after: 'digit+symbol+fence',
after: 'nothing+digit+frac+surd+binop+relop+punct+array+openfence+closefence+space',
value: '\\infty',
},

Expand Down Expand Up @@ -350,12 +352,12 @@ const INLINE_SHORTCUTS = {
// The shortcut for the greek letter "xi" is interfering with "x in"
'xin': {
mode: 'math',
after: 'nothing+space+letter+digit+symbol+fence',
after: 'nothing+text+relop+punct+openfence+space',
value: 'x \\in',
},
'in': {
mode: 'math',
after: 'space+letter+digit+symbol+fence',
after: 'nothing+letter+closefence',
value: '\\in',
},
'!in': '\\notin',
Expand Down Expand Up @@ -455,16 +457,16 @@ const INLINE_SHORTCUTS = {
'Im': '\\operatorname{Im}',

// UNITS
'mm': { after: 'number',
'mm': { after: 'digit',
value: '\\operatorname{mm}', // millimeter
},
'cm': { after: 'number',
'cm': { after: 'digit',
value: '\\operatorname{cm}', // centimeter
},
'km': { after: 'number',
'km': { after: 'digit',
value: '\\operatorname{km}', // kilometer
},
'kg': { after: 'number',
'kg': { after: 'digit',
value: '\\operatorname{kg}', // kilogram
},

Expand Down Expand Up @@ -629,77 +631,55 @@ function validateShortcut(siblings, shortcut) {
// If we have no context, we assume all the shortcuts are valid
if (!siblings) return shortcut ? shortcut.value : undefined;

// first
// 'nothing'
// placeholder
// 'nothing'
// surd
//
// supsub
// atom before?
// genfrac
//
// textord or mord
// f, g, h (isFunction = true) 'function'
// [a-zA-Z]+greek+cyrillic 'letter'
// [0-9] 'digit'
// mbin 'binop'
// mrel 'relop'
// mop
// sin (isFunction = true) 'function'
// minner
// mpunct
// array
//
// mopen
// 'openfence'
// leftright mclose
// 'closefence'
// group, accent, overlap. overunder
// type of last atom of body

// box -- group style

// mathstyle
// sizing
// color
// font


let nothing = false;
let letter = false;
let digit = false;
let space = false;
let number = false;
let symbol = false;
let fence = false;
let isFunction = false;
let frac = false;
let surd = false;
let binop = false;
let relop = false;
let punct = false;
let array = false;
let openfence = false;
let closefence = false;
let text = false;
let nothing = false;
let space = false;
const sibling = siblings[siblings.length - 1];
nothing = !sibling; // start of a group
nothing = !sibling || sibling.type === 'first'; // start of a group
if (sibling) {
text = sibling.mode === 'text';
letter = !text && sibling.type === 'mord' && /[a-zA-Z]+$/.test(sibling.body);
space = false; // /\s$/.test(context);
letter = !text && sibling.type === 'mord' && Definitions.LETTER.test(sibling.body);
digit = !text && sibling.type === 'mord' && /[0-9]+$/.test(sibling.body);
number = digit;
symbol = /mrel|mop|leftright|genfrac/.test(sibling.type);
fence = /mopen|mclose/.test(sibling.type);
isFunction = !text && sibling.isFunction;
frac = sibling.type === 'genfrac';
surd = sibling.type === 'surd';
binop = sibling.type === 'mbin';
relop = sibling.type === 'mrel';
punct = sibling.type === 'mpunct' || sibling.type === 'minner';
array = sibling.array;
openfence = sibling.type === 'mopen';
closefence = sibling.type === 'mclose' || sibling.type === 'leftright';
}

if (typeof shortcut === 'object') {
console.log('considering ' + shortcut.value + ' if after ' + shortcut.after);
// If this is a complex shortcut with conditions, consider them now
if ( (/text/.test(shortcut.after) && text) ||
// If this is a conditional shortcut, consider the conditions now
if ( (/nothing/.test(shortcut.after) && nothing) ||
(/letter/.test(shortcut.after) && letter) ||
(/space/.test(shortcut.after) && space) ||
(/digit/.test(shortcut.after) && digit) ||
(/number/.test(shortcut.after) && number) ||
(/symbol/.test(shortcut.after) && symbol) ||
(/nothing/.test(shortcut.after) && nothing) ||
(/fence/.test(shortcut.after) && fence)){
(/function/.test(shortcut.after) && isFunction) ||
(/frac/.test(shortcut.after) && frac) ||
(/surd/.test(shortcut.after) && surd) ||
(/binop/.test(shortcut.after) && binop) ||
(/relop/.test(shortcut.after) && relop) ||
(/punct/.test(shortcut.after) && punct) ||
(/array/.test(shortcut.after) && array) ||
(/openfence/.test(shortcut.after) && openfence) ||
(/closefence/.test(shortcut.after) && closefence) ||
(/text/.test(shortcut.after) && text) ||
(/space/.test(shortcut.after) && space)){
shortcut = shortcut.value;
} else {
console.log('rejected');
shortcut = null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/addons/auto-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function createMathMLNode(latex, options) {
"</math>";
} catch (e) {
console.error( 'Could not convert\'' + latex + '\' to MathML with ', e );
span.innerText = latex;
span.textContent = latex;
}
span.className = 'sr-only';
return span;
Expand Down
5 changes: 3 additions & 2 deletions src/core/mathAtom.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const makeOrd = Span.makeOrd;
const makeInner = Span.makeInner;
const makeHlist = Span.makeHlist;
const makeVlist = Span.makeVlist;
const GREEK_REGEX = /\u0393|\u0394|\u0398|\u039b|\u039E|\u03A0|\u03A3|\u03a5|\u03a6|\u03a8|\u03a9|[\u03b1-\u03c9]|\u03d1|\u03d5|\u03d6|\u03f1|\u03f5/;
export const GREEK_REGEX = /\u0393|\u0394|\u0398|\u039b|\u039E|\u03A0|\u03A3|\u03a5|\u03a6|\u03a8|\u03a9|[\u03b1-\u03c9]|\u03d1|\u03d5|\u03d6|\u03f1|\u03f5/;

// TeX by default auto-italicize latin letters and lowercase greek letters
const AUTO_ITALIC_REGEX = /^([A-Za-z]|[\u03b1-\u03c9]|\u03d1|\u03d5|\u03d6|\u03f1|\u03f5)$/;
Expand Down Expand Up @@ -1789,7 +1789,8 @@ function makeRoot(parseMode, body) {
export default {
MathAtom,
decompose,
makeRoot
makeRoot,
GREEK_REGEX
}


Expand Down
Loading

0 comments on commit 10616a5

Please sign in to comment.