All files / src/libs/Modals BaseModalForm.ts

0% Statements 0/41
0% Branches 0/38
0% Functions 0/7
0% Lines 0/39

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 127 128 129 130 131 132 133                                                                                                                                                                                                                                                                         
import { App, TFile } from 'obsidian';
import type { ILogger, ILogger_ } from 'src/interfaces/ILogger';
import { Tags } from 'src/libs/Tags/Tags';
import {
    FormConfiguration,
    IFormResult,
    IModalForm,
    IResultData,
} from 'src/types/ModalFormType';
import type { IPrjSettings } from 'src/types/PrjSettings';
import { Inject } from '../DependencyInjection/decorators/Inject';
import { HelperObsidian } from '../Helper/Obsidian';
 
/**
 * Represents a base class for modal forms.
 */
export default abstract class BaseModalForm {
    @Inject('IApp')
    protected _IApp!: App;
    @Inject('IPrjSettings')
    protected _IPrjSettings!: IPrjSettings;
    @Inject('ILogger_', (x: ILogger_) => x.getLogger('BaseModalForm'))
    protected _logger?: ILogger;
    protected _modalFormApi: IModalForm | null | undefined = null;
 
    /**
     * Creates an instance an loads the ModalForms API.
     */
    constructor() {
        if (this._modalFormApi === undefined) {
            this._logger?.error('ModalForms API not found');
        } else {
            this._modalFormApi = this.setApi();
        }
    }
 
    /**
     * Checks if the ModalForms API is available
     * @returns True if the API is available
     * @remarks Log an error if the API is not available
     */
    protected isApiAvailable(): boolean {
        Iif (this._modalFormApi === undefined)
            this._logger?.error('ModalForms API not found');
 
        Iif (this._modalFormApi === null) {
            this._modalFormApi = this.setApi();
        }
 
        Iif (this._modalFormApi === null) return false;
 
        return true;
    }
 
    /**
     * Returns the ModalForms API
     * @returns The ModalForms API
     */
    protected getApi(): IModalForm {
        if (this._modalFormApi === undefined || this._modalFormApi === null) {
            this._logger?.error('ModalForms API not found');
            throw new Error('ModalForms API not found');
        } else {
            return this._modalFormApi;
        }
    }
 
    /**
     * Sets the ModalForms API
     * @returns The ModalForms API or undefined if not found
     */
    private setApi(): IModalForm | undefined {
        // eslint-disable-next-line @typescript-eslint/no-explicit-any
        const modalFormApi = (this._IApp as any).plugins.plugins.modalforms
            .api as IModalForm;
 
        if (!modalFormApi) {
            this._logger?.error('ModalForms API not found');
 
            return undefined;
        } else {
            this._logger?.trace('ModalForms API found');
 
            return modalFormApi;
        }
    }
 
    public abstract evaluateForm(
        result: IFormResult,
    ): Promise<unknown | undefined>;
 
    public abstract openForm(): Promise<IFormResult | undefined>;
 
    protected abstract constructForm(): FormConfiguration;
 
    /**
     * Returns the tags from the file.
     * @param activeFile The file to get the tags from.
     * @returns The tags from the file.
     */
    protected getTagsFromActiveFile(
        activeFile: TFile | undefined = HelperObsidian.getActiveFile(),
    ): string[] {
        const activeFileTags = new Tags(undefined);
        activeFileTags.loadTagsFromFile(activeFile);
 
        const tags: string[] = activeFileTags.primitiveOf();
 
        return tags;
    }
 
    /**
     * Converts a preset object to an IResultData object
     * @param preset The preset object to convert, like DocumentData etc.
     * @returns The converted IResultData object
     */
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    protected convertPresetToIResultData<T extends Record<string, any>>(
        preset: Partial<T> | undefined,
    ): IResultData {
        const convertedPreset: IResultData = {};
 
        Iif (preset) {
            for (const [key, value] of Object.entries(preset)) {
                Iif (typeof value === 'function') continue;
                convertedPreset[key] = value ?? '';
            }
        }
 
        return convertedPreset;
    }
}
 
Zur TypeDoc-Dokumentation