FIX: Don't let table-build automatically fill empty headers with default values (#27894)

* FIX: Don't let table-build automatically fill empty headers with default values

The old table builder would fill empty headers with default values A~Z when editing.
This commit makes table-builder respect the old empty headers

related meta topic: https://meta.discourse.org/t/editing-a-table-with-empty-headers-fills-them-in-with-the-default-text-column-a-column-b/268472
This commit is contained in:
锦心 2024-07-13 00:41:18 +08:00 committed by GitHub
parent 271cbcefa9
commit 63ca30ccb4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 3 deletions

View File

@ -90,7 +90,10 @@ export default class SpreadsheetEditor extends Component {
@action @action
insertTable() { insertTable() {
const updatedHeaders = this.spreadsheet.getHeaders().split(","); // keys const updatedHeaders = this.spreadsheet
.getHeaders()
.split(",")
.map((c) => c.trim()); // keys
const updatedData = this.spreadsheet.getData(); // values const updatedData = this.spreadsheet.getData(); // values
const markdownTable = this.buildTableMarkdown(updatedHeaders, updatedData); const markdownTable = this.buildTableMarkdown(updatedHeaders, updatedData);
@ -205,7 +208,7 @@ export default class SpreadsheetEditor extends Component {
// headings // headings
headings = this.extractTableContent(row).map((heading) => { headings = this.extractTableContent(row).map((heading) => {
return { return {
title: heading, title: heading || " ",
width: Math.max( width: Math.max(
heading.length * rowWidthFactor, heading.length * rowWidthFactor,
this.defaultColWidth this.defaultColWidth
@ -221,7 +224,9 @@ export default class SpreadsheetEditor extends Component {
} }
}); });
headings.forEach((h, i) => (h.align = this.alignments?.[i] ?? "left")); headings.forEach((h, i) => {
h.align = this.alignments?.[i] ?? "left";
});
return this.buildSpreadsheet(rows, headings); return this.buildSpreadsheet(rows, headings);
} }

View File

@ -5,6 +5,7 @@ describe "Table Builder", type: :system do
let(:composer) { PageObjects::Components::Composer.new } let(:composer) { PageObjects::Components::Composer.new }
let(:insert_table_modal) { PageObjects::Modals::InsertTable.new } let(:insert_table_modal) { PageObjects::Modals::InsertTable.new }
fab!(:topic) { Fabricate(:topic, user: user) } fab!(:topic) { Fabricate(:topic, user: user) }
fab!(:topic2) { Fabricate(:topic, user: user) }
fab!(:post1) { create_post(user: user, topic: topic, raw: <<~RAW) } fab!(:post1) { create_post(user: user, topic: topic, raw: <<~RAW) }
|Make | Model | Year| |Make | Model | Year|
|--- | --- | ---| |--- | --- | ---|
@ -12,6 +13,12 @@ describe "Table Builder", type: :system do
|Nissan | Skyline | 1999| |Nissan | Skyline | 1999|
|Honda | S2000 | 2001| |Honda | S2000 | 2001|
RAW RAW
fab!(:post2) { create_post(user: user, topic: topic2, raw: <<~RAW) }
| | | |
|--- | --- | ---|
|Some | content | here|
|1 | 2 | 3|
RAW
let(:topic_page) { PageObjects::Pages::Topic.new } let(:topic_page) { PageObjects::Pages::Topic.new }
@ -108,6 +115,25 @@ describe "Table Builder", type: :system do
end end
end end
it "should respect the original empty header" do
topic_page.visit_topic(topic2)
topic_page.find(".btn-edit-table", visible: :all).click
expect(page).to have_selector(".insert-table-modal")
insert_table_modal.type_in_cell(0, 0, " updated")
insert_table_modal.click_insert_table
updated_post = <<~RAW
| | | |
|--- | --- | ---|
|Some updated | content | here|
|1 | 2 | 3|
RAW
try_until_success do
expect(normalize_value(post2.reload.raw)).to eq(normalize_value(updated_post))
end
end
context "when adding an edit reason" do context "when adding an edit reason" do
it "should add the edit reason to the edit history" do it "should add the edit reason to the edit history" do
edit_reason = "Updated Nissan model" edit_reason = "Updated Nissan model"