WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 8299f73

Browse files
authored
feat: enhance table extension to respect user-defined styles for widths (#7165)
* feat: enhance table extension to respect user-defined styles for widths * feat: add pull request description template to guidelines * fix: improve width style check in updateColumns function * refactor: remove unused addAttributes method from Table extension
1 parent d83e600 commit 8299f73

File tree

6 files changed

+80
-2
lines changed

6 files changed

+80
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tiptap/extension-table": patch
3+
---
4+
5+
Allow setting custom table widths by respecting user-provided `style` attributes instead of always overriding them with calculated widths.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
applyTo: '**'
3+
---
4+
5+
When asked to write a pull request description, use the following template:
6+
7+
```
8+
## Changes Overview
9+
10+
<!-- Briefly describe your changes. -->
11+
12+
## Implementation Approach
13+
14+
<!-- Describe your approach to implementing these changes. Keep it concise. -->
15+
16+
## Testing Done
17+
18+
<!-- Explain how you tested these changes. Link to test scenarios or specs if relevant. -->
19+
20+
## Verification Steps
21+
22+
<!-- Describe steps reviewers can take to verify the functionality of your changes. -->
23+
24+
## Additional Notes
25+
26+
<!-- Add any other notes or screenshots about the PR here. -->
27+
28+
## Checklist
29+
30+
- [ ] I have created a [changeset](https://github.com/changesets/changesets) for this PR if necessary.
31+
- [ ] My changes do not break the library.
32+
- [ ] I have added tests where applicable.
33+
- [ ] I have followed the project guidelines.
34+
- [ ] I have fixed any lint issues.
35+
36+
## Related Issues
37+
38+
<!-- Link any related issues here -->
39+
```
40+
41+
When generating the pull request description, ensure it is clear, concise, and follows the provided template. Focus on the key aspects of the changes made, how they were implemented, and how they can be verified.
42+
43+
Make it as minimal as possible to convey the necessary information effectively without causing to much noise and making it hard to read.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
applyTo: '**'
3+
---
4+
5+
When a user asks for a changeset to be generated, follow the following rules:
6+
7+
Create a good changeset file for the changes in the diff. Don't include non-frontfacing changes, as the changeset file will be used for the changelog. Our users don't care about deep logic that they'll not interact with so we only want to generate changelog entries for front-facing/user-facing changes and API changes our users will need to know about.
8+
9+
Make sure that the changeset file stays minimal and short but includes important information that may be important for our users to understand what actually changed.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ demos/src/Dev/**
4444
# instructions
4545
.github/instructions/*
4646
!.github/instructions/tiptap.instructions.md
47+
!.github/instructions/changeset.instructions.md
48+
!.github/instructions/PR.instructions.md

packages/extension-table/src/table/TableView.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export function updateColumns(
5858
nextDOM = after
5959
}
6060

61-
if (fixedWidth) {
61+
// Check if user has set a width style on the table node
62+
const hasUserWidth = node.attrs.style && typeof node.attrs.style === 'string' && /\bwidth\s*:/i.test(node.attrs.style)
63+
64+
if (fixedWidth && !hasUserWidth) {
6265
table.style.width = `${totalWidth}px`
6366
table.style.minWidth = ''
6467
} else {
@@ -86,6 +89,12 @@ export class TableView implements NodeView {
8689
this.dom = document.createElement('div')
8790
this.dom.className = 'tableWrapper'
8891
this.table = this.dom.appendChild(document.createElement('table'))
92+
93+
// Apply user styles to the table element
94+
if (node.attrs.style) {
95+
this.table.style.cssText = node.attrs.style
96+
}
97+
8998
this.colgroup = this.table.appendChild(document.createElement('colgroup'))
9099
updateColumns(node, this.colgroup, this.table, cellMinWidth)
91100
this.contentDOM = this.table.appendChild(document.createElement('tbody'))

packages/extension-table/src/table/table.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,20 @@ export const Table = Node.create<TableOptions>({
280280
renderHTML({ node, HTMLAttributes }) {
281281
const { colgroup, tableWidth, tableMinWidth } = createColGroup(node, this.options.cellMinWidth)
282282

283+
const userStyles = HTMLAttributes.style as string | undefined
284+
285+
function getTableStyle() {
286+
if (userStyles) {
287+
return userStyles
288+
}
289+
290+
return tableWidth ? `width: ${tableWidth}` : `min-width: ${tableMinWidth}`
291+
}
292+
283293
const table: DOMOutputSpec = [
284294
'table',
285295
mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
286-
style: tableWidth ? `width: ${tableWidth}` : `min-width: ${tableMinWidth}`,
296+
style: getTableStyle(),
287297
}),
288298
colgroup,
289299
['tbody', 0],

0 commit comments

Comments
 (0)