UNPKG

@eclipse-glsp/layout-elk

Version:

Integration of ELK graph layout algorithms in GLSP Node Server

89 lines 4.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.configureELKLayoutModule = configureELKLayoutModule; /******************************************************************************** * Copyright (c) 2022-2025 STMicroelectronics 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 WITH Classpath-exception-2.0 ********************************************************************************/ const server_1 = require("@eclipse-glsp/server"); const elk_bundled_1 = require("elkjs/lib/elk.bundled"); const inversify_1 = require("inversify"); const element_filter_1 = require("./element-filter"); const glsp_elk_layout_engine_1 = require("./glsp-elk-layout-engine"); const layout_configurator_1 = require("./layout-configurator"); /** * Utility method to create a DI module that provides all necessary bindings to use the {@link GlspElkLayoutEngine} in a node GLSP server * implementation. A set of configuration options is provided to enable easy customization. In most cases at least * the custom {@link layoutConfigurator} binding should be provided (in addition to the required `algorithms' property) via these options. * * The constructed module is not intended for standalone use cases and only works in combination with a GLSPDiagramModule. * * * The following bindings are provided: * - {@link ILayoutConfigurator} * - {@link IElementFilter} * - {@link LayoutEngine} * - {@link ElkFactory} * * @param options The configuration options * @returns A DI module that can be loaded as additional module when configuring a diagram module for a GLSP server. */ function configureELKLayoutModule(options) { return new inversify_1.ContainerModule((bind, unbind, isBound, rebind) => { if (options.elementFilter) { bind(element_filter_1.ElementFilter).to(options.elementFilter).inSingletonScope(); } else { bind(element_filter_1.ElementFilter).to(element_filter_1.DefaultElementFilter).inSingletonScope(); } if (options.layoutConfigurator) { bind(layout_configurator_1.LayoutConfigurator).to(options.layoutConfigurator); } else { bind(layout_configurator_1.LayoutConfigurator) .toDynamicValue(context => new layout_configurator_1.FallbackLayoutConfigurator(options.algorithms, options.defaultLayoutOptions)) .inSingletonScope(); } const elkFactory = () => new elk_bundled_1.default({ algorithms: options.algorithms, defaultLayoutOptions: options.defaultLayoutOptions, // The node implementation relied on elkjs' `FakeWorker` to set the `workerFactory`. // However, since the required file is dynamically loaded and not available in a web-worker context, // it needs to be mocked manually. workerFactory: options.isWebWorker ? () => ({ postMessage: () => { } }) : undefined }); bind(glsp_elk_layout_engine_1.ElkFactory).toConstantValue(elkFactory); bind(glsp_elk_layout_engine_1.GlspElkLayoutEngine) .toDynamicValue(context => { const container = context.container; const factory = container.get(glsp_elk_layout_engine_1.ElkFactory); const filter = container.get(element_filter_1.ElementFilter); const configurator = container.get(layout_configurator_1.LayoutConfigurator); const modelState = container.get(server_1.ModelState); return new glsp_elk_layout_engine_1.GlspElkLayoutEngine(factory, filter, configurator, modelState); }) .inSingletonScope(); bind(server_1.LayoutEngine).toService(glsp_elk_layout_engine_1.GlspElkLayoutEngine); if (!isBound(server_1.Logger)) { bind(server_1.Logger).to(server_1.NullLogger).inSingletonScope(); } if (!isBound(server_1.LoggerFactory)) { bind(server_1.LoggerFactory).toFactory(dynamicContext => (caller) => { const logger = dynamicContext.container.get(server_1.Logger); logger.caller = caller; return logger; }); } }); } //# sourceMappingURL=di.config.js.map