mirror of
https://github.com/zadam/trilium.git
synced 2025-11-06 13:26:01 +01:00
Implement plugin
This commit is contained in:
36
src/mathcommand.js
Normal file
36
src/mathcommand.js
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user