Fix writer and seperate schema

This commit is contained in:
Sauli Anto
2020-08-29 17:33:41 +03:00
parent 0ef44427ce
commit 7d0cd0187f
10 changed files with 101 additions and 88 deletions

View File

@@ -1,4 +1,5 @@
import Command from '@ckeditor/ckeditor5-core/src/command';
import { getSelectedMathModelWidget } from './utils';
export default class MathCommand extends Command {
@@ -9,17 +10,18 @@ export default class MathCommand extends Command {
model.change( writer => {
let mathtex;
if ( selectedElement && selectedElement.is( 'mathtex' ) ) {
if ( selectedElement && ( selectedElement.is( 'element', 'mathtex-inline' ) ||
selectedElement.is( 'element', 'mathtex-display' ) ) ) {
// Update selected element
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 } );
mathtex = writer.createElement( display ? 'mathtex-display' : 'mathtex-inline', { equation, type, display } );
} else {
// Create new model element
mathtex = writer.createElement( 'mathtex', { equation, type: outputType, display } );
mathtex = writer.createElement( display ? 'mathtex-display' : 'mathtex-inline', { equation, type: outputType, display } );
}
model.insertContent( mathtex );
writer.setSelection( mathtex, 'on' );
@@ -29,9 +31,10 @@ export default class MathCommand extends Command {
refresh() {
const model = this.editor.model;
const selection = model.document.selection;
const selectedElement = selection.getSelectedElement();
const isAllowed = model.schema.checkChild( selection.focus.parent, 'mathtex' );
this.isEnabled = isAllowed;
this.isEnabled = selectedElement === null || ( selectedElement.is( 'element', 'mathtex-inline' ) ||
selectedElement.is( 'element', 'mathtex-display' ) );
const selectedEquation = getSelectedMathModelWidget( selection );
this.value = selectedEquation ? selectedEquation.getAttribute( 'equation' ) : null;