Skip to content

Commit

Permalink
refactor(permission): modify the logic of skipping rendering in the c… (
Browse files Browse the repository at this point in the history
#2426)

* refactor(permission): modify the logic of skipping rendering in the case of invisible permissions

* chore: lint fix
  • Loading branch information
ybzky committed Jun 6, 2024
1 parent e4d0f44 commit d4a0929
Show file tree
Hide file tree
Showing 18 changed files with 30 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
* limitations under the License.
*/

import type { ICellData, IRange, IScale } from '@univerjs/core';
import type { IRange, IScale } from '@univerjs/core';

import { fixLineWidthByScale, getColor, inViewRanges, mergeRangeIfIntersects } from '../../../basics/tools';
import type { UniverRenderingContext } from '../../../context';
import type { IDrawInfo } from '../../extension';
import { SpreadsheetExtensionRegistry } from '../../extension';
import type { SpreadsheetSkeleton } from '../sheet-skeleton';
import type { Spreadsheet } from '../spreadsheet';
import type { ISheetRenderExtension } from '../interfaces';
import { SheetExtension } from './sheet-extension';

const UNIQUE_KEY = 'DefaultBackgroundExtension';
Expand Down Expand Up @@ -89,12 +88,6 @@ export class Background extends SheetExtension {
return true;
}

const cellData = worksheet.getCell(rowIndex, columnIndex) as ICellData & ISheetRenderExtension;

if (cellData.backgroundRenderExtension?.isSkip) {
return true;
}

const cellInfo = backgroundPositions?.getValue(rowIndex, columnIndex);
if (cellInfo == null) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

import type { ICellData, IRange, IScale, ObjectMatrix } from '@univerjs/core';
import type { IRange, IScale, ObjectMatrix } from '@univerjs/core';
import { BorderStyleTypes } from '@univerjs/core';

import { BORDER_TYPE, COLOR_BLACK_RGB, FIX_ONE_PIXEL_BLUR_OFFSET } from '../../../basics/const';
import { drawDiagonalLineByBorderType, drawLineByBorderType, getLineWidth, setLineType } from '../../../basics/draw';
import type { UniverRenderingContext } from '../../../context';
import { SpreadsheetExtensionRegistry } from '../../extension';
import type { BorderCacheItem, ISheetRenderExtension } from '../interfaces';
import type { BorderCacheItem } from '../interfaces';
import type { SpreadsheetSkeleton } from '../sheet-skeleton';
import { SheetExtension } from './sheet-extension';

Expand Down Expand Up @@ -78,12 +78,6 @@ export class Border extends SheetExtension {
return true;
}

const cellData = worksheet.getCell(rowIndex, columnIndex) as ICellData & ISheetRenderExtension;

if (cellData.borderRenderExtension?.isSkip) {
return true;
}

const cellInfo = this.getCellIndex(
rowIndex,
columnIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ export class Marker extends SheetExtension {
return;
}

if (cellData.markers?.isSkip) {
return;
}

if (cellData.markers?.tr) {
ctx.save();
const marker = cellData.markers.tr;
Expand Down
17 changes: 0 additions & 17 deletions packages/engine-render/src/components/sheets/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import type { BORDER_TYPE } from '../../basics/const';
import type { DocumentSkeleton } from '../docs/layout/doc-skeleton';
import type { Canvas } from '../../canvas';
import type { UniverRenderingContext } from '../../context';
import type { ISheetFontRenderExtension } from './extensions';

export
interface BorderCache {
Expand Down Expand Up @@ -110,19 +109,3 @@ export interface IPaintForScrolling {
scaleY: number;
}

export type ISheetRenderExtension = ISheetFontRenderExtension
& ISheetBackgroundRenderExtension
& ISheetBorderRenderExtension;

export interface ISheetBackgroundRenderExtension {
backgroundRenderExtension?: {
isSkip?: boolean;
};
};

export interface ISheetBorderRenderExtension {
borderRenderExtension?: {
isSkip?: boolean;
};
};

Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,15 @@
*/

import type { Workbook } from '@univerjs/core';
import { Disposable, IPermissionService, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import type { IRangeProtectionRule, IWorksheetProtectionRenderCellData } from '@univerjs/sheets';
import { INTERCEPTOR_POINT, RangeProtectionRenderModel, RangeProtectionRuleModel, SheetInterceptorService, WorksheetEditPermission, WorksheetProtectionRuleModel, WorksheetViewPermission } from '@univerjs/sheets';
import { Disposable, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
import { INTERCEPTOR_POINT, SheetInterceptorService } from '@univerjs/sheets';
import { SheetSkeletonManagerService } from '@univerjs/sheets-ui';
import { Inject } from '@wendellhu/redi';
import { bufferTime, filter } from 'rxjs/operators';
import type { ISheetFontRenderExtension, ISheetRenderExtension } from '@univerjs/engine-render';
import type { ISheetFontRenderExtension } from '@univerjs/engine-render';
import { IRenderManagerService } from '@univerjs/engine-render';
import { ConditionalFormattingRuleModel, ConditionalFormattingService, ConditionalFormattingViewModel, DEFAULT_PADDING, DEFAULT_WIDTH } from '@univerjs/sheets-conditional-formatting';
import type { IConditionalFormattingCellData, IDataBarCellData, IIconSetCellData } from '@univerjs/sheets-conditional-formatting';
import type { IRangeProtectionRenderCellData } from '@univerjs/sheets-ui';
import { SheetSkeletonManagerService } from '@univerjs/sheets-ui';
import { UnitAction } from '@univerjs/protocol';
import type { IConditionalFormattingCellData } from '@univerjs/sheets-conditional-formatting';

@OnLifecycle(LifecycleStages.Rendered, SheetsCfRenderController)
export class SheetsCfRenderController extends Disposable {
Expand All @@ -35,18 +32,12 @@ export class SheetsCfRenderController extends Disposable {
@Inject(IUniverInstanceService) private _univerInstanceService: IUniverInstanceService,
@Inject(IRenderManagerService) private _renderManagerService: IRenderManagerService,
@Inject(ConditionalFormattingViewModel) private _conditionalFormattingViewModel: ConditionalFormattingViewModel,
@Inject(ConditionalFormattingRuleModel) private _conditionalFormattingRuleModel: ConditionalFormattingRuleModel,
@Inject(RangeProtectionRenderModel) private _selectionProtectionRenderModel: RangeProtectionRenderModel,
@Inject(RangeProtectionRuleModel) private _rangeProtectionRuleModel: RangeProtectionRuleModel,
@Inject(WorksheetProtectionRuleModel) private _worksheetProtectionRuleModel: WorksheetProtectionRuleModel,
@IPermissionService private _permissionService: IPermissionService
@Inject(ConditionalFormattingRuleModel) private _conditionalFormattingRuleModel: ConditionalFormattingRuleModel
) {
super();

this._initViewModelInterceptor();
this._initSkeleton();
this._initViewModelByRangeInterceptor();
this._initViewModelBySheetInterceptor();
}

private _initSkeleton() {
Expand Down Expand Up @@ -118,60 +109,4 @@ export class SheetsCfRenderController extends Disposable {
priority: 10,
}));
}

private _initViewModelByRangeInterceptor() {
this.disposeWithMe(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {
handler: (cell = {}, context, next) => {
const { unitId, subUnitId, row, col } = context;

const permissionList = this._selectionProtectionRenderModel.getCellInfo(unitId, subUnitId, row, col)
.filter((p) => !!p.ruleId)
.map((p) => {
const rule = this._rangeProtectionRuleModel.getRule(unitId, subUnitId, p.ruleId!) || {} as IRangeProtectionRule;
return {
...p, ranges: rule.ranges!,
};
})
.filter((p) => !!p.ranges);
if (permissionList.length) {
const isSkipRender = permissionList.some((p) => !p?.[UnitAction.View]);
const _cellData: IRangeProtectionRenderCellData & ISheetRenderExtension & IIconSetCellData & IDataBarCellData = { ...cell, selectionProtection: permissionList };
const { dataBar, iconSet } = _cellData;
if (isSkipRender) {
dataBar && (dataBar.isSkip = true);
iconSet && (iconSet.isSkip = true);
}

return next(_cellData);
}
return next(cell);
},
}
));
}

private _initViewModelBySheetInterceptor() {
this.disposeWithMe(this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {
handler: (cell = {}, context, next) => {
const { unitId, subUnitId } = context;
const worksheetRule = this._worksheetProtectionRuleModel.getRule(unitId, subUnitId);
if (worksheetRule?.permissionId && worksheetRule.name) {
const selectionProtection = [{
[UnitAction.View]: this._permissionService.getPermissionPoint(new WorksheetViewPermission(unitId, subUnitId).id)?.value ?? false,
[UnitAction.Edit]: this._permissionService.getPermissionPoint(new WorksheetEditPermission(unitId, subUnitId).id)?.value ?? false,
}];
const isSkipRender = !selectionProtection[0]?.[UnitAction.View];
const _cellData: IWorksheetProtectionRenderCellData & ISheetFontRenderExtension & IIconSetCellData & IDataBarCellData = { ...cell, hasWorksheetRule: true, selectionProtection };
const { dataBar, iconSet } = _cellData;
if (isSkipRender) {
dataBar && (dataBar.isSkip = true);
iconSet && (iconSet.isSkip = true);
}
return next(_cellData);
}
return next(cell);
},
}
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ export class DataBar extends SheetExtension {
return;
}

if (cellData?.dataBar?.isSkip) {
return;
}

const { color, value, startPoint, isGradient } = cellData.dataBar;
const cellInfo = this.getCellIndex(row, col, rowHeightAccumulation, columnWidthAccumulation, dataMergeCache);
let { isMerged, isMergedMainCell, mergeInfo, startY, endY, startX, endX } = cellInfo;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ export class ConditionalFormattingIcon extends SheetExtension {
return;
}

if (cellData.iconSet?.isSkip) {
return;
}

const { iconType, iconId } = cellData.iconSet;
if (iconType === EMPTY_ICON_TYPE) {
return;
Expand Down
2 changes: 0 additions & 2 deletions packages/sheets-conditional-formatting/src/render/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export interface IDataBarRenderParams {
startPoint: number; //0-100
isGradient: boolean;
isShowValue: boolean;
isSkip?: boolean;

}
export interface IDataBarCellData extends ICellData {
Expand All @@ -34,7 +33,6 @@ export interface IIconSetRenderParams {
iconId: string;
iconType: IIconType;
isShowValue: boolean;
isSkip?: boolean;
}

export interface IIconSetCellData extends ICellData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ export class DataValidationAlertController extends Disposable {
const cellData = worksheet.getCell(cellPos.location.row, cellPos.location.col);

if (cellData?.dataValidation?.validStatus === DataValidationStatus.INVALID) {
if (cellData.dataValidation?.isSkip) {
return;
}
const currentAlert = this._cellAlertManagerService.currentAlert.get(ALERT_KEY);
const currentLoc = currentAlert?.alert?.location;
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ export class CheckboxRender implements IBaseDataValidationWidget {
return;
}

if (data.dataValidation?.isSkip) {
return;
}

const colors = this._themeService.getCurrentTheme();
if (!validator.skipDefaultFontRender(rule, value, { unitId, subUnitId })) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ export class DropdownMultipleWidget implements IBaseDataValidationWidget {
return;
}

if (data.dataValidation?.isSkip) {
return;
}

const cellBounding = {
startX: _cellBounding.startX + leftOffset,
endX: _cellBounding.endX - rightOffset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ export class DropdownWidget implements IBaseDataValidationWidget {
return;
}

if (data.dataValidation?.isSkip) {
return;
}

const cellBounding = {
startX: _cellBounding.startX + leftOffset,
endX: _cellBounding.endX - rightOffset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@
*/

import { ColorKit, CommandType, Disposable, EDITOR_ACTIVATED, fromCallback, groupBy, ICommandService, IContextService, IUniverInstanceService, LifecycleStages, ObjectMatrix, OnLifecycle, replaceInDocumentBody, rotate, ThemeService, Tools, UniverInstanceType } from '@univerjs/core';
import type { ICellData, ICellDataForSheetInterceptor, IObjectMatrixPrimitiveType, IRange, Nullable, Workbook, Worksheet } from '@univerjs/core';
import type { ICellData, IObjectMatrixPrimitiveType, IRange, Nullable, Workbook, Worksheet } from '@univerjs/core';
import { IRenderManagerService, RENDER_RAW_FORMULA_KEY } from '@univerjs/engine-render';
import type { IFindComplete, IFindMatch, IFindMoveParams, IFindQuery, IFindReplaceProvider, IReplaceAllResult } from '@univerjs/find-replace';
import { FindBy, FindDirection, FindModel, FindReplaceController, FindScope, IFindReplaceService } from '@univerjs/find-replace';
import type { ICellPermission, ISelectionWithStyle, ISetRangeValuesCommandParams, ISetSelectionsOperationParams, ISetWorksheetActivateCommandParams, ISheetCommandSharedParams } from '@univerjs/sheets';
import type { ISelectionWithStyle, ISetRangeValuesCommandParams, ISetSelectionsOperationParams, ISetWorksheetActivateCommandParams, ISheetCommandSharedParams } from '@univerjs/sheets';
import { SelectionManagerService, SetRangeValuesCommand, SetSelectionsOperation, SetWorksheetActivateCommand, SetWorksheetActiveOperation } from '@univerjs/sheets';
import type { IScrollToCellCommandParams } from '@univerjs/sheets-ui';
import { getCoordByCell, getSheetObject, ScrollToCellCommand, SheetSkeletonManagerService } from '@univerjs/sheets-ui';
import { type IDisposable, Inject, Injector } from '@wendellhu/redi';
import { debounceTime, filter, merge, skip, Subject, throttleTime } from 'rxjs';

import { UnitAction } from '@univerjs/protocol';
import type { ISheetFindReplaceHighlightShapeProps } from '../views/shapes/find-replace-highlight.shape';
import { SheetFindReplaceHighlightShape } from '../views/shapes/find-replace-highlight.shape';
import type { ISheetReplaceCommandParams, ISheetReplacement } from '../commands/commands/sheet-replace.command';
Expand Down Expand Up @@ -367,12 +366,6 @@ export class SheetFindModel extends FindModel {
for (const value of iter) {
const { row, col, colSpan, rowSpan, value: cellData } = value;

const permissionCheck = this._getFindReplacePermissionCheck({ row, col, unitId, subUnitId });

if (!permissionCheck) {
continue;
}

if (dedupeFn?.(row, col) || !cellData) {
continue;
};
Expand Down Expand Up @@ -884,21 +877,6 @@ export class SheetFindModel extends FindModel {
const newContent = currentContent.v!.toString().replace(new RegExp(escapeRegExp(findString), replaceFlag), replaceString!);
return { v: newContent };
}

private _getFindReplacePermissionCheck(_cellInfo: { row: number; col: number; unitId: string; subUnitId: string }) {
const { row, col, subUnitId } = _cellInfo;
const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
const worksheet = workbook.getSheetBySheetId(subUnitId);
if (!worksheet) {
return false;
}
const permission = (worksheet.getCell(row, col) as (ICellDataForSheetInterceptor & { selectionProtection: ICellPermission[] }))?.selectionProtection?.[0];
const viewPermission = permission?.[UnitAction.View];
if (viewPermission === false) {
return false;
}
return true;
}
}

function escapeRegExp(text: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ export class CellCustomRenderController extends Disposable implements IRenderMod
const activeRenderInfo = getActiveRender(evt);
if (activeRenderInfo) {
const [activeRender, cellContext] = activeRenderInfo;
if (cellContext.data.dataValidation?.isSkip) {
return;
}
activeRender.onPointerDown?.(cellContext, evt);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import type { DocumentDataModel, ICommandInfo, IParagraph, ITextRun, JSONXActions, Nullable } from '@univerjs/core';
import {
BooleanNumber,
createInterceptorKey,
DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY,
DOCS_NORMAL_EDITOR_UNIT_ID_KEY,
EDITOR_ACTIVATED,
Expand All @@ -26,7 +25,6 @@ import {
HorizontalAlign,
ICommandService,
IContextService,
InterceptorManager,
IUndoRedoService,
IUniverInstanceService,
LifecycleStages,
Expand Down Expand Up @@ -57,14 +55,10 @@ import { IFormulaEditorManagerService } from '../../services/editor/formula-edit
import type { IEditorBridgeServiceParam } from '../../services/editor-bridge.service';
import { IEditorBridgeService } from '../../services/editor-bridge.service';

export const FORMULA_EDIT_PERMISSION_CHECK = createInterceptorKey<boolean, { row: number; col: number }>('formulaEditPermissionCheck');

@OnLifecycle(LifecycleStages.Rendered, FormulaEditorController)
export class FormulaEditorController extends RxDisposable {
private _loadedMap = new WeakSet<RenderComponentType>();

public interceptor = new InterceptorManager({ FORMULA_EDIT_PERMISSION_CHECK });

constructor(
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
Expand Down Expand Up @@ -257,12 +251,6 @@ export class FormulaEditorController extends RxDisposable {
return;
}

const permissionCheck = this.interceptor.fetchThroughInterceptors(FORMULA_EDIT_PERMISSION_CHECK)(null, { row: editCellState.row, col: editCellState.column });
if (!permissionCheck) {
this._syncContentAndRender(DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, '', [{ startIndex: 0 }], []);
return;
}

this._editorSyncHandler(editCellState);
});

Expand Down
Loading

0 comments on commit d4a0929

Please sign in to comment.