|
13 | 13 | * |
14 | 14 | * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
15 | 15 | ********************************************************************************/ |
16 | | -import { LayoutEngine, Logger, LoggerFactory, ModelState, NullLogger } from '@eclipse-glsp/server'; |
| 16 | +import { |
| 17 | + BindingTarget, |
| 18 | + GLSPModule, |
| 19 | + LayoutEngine, |
| 20 | + Logger, |
| 21 | + LoggerFactory, |
| 22 | + ModelState, |
| 23 | + NullLogger, |
| 24 | + applyBindingTarget |
| 25 | +} from '@eclipse-glsp/server'; |
17 | 26 | import ElkConstructor, { LayoutOptions } from 'elkjs/lib/elk.bundled'; |
18 | | -import { ContainerModule } from 'inversify'; |
| 27 | +import { ContainerModule, injectable, interfaces } from 'inversify'; |
19 | 28 | import { DefaultElementFilter, ElementFilter } from './element-filter'; |
20 | 29 | import { ElkFactory, GlspElkLayoutEngine } from './glsp-elk-layout-engine'; |
21 | 30 | import { FallbackLayoutConfigurator, LayoutConfigurator } from './layout-configurator'; |
22 | 31 |
|
23 | 32 | type Constructor<T> = new (...args: any[]) => T; |
24 | 33 |
|
25 | 34 | /** |
26 | | - * Configuration options for the {@link configureELKLayoutModule} function. |
| 35 | + * Configuration options for the {@link ElkLayoutModule} (and the legacy {@link configureELKLayoutModule} factory). |
27 | 36 | */ |
28 | 37 | export interface ElkModuleOptions { |
29 | 38 | /** |
@@ -53,71 +62,90 @@ export interface ElkModuleOptions { |
53 | 62 | } |
54 | 63 |
|
55 | 64 | /** |
56 | | - * Utility method to create a DI module that provides all necessary bindings to use the {@link GlspElkLayoutEngine} in a node GLSP server |
57 | | - * implementation. A set of configuration options is provided to enable easy customization. In most cases at least |
58 | | - * the custom {@link layoutConfigurator} binding should be provided (in addition to the required `algorithms' property) via these options. |
| 65 | + * DI module that provides the bindings needed to use the {@link GlspElkLayoutEngine} in a node GLSP server. |
59 | 66 | * |
60 | | - * The constructed module is not intended for standalone use cases and only works in combination with a GLSPDiagramModule. |
| 67 | + * Subclass and override the `bindXxx()` hooks to customize individual bindings — e.g. to swap the |
| 68 | + * layout configurator or element filter without re-implementing the whole module. The module is |
| 69 | + * only meaningful in combination with a `GLSPDiagramModule`. |
61 | 70 | * |
62 | | - * * The following bindings are provided: |
63 | | - * - {@link ILayoutConfigurator} |
64 | | - * - {@link IElementFilter} |
65 | | - * - {@link LayoutEngine} |
| 71 | + * Bindings provided: |
| 72 | + * - {@link ElementFilter} |
| 73 | + * - {@link LayoutConfigurator} |
66 | 74 | * - {@link ElkFactory} |
67 | | - * |
68 | | - * @param options The configuration options |
69 | | - * @returns A DI module that can be loaded as additional module when configuring a diagram module for a GLSP server. |
| 75 | + * - {@link GlspElkLayoutEngine} + {@link LayoutEngine} (toService) |
| 76 | + * - Fallback bindings for {@link Logger} and {@link LoggerFactory} if absent. |
70 | 77 | */ |
71 | | -export function configureELKLayoutModule(options: ElkModuleOptions): ContainerModule { |
72 | | - return new ContainerModule((bind, unbind, isBound, rebind) => { |
73 | | - if (options.elementFilter) { |
74 | | - bind(ElementFilter).to(options.elementFilter).inSingletonScope(); |
75 | | - } else { |
76 | | - bind(ElementFilter).to(DefaultElementFilter).inSingletonScope(); |
77 | | - } |
| 78 | +@injectable() |
| 79 | +export class ElkLayoutModule extends GLSPModule { |
| 80 | + constructor(protected readonly options: ElkModuleOptions) { |
| 81 | + super(); |
| 82 | + } |
78 | 83 |
|
79 | | - if (options.layoutConfigurator) { |
80 | | - bind(LayoutConfigurator).to(options.layoutConfigurator); |
81 | | - } else { |
82 | | - bind(LayoutConfigurator) |
83 | | - .toDynamicValue(context => new FallbackLayoutConfigurator(options.algorithms, options.defaultLayoutOptions)) |
84 | | - .inSingletonScope(); |
| 84 | + protected configure(bind: interfaces.Bind, unbind: interfaces.Unbind, isBound: interfaces.IsBound, rebind: interfaces.Rebind): void { |
| 85 | + const context = { bind, unbind, isBound, rebind }; |
| 86 | + applyBindingTarget(context, ElementFilter, this.bindElementFilter()).inSingletonScope(); |
| 87 | + applyBindingTarget(context, LayoutConfigurator, this.bindLayoutConfigurator()).inSingletonScope(); |
| 88 | + applyBindingTarget(context, ElkFactory, this.bindElkFactory()); |
| 89 | + applyBindingTarget(context, GlspElkLayoutEngine, this.bindGlspElkLayoutEngine()).inSingletonScope(); |
| 90 | + bind(LayoutEngine).toService(GlspElkLayoutEngine); |
| 91 | + this.bindLoggerFallbacks(bind, isBound); |
| 92 | + } |
| 93 | + |
| 94 | + protected bindElementFilter(): BindingTarget<ElementFilter> { |
| 95 | + return this.options.elementFilter ?? DefaultElementFilter; |
| 96 | + } |
| 97 | + |
| 98 | + protected bindLayoutConfigurator(): BindingTarget<LayoutConfigurator> { |
| 99 | + if (this.options.layoutConfigurator) { |
| 100 | + return this.options.layoutConfigurator; |
85 | 101 | } |
| 102 | + return { dynamicValue: () => new FallbackLayoutConfigurator(this.options.algorithms, this.options.defaultLayoutOptions) }; |
| 103 | + } |
86 | 104 |
|
87 | | - const elkFactory: ElkFactory = () => |
| 105 | + protected bindElkFactory(): BindingTarget<ElkFactory> { |
| 106 | + const { algorithms, defaultLayoutOptions, isWebWorker } = this.options; |
| 107 | + const factory: ElkFactory = () => |
88 | 108 | new ElkConstructor({ |
89 | | - algorithms: options.algorithms, |
90 | | - defaultLayoutOptions: options.defaultLayoutOptions, |
91 | | - // The node implementation relied on elkjs' `FakeWorker` to set the `workerFactory`. |
92 | | - // However, since the required file is dynamically loaded and not available in a web-worker context, |
93 | | - // it needs to be mocked manually. |
94 | | - workerFactory: options.isWebWorker ? () => ({ postMessage: () => {} }) as unknown as Worker : undefined |
| 109 | + algorithms, |
| 110 | + defaultLayoutOptions, |
| 111 | + // The node implementation relies on elkjs' `FakeWorker` to set the `workerFactory`. The required file is |
| 112 | + // dynamically loaded and not available in a web-worker context, so it has to be mocked manually there. |
| 113 | + workerFactory: isWebWorker ? () => ({ postMessage: () => {} }) as unknown as Worker : undefined |
95 | 114 | }); |
| 115 | + return { constantValue: factory }; |
| 116 | + } |
96 | 117 |
|
97 | | - bind(ElkFactory).toConstantValue(elkFactory); |
98 | | - |
99 | | - bind(GlspElkLayoutEngine) |
100 | | - .toDynamicValue(context => { |
101 | | - const container = context.container; |
102 | | - const factory = container.get<ElkFactory>(ElkFactory); |
103 | | - const filter = container.get<ElementFilter>(ElementFilter); |
104 | | - const configurator = container.get<LayoutConfigurator>(LayoutConfigurator); |
105 | | - const modelState = container.get<ModelState>(ModelState); |
| 118 | + protected bindGlspElkLayoutEngine(): BindingTarget<GlspElkLayoutEngine> { |
| 119 | + return { |
| 120 | + dynamicValue: ctx => { |
| 121 | + const factory = ctx.container.get<ElkFactory>(ElkFactory); |
| 122 | + const filter = ctx.container.get<ElementFilter>(ElementFilter); |
| 123 | + const configurator = ctx.container.get<LayoutConfigurator>(LayoutConfigurator); |
| 124 | + const modelState = ctx.container.get<ModelState>(ModelState); |
106 | 125 | return new GlspElkLayoutEngine(factory, filter, configurator, modelState); |
107 | | - }) |
108 | | - .inSingletonScope(); |
109 | | - bind(LayoutEngine).toService(GlspElkLayoutEngine); |
| 126 | + } |
| 127 | + }; |
| 128 | + } |
110 | 129 |
|
| 130 | + /** Provide fallbacks so the module works standalone in tests/specs that don't preconfigure logging. */ |
| 131 | + protected bindLoggerFallbacks(bind: interfaces.Bind, isBound: interfaces.IsBound): void { |
111 | 132 | if (!isBound(Logger)) { |
112 | 133 | bind(Logger).to(NullLogger).inSingletonScope(); |
113 | 134 | } |
114 | | - |
115 | 135 | if (!isBound(LoggerFactory)) { |
116 | 136 | bind(LoggerFactory).toFactory(dynamicContext => (caller: string) => { |
117 | 137 | const logger = dynamicContext.container.get(Logger); |
118 | 138 | logger.caller = caller; |
119 | 139 | return logger; |
120 | 140 | }); |
121 | 141 | } |
122 | | - }); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Utility wrapper around {@link ElkLayoutModule} for the common case where no override is needed. |
| 147 | + * Prefer subclassing {@link ElkLayoutModule} when individual bindings need to be customized. |
| 148 | + */ |
| 149 | +export function configureELKLayoutModule(options: ElkModuleOptions): ContainerModule { |
| 150 | + return new ElkLayoutModule(options); |
123 | 151 | } |
0 commit comments