Skip to content

Commit

Permalink
Merge branch 'dev' into feat/drawing-print
Browse files Browse the repository at this point in the history
  • Loading branch information
weird94 committed Jun 6, 2024
2 parents 6cded33 + 515e0ae commit 25da5f0
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 53 deletions.
28 changes: 28 additions & 0 deletions .vscode/redi.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
// Place your univer workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
// "Print to console": {
// "scope": "javascript,typescript",
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"Redi Injection Identifier": {
"prefix": ["@I"],
"body": ["@I${1:identifier} private readonly _${2:name}: I${1:identifier},"],
"description": "Inject an identifier with Redi."
},
"Redi Injection": {
"prefix": ["@In"],
"body": ["@Inject(${1:identifier}) private readonly _${2:name}: ${1:identifier},"],
"description": "Inject a class item with Redi."
}
}
1 change: 0 additions & 1 deletion packages/core/src/services/command/command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ export interface IOperation<P extends object = object, R = boolean> extends ICom
/**
* The command info, only a command id and responsible params
*/
// TODO: change object to serializable interface type
export interface ICommandInfo<T extends object = object> {
id: string;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,15 @@ export const SetProtectionCommand: ICommand<ISetProtectionParams> = {
if (oldRule) {
if (oldRule.unitType === UnitObject.Worksheet) {
redoMutations.push({ id: DeleteWorksheetProtectionMutation.id, params: { unitId, subUnitId } });
undoMutations.push({ id: AddWorksheetProtectionMutation.id, params: { unitId, rule: oldRule } });
undoMutations.push({ id: AddWorksheetProtectionMutation.id, params: { unitId, rule: oldRule, subUnitId: oldRule.subUnitId } });
} else if (oldRule.unitType === UnitObject.SelectRange) {
redoMutations.push({ id: DeleteRangeProtectionMutation.id, params: { unitId, subUnitId, ruleIds: [(oldRule as IRangeProtectionRule).id] } });
undoMutations.push({ id: AddRangeProtectionMutation.id, params: { unitId, subUnitId, rules: [oldRule] } });
}
}

if (rule.unitType === UnitObject.Worksheet) {
redoMutations.push({ id: AddWorksheetProtectionMutation.id, params: { unitId, rule } });
redoMutations.push({ id: AddWorksheetProtectionMutation.id, params: { unitId, rule, subUnitId: rule.subUnitId } });
undoMutations.unshift({ id: DeleteWorksheetProtectionMutation.id, params: { unitId, subUnitId } });
} else if (rule.unitType === UnitObject.SelectRange) {
(rule as IRangeProtectionRule).id = rangeProtectionRuleModel.createRuleId(unitId, subUnitId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ export const AddWorksheetProtectionCommand: ICommand<IAddWorksheetProtectionPara
const result = await commandService.executeCommand(AddWorksheetProtectionMutation.id, {
unitId,
rule,
subUnitId: rule.subUnitId,
});

if (result) {
const redoMutations = [{ id: AddWorksheetProtectionMutation.id, params: { unitId, rule } }];
const redoMutations = [{ id: AddWorksheetProtectionMutation.id, params: { unitId, rule, subUnitId: rule.subUnitId } }];
const undoMutations = [{ id: DeleteWorksheetProtectionMutation.id, params: { unitId, subUnitId } }];
undoRedoService.pushUndoRedo({
unitID: unitId,
Expand Down Expand Up @@ -87,7 +88,7 @@ export const DeleteWorksheetProtectionCommand: ICommand<IDeleteWorksheetProtecti
});

const redoMutations = [{ id: DeleteWorksheetProtectionMutation.id, params: { unitId, subUnitId } }];
const undoMutations = [{ id: AddWorksheetProtectionMutation.id, params: { unitId, rule } }];
const undoMutations = [{ id: AddWorksheetProtectionMutation.id, params: { unitId, rule, subUnitId: rule.subUnitId } }];
undoRedoService.pushUndoRedo({
unitID: unitId,
redoMutations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import type { IWorksheetProtectionRule } from '../../services/permission/type';

export interface IAddWorksheetProtectionParams {
unitId: string;
subUnitId: string;
rule: IWorksheetProtectionRule;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/sheets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,3 +375,5 @@ export {

export { RangeProtectionPermissionEditPoint } from './services/permission/permission-point/range/edit';
export { RangeProtectionPermissionViewPoint } from './services/permission/permission-point/range/view';

export type { IAddWorksheetProtectionParams } from './commands/mutations/add-worksheet-protection.mutation';
13 changes: 7 additions & 6 deletions packages/ui/src/services/parts/parts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ export enum BuiltInUIPart {
export interface IUIPartsService {
componentRegistered$: Observable<ComponentPartKey>;

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

export const IUIPartsService = createIdentifier<IUIPartsService>('ui.parts.service');

export class UIPartsService extends Disposable implements IUIPartsService {
private _componentsByPart: Map<ComponentPartKey, Set<ComponentRenderer>> = new Map();
private _componentsByPart: Map<ComponentPartKey, Set<ComponentType>> = new Map();

private readonly _componentRegistered$ = new Subject<ComponentPartKey>();
readonly componentRegistered$ = this._componentRegistered$.asObservable();
Expand All @@ -54,24 +54,25 @@ export class UIPartsService extends Disposable implements IUIPartsService {
this._componentRegistered$.complete();
}

registerComponent(part: ComponentPartKey, component: () => React.ComponentType): IDisposable {
registerComponent(part: ComponentPartKey, componentFactory: () => React.ComponentType): IDisposable {
const componentType = componentFactory();
const components = (
this._componentsByPart.get(part)
|| this._componentsByPart.set(part, new Set()).get(part)!
).add(component);
).add(componentType);

this._componentRegistered$.next(part);

return toDisposable(() => {
components.delete(component);
components.delete(componentType);
if (components.size === 0) {
this._componentsByPart.delete(part);
}
this._componentRegistered$.next(part);
});
}

getComponents(part: ComponentPartKey): Set<ComponentRenderer> {
getComponents(part: ComponentPartKey): Set<ComponentType> {
return new Set([...(this._componentsByPart.get(part) || new Set())]);
}
}
46 changes: 14 additions & 32 deletions packages/ui/src/views/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import { useDependency } from '@wendellhu/redi/react-bindings';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import type { IWorkbenchOptions } from '../controllers/ui/ui.controller';
import { IMessageService } from '../services/message/message.service';
import { BuiltInUIPart, IUIPartsService } from '../services/parts/parts.service';
import { BuiltInUIPart } from '../services/parts/parts.service';
import styles from './app.module.less';
import { ComponentContainer } from './components/ComponentContainer';
import { ComponentContainer, useComponentsOfPart } from './components/ComponentContainer';
import { Toolbar } from './components/doc-bars/Toolbar';
import { Sidebar } from './components/sidebar/Sidebar';
import { ZenZone } from './components/zen-zone/ZenZone';
Expand All @@ -50,30 +50,12 @@ export function App(props: IUniverAppProps) {
const messageService = useDependency(IMessageService);
const contentRef = useRef<HTMLDivElement>(null);

const uiPartsService = useDependency(IUIPartsService);

const [updateTrigger, setUpdateTrigger] = useState({});

useEffect(() => {
const updateSubscription = uiPartsService.componentRegistered$.subscribe(() => {
setUpdateTrigger({});
});

return () => {
updateSubscription.unsubscribe();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const { headerComponents, contentComponents, footerComponents, headerMenuComponents, leftSidebarComponents, globalComponents } = useMemo(() => ({
headerComponents: uiPartsService.getComponents(BuiltInUIPart.HEADER),
contentComponents: uiPartsService.getComponents(BuiltInUIPart.CONTENT),
footerComponents: uiPartsService.getComponents(BuiltInUIPart.FOOTER),
headerMenuComponents: uiPartsService.getComponents(BuiltInUIPart.HEADER_MENU),
leftSidebarComponents: uiPartsService.getComponents(BuiltInUIPart.LEFT_SIDEBAR),
globalComponents: uiPartsService.getComponents(BuiltInUIPart.GLOBAL),
// eslint-disable-next-line react-hooks/exhaustive-deps
}), [updateTrigger]);
const footerComponents = useComponentsOfPart(BuiltInUIPart.FOOTER);
const headerComponents = useComponentsOfPart(BuiltInUIPart.HEADER);
const headerMenuComponents = useComponentsOfPart(BuiltInUIPart.HEADER_MENU);
const contentComponents = useComponentsOfPart(BuiltInUIPart.CONTENT);
const leftSidebarComponents = useComponentsOfPart(BuiltInUIPart.LEFT_SIDEBAR);
const globalComponents = useComponentsOfPart(BuiltInUIPart.GLOBAL);

useEffect(() => {
if (!themeService.getCurrentTheme()) {
Expand Down Expand Up @@ -135,12 +117,12 @@ export function App(props: IUniverAppProps) {
<section className={styles.appContainer}>
<div className={styles.appContainerWrapper}>
<aside className={styles.appContainerLeftSidebar}>
<ComponentContainer components={leftSidebarComponents} />
<ComponentContainer key="left-sidebar" components={leftSidebarComponents} />
</aside>

<section className={styles.appContainerContent}>
<header>
{header && <ComponentContainer components={headerComponents} />}
{header && <ComponentContainer key="header" components={headerComponents} />}
</header>

<section
Expand All @@ -149,7 +131,7 @@ export function App(props: IUniverAppProps) {
data-range-selector
onContextMenu={(e) => e.preventDefault()}
>
<ComponentContainer components={contentComponents} />
<ComponentContainer key="content" components={contentComponents} />
</section>
</section>

Expand All @@ -161,15 +143,15 @@ export function App(props: IUniverAppProps) {
{/* footer */}
{footer && (
<footer className={styles.appFooter}>
<ComponentContainer components={footerComponents} />
<ComponentContainer key="footer" components={footerComponents} />
</footer>
)}

<ZenZone />
</section>
</div>
<ComponentContainer components={globalComponents} />
<ComponentContainer components={builtInGlobalComponents} />
<ComponentContainer key="global" components={globalComponents} />
<ComponentContainer key="built-in-global" components={builtInGlobalComponents} />
{contextMenu && <ContextMenu />}
</ConfigProvider>
);
Expand Down
12 changes: 6 additions & 6 deletions packages/ui/src/views/components/ComponentContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useObservable } from '../../components/hooks/observable';
import { IUIPartsService } from '../../services/parts/parts.service';

export interface IComponentContainerProps {
components?: Set<() => ComponentType>;
components?: Set<ComponentType>;
fallback?: React.ReactNode;
sharedProps?: Record<string, unknown>;
}
Expand All @@ -32,9 +32,9 @@ export function ComponentContainer(props: IComponentContainerProps) {
const { components, fallback, sharedProps } = props;
if (!components || components.size === 0) return fallback ?? null;

return Array.from(components.values()).map((component, index) =>
React.createElement(component(), { key: `${index}`, ...sharedProps })
);
return Array.from(components.values()).map((component, index) => {
return React.createElement(component, { key: `${component.displayName ?? index}`, ...sharedProps });
});
}

/**
Expand All @@ -55,9 +55,9 @@ export function useComponentsOfPart(part: string, injector?: Injector) {
),
undefined,
undefined,
[uiPartsService]
[uiPartsService, part]
);

// eslint-disable-next-line react-hooks/exhaustive-deps
// eslint-disable-next-line react-hooks/exhaustive-deps
return useMemo(() => uiPartsService.getComponents(part), [componentPartUpdateCount]);
}
2 changes: 1 addition & 1 deletion packages/ui/src/views/components/doc-bars/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const MENU_POSITIONS = [
];

export interface IToolbarProps {
headerMenuComponents?: Set<() => ComponentType>;
headerMenuComponents?: Set<ComponentType>;
}

/**
Expand Down
4 changes: 1 addition & 3 deletions packages/ui/src/views/parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@ import type { ComponentType } from 'react';

import { GlobalZone } from './components/global-zone/GlobalZone';

export const builtInGlobalComponents: Set<() => ComponentType> = new Set([
() => GlobalZone,
]);
export const builtInGlobalComponents: Set<ComponentType> = new Set([GlobalZone]);

0 comments on commit 25da5f0

Please sign in to comment.