FIX: Only update table portion from raw post

This commit is contained in:
Keegan George 2022-07-18 11:03:57 -07:00
parent d74b713d0b
commit 2ddb6de654
4 changed files with 24 additions and 8 deletions

View File

@ -31,6 +31,7 @@ export default apiInitializer("0.11.1", (api) => {
function generateModal(event) { function generateModal(event) {
const table = event.target.parentNode.lastElementChild; const table = event.target.parentNode.lastElementChild;
const tempTable = table.cloneNode(true); const tempTable = table.cloneNode(true);
const tableId = event.target.getAttribute("data-table-id");
return ajax(`/posts/${this.id}`, { type: "GET" }) return ajax(`/posts/${this.id}`, { type: "GET" })
.then((post) => { .then((post) => {
@ -38,19 +39,20 @@ export default apiInitializer("0.11.1", (api) => {
model: post, model: post,
}).setProperties({ }).setProperties({
tableHtml: tempTable, tableHtml: tempTable,
submitOnEnter: false, tableId,
}); });
}) })
.catch(popupAjaxError); .catch(popupAjaxError);
} }
function generatePopups(tables, attrs) { function generatePopups(tables, attrs) {
tables.forEach((table) => { tables.forEach((table, index) => {
if (site.isMobileDevice) { if (site.isMobileDevice) {
return; return;
} }
const popupBtn = createButton(); const popupBtn = createButton();
popupBtn.setAttribute("data-table-id", index);
table.parentNode.classList.add("fullscreen-table-wrapper"); table.parentNode.classList.add("fullscreen-table-wrapper");
const expandBtn = document.querySelector(".open-popup-link"); const expandBtn = document.querySelector(".open-popup-link");
@ -59,6 +61,7 @@ export default apiInitializer("0.11.1", (api) => {
} else { } else {
table.parentNode.insertBefore(popupBtn, table); table.parentNode.insertBefore(popupBtn, table);
} }
popupBtn.addEventListener("click", generateModal.bind(attrs), false); popupBtn.addEventListener("click", generateModal.bind(attrs), false);
}); });
} }

View File

@ -5,6 +5,7 @@ import { arrayToTable, tableToObj } from "../lib/utilities";
import Component from "@ember/component"; import Component from "@ember/component";
import { ajax } from "discourse/lib/ajax"; import { ajax } from "discourse/lib/ajax";
import { popupAjaxError } from "discourse/lib/ajax-error"; import { popupAjaxError } from "discourse/lib/ajax-error";
import { findTableRegex } from "../lib/utilities";
export default Component.extend({ export default Component.extend({
tagName: "", tagName: "",
@ -53,9 +54,6 @@ export default Component.extend({
data: tableData, data: tableData,
columns, columns,
}); });
const originalData = this.spreadsheet.getData();
console.log("Original Data:", originalData);
}, },
@action @action
@ -74,7 +72,6 @@ export default Component.extend({
@action @action
editTable() { editTable() {
// TODO: insert table edit submission logic
const updatedHeaders = this.spreadsheet.getHeaders().split(","); // keys const updatedHeaders = this.spreadsheet.getHeaders().split(","); // keys
const updatedData = this.spreadsheet.getData(); // values const updatedData = this.spreadsheet.getData(); // values
@ -83,8 +80,20 @@ export default Component.extend({
const newRaw = markdownTable; const newRaw = markdownTable;
const editReason = const editReason =
this.get("editReason") || "Update Table with Table Editor"; this.get("editReason") || "Update Table with Table Editor";
const tableId = this.get("tableId");
this.updateTable(postId, newRaw, editReason); const raw = this.model.raw;
const tableToEdit = raw.match(findTableRegex());
let editedTable;
if (tableToEdit.length > 1) {
editedTable = raw.replace(tableToEdit[tableId], newRaw);
} else {
editedTable = raw.replace(findTableRegex(), newRaw);
}
this.updateTable(postId, editedTable, editReason);
}, },
updateTable(postId, raw, edit_reason) { updateTable(postId, raw, edit_reason) {

View File

@ -85,3 +85,7 @@ export function arrayToTable(array, columns, alignment = "center") {
// Return table // Return table
return table; return table;
} }
export function findTableRegex() {
return /((\r?){2}|^)([^\r\n]*\|[^\r\n]*(\r?\n)?)+(?=(\r?\n){2}|$)/gm;
}

View File

@ -1,6 +1,6 @@
<SpreadsheetEditor <SpreadsheetEditor
@model={{this.model}} @model={{this.model}}
@tableHtml={{this.tableHtml}} @tableHtml={{this.tableHtml}}
@tableId={{this.tableId}}
@triggerModalClose={{action "closeEditModal"}} @triggerModalClose={{action "closeEditModal"}}
@submitOnClose="false"
/> />