2019-09-28 18:09:39 +03:00
|
|
|
export const EQUATION_REGEXP = /^(\\\[.*?\\\]|\\\(.*?\\\))$/;
|
2019-09-28 18:03:05 +03:00
|
|
|
|
|
|
|
export const defaultConfig = {
|
|
|
|
engine: 'mathjax',
|
|
|
|
outputType: 'script',
|
|
|
|
forceOutputType: false
|
|
|
|
};
|
|
|
|
|
2019-08-31 20:48:37 +03:00
|
|
|
export function renderEquation( equation, element, engine = 'katex', display = false ) {
|
2019-09-28 18:03:05 +03:00
|
|
|
if ( !element ) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-28 13:01:08 +03:00
|
|
|
/* eslint-disable */
|
2019-09-17 20:07:01 +03:00
|
|
|
if ( engine === 'mathjax' && typeof MathJax !== 'undefined' ) {
|
2019-09-28 13:01:08 +03:00
|
|
|
const version = MathJax.version;
|
|
|
|
// If major version is 3
|
|
|
|
if ( isMathJaxVersion3( version ) ) {
|
|
|
|
const options = MathJax.getMetricsFor( element );
|
|
|
|
|
|
|
|
MathJax.texReset();
|
|
|
|
MathJax.tex2chtmlPromise( equation, options ).then( node => {
|
|
|
|
if ( element.firstChild ) {
|
|
|
|
element.firstChild.replaceWith( node );
|
2019-10-01 17:46:51 +03:00
|
|
|
} else {
|
2019-09-28 13:01:08 +03:00
|
|
|
element.appendChild( node );
|
|
|
|
}
|
|
|
|
MathJax.startup.document.clear();
|
|
|
|
MathJax.startup.document.updateDocument();
|
|
|
|
} );
|
2019-08-31 20:48:37 +03:00
|
|
|
} else {
|
2019-09-28 18:30:56 +03:00
|
|
|
// Fixme: MathJax typesetting cause occasionally math processing error without timeout
|
2019-09-28 18:03:05 +03:00
|
|
|
setTimeout( () => {
|
2019-09-28 18:30:56 +03:00
|
|
|
if ( display ) {
|
|
|
|
element.innerHTML = '\\[' + equation + '\\]';
|
|
|
|
} else {
|
|
|
|
element.innerHTML = '\\(' + equation + '\\)';
|
|
|
|
}
|
2019-09-28 18:03:05 +03:00
|
|
|
MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, element ] );
|
2019-09-28 18:30:56 +03:00
|
|
|
}, 50);
|
2019-08-31 20:48:37 +03:00
|
|
|
}
|
2019-09-17 16:19:35 +03:00
|
|
|
} else if ( engine === 'katex' && typeof katex !== 'undefined' ) {
|
2019-08-31 20:48:37 +03:00
|
|
|
katex.render( equation, element, {
|
|
|
|
throwOnError: false,
|
|
|
|
displayMode: display
|
|
|
|
} );
|
2019-09-17 16:19:35 +03:00
|
|
|
} else if ( typeof engine === 'function' ) {
|
2019-09-28 13:01:08 +03:00
|
|
|
engine( equation, element, display );
|
2019-08-31 20:48:37 +03:00
|
|
|
} else {
|
|
|
|
element.innerHTML = equation;
|
2019-09-17 16:19:35 +03:00
|
|
|
console.warn( `math-tex-typesetting-missing: Missing the mathematical typesetting engine (${engine}) for tex.` );
|
2019-08-31 20:48:37 +03:00
|
|
|
}
|
2019-09-28 13:01:08 +03:00
|
|
|
/* eslint-enable */
|
|
|
|
}
|
|
|
|
|
2019-09-28 18:03:05 +03:00
|
|
|
// Simple MathJax 3 version check
|
2019-09-28 13:01:08 +03:00
|
|
|
export function isMathJaxVersion3( version ) {
|
2019-09-28 13:02:32 +03:00
|
|
|
return version && typeof version === 'string' && version.split( '.' ).length === 3 && version.split( '.' )[ 0 ] === '3';
|
2019-08-31 20:48:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getSelectedMathModelWidget( selection ) {
|
|
|
|
const selectedElement = selection.getSelectedElement();
|
|
|
|
|
|
|
|
if ( selectedElement && selectedElement.is( 'mathtex' ) ) {
|
|
|
|
return selectedElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2019-09-17 16:31:39 +03:00
|
|
|
|
2019-09-28 18:03:05 +03:00
|
|
|
// Remove delimiters and figure display mode for the model
|
|
|
|
export function removeDelimiters( equation ) {
|
|
|
|
equation = equation.trim();
|
|
|
|
|
|
|
|
// Remove delimiters (e.g. \( \) or \[ \])
|
|
|
|
const hasInlineDelimiters = equation.includes( '\\(' ) && equation.includes( '\\)' );
|
|
|
|
const hasDisplayDelimiters = equation.includes( '\\[' ) && equation.includes( '\\]' );
|
|
|
|
if ( hasInlineDelimiters || hasDisplayDelimiters ) {
|
|
|
|
equation = equation.substring( 2, equation.length - 2 ).trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
equation,
|
|
|
|
display: hasDisplayDelimiters
|
|
|
|
};
|
|
|
|
}
|