-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bug]: HTML output of Table is missing colgroup element (#4281)
* [Bug]: HTML output of Table is missing colgroup element Fixes #4280 * Fixed typo and added some documentation
- Loading branch information
1 parent
6d8577e
commit 6b2edc5
Showing
4 changed files
with
75 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Node as ProseMirrorNode } from '@tiptap/pm/model' | ||
import { DOMOutputSpec } from 'prosemirror-model' | ||
|
||
/** | ||
* Creates a colgroup element for a table node in ProseMirror. | ||
* | ||
* @param node - The ProseMirror node representing the table. | ||
* @param cellMinWidth - The minimum width of a cell in the table. | ||
* @param overrideCol - (Optional) The index of the column to override the width of. | ||
* @param overrideValue - (Optional) The width value to use for the overridden column. | ||
* @returns An object containing the colgroup element, the total width of the table, and the minimum width of the table. | ||
*/ | ||
export function createColGroup( | ||
node: ProseMirrorNode, | ||
cellMinWidth: number, | ||
overrideCol?: number, | ||
overrideValue?: any, | ||
) { | ||
let totalWidth = 0 | ||
let fixedWidth = true | ||
const cols: DOMOutputSpec[] = [] | ||
const row = node.firstChild | ||
|
||
if (!row) { | ||
return {} | ||
} | ||
|
||
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` : '' | ||
|
||
totalWidth += hasWidth || cellMinWidth | ||
|
||
if (!hasWidth) { | ||
fixedWidth = false | ||
} | ||
|
||
cols.push(['col', cssWidth ? { style: `width: ${cssWidth}` } : {}]) | ||
} | ||
} | ||
|
||
const tableWidth = fixedWidth ? `${totalWidth}px` : '' | ||
const tableMinWidth = fixedWidth ? '' : `${totalWidth}px` | ||
|
||
const colgroup: DOMOutputSpec = ['colgroup', {}, ...cols] | ||
|
||
return { colgroup, tableWidth, tableMinWidth } | ||
} |