|
@@ -1,10 +1,24 @@
|
|
|
-import { Component, OnInit, OnDestroy } from '@angular/core';
|
|
|
+import {
|
|
|
+ Component,
|
|
|
+ OnInit,
|
|
|
+ OnDestroy,
|
|
|
+ inject,
|
|
|
+ ComponentFactoryResolver,
|
|
|
+ TemplateRef,
|
|
|
+ ViewChild,
|
|
|
+ ViewContainerRef,
|
|
|
+ Renderer2,
|
|
|
+ ElementRef,
|
|
|
+} from '@angular/core';
|
|
|
import { Editor } from 'ngx-editor';
|
|
|
import { DateAdapter } from '@angular/material/core';
|
|
|
import { JournalEntry } from 'src/interfaces/interfaces';
|
|
|
import localeDe from '@angular/common/locales/de';
|
|
|
import { registerLocaleData } from '@angular/common';
|
|
|
import { DataService } from 'src/services/data/data.service';
|
|
|
+import { TooltipService } from 'src/services/tooltip/tooltip.service';
|
|
|
+import { HighlightComponent } from 'src/app/shared-components/highlight/highlight.component';
|
|
|
+import { DomSanitizer } from '@angular/platform-browser';
|
|
|
|
|
|
@Component({
|
|
|
selector: 'app-journal-notes',
|
|
@@ -12,6 +26,11 @@ import { DataService } from 'src/services/data/data.service';
|
|
|
styleUrl: './journal-notes.component.scss',
|
|
|
})
|
|
|
export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
|
+ @ViewChild('tooltip', { read: TemplateRef }) tooltip!: TemplateRef<any>;
|
|
|
+ @ViewChild('test', { read: ViewContainerRef }) test!: ViewContainerRef;
|
|
|
+
|
|
|
+ tooltipText = 'Initial';
|
|
|
+
|
|
|
editor: Editor = new Editor();
|
|
|
toolbar: any = [
|
|
|
// default value
|
|
@@ -33,6 +52,14 @@ export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
|
/**The array of JournalEntries, synched to the */
|
|
|
public entries: JournalEntry[] = [];
|
|
|
|
|
|
+ public tooltipifiedEntries: JournalEntry[] = [];
|
|
|
+
|
|
|
+ tooltipifiedEntry: JournalEntry = {
|
|
|
+ title: 'Title',
|
|
|
+ content: 'Content',
|
|
|
+ created: new Date(),
|
|
|
+ };
|
|
|
+
|
|
|
/** Holds the data for the current entry */
|
|
|
public currentEntry: JournalEntry = {
|
|
|
title: '',
|
|
@@ -40,18 +67,18 @@ export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
|
created: new Date(),
|
|
|
};
|
|
|
|
|
|
- constructor(
|
|
|
- private _adapter: DateAdapter<any>,
|
|
|
- private dataService: DataService,
|
|
|
- ) {
|
|
|
- registerLocaleData(localeDe);
|
|
|
- this.entries = this.dataService.notesData;
|
|
|
- }
|
|
|
+ private _adapter: DateAdapter<any> = inject(DateAdapter);
|
|
|
+ private dataService: DataService = inject(DataService);
|
|
|
+ private tooltipService: TooltipService = inject(TooltipService);
|
|
|
+ private sanitizer: DomSanitizer = inject(DomSanitizer);
|
|
|
+ private renderer: Renderer2 = inject(Renderer2);
|
|
|
+ private el: ElementRef = inject(ElementRef);
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
+ registerLocaleData(localeDe);
|
|
|
this._adapter.setLocale('de');
|
|
|
this.entries = this.dataService.notesData;
|
|
|
- console.log('JournalNotesComponent: ', this.entries);
|
|
|
+ this.tooltipifiedEntries = this.entries;
|
|
|
|
|
|
// if the list is empty, set the currentEntryIndex to -1 to hide the entry-container
|
|
|
if (this.entries.length === 0) {
|
|
@@ -59,6 +86,7 @@ export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
|
} else {
|
|
|
this.currentEntry = this.entries[0];
|
|
|
}
|
|
|
+ this.tooltipify();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -70,6 +98,10 @@ export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
|
this.currentEntry = this.getEntryAt(index);
|
|
|
this.isNewEntry = false;
|
|
|
this.isInEditMode = false;
|
|
|
+ // this.tooltipify();
|
|
|
+ this.collectNamesFromEntry().forEach((name) => {
|
|
|
+ this.addHighlightsToText(name);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
// DONE
|
|
@@ -100,6 +132,7 @@ export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
|
this.isInEditMode = false;
|
|
|
this.entries[this.currentEntryIndex] = this.currentEntry;
|
|
|
+ this.tooltipify();
|
|
|
this.uploadNotes();
|
|
|
}
|
|
|
|
|
@@ -131,10 +164,66 @@ export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
|
|
|
|
private uploadNotes(): void {
|
|
|
- console.log('Uploading notes to the server: ', this.entries);
|
|
|
this.dataService.notesData = this.entries;
|
|
|
}
|
|
|
|
|
|
+ ////////////////////////////////////////////////////
|
|
|
+
|
|
|
+ tooltipify() {
|
|
|
+ let result: any = this.tooltipService.tooltipifyEntry(
|
|
|
+ JSON.parse(JSON.stringify(this.currentEntry.content)),
|
|
|
+ );
|
|
|
+ console.log(result);
|
|
|
+ this.tooltipifiedEntry = result.content;
|
|
|
+ result.npcs.forEach((name: string) => {
|
|
|
+ this.addHighlightsToText(name);
|
|
|
+ });
|
|
|
+
|
|
|
+ // result = {
|
|
|
+ // enntryContent: string;
|
|
|
+ // npcs: string[] = [xyz];
|
|
|
+ // npcDescriptions: {
|
|
|
+ // name1: 'description1',
|
|
|
+ // name2: 'description2',
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // result.forEach((entry: any) => {
|
|
|
+ // entry.content = this.sanitizer.bypassSecurityTrustHtml(entry.content);
|
|
|
+ // });
|
|
|
+ // this.tooltipifiedEntries = result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Searches the current entry for names that are mentioned in the text.
|
|
|
+ * @returns An array of all the names that are mentioned in the current entry.
|
|
|
+ */
|
|
|
+ private collectNamesFromEntry(): string[] {
|
|
|
+ const matches = this.currentEntry.content.match(/(?<=@)\w+/g);
|
|
|
+ const uniqueMatches = [...new Set(matches)];
|
|
|
+ return uniqueMatches;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Highlights and adds tooltips to the names that are mentioned in the current entry.
|
|
|
+ * @param name The name of the person which mentionings are to be highlighted.
|
|
|
+ */
|
|
|
+ addHighlightsToText(name: string) {
|
|
|
+ // Target the correct containers
|
|
|
+ const parent = this.el.nativeElement.querySelectorAll('.' + name);
|
|
|
+ parent.forEach((element: any) => {
|
|
|
+ const componentRef = this.test.createComponent(HighlightComponent);
|
|
|
+ // The name
|
|
|
+ componentRef.instance.text = name;
|
|
|
+ // A reference to the tooltip template
|
|
|
+ componentRef.instance.tooltip = this.tooltip;
|
|
|
+ // Appends it at the right spot
|
|
|
+ this.renderer.appendChild(element, componentRef.location.nativeElement);
|
|
|
+ });
|
|
|
+ // Modufies the text inside the tooltip template
|
|
|
+ // Muss auf mouseover erfolgen
|
|
|
+ this.tooltipText = 'Neuer Tooltip <b>text</b>';
|
|
|
+ }
|
|
|
+
|
|
|
ngOnDestroy(): void {
|
|
|
this.editor.destroy();
|
|
|
}
|