2019-09-28 18:03:05 +03:00
|
|
|
export const defaultConfig = {
|
|
|
|
engine: 'mathjax',
|
|
|
|
outputType: 'script',
|
|
|
|
forceOutputType: false
|
|
|
|
};
|
|
|
|
|
2019-10-01 23:11:51 +03:00
|
|
|
export function getSelectedMathModelWidget( selection ) {
|
|
|
|
const selectedElement = selection.getSelectedElement();
|
|
|
|
|
|
|
|
if ( selectedElement && selectedElement.is( 'mathtex' ) ) {
|
|
|
|
return selectedElement;
|
2019-09-28 18:03:05 +03:00
|
|
|
}
|
2019-10-01 23:11:51 +03:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function renderEquation( equation, element, engine = 'katex', display = false ) {
|
2019-09-17 20:07:01 +03:00
|
|
|
if ( engine === 'mathjax' && typeof MathJax !== 'undefined' ) {
|
2019-10-01 23:11:51 +03:00
|
|
|
if ( isMathJaxVersion3( MathJax.version ) ) {
|
|
|
|
const options = MathJax.getMetricsFor( element, display );
|
|
|
|
let promiseFunction = undefined;
|
|
|
|
if ( typeof MathJax.tex2chtmlPromise !== 'undefined' ) {
|
|
|
|
promiseFunction = MathJax.tex2chtmlPromise;
|
|
|
|
} else if ( typeof MathJax.tex2svgPromise !== 'undefined' ) {
|
|
|
|
promiseFunction = MathJax.tex2svgPromise;
|
|
|
|
}
|
2019-09-28 13:01:08 +03:00
|
|
|
|
2019-10-01 23:11:51 +03:00
|
|
|
if ( typeof promiseFunction !== 'undefined' ) {
|
|
|
|
promiseFunction( equation, options ).then( node => {
|
|
|
|
if ( element.firstChild ) {
|
|
|
|
element.firstChild.replaceWith( node );
|
|
|
|
} else {
|
|
|
|
element.appendChild( node );
|
|
|
|
}
|
|
|
|
MathJax.startup.document.clear();
|
|
|
|
MathJax.startup.document.updateDocument();
|
|
|
|
} );
|
|
|
|
}
|
2019-08-31 20:48:37 +03:00
|
|
|
} else {
|
2019-10-01 23:11:51 +03:00
|
|
|
// Fixme: MathJax typesetting cause occasionally math processing error without asynchronous call
|
|
|
|
// eslint-disable-next-line
|
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-10-01 23:11:51 +03:00
|
|
|
MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub, element ] ); // eslint-disable-line
|
|
|
|
} );
|
2019-08-31 20:48:37 +03:00
|
|
|
}
|
2019-09-17 16:19:35 +03:00
|
|
|
} else if ( engine === 'katex' && typeof katex !== 'undefined' ) {
|
2019-10-01 23:11:51 +03:00
|
|
|
katex.render( equation, element, {
|
2019-08-31 20:48:37 +03:00
|
|
|
throwOnError: false,
|
|
|
|
displayMode: display
|
2019-10-01 23:11:51 +03:00
|
|
|
} );
|
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-10-01 23:11:51 +03:00
|
|
|
// eslint-disable-next-line
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-10-01 23:11:51 +03:00
|
|
|
// Check if equation has delimiters
|
|
|
|
export function hasDelimiters( text ) {
|
|
|
|
return text.match( /^(\\\[.*?\\\]|\\\(.*?\\\))$/ );
|
2019-08-31 20:48:37 +03:00
|
|
|
}
|
2019-09-17 16:31:39 +03:00
|
|
|
|
2019-10-01 23:11:51 +03:00
|
|
|
// Extract delimiters and figure display mode for the model
|
|
|
|
export function extractDelimiters( equation ) {
|
2019-09-28 18:03:05 +03:00
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|