feat: api for hide/show rows and cols

This commit is contained in:
Sanchit Agarwal 2024-09-23 09:08:54 +00:00
parent 0e4d0ba6bf
commit 9fe2294bb4
3 changed files with 194 additions and 0 deletions

View File

@ -3,6 +3,7 @@ import { Context } from "../context";
import { deleteRowCol, insertRowCol } from "../modules";
import { CommonOptions, getSheet } from "./common";
import { INVALID_PARAMS } from "./errors";
import { getSheetIndex } from "../utils";
export function freeze(
ctx: Context,
@ -81,6 +82,122 @@ export function deleteRowOrColumn(
deleteRowCol(ctx, { type, start, end, id: sheet.id! });
}
export function hideRowOrColumn(
ctx: Context,
rowColInfo: string[],
type: "row" | "column"
) {
if (!["row", "column"].includes(type)) {
throw INVALID_PARAMS;
}
if (!ctx || !ctx.config) return;
const index = getSheetIndex(ctx, ctx.currentSheetId) as number;
if (type === "row") {
/* TODO:
if (
!checkProtectionAuthorityNormal(Store.currentSheetIndex, "formatRows")
) {
return ;
} */
const rowhidden = ctx.config.rowhidden ?? {};
rowColInfo.forEach((r) => {
rowhidden[r] = 0;
});
/* ,luck
if(Store.clearjfundo){
let redo = {};
redo["type"] = "showHidRows";
redo["sheetIndex"] = Store.currentSheetIndex;
redo["config"] = $.extend(true, {}, Store.config);
redo["curconfig"] = cfg;
Store.jfundo.length = 0;
Store.jfredo.push(redo);
} */
ctx.config.rowhidden = rowhidden;
// const rowLen = ctx.luckysheetfile[index].data!.length;
/**
*
* index===-1
* -1===index
*/
} else if (type === "column") {
// 隐藏列
const colhidden = ctx.config.colhidden ?? {};
rowColInfo.forEach((r) => {
colhidden[r] = 0;
});
ctx.config.colhidden = colhidden;
// const columnLen = ctx.luckysheetfile[index].data![0].length;
}
ctx.luckysheetfile[index].config = ctx.config;
}
export function showRowOrColumn(
ctx: Context,
rowColInfo: string[],
type: "row" | "column"
) {
if (!["row", "column"].includes(type)) {
throw INVALID_PARAMS;
}
if (!ctx || !ctx.config) return;
const index = getSheetIndex(ctx, ctx.currentSheetId) as number;
if (type === "row") {
/* TODO:
if (
!checkProtectionAuthorityNormal(Store.currentSheetIndex, "formatRows")
) {
return ;
} */
const rowhidden = ctx.config.rowhidden ?? {};
rowColInfo.forEach((r) => {
delete rowhidden[r];
});
/* ,luck
if(Store.clearjfundo){
let redo = {};
redo["type"] = "showHidRows";
redo["sheetIndex"] = Store.currentSheetIndex;
redo["config"] = $.extend(true, {}, Store.config);
redo["curconfig"] = cfg;
Store.jfundo.length = 0;
Store.jfredo.push(redo);
} */
ctx.config.rowhidden = rowhidden;
// const rowLen = ctx.luckysheetfile[index].data!.length;
/**
*
* index===-1
* -1===index
*/
} else if (type === "column") {
// 隐藏列
const colhidden = ctx.config.colhidden ?? {};
rowColInfo.forEach((r) => {
delete colhidden[r];
});
ctx.config.colhidden = colhidden;
// const columnLen = ctx.luckysheetfile[index].data![0].length;
}
ctx.luckysheetfile[index].config = ctx.config;
}
export function setRowHeight(
ctx: Context,
rowInfo: Record<string, number>,

View File

@ -170,6 +170,16 @@ export function generateAPIs(
api.deleteRowOrColumn(draftCtx, type, start, end, options)
),
hideRowOrColumn: (rowOrColInfo: string[], type: "row" | "column") =>
setContext((draftCtx) =>
api.hideRowOrColumn(draftCtx, rowOrColInfo, type)
),
showRowOrColumn: (rowOrColInfo: string[], type: "row" | "column") =>
setContext((draftCtx) =>
api.showRowOrColumn(draftCtx, rowOrColInfo, type)
),
setRowHeight: (
rowInfo: Record<string, number>,
options: api.CommonOptions = {},

View File

@ -363,6 +363,73 @@ export const SetRowHeight: StoryFn<typeof Workbook> = () => {
);
};
export const HideRow: StoryFn<typeof Workbook> = () => {
const ref = useRef<WorkbookInstance>(null);
const [data, setData] = useState<Sheet[]>([
{
name: "Sheet1",
celldata: [
{ r: 0, c: 0, v: { v: "0" } },
{ r: 1, c: 0, v: { v: "1" } },
{ r: 2, c: 0, v: { v: "hide this!" } },
{ r: 3, c: 0, v: { v: "3" } },
{ r: 4, c: 0, v: { v: "4" } },
],
order: 0,
row: 5,
column: 1,
},
]);
const onChange = useCallback((d: Sheet[]) => {
setData(d);
}, []);
return (
<ApiExecContainer
onRun={() => {
ref.current?.hideRowOrColumn(["2"], "row");
}}
>
<Workbook ref={ref} data={data} onChange={onChange} />
</ApiExecContainer>
);
};
export const ShowRow: StoryFn<typeof Workbook> = () => {
const ref = useRef<WorkbookInstance>(null);
const [data, setData] = useState<Sheet[]>([
{
name: "Sheet1",
celldata: [
{ r: 0, c: 0, v: { v: "0" } },
{ r: 1, c: 0, v: { v: "1" } },
{ r: 2, c: 0, v: { v: "show this" } },
{ r: 3, c: 0, v: { v: "3" } },
{ r: 4, c: 0, v: { v: "4" } },
],
config: {
rowhidden: {
"2": 0,
},
},
order: 0,
row: 5,
column: 1,
},
]);
const onChange = useCallback((d: Sheet[]) => {
setData(d);
}, []);
return (
<ApiExecContainer
onRun={() => {
ref.current?.showRowOrColumn(["2"], "row");
}}
>
<Workbook ref={ref} data={data} onChange={onChange} />
</ApiExecContainer>
);
};
export const SetColumnWidth: StoryFn<typeof Workbook> = () => {
const ref = useRef<WorkbookInstance>(null);
const [data, setData] = useState<Sheet[]>([