2022-07-14 11:56:11 -04:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import loadScript from "discourse/lib/load-script";
|
2022-07-18 14:18:58 -04:00
|
|
|
import { arrayToTable, findTableRegex, tableToObj } from "../lib/utilities";
|
2022-07-14 11:56:11 -04:00
|
|
|
import Component from "@ember/component";
|
2022-07-16 14:00:05 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2022-07-18 14:18:58 -04:00
|
|
|
import I18n from "I18n";
|
2022-07-19 19:33:10 -04:00
|
|
|
import { schedule } from "@ember/runloop";
|
2022-07-14 11:56:11 -04:00
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
tagName: "",
|
2022-07-16 14:00:05 -04:00
|
|
|
showEditReason: false,
|
2022-07-19 19:33:10 -04:00
|
|
|
spreadsheet: null,
|
2022-07-14 11:56:11 -04:00
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
// Lifecycle Methods:
|
2022-07-14 11:56:11 -04:00
|
|
|
didInsertElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
schedule("afterRender", () => {
|
|
|
|
this.loadLibraries().then(() => {
|
|
|
|
if (this.isEditingTable) {
|
|
|
|
this.buildPopulatedTable(this.tableHtml);
|
|
|
|
} else {
|
|
|
|
this.buildNewTable();
|
|
|
|
}
|
|
|
|
});
|
2022-07-14 11:56:11 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
willDestroyElement() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.spreadsheet?.destroy();
|
|
|
|
},
|
|
|
|
|
|
|
|
// Getters:
|
|
|
|
get isEditingTable() {
|
|
|
|
if (this.tableHtml) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
get modalAttributes() {
|
|
|
|
if (this.isEditingTable) {
|
|
|
|
return {
|
|
|
|
title: themePrefix("discourse_table_builder.edit.modal.title"),
|
|
|
|
insertTable: {
|
|
|
|
title: themePrefix("discourse_table_builder.edit.modal.create"),
|
|
|
|
icon: "pencil-alt",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
title: themePrefix("discourse_table_builder.modal.title"),
|
|
|
|
insertTable: {
|
|
|
|
title: themePrefix("discourse_table_builder.modal.create"),
|
|
|
|
icon: "plus",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Actions:
|
|
|
|
@action
|
|
|
|
showEditReasonField() {
|
|
|
|
if (this.showEditReason) {
|
|
|
|
return this.set("showEditReason", false);
|
|
|
|
} else {
|
|
|
|
return this.set("showEditReason", true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
cancelTableInsertion() {
|
|
|
|
this.triggerModalClose();
|
|
|
|
},
|
|
|
|
|
|
|
|
@action
|
|
|
|
insertTable() {
|
|
|
|
const updatedHeaders = this.spreadsheet.getHeaders().split(","); // keys
|
|
|
|
const updatedData = this.spreadsheet.getData(); // values
|
|
|
|
const markdownTable = this.buildTableMarkdown(updatedHeaders, updatedData);
|
|
|
|
|
|
|
|
if (!this.isEditingTable) {
|
|
|
|
this.toolbarEvent.addText(markdownTable);
|
|
|
|
return this.triggerModalClose();
|
|
|
|
} else {
|
|
|
|
return this.updateTable(markdownTable);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// Helper Methods:
|
2022-07-14 12:21:22 -04:00
|
|
|
loadLibraries() {
|
|
|
|
return loadScript(settings.theme_uploads.jsuites).then(() => {
|
|
|
|
return loadScript(settings.theme_uploads.jspreadsheet);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
buildNewTable() {
|
|
|
|
const data = [
|
|
|
|
["", "", ""],
|
|
|
|
["", "", ""],
|
|
|
|
["", "", ""],
|
|
|
|
];
|
|
|
|
|
|
|
|
const columns = [
|
|
|
|
{ title: "Column 1", width: 150 },
|
|
|
|
{ title: "Column 2", width: 150 },
|
|
|
|
{ title: "Column 3", width: 150 },
|
|
|
|
];
|
|
|
|
|
|
|
|
return this.buildSpreadsheet(data, columns);
|
|
|
|
},
|
|
|
|
|
|
|
|
buildPopulatedTable(table) {
|
2022-07-14 11:56:11 -04:00
|
|
|
const tableObject = tableToObj(table);
|
|
|
|
const headings = [];
|
|
|
|
const tableData = [];
|
2022-07-16 14:00:05 -04:00
|
|
|
|
2022-07-14 11:56:11 -04:00
|
|
|
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,
|
2022-07-14 13:05:27 -04:00
|
|
|
width: heading.length * 15,
|
2022-07-14 11:56:11 -04:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
return this.buildSpreadsheet(tableData, columns);
|
|
|
|
},
|
|
|
|
|
|
|
|
buildSpreadsheet(data, columns, opts = {}) {
|
2022-07-14 11:56:11 -04:00
|
|
|
const spreadsheetContainer = document.querySelector("#spreadsheet");
|
|
|
|
|
2022-07-14 12:21:22 -04:00
|
|
|
// eslint-disable-next-line no-undef
|
2022-07-14 11:56:11 -04:00
|
|
|
this.spreadsheet = jspreadsheet(spreadsheetContainer, {
|
2022-07-19 19:33:10 -04:00
|
|
|
data,
|
2022-07-14 11:56:11 -04:00
|
|
|
columns,
|
2022-07-19 19:33:10 -04:00
|
|
|
...opts,
|
2022-07-14 11:56:11 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
buildUpdatedPost(tableId, raw, newRaw) {
|
|
|
|
const tableToEdit = raw.match(findTableRegex());
|
|
|
|
let editedTable;
|
|
|
|
|
|
|
|
if (tableToEdit.length > 1) {
|
|
|
|
editedTable = raw.replace(tableToEdit[tableId], newRaw);
|
2022-07-16 14:00:05 -04:00
|
|
|
} else {
|
2022-07-19 19:33:10 -04:00
|
|
|
editedTable = raw.replace(findTableRegex(), newRaw);
|
2022-07-16 14:00:05 -04:00
|
|
|
}
|
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
return editedTable;
|
2022-07-14 11:56:11 -04:00
|
|
|
},
|
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
updateTable(markdownTable) {
|
2022-07-18 14:18:58 -04:00
|
|
|
const tableId = this.get("tableId");
|
2022-07-16 14:00:05 -04:00
|
|
|
const postId = this.model.id;
|
|
|
|
const newRaw = markdownTable;
|
|
|
|
const editReason =
|
2022-07-18 14:18:58 -04:00
|
|
|
this.get("editReason") ||
|
|
|
|
I18n.t(themePrefix("discourse_table_builder.edit.default_edit_reason"));
|
2022-07-18 14:03:57 -04:00
|
|
|
const raw = this.model.raw;
|
2022-07-18 14:18:58 -04:00
|
|
|
const newPostRaw = this.buildUpdatedPost(tableId, raw, newRaw);
|
2022-07-18 14:03:57 -04:00
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
return this.sendTableUpdate(postId, newPostRaw, editReason);
|
2022-07-16 14:00:05 -04:00
|
|
|
},
|
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
sendTableUpdate(postId, raw, edit_reason) {
|
2022-07-16 14:00:05 -04:00
|
|
|
return ajax(`/posts/${postId}.json`, {
|
|
|
|
type: "PUT",
|
|
|
|
data: {
|
|
|
|
post: {
|
|
|
|
raw,
|
|
|
|
edit_reason,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError)
|
|
|
|
.finally(() => {
|
|
|
|
this.triggerModalClose();
|
|
|
|
});
|
2022-07-14 11:56:11 -04:00
|
|
|
},
|
2022-07-14 20:07:05 -04:00
|
|
|
|
|
|
|
buildTableMarkdown(headers, data) {
|
|
|
|
const table = [];
|
|
|
|
data.forEach((row) => {
|
|
|
|
const result = {};
|
|
|
|
|
|
|
|
headers.forEach((key, index) => (result[key] = row[index]));
|
|
|
|
table.push(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
return arrayToTable(table);
|
|
|
|
},
|
2022-07-14 11:56:11 -04:00
|
|
|
});
|