All files / src/libs/ContextMenus ContextMenu.ts

0% Statements 0/26
0% Branches 0/18
0% Functions 0/10
0% Lines 0/26

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                                                                                                                                                                                                                                                                       
import { Menu, TAbstractFile } from 'obsidian';
import type { IApp } from 'src/interfaces/IApp';
import type { ILogger, ILogger_ } from 'src/interfaces/ILogger';
import type { IPrj } from 'src/interfaces/IPrj';
import { IContextMenu } from './interfaces/IContextMenu';
import { Inject } from '../DependencyInjection/decorators/Inject';
 
/**
 * Represents a class for creating a context menu.
 */
export class ContextMenu implements IContextMenu {
    @Inject('ILogger_', (x: ILogger_) => x.getLogger('ContextMenu'), false)
    protected _logger?: ILogger;
    @Inject('IApp')
    protected _IApp: IApp;
    @Inject('IPrj')
    protected _IPrj: IPrj;
    protected _hasEventsAndCommandsRegistered = false;
    protected _bindContextMenu = this.onContextMenu.bind(this);
 
    /**
     * Creates a new instance of the ContextMenu class.
     */
    protected constructor() {}
 
    /**
     * Run this method to signalize that the class is initialized.
     */
    protected isInitialized(): void {
        this.registerEventsAndCommands();
    }
 
    /**
     * Deconstructs the 'ContextMenu' events and commands.
     */
    public deconstructor(): void {
        if (this._hasEventsAndCommandsRegistered) {
            this.deRegisterEventsAndCommands();
        } else {
            this._logger?.trace(`No events to deconstruct`);
        }
    }
 
    /**
     * Registers the events and commands for the class.
     * @remarks This method calls the onConstructon method.
     */
    private registerEventsAndCommands(): void {
        try {
            this.onConstruction();
            this._hasEventsAndCommandsRegistered = true;
            this._logger?.trace(`Constructed '${this.constructor.name}'`);
        } catch (error) {
            this._logger?.error(
                `Error constructing '${this.constructor.name}'`,
                error,
            );
        }
    }
 
    /**
     * Deregesters the events and commands for the class.
     * @remarks This method calls the onDeconstructon method.
     */
    private deRegisterEventsAndCommands(): void {
        try {
            this.onDeconstruction();
            this._hasEventsAndCommandsRegistered = false;
        } catch (error) {
            this._logger?.error(`Error deconstructing events`, error);
 
            throw error;
        }
    }
 
    /**
     * This method is called when the class is constructed.
     * @remarks - This method should be overridden by the derived class.
     * - You should register events and commands in this method.
     * @example
     * ```typescript
     * this._app.workspace.on('file-menu', this.bindContextMenu);
     * this._plugin.addCommand({
     *       id: 'get-metadata-file',
     *       name: 'Show Metadata File',
     *       callback: () => {
     *           ContextMenu.getInstance().invoke();
     *       },
     *   });
     * ```
     * @override
     */
    protected onConstruction(): void {
        throw new Error('Method not implemented; Override this method!');
    }
 
    /**
     * This method is called when the class is deconstructed.
     * @remarks This method should be overridden by the derived class.
     * - You should deregister events and if necessary commands in this method.
     * @example
     * ```typescript
     * this._instance._app.workspace.off(
     *           'file-menu',
     *           this._instance.bindContextMenu,
     *       );
     * ```
     * @override
     */
    protected onDeconstruction(): void {
        throw new Error('Method not implemented; Override this method!');
    }
 
    /**
     * This method is called when the context menu is invoked.
     * @param menu The context menu to add items to.
     * @param file The file the context menu is invoked on.
     * @override
     */
    protected onContextMenu(menu: Menu, file: TAbstractFile): void {
        throw new Error('Method not implemented; Override this method!');
    }
 
    /**
     * This method is called when the command menu is invoked.
     * @override
     */
    public invoke(): Promise<void> {
        throw new Error('Method not implemented; Override this method!');
    }
}
 
Zur TypeDoc-Dokumentation