diff --git a/packages/sprotty/src/base/commands/command-stack-options.ts b/packages/sprotty/src/base/commands/command-stack-options.ts index 5fb8727..bf43b6a 100644 --- a/packages/sprotty/src/base/commands/command-stack-options.ts +++ b/packages/sprotty/src/base/commands/command-stack-options.ts @@ -14,8 +14,8 @@ * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ -import { Container } from "inversify"; -import { safeAssign } from "sprotty-protocol/lib/utils/object"; +import { Container, interfaces } from 'inversify'; +import { safeAssign } from 'sprotty-protocol/lib/utils/object'; import { TYPES } from '../types'; /** @@ -37,6 +37,31 @@ export interface CommandStackOptions { undoHistoryLimit: number } +export const defaultCommandStackOptions: () => CommandStackOptions = () => ({ + defaultDuration: 250, + undoHistoryLimit: 50 +}); + +/** + * Utility function to partially set command stack options. Default values (from `defaultViewerOptions`) are used for + * options that are not specified. + */ +export function configureCommandStackOptions(context: { bind: interfaces.Bind, isBound: interfaces.IsBound, rebind: interfaces.Rebind }, + options: Partial): void { + const opt: CommandStackOptions = { + ...defaultCommandStackOptions(), + ...options + }; + if (context.isBound(TYPES.CommandStackOptions)) { + context.rebind(TYPES.CommandStackOptions).toConstantValue(opt); + } else { + context.bind(TYPES.CommandStackOptions).toConstantValue(opt); + } +} + +/** + * Utility function to partially override the currently configured command stack options in a DI container. + */ export function overrideCommandStackOptions(container: Container, options: Partial): CommandStackOptions { const defaultOptions = container.get(TYPES.CommandStackOptions); safeAssign(defaultOptions, options); diff --git a/packages/sprotty/src/base/di.config.ts b/packages/sprotty/src/base/di.config.ts index c304a15..0f8f436 100644 --- a/packages/sprotty/src/base/di.config.ts +++ b/packages/sprotty/src/base/di.config.ts @@ -21,7 +21,7 @@ import { LogLevel, NullLogger } from "../utils/logging"; import { ActionDispatcher, IActionDispatcher } from "./actions/action-dispatcher"; import { ActionHandlerRegistry } from "./actions/action-handler"; import { CommandStack, ICommandStack } from "./commands/command-stack"; -import { CommandStackOptions } from "./commands/command-stack-options"; +import { CommandStackOptions, defaultCommandStackOptions } from "./commands/command-stack-options"; import { SModelFactory, SModelRegistry } from './model/smodel-factory'; import { AnimationFrameSyncer } from "./animations/animation-frame-syncer"; import { IViewer, ModelViewer, HiddenModelViewer, PopupModelViewer, ModelRenderer, PatcherProvider } from "./views/viewer"; @@ -82,10 +82,7 @@ const defaultContainerModule = new ContainerModule((bind, _unbind, isBound) => { }); }; }); - bind(TYPES.CommandStackOptions).toConstantValue({ - defaultDuration: 250, - undoHistoryLimit: 50 - }); + bind(TYPES.CommandStackOptions).toConstantValue(defaultCommandStackOptions()); // Viewer --------------------------------------------- bind(ModelViewer).toSelf().inSingletonScope(); diff --git a/packages/sprotty/src/base/views/viewer-options.ts b/packages/sprotty/src/base/views/viewer-options.ts index c36c74a..62e588f 100644 --- a/packages/sprotty/src/base/views/viewer-options.ts +++ b/packages/sprotty/src/base/views/viewer-options.ts @@ -77,10 +77,11 @@ export function configureViewerOptions(context: { bind: interfaces.Bind, isBound ...defaultViewerOptions(), ...options }; - if (context.isBound(TYPES.ViewerOptions)) + if (context.isBound(TYPES.ViewerOptions)) { context.rebind(TYPES.ViewerOptions).toConstantValue(opt); - else + } else { context.bind(TYPES.ViewerOptions).toConstantValue(opt); + } } /**