UNPKG

@theia/monaco

Version:
115 lines 5.31 kB
"use strict"; // ***************************************************************************** // Copyright (C) 2022 Ericsson 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 // ***************************************************************************** Object.defineProperty(exports, "__esModule", { value: true }); exports.MonacoMarkdownRenderer = void 0; const tslib_1 = require("tslib"); const inversify_1 = require("@theia/core/shared/inversify"); const language_1 = require("@theia/monaco-editor-core/esm/vs/editor/common/languages/language"); const markdownRenderer_1 = require("@theia/monaco-editor-core/esm/vs/editor/browser/widget/markdownRenderer/browser/markdownRenderer"); const standaloneServices_1 = require("@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices"); const browser_1 = require("@theia/core/lib/browser"); const opener_1 = require("@theia/monaco-editor-core/esm/vs/platform/opener/common/opener"); const uri_1 = require("@theia/core/lib/common/uri"); const lifecycle_1 = require("@theia/monaco-editor-core/esm/vs/base/common/lifecycle"); const core_1 = require("@theia/core"); let MonacoMarkdownRenderer = class MonacoMarkdownRenderer { render(markdown, options, markedOptions) { return this.delegate.render(markdown, this.transformOptions(options), markedOptions); } transformOptions(options) { if (!(options === null || options === void 0 ? void 0 : options.actionHandler)) { return options; } const monacoActionHandler = { disposables: this.toDisposableStore(options.actionHandler.disposables), callback: (content, e) => options.actionHandler.callback(content, e === null || e === void 0 ? void 0 : e.browserEvent) }; return { ...options, actionHandler: monacoActionHandler }; } toDisposableStore(current) { if (current instanceof lifecycle_1.DisposableStore) { return current; } else if (current instanceof core_1.DisposableCollection) { const store = new lifecycle_1.DisposableStore(); current['disposables'].forEach(disposable => store.add(disposable)); return store; } else { return new lifecycle_1.DisposableStore(); } } init() { const languages = standaloneServices_1.StandaloneServices.get(language_1.ILanguageService); const openerService = standaloneServices_1.StandaloneServices.get(opener_1.IOpenerService); openerService.registerOpener({ open: (u, options) => this.interceptOpen(u, options) }); const that = this; const prefs = new class { get codeBlockFontFamily() { return that.preferences.get('editor.fontFamily'); } }; this.delegate = new markdownRenderer_1.MarkdownRenderer(prefs, languages, openerService); } async interceptOpen(monacoUri, monacoOptions) { let options = undefined; if (monacoOptions) { if ('openToSide' in monacoOptions && monacoOptions.openToSide) { options = Object.assign(options || {}, { widgetOptions: { mode: 'split-right' } }); } if ('openExternal' in monacoOptions && monacoOptions.openExternal) { options = Object.assign(options || {}, { openExternal: true }); } } const uri = new uri_1.URI(monacoUri.toString()); try { await (0, browser_1.open)(this.openerService, uri, options); return true; } catch (e) { console.error(`Fail to open '${uri.toString()}':`, e); return false; } } }; exports.MonacoMarkdownRenderer = MonacoMarkdownRenderer; tslib_1.__decorate([ (0, inversify_1.inject)(browser_1.OpenerService), tslib_1.__metadata("design:type", Object) ], MonacoMarkdownRenderer.prototype, "openerService", void 0); tslib_1.__decorate([ (0, inversify_1.inject)(browser_1.PreferenceService), tslib_1.__metadata("design:type", Object) ], MonacoMarkdownRenderer.prototype, "preferences", void 0); tslib_1.__decorate([ (0, inversify_1.postConstruct)(), tslib_1.__metadata("design:type", Function), tslib_1.__metadata("design:paramtypes", []), tslib_1.__metadata("design:returntype", void 0) ], MonacoMarkdownRenderer.prototype, "init", null); exports.MonacoMarkdownRenderer = MonacoMarkdownRenderer = tslib_1.__decorate([ (0, inversify_1.injectable)() ], MonacoMarkdownRenderer); //# sourceMappingURL=monaco-markdown-renderer.js.map