Remove COPY_DOWNLOAD_LINK command

Hello, Team,

I am try to remove “file: copy download link” command. when I rebind FileDownloadCommandContribution in my module:

rebind(FileDownloadCommandContribution).to(MyFileDownloadCommandContribution).inSingletonScope();

I got following error:
Could not unbind serviceIdentifier: FileDownloadCommandContribution

Can anyone help?

@leogong99 thank you for the discussion, I just wanted to mention that it is possible to use the CommandRegistry.unregisterCommand API to unregister commands you do not want or require.


You cannot unbind a contribution, instead you can filter it out thanks to https://github.com/eclipse-theia/theia/pull/9317.

theia-no-download-frontend-module.ts:

import { bindContribution, FilterContribution } from '@theia/core/lib/common';
import { ContainerModule } from '@theia/core/shared/inversify';
import { TheiaNoDownloadFilterContribution } from './theia-no-download-contribution';

export default new ContainerModule(bind => {
    bind(TheiaNoDownloadFilterContribution).toSelf().inSingletonScope();
    bindContribution(bind, TheiaNoDownloadFilterContribution, [FilterContribution]);
});

theia-no-download-contribution.ts:

import { ContributionFilterRegistry, Filter, FilterContribution } from '@theia/core';
import { injectable } from '@theia/core/shared/inversify';
import { FileDownloadCommandContribution} from '@theia/filesystem/lib/browser/download/file-download-command-contribution';

@injectable()
export class TheiaNoDownloadFilterContribution implements FilterContribution {

    registerContributionFilters(registry: ContributionFilterRegistry): void {
        registry.addFilters('*', [
            filterClassName(name => name !== FileDownloadCommandContribution.name)
        ]);
    }
}

function filterClassName(filter: Filter<string>): Filter<Object> {
    return object => {
        const className = object?.constructor?.name;
        return className
            ? filter(className)
            : false;
    };
}

Thanks @vince-fugnitto.

I have try above suggestion. it completed remove download feature. My goal is still keep download feature but remove copy_down_link option.

Any more suggestion?

Thank you very much.

Leo

@leogong99 if I understand correctly you want to keep the feature but remove the context-menu item only? If that’s the case you can unregister the menu (https://eclipse-theia.github.io/theia/docs/next/classes/core.menumodelregistry-1.html#unregistermenuaction)

If I did not understand the use-case please let me know! :slight_smile:

Thanks again @vince-fugnitto.
There is “copy download link” commands at here https://github.com/eclipse-theia/theia/blob/0ad6d10e354/packages/filesystem/src/browser/download/file-download-command-contribution.ts#L46

I would like to remove this command.

Here is my code:
`

export class MYFileDownloadCommandContribution extends FileDownloadCommandContribution {
    registerCommands(commandRegistry: CommandRegistry): void {
        super.registerCommands(commandRegistry);
        // Remove File -> copy download link.
        commandRegistry.unregisterCommand(FileDownloadCommands.COPY_DOWNLOAD_LINK);
    }
}

// my module:

bind(MYFileDownloadCommandContribution).toSelf().inSingletonScope(); rebind(FileDownloadCommandContribution).to(MYFileDownloadCommandContribution).inSingletonScope();

`

Seems like FileDownloadCommandContribution was not initial load so rebind won’t find FileDownloadCommandContribution

Could not unbind serviceIdentifier: FileDownloadCommandContribution

@leogong99 as mentioned you cannot rebind a contribution, only filter it out. If you were to use the api to unregisterCommand it would look something like this:

theia-no-download-contribution.ts:

import { inject, injectable } from '@theia/core/shared/inversify';
import { CommandRegistry } from '@theia/core';
import { FrontendApplicationContribution } from '@theia/core/lib/browser/frontend-application';
import { FileDownloadCommands } from '@theia/filesystem/lib/browser/download/file-download-command-contribution';

@injectable()
export class TheiaNoDownloadContribution implements FrontendApplicationContribution {

    @inject(CommandRegistry)
    protected readonly commandRegistry: CommandRegistry;

    async onStart(): Promise<void> {
        this.commandRegistry.unregisterCommand(FileDownloadCommands.COPY_DOWNLOAD_LINK);
    }
}

theia-no-download-module.ts:

import { FrontendApplicationContribution } from '@theia/core/lib/browser';
import { ContainerModule } from '@theia/core/shared/inversify';
import { TheiaNoDownloadContribution } from './theia-unregister-contribution';

export default new ContainerModule(bind => {
    bind(TheiaNoDownloadContribution).toSelf().inSingletonScope();
    bind(FrontendApplicationContribution).to(TheiaNoDownloadContribution);
});

Thank you very much. Really appreciate your help.

1 Like