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 border should cover origin border (#4321)

ybzky7cdd2e624fe4
diff --git a/packages/core/src/types/const/const.ts b/packages/core/src/types/const/const.ts
index de9f5fb8f1..ed4412700d 100644
--- a/packages/core/src/types/const/const.ts
+++ b/packages/core/src/types/const/const.ts
@@ -154,6 +154,15 @@ export const DEFAULT_STYLES = {
l: 0,
},
n: null,
+ /**
+ * border
+ */
+ bd: {
+ b: null,
+ l: null,
+ r: null,
+ t: null,
+ },
};
export const DEFAULT_SLIDE = {
diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
index 13a5b05d8f..33b7e2268c 100644
--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts
@@ -233,37 +233,8 @@ export class SheetClipboardController extends RxDisposable {
const mergedCellByRowCol = currentSheet!.getMergedCell(row, col);
const textStyle = range.getTextStyle();
- // const color = range.getFontColor();
- // const backgroundColor = range.getBackground();
let style = '';
- // if (color) {
- // style += `color: ${color};`;
- // }
- // if (backgroundColor) {
- // style += `background-color: ${backgroundColor};`;
- // }
- // if (textStyle?.bl) {
- // style += 'font-weight: bold;';
- // }
- // if (textStyle?.fs) {
- // style += `font-size: ${textStyle.fs}px;`;
- // }
- // if (textStyle?.tb === WrapStrategy.WRAP) {
- // style += 'word-wrap: break-word;';
- // }
- // if (textStyle?.it) {
- // style += 'font-style: italic;';
- // }
- // if (textStyle?.ff) {
- // style += `font-family: ${textStyle.ff};`;
- // }
- // if (textStyle?.st) {
- // style += 'text-decoration: line-through;';
- // }
- // if (textStyle?.ul) {
- // style += 'text-decoration: underline';
- // }
if (textStyle) {
style = handleStyleToString(textStyle);
@@ -288,7 +259,7 @@ export class SheetClipboardController extends RxDisposable {
properties.style = style;
}
- return properties;
+ return Object.keys(properties).length ? properties : null;
},
onCopyColumn(col: number) {
const sheet = currentSheet!;
diff --git a/packages/sheets-ui/src/controllers/clipboard/utils.ts b/packages/sheets-ui/src/controllers/clipboard/utils.ts
index 2e9b767fc1..2bcb82ecea 100644
--- a/packages/sheets-ui/src/controllers/clipboard/utils.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/utils.ts
@@ -14,7 +14,9 @@
* limitations under the License.
*/
-import type { IAccessor, ICellData, ICustomRange, IDocumentBody, IMutationInfo, IParagraph, IRange, Nullable } from '@univerjs/core';
+/* eslint-disable max-lines-per-function */
+
+import type { IAccessor, IBorderData, ICellData, ICustomRange, IDocumentBody, IMutationInfo, IParagraph, IRange, IStyleData, Nullable } from '@univerjs/core';
import type {
IAddWorksheetMergeMutationParams,
IMoveRangeMutationParams,
@@ -99,7 +101,6 @@ export function getDefaultOnPasteCellMutations(
};
}
-// eslint-disable-next-line max-lines-per-function
export function getMoveRangeMutations(
from: {
unitId: string;
@@ -418,6 +419,22 @@ export function getSetCellStyleMutations(
cl: null,
}, value.s),
};
+
+ // Here I don't know why when setting the border, an empty object is also assigned to the adjacent cells without borders.
+ // This is the fundamental cause of the problem. This should be unreasonable, so I bypassed this problem first.
+ const cellBd = (newValue.s as IStyleData).bd as IBorderData;
+ if (cellBd) {
+ const isValid = Object.keys(cellBd).length > 0;
+ if (!isValid) {
+ (newValue.s as IStyleData)!.bd = {
+ b: null,
+ l: null,
+ r: null,
+ t: null,
+ };
+ }
+ }
+
const content = String(value.v);
const numfmtValue = numfmt.parseValue(content);
if (numfmtValue?.z) {
@@ -474,14 +491,12 @@ export function getClearCellStyleMutations(
const { unitId, subUnitId, range } = pasteTo;
const { mapFunc } = virtualizeDiscreteRanges([range]);
- matrix.forValue((row, col, value) => {
- // NOTE: When pasting, the original cell may contain a default style that is not explicitly carried, resulting in the failure to overwrite the style of the target cell.
- // If the original cell has a style (lack of other default styles) or is undefined (all default styles), we need to clear the existing styles in the target area
- // If the original cell style is "", it is to handle the situation where the target area contains merged cells. The style is not overwritten, only the value is overwritten. There is no need to clear the existing style of the target area.
- if (value.s) {
- const { row: actualRow, col: actualCol } = mapFunc(row, col);
+ matrix.forEach((rowIndex, row) => {
+ Object.keys(row).forEach((colIndexStr) => {
+ const colIndex = Number(colIndexStr);
+ const { row: actualRow, col: actualCol } = mapFunc(rowIndex, colIndex);
clearStyleMatrix.setValue(actualRow, actualCol, { s: null });
- }
+ });
});
// clear style
if (clearStyleMatrix.getLength() > 0) {
diff --git a/packages/sheets/src/basics/cell-style.ts b/packages/sheets/src/basics/cell-style.ts
index 816065d423..5f0bd58431 100644
--- a/packages/sheets/src/basics/cell-style.ts
+++ b/packages/sheets/src/basics/cell-style.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import { normalizeTextRuns, Tools } from '@univerjs/core';
import type { IBorderData, ICellData, IDocumentData, IKeyValue, IParagraph, IStyleData, ITextRun, ITextStyle, Nullable, Styles } from '@univerjs/core';
+import { normalizeTextRuns, Tools } from '@univerjs/core';
export function handleStyle(styles: Styles, oldVal: ICellData, newVal: ICellData) {
// use null to clear style
@@ -36,6 +36,13 @@ export function handleStyle(styles: Styles, oldVal: ICellData, newVal: ICellData
// then remove null
if (merge) {
Tools.removeNull(merge);
+
+ // remove empty object
+ Object.entries(merge).forEach(([key, val]) => {
+ if (typeof val === 'object' && val !== null && Object.keys(val).length === 0) {
+ delete merge[key as keyof IStyleData];
+ }
+ });
}
if (Tools.isEmptyObject(merge)) {