Skip to content

Commit 703e517

Browse files
committed
fix(table): style problem in table grid
1 parent e970969 commit 703e517

9 files changed

Lines changed: 332 additions & 183 deletions

File tree

src/app/globals.css

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454
"Courier New", monospace;
5555
--max-key-length: 300px;
5656
--max-value-length: 500px;
57-
--tbl-key-bg-color: rgb(242, 242, 242);
58-
--tbl-border: 1px solid #e0e0e080;
5957

6058
--bg-key: #e0e0e04d;
6159
--hl-key: #a31515;
@@ -296,19 +294,17 @@ https://stackoverflow.com/questions/47017753/monaco-editor-dynamically-resizable
296294
padding: 4px 6px;
297295
text-align: left;
298296
vertical-align: top;
297+
border-collapse: collapse;
299298
}
300299

301300
.tbl-header {
302-
max-width: var(--max-value-length);
303-
background-color: var(--tbl-key-bg-color);
304301
white-space: nowrap;
305302
overflow-x: hidden;
306303
text-overflow: ellipsis;
307304
font-weight: 400;
308305
}
309306

310307
.tbl-value {
311-
max-width: var(--max-value-length);
312308
white-space: nowrap;
313309
overflow-x: hidden;
314310
text-overflow: ellipsis;
@@ -318,13 +314,28 @@ https://stackoverflow.com/questions/47017753/monaco-editor-dynamically-resizable
318314
color: var(--hl-index);
319315
font-family: var(--graph-font-family);
320316
vertical-align: top;
321-
border: var(--tbl-border);
322317
}
323318

324319
.tbl-expander {
325320
cursor: pointer;
326321
}
327322

323+
.tbl-b-l {
324+
border-left-width: 1px;
325+
}
326+
327+
.tbl-b-r {
328+
border-right-width: 1px;
329+
}
330+
331+
.tbl-b-t {
332+
border-top-width: 1px;
333+
}
334+
335+
.tbl-b-b {
336+
border-bottom-width: 1px;
337+
}
338+
328339
.hide-scrollbar::-webkit-scrollbar {
329340
display: none;
330341
}

src/containers/editor/components/InitialSetup.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ function useInitial() {
5555

5656
// measure graph style
5757
const el = document.getElementById("width-measure")!;
58+
const { maxWidth: maxValueWidth } = getComputedStyle(el.querySelector(".graph-v")!);
5859

5960
{
6061
const span = el.querySelector("span")!;
6162
const { lineHeight } = getComputedStyle(span);
6263
const { borderWidth } = getComputedStyle(el);
6364
const { paddingLeft, paddingRight } = getComputedStyle(el.querySelector(".graph-kv")!);
6465
const { marginRight, maxWidth: maxKeyWidth } = getComputedStyle(el.querySelector(".graph-k")!);
65-
const { maxWidth: maxValueWidth } = getComputedStyle(el.querySelector(".graph-v")!);
6666

6767
const measured = {
6868
fontWidth: Math.ceil(span.offsetWidth / span.textContent!.length),
@@ -82,12 +82,13 @@ function useInitial() {
8282
{
8383
const span = el.querySelector("span")!;
8484
const { height: rowHeight } = getComputedStyle(el.querySelector(".tbl-row")!);
85-
const { maxWidth: maxCellWidth, paddingLeft, paddingRight } = getComputedStyle(el.querySelector(".tbl-cell")!);
85+
const { paddingLeft, paddingRight } = getComputedStyle(el.querySelector(".tbl-cell")!);
8686
const measured = {
8787
fontWidth: Math.ceil(span.offsetWidth / span.textContent!.length),
8888
rowHeight: px2num(rowHeight),
89-
maxCellWidth: px2num(maxCellWidth),
89+
maxCellWidth: px2num(maxValueWidth),
9090
padding: px2num(paddingLeft) + px2num(paddingRight),
91+
scrollbarWidth: getScrollbarWidth(),
9192
};
9293

9394
setupGlobalTableStyle(measured);
@@ -96,3 +97,20 @@ function useInitial() {
9697
}
9798
}, []);
9899
}
100+
101+
// Calculate the scrollbar width
102+
function getScrollbarWidth() {
103+
const outer = document.createElement("div");
104+
outer.style.width = "100px";
105+
outer.style.height = "100px";
106+
outer.style.overflow = "scroll";
107+
document.body.appendChild(outer);
108+
109+
const inner = document.createElement("div");
110+
inner.style.width = "100%";
111+
outer.appendChild(inner);
112+
113+
const scrollbarWidth = outer.offsetWidth - inner.offsetWidth;
114+
document.body.removeChild(outer);
115+
return scrollbarWidth;
116+
}

src/containers/editor/table/Cell.tsx

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,45 @@
11
import { memo } from "react";
2-
import { cellClassMap, globalStyle } from "@/lib/table/style";
2+
import { borderClassMap, cellClassMap, globalStyle } from "@/lib/table/style";
3+
import { isDummyType } from "@/lib/table/tableNode";
34
import type { TableNode } from "@/lib/table/types";
45
import { cn } from "@/lib/utils";
56

67
interface CellProps extends Omit<TableNode, "next" | "heads"> {
7-
index: number; // row number
8+
rowInTable: number;
9+
colInTable: number;
810
}
911

12+
const headerBgClassNames = ["bg-stone-300", "bg-stone-100"];
13+
1014
const Cell = memo((props: CellProps) => {
11-
const isDummy = props.row !== props.index;
15+
const isDummy = isDummyType(props.type);
16+
const classNames = props.classNames ?? [];
17+
const isFirstRow = props.row === props.rowInTable;
18+
const isLastRow = props.row + props.span - 1 === props.rowInTable;
19+
const bgClassName =
20+
props.type === "header" || props.type === "dummyHeader" ? headerBgClassNames[props.level % 2] : "";
1221

1322
return (
14-
<span
15-
className={cn("tbl-cell", cellClassMap[props.type], ...(props.classNames ?? []))}
16-
style={{ width: `${props.width}px`, height: isDummy ? `${globalStyle.rowHeight}px` : undefined }}
23+
<div
24+
id={props.id}
25+
data-type={props.type}
26+
data-position={`${props.rowInTable},${props.colInTable}`}
27+
data-level={props.level}
28+
className={cn(
29+
"tbl-cell tbl-b-l tbl-b-r",
30+
isFirstRow ? "tbl-b-t hover:bg-blue-100 dark:hover:bg-blue-900" : "",
31+
isLastRow ? "tbl-b-b" : "",
32+
bgClassName,
33+
cellClassMap[props.type],
34+
...classNames,
35+
)}
36+
style={{
37+
width: `${props.width}px`,
38+
height: isFirstRow ? undefined : `${globalStyle.rowHeight}px`,
39+
}}
1740
>
1841
{isDummy ? undefined : props.text}
19-
</span>
42+
</div>
2043
);
2144
});
2245
Cell.displayName = "Cell";

src/containers/editor/table/Table.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import { useRef } from "react";
44
import Background from "@/components/Background";
55
import { globalStyle } from "@/lib/table/style";
6-
import { getRow } from "@/lib/table/tableNode";
76
import { useVirtualizer } from "@tanstack/react-virtual";
87
import Cell from "./Cell";
98
import { useOnResize } from "./useOnResize";
@@ -16,7 +15,7 @@ export function Table() {
1615
const tableTree = useTableTree();
1716
const { width, height } = useOnResize(containerRef, tableTree);
1817
const virtualizer = useVirtualizer({
19-
count: tableTree.root.span,
18+
count: tableTree.grid.length,
2019
getScrollElement: () => virtualRef.current,
2120
estimateSize: (i) => globalStyle.rowHeight,
2221
overscan: 10,
@@ -27,20 +26,24 @@ export function Table() {
2726
<div
2827
ref={virtualRef}
2928
className="bg-white"
30-
style={{ width: `${width}px`, height: `${height}px`, overflow: "auto" }}
29+
style={{
30+
width: `${width + globalStyle.scrollbarWidth}px`,
31+
height: `${height}px`,
32+
overflow: "auto",
33+
}}
3134
>
3235
<div style={{ height: `${virtualizer.getTotalSize()}px`, width: "100%", position: "relative" }}>
33-
{virtualizer.getVirtualItems().map(({ index, start }) => (
36+
{virtualizer.getVirtualItems().map(({ index: row, start }) => (
3437
<div
35-
key={index}
38+
key={row}
3639
className="tbl-row"
3740
style={{
3841
height: globalStyle.rowHeight,
3942
transform: `translateY(${start}px)`,
4043
}}
4144
>
42-
{getRow(tableTree.root, index).map((nd) => (
43-
<Cell key={nd.id} {...nd} index={index} />
45+
{tableTree.grid[row].map((nd, col) => (
46+
<Cell key={`${row}-${col}`} {...nd} rowInTable={row} colInTable={col} />
4447
))}
4548
</div>
4649
))}

src/containers/editor/table/useTableTree.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ export function useTableTree() {
3838
(async () => {
3939
const t = await window.worker.createTable();
4040
setTableTree(t);
41-
console.l(
42-
"create a new table:",
43-
treeVersion,
44-
nodeTo2dArray(t.root, (nd, row) => (nd.row === row ? nd.text : "")),
45-
);
41+
console.l("create a new table:", treeVersion, t);
4642
t.width && count("tableModeView");
4743
})();
4844
}, [usable, isTableView, treeVersion, setTableTree]);

0 commit comments

Comments
 (0)