Newer
Older
AfterViewInit,
Component,
ElementRef,
EventEmitter,
Input,
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, OnDestroy {
@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 readonly mathKeyboardLayout = IQB_MATH_KEYBOARD_LAYOUTS;
mathFieldElement: MathfieldElement = new MathfieldElement({
mathVirtualKeyboardPolicy: 'manual'
constructor(public elementRef: ElementRef) {}
this.setKeyboardLayout();
this.inputRef.nativeElement.appendChild(this.mathFieldElement);
this.mathFieldElement.readOnly = this.readonly;
setTimeout(() => {
this.mathFieldElement.menuItems = [];
}); // Disable context menu
window.mathVirtualKeyboard.addEventListener('virtual-keyboard-layer-change', () => MathInputComponent.resetShift());
}
private static resetShift(): void {
window.mathVirtualKeyboard.shiftPressCount = 0;
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());
}
this.focusIn.emit(this.mathFieldElement);
window.mathVirtualKeyboard.show({ firstLayer: true, resetShift: true });
}
private setKeyboardLayout(): void {
window.mathVirtualKeyboard.layouts = [
this.mathKeyboardLayout.iqbNumeric,
this.mathKeyboardLayout.iqbSymbols,
this.mathKeyboardLayout.iqbText,
this.mathKeyboardLayout.iqbGreek
];
}
onFocusOut() {
this.focusOut.emit(this.mathFieldElement);
// eslint-disable-next-line class-methods-use-this
ngOnDestroy(): void {
window.mathVirtualKeyboard
.removeEventListener('virtual-keyboard-layer-change', () => MathInputComponent.resetShift());
}