-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(table): set min-width to <col> #5464
Changes from 2 commits
3c44b42
b4e92c5
d48c188
4ae99cf
644b141
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@tiptap/extension-table": patch | ||
--- | ||
|
||
enforce cellMinWidth even on column not resized by the user, fixes #5435 |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -1,49 +1,55 @@ | ||||||||
// @ts-nocheck | ||||||||
import { Node as ProseMirrorNode } from '@tiptap/pm/model' | ||||||||
import { NodeView } from '@tiptap/pm/view' | ||||||||
|
||||||||
import { getColStyleDeclaration } from './utilities/colStyle.js' | ||||||||
|
||||||||
export function updateColumns( | ||||||||
node: ProseMirrorNode, | ||||||||
colgroup: Element, | ||||||||
table: Element, | ||||||||
colgroup: HTMLTableColElement, // <colgroup> has the same prototype as <col> | ||||||||
table: HTMLTableElement, | ||||||||
cellMinWidth: number, | ||||||||
overrideCol?: number, | ||||||||
overrideValue?: any, | ||||||||
overrideValue?: number, | ||||||||
) { | ||||||||
let totalWidth = 0 | ||||||||
let fixedWidth = true | ||||||||
let nextDOM = colgroup.firstChild | ||||||||
const row = node.firstChild | ||||||||
|
||||||||
for (let i = 0, col = 0; i < row.childCount; i += 1) { | ||||||||
const { colspan, colwidth } = row.child(i).attrs | ||||||||
|
||||||||
for (let j = 0; j < colspan; j += 1, col += 1) { | ||||||||
const hasWidth = overrideCol === col ? overrideValue : colwidth && colwidth[j] | ||||||||
const cssWidth = hasWidth ? `${hasWidth}px` : '' | ||||||||
if (row !== null) { | ||||||||
for (let i = 0, col = 0; i < row.childCount; i += 1) { | ||||||||
const { colspan, colwidth } = row.child(i).attrs | ||||||||
|
||||||||
totalWidth += hasWidth || cellMinWidth | ||||||||
for (let j = 0; j < colspan; j += 1, col += 1) { | ||||||||
const hasWidth = overrideCol === col ? overrideValue : (colwidth && colwidth[j]) as number | undefined | ||||||||
const cssWidth = hasWidth ? `${hasWidth}px` : '' | ||||||||
|
||||||||
if (!hasWidth) { | ||||||||
fixedWidth = false | ||||||||
} | ||||||||
totalWidth += hasWidth || cellMinWidth | ||||||||
|
||||||||
if (!nextDOM) { | ||||||||
colgroup.appendChild(document.createElement('col')).style.width = cssWidth | ||||||||
} else { | ||||||||
if (nextDOM.style.width !== cssWidth) { | ||||||||
nextDOM.style.width = cssWidth | ||||||||
if (!hasWidth) { | ||||||||
fixedWidth = false | ||||||||
} | ||||||||
|
||||||||
nextDOM = nextDOM.nextSibling | ||||||||
if (nextDOM === null) { | ||||||||
const colElement = document.createElement('col') | ||||||||
|
||||||||
colElement.style.setProperty(...getColStyleDeclaration(cellMinWidth, hasWidth)) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While clever, I would prefer to be explicit around this, so that it is easy to understand what is happening:
Suggested change
Same thing in the next few lines |
||||||||
colgroup.appendChild(colElement) | ||||||||
} else { | ||||||||
if ((nextDOM as HTMLTableColElement).style.width !== cssWidth) { | ||||||||
(nextDOM as HTMLTableColElement).style.setProperty(...getColStyleDeclaration(cellMinWidth, hasWidth)) | ||||||||
} | ||||||||
|
||||||||
nextDOM = nextDOM.nextSibling | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
while (nextDOM) { | ||||||||
const after = nextDOM.nextSibling | ||||||||
|
||||||||
nextDOM.parentNode.removeChild(nextDOM) | ||||||||
nextDOM.parentNode?.removeChild(nextDOM) | ||||||||
nextDOM = after | ||||||||
} | ||||||||
|
||||||||
|
@@ -61,13 +67,13 @@ export class TableView implements NodeView { | |||||||
|
||||||||
cellMinWidth: number | ||||||||
|
||||||||
dom: Element | ||||||||
dom: HTMLDivElement | ||||||||
|
||||||||
table: Element | ||||||||
table: HTMLTableElement | ||||||||
|
||||||||
colgroup: Element | ||||||||
colgroup: HTMLTableColElement | ||||||||
|
||||||||
contentDOM: Element | ||||||||
contentDOM: HTMLTableSectionElement | ||||||||
|
||||||||
constructor(node: ProseMirrorNode, cellMinWidth: number) { | ||||||||
this.node = node | ||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export function getColStyleDeclaration(minWidth: number, width: number | undefined): [string, string] { | ||
if (width) { | ||
// apply the stored width unless it is below the configured minimum cell width | ||
return ['width', `${Math.max(width, minWidth)}px`] | ||
} | ||
|
||
// set the minimum with on the column if it has no stored width | ||
return ['min-width', `${minWidth}px`] | ||
|
||
} | ||
|
||
export function getColStyle(minWidth: number, width: number): string { | ||
return getColStyleDeclaration(minWidth, width).join(': ') | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is abstracting over a couple of if statements, I'm not sure it warrants the complexity it adds. I would honestly prefer for there to be a single function here, either stick to the string version or the array version, both just adds complexity and indirection. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I extracted them because the logic was originally copy-pasted in both the create and the update code, I can remove the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the nit, would prefer to keep this as
Just in case it is undefined, and to reduce differentiation from the prior behavior