2022-07-14 11:56:11 -04:00
|
|
|
import { action } from "@ember/object";
|
|
|
|
import loadScript from "discourse/lib/load-script";
|
2022-08-11 14:33:34 -04:00
|
|
|
import {
|
|
|
|
arrayToTable,
|
|
|
|
findTableRegex,
|
|
|
|
tokenRange,
|
2023-08-18 13:30:20 -04:00
|
|
|
} from "../../discourse-table-builder/lib/utilities";
|
2023-02-16 21:16:48 -05:00
|
|
|
|
2022-08-11 14:59:08 -04:00
|
|
|
import Component from "@glimmer/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-21 17:19:01 -04:00
|
|
|
import { tracked } from "@glimmer/tracking";
|
2023-02-16 21:16:48 -05:00
|
|
|
|
2022-08-11 14:59:08 -04:00
|
|
|
export default class SpreadsheetEditor extends Component {
|
2022-07-21 17:19:01 -04:00
|
|
|
@tracked showEditReason = false;
|
2023-01-04 07:54:43 -05:00
|
|
|
@tracked loading = null;
|
2022-07-21 17:19:01 -04:00
|
|
|
spreadsheet = null;
|
2022-08-10 16:50:38 -04:00
|
|
|
defaultColWidth = 150;
|
2023-08-30 16:36:22 -04:00
|
|
|
isEditingTable = !!this.args.model.tableTokens;
|
2022-07-19 19:33:10 -04:00
|
|
|
|
|
|
|
// Getters:
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2022-07-21 17:19:01 -04:00
|
|
|
}
|
2022-07-19 19:33:10 -04:00
|
|
|
|
|
|
|
// Actions:
|
2022-07-21 17:19:01 -04:00
|
|
|
@action
|
|
|
|
createSpreadsheet(spreadsheet) {
|
|
|
|
this.spreadsheet = spreadsheet;
|
|
|
|
|
|
|
|
schedule("afterRender", () => {
|
|
|
|
this.loadLibraries().then(() => {
|
|
|
|
if (this.isEditingTable) {
|
2023-08-30 16:36:22 -04:00
|
|
|
this.buildPopulatedTable(this.args.model.tableTokens);
|
2022-07-21 17:19:01 -04:00
|
|
|
} else {
|
|
|
|
this.buildNewTable();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
@action
|
|
|
|
showEditReasonField() {
|
2023-08-31 12:28:05 -04:00
|
|
|
this.showEditReason = !this.showEditReason;
|
2022-07-21 17:19:01 -04:00
|
|
|
}
|
2022-07-19 19:33:10 -04:00
|
|
|
|
|
|
|
@action
|
|
|
|
insertTable() {
|
|
|
|
const updatedHeaders = this.spreadsheet.getHeaders().split(","); // keys
|
|
|
|
const updatedData = this.spreadsheet.getData(); // values
|
|
|
|
const markdownTable = this.buildTableMarkdown(updatedHeaders, updatedData);
|
|
|
|
|
|
|
|
if (!this.isEditingTable) {
|
2023-08-30 16:36:22 -04:00
|
|
|
this.args.model.toolbarEvent.addText(markdownTable);
|
|
|
|
return this.args.closeModal();
|
2022-07-19 19:33:10 -04:00
|
|
|
} else {
|
|
|
|
return this.updateTable(markdownTable);
|
|
|
|
}
|
2022-07-21 17:19:01 -04:00
|
|
|
}
|
2022-07-19 19:33:10 -04:00
|
|
|
|
|
|
|
// Helper Methods:
|
2022-07-14 12:21:22 -04:00
|
|
|
loadLibraries() {
|
2022-08-15 11:47:33 -04:00
|
|
|
this.loading = true;
|
|
|
|
return loadScript(settings.theme_uploads_local.jsuites)
|
|
|
|
.then(() => {
|
|
|
|
return loadScript(settings.theme_uploads_local.jspreadsheet);
|
|
|
|
})
|
|
|
|
.finally(() => (this.loading = false));
|
2022-07-21 17:19:01 -04:00
|
|
|
}
|
2022-07-14 12:21:22 -04:00
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
buildNewTable() {
|
|
|
|
const data = [
|
|
|
|
["", "", ""],
|
|
|
|
["", "", ""],
|
|
|
|
["", "", ""],
|
2022-08-12 14:11:27 -04:00
|
|
|
["", "", ""],
|
|
|
|
["", "", ""],
|
|
|
|
["", "", ""],
|
2022-07-19 19:33:10 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
const columns = [
|
2022-08-10 16:50:38 -04:00
|
|
|
{
|
|
|
|
title: I18n.t(
|
|
|
|
themePrefix("discourse_table_builder.default_header.col_1")
|
|
|
|
),
|
|
|
|
width: this.defaultColWidth,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: I18n.t(
|
|
|
|
themePrefix("discourse_table_builder.default_header.col_2")
|
|
|
|
),
|
|
|
|
width: this.defaultColWidth,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: I18n.t(
|
|
|
|
themePrefix("discourse_table_builder.default_header.col_3")
|
|
|
|
),
|
|
|
|
width: this.defaultColWidth,
|
|
|
|
},
|
2022-08-12 14:11:27 -04:00
|
|
|
{
|
|
|
|
title: I18n.t(
|
|
|
|
themePrefix("discourse_table_builder.default_header.col_4")
|
|
|
|
),
|
|
|
|
width: this.defaultColWidth,
|
|
|
|
},
|
2022-07-19 19:33:10 -04:00
|
|
|
];
|
|
|
|
|
|
|
|
return this.buildSpreadsheet(data, columns);
|
2022-07-21 17:19:01 -04:00
|
|
|
}
|
2022-07-19 19:33:10 -04:00
|
|
|
|
2022-08-09 19:51:17 -04:00
|
|
|
extractTableContent(data) {
|
|
|
|
return data
|
|
|
|
.flat()
|
|
|
|
.filter((t) => t.type === "inline")
|
|
|
|
.map((t) => t.content);
|
|
|
|
}
|
2022-07-16 14:00:05 -04:00
|
|
|
|
2022-08-09 19:51:17 -04:00
|
|
|
buildPopulatedTable(tableTokens) {
|
|
|
|
const contentRows = tokenRange(tableTokens, "tr_open", "tr_close");
|
|
|
|
const rows = [];
|
|
|
|
let headings;
|
|
|
|
const rowWidthFactor = 8;
|
|
|
|
|
|
|
|
contentRows.forEach((row, index) => {
|
|
|
|
if (index === 0) {
|
|
|
|
// headings
|
|
|
|
headings = this.extractTableContent(row).map((heading) => {
|
|
|
|
return {
|
|
|
|
title: heading,
|
2022-08-10 16:50:38 -04:00
|
|
|
width: Math.max(
|
|
|
|
heading.length * rowWidthFactor,
|
|
|
|
this.defaultColWidth
|
|
|
|
),
|
|
|
|
align: "left",
|
2022-08-09 19:51:17 -04:00
|
|
|
};
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// rows:
|
2022-08-10 16:50:38 -04:00
|
|
|
rows.push(this.extractTableContent(row));
|
2022-07-14 11:56:11 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-08-09 19:51:17 -04:00
|
|
|
return this.buildSpreadsheet(rows, headings);
|
2022-07-21 17:19:01 -04:00
|
|
|
}
|
2022-07-19 19:33:10 -04:00
|
|
|
|
|
|
|
buildSpreadsheet(data, columns, opts = {}) {
|
2022-08-12 15:10:04 -04:00
|
|
|
const postNumber = this.args.model?.post_number;
|
|
|
|
const exportFileName = postNumber
|
|
|
|
? `post-${postNumber}-table-export`
|
|
|
|
: `post-table-export`;
|
|
|
|
|
2022-07-14 12:21:22 -04:00
|
|
|
// eslint-disable-next-line no-undef
|
2022-07-21 17:19:01 -04:00
|
|
|
this.spreadsheet = jspreadsheet(this.spreadsheet, {
|
2022-07-19 19:33:10 -04:00
|
|
|
data,
|
2022-07-14 11:56:11 -04:00
|
|
|
columns,
|
2022-08-10 16:50:38 -04:00
|
|
|
defaultColAlign: "left",
|
|
|
|
wordWrap: true,
|
2022-08-12 15:10:04 -04:00
|
|
|
csvFileName: exportFileName,
|
2023-09-01 14:55:36 -04:00
|
|
|
text: this.localeMapping(),
|
2022-07-19 19:33:10 -04:00
|
|
|
...opts,
|
2022-07-14 11:56:11 -04:00
|
|
|
});
|
2022-07-21 17:19:01 -04:00
|
|
|
}
|
2022-07-14 11:56:11 -04:00
|
|
|
|
2023-04-25 10:23:28 -04:00
|
|
|
buildUpdatedPost(tableIndex, raw, newRaw) {
|
2022-07-19 19:33:10 -04:00
|
|
|
const tableToEdit = raw.match(findTableRegex());
|
|
|
|
let editedTable;
|
|
|
|
|
2023-01-04 02:06:25 -05:00
|
|
|
if (tableToEdit.length) {
|
2023-04-25 10:23:28 -04:00
|
|
|
editedTable = raw.replace(tableToEdit[tableIndex], newRaw);
|
2022-07-16 14:00:05 -04:00
|
|
|
} else {
|
2023-01-04 02:06:25 -05:00
|
|
|
return raw;
|
2022-07-16 14:00:05 -04:00
|
|
|
}
|
|
|
|
|
2022-08-12 14:14:01 -04:00
|
|
|
// replace null characters
|
|
|
|
editedTable = editedTable.replace(/\0/g, "\ufffd");
|
2022-07-19 19:33:10 -04:00
|
|
|
return editedTable;
|
2022-07-21 17:19:01 -04:00
|
|
|
}
|
2022-07-14 11:56:11 -04:00
|
|
|
|
2022-07-19 19:33:10 -04:00
|
|
|
updateTable(markdownTable) {
|
2023-08-30 16:36:22 -04:00
|
|
|
const tableIndex = this.args.model.tableIndex;
|
|
|
|
const postId = this.args.model.post.id;
|
2022-07-16 14:00:05 -04:00
|
|
|
const newRaw = markdownTable;
|
2022-07-21 17:19:01 -04:00
|
|
|
|
2022-07-16 14:00:05 -04:00
|
|
|
const editReason =
|
2022-07-21 17:19:01 -04:00
|
|
|
this.editReason ||
|
2022-07-18 14:18:58 -04:00
|
|
|
I18n.t(themePrefix("discourse_table_builder.edit.default_edit_reason"));
|
2023-08-30 16:36:22 -04:00
|
|
|
const raw = this.args.model.post.raw;
|
2023-04-25 10:23:28 -04:00
|
|
|
const newPostRaw = this.buildUpdatedPost(tableIndex, 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-21 17:19:01 -04:00
|
|
|
}
|
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(() => {
|
2023-08-30 16:36:22 -04:00
|
|
|
this.args.closeModal();
|
2022-07-16 14:00:05 -04:00
|
|
|
});
|
2022-07-21 17:19:01 -04:00
|
|
|
}
|
2022-07-14 20:07:05 -04:00
|
|
|
|
|
|
|
buildTableMarkdown(headers, data) {
|
|
|
|
const table = [];
|
|
|
|
data.forEach((row) => {
|
|
|
|
const result = {};
|
|
|
|
|
2023-02-16 21:16:48 -05:00
|
|
|
headers.forEach((_key, index) => {
|
|
|
|
const columnKey = `col${index}`;
|
|
|
|
return (result[columnKey] = row[index]);
|
|
|
|
});
|
2022-07-14 20:07:05 -04:00
|
|
|
table.push(result);
|
|
|
|
});
|
|
|
|
|
2023-02-16 21:16:48 -05:00
|
|
|
return arrayToTable(table, headers);
|
2022-07-21 17:19:01 -04:00
|
|
|
}
|
2023-09-01 14:55:36 -04:00
|
|
|
|
|
|
|
localeMapping() {
|
|
|
|
return {
|
|
|
|
noRecordsFound: prefixedLocale("no_records_found"),
|
|
|
|
show: prefixedLocale("show"),
|
|
|
|
entries: prefixedLocale("entries"),
|
|
|
|
insertANewColumnBefore: prefixedLocale("context_menu.col.before"),
|
|
|
|
insertANewColumnAfter: prefixedLocale("context_menu.col.after"),
|
|
|
|
deleteSelectedColumns: prefixedLocale("context_menu.col.delete"),
|
|
|
|
renameThisColumn: prefixedLocale("context_menu.col.rename"),
|
|
|
|
orderAscending: prefixedLocale("context_menu.order.ascending"),
|
|
|
|
orderDescending: prefixedLocale("context_menu.order.descending"),
|
|
|
|
insertANewRowBefore: prefixedLocale("context_menu.row.before"),
|
|
|
|
insertANewRowAfter: prefixedLocale("context_menu.row.after"),
|
|
|
|
deleteSelectedRows: prefixedLocale("context_menu.row.delete"),
|
|
|
|
copy: prefixedLocale("context_menu.copy"),
|
|
|
|
paste: prefixedLocale("context_menu.paste"),
|
|
|
|
saveAs: prefixedLocale("context_menu.save"),
|
|
|
|
about: prefixedLocale("about"),
|
|
|
|
areYouSureToDeleteTheSelectedRows: prefixedLocale(
|
|
|
|
"prompts.delete_selected_rows"
|
|
|
|
),
|
|
|
|
areYouSureToDeleteTheSelectedColumns: prefixedLocale(
|
|
|
|
"prompts.delete_selected_cols"
|
|
|
|
),
|
|
|
|
thisActionWillDestroyAnyExistingMergedCellsAreYouSure: prefixedLocale(
|
|
|
|
"prompts.will_destroy_merged_cells"
|
|
|
|
),
|
|
|
|
thisActionWillClearYourSearchResultsAreYouSure: prefixedLocale(
|
|
|
|
"prompts.will_clear_search_results"
|
|
|
|
),
|
|
|
|
thereIsAConflictWithAnotherMergedCell: prefixedLocale(
|
|
|
|
"prompts.conflict_with_merged_cells"
|
|
|
|
),
|
|
|
|
invalidMergeProperties: prefixedLocale("invalid_merge_props"),
|
|
|
|
cellAlreadyMerged: prefixedLocale("cells_already_merged"),
|
|
|
|
noCellsSelected: prefixedLocale("no_cells_selected"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function prefixedLocale(localeString) {
|
|
|
|
return I18n.t(
|
|
|
|
themePrefix(`discourse_table_builder.spreadsheet.${localeString}`)
|
|
|
|
);
|
2022-07-21 17:19:01 -04:00
|
|
|
}
|