Skip to content
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
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fileverse-dev/fortune-core",
"version": "1.3.10",
"version": "1.3.11",
"main": "lib/index.js",
"module": "es/index.js",
"typings": "lib/index.d.ts",
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/api/range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ export function setCellValuesByRange(
throw new Error("data size does not match range");
}

const sheet = getSheet(ctx, options);
const sheetId = sheet.id || ctx.currentSheetId;
const cellChanges: {
sheetId: string;
path: string[];
key?: string;
value: any;
type?: "update" | "delete";
}[] = [];

for (let i = 0; i < rowCount; i += 1) {
for (let j = 0; j < columnCount; j += 1) {
const row = range.row[0] + i;
Expand All @@ -133,8 +143,21 @@ export function setCellValuesByRange(
options,
callAfterUpdate
);
if (ctx?.hooks?.updateCellYdoc && sheet.data) {
cellChanges.push({
sheetId,
path: ["celldata"],
value: { r: row, c: column, v: sheet.data?.[row]?.[column] ?? null },
key: `${row}_${column}`,
type: "update",
});
}
}
}

if (cellChanges.length > 0 && ctx?.hooks?.updateCellYdoc) {
ctx.hooks.updateCellYdoc(cellChanges);
}
}

export function setCellFormatByRange(
Expand Down
30 changes: 30 additions & 0 deletions packages/core/src/api/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CellMatrix, CellWithRowAndCol, Sheet } from "../types";
import { getSheetIndex } from "../utils";
import {
api,
changeSheet,
execfunction,
getFlowdata,
insertUpdateFunctionGroup,
Expand Down Expand Up @@ -229,6 +230,35 @@ export function copySheet(ctx: Context, sheetId: string) {
const sheetOrderList: Record<string, number> = {};
sheetOrderList[newSheetId] = order;
api.setSheetOrder(ctx, sheetOrderList);

// Make the duplicated sheet active before doing any full-sheet external sync.
// Some integrations derive the active sheet from ctx.currentSheetId.
changeSheet(ctx, newSheetId, undefined, true, true);

// Duplicating a sheet bypasses per-cell update flows; ensure external sync (e.g. Yjs) gets a full snapshot.
// Prefer updateAllCell for performance; otherwise emit celldata updates for the new sheet.
if (ctx?.hooks?.updateAllCell) {
ctx.hooks.updateAllCell(newSheetId);
} else if (ctx?.hooks?.updateCellYdoc) {
const changes: any[] = [];
const celldata = newSheet.celldata || dataToCelldata(newSheet.data);
if (Array.isArray(celldata)) {
celldata.forEach((d: any) => {
if (d == null) return;
const { r } = d;
const { c } = d;
if (!_.isNumber(r) || !_.isNumber(c)) return;
changes.push({
sheetId: newSheetId,
path: ["celldata"],
key: `${r}_${c}`,
value: { r, c, v: d.v ?? null },
type: d.v == null ? "delete" : "update",
});
});
}
if (changes.length > 0) ctx.hooks.updateCellYdoc(changes);
}
}

export function calculateSheetFromula(ctx: Context, id: string) {
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/events/keyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,27 +650,41 @@ export async function handleGlobalKeyDown(
e.preventDefault();
return;
}
let handledFlvShortcut = false;
if ((e.ctrlKey || (e.metaKey && e.shiftKey)) && e.code === "KeyE") {
textFormat(ctx, "center");
handledFlvShortcut = true;
} else if ((e.ctrlKey || (e.metaKey && e.shiftKey)) && e.code === "KeyL") {
textFormat(ctx, "left");
handledFlvShortcut = true;
} else if ((e.ctrlKey || (e.metaKey && e.shiftKey)) && e.code === "KeyR") {
textFormat(ctx, "right");
handledFlvShortcut = true;
}
if ((e.metaKey || e.ctrlKey) && e.code === "KeyK") {
handleLink(ctx, cellInput);
}
if ((e.metaKey || e.ctrlKey) && !e.shiftKey && e.code === "Semicolon") {
fillDate(ctx);
handledFlvShortcut = true;
}
if ((e.metaKey || e.ctrlKey) && e.shiftKey && e.code === "Semicolon") {
fillTime(ctx);
handledFlvShortcut = true;
}
if ((e.metaKey || e.ctrlKey) && !e.shiftKey && e.code === "KeyR") {
fillRightData(ctx);
handledFlvShortcut = true;
}
if ((e.metaKey || e.ctrlKey) && e.code === "KeyD") {
fillDownData(ctx);
handledFlvShortcut = true;
}
if (handledFlvShortcut) {
jfrefreshgrid(ctx, null, undefined);
e.stopPropagation();
e.preventDefault();
return;
}
/* FLV */

Expand Down
105 changes: 79 additions & 26 deletions packages/core/src/events/paste.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,20 @@ const handleFormulaOnPaste = (ctx: Context, d: any) => {
cell.f = f;
cell.m = v.toString();
x[c] = cell;

changes.push({
sheetId: ctx.currentSheetId,
path: ["celldata"],
value: {
r,
c,
v: d[r][c],
},
key: `${r}_${c}`,
type: "update",
});
}
d[r] = x;
changes.push({
sheetId: ctx.currentSheetId,
path: ["celldata"],
value: {
r,
c,
v: d[r][c],
},
key: `${r}_${c}`,
type: "update",
});
}
}
if (ctx?.hooks?.updateCellYdoc) {
Expand Down Expand Up @@ -590,7 +591,9 @@ function pasteHandler(ctx: Context, data: any, borderInfo?: any) {
// selectHightlightShow();
}
jfrefreshgrid(ctx, null, undefined);
handleFormulaOnPaste(ctx, d);
if (data.includes("=")) {
handleFormulaOnPaste(ctx, d);
}
// for (let r = 0; r < d.length; r += 1) {
// const x = d[r];
// for (let c = 0; c < d[0].length; c += 1) {
Expand Down Expand Up @@ -806,19 +809,17 @@ function pasteHandler(ctx: Context, data: any, borderInfo?: any) {

x[c + curC] = cell;
}
changes.push(
changes.push({
sheetId: ctx.currentSheetId,
path: ["celldata"],
value: {
r,
c,
v: d[r][c],
},
key: `${r}_${c}`,
type: "update",
})
);
changes.push({
sheetId: ctx.currentSheetId,
path: ["celldata"],
value: {
r: r + curR,
c: c + curC,
v: d[r + curR][c + curC],
},
key: `${r + curR}_${c + curC}`,
type: "update",
});
}
d[r + curR] = x;
}
Expand All @@ -840,7 +841,9 @@ function pasteHandler(ctx: Context, data: any, borderInfo?: any) {
// selectHightlightShow();
// }
jfrefreshgrid(ctx, null, undefined);
handleFormulaOnPaste(ctx, d);
if (data.includes("=")) {
handleFormulaOnPaste(ctx, d);
}

// for (let r = 0; r < d.length; r += 1) {
// const x = d[r];
Expand Down Expand Up @@ -956,6 +959,14 @@ function pasteHandlerOfCutPaste(
expandRowsAndColumns(d, addr, addc);
}

const changes: {
sheetId: string;
path: string[];
value: any;
key: string;
type: "update";
}[] = [];

const borderInfoCompute = getBorderInfoCompute(ctx, copySheetId);
const c_dataVerification =
_.cloneDeep(
Expand Down Expand Up @@ -1016,6 +1027,13 @@ function pasteHandlerOfCutPaste(
}

d[i][j] = null;
changes.push({
sheetId: ctx.currentSheetId,
path: ["celldata"],
value: { r: i, c: j, v: null },
key: `${i}_${j}`,
type: "update",
});

delete dataVerification[`${i}_${j}`];

Expand Down Expand Up @@ -1145,6 +1163,13 @@ function pasteHandlerOfCutPaste(
}

x[c] = _.cloneDeep(value);
changes.push({
sheetId: ctx.currentSheetId,
path: ["celldata"],
value: { r: h, c, v: d[h][c] },
key: `${h}_${c}`,
type: "update",
});

if (value != null && copyHasMC && x[c]?.mc) {
if (x[c]!.mc!.rs != null) {
Expand Down Expand Up @@ -1175,6 +1200,10 @@ function pasteHandlerOfCutPaste(
last.row = [minh, maxh];
last.column = [minc, maxc];

if (changes.length > 0 && ctx?.hooks?.updateCellYdoc) {
ctx.hooks.updateCellYdoc(changes);
}

// 若有行高改变,重新计算行高改变
if (copyRowlChange) {
// if (ctx.currentSheetId !== copySheetIndex) {
Expand Down Expand Up @@ -1575,6 +1604,14 @@ function pasteHandlerOfCopyPaste(
expandRowsAndColumns(d, addr, addc);
}

const changes: {
sheetId: string;
path: string[];
value: any;
key: string;
type: "update";
}[] = [];

const borderInfoCompute = getBorderInfoCompute(ctx, copySheetIndex);
const c_dataVerification =
_.cloneDeep(
Expand Down Expand Up @@ -1700,6 +1737,7 @@ function pasteHandlerOfCopyPaste(
}
}

let afterHookCalled = false;
if (!_.isNil(value) && !_.isNil(value.f)) {
let adjustedFormula = value.f;
let isError = false;
Expand Down Expand Up @@ -1783,6 +1821,7 @@ function pasteHandlerOfCopyPaste(
m:
funcV[1] instanceof Promise ? "[object Promise]" : funcV[1],
});
afterHookCalled = true;
}
}

Expand Down Expand Up @@ -1825,6 +1864,16 @@ function pasteHandlerOfCopyPaste(
};
}
}
// If afterUpdateCell ran for this cell, it is expected to handle Yjs sync.
if (!(ctx?.hooks?.afterUpdateCell && afterHookCalled)) {
changes.push({
sheetId: ctx.currentSheetId,
path: ["celldata"],
value: { r: h, c, v: d[h][c] },
key: `${h}_${c}`,
type: "update",
});
}
}
d[h] = x;
}
Expand Down Expand Up @@ -1973,6 +2022,10 @@ function pasteHandlerOfCopyPaste(
}
}

if (changes.length > 0 && ctx?.hooks?.updateCellYdoc) {
ctx.hooks.updateCellYdoc(changes);
}

if (copyRowlChange || addr > 0 || addc > 0) {
// cfg = rowlenByRange(d, minh, maxh, cfg);
// const allParam = {
Expand Down
Loading
Loading