Notes/src/mathcommand.js

43 lines
1.6 KiB
JavaScript
Raw Normal View History

2019-08-31 20:48:37 +03:00
import Command from '@ckeditor/ckeditor5-core/src/command';
2020-08-29 17:33:41 +03:00
2019-08-31 20:48:37 +03:00
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;
2020-08-29 17:33:41 +03:00
if ( selectedElement && ( selectedElement.is( 'element', 'mathtex-inline' ) ||
selectedElement.is( 'element', 'mathtex-display' ) ) ) {
2019-08-31 20:48:37 +03:00
// 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;
2019-09-28 13:01:08 +03:00
2020-08-29 17:33:41 +03:00
mathtex = writer.createElement( display ? 'mathtex-display' : 'mathtex-inline', { equation, type, display } );
2019-08-31 20:48:37 +03:00
} else {
// Create new model element
2020-08-29 17:33:41 +03:00
mathtex = writer.createElement( display ? 'mathtex-display' : 'mathtex-inline', { equation, type: outputType, display } );
2019-08-31 20:48:37 +03:00
}
model.insertContent( mathtex );
} );
}
refresh() {
const model = this.editor.model;
const selection = model.document.selection;
2020-08-29 17:33:41 +03:00
const selectedElement = selection.getSelectedElement();
2019-08-31 20:48:37 +03:00
2020-08-29 17:33:41 +03:00
this.isEnabled = selectedElement === null || ( selectedElement.is( 'element', 'mathtex-inline' ) ||
selectedElement.is( 'element', 'mathtex-display' ) );
2019-08-31 20:48:37 +03:00
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
}
}