From 99945f8841579aa20626b0d45c83afba90ab64b2 Mon Sep 17 00:00:00 2001
From: jojohoch <joachim.hoch@iqb.hu-berlin.de>
Date: Wed, 18 Dec 2024 11:05:13 +0100
Subject: [PATCH] Fix flickering of iqb keyboard, when first formula is added

---
 .../text-area-math/text-area-math.component.ts      | 13 +++++++++----
 projects/common/services/range-selection-service.ts |  8 ++++++++
 .../player/modules/logging/services/log.service.ts  |  2 +-
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/projects/common/components/input-elements/text-area-math/text-area-math.component.ts b/projects/common/components/input-elements/text-area-math/text-area-math.component.ts
index 09929a672..39969cce9 100644
--- a/projects/common/components/input-elements/text-area-math/text-area-math.component.ts
+++ b/projects/common/components/input-elements/text-area-math/text-area-math.component.ts
@@ -91,15 +91,20 @@ export class TextAreaMathComponent extends TextInputComponent implements OnInit
     if (this.segments.length === 0) {
       this.addStartSegment();
       // wait for rendering of segments
-      setTimeout(() => this.addNewSegments());
+      setTimeout(() => this.addNewSegments(false));
     } else {
-      this.addNewSegments();
+      this.addNewSegments(true);
     }
   }
 
-  private addNewSegments() {
+  private addNewSegments(withFocusUpdate: boolean) {
     this.segments = this.elementFormControl.value;
-    this.updateFocus(this.selectedFocus.value);
+    if (withFocusUpdate) {
+      this.updateFocus(this.selectedFocus.value);
+    } else {
+      RangeSelectionService
+        .setRange(this.segmentComponents.toArray()[this.selectedFocus.value].inputComponent.inputRef.nativeElement);
+    }
     const range = RangeSelectionService.getRange();
     if (!range) return;
 
diff --git a/projects/common/services/range-selection-service.ts b/projects/common/services/range-selection-service.ts
index 816961d70..f1bf85979 100644
--- a/projects/common/services/range-selection-service.ts
+++ b/projects/common/services/range-selection-service.ts
@@ -36,6 +36,14 @@ export class RangeSelectionService {
     return { start, end };
   }
 
+  static setRange(element: HTMLElement) {
+    const range = document.createRange();
+    range.selectNode(element);
+    const selection = window.getSelection();
+    selection?.removeAllRanges();
+    selection?.addRange(range);
+  }
+
   static setSelectionRange(
     inputElement: HTMLElement,
     start: number,
diff --git a/projects/player/modules/logging/services/log.service.ts b/projects/player/modules/logging/services/log.service.ts
index 47ad6e147..651ce17b3 100644
--- a/projects/player/modules/logging/services/log.service.ts
+++ b/projects/player/modules/logging/services/log.service.ts
@@ -6,7 +6,7 @@ export enum LogLevel { NONE = 0, ERROR = 1, WARN = 2, INFO = 3, DEBUG = 4 }
   providedIn: 'root'
 })
 export class LogService {
-  static level: LogLevel = 4;
+  static level: LogLevel = 0;
 
   static error(...args: unknown[]): void {
     if (LogService.level >= LogLevel.ERROR) {
-- 
GitLab