All files / src/libs/Helper Obsidian.ts

97.05% Statements 33/34
100% Branches 14/14
88.88% Functions 8/9
96.87% Lines 31/32

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 1131x 1x 1x           1x 1x                   1x     1x 8x 1x                       4x               4x 4x   4x 2x   2x     2x                 4x               4x 4x 4x 4x 4x 4x 4x             4x             4x   4x   4x 4x 4x   2x                            
import { App, Notice, TFile } from 'obsidian';
import { ImplementsStatic } from 'src/classes/decorators/ImplementsStatic';
import { Singleton } from 'src/classes/decorators/Singleton';
import type { ILogger, ILogger_ } from 'src/interfaces/ILogger';
import {
    IHelperObsidian_,
    IHelperObsidian,
} from './interfaces/IHelperObsidian';
import { Inject } from '../DependencyInjection/decorators/Inject';
import { RegisterInstance } from '../DependencyInjection/decorators/RegisterInstance';
 
/**
 * Represents a class for Obsidian related helper methods.
 * @see {@link Singleton}
 */
// eslint-disable-next-line deprecation/deprecation
@ImplementsStatic<IHelperObsidian_>()
@RegisterInstance('IHelperObsidian')
@Singleton
export class HelperObsidian implements IHelperObsidian {
    @Inject('IApp')
    // eslint-disable-next-line @typescript-eslint/naming-convention
    protected _IApp: App;
    @Inject('ILogger_', (x: ILogger_) => x.getLogger('HelperObsidian'), false)
    protected _logger?: ILogger;
 
    /**
     * Create a Singleton instance of the HelperObsidian class.
     */
    constructor() {}
 
    /**
     * Gets the active file in the workspace.
     * @returns The active file in the workspace, or undefined if no file is active.
     */
    public static getActiveFile(): TFile | undefined {
        return new HelperObsidian().getActiveFile();
    }
 
    /**
     * Gets the active file in the workspace.
     * @returns The active file in the workspace, or undefined if no file is active.
     */
    public getActiveFile(): TFile | undefined {
        const workspace = this._IApp.workspace;
        const activeFile = workspace.getActiveFile();
 
        if (!activeFile) {
            this._logger?.warn('No active file found');
 
            return undefined;
        }
 
        return activeFile;
    }
 
    /**
     * Opens the specified file in the active leaf.
     * @param file The file to open.
     * @returns A promise that resolves when the file is opened.
     */
    public static async openFile(file: TFile): Promise<void> {
        return new HelperObsidian().openFile(file);
    }
 
    /**
     * Opens the specified file in the active leaf.
     * @param file The file to open.
     */
    public async openFile(file: TFile): Promise<void> {
        this._logger?.trace(`Opening file: ${file.path}`);
        const workspace = this._IApp.workspace;
        const newLeaf = workspace.getLeaf(true);
        await newLeaf.openFile(file);
        const view = newLeaf.getViewState();
        view.state.mode = 'preview';
        newLeaf.setViewState(view);
    }
 
    /**
     * Rebuilds the active view.
     */
    public static rebuildActiveView(): void {
        new HelperObsidian().rebuildActiveView();
    }
 
    /**
     * Rebuilds the active view.
     */
    public rebuildActiveView(): void {
        const workspace = this._IApp.workspace;
        // eslint-disable-next-line @typescript-eslint/no-explicit-any, deprecation/deprecation
        const activeLeaf = workspace.activeLeaf as any;
 
        if (activeLeaf) {
            try {
                activeLeaf.rebuildView();
            } catch (error) {
                this._logger?.error('Error rebuilding active view', error);
            }
        }
    }
 
    /**
     * Shows a notice
     * @param message The message to show
     * @param timeout The timeout of the notice
     */
    public showNotice(message: string, timeout: number): void {
        new Notice(message, timeout);
    }
}
 
Zur TypeDoc-Dokumentation