Files
Trilium/src/ui/mathview.js

74 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-08-31 20:48:37 +03:00
import View from '@ckeditor/ckeditor5-ui/src/view';
2019-10-01 23:11:51 +03:00
2019-08-31 20:48:37 +03:00
import { renderEquation } from '../utils';
export default class MathView extends View {
constructor( engine, locale ) {
super( locale );
this.engine = engine;
2019-09-28 13:01:08 +03:00
2019-08-31 20:48:37 +03:00
this.set( 'value', '' );
2019-09-28 13:01:08 +03:00
this.set( 'display', false );
2019-08-31 20:48:37 +03:00
2019-09-17 15:47:58 +03:00
this.on( 'change', () => {
2019-08-31 20:48:37 +03:00
this.updateMath();
} );
this.setTemplate( {
2019-10-01 23:11:51 +03:00
tag: 'iframe',
2019-08-31 20:48:37 +03:00
attributes: {
class: [
'ck',
'ck-math-preview'
],
}
} );
}
updateMath() {
2019-10-01 23:11:51 +03:00
const el = this.element;
if ( el ) {
// Fixme
// eslint-disable-next-line
setTimeout( () => {
let docEl = ( el.contentWindow || el.contentDocument );
if ( docEl.document ) {
docEl = docEl.document;
}
const headEl = docEl.head;
// Remove old styles
while ( headEl.hasChildNodes() ) {
headEl.removeChild( headEl.firstChild );
}
// Add all MathJax styles
const styles = document.head.getElementsByTagName( 'style' ); // eslint-disable-line
for ( const style of styles ) {
const id = style.getAttribute( 'id' );
if ( id && id.startsWith( 'MJX' ) ) {
headEl.appendChild( style.cloneNode( true ) );
}
}
const links = document.head.getElementsByTagName( 'link' ); // eslint-disable-line
for ( const link of links ) {
headEl.appendChild( link.cloneNode( true ) );
}
const bodyEl = docEl.body;
bodyEl.setAttribute( 'style', 'margin-left: 0; margin-right: 0; user-select: none;' );
renderEquation( this.value, bodyEl, this.engine, this.display );
}, 100 );
}
2019-08-31 20:48:37 +03:00
}
render() {
super.render();
this.updateMath();
}
}