Skip to content

Commit 73f2198

Browse files
committed
feat: make tables prettier
1 parent ec5dd2a commit 73f2198

3 files changed

Lines changed: 73 additions & 31 deletions

File tree

packages/context-editor/src/commands/tables.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ type AddTableOptions = {
2828
};
2929

3030
const defaultOptions = {
31-
rowCount: 2,
32-
colCount: 2,
31+
rowCount: 3,
32+
colCount: 3,
3333
withHeaderRow: true,
3434
cellContent: undefined,
3535
};

packages/context-editor/src/plugins/structureDecorations.tsx

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,39 @@ export default () => {
99
return new Plugin({
1010
props: {
1111
decorations: (state) => {
12+
const counts = new Map<string, number>();
1213
const decorations: Decoration[] = [];
1314
state.doc.descendants((node, pos) => {
14-
if (
15-
node.type.name === "table_row" ||
16-
node.type.name === "table_cell" ||
17-
node.type.name === "table_header"
18-
) {
19-
// Don't add decorations to table rows or cells
15+
let nodeIsDescendantOfTable = false;
16+
state.doc.nodesBetween(pos, pos, (node) => {
17+
if (
18+
node.type.name === "table" ||
19+
node.type.name === "table_row" ||
20+
node.type.name === "table_cell" ||
21+
node.type.name === "table_header"
22+
) {
23+
nodeIsDescendantOfTable = true;
24+
}
25+
});
26+
if (nodeIsDescendantOfTable) {
2027
return;
2128
}
29+
const count = counts.get(node.type.name) || 0;
30+
counts.set(node.type.name, count + 1);
2231
if (node.type.isBlock) {
23-
// TODO: is there a better key we can use?
24-
decorations.push(widget(pos, BlockDecoration, { key: `node-${pos}` }));
32+
decorations.push(
33+
widget(pos, BlockDecoration, {
34+
key: `node-${node.type.name}-${count}`,
35+
})
36+
);
2537
} else {
26-
const hasMarks = !!node.marks.length;
27-
const isMath = node.type.name === "math_inline";
28-
if (hasMarks || isMath) {
29-
/* If it's an inline node with marks OR is inline math */
30-
decorations.push(widget(pos, InlineDecoration, { key: `mark-${pos}` }));
38+
// If it's an inline node with marks OR is inline math
39+
if (node.marks.length > 0 || node.type.name === "math_inline") {
40+
decorations.push(
41+
widget(pos, InlineDecoration, {
42+
key: `mark-${node.type.name}-${count}`,
43+
})
44+
);
3145
}
3246
}
3347
});

packages/context-editor/src/style.css

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,21 @@
7373
.ProseMirror ul,
7474
.ProseMirror ol,
7575
.ProseMirror li,
76-
.ProseMirror section {
76+
.ProseMirror section,
77+
.ProseMirror table {
7778
border-left: 1px solid #777;
7879
padding-left: 5px;
7980
padding-top: 5px;
8081
margin-top: 16px;
8182
margin-bottom: 16px;
8283
}
84+
.ProseMirror tr * {
85+
border: none;
86+
}
87+
.ProseMirror tr p {
88+
margin: 0;
89+
}
90+
8391
.ProseMirror section {
8492
margin-left: -5px;
8593
}
@@ -168,33 +176,53 @@
168176
min-height: unset !important; /* Remove the default min-height */
169177
}
170178

179+
/* Table */
171180
.ProseMirror table {
172-
border-collapse: collapse;
173-
margin: 25px 0;
181+
border-spacing: 0;
182+
border-collapse: separate;
174183
font-size: 0.9em;
175184
font-family: sans-serif;
176185
min-width: 400px;
186+
width: 100%;
187+
table-layout: fixed;
177188
}
178189

179-
.ProseMirror thead tr {
180-
background-color: #009879;
181-
color: #ffffff;
182-
text-align: left;
190+
.ProseMirror table td,
191+
.ProseMirror table th {
192+
border-top: 1px solid #ccc;
193+
border-left: 1px solid #ccc;
183194
}
184195

185-
.ProseMirror th,
186-
.ProseMirror td {
187-
padding: 6px 8px;
196+
.ProseMirror table td:last-child,
197+
.ProseMirror table th:last-child {
198+
border-right: 1px solid #ccc;
188199
}
189200

190-
.ProseMirror tbody tr {
191-
border-bottom: 1px solid #dddddd;
201+
.ProseMirror table tr:last-child td {
202+
border-bottom: 1px solid #ccc;
192203
}
193204

194-
.ProseMirror tbody tr:nth-of-type(even) {
195-
background-color: #f3f3f3;
205+
.ProseMirror table td {
206+
vertical-align: top;
207+
}
208+
209+
.ProseMirror table {
210+
border-left: 1px solid #777;
211+
}
212+
213+
.wrap-widget + table {
214+
position: relative;
215+
top: -1px;
216+
}
217+
218+
.wrap-widget button {
219+
z-index: 10;
196220
}
197221

198-
.ProseMirror tbody tr:last-of-type {
199-
border-bottom: 2px solid #009879;
222+
.ProseMirror thead tr {
223+
text-align: left;
224+
}
225+
226+
.ProseMirror tbody tr:nth-of-type(even) {
227+
background-color: #f3f3f3;
200228
}

0 commit comments

Comments
 (0)