Newer
Older
// eslint-disable-next-line max-classes-per-file
import {
NgModule, CUSTOM_ELEMENTS_SCHEMA,
Component, AfterViewInit, ViewChild, ElementRef, Input, OnChanges, SimpleChanges
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { MathfieldElement, VirtualKeyboardDefinition } from 'mathlive';
import { VirtualKeyboardLayer } from 'mathlive/dist/public/options';
import { MatButtonToggleChange, MatButtonToggleModule } from '@angular/material/button-toggle';
@Component({
selector: 'aspect-mathlive-math-field',
template: `
<mat-button-toggle-group *ngIf="enableModeSwitch"
[value]="mathFieldElement.mode"
(change)="setParseMode($event)">
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<mat-button-toggle value="math">Formel</mat-button-toggle>
<mat-button-toggle value="text">Text</mat-button-toggle>
</mat-button-toggle-group>
<div #mathfield>
</div>
`,
styles: [
'mat-button-toggle-group {height: 20px;}',
':host ::ng-deep .mat-button-toggle-label-content {line-height: unset}'
]
})
export class MathInputComponent implements AfterViewInit, OnChanges {
@Input() value!: string;
@Input() enableModeSwitch: boolean = false;
@ViewChild('mathfield') mathfieldRef!: ElementRef;
mathFieldElement: MathfieldElement = new MathfieldElement({
virtualKeyboardMode: 'onfocus',
customVirtualKeyboardLayers: MathInputComponent.setupKeyboadLayer(),
customVirtualKeyboards: MathInputComponent.setupKeyboard(),
virtualKeyboards: 'aspect-keyboard roman greek',
keypressSound: null,
plonkSound: null,
decimalSeparator: ',',
// defaultMode: 'math'
});
ngAfterViewInit(): void {
this.setupMathfield();
}
setupMathfield(): void {
this.mathfieldRef.nativeElement.appendChild(this.mathFieldElement);
this.mathFieldElement.value = this.value;
}
static setupKeyboard(): Record<string, VirtualKeyboardDefinition> {
return {
'aspect-keyboard': {
label: 'Formel', // Label displayed in the Virtual Keyboard Switcher
tooltip: 'Zahlen & Formeln', // Tooltip when hovering over the label
layer: 'aspect-keyboard-layer'
}
};
}
static setupKeyboadLayer(): Record<string, string | Partial<VirtualKeyboardLayer>> {
return {
'aspect-keyboard-layer': {
styles: '',
rows: [
[
{ label: '7', key: '7' },
{ label: '8', key: '8' },
{ label: '9', key: '9' },
{ class: 'separator w5' },
{ latex: '+' },
{ class: 'separator w5' },
{ latex: '<' },
{ latex: '>' },
{ latex: '\\neq' },
{ class: 'separator w5' },
{ latex: '€' },
{ class: 'separator w5' },
{ label: '<span><i>x</i> ²</span>', insert: '$$#@^{2}$$' },
{ latex: '$$#@^{#?}' },
{ latex: '$$#@_{#?}' }
],
[
{ label: '4', latex: '4' },
{ label: '5', key: '5' },
{ label: '6', key: '6' },
{ class: 'separator w5' },
{ latex: '-' },
{ class: 'separator w5' },
{ latex: '\\le' },
{ latex: '\\ge' },
{ latex: '\\approx' },
{ class: 'separator w5' },
{ latex: '\\%' },
{ class: 'separator w5' },
{ class: 'small', latex: '\\frac{#0}{#0}' },
{ latex: '\\sqrt{#0}', insert: '$$\\sqrt{#0}$$' },
{ class: 'separator' }
],
[
{ label: '1', key: '1' },
{ label: '2', key: '2' },
{ label: '3', key: '3' },
{ class: 'separator w5' },
{ latex: '\\times' },
{ class: 'separator w5' },
{ latex: '(' },
{ latex: ')' },
{ latex: '\\Rightarrow' },
{ class: 'separator w5' },
{ latex: '°' },
{ class: 'separator w5' },
{ latex: '\\overline' },
{ class: 'separator' },
{ class: 'separator' }
],
[
{ label: '0', key: '0' },
{ latex: ',' },
{ latex: '=' },
{ class: 'separator w5' },
{ latex: '\\div' },
{ class: 'separator w5' },
{ latex: '[' },
{ latex: ']' },
{ latex: '\\Leftrightarrow' },
{ class: 'separator w5' },
{ latex: '\\mid' },
{ class: 'separator w5' },
{
class: 'action',
label: "<svg><use xlink:href='#svg-arrow-left' /></svg>",
// command: ['performWithFeedback', 'moveToPreviousChar']
// command: ['performWithFeedback', 'moveToPreviousChar']
command: ['switchMode', 'math',
'\\frac{#0}{#0}']
// command: ['insert', '\\frac{#0}{#0}']
},
{
class: 'action',
label: "<svg><use xlink:href='#svg-arrow-right' /></svg>",
// command: ['performWithFeedback', 'moveToNextChar']
command: ['switchMode', 'math']
},
{
class: 'action font-glyph bottom right',
label: '⌫',
// ifMode: 'math',
command: ['performWithFeedback', 'deleteBackward']
// command: ['insert', '\\sqrt{#0}', { format: 'latex' }]
}
]
]
}
};
}
ngOnChanges(changes: SimpleChanges): void {
this.mathFieldElement.setValue(changes.value.currentValue, { mode: 'text' });
}
setParseMode(event: MatButtonToggleChange) {
// TODO Keyboard moving up and down on focus loss may be avoided by using useSharedVirtualKeyboard
this.mathFieldElement.mode = event.value;
(this.mathfieldRef.nativeElement.childNodes[0] as HTMLElement).focus();
}
}
@NgModule({
declarations: [
MathInputComponent
],
imports: [
CommonModule,
MatButtonToggleModule
],
exports: [
MathInputComponent
],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
export class MathEditorModule {}