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();
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");
const expandBtn = document.querySelector(".open-popup-link");

View File

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

View File

@ -86,6 +86,10 @@ export function arrayToTable(array, columns, alignment = "center") {
return table;
}
/**
*
* @returns a regular experssion finding all markdown tables
*/
export function findTableRegex() {
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"}}
@class="table-editor-modal"
>
<div class="edit-reason">
<DButton
@icon="info-circle"
@ -31,10 +30,9 @@
@icon="pencil-alt"
@action={{action "editTable"}}
/>
<DButton
@class="btn-flat"
@label={{theme-prefix "discourse_table_builder.edit.modal.cancel"}}
@action={{action "cancelTableEdit"}}
/>
</div>
</div>

View File

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

View File

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