All files / src/libs/ContextMenus CopyMarkdownLink.ts

0% Statements 0/30
0% Branches 0/2
0% Functions 0/10
0% Lines 0/28

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                                                                                                                                                                                                     
import { Menu, TAbstractFile, TFile } from 'obsidian';
import { ImplementsStatic } from 'src/classes/decorators/ImplementsStatic';
import { Singleton } from 'src/classes/decorators/Singleton';
import { ContextMenu } from './ContextMenu';
import { IContextMenu } from './interfaces/IContextMenu';
import { Inject } from '../DependencyInjection/decorators/Inject';
import { Lifecycle } from '../LifecycleManager/decorators/Lifecycle';
import { ILifecycleObject } from '../LifecycleManager/interfaces/ILifecycleObject';
import type ITranslationService from '../TranslationService/interfaces/ITranslationService';
 
/**
 * Represents a context menu for copying markdown links.
 * @see {@link Singleton}
 * @see {@link Lifecycle}
 */
@Lifecycle('Static')
@ImplementsStatic<ILifecycleObject>()
@Singleton
export class CopyMarkdownLink extends ContextMenu implements IContextMenu {
    protected _bindContextMenu = this.onContextMenu.bind(this);
    @Inject('ITranslationService')
    private readonly _translationService: ITranslationService;
 
    /**
     * Creates an instance of CopyMarkdownLink.
     */
    constructor() {
        super();
    }
 
    /**
     * This method is called when the application is unloaded.
     */
    public static onLoad(): void {
        const instance = new CopyMarkdownLink();
        instance.isInitialized();
    }
 
    /**
     * This method is called when the application is unloaded.
     */
    public static onUnload(): void {
        const instance = new CopyMarkdownLink();
        instance.deconstructor();
    }
 
    /**
     * Initializes the context menu.
     */
    protected onConstruction(): void {
        this._IApp.workspace.on('file-menu', this._bindContextMenu);
    }
 
    /**
     * Cleans up the context menu.
     */
    protected onDeconstruction(): void {
        this._IApp.workspace.off('file-menu', this._bindContextMenu);
    }
 
    /**
     * Handles the context menu event.
     * @param menu - The context menu.
     * @param file - The file associated with the context menu.
     */
    protected onContextMenu(menu: Menu, file: TAbstractFile): void {
        Iif (!(file instanceof TFile)) {
            return;
        }
 
        const fileText = this._IApp.metadataCache.fileToLinktext(
            file,
            file.path,
        );
 
        Iif (!fileText) {
            return;
        }
 
        const linktext = `[[${fileText}]]`;
 
        menu.addSeparator();
 
        menu.addItem((item) => {
            item.setTitle(this._translationService.get('Copy MD Link'))
                .setIcon('link')
                .onClick(async () => {
                    navigator.clipboard.writeText(linktext);
                });
        });
    }
    /**
     * Invokes the context menu.
     */
    public invoke(): Promise<void> {
        throw new Error('Method not implemented!');
    }
}
 
Zur TypeDoc-Dokumentation