PluginProbe
Tableberg – Simple Gutenberg Table Block / 1.1.5
Tableberg – Simple Gutenberg Table Block v1.1.5
1.1.5 1.1.4 1.1.3 1.1.2 1.1.1 1.1.0 1.0.5 1.0.4 1.0.3 1.0.2 1.0.1 trunk 0.0.2 0.2.1 0.3.2 0.3.3 0.4.1 0.5.0 0.5.1 0.5.2 0.5.3 0.5.4 0.5.5 0.5.6 0.5.7 All 42 releases
tableberg / src / pagination.ts

pagination.ts in Tableberg – Simple Gutenberg Table Block 1.1.5, at src/pagination.ts

60 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { Cell, CellKey } from "./attributes";
2
3 export function tableHasRowSpanningCells(
4 cells: Record<CellKey, Cell>
5 ): boolean {
6 return Object.values(cells).some(cell => (cell.span?.rowSpan || 1) > 1);
7 }
8
9 export function getTotalPages(
10 totalRows: number,
11 pageSize: number,
12 headerEnabled: boolean,
13 footerEnabled: boolean
14 ): number {
15 let dataRows = totalRows;
16 if (headerEnabled) dataRows--;
17 if (footerEnabled) dataRows--;
18
19 if (dataRows <= 0) return 1;
20 if (pageSize <= 0) return 1;
21
22 return Math.ceil(dataRows / pageSize);
23 }
24
25 export function getPagedRowIndices(
26 totalRows: number,
27 pageSize: number,
28 currentPage: number,
29 headerEnabled: boolean,
30 footerEnabled: boolean
31 ): number[] {
32 const result: number[] = [];
33
34 if (headerEnabled) {
35 result.push(0);
36 }
37
38 const dataStartRow = headerEnabled ? 1 : 0;
39 const dataEndRow = footerEnabled ? totalRows - 2 : totalRows - 1;
40 const dataRowCount = dataEndRow - dataStartRow + 1;
41
42 if (dataRowCount > 0 && pageSize > 0) {
43 const pageStartIndex = currentPage * pageSize;
44 const pageEndIndex = Math.min(
45 pageStartIndex + pageSize - 1,
46 dataRowCount - 1
47 );
48
49 for (let i = pageStartIndex; i <= pageEndIndex; i++) {
50 result.push(dataStartRow + i);
51 }
52 }
53
54 if (footerEnabled) {
55 result.push(totalRows - 1);
56 }
57
58 return result;
59 }
60