Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added configureCommandStackOptions utility #433

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading