DEV: Remove INLINE_ONEBOX_* constants

DEV: Remove INLINE_ONEBOX_* constants

There were two constants here, INLINE_ONEBOX_LOADING_CSS_CLASS and INLINE_ONEBOX_CSS_CLASS that were both longer than the strings they were DRYing up: inline-onebox-loading and inline-onebox

I normally appreciate constants, but in this case it meant that we had a lot of JS imports resulting in many more lines of code (and CPU cycles spent figuring them out.)

It also meant we had an .erb file and had to invoke Ruby to create the JS file, which meant the app was harder to port to Ember CLI.

I removed the constants. It’s less DRY but faster and simpler, and arguably the loss of DRYness is not significant as you can still search for the inline-onebox-loading and inline-onebox strings easily if you are refactoring.

diff --git a/app/assets/javascripts/discourse/app/components/composer-editor.js b/app/assets/javascripts/discourse/app/components/composer-editor.js
index 7d89caa..7af8483 100644
--- a/app/assets/javascripts/discourse/app/components/composer-editor.js
+++ b/app/assets/javascripts/discourse/app/components/composer-editor.js
@@ -43,10 +43,6 @@ import {
   cacheShortUploadUrl,
   resolveAllShortUrls
 } from "pretty-text/upload-short-url";
-import {
-  INLINE_ONEBOX_LOADING_CSS_CLASS,
-  INLINE_ONEBOX_CSS_CLASS
-} from "pretty-text/context/inline-onebox-css-classes";
 import ENV from "discourse-common/config/environment";
 
 const REBUILD_SCROLL_MAP_EVENTS = ["composer:resized", "composer:typed-reply"];
@@ -968,32 +964,29 @@ export default Component.extend({
           // Inline Oneboxes = `a.inline-onebox-loading` -> `a.inline-onebox`
 
           let loadedOneboxes = $preview.find(
-            `aside.onebox, a.${LOADING_ONEBOX_CSS_CLASS}, a.${INLINE_ONEBOX_CSS_CLASS}`
+            `aside.onebox, a.${LOADING_ONEBOX_CSS_CLASS}, a.inline-onebox-loading`
           ).length;
 
-          $preview
-            .find(`a.onebox, a.${INLINE_ONEBOX_LOADING_CSS_CLASS}`)
-            .each((_, link) => {
-              const $link = $(link);
-              const text = $link.text();
-              const isInline =
-                $link.attr("class") === INLINE_ONEBOX_LOADING_CSS_CLASS;
-              const m = isInline ? inlineOneboxes : oneboxes;
-
-              if (loadedOneboxes < this.siteSettings.max_oneboxes_per_post) {
-                if (m[text] === undefined) {
-                  m[text] = [];
-                  loadedOneboxes++;
-                }
+          $preview.find(`a.onebox, a.inline-onebox-loading`).each((_, link) => {
+            const $link = $(link);
+            const text = $link.text();
+            const isInline = $link.attr("class") === "inline-onebox-loading";
+            const m = isInline ? inlineOneboxes : oneboxes;
+
+            if (loadedOneboxes < this.siteSettings.max_oneboxes_per_post) {
+              if (m[text] === undefined) {
+                m[text] = [];
+                loadedOneboxes++;
+              }
+              m[text].push(link);
+            } else {
+              if (m[text] !== undefined) {
                 m[text].push(link);
-              } else {
-                if (m[text] !== undefined) {
-                  m[text].push(link);
-                } else if (isInline) {
-                  $link.removeClass(INLINE_ONEBOX_LOADING_CSS_CLASS);
-                }
+              } else if (isInline) {
+                $link.removeClass("inline-onebox-loading");
               }
-            });
+            }
+          });
 
           if (Object.keys(oneboxes).length > 0) {
             this._loadOneboxes(oneboxes);
diff --git a/app/assets/javascripts/pretty-text-bundle.js b/app/assets/javascripts/pretty-text-bundle.js
index a2adade..62dd6c1 100644
--- a/app/assets/javascripts/pretty-text-bundle.js
+++ b/app/assets/javascripts/pretty-text-bundle.js
@@ -11,6 +11,5 @@
 //= require ./pretty-text/addon/sanitizer
 //= require ./pretty-text/addon/oneboxer
 //= require ./pretty-text/addon/oneboxer-cache
-//= require ./pretty-text/addon/context/inline-onebox-css-classes
 //= require ./pretty-text/addon/inline-oneboxer
 //= require ./pretty-text/addon/upload-short-url
diff --git a/app/assets/javascripts/pretty-text/addon/context/inline-onebox-css-classes.js.erb b/app/assets/javascripts/pretty-text/addon/context/inline-onebox-css-classes.js.erb
deleted file mode 100644
index a252fb2..0000000
--- a/app/assets/javascripts/pretty-text/addon/context/inline-onebox-css-classes.js.erb
+++ /dev/null
@@ -1,5 +0,0 @@
-export const INLINE_ONEBOX_LOADING_CSS_CLASS =
-  "<%= CookedPostProcessor::INLINE_ONEBOX_LOADING_CSS_CLASS %>";
-
-export const INLINE_ONEBOX_CSS_CLASS =
-  "<%= CookedPostProcessor::INLINE_ONEBOX_CSS_CLASS %>";
diff --git a/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/onebox.js b/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/onebox.js
index 7358bda..94bd5dd 100644
--- a/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/onebox.js
+++ b/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown/onebox.js
@@ -1,9 +1,5 @@
 import { lookupCache } from "pretty-text/oneboxer-cache";
 import { cachedInlineOnebox } from "pretty-text/inline-oneboxer";
-import {
-  INLINE_ONEBOX_LOADING_CSS_CLASS,
-  INLINE_ONEBOX_CSS_CLASS
-} from "pretty-text/context/inline-onebox-css-classes";
 
 const ONEBOX = 1;
 const INLINE = 2;
@@ -103,9 +99,9 @@ function applyOnebox(state, silent) {
 
             if (onebox && onebox.title) {
               text.content = onebox.title;
-              attrs.push(["class", INLINE_ONEBOX_CSS_CLASS]);
+              attrs.push(["class", "inline-onebox"]);
             } else if (!onebox) {
-              attrs.push(["class", INLINE_ONEBOX_LOADING_CSS_CLASS]);
+              attrs.push(["class", "inline-onebox-loading"]);
             }
           }
         }
diff --git a/app/assets/javascripts/pretty-text/addon/inline-oneboxer.js b/app/assets/javascripts/pretty-text/addon/inline-oneboxer.js
index 6645d1d..a4587ed 100644
--- a/app/assets/javascripts/pretty-text/addon/inline-oneboxer.js
+++ b/app/assets/javascripts/pretty-text/addon/inline-oneboxer.js
@@ -1,8 +1,3 @@
-import {
-  INLINE_ONEBOX_LOADING_CSS_CLASS,
-  INLINE_ONEBOX_CSS_CLASS
-} from "pretty-text/context/inline-onebox-css-classes";
-
 const _cache = {};
 
 export function applyInlineOneboxes(inline, ajax, opts) {
@@ -27,8 +22,8 @@ export function applyInlineOneboxes(inline, ajax, opts) {
         links.forEach(link => {
           $(link)
             .text(onebox.title)
-            .addClass(INLINE_ONEBOX_CSS_CLASS)
-            .removeClass(INLINE_ONEBOX_LOADING_CSS_CLASS);
+            .addClass("inline-onebox")
+            .removeClass("inline-onebox-loading");
         });
       }
     });
diff --git a/app/assets/javascripts/pretty-text/addon/white-lister.js b/app/assets/javascripts/pretty-text/addon/white-lister.js
index 8a3a114..88eb290 100644
--- a/app/assets/javascripts/pretty-text/addon/white-lister.js
+++ b/app/assets/javascripts/pretty-text/addon/white-lister.js
@@ -1,8 +1,3 @@
-import {
-  INLINE_ONEBOX_LOADING_CSS_CLASS,
-  INLINE_ONEBOX_CSS_CLASS
-} from "pretty-text/context/inline-onebox-css-classes";
-
 // to match:
 // abcd
 // abcd[test]
@@ -121,8 +116,8 @@ export const DEFAULT_LIST = [
   "a.mention",
   "a.mention-group",
   "a.onebox",
-  `a.${INLINE_ONEBOX_CSS_CLASS}`,
-  `a.${INLINE_ONEBOX_LOADING_CSS_CLASS}`,
+  `a.inline-onebox`,
+  `a.inline-onebox-loading`,
   "a[data-bbcode]",
   "a[name]",
   "a[rel=nofollow]",
diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb
index d2e8f4d..d6fb05a 100644
--- a/lib/cooked_post_processor.rb
+++ b/lib/cooked_post_processor.rb
@@ -4,8 +4,6 @@
 # For example, inserting the onebox content, or image sizes/thumbnails.
 
 class CookedPostProcessor
-  INLINE_ONEBOX_LOADING_CSS_CLASS = "inline-onebox-loading"
-  INLINE_ONEBOX_CSS_CLASS = "inline-onebox"
   LIGHTBOX_WRAPPER_CSS_CLASS = "lightbox-wrapper"
   LOADING_SIZE = 10
   LOADING_COLORS = 32
@@ -518,7 +516,7 @@ class CookedPostProcessor
     oneboxes = {}
     inlineOneboxes = {}
 
-    Oneboxer.apply(@doc, extra_paths: [".#{INLINE_ONEBOX_LOADING_CSS_CLASS}"]) do |url, element|
+    Oneboxer.apply(@doc, extra_paths: [".inline-onebox-loading"]) do |url, element|
       is_onebox = element["class"] == Oneboxer::ONEBOX_CSS_CLASS
       map = is_onebox ? oneboxes : inlineOneboxes
       skip_onebox = limit <= 0 && !map[url]
@@ -724,14 +722,14 @@ class CookedPostProcessor
 
     if title = inline_onebox&.dig(:title)
       element.children = CGI.escapeHTML(title)
-      element.add_class(INLINE_ONEBOX_CSS_CLASS)
+      element.add_class("inline-onebox")
     end
 
     remove_inline_onebox_loading_class(element)
   end
 

[... diff too long, it was truncated ...]

GitHub sha: f9608c0a