Skip to content

Commit

Permalink
Added configureCommandStackOptions utility
Browse files Browse the repository at this point in the history
  • Loading branch information
spoenemann committed Feb 21, 2024
1 parent a0bbb68 commit 97c9772
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
29 changes: 27 additions & 2 deletions packages/sprotty/src/base/commands/command-stack-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand All @@ -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<CommandStackOptions>): 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>): CommandStackOptions {
const defaultOptions = container.get<CommandStackOptions>(TYPES.CommandStackOptions);
safeAssign(defaultOptions, options);
Expand Down
7 changes: 2 additions & 5 deletions packages/sprotty/src/base/di.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -82,10 +82,7 @@ const defaultContainerModule = new ContainerModule((bind, _unbind, isBound) => {
});
};
});
bind<CommandStackOptions>(TYPES.CommandStackOptions).toConstantValue({
defaultDuration: 250,
undoHistoryLimit: 50
});
bind<CommandStackOptions>(TYPES.CommandStackOptions).toConstantValue(defaultCommandStackOptions());

// Viewer ---------------------------------------------
bind(ModelViewer).toSelf().inSingletonScope();
Expand Down
5 changes: 3 additions & 2 deletions packages/sprotty/src/base/views/viewer-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand Down

0 comments on commit 97c9772

Please sign in to comment.