From b7a0a22e95601298dc9e2616e073d9ade1c8ae60 Mon Sep 17 00:00:00 2001 From: Keegan George Date: Mon, 18 Jul 2022 11:18:58 -0700 Subject: [PATCH] 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 --- .../api-initializers/table-editor.js | 2 +- .../components/spreadsheet-editor.js | 19 ++++++++++++------- javascripts/discourse/lib/utilities.js | 4 ++++ .../components/spreadsheet-editor.hbs | 4 +--- .../templates/modal/table-editor-modal.hbs | 2 +- locales/en.yml | 1 + 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/javascripts/discourse/api-initializers/table-editor.js b/javascripts/discourse/api-initializers/table-editor.js index 7ab220f..4a16fb9 100644 --- a/javascripts/discourse/api-initializers/table-editor.js +++ b/javascripts/discourse/api-initializers/table-editor.js @@ -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"); diff --git a/javascripts/discourse/components/spreadsheet-editor.js b/javascripts/discourse/components/spreadsheet-editor.js index e6c2b66..4f878be 100644 --- a/javascripts/discourse/components/spreadsheet-editor.js +++ b/javascripts/discourse/components/spreadsheet-editor.js @@ -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) { diff --git a/javascripts/discourse/lib/utilities.js b/javascripts/discourse/lib/utilities.js index 54bea0c..2bcc3b4 100644 --- a/javascripts/discourse/lib/utilities.js +++ b/javascripts/discourse/lib/utilities.js @@ -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; } diff --git a/javascripts/discourse/templates/components/spreadsheet-editor.hbs b/javascripts/discourse/templates/components/spreadsheet-editor.hbs index 96c023c..5c38d67 100644 --- a/javascripts/discourse/templates/components/spreadsheet-editor.hbs +++ b/javascripts/discourse/templates/components/spreadsheet-editor.hbs @@ -2,7 +2,6 @@ @title={{theme-prefix "discourse_table_builder.edit.modal.title"}} @class="table-editor-modal" > -
- -
\ No newline at end of file + diff --git a/javascripts/discourse/templates/modal/table-editor-modal.hbs b/javascripts/discourse/templates/modal/table-editor-modal.hbs index 95e3002..b820d55 100644 --- a/javascripts/discourse/templates/modal/table-editor-modal.hbs +++ b/javascripts/discourse/templates/modal/table-editor-modal.hbs @@ -3,4 +3,4 @@ @tableHtml={{this.tableHtml}} @tableId={{this.tableId}} @triggerModalClose={{action "closeEditModal"}} -/> \ No newline at end of file +/> diff --git a/locales/en.yml b/locales/en.yml index c6e6a84..360aba5 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -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"