-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathmathcommand.ts
More file actions
64 lines (56 loc) · 1.92 KB
/
Copy pathmathcommand.ts
File metadata and controls
64 lines (56 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Command } from 'ckeditor5/src/core';
import { getSelectedMathModelWidget } from './utils';
export default class MathCommand extends Command {
public override value: string | null = null;
public override execute(
equation: string,
display?: boolean,
outputType: 'script' | 'span' = 'script',
forceOutputType?: boolean
): void {
const model = this.editor.model;
const selection = model.document.selection;
const selectedElement = selection.getSelectedElement();
model.change( writer => {
let 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(
display ? 'mathtex-display' : 'mathtex-inline',
{ equation, type, display }
);
} else {
// Create new model element
mathtex = writer.createElement(
display ? 'mathtex-display' : 'mathtex-inline',
{ equation, type: outputType, display }
);
}
model.insertContent( mathtex );
} );
}
public display = false;
public override refresh(): void {
const model = this.editor.model;
const selection = model.document.selection;
const selectedElement = selection.getSelectedElement();
this.isEnabled =
selectedElement === null ||
selectedElement.is( 'element', 'mathtex-inline' ) ||
selectedElement.is( 'element', 'mathtex-display' );
const selectedEquation = getSelectedMathModelWidget( selection );
const value = selectedEquation?.getAttribute( 'equation' );
this.value = typeof value === 'string' ? value : null;
const display = selectedEquation?.getAttribute( 'display' );
this.display = typeof display === 'boolean' ? display : false;
}
}