DEV: Cleanup

- Linting Fixes (prettier, eslint, ember template lint)
- Reorganize code, moving declarations, consolidating functionality into separate methods
- Add default edit reason locale vs hard-coded string
This commit is contained in:
Keegan George 2022-07-18 11:18:58 -07:00
parent 2ddb6de654
commit b7a0a22e95
6 changed files with 20 additions and 12 deletions

View File

@ -52,7 +52,7 @@ export default apiInitializer("0.11.1", (api) => {
} }
const popupBtn = createButton(); const popupBtn = createButton();
popupBtn.setAttribute("data-table-id", index); popupBtn.setAttribute("data-table-id", index); // sets a table id so each table can be distinctly edited
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");

View File

@ -1,11 +1,11 @@
// import Controller from "@ember/controller"; // import Controller from "@ember/controller";
import { action } from "@ember/object"; import { action } from "@ember/object";
import loadScript from "discourse/lib/load-script"; import loadScript from "discourse/lib/load-script";
import { arrayToTable, tableToObj } from "../lib/utilities"; import { arrayToTable, findTableRegex, 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"; import I18n from "I18n";
export default Component.extend({ export default Component.extend({
tagName: "", tagName: "",
@ -76,15 +76,20 @@ export default Component.extend({
const updatedData = this.spreadsheet.getData(); // values const updatedData = this.spreadsheet.getData(); // values
const markdownTable = this.buildTableMarkdown(updatedHeaders, updatedData); const markdownTable = this.buildTableMarkdown(updatedHeaders, updatedData);
const tableId = this.get("tableId");
const postId = this.model.id; const postId = this.model.id;
const newRaw = markdownTable; const newRaw = markdownTable;
const editReason = const editReason =
this.get("editReason") || "Update Table with Table Editor"; this.get("editReason") ||
const tableId = this.get("tableId"); I18n.t(themePrefix("discourse_table_builder.edit.default_edit_reason"));
const raw = this.model.raw; const raw = this.model.raw;
const tableToEdit = raw.match(findTableRegex()); const newPostRaw = this.buildUpdatedPost(tableId, raw, newRaw);
this.updateTable(postId, newPostRaw, editReason);
},
buildUpdatedPost(tableId, raw, newRaw) {
const tableToEdit = raw.match(findTableRegex());
let editedTable; let editedTable;
if (tableToEdit.length > 1) { if (tableToEdit.length > 1) {
@ -93,7 +98,7 @@ export default Component.extend({
editedTable = raw.replace(findTableRegex(), newRaw); editedTable = raw.replace(findTableRegex(), newRaw);
} }
this.updateTable(postId, editedTable, editReason); return editedTable;
}, },
updateTable(postId, raw, edit_reason) { updateTable(postId, raw, edit_reason) {

View File

@ -86,6 +86,10 @@ export function arrayToTable(array, columns, alignment = "center") {
return table; return table;
} }
/**
*
* @returns a regular experssion finding all markdown tables
*/
export function findTableRegex() { export function findTableRegex() {
return /((\r?){2}|^)([^\r\n]*\|[^\r\n]*(\r?\n)?)+(?=(\r?\n){2}|$)/gm; return /((\r?){2}|^)([^\r\n]*\|[^\r\n]*(\r?\n)?)+(?=(\r?\n){2}|$)/gm;
} }

View File

@ -2,7 +2,6 @@
@title={{theme-prefix "discourse_table_builder.edit.modal.title"}} @title={{theme-prefix "discourse_table_builder.edit.modal.title"}}
@class="table-editor-modal" @class="table-editor-modal"
> >
<div class="edit-reason"> <div class="edit-reason">
<DButton <DButton
@icon="info-circle" @icon="info-circle"
@ -31,10 +30,9 @@
@icon="pencil-alt" @icon="pencil-alt"
@action={{action "editTable"}} @action={{action "editTable"}}
/> />
<DButton <DButton
@class="btn-flat" @class="btn-flat"
@label={{theme-prefix "discourse_table_builder.edit.modal.cancel"}} @label={{theme-prefix "discourse_table_builder.edit.modal.cancel"}}
@action={{action "cancelTableEdit"}} @action={{action "cancelTableEdit"}}
/> />
</div> </div>

View File

@ -3,4 +3,4 @@
@tableHtml={{this.tableHtml}} @tableHtml={{this.tableHtml}}
@tableId={{this.tableId}} @tableId={{this.tableId}}
@triggerModalClose={{action "closeEditModal"}} @triggerModalClose={{action "closeEditModal"}}
/> />

View File

@ -25,5 +25,6 @@ en:
create: "Update Table" create: "Update Table"
reason: "why are you editing?" reason: "why are you editing?"
trigger_reason: "Add reason for edit" trigger_reason: "Add reason for edit"
default_edit_reason: "Update Table with Table Editor"
theme_metadata: theme_metadata:
description: "Adds a button to the composer to easily build tables in markdown" description: "Adds a button to the composer to easily build tables in markdown"