|
@@ -1,6 +1,10 @@
|
|
-import { Component, OnInit, OnDestroy } from '@angular/core';
|
|
|
|
|
|
+import { Component, OnInit, OnDestroy, Inject } from '@angular/core';
|
|
import { Editor } from 'ngx-editor';
|
|
import { Editor } from 'ngx-editor';
|
|
-import { marked } from 'marked';
|
|
|
|
|
|
+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';
|
|
|
|
|
|
@Component({
|
|
@Component({
|
|
selector: 'app-journal-notes',
|
|
selector: 'app-journal-notes',
|
|
@@ -9,27 +13,128 @@ import { marked } from 'marked';
|
|
})
|
|
})
|
|
export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
export class JournalNotesComponent implements OnInit, OnDestroy {
|
|
editor: Editor = new Editor();
|
|
editor: Editor = new Editor();
|
|
- html = '';
|
|
|
|
|
|
+ toolbar: any = [
|
|
|
|
+ // default value
|
|
|
|
+ ['bold', 'italic'],
|
|
|
|
+ ['bullet_list'],
|
|
|
|
+ [{ heading: ['h3', 'h4', 'h5', 'h6'] }],
|
|
|
|
+ ];
|
|
|
|
|
|
- markdownString =
|
|
|
|
- ' # Hello \n \
|
|
|
|
- this is a test \
|
|
|
|
- **hi** \
|
|
|
|
- \
|
|
|
|
- - 1\
|
|
|
|
- - 2';
|
|
|
|
|
|
+ /** Used to show the interactale form or the display version of an entry. */
|
|
|
|
+ public isInEditMode = false;
|
|
|
|
|
|
- htmlString = '';
|
|
|
|
|
|
+ /** The index of the currently active entry */
|
|
|
|
+ public currentEntryIndex: number = 0;
|
|
|
|
|
|
- constructor() {
|
|
|
|
- this.htmlString = marked(this.markdownString);
|
|
|
|
- console.log(this.markdownString);
|
|
|
|
- console.log(this.htmlString);
|
|
|
|
|
|
+ private backupIndex: number = -1;
|
|
|
|
+ /** Indicates, if the currentEntry is a newly generated one that is still not saved. */
|
|
|
|
+ public isNewEntry = false;
|
|
|
|
+
|
|
|
|
+ /**The array of JournalEntries, synched to the */
|
|
|
|
+ public entries: JournalEntry[] = [];
|
|
|
|
+
|
|
|
|
+ /** Holds the data for the current entry */
|
|
|
|
+ public currentEntry: JournalEntry = {
|
|
|
|
+ title: '',
|
|
|
|
+ content: '',
|
|
|
|
+ created: new Date(),
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ constructor(
|
|
|
|
+ private _adapter: DateAdapter<any>,
|
|
|
|
+ private dataService: DataService,
|
|
|
|
+ ) {
|
|
|
|
+ registerLocaleData(localeDe);
|
|
|
|
+ this.entries = this.dataService.notesData;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ngOnInit(): void {
|
|
|
|
+ this._adapter.setLocale('de');
|
|
|
|
+ this.entries = this.dataService.notesData;
|
|
|
|
+ console.log('JournalNotesComponent: ', this.entries);
|
|
|
|
+
|
|
|
|
+ // if the list is empty, set the currentEntryIndex to -1 to hide the entry-container
|
|
|
|
+ if (this.entries.length === 0) {
|
|
|
|
+ this.currentEntryIndex = -1;
|
|
|
|
+ } else {
|
|
|
|
+ this.currentEntry = this.entries[0];
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
- ngOnInit(): void {}
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Sets the currentEntry variable when being clicked on in the entries list on the left.
|
|
|
|
+ * @param index The index of the selected entry.
|
|
|
|
+ */
|
|
|
|
+ public selectEntry(index: number): void {
|
|
|
|
+ this.currentEntryIndex = index;
|
|
|
|
+ this.currentEntry = this.getEntryAt(index);
|
|
|
|
+ this.isNewEntry = false;
|
|
|
|
+ this.isInEditMode = false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // DONE
|
|
|
|
+ addEntry(): void {
|
|
|
|
+ this.currentEntry = {
|
|
|
|
+ title: '',
|
|
|
|
+ content: '',
|
|
|
|
+ created: new Date(),
|
|
|
|
+ };
|
|
|
|
+ this.isNewEntry = true;
|
|
|
|
+ this.isInEditMode = true;
|
|
|
|
+ // Hightlight no entry because the placeholder is shown as active
|
|
|
|
+ this.backupIndex = this.currentEntryIndex;
|
|
|
|
+ this.currentEntryIndex = -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public editEntry(): void {
|
|
|
|
+ this.isInEditMode = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // DONE
|
|
|
|
+ public saveEntry(): void {
|
|
|
|
+ if (this.isNewEntry) {
|
|
|
|
+ // Prepend the new JournalEntry
|
|
|
|
+ this.entries.unshift(this.currentEntry);
|
|
|
|
+ this.isNewEntry = false;
|
|
|
|
+ this.currentEntryIndex = 0;
|
|
|
|
+ }
|
|
|
|
+ this.isInEditMode = false;
|
|
|
|
+ this.entries[this.currentEntryIndex] = this.currentEntry;
|
|
|
|
+ this.uploadNotes();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public discardEntry(): void {
|
|
|
|
+ if (this.isNewEntry) {
|
|
|
|
+ this.currentEntryIndex = this.backupIndex;
|
|
|
|
+ this.isNewEntry = false;
|
|
|
|
+ }
|
|
|
|
+ if (this.entries.length > 0) {
|
|
|
|
+ // Reset the currentEntry to the last saved version
|
|
|
|
+ this.currentEntry = this.getEntryAt(this.currentEntryIndex);
|
|
|
|
+ }
|
|
|
|
+ this.isInEditMode = false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ deleteEntry(): void {
|
|
|
|
+ this.entries.splice(this.currentEntryIndex, 1);
|
|
|
|
+ if (this.entries.length === 0) {
|
|
|
|
+ this.currentEntryIndex = -1;
|
|
|
|
+ } else {
|
|
|
|
+ this.currentEntryIndex = Math.max(this.currentEntryIndex - 1, 0);
|
|
|
|
+ this.currentEntry = this.getEntryAt(this.currentEntryIndex);
|
|
|
|
+ }
|
|
|
|
+ this.uploadNotes();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private getEntryAt(index: number): JournalEntry {
|
|
|
|
+ return JSON.parse(JSON.stringify(this.entries[index]));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private uploadNotes(): void {
|
|
|
|
+ console.log('Uploading notes to the server: ', this.entries);
|
|
|
|
+ this.dataService.notesData = this.entries;
|
|
|
|
+ }
|
|
|
|
|
|
- // make sure to destory the editor
|
|
|
|
ngOnDestroy(): void {
|
|
ngOnDestroy(): void {
|
|
this.editor.destroy();
|
|
this.editor.destroy();
|
|
}
|
|
}
|