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

feat: ui plugin support override dependencies #2125

Merged
merged 1 commit into from
Apr 29, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { createIdentifier } from '@wendellhu/redi';
import { describe, expect, it } from 'vitest';
import { mergeOverrideWithDependencies } from '../plugin-override';

describe('test dependency override', () => {
it('should override dependencies', () => {
class A {}
interface IA {}
const IA = createIdentifier<IA>('IA');

expect(mergeOverrideWithDependencies([[A]], [[A, null]]))
.toEqual([]);

expect(mergeOverrideWithDependencies([[IA, { useClass: A }]], [[IA, null]]))
.toEqual([]);

expect(mergeOverrideWithDependencies([[A], [IA, { useClass: A }]], [[IA, { useValue: {} }]]))
.toEqual([[A], [IA, { useValue: {} }]]);
});
});
22 changes: 7 additions & 15 deletions packages/core/src/services/plugin/plugin-override.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,25 @@
* limitations under the License.
*/

/* eslint-disable ts/no-explicit-any */
import type { Dependency, DependencyIdentifier, DependencyItem } from '@wendellhu/redi';

import type { Dependency, DependencyItem, IdentifierDecorator } from '@wendellhu/redi';
export type NullableDependencyPair<T> = [DependencyIdentifier<T>, DependencyItem<T> | null];

/**
* Overrides the dependencies definited in the plugin. Only dependencies that are identified by `IdentifierDecorator` can be overridden.
* Overrides the dependencies defined in the plugin. Only dependencies that are identified by `IdentifierDecorator` can be overridden.
* If you override a dependency with `null`, the original dependency will be removed.
*/
export type DependencyOverride = [identifier: IdentifierDecorator<any>, DependencyItem<any> | null][];
// eslint-disable-next-line ts/no-explicit-any
export type DependencyOverride = NullableDependencyPair<any>[];

export function mergeOverrideWithDependencies(dependencies: Dependency[], override?: DependencyOverride): Dependency[] {
if (!override) {
return dependencies;
}
if (!override) return dependencies;

const result: Dependency[] = [];
for (const dependency of dependencies) {
if (dependency.length === 1) {
result.push(dependency);
continue;
}

const overrideItem = override.find(([identifier]) => identifier === dependency[0]);
if (overrideItem) {
if (overrideItem[1] === null) {
continue;
}
if (overrideItem[1] === null) continue;
result.push([dependency[0], overrideItem[1]]);
} else {
result.push(dependency);
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
IContextMenuService,
} from './services/contextmenu/contextmenu.service';
export { DesktopDialogService } from './services/dialog/desktop-dialog.service';
export { type IDialogPartMethodOptions } from './views/components/dialog-part/interface';
export { IDialogService } from './services/dialog/dialog.service';
export { ILayoutService, DesktopLayoutService } from './services/layout/layout.service';
export {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class DesktopDialogService extends Disposable implements IDialogService {
this._dialogOptions$.next([...this._dialogOptions]);
}

getObservableDialog() {
getDialogs$() {
return this._dialogOptions$;
}
}
4 changes: 2 additions & 2 deletions packages/ui/src/services/dialog/dialog.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

import type { IDisposable } from '@wendellhu/redi';
import { createIdentifier } from '@wendellhu/redi';
import type { Subject } from 'rxjs';
import type { Observable } from 'rxjs';

import type { IDialogPartMethodOptions } from '../../views/components/dialog-part/interface';

export const IDialogService = createIdentifier<IDialogService>('univer.ui.dialog-service');
export interface IDialogService {
open(params: IDialogPartMethodOptions): IDisposable;
close(id: string): void;
getObservableDialog(): Subject<IDialogPartMethodOptions[]>;
getDialogs$(): Observable<IDialogPartMethodOptions[]>;
}
11 changes: 7 additions & 4 deletions packages/ui/src/ui-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
* limitations under the License.
*/

import { IContextService, ILocalStorageService, LocaleService, Plugin } from '@univerjs/core';
import type { DependencyOverride } from '@univerjs/core';
import { IContextService, ILocalStorageService, LocaleService, mergeOverrideWithDependencies, Plugin } from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';

Expand Down Expand Up @@ -60,6 +61,8 @@ const PLUGIN_NAME = 'ui';
export interface IUniverUIConfig extends IWorkbenchOptions {
/** Disable auto focus when Univer bootstraps. */
disableAutoFocus?: true;

override?: DependencyOverride;
}

export const DISABLE_AUTO_FOCUS_KEY = 'DISABLE_AUTO_FOCUS';
Expand Down Expand Up @@ -92,8 +95,7 @@ export class UniverUIPlugin extends Plugin {
}

private _initDependencies(injector: Injector): void {
const dependencies: Dependency[] = [
// legacy managers - deprecated
const dependencies: Dependency[] = mergeOverrideWithDependencies([
[ComponentManager],
[ZIndexManager],

Expand All @@ -118,12 +120,13 @@ export class UniverUIPlugin extends Plugin {
[IRangeSelectorService, { useClass: RangeSelectorService }],
[ICanvasPopupService, { useClass: CanvasPopupService }],
[IProgressService, { useClass: ProgressService }],

// controllers
[IUIController, { useClass: DesktopUIController }],
[SharedController],
[ErrorController],
[ShortcutPanelController],
];
], this._config.override);

dependencies.forEach((dependency) => injector.add(dependency));
}
Expand Down
6 changes: 2 additions & 4 deletions packages/ui/src/views/components/dialog-part/DialogPart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@ export function DialogPart() {
const [dialogOptions, setDialogOptions] = useState<IDialogPartMethodOptions[]>([]);

useEffect(() => {
const dialog$ = dialogService.getObservableDialog();
const dialog$ = dialogService.getDialogs$();
const subscription = dialog$.subscribe((options: IDialogPartMethodOptions[]) => {
setDialogOptions(options);
});

return () => {
subscription.unsubscribe();
};
return () => subscription.unsubscribe();
}, [dialogService]);

const attrs = dialogOptions.map((options) => {
Expand Down
Loading
Loading