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

fix: parsing of number formats should be done in plugins #3959

Merged
merged 2 commits into from
Nov 7, 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 @@ -160,4 +160,162 @@ describe('test editor', () => {
// The date-time drop is a numeric value, not a literal string
expect(result?.v).toBe(0.5231712962962963);
});

it('edit number will throw style in this editing', () => {
const sheetInterceptorService = testBed.get(SheetInterceptorService);
const cellData = {
p: {
id: '__INTERNAL_EDITOR__DOCS_NORMAL',
documentStyle: {
pageSize: {
width: 230.41758728027344,
},
marginTop: 6,
marginBottom: 2,
marginRight: 2,
marginLeft: 2,
renderConfig: {
verticalAlign: 0,
horizontalAlign: 0,
wrapStrategy: 0,
background: {},
centerAngle: 0,
vertexAngle: 0,
},
},
body: {
dataStream: '2022-11-11\r\n',
textRuns: [
{
st: 0,
ed: 2,
ts: {
ff: 'Arial',
fs: 11,
},
},
{
st: 2,
ed: 10,
ts: {
ff: 'Arial',
fs: 11,
bl: 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer use BooleanNumber.TRUE ?

},
},
],
paragraphs: [
{
startIndex: 10,
paragraphStyle: {
horizontalAlign: 0,
},
},
],
customRanges: [],
customDecorations: [],
},
drawings: {},
drawingsOrder: [],
settings: {
zoomRatio: 1,
},
},
v: null,
f: null,
si: null,
};
const location = {
workbook,
worksheet,
unitId,
subUnitId,
row: 0,
col: 0,
origin: cellData,
};

const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
expect(result?.s).toBeFalsy();
expect(result?.p).toBeFalsy();
expect(result?.v).toBe(44876);
});

it('edit number with bullet should keep rich text', () => {
const sheetInterceptorService = testBed.get(SheetInterceptorService);
const richTextParams = {
id: '__INTERNAL_EDITOR__ZEN_EDITOR',
documentStyle: {
pageSize: {
width: 595,
},
documentFlavor: 0,
marginTop: 0,
marginBottom: 0,
marginRight: 0,
marginLeft: 0,
renderConfig: {
vertexAngle: 0,
centerAngle: 0,
},
},
drawings: {},
drawingsOrder: [],
body: {
dataStream: '123123123\r\n',
textRuns: [
{
st: 0,
ed: 9,
ts: {
ff: 'Arial',
fs: 11,
},
},
],
paragraphs: [
{
startIndex: 9,
paragraphStyle: {
horizontalAlign: 0,
},
bullet: {
nestingLevel: 0,
textStyle: {
fs: 20,
},
listType: 'CHECK_LIST',
listId: 'xIECkc',
},
},
],
sectionBreaks: [
{
startIndex: 10,
},
],
customRanges: [],
customDecorations: [],
},
};
const cellData = {
p: richTextParams,
v: null,
f: null,
si: null,
};
const location = {
workbook,
worksheet,
unitId,
subUnitId,
row: 0,
col: 0,
origin: cellData,
};

const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
expect(result?.p).toEqual(richTextParams);
expect(result?.v).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import type { IRange, Nullable, Workbook } from '@univerjs/core';
import type { IDocumentBody, IRange, Nullable, Workbook } from '@univerjs/core';
import type {
INumfmtItemWithCache,
IRemoveNumfmtMutationParams,
Expand Down Expand Up @@ -144,9 +144,11 @@ export class NumfmtEditorController extends Disposable {
toDisposable(
this._sheetInterceptorService.writeCellInterceptor.intercept(AFTER_CELL_EDIT,
{
// eslint-disable-next-line complexity
handler: (value, context, next) => {
// clear the effect
this._collectEffectMutation.clean();
const { worksheet, row, col } = context;
const currentNumfmtValue = this._numfmtService.getValue(
context.unitId,
context.subUnitId,
Expand All @@ -164,19 +166,33 @@ export class NumfmtEditorController extends Disposable {
null
);
};

// if the value is empty or the current numfmt is text format, return the value directly
if (!value?.v || currentNumfmtValue?.pattern === DEFAULT_TEXT_FORMAT) {
if (!value?.v && !value?.p) {
return next(value);
}

const content = String(value.v);
if (currentNumfmtValue?.pattern === DEFAULT_TEXT_FORMAT) {
return next(value);
}

const body = value.p?.body;
const content = value?.p?.body?.dataStream ? value.p.body.dataStream.replace(/\r\n$/, '') : String(value.v);
const numfmtInfo = numfmt.parseDate(content) || numfmt.parseTime(content) || numfmt.parseNumber(content);

if (body) {
if (!canConvertRichTextToNumfmt(body)) {
return next(value);
} else {
const { dataStream } = body;
const dataStreamWithoutEnd = dataStream.replace(/\r\n$/, '');
const num = Number(dataStreamWithoutEnd);
if (Number.isNaN(num) && !numfmtInfo) {
return next(value);
}
}
}

if (numfmtInfo) {
if (numfmtInfo.z) {
const v = Number(numfmtInfo.v);
this._collectEffectMutation.add(
context.unitId,
context.subUnitId,
Expand All @@ -186,12 +202,12 @@ export class NumfmtEditorController extends Disposable {
pattern: numfmtInfo.z,
}
);
return { ...value, v, t: CellValueType.NUMBER };
}
} else if (
['date', 'time', 'datetime', 'percent'].includes(currentNumfmtType) ||
!isNumeric(content)
) {
const v = Number(numfmtInfo.v);
// The format needs to discard the current style settings
const originStyle = worksheet.getCellStyleOnly(row, col)?.s;
return { ...value, v, p: null, s: originStyle, t: CellValueType.NUMBER };
} else if (['date', 'time', 'datetime', 'percent'].includes(currentNumfmtType) || !isNumeric(content)) {
clean();
}
return next(value);
Expand Down Expand Up @@ -282,3 +298,23 @@ export class NumfmtEditorController extends Disposable {
function isNumeric(str: string) {
return /^-?\d+(\.\d+)?$/.test(str);
}

function canConvertRichTextToNumfmt(body: IDocumentBody): boolean {
const { textRuns = [], paragraphs = [], customRanges, customBlocks = [] } = body;

// Some styles are unique to rich text. When this style appears, we consider the value to be rich text.
const richTextStyle = ['va'];

return !(
textRuns.some((textRun) => {
const hasRichTextStyle = Boolean(textRun.ts && Object.keys(textRun.ts).some((property) => {
return richTextStyle.includes(property);
}));
return hasRichTextStyle;
}) ||
paragraphs.some((paragraph) => paragraph.bullet) ||
paragraphs.length >= 2 ||
Boolean(customRanges?.length) ||
customBlocks.length > 0
);
}
Loading
Loading