Skip to content

fix: Table column widths not being set in exported HTML #1947

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions packages/core/src/blocks/TableBlockContent/TableBlockContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,45 @@ export const TableBlockContent = createStronglyTypedTiptapNode({
];
},

renderHTML({ HTMLAttributes }) {
return createDefaultBlockDOMOutputSpec(
renderHTML({ node, HTMLAttributes }) {
const domOutputSpec = createDefaultBlockDOMOutputSpec(
this.name,
"table",
{
...(this.options.domAttributes?.blockContent || {}),
...(this.options.domAttributes?.blockContent || { fesfes: "fesfes" }),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this?

...HTMLAttributes,
},
this.options.domAttributes?.inlineContent || {},
);

// Need to manually add column widths.
const cols: HTMLTableColElement[] = [];
for (const tableCell of node.children[0].children) {
const colWidths: null | (number | undefined)[] =
tableCell.attrs["colwidth"];

if (colWidths) {
for (const colWidth of tableCell.attrs["colwidth"]) {
const col = document.createElement("col");
if (colWidth) {
col.style = `width: ${colWidth}px`;
}

cols.push(col);
}
} else {
cols.push(document.createElement("col"));
}
}

const colGroup = document.createElement("colgroup");
for (const col of cols) {
colGroup.appendChild(col);
}
Comment on lines +48 to +71
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Need to manually add column widths.
const cols: HTMLTableColElement[] = [];
for (const tableCell of node.children[0].children) {
const colWidths: null | (number | undefined)[] =
tableCell.attrs["colwidth"];
if (colWidths) {
for (const colWidth of tableCell.attrs["colwidth"]) {
const col = document.createElement("col");
if (colWidth) {
col.style = `width: ${colWidth}px`;
}
cols.push(col);
}
} else {
cols.push(document.createElement("col"));
}
}
const colGroup = document.createElement("colgroup");
for (const col of cols) {
colGroup.appendChild(col);
}
// Need to manually add colgroup element
const colGroup = document.createElement("colgroup");
for (const tableCell of node.children[0].children) {
const colWidths: null | (number | undefined)[] =
tableCell.attrs["colwidth"];
if (colWidths) {
for (const colWidth of tableCell.attrs["colwidth"]) {
const col = document.createElement("col");
if (colWidth) {
col.style = `width: ${colWidth}px`;
}
colGroup.appendChild(col);
}
} else {
colGroup.appendChild(document.createElement("col"));
}
}

Just makes it a bit clearer what is happening here without the extra insertion loop


domOutputSpec.dom.firstChild?.appendChild(colGroup);

return domOutputSpec;
},

// This node view is needed for the `columnResizing` plugin. By default, the
Expand Down Expand Up @@ -146,8 +175,8 @@ const TableParagraph = createStronglyTypedTiptapNode({
];
},

renderHTML({ HTMLAttributes }) {
return ["p", HTMLAttributes, 0];
renderHTML({ node, HTMLAttributes }) {
return ["p", HTMLAttributes, node.childCount ? 0 : ["br"]];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a break?

},
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ <h1>Heading 1</h1>
<code class="bn-inline-content language-text" data-language="text">console.log("Hello World");</code>
</pre>
<table>
<colgroup>
<col />
<col />
</colgroup>
<tr>
<td colspan="1" rowspan="1">
<p>Table Cell 1</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ <h2 data-level="2">Heading 1</h2>
<code class="bn-inline-content language-typescript" data-language="typescript">console.log("Hello World");</code>
</pre>
<table>
<colgroup>
<col />
<col />
</colgroup>
<tr>
<td colspan="1" rowspan="1">
<p>Table Cell 1</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<div class="bn-block-group" data-node-type="blockGroup">
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
<div class="bn-block" data-node-type="blockContainer" data-id="1">
<div class="bn-block-content" data-content-type="table">
<div class="bn-block-content" data-content-type="table" fesfes="fesfes">
<table class="bn-inline-content">
<colgroup>
<col style="width: 100px;" />
<col style="width: 200px;" />
<col style="width: 300px;" />
</colgroup>
<tr>
<td colspan="1" rowspan="1" colwidth="100">
<p>Table Cell</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<div class="bn-block-group" data-node-type="blockGroup">
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
<div class="bn-block" data-node-type="blockContainer" data-id="1">
<div class="bn-block-content" data-content-type="table">
<div class="bn-block-content" data-content-type="table" fesfes="fesfes">
<table class="bn-inline-content">
<colgroup>
<col />
<col />
<col />
</colgroup>
<tr>
<td colspan="1" rowspan="1">
<p>Table Cell</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<div class="bn-block-group" data-node-type="blockGroup">
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
<div class="bn-block" data-node-type="blockContainer" data-id="1">
<div class="bn-block-content" data-content-type="table">
<div class="bn-block-content" data-content-type="table" fesfes="fesfes">
<table class="bn-inline-content">
<colgroup>
<col />
<col />
<col />
</colgroup>
<tr>
<th colspan="1" rowspan="1">
<p>Table Cell</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<div class="bn-block-group" data-node-type="blockGroup">
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
<div class="bn-block" data-node-type="blockContainer" data-id="1">
<div class="bn-block-content" data-content-type="table">
<div class="bn-block-content" data-content-type="table" fesfes="fesfes">
<table class="bn-inline-content">
<colgroup>
<col />
<col />
<col />
</colgroup>
<tr>
<th colspan="1" rowspan="1">
<p>Table Cell</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<div class="bn-block-group" data-node-type="blockGroup">
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
<div class="bn-block" data-node-type="blockContainer" data-id="1">
<div class="bn-block-content" data-content-type="table">
<div class="bn-block-content" data-content-type="table" fesfes="fesfes">
<table class="bn-inline-content">
<colgroup>
<col style="width: 100px;" />
<col />
<col style="width: 300px;" />
</colgroup>
<tr>
<td
data-text-color="blue"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<div class="bn-block-group" data-node-type="blockGroup">
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
<div class="bn-block" data-node-type="blockContainer" data-id="1">
<div class="bn-block-content" data-content-type="table">
<div class="bn-block-content" data-content-type="table" fesfes="fesfes">
<table class="bn-inline-content">
<colgroup>
<col style="width: 100px;" />
<col />
<col style="width: 300px;" />
</colgroup>
<tr>
<td colspan="1" rowspan="1" colwidth="100">
<p>Table Cell</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<div class="bn-block-group" data-node-type="blockGroup">
<div class="bn-block-outer" data-node-type="blockOuter" data-id="1">
<div class="bn-block" data-node-type="blockContainer" data-id="1">
<div class="bn-block-content" data-content-type="table">
<div class="bn-block-content" data-content-type="table" fesfes="fesfes">
<table class="bn-inline-content">
<colgroup>
<col style="width: 100px;" />
<col style="width: 200px;" />
<col style="width: 300px;" />
</colgroup>
<tr>
<td
data-text-color="blue"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<table>
<colgroup>
<col style="width: 100px;" />
<col style="width: 200px;" />
<col style="width: 300px;" />
</colgroup>
<tr>
<td colspan="1" rowspan="1" colwidth="100">
<p>Table Cell</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<table>
<colgroup>
<col />
<col />
<col />
</colgroup>
<tr>
<td colspan="1" rowspan="1">
<p>Table Cell</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<table>
<colgroup>
<col />
<col />
<col />
</colgroup>
<tr>
<th colspan="1" rowspan="1">
<p>Table Cell</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<table>
<colgroup>
<col />
<col />
<col />
</colgroup>
<tr>
<th colspan="1" rowspan="1">
<p>Table Cell</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<table>
<colgroup>
<col style="width: 100px;" />
<col />
<col style="width: 300px;" />
</colgroup>
<tr>
<td
data-text-color="blue"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<table>
<colgroup>
<col style="width: 100px;" />
<col />
<col style="width: 300px;" />
</colgroup>
<tr>
<td colspan="1" rowspan="1" colwidth="100">
<p>Table Cell</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<table>
<colgroup>
<col style="width: 100px;" />
<col style="width: 200px;" />
<col style="width: 300px;" />
</colgroup>
<tr>
<td
data-text-color="blue"
Expand Down
Loading