mirror of
https://github.com/discourse/discourse-table-builder.git
synced 2026-09-17 15:22:14 -04:00
Known issue: overrides other content on the post and replaces the post with only the table
118 lines
2.8 KiB
JavaScript
118 lines
2.8 KiB
JavaScript
// import Controller from "@ember/controller";
|
|
import { action } from "@ember/object";
|
|
import loadScript from "discourse/lib/load-script";
|
|
import { arrayToTable, tableToObj } from "../lib/utilities";
|
|
import Component from "@ember/component";
|
|
import { ajax } from "discourse/lib/ajax";
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
export default Component.extend({
|
|
tagName: "",
|
|
showEditReason: false,
|
|
|
|
didInsertElement() {
|
|
this._super(...arguments);
|
|
|
|
this.loadLibraries().then(() => {
|
|
this.buildTable(this.tableHtml);
|
|
});
|
|
},
|
|
|
|
loadLibraries() {
|
|
return loadScript(settings.theme_uploads.jsuites).then(() => {
|
|
return loadScript(settings.theme_uploads.jspreadsheet);
|
|
});
|
|
},
|
|
|
|
buildTable(table) {
|
|
const tableObject = tableToObj(table);
|
|
const headings = [];
|
|
const tableData = [];
|
|
|
|
tableObject.forEach((object) => {
|
|
// Build Headings
|
|
if (!headings.includes(...Object.keys(object))) {
|
|
headings.push(...Object.keys(object));
|
|
}
|
|
|
|
// Build Table Data
|
|
tableData.push([...Object.values(object)]);
|
|
});
|
|
|
|
const columns = headings.map((heading) => {
|
|
return {
|
|
title: heading,
|
|
width: heading.length * 15,
|
|
};
|
|
});
|
|
|
|
const spreadsheetContainer = document.querySelector("#spreadsheet");
|
|
|
|
// eslint-disable-next-line no-undef
|
|
this.spreadsheet = jspreadsheet(spreadsheetContainer, {
|
|
data: tableData,
|
|
columns,
|
|
});
|
|
|
|
const originalData = this.spreadsheet.getData();
|
|
console.log("Original Data:", originalData);
|
|
},
|
|
|
|
@action
|
|
showEditReasonField() {
|
|
if (this.showEditReason) {
|
|
return this.set("showEditReason", false);
|
|
} else {
|
|
return this.set("showEditReason", true);
|
|
}
|
|
},
|
|
|
|
@action
|
|
cancelTableEdit() {
|
|
this.triggerModalClose();
|
|
},
|
|
|
|
@action
|
|
editTable() {
|
|
// TODO: insert table edit submission logic
|
|
const updatedHeaders = this.spreadsheet.getHeaders().split(","); // keys
|
|
const updatedData = this.spreadsheet.getData(); // values
|
|
|
|
const markdownTable = this.buildTableMarkdown(updatedHeaders, updatedData);
|
|
const postId = this.model.id;
|
|
const newRaw = markdownTable;
|
|
const editReason =
|
|
this.get("editReason") || "Update Table with Table Editor";
|
|
|
|
this.updateTable(postId, newRaw, editReason);
|
|
},
|
|
|
|
updateTable(postId, raw, edit_reason) {
|
|
return ajax(`/posts/${postId}.json`, {
|
|
type: "PUT",
|
|
data: {
|
|
post: {
|
|
raw,
|
|
edit_reason,
|
|
},
|
|
},
|
|
})
|
|
.catch(popupAjaxError)
|
|
.finally(() => {
|
|
this.triggerModalClose();
|
|
});
|
|
},
|
|
|
|
buildTableMarkdown(headers, data) {
|
|
const table = [];
|
|
data.forEach((row) => {
|
|
const result = {};
|
|
|
|
headers.forEach((key, index) => (result[key] = row[index]));
|
|
table.push(result);
|
|
});
|
|
|
|
return arrayToTable(table);
|
|
},
|
|
});
|