Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix: paste line break \n will cause crash (#1055)

YuHong45fe57b38ace
diff --git a/packages/engine-formula/src/engine/analysis/__tests__/parser.spec.ts b/packages/engine-formula/src/engine/analysis/__tests__/parser.spec.ts
index f5faad1149..00af17a0dd 100644
--- a/packages/engine-formula/src/engine/analysis/__tests__/parser.spec.ts
+++ b/packages/engine-formula/src/engine/analysis/__tests__/parser.spec.ts
@@ -68,8 +68,16 @@ describe('Test indirect', () => {
[testBed.unitId]: testBed.sheetData,
},
});
-
- formulaRuntimeService.setCurrent(0, 0, testBed.sheetId, testBed.unitId);
+ const sheetItem = testBed.sheetData[testBed.sheetId];
+
+ formulaRuntimeService.setCurrent(
+ 0,
+ 0,
+ sheetItem.rowCount,
+ sheetItem.columnCount,
+ testBed.sheetId,
+ testBed.unitId
+ );
functionService.registerExecutors(new Sum(FUNCTION_NAMES_MATH.SUM), new Plus(FUNCTION_NAMES_META.PLUS));
});
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
index 6205be5457..d96fc998ca 100644
--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
@@ -16,6 +16,7 @@
import type {
ICellData,
+ IDocumentData,
IMutationInfo,
IObjectArrayPrimitiveType,
IObjectMatrixPrimitiveType,
@@ -75,7 +76,9 @@ import type {
ISheetClipboardHook,
ISheetRangeLocation,
} from '../../services/clipboard/type';
+import { SheetSkeletonManagerService } from '../../services/sheet-skeleton-manager.service';
import {
+ generateBody,
getClearAndSetMergeMutations,
getClearCellStyleMutations,
getDefaultOnPasteCellMutations,
@@ -95,6 +98,7 @@ export class SheetClipboardController extends Disposable {
@IConfigService private readonly _configService: IConfigService,
@ISheetClipboardService private readonly _sheetClipboardService: ISheetClipboardService,
@IMessageService private readonly _messageService: IMessageService,
+ @Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,
@Inject(Injector) private readonly _injector: Injector,
@Inject(LocaleService) private readonly _localService: LocaleService
) {
@@ -400,17 +404,41 @@ export class SheetClipboardController extends Disposable {
};
}
+ private _generateDocumentDataModelSnapshot(snapshot: Partial<IDocumentData>) {
+ const currentSkeleton = this._sheetSkeletonManagerService.getCurrent();
+ if (currentSkeleton == null) {
+ return null;
+ }
+ const { skeleton } = currentSkeleton;
+ const documentModel = skeleton.getBlankCellDocumentModel()?.documentModel;
+ const p = documentModel?.getSnapshot();
+ const documentData = { ...p, ...snapshot };
+ documentModel?.reset(documentData);
+ return documentModel?.getSnapshot();
+ }
+
private _onPastePlainText(pasteTo: ISheetRangeLocation, text: string, payload: ICopyPastePayload) {
const { range, unitId, subUnitId } = pasteTo;
- const cellValue: IObjectMatrixPrimitiveType<ICellData> = {
- [range.startRow]: {
- [range.startColumn]: {
- v: text,
+ let cellValue: IObjectMatrixPrimitiveType<ICellData>;
+ if (/\r|\n/.test(text)) {
+ const body = generateBody(text);
+ const p = this._generateDocumentDataModelSnapshot({ body });
+ cellValue = {
+ [range.startRow]: {
+ [range.startColumn]: {
+ p,
+ },
},
- },
- };
-
- const workbook = this._currentUniverSheet.getCurrentUniverSheetInstance();
+ };
+ } else {
+ cellValue = {
+ [range.startRow]: {
+ [range.startColumn]: {
+ v: text,
+ },
+ },
+ };
+ }
const setRangeValuesParams: ISetRangeValuesMutationParams = {
unitId,
diff --git a/packages/sheets-ui/src/controllers/clipboard/utils.ts b/packages/sheets-ui/src/controllers/clipboard/utils.ts
index 7308579d94..1ee9bda942 100644
--- a/packages/sheets-ui/src/controllers/clipboard/utils.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/utils.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { ICellData, IMutationInfo, IRange, Nullable } from '@univerjs/core';
+import type { ICellData, IDocumentBody, IMutationInfo, IParagraph, IRange, Nullable } from '@univerjs/core';
import { IUniverInstanceService, ObjectMatrix, Range, Rectangle } from '@univerjs/core';
import type {
IAddWorksheetMergeMutationParams,
@@ -522,3 +522,24 @@ export function getClearAndSetMergeMutations(
return { undos: undoMutationsInfo, redos: redoMutationsInfo };
}
+
+export function generateBody(text: string): IDocumentBody {
+ // Convert all \n to \r, because we use \r to indicate paragraph break.
+ let dataStream = text.replace(/(\r\n|\n)/g, '\r');
+ if (!dataStream.endsWith('\r\n')) {
+ dataStream += '\r\n';
+ }
+ const paragraphs: IParagraph[] = [];
+
+ for (let i = 0; i < dataStream.length; i++) {
+ if (dataStream[i] === '\r') {
+ paragraphs.push({ startIndex: i });
+ }
+ }
+
+ return {
+ dataStream,
+ paragraphs,
+ sectionBreaks: [{ startIndex: dataStream.indexOf('\n') }],
+ };
+}
diff --git a/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts b/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
index 2cd43947ee..a17c30aacf 100644
--- a/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
+++ b/packages/sheets-ui/src/services/clipboard/html-to-usm/converter.ts
@@ -18,6 +18,7 @@ import type { IDocumentBody, IDocumentData, ITextRun, ITextStyle, Nullable } fro
import { ObjectMatrix } from '@univerjs/core';
import { handleStringToStyle } from '@univerjs/ui';
+import { generateBody } from '../../../controllers/clipboard/utils';
import type { ISheetSkeletonManagerParam } from '../../sheet-skeleton-manager.service';
import type {
ICellDataWithSpanInfo,
@@ -221,12 +222,26 @@ export class HtmlToUSMService {
// TODO@Dushusir Temporarily use handleStringToStyle. After all replication and paste function is completed, fix the handleStringToStyle method
const style = handleStringToStyle(undefined, value.properties?.style);
- valueMatrix.setValue(row, col, {
- v: value.content,
- s: style,
- rowSpan: value.rowSpan,
- colSpan: value.colSpan,
- });
+ if (/\r|\n/.test(value.content)) {
+ const body = generateBody(value.content);
+ const p = this._generateDocumentDataModelSnapshot({
+ body,
+ });
+ valueMatrix.setValue(row, col, {
+ v: value.content,
+ p,
+ s: style,
+ rowSpan: value.rowSpan,
+ colSpan: value.colSpan,
+ });
+ } else {
+ valueMatrix.setValue(row, col, {
+ v: value.content,
+ s: style,
+ rowSpan: value.rowSpan,
+ colSpan: value.colSpan,
+ });
+ }
});
return {
rowProperties: [],
diff --git a/packages/sheets-ui/src/services/clipboard/usm-to-html/convertor.ts b/packages/sheets-ui/src/services/clipboard/usm-to-html/convertor.ts
index f7b644b92a..3202a75caa 100644
--- a/packages/sheets-ui/src/services/clipboard/usm-to-html/convertor.ts
+++ b/packages/sheets-ui/src/services/clipboard/usm-to-html/convertor.ts
@@ -120,7 +120,7 @@ export class USMToHtmlService {
range: IRange,
hooks: ISheetClipboardHook[]
): string {
- const { startColumn, endColumn, startRow, endRow } = range;
+ const { startColumn, endColumn } = range;
const colStyles = getColStyle(getArrayFromTo(startColumn, endColumn), hooks);
// row styles and table contents
const rowContents: string[] = [];