import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import { toWidget } from '@ckeditor/ckeditor5-widget/src/utils';
import Widget from '@ckeditor/ckeditor5-widget/src/widget';
import MathCommand from './mathcommand';
import { defaultConfig, renderEquation, removeDelimiters } from './utils';
export default class MathEditing extends Plugin {
static get requires() {
return [ Widget ];
}
static get pluginName() {
return 'MathEditing';
}
init() {
const editor = this.editor;
editor.commands.add( 'math', new MathCommand( editor ) );
this._defineSchema();
this._defineConverters();
}
_defineSchema() {
const schema = this.editor.model.schema;
schema.register( 'mathtex', {
allowWhere: '$text',
isInline: true,
isObject: true,
allowAttributes: [ 'equation', 'type', 'display' ]
} );
}
_defineConverters() {
const conversion = this.editor.conversion;
const mathConfig = {
...defaultConfig,
...this.editor.config.get( 'math' )
};
// View -> Model
conversion.for( 'upcast' )
// MathJax inline way (e.g. )
.elementToElement( {
view: {
name: 'script',
attributes: {
type: 'math/tex'
}
},
model: ( viewElement, modelWriter ) => {
const equation = viewElement.getChild( 0 ).data.trim();
return modelWriter.createElement( 'mathtex', {
equation,
type: mathConfig.forceOutputType ? mathConfig.outputType : 'script',
display: false
} );
}
} )
// MathJax display way (e.g. )
.elementToElement( {
view: {
name: 'script',
attributes: {
type: 'math/tex; mode=display'
}
},
model: ( viewElement, modelWriter ) => {
const equation = viewElement.getChild( 0 ).data.trim();
return modelWriter.createElement( 'mathtex', {
equation,
type: mathConfig.forceOutputType ? mathConfig.outputType : 'script',
display: true
} );
}
} )
// Todo: Implement input conversion
/*
// MathML (e.g.