Skip to content

Commit

Permalink
feat: add api for ui and f-enum (#4406)
Browse files Browse the repository at this point in the history
  • Loading branch information
weird94 authored Jan 2, 2025
1 parent 52990cc commit 51ed14e
Show file tree
Hide file tree
Showing 11 changed files with 171 additions and 29 deletions.
35 changes: 27 additions & 8 deletions packages/core/src/facade/f-enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,31 @@ export class FEnum extends FBase {
return instance;
}

readonly UniverInstanceType = UniverInstanceType;
readonly LifecycleStages = LifecycleStages;

readonly DataValidationType = DataValidationType;
readonly DataValidationErrorStyle = DataValidationErrorStyle;
readonly DataValidationRenderMode = DataValidationRenderMode;
readonly DataValidationOperator = DataValidationOperator;
readonly DataValidationStatus = DataValidationStatus;
get UniverInstanceType() {
return UniverInstanceType;
}

get LifecycleStages() {
return LifecycleStages;
}

get DataValidationType() {
return DataValidationType;
}

get DataValidationErrorStyle() {
return DataValidationErrorStyle;
}

get DataValidationRenderMode() {
return DataValidationRenderMode;
}

get DataValidationOperator() {
return DataValidationOperator;
}

get DataValidationStatus() {
return DataValidationStatus;
}
}
9 changes: 7 additions & 2 deletions packages/core/src/facade/f-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ export class FEventName extends FBase {
return instance;
}

UnitCreated = 'UnitCreated' as const;
LifeCycleChanged = 'LifeCycleChanged' as const;
get UnitCreated() {
return 'UnitCreated' as const;
}

get LifeCycleChanged() {
return 'LifeCycleChanged' as const;
}
}

export interface IEventParamConfig {
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/facade/f-univer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { ICommandService } from '../services/command/command.service';
import { IUniverInstanceService } from '../services/instance/instance.service';
import { LifecycleService } from '../services/lifecycle/lifecycle.service';
import { RedoCommand, UndoCommand } from '../services/undoredo/undoredo.service';
import { toDisposable } from '../shared';
import { ColorBuilder, toDisposable } from '../shared';
import { Univer } from '../univer';
import { FBaseInitialable } from './f-base';
import { FBlob } from './f-blob';
Expand Down Expand Up @@ -190,6 +190,10 @@ export class FUniver extends FBaseInitialable {
return this._injector.createInstance(FBlob);
}

newColor(): ColorBuilder {
return new ColorBuilder();
}

get Enum() {
return FEnum.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import { ColorBuilder } from '@univerjs/core';
import { FWorkbook } from '@univerjs/sheets/facade';

export interface IFWorkbookConditionalFormattingMixin {
/**
* @deprecated use `univerAPI.newColor()` as instead.
*/
newColor(): ColorBuilder;
}

Expand Down
8 changes: 4 additions & 4 deletions packages/sheets/src/facade/f-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import type { FWorksheet } from './f-worksheet';
import { FEventName } from '@univerjs/core';

export interface IFSheetEventMixin {
SheetCreated: 'SheetCreated';
BeforeSheetCreate: 'BeforeSheetCreate';
get SheetCreated(): 'SheetCreated' ;
get BeforeSheetCreate(): 'BeforeSheetCreate';
}

export class FSheetEventName extends FEventName implements IFSheetEventMixin {
override readonly SheetCreated = 'SheetCreated' as const;
override readonly BeforeSheetCreate = 'BeforeSheetCreate' as const;
override get SheetCreated(): 'SheetCreated' { return 'SheetCreated' as const; }
override get BeforeSheetCreate(): 'BeforeSheetCreate' { return 'BeforeSheetCreate' as const; }
}

export interface IBeforeSheetCreateEventParams extends IEventBase {
Expand Down
31 changes: 25 additions & 6 deletions packages/sheets/src/facade/f-univer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,23 @@ export interface IFUniverSheetsMixin {
*
* @param {Partial<IWorkbookData>} data The snapshot of the spreadsheet.
* @returns {FWorkbook} FWorkbook API instance.
* @example
* ```ts
* univerAPI.createUniverSheet({ id: 'Sheet1', name: 'Sheet1' });
* ```
*/
createUniverSheet(data: Partial<IWorkbookData>): FWorkbook;
/**
* Get the currently focused Univer spreadsheet.
*
* @returns {FWorkbook | null} The currently focused Univer spreadsheet.
* @example
* ```ts
* univerAPI.getActiveUniverSheet();
* ```
*/
getActiveUniverSheet(): FWorkbook | null;
/**
* @deprecated use `getActiveUniverSheet` as instead.
*/
getActiveWorkbook(): FWorkbook | null;
/**
Expand All @@ -46,19 +57,23 @@ export interface IFUniverSheetsMixin {
getUniverSheet(id: string): FWorkbook | null;
/**
* Get the PermissionInstance.
* @deprecated This function is deprecated and will be removed in version 0.6.0.
* Please use the function with the same name on the `FWorkbook` instance instead.
* @returns {FPermission} - The PermissionInstance.
* @deprecated This function is deprecated and will be removed in version 0.6.0. Please use the function with the same name on the `FWorkbook` instance instead.
*/
getPermission(): FPermission;

/**
* Register a callback that will be triggered when a Univer Sheet is created.
* @deprecated Use `univerAPI.addEvent(univerAPI.Event.UnitCreated, () => {})`
*/
onUniverSheetCreated(callback: (workbook: FWorkbook) => void): IDisposable;

/**
* Create a new defined name builder.
* @returns {FDefinedNameBuilder} - The defined name builder.
*
* @example
* ```ts
* univerAPI.newDefinedName();
* ```
*/
newDefinedName(): FDefinedNameBuilder;
}
Expand Down Expand Up @@ -136,7 +151,7 @@ export class FUniverSheetsMixin extends FUniver implements IFUniverSheetsMixin {
return this._injector.createInstance(FWorkbook, workbook);
};

override getActiveWorkbook(): FWorkbook | null {
override getActiveUniverSheet(): FWorkbook | null {
const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
if (!workbook) {
return null;
Expand All @@ -145,6 +160,10 @@ export class FUniverSheetsMixin extends FUniver implements IFUniverSheetsMixin {
return this._injector.createInstance(FWorkbook, workbook);
}

override getActiveWorkbook(): FWorkbook | null {
return this.getActiveUniverSheet();
}

override getUniverSheet(id: string): FWorkbook | null {
const workbook = this._univerInstanceService.getUnit<Workbook>(id, UniverInstanceType.UNIVER_SHEET);
if (!workbook) {
Expand Down
35 changes: 35 additions & 0 deletions packages/ui/src/facade/f-enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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 { FEnum } from '@univerjs/core';
import { BuiltInUIPart } from '@univerjs/ui';

interface IFUIEnumMixin {
get BuiltInUIPart(): typeof BuiltInUIPart;
}

export class FUIEnum extends FEnum implements IFUIEnumMixin {
override get BuiltInUIPart(): typeof BuiltInUIPart {
return BuiltInUIPart;
};
}

FEnum.extend(FUIEnum);

declare module '@univerjs/core' {
// eslint-disable-next-line ts/naming-convention
interface FEnum extends IFUIEnumMixin {}
}
41 changes: 38 additions & 3 deletions packages/ui/src/facade/f-univer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

import type { IDisposable } from '@univerjs/core';
import type { IMessageProps } from '@univerjs/design';
import type { IDialogPartMethodOptions, ISidebarMethodOptions } from '@univerjs/ui';
import type { BuiltInUIPart, IDialogPartMethodOptions, ISidebarMethodOptions } from '@univerjs/ui';
import type { IFacadeMenuItem, IFacadeSubmenuItem } from './f-menu-builder';
import { FUniver } from '@univerjs/core';
import { ComponentManager, CopyCommand, IDialogService, IMessageService, ISidebarService, PasteCommand } from '@univerjs/ui';
import { ComponentManager, CopyCommand, IDialogService, IMessageService, ISidebarService, IUIPartsService, PasteCommand } from '@univerjs/ui';
import { FMenu, FSubmenu } from './f-menu-builder';
import { FShortcut } from './f-shortcut';

Expand Down Expand Up @@ -113,6 +113,29 @@ export interface IFUniverUIMixin {
* ```
*/
showMessage(options: IMessageProps): void;

/**
* Set the visibility of a built-in UI part.
* @param key the built-in UI part
* @param visible the visibility
* @returns the {@link FUniver} object
* example
* ```ts
* univerAPI.setUIVisible(BuiltInUIPart.HEADER, false);
* ```
*/
setUIVisible(key: BuiltInUIPart, visible: boolean): FUniver;

/**
* Get the visibility of a built-in UI part.
* @param key the built-in UI part
* @returns the visibility
* example
* ```ts
* univerAPI.isUIVisible(BuiltInUIPart.HEADER);
* ```
*/
isUIVisible(key: BuiltInUIPart): boolean;
}

export class FUniverUIMixin extends FUniver implements IFUniverUIMixin {
Expand Down Expand Up @@ -164,9 +187,21 @@ export class FUniverUIMixin extends FUniver implements IFUniverUIMixin {
return this._injector.get(ComponentManager);
}

override showMessage(options: IMessageProps): void {
override showMessage(options: IMessageProps): FUniver {
const messageService = this._injector.get(IMessageService);
messageService.show(options);
return this;
}

override setUIVisible(ui: BuiltInUIPart, visible: boolean): FUniver {
const uiPartService = this._injector.get(IUIPartsService);
uiPartService.setUIVisible(ui, visible);
return this;
}

override isUIVisible(ui: BuiltInUIPart): boolean {
const uiPartService = this._injector.get(IUIPartsService);
return uiPartService.isUIVisible(ui);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/facade/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import './f-univer';
import './f-hooks';
import './f-menu-builder';
import './f-enum';

export type * from './f-hooks';
export type * from './f-menu-builder';
Expand Down
19 changes: 18 additions & 1 deletion packages/ui/src/services/parts/parts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import type { ComponentType } from '../../common/component-manager';
import { createIdentifier, Disposable, toDisposable } from '@univerjs/core';
import { type Observable, Subject } from 'rxjs';

type ComponentRenderer = () => ComponentType;
export type ComponentRenderer = () => ComponentType;
type ComponentPartKey = BuiltInUIPart | string;

export enum BuiltInUIPart {
Expand All @@ -36,9 +36,14 @@ export enum BuiltInUIPart {

export interface IUIPartsService {
componentRegistered$: Observable<ComponentPartKey>;
uiVisibleChange$: Observable<{ ui: ComponentPartKey; visible: boolean }>;

registerComponent(part: ComponentPartKey, componentFactory: () => ComponentType): IDisposable;
getComponents(part: ComponentPartKey): Set<ComponentRenderer>;

setUIVisible(part: ComponentPartKey, visible: boolean): void;

isUIVisible(part: ComponentPartKey): boolean;
}

export const IUIPartsService = createIdentifier<IUIPartsService>('ui.parts.service');
Expand All @@ -48,13 +53,25 @@ export class UIPartsService extends Disposable implements IUIPartsService {

private readonly _componentRegistered$ = new Subject<ComponentPartKey>();
readonly componentRegistered$ = this._componentRegistered$.asObservable();
private readonly _uiVisible = new Map<ComponentPartKey, boolean>();
private readonly _uiVisibleChange$ = new Subject<{ ui: ComponentPartKey; visible: boolean }>();
readonly uiVisibleChange$ = this._uiVisibleChange$.asObservable();

override dispose(): void {
super.dispose();

this._componentRegistered$.complete();
}

setUIVisible(part: ComponentPartKey, visible: boolean): void {
this._uiVisible.set(part, visible);
this._uiVisibleChange$.next({ ui: part, visible });
}

isUIVisible(part: ComponentPartKey): boolean {
return this._uiVisible.get(part) ?? true;
}

registerComponent(part: ComponentPartKey, componentFactory: () => React.ComponentType): IDisposable {
const componentType = componentFactory();
const components = (
Expand Down
12 changes: 8 additions & 4 deletions packages/ui/src/views/components/ComponentContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
* limitations under the License.
*/

import { useDependency } from '@univerjs/core';
import type { Injector } from '@univerjs/core';
import type { ComponentType } from 'react';
import type { ComponentRenderer } from '../../services/parts/parts.service';
import { useDependency } from '@univerjs/core';
import React, { useMemo, useRef } from 'react';
import { debounceTime, filter, map, startWith } from 'rxjs';
import type { Injector } from '@univerjs/core';
import { useObservable } from '../../components/hooks/observable';
import { IUIPartsService } from '../../services/parts/parts.service';

Expand All @@ -44,8 +45,11 @@ export function ComponentContainer(props: IComponentContainerProps) {
* @param injector The injector to get the service. It is optional. However, you should not change this prop in a given
* component.
*/
export function useComponentsOfPart(part: string, injector?: Injector) {
// eslint-disable-next-line react-refresh/only-export-components
export function useComponentsOfPart(part: string, injector?: Injector): Set<ComponentRenderer> {
const uiPartsService = injector?.get(IUIPartsService) ?? useDependency(IUIPartsService);
const uiVisibleChange$ = useMemo(() => uiPartsService.uiVisibleChange$.pipe(filter((ui) => ui.ui === part)), [part, uiPartsService]);
useObservable(uiVisibleChange$);

const updateCounterRef = useRef<number>(0);
const componentPartUpdateCount = useObservable(
Expand All @@ -61,5 +65,5 @@ export function useComponentsOfPart(part: string, injector?: Injector) {
);

// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(() => uiPartsService.getComponents(part), [componentPartUpdateCount]);
return useMemo(() => uiPartsService.isUIVisible(part) ? uiPartsService.getComponents(part) : new Set(), [componentPartUpdateCount]);
}

0 comments on commit 51ed14e

Please sign in to comment.