Implement plugin

This commit is contained in:
Sauli Anto
2019-08-31 20:48:37 +03:00
commit 13a10dcfdd
16 changed files with 3813 additions and 0 deletions

36
src/mathcommand.js Normal file
View File

@@ -0,0 +1,36 @@
import Command from '@ckeditor/ckeditor5-core/src/command';
import { getSelectedMathModelWidget } from './utils';
export default class MathCommand extends Command {
execute( equation ) {
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
const mode = selectedElement.getAttribute( 'mode' );
const display = selectedElement.getAttribute( 'display' );
mathtex = writer.createElement( 'mathtex', { equation, mode, display } );
} else {
// Create new model element
mathtex = writer.createElement( 'mathtex', { equation, mode: 'script', display: true } );
}
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;
}
}