From 044b40ba35491ec699af2c7c69d8e24e313595c9 Mon Sep 17 00:00:00 2001
From: rhenck <richard.henck@iqb.hu-berlin.de>
Date: Mon, 24 Jun 2024 11:47:16 +0200
Subject: [PATCH] [editor] Improve code style

---
 .../services/unit-services/element.service.ts | 52 +++++++++----------
 .../services/unit-services/page.service.ts    | 12 ++---
 .../services/unit-services/section.service.ts | 33 ++++++------
 .../services/unit-services/unit.service.ts    | 20 +++----
 4 files changed, 55 insertions(+), 62 deletions(-)

diff --git a/projects/editor/src/app/services/unit-services/element.service.ts b/projects/editor/src/app/services/unit-services/element.service.ts
index 0f6b1a00a..a5b2d6cd3 100644
--- a/projects/editor/src/app/services/unit-services/element.service.ts
+++ b/projects/editor/src/app/services/unit-services/element.service.ts
@@ -3,12 +3,8 @@ import { UnitService } from 'editor/src/app/services/unit-services/unit.service'
 import { SelectionService } from 'editor/src/app/services/selection.service';
 import { IDService } from 'editor/src/app/services/id.service';
 import {
-  CompoundElement,
-  InputElement, PlayerElement,
-  PositionedUIElement,
-  UIElement,
-  UIElementProperties,
-  UIElementType, UIElementValue
+  UIElement, CompoundElement, InputElement, PlayerElement, PositionedUIElement,
+  UIElementProperties, UIElementType, UIElementValue
 } from 'common/models/elements/element';
 import { Section } from 'common/models/section';
 import { GeometryProperties } from 'common/models/elements/geometry/geometry';
@@ -18,9 +14,7 @@ import { AudioProperties } from 'common/models/elements/media-elements/audio';
 import { VideoProperties } from 'common/models/elements/media-elements/video';
 import { ImageProperties } from 'common/models/elements/media-elements/image';
 import {
-  PlayerProperties,
-  PositionProperties,
-  PropertyGroupGenerators
+  PlayerProperties, PositionProperties, PropertyGroupGenerators
 } from 'common/models/elements/property-group-interfaces';
 import { ElementFactory } from 'common/util/element.factory';
 import { ReferenceManager } from 'editor/src/app/services/reference-manager';
@@ -52,7 +46,7 @@ export class ElementService {
         try {
           newElementProperties = await this.prepareElementProps(elementType, section.dynamicPositioning, coordinates);
         } catch (e) {
-          if (e == 'dialogCanceled') return {};
+          if (e === 'dialogCanceled') return {};
         }
         const newElementID = this.idService.getAndRegisterNewID(elementType);
         section.addElement(ElementFactory.createElement({
@@ -61,7 +55,7 @@ export class ElementService {
           ...newElementProperties,
           id: newElementID
         }) as PositionedUIElement);
-        return {newElementID};
+        return { newElementID };
       },
       rollback: (deletedData: Record<string, unknown>) => {
         this.idService.unregisterID(deletedData.newElementID as string);
@@ -76,19 +70,21 @@ export class ElementService {
     const newElementProperties: Partial<UIElementProperties> = {};
 
     switch (elementType) {
-      case "geometry":
+      case 'geometry':
         (newElementProperties as GeometryProperties).appDefinition =
           await firstValueFrom(this.dialogService.showGeogebraAppDefinitionDialog());
-        if (!(newElementProperties as GeometryProperties).appDefinition) return Promise.reject('dialogCanceled'); // dialog canceled
+        if (!(newElementProperties as GeometryProperties).appDefinition) {
+          return Promise.reject('dialogCanceled');
+        }
         break;
-      case "audio":
+      case 'audio':
         (newElementProperties as AudioProperties).src = await FileService.loadAudio();
         break;
-      case "video":
+      case 'video':
         (newElementProperties as VideoProperties).src = await FileService.loadVideo();
         break;
-      case "image":
-      case "hotspot-image":
+      case 'image':
+      case 'hotspot-image':
         (newElementProperties as ImageProperties).src = await FileService.loadImage();
         break;
       case 'frame':
@@ -123,13 +119,13 @@ export class ElementService {
             this.unitService.unit.pages[this.selectionService.selectedPageIndex].sections[x.sectionIndex]
               .deleteElement(x.element.id);
           });
-          return {elementSections};
+          return { elementSections };
         }
         return {};
       },
       rollback: (deletedData: Record<string, unknown>) => {
         this.unitService.registerIDs(elements);
-        (deletedData.elementSections as {sectionIndex: number, element: UIElement}[]).forEach(x => {
+        (deletedData.elementSections as { sectionIndex: number, element: UIElement }[]).forEach(x => {
           this.unitService.unit
             .pages[this.selectionService.selectedPageIndex]
             .sections[x.sectionIndex]
@@ -139,12 +135,12 @@ export class ElementService {
     });
   }
 
-  private findElementsInSections(elements: UIElement[]): {sectionIndex: number, element: UIElement}[] {
-    const elementSections: {sectionIndex: number, element: UIElement}[] = []
+  private findElementsInSections(elements: UIElement[]): { sectionIndex: number, element: UIElement }[] {
+    const elementSections: { sectionIndex: number, element: UIElement }[] = [];
     elements.forEach(element => {
       this.unitService.unit.pages[this.selectionService.selectedPageIndex].sections.forEach((section, i) => {
         if (section.elements.map(el => el.id).includes(element.id)) {
-          elementSections.push({sectionIndex: i, element});
+          elementSections.push({ sectionIndex: i, element });
         }
       });
     });
@@ -152,7 +148,7 @@ export class ElementService {
   }
 
   updateElementsProperty(elements: UIElement[], property: string, value: unknown): void {
-    console.log('updateElementsProperty ', elements, property, value);
+    // console.log('updateElementsProperty ', elements, property, value);
     elements.forEach(element => {
       if (property === 'id') {
         if (this.idService.validateAndAddNewID(value as string, element.id)) {
@@ -164,7 +160,9 @@ export class ElementService {
         this.handleClozeDocumentChange(element as ClozeElement, value as ClozeDocument);
       } else {
         element.setProperty(property, value);
-        if (element.type === 'geometry' && property !== 'trackedVariables') this.unitService.geometryElementPropertyUpdated.next(element.id);
+        if (element.type === 'geometry' && property !== 'trackedVariables') {
+          this.unitService.geometryElementPropertyUpdated.next(element.id);
+        }
         if (element.type === 'math-table') this.unitService.mathTableElementPropertyUpdated.next(element.id);
         if (element.type === 'table') this.unitService.tablePropUpdated.next(element.id);
       }
@@ -285,7 +283,7 @@ export class ElementService {
         break;
       case 'cloze':
         this.dialogService.showClozeTextEditDialog(
-          (element as ClozeElement).document!,
+          (element as ClozeElement).document,
           (element as ClozeElement).styling.fontSize
         ).subscribe((result: string) => {
           if (result) {
@@ -358,7 +356,8 @@ export class ElementService {
 
   duplicateSelectedElements(): void {
     const selectedSection =
-      this.unitService.unit.pages[this.selectionService.selectedPageIndex].sections[this.selectionService.selectedSectionIndex];
+      this.unitService.unit.pages[this.selectionService.selectedPageIndex]
+        .sections[this.selectionService.selectedSectionIndex];
     this.selectionService.getSelectedElements().forEach((element: UIElement) => {
       selectedSection.elements.push(this.duplicateElement(element, true) as PositionedUIElement);
     });
@@ -423,7 +422,6 @@ export class ElementService {
   }
 
   updateElementsDimensionsProperty(elements: UIElement[], property: string, value: number | null): void {
-    console.log('updateElementsDimensionsProperty', property, value);
     elements.forEach(element => {
       element.setDimensionsProperty(property, value);
     });
diff --git a/projects/editor/src/app/services/unit-services/page.service.ts b/projects/editor/src/app/services/unit-services/page.service.ts
index 477c7b497..ba14415a9 100644
--- a/projects/editor/src/app/services/unit-services/page.service.ts
+++ b/projects/editor/src/app/services/unit-services/page.service.ts
@@ -1,7 +1,6 @@
 import { Injectable } from '@angular/core';
 import { Page } from 'common/models/page';
 import { UnitService } from 'editor/src/app/services/unit-services/unit.service';
-import { MessageService } from 'editor/src/app/services/message.service';
 import { SelectionService } from 'editor/src/app/services/selection.service';
 import { ArrayUtils } from 'common/util/array';
 
@@ -10,7 +9,6 @@ import { ArrayUtils } from 'common/util/array';
 })
 export class PageService {
   constructor(private unitService: UnitService,
-              private messageService: MessageService,
               private selectionService: SelectionService) { }
 
   addPage(): void {
@@ -18,7 +16,7 @@ export class PageService {
       title: 'Seite hinzugefügt',
       command: () => {
         this.unitService.unit.pages.push(new Page());
-        this.selectionService.selectedPageIndex = this.unitService.unit.pages.length - 1; // TODO selection stuff here is not good
+        this.selectionService.selectedPageIndex = this.unitService.unit.pages.length - 1;
         return {};
       },
       rollback: () => {
@@ -46,8 +44,8 @@ export class PageService {
         return {};
       },
       rollback: (deletedData: Record<string, unknown>) => {
-        this.unitService.registerIDs((deletedData['deletedpage'] as Page).getAllElements());
-        this.unitService.unit.pages.splice(deletedData['pageIndex'] as number, 0, deletedData['deletedpage'] as Page);
+        this.unitService.registerIDs((deletedData.deletedpage as Page).getAllElements());
+        this.unitService.unit.pages.splice(deletedData.pageIndex as number, 0, deletedData.deletedpage as Page);
         this.unitService.updateSectionCounter();
       }
     });
@@ -63,9 +61,9 @@ export class PageService {
           direction === 'left' ? 'up' : 'down'
         );
         this.unitService.updateSectionCounter();
-        return {direction};
+        return { direction };
       },
-      rollback: (deletedData: Record<string, unknown>) => {
+      rollback: () => {
         ArrayUtils.moveArrayItem(
           this.unitService.unit.pages[pageIndex],
           this.unitService.unit.pages,
diff --git a/projects/editor/src/app/services/unit-services/section.service.ts b/projects/editor/src/app/services/unit-services/section.service.ts
index 1224b7e33..63ae6245b 100644
--- a/projects/editor/src/app/services/unit-services/section.service.ts
+++ b/projects/editor/src/app/services/unit-services/section.service.ts
@@ -5,7 +5,6 @@ import { Page } from 'common/models/page';
 import { Section } from 'common/models/section';
 import { PositionedUIElement, UIElement, UIElementValue } from 'common/models/elements/element';
 import { ArrayUtils } from 'common/util/array';
-import { IDService } from 'editor/src/app/services/id.service';
 import { VisibilityRule } from 'common/models/visibility-rule';
 import { ElementService } from 'editor/src/app/services/unit-services/element.service';
 
@@ -15,8 +14,7 @@ import { ElementService } from 'editor/src/app/services/unit-services/element.se
 export class SectionService {
   constructor(private unitService: UnitService,
               private elementService: ElementService,
-              private selectionService: SelectionService,
-              private idService: IDService) { }
+              private selectionService: SelectionService) { }
 
   updateSectionProperty(section: Section, property: string, value: string | number | boolean | VisibilityRule[] | { value: number; unit: string }[]): void {
     this.unitService.updateUnitDefinition({
@@ -26,7 +24,7 @@ export class SectionService {
         section.setProperty(property, value);
         if (property === 'ignoreNumbering') this.unitService.updateSectionCounter();
         this.unitService.elementPropertyUpdated.next();
-        return {oldValue};
+        return { oldValue };
       },
       rollback: (deletedData: Record<string, unknown>) => {
         section.setProperty(property, deletedData.oldValue as UIElementValue);
@@ -39,14 +37,11 @@ export class SectionService {
     this.unitService.updateUnitDefinition({
       title: 'Abschnitt hinzugefügt',
       command: () => {
-        const newSection = section;
-        if (section) {
-          this.unitService.registerIDs(section.elements);
-        }
+        if (section) this.unitService.registerIDs(section.elements);
         page.addSection(section, sectionIndex);
         this.selectionService.selectedSectionIndex =
           Math.max(0, this.selectionService.selectedSectionIndex - 1);
-        return {section, sectionIndex};
+        return { section, sectionIndex };
       },
       rollback: (deletedData: Record<string, unknown>) => {
         if (deletedData.section) {
@@ -73,7 +68,7 @@ export class SectionService {
           this.selectionService.selectedSectionIndex =
             Math.max(0, this.selectionService.selectedSectionIndex - 1);
           this.unitService.updateSectionCounter();
-          return {deletedSection: sectionToDelete, pageIndex, sectionIndex};
+          return { deletedSection: sectionToDelete, pageIndex, sectionIndex };
         }
         return {};
       },
@@ -90,7 +85,7 @@ export class SectionService {
   duplicateSection(section: Section, sectionIndex: number): void {
     const page = this.unitService.getSelectedPage();
     this.unitService.updateUnitDefinition({
-      title: `Abschnitt dupliziert`,
+      title: 'Abschnitt dupliziert',
       command: () => {
         const newSection: Section = new Section({
           ...section,
@@ -101,7 +96,7 @@ export class SectionService {
         this.unitService.updateSectionCounter();
         return {};
       },
-      rollback: (deletedData: Record<string, unknown>) => {
+      rollback: () => {
         this.unitService.unregisterIDs(page.sections[sectionIndex + 1].getAllElements());
         page.deleteSection(sectionIndex + 1);
         this.selectionService.selectedSectionIndex -= 1;
@@ -113,16 +108,18 @@ export class SectionService {
   moveSection(section: Section, direction: 'up' | 'down'): void {
     const page = this.unitService.getSelectedPage();
     this.unitService.updateUnitDefinition({
-      title: `Abschnitt verschoben`,
+      title: 'Abschnitt verschoben',
       command: () => {
         ArrayUtils.moveArrayItem(section, page.sections, direction);
-        direction === 'up' ? this.selectionService.selectedSectionIndex -= 1 : this.selectionService.selectedSectionIndex += 1;
+        direction === 'up' ? this.selectionService.selectedSectionIndex -= 1 :
+          this.selectionService.selectedSectionIndex += 1;
         this.unitService.updateSectionCounter();
         return {};
       },
-      rollback: (deletedData: Record<string, unknown>) => {
+      rollback: () => {
         ArrayUtils.moveArrayItem(section, page.sections, direction === 'up' ? 'down' : 'up');
-        direction === 'up' ? this.selectionService.selectedSectionIndex += 1 : this.selectionService.selectedSectionIndex -= 1;
+        direction === 'up' ? this.selectionService.selectedSectionIndex += 1 :
+          this.selectionService.selectedSectionIndex -= 1;
         this.unitService.updateSectionCounter();
       }
     });
@@ -136,7 +133,7 @@ export class SectionService {
   /* Move element between sections */
   transferElements(elements: UIElement[], previousSection: Section, newSection: Section): void {
     this.unitService.updateUnitDefinition({
-      title: `Element zwischen Abschnitten verschoben`,
+      title: 'Element zwischen Abschnitten verschoben',
       command: () => {
         previousSection.elements = previousSection.elements.filter(element => !elements.includes(element));
         elements.forEach(element => {
@@ -144,7 +141,7 @@ export class SectionService {
         });
         return {};
       },
-      rollback: (deletedData: Record<string, unknown>) => {
+      rollback: () => {
         newSection.elements = newSection.elements.filter(element => !elements.includes(element));
         elements.forEach(element => {
           previousSection.elements.push(element as PositionedUIElement);
diff --git a/projects/editor/src/app/services/unit-services/unit.service.ts b/projects/editor/src/app/services/unit-services/unit.service.ts
index 05eb505fb..1524da362 100644
--- a/projects/editor/src/app/services/unit-services/unit.service.ts
+++ b/projects/editor/src/app/services/unit-services/unit.service.ts
@@ -1,5 +1,5 @@
 import { Injectable } from '@angular/core';
-import {  Subject } from 'rxjs';
+import { Subject } from 'rxjs';
 import { FileService } from 'common/services/file.service';
 import { MessageService } from 'editor/src/app/services/message.service';
 import { Unit, UnitProperties } from 'common/models/unit';
@@ -10,16 +10,16 @@ import {
 import { DropListElement } from 'common/models/elements/input-elements/drop-list';
 import { StateVariable } from 'common/models/state-variable';
 import { VersionManager } from 'common/services/version-manager';
+import { Page } from 'common/models/page';
+import { Section } from 'common/models/section';
+import { SectionCounter } from 'common/util/section-counter';
 import { ReferenceList, ReferenceManager } from 'editor/src/app/services/reference-manager';
+import { HistoryService, UnitUpdateCommand } from 'editor/src/app/services/history.service';
 import { DialogService } from '../dialog.service';
 import { VeronaAPIService } from '../verona-api.service';
 import { SelectionService } from '../selection.service';
 import { IDService } from '../id.service';
 import { UnitDefinitionSanitizer } from '../sanitizer';
-import { HistoryService, UnitUpdateCommand } from 'editor/src/app/services/history.service';
-import { Page } from 'common/models/page';
-import { Section } from 'common/models/section';
-import { SectionCounter } from 'common/util/section-counter';
 
 @Injectable({
   providedIn: 'root'
@@ -92,7 +92,7 @@ export class UnitService {
 
   updateUnitDefinition(command?: UnitUpdateCommand): void {
     if (command) {
-      let deletedData = command.command();
+      const deletedData = command.command();
       if (deletedData instanceof Promise) {
         deletedData.then((deletedData) => {
           this.historyService.addCommand(command, deletedData);
@@ -173,7 +173,7 @@ export class UnitService {
       let refs: ReferenceList[] = [];
       let dialogText: string = '';
       switch (deletedObjectType) {
-        case "page":
+        case 'page':
           refs = this.referenceManager.getPageElementsReferences(
             this.unit.pages[this.selectionService.selectedPageIndex]
           );
@@ -183,18 +183,18 @@ export class UnitService {
           refs = refs.concat(pageNavButtonRefs);
           dialogText = `Seite ${this.selectionService.selectedPageIndex + 1} löschen?`;
           break;
-        case "section":
+        case 'section':
           refs = this.referenceManager.getSectionElementsReferences([object as Section]);
           dialogText = `Abschnitt ${this.selectionService.selectedSectionIndex + 1} löschen?`;
           break;
-        case "elements":
+        case 'elements':
           refs = this.referenceManager.getElementsReferences(object as UIElement[]);
           dialogText = 'Folgende Elemente werden gelöscht:';
       }
 
       this.dialogService.showDeleteConfirmDialog(
         dialogText,
-        deletedObjectType == 'elements' ? object as UIElement[] : undefined,
+        deletedObjectType === 'elements' ? object as UIElement[] : undefined,
         refs)
         .subscribe((result: boolean) => {
           if (result) {
-- 
GitLab