Newer
Older
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Input,
OnChanges,
Output,
SimpleChanges,
ViewChild
import { MathfieldElement } from '@iqb/mathlive';
import { MatButtonToggleChange } from '@angular/material/button-toggle';
import { IQB_MATH_KEYBOARD_LAYOUTS } from 'common/math-editor/keyboard-layout.config';
template: `
<mat-button-toggle-group *ngIf="enableModeSwitch"
[value]="mathFieldElement.mode"
(change)="setParseMode($event)">
<mat-button-toggle value="math">Formel</mat-button-toggle>
<mat-button-toggle value="text">Text</mat-button-toggle>
</mat-button-toggle-group>
<div #inputRef
(input)="onInput()"
[class.full-width]="fullWidth"
[class.inline-block]="!fullWidth"
[class.read-only]="readonly"
(focusin)="onFocusIn()"
(focusout)="onFocusOut()">
styles: [`
mat-button-toggle-group {
height: auto;
}
:host ::ng-deep .full-width math-field {
display: block;
}
.inline-block{
display: inline-block;
}
:host ::ng-deep math-field::part(virtual-keyboard-toggle) {
display: none;
}
:host ::ng-deep math-field::part(menu-toggle) {
display: none;
}
:host ::ng-deep .read-only math-field {
}
:host ::ng-deep .mat-button-toggle-label-content {
line-height: unset;
}`
]
})
export class MathInputComponent implements AfterViewInit, OnChanges {
@Input() value!: string;
@Output() valueChange: EventEmitter<string> = new EventEmitter();
@Output() focusIn: EventEmitter<MathfieldElement> = new EventEmitter();
@Output() focusOut: EventEmitter<MathfieldElement> = new EventEmitter();
@ViewChild('inputRef') inputRef!: ElementRef;
protected readonly window = window;
private mathKeyboardLayout = IQB_MATH_KEYBOARD_LAYOUTS;
mathFieldElement: MathfieldElement = new MathfieldElement({
mathVirtualKeyboardPolicy: 'manual'
constructor(public elementRef: ElementRef) {
}
this.inputRef.nativeElement.appendChild(this.mathFieldElement);
this.mathFieldElement.readOnly = this.readonly;
setTimeout(() => {
this.mathFieldElement.menuItems = [];
}); // Disable context menu
if (changes.value) {
this.mathFieldElement.setValue(changes.value.currentValue, { mode: 'text' });
}
setFocus(offset?: number): void {
this.mathFieldElement.focus();
}
setParseMode(event: MatButtonToggleChange) {
this.mathFieldElement.mode = event.value;
(this.inputRef.nativeElement.childNodes[0] as HTMLElement).focus();
onInput() {
this.valueChange.emit(this.mathFieldElement.getValue());
}
onFocusIn() {
this.focusIn.emit(this.mathFieldElement);
// TODO Reset Shift key
this.resetKeyboardLayout();
private resetKeyboardLayout() {
window.mathVirtualKeyboard.layouts = ['default'];
// Start with first tab of keyboard
setTimeout(() => this.setKeyboardLayout());
}
private setKeyboardLayout(): void {
window.mathVirtualKeyboard.layouts = [
this.mathKeyboardLayout.iqbNumeric,
this.mathKeyboardLayout.iqbSymbols,
this.mathKeyboardLayout.iqbText,
this.mathKeyboardLayout.iqbGreek
];
}
onFocusOut() {
this.focusOut.emit(this.mathFieldElement);