UNPKG

@theia/core

Version:

Theia is a cloud & desktop IDE framework implemented in TypeScript.

88 lines • 3.69 kB
// ***************************************************************************** // Copyright (C) 2017 TypeFox and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { __decorate, __param } from "tslib"; import { named, injectable, inject } from 'inversify'; import { ContributionProvider, Prioritizeable, Emitter, Disposable } from '../common/index.js'; import { match } from '../common/glob.js'; export const OpenHandler = Symbol('OpenHandler'); export const OpenerService = Symbol('OpenerService'); export async function open(openerService, uri, options) { const opener = await openerService.getOpener(uri, options); return opener.open(uri, options); } export function getDefaultHandler(uri, preferenceService) { const associations = preferenceService.get('workbench.editorAssociations', { fallback: {} }); const defaultHandler = Object.entries(associations).find(([key]) => match(key, uri.path.base))?.[1]; if (typeof defaultHandler === 'string') { return defaultHandler; } return undefined; } export const defaultHandlerPriority = 100_000; let DefaultOpenerService = class DefaultOpenerService { constructor(handlersProvider) { this.handlersProvider = handlersProvider; // Collection of open-handlers for custom-editor contributions. this.customEditorOpenHandlers = []; this.onDidChangeOpenersEmitter = new Emitter(); this.onDidChangeOpeners = this.onDidChangeOpenersEmitter.event; } addHandler(openHandler) { this.customEditorOpenHandlers.push(openHandler); this.onDidChangeOpenersEmitter.fire(); return Disposable.create(() => { this.removeHandler(openHandler); }); } removeHandler(openHandler) { this.customEditorOpenHandlers.splice(this.customEditorOpenHandlers.indexOf(openHandler), 1); this.onDidChangeOpenersEmitter.fire(); } async getOpener(uri, options) { const handlers = await this.prioritize(uri, options); if (handlers.length >= 1) { return handlers[0]; } return Promise.reject(new Error(`There is no opener for ${uri}.`)); } async getOpeners(uri, options) { return uri ? this.prioritize(uri, options) : this.getHandlers(); } async prioritize(uri, options) { const prioritized = await Prioritizeable.prioritizeAll(this.getHandlers(), async (handler) => { try { return await handler.canHandle(uri, options); } catch { return 0; } }); return prioritized.map(p => p.value); } getHandlers() { return [ ...this.handlersProvider.getContributions(), ...this.customEditorOpenHandlers ]; } }; DefaultOpenerService = __decorate([ injectable(), __param(0, inject(ContributionProvider)), __param(0, named(OpenHandler)) ], DefaultOpenerService); export { DefaultOpenerService }; //# sourceMappingURL=opener-service.js.map