All files / src/models NoteModel.ts

0% Statements 0/34
0% Branches 0/19
0% Functions 0/7
0% Lines 0/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126                                                                                                                                                                                                                                                           
// Note: DocumentModel class
 
import { TFile } from 'obsidian';
import { Path } from 'src/classes/Path';
import { Inject } from 'src/libs/DependencyInjection/decorators/Inject';
import { IDIContainer } from 'src/libs/DependencyInjection/interfaces/IDIContainer';
import { HelperGeneral } from 'src/libs/Helper/General';
import type { IPrjSettings } from 'src/types/PrjSettings';
import PrjNoteData from './Data/PrjNoteData';
import { FileModel } from './FileModel';
import IPrjModel from './interfaces/IPrjModel';
 
/**
 * Represents the model for a note.
 */
export class NoteModel
    extends FileModel<PrjNoteData>
    implements IPrjModel<PrjNoteData>
{
    @Inject('IPrjSettings')
    protected static _IPrjSettings: IPrjSettings;
 
    /**
     * The data of the note.
     */
    public get data(): Partial<PrjNoteData> {
        return this._data;
    }
    /**
     * The data of the note.
     */
    public set data(value: Partial<PrjNoteData>) {
        this._data = value;
    }
 
    /**
     * Creates a new instance of the note model.
     * @param file The file to create the model for.
     * @param dependencies The optional dependencies to use.
     */
    constructor(file: TFile | undefined, dependencies?: IDIContainer) {
        super(file, PrjNoteData, undefined, dependencies);
    }
 
    /**
     * Get a wikilink for the note
     * @param text The text to display in the wikilink
     * @returns The wikilink. E.g. `[[FileName]]` or `[[FileName|Text]]`
     * @deprecated This method is deprecated and will be removed in a future version.
     */
    public getWikilink(text: string | undefined): string {
        if (text) {
            return `[[${this.file.name}|${text}]]`;
        } else {
            return `[[${this.file.name}]]`;
        }
    }
 
    /**
     * Returns the file contents of the document
     * @returns String containing the file contents
     * @deprecated This method is deprecated and will be removed in a future version.
     */
    public async getFileContents(): Promise<string | undefined> {
        try {
            return this._IApp.vault.read(this.file);
        } catch (error) {
            this._logger?.error(error);
        }
    }
 
    /**
     * Returns the tags of the document as an array of strings
     * @returns Array of strings containing the tags
     * @deprecated This method is deprecated and will be removed in a future version.
     */
    public getTags(): string[] {
        const tags = this.data.tags;
        let formattedTags: string[] = [];
 
        if (tags && typeof tags === 'string') {
            formattedTags = [tags];
        } else Iif (Array.isArray(tags)) {
            formattedTags = [...tags];
        }
 
        return formattedTags;
    }
 
    //#region Static API
    /**
     * Static API for the NoteModel class.
     */
 
    /**
     * Generates a filename based on the provided NoteModel.
     * @param model The NoteModel object used to generate the filename.
     * @returns The generated filename as a string.
     */
    public static generateFilename(model: NoteModel): string {
        const newFileName: string[] = [];
 
        Iif (model.data.date) {
            newFileName.push(
                `${HelperGeneral.formatDate(model.data.date, this._IPrjSettings.dateFormat)}`,
            );
        }
 
        Iif (model.data.tags) {
            const firstTag = model.data.tags.first();
 
            Iif (firstTag && firstTag !== undefined) {
                const lastTagElement = firstTag.getElements().last();
                lastTagElement && newFileName.push(lastTagElement);
            }
        }
 
        Iif (model.data.title) {
            newFileName.push(model.data.title);
        }
 
        return Path.sanitizeFilename(newFileName.join(' - '));
    }
    //#endregion
}
 
Zur TypeDoc-Dokumentation