Skip to content
This repository was archived by the owner on Apr 17, 2025. It is now read-only.

FIX: Table editing sometimes not working #50

Merged
merged 2 commits into from
Apr 25, 2023
Merged
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
16 changes: 10 additions & 6 deletions javascripts/discourse/api-initializers/table-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ export default apiInitializer("0.11.1", (api) => {
return openPopupBtn;
}

function generateModal(event) {
const tableId = event.target.getAttribute("data-table-id");
function generateModal() {
const tableIndex = this.tableIndex;

return ajax(`/posts/${this.id}`, { type: "GET" })
.then((post) =>
parseAsync(post.raw).then((tokens) => {
const allTables = tokenRange(tokens, "table_open", "table_close");
const tableTokens = allTables[tableId];
const tableTokens = allTables[tableIndex];

showModal("insert-table-modal", {
model: post,
}).setProperties({
tableId,
tableIndex,
tableTokens,
});
})
Expand All @@ -53,7 +53,7 @@ export default apiInitializer("0.11.1", (api) => {
function generatePopups(tables, attrs) {
tables.forEach((table, index) => {
const popupBtn = createButton();
popupBtn.setAttribute("data-table-id", index); // sets a table id so each table can be distinctly edited
table.parentNode.setAttribute("data-table-index", index);
table.parentNode.classList.add("fullscreen-table-wrapper");
const expandBtn = table.parentNode.querySelector(".open-popup-link");

Expand All @@ -66,7 +66,11 @@ export default apiInitializer("0.11.1", (api) => {
table.parentNode.insertBefore(buttonWrapper, table);
}

popupBtn.addEventListener("click", generateModal.bind(attrs), false);
popupBtn.addEventListener(
"click",
generateModal.bind({ tableIndex: index, ...attrs }),
false
);
});
}

Expand Down
17 changes: 5 additions & 12 deletions javascripts/discourse/components/spreadsheet-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,9 @@ export default class SpreadsheetEditor extends Component {
@tracked loading = null;
spreadsheet = null;
defaultColWidth = 150;
isEditingTable = !!this.args.tableTokens;

// Getters:
get isEditingTable() {
if (this.args.tableTokens) {
return true;
}

return false;
}

get modalAttributes() {
if (this.isEditingTable) {
return {
Expand Down Expand Up @@ -196,12 +189,12 @@ export default class SpreadsheetEditor extends Component {
});
}

buildUpdatedPost(tableId, raw, newRaw) {
buildUpdatedPost(tableIndex, raw, newRaw) {
const tableToEdit = raw.match(findTableRegex());
let editedTable;

if (tableToEdit.length) {
editedTable = raw.replace(tableToEdit[tableId], newRaw);
editedTable = raw.replace(tableToEdit[tableIndex], newRaw);
} else {
return raw;
}
Expand All @@ -212,15 +205,15 @@ export default class SpreadsheetEditor extends Component {
}

updateTable(markdownTable) {
const tableId = this.args.tableId;
const tableIndex = this.args.tableIndex;
const postId = this.args.model.id;
const newRaw = markdownTable;

const editReason =
this.editReason ||
I18n.t(themePrefix("discourse_table_builder.edit.default_edit_reason"));
const raw = this.args.model.raw;
const newPostRaw = this.buildUpdatedPost(tableId, raw, newRaw);
const newPostRaw = this.buildUpdatedPost(tableIndex, raw, newRaw);

return this.sendTableUpdate(postId, newPostRaw, editReason);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<SpreadsheetEditor
@model={{this.model}}
@tableTokens={{this.tableTokens}}
@tableId={{this.tableId}}
@tableIndex={{this.tableIndex}}
@triggerModalClose={{action "closeEditModal"}}
@toolbarEvent={{this.toolbarEvent}}
/>