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(sheet): force string adds popup #1934

Merged
merged 4 commits into from
Apr 23, 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
Expand Up @@ -1117,7 +1117,15 @@ export class SpreadsheetSkeleton extends Skeleton {
const textStyle = this._getFontFormat(style);
fontString = getFontStyleString(textStyle, this._localService).fontCache;

documentModel = this._getDocumentDataByStyle(extractPureTextFromCell(cell), textStyle, {
let cellText = extractPureTextFromCell(cell);

// Add a single quotation mark to the force string type. Don't add single quotation mark in extractPureTextFromCell, because copy and paste will be affected.
// edit mode when displayRawFormula is true
if (cell.t === CellValueType.FORCE_STRING && displayRawFormula) {
cellText = `'${cellText}`;
}

documentModel = this._getDocumentDataByStyle(cellText, textStyle, {
...cellOtherConfig,
textRotation,
cellValueType: cell.t!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
* limitations under the License.
*/

import type { IDocumentData, Univer, Workbook } from '@univerjs/core';
import { IContextService, LocaleService } from '@univerjs/core';
import type { ICellData, IDocumentData, Univer, Workbook } from '@univerjs/core';
import { CellValueType, IContextService, LocaleService } from '@univerjs/core';
import { LexerTreeBuilder } from '@univerjs/engine-formula';
import { SpreadsheetSkeleton } from '@univerjs/engine-render';
import type { Injector } from '@wendellhu/redi';
Expand Down Expand Up @@ -77,6 +77,7 @@ describe('Test EndEditController', () => {
let contextService: IContextService;
let lexerTreeBuilder: LexerTreeBuilder;
let spreadsheetSkeleton: SpreadsheetSkeleton;
let getCellDataByInputCell: (cell: ICellData, inputCell: ICellData) => ICellData | null;

beforeEach(() => {
const testBed = createTestBed();
Expand All @@ -99,14 +100,23 @@ describe('Test EndEditController', () => {
localeService,
contextService
);

getCellDataByInputCell = (cell: ICellData, inputCell: ICellData) => {
const documentLayoutObject = spreadsheetSkeleton.getCellDocumentModelWithFormula(inputCell);
if (!documentLayoutObject) {
throw new Error('documentLayoutObject is undefined');
}

return getCellDataByInput(cell, documentLayoutObject, lexerTreeBuilder);
};
});

afterEach(() => {
univer.dispose();
});

describe('getCellDataByInput', () => {
it('normal cell', () => {
describe('Function getCellDataByInput', () => {
it('Normal cell', () => {
const cell = {
v: 1,
};
Expand All @@ -115,50 +125,32 @@ describe('Test EndEditController', () => {
v: 2,
};

const documentLayoutObject = spreadsheetSkeleton.getCellDocumentModelWithFormula(inputCell);
if (!documentLayoutObject) {
expect(documentLayoutObject).not.toBeUndefined();
return;
}

const cellData = getCellDataByInput(cell, documentLayoutObject, lexerTreeBuilder);
const cellData = getCellDataByInputCell(cell, inputCell);
expect(cellData).toEqual({ v: '2', f: null, si: null, p: null });
});
it('rich text cell', () => {
it('Rich text cell', () => {
const cell = {
v: 1,
};
const inputCell = {
p: richTextDemo,
};

const documentLayoutObject = spreadsheetSkeleton.getCellDocumentModelWithFormula(inputCell);
if (!documentLayoutObject) {
expect(documentLayoutObject).not.toBeUndefined();
return;
}

const cellData = getCellDataByInput(cell, documentLayoutObject, lexerTreeBuilder);
const cellData = getCellDataByInputCell(cell, inputCell);
expect(cellData).toEqual({ v: null, f: null, si: null, p: richTextDemo });
});
it('formula cell', () => {
it('Formula cell', () => {
const cell = {
v: 1,
};
const inputCell = {
f: '=SUM(1)',
};

const documentLayoutObject = spreadsheetSkeleton.getCellDocumentModelWithFormula(inputCell);
if (!documentLayoutObject) {
expect(documentLayoutObject).not.toBeUndefined();
return;
}

const cellData = getCellDataByInput(cell, documentLayoutObject, lexerTreeBuilder);
const cellData = getCellDataByInputCell(cell, inputCell);
expect(cellData).toEqual({ v: null, f: '=SUM(1)', p: null });
});
it('clear formula cell', () => {
it('Clear formula cell', () => {
const cell = {
f: '=H18:H25',
v: 0,
Expand All @@ -169,14 +161,38 @@ describe('Test EndEditController', () => {
v: '',
};

const documentLayoutObject = spreadsheetSkeleton.getCellDocumentModelWithFormula(inputCell);
if (!documentLayoutObject) {
expect(documentLayoutObject).not.toBeUndefined();
return;
}

const cellData = getCellDataByInput(cell, documentLayoutObject, lexerTreeBuilder);
const cellData = getCellDataByInputCell(cell, inputCell);
expect(cellData).toEqual({ v: '', f: null, si: null, p: null, t: undefined });
});

it('Clear formula cell with rich text', () => {
const cell = {
f: '=H18:H25',
v: 0,
t: 2,
};

const inputCell = {
p: richTextDemo,
};

const cellData = getCellDataByInputCell(cell, inputCell);
expect(cellData).toEqual({ v: null, f: null, si: null, p: richTextDemo, t: undefined });
});

it('Input force string, normal string', () => {
const cell = {
v: null,
};

let cellData = getCellDataByInputCell(cell, { v: "'test" });
expect(cellData).toEqual({ v: 'test', t: CellValueType.FORCE_STRING, f: null, si: null, p: null });

cellData = getCellDataByInputCell(cell, { v: "'1" });
expect(cellData).toEqual({ v: '1', t: CellValueType.FORCE_STRING, f: null, si: null, p: null });

cellData = getCellDataByInputCell(cell, { v: "'=SUM" });
expect(cellData).toEqual({ v: '=SUM', t: CellValueType.FORCE_STRING, f: null, si: null, p: null });
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import type { ICellData, ICommandInfo, IDocumentBody, Nullable, Observer } from '@univerjs/core';
import {
import { CellValueType,
DEFAULT_EMPTY_DOCUMENT_VALUE,
Direction,
Disposable,
Expand Down Expand Up @@ -54,6 +54,7 @@ import { ICellEditorManagerService } from '../../services/editor/cell-editor-man
import type { IEditorBridgeServiceVisibleParam } from '../../services/editor-bridge.service';
import { IEditorBridgeService } from '../../services/editor-bridge.service';
import { MOVE_SELECTION_KEYCODE_LIST } from '../shortcuts/editor.shortcut';
import { extractStringFromForceString, isForceString } from '../utils/cell-tools';

function isRichText(body: IDocumentBody) {
const { textRuns = [], paragraphs = [] } = body;
Expand Down Expand Up @@ -430,6 +431,13 @@ export function getCellDataByInput(
cellData.f = newDataStream;
cellData.v = null;
cellData.p = null;
} else if (isForceString(newDataStream)) {
const v = extractStringFromForceString(newDataStream);
cellData.v = v;
cellData.f = null;
cellData.si = null;
cellData.p = null;
cellData.t = CellValueType.FORCE_STRING;
} else if (isRichText(body)) {
if (body.dataStream === '\r\n') {
cellData.v = '';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* 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 type { Workbook } from '@univerjs/core';
import { CellValueType, Disposable, isRealNum, IUniverInstanceService, LifecycleStages, LocaleService, OnLifecycle, UniverInstanceType } from '@univerjs/core';

import { Inject } from '@wendellhu/redi';
import { HoverManagerService } from '../services/hover-manager.service';
import { CellAlertManagerService, CellAlertType } from '../services/cell-alert-manager.service';

const ALERT_KEY = 'SHEET_FORCE_STRING_ALERT';

@OnLifecycle(LifecycleStages.Rendered, ForceStringAlertController)
export class ForceStringAlertController extends Disposable {
constructor(
@Inject(HoverManagerService) private readonly _hoverManagerService: HoverManagerService,
@Inject(CellAlertManagerService) private readonly _cellAlertManagerService: CellAlertManagerService,
@IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@Inject(LocaleService) private readonly _localeService: LocaleService
) {
super();
this._init();
}

private _init() {
this._initCellAlertPopup();
}

private _initCellAlertPopup() {
this.disposeWithMe(this._hoverManagerService.currentCell$.subscribe((cellPos) => {
if (cellPos) {
const workbook = this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.SHEET)!;
const worksheet = workbook.getActiveSheet();
const cellData = worksheet.getCell(cellPos.location.row, cellPos.location.col);

if (cellData?.t === CellValueType.FORCE_STRING && cellData.v && isRealNum(cellData.v)) {
const currentAlert = this._cellAlertManagerService.currentAlert.get(ALERT_KEY);
const currentLoc = currentAlert?.alert?.location;
if (
currentLoc &&
currentLoc.row === cellPos.location.row &&
currentLoc.col === cellPos.location.col &&
currentLoc.subUnitId === cellPos.location.subUnitId &&
currentLoc.unitId === cellPos.location.unitId
) {
return;
}

this._cellAlertManagerService.showAlert({
type: CellAlertType.ERROR,
title: this._localeService.t('info.error'),
message: this._localeService.t('info.forceStringInfo'),
location: cellPos.location,
width: 200,
height: 74,
key: ALERT_KEY,
});
return;
}
}

this._cellAlertManagerService.removeAlert(ALERT_KEY);
}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* 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 { CellValueType, isRealNum, LifecycleStages, OnLifecycle, RxDisposable, ThemeService } from '@univerjs/core';
import { Inject } from '@wendellhu/redi';
import { INTERCEPTOR_POINT, SheetInterceptorService } from '@univerjs/sheets';
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';

/**
* @todo RenderUnit
*/
@OnLifecycle(LifecycleStages.Rendered, ForceStringRenderController)
export class ForceStringRenderController extends RxDisposable {
constructor(
@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,
@Inject(SheetInterceptorService) private readonly _sheetInterceptorService: SheetInterceptorService,
@Inject(ThemeService) private readonly _themeService: ThemeService
) {
super();
this._init();
}

private _init() {
this._initViewModelIntercept();
}


private _initViewModelIntercept() {
const color = this._themeService.getCurrentTheme().errorColor;
const FORCE_STRING_MARK = {
tr: {
size: 6,
color,
},
};

this.disposeWithMe(
this._sheetInterceptorService.intercept(
INTERCEPTOR_POINT.CELL_CONTENT,
{

handler: (cell, pos, next) => {
const skeleton = this._sheetSkeletonManagerService.getCurrent()?.skeleton;
if (!skeleton) {
return next(cell);
}

const cellRaw = pos.worksheet.getCellRaw(pos.row, pos.col);

if (!cellRaw || cellRaw.v === null || cellRaw.v === undefined) {
return next(cell);
}

if (cell?.t === CellValueType.FORCE_STRING && isRealNum(cellRaw.v)) {
return next({
...cell,
markers: {
...cell?.markers,
...FORCE_STRING_MARK,
},
});
}

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