fix: add row when pasting too many rows in sync

This commit is contained in:
gazedreamily 2023-02-28 15:55:34 +08:00 committed by Qaplagzy
parent e4794f7d0c
commit ba92a6b1a3
2 changed files with 53 additions and 44 deletions

View File

@ -169,39 +169,13 @@ export function patchToOp(
options?: PatchOptions,
undo: boolean = false
): Op[] {
let ops: Op[] = [];
const maxRow: Record<string, number> = {};
_.forEach(patches, (p) => {
let ops = patches.map((p) => {
const op: Op = {
op: p.op,
value: p.value,
path: p.path,
};
if (
p.path[0] === "luckysheetfile" &&
_.isNumber(p.path[1]) &&
op.op === "add" &&
op.path.length === 4 &&
op.path[2] === "data" &&
_.isNumber(p.path[3])
) {
const id = ctx.luckysheetfile[p.path[1]].id!;
if (_.isNil(maxRow[id])) {
maxRow[id] = ctx.luckysheetfile[getSheetIndex(ctx, id) as number]
.row as number;
}
maxRow[id] = maxRow[id] > p.path[3] + 1 ? maxRow[id] : p.path[3] + 1;
for (let c = 0; c < op.value.length!; c += 1) {
if (op.value[c] !== null) {
ops.push({
op: "replace",
id,
path: ["data", op.path[3], c],
value: op.value[c],
});
}
}
} else if (p.path[0] === "luckysheetfile" && _.isNumber(p.path[1])) {
if (p.path[0] === "luckysheetfile" && _.isNumber(p.path[1])) {
const id = ctx.luckysheetfile[p.path[1]].id!;
op.id = id;
op.path = p.path.slice(2);
@ -209,22 +183,8 @@ export function patchToOp(
op.path = ["calcChain"];
op.value = ctx.luckysheetfile[p.path[1]].calcChain;
}
ops.push(op);
} else {
ops.push(op);
}
});
_.forEach(_.keys(maxRow), (id) => {
if (
ctx.luckysheetfile[getSheetIndex(ctx, id) as number].row !== maxRow[id]
) {
ops.push({
op: "replace",
id,
path: ["row"],
value: maxRow[id],
});
}
return op;
});
_.every(ops, (p) => {
if (

View File

@ -16,6 +16,7 @@ import {
inverseRowColOptions,
ensureSheetIndex,
CellMatrix,
insertRowCol,
} from "@fortune-sheet/core";
import React, {
useMemo,
@ -531,6 +532,54 @@ const Workbook = React.forwardRef<WorkbookInstance, Settings & AdditionalProps>(
cellInput.current === document.activeElement ||
document.activeElement?.className === "fortune-sheet-overlay"
) {
let { clipboardData } = e;
if (!clipboardData) {
// @ts-ignore
// for IE
clipboardData = window.clipboardData;
}
const txtdata =
clipboardData!.getData("text/html") ||
clipboardData!.getData("text/plain");
const ele = document.createElement("div");
ele.innerHTML = txtdata;
const trList = ele.querySelectorAll("table tr");
const maxRow =
trList.length + context.luckysheet_select_save![0].row[0];
const rowToBeAdded =
maxRow -
context.luckysheetfile[
getSheetIndex(
context,
context!.currentSheetId! as string
) as number
].data!.length;
const range = context.luckysheet_select_save;
if (rowToBeAdded > 0) {
const insertRowColOp: SetContextOptions["insertRowColOp"] = {
type: "row",
index:
context.luckysheetfile[
getSheetIndex(
context,
context!.currentSheetId! as string
) as number
].data!.length - 1,
count: rowToBeAdded,
direction: "rightbottom",
id: context.currentSheetId,
};
setContextWithProduce(
(draftCtx) => {
insertRowCol(draftCtx, insertRowColOp);
draftCtx.luckysheet_select_save = range;
},
{
insertRowColOp,
}
);
}
setContextWithProduce((draftCtx) => {
try {
handlePaste(draftCtx, e);
@ -540,7 +589,7 @@ const Workbook = React.forwardRef<WorkbookInstance, Settings & AdditionalProps>(
});
}
},
[setContextWithProduce]
[context, setContextWithProduce]
);
const onMoreToolbarItemsClose = useCallback(() => {