Notes/src/mathcommand.js

41 lines
1.4 KiB
JavaScript
Raw Normal View History

2019-08-31 20:48:37 +03:00
import Command from '@ckeditor/ckeditor5-core/src/command';
import { getSelectedMathModelWidget } from './utils';
export default class MathCommand extends Command {
2019-09-17 19:39:49 +03:00
execute( equation, display, outputType, forceOutputType ) {
2019-08-31 20:48:37 +03:00
const model = this.editor.model;
const selection = model.document.selection;
const selectedElement = selection.getSelectedElement();
model.change( writer => {
let mathtex;
if ( selectedElement && selectedElement.is( 'mathtex' ) ) {
// Update selected element
2019-09-17 19:39:49 +03:00
const typeAttr = selectedElement.getAttribute( 'type' );
// Use already set type if found and is not forced
const type = forceOutputType ? outputType : typeAttr || outputType;
mathtex = writer.createElement( 'mathtex', { equation, type, display } );
2019-08-31 20:48:37 +03:00
} else {
// Create new model element
2019-09-17 19:39:49 +03:00
mathtex = writer.createElement( 'mathtex', { equation, type: outputType, display } );
2019-08-31 20:48:37 +03:00
}
model.insertContent( mathtex );
writer.setSelection( mathtex, 'on' );
} );
}
refresh() {
const model = this.editor.model;
const selection = model.document.selection;
const isAllowed = model.schema.checkChild( selection.focus.parent, 'mathtex' );
this.isEnabled = isAllowed;
const selectedEquation = getSelectedMathModelWidget( selection );
this.value = selectedEquation ? selectedEquation.getAttribute( 'equation' ) : null;
2019-09-17 15:47:58 +03:00
this.display = selectedEquation ? selectedEquation.getAttribute( 'display' ) : null;
2019-08-31 20:48:37 +03:00
}
}