mirror of
https://github.com/ruilisi/fortune-sheet.git
synced 2025-01-05 10:27:04 +08:00
feat: api for hide/show rows and cols
This commit is contained in:
parent
0e4d0ba6bf
commit
9fe2294bb4
@ -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>,
|
||||
|
@ -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 = {},
|
||||
|
@ -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[]>([
|
||||
|
Loading…
Reference in New Issue
Block a user