A platform for community discussion. Free, open, simple.
DEV: allow render-tags to pass classes so we can use it in the history modal (#31755)
Krisb4f962698466
diff --git a/app/assets/javascripts/discourse/app/components/modal/history.js b/app/assets/javascripts/discourse/app/components/modal/history.jsindex 6929a109df1..13efa09936d 100644--- a/app/assets/javascripts/discourse/app/components/modal/history.js+++ b/app/assets/javascripts/discourse/app/components/modal/history.js@@ -126,10 +126,10 @@ export default class History extends Component {get previousTagChanges() {const previousArray = customTagArray(- this.postRevision.tags_changes.previous+ this.postRevision.tags_changes?.previous);const currentSet = new Set(- customTagArray(this.postRevision.tags_changes.current)+ customTagArray(this.postRevision.tags_changes?.current));return previousArray.map((name) => ({@@ -140,9 +140,11 @@ export default class History extends Component {get currentTagChanges() {const previousSet = new Set(- customTagArray(this.postRevision.tags_changes.previous)+ customTagArray(this.postRevision.tags_changes?.previous)+ );+ const currentArray = customTagArray(+ this.postRevision.tags_changes?.current);- const currentArray = customTagArray(this.postRevision.tags_changes.current);return currentArray.map((name) => ({name,diff --git a/app/assets/javascripts/discourse/app/components/modal/history/revisions.gjs b/app/assets/javascripts/discourse/app/components/modal/history/revisions.gjsnew file mode 100644index 00000000000..03362fb2e2a--- /dev/null+++ b/app/assets/javascripts/discourse/app/components/modal/history/revisions.gjs@@ -0,0 +1,143 @@+import Component from "@ember/component";+import { hash } from "@ember/helper";+import EmberObject from "@ember/object";+import didInsert from "@ember/render-modifiers/modifiers/did-insert";+import didUpdate from "@ember/render-modifiers/modifiers/did-update";+import { and, eq, not, or } from "truth-helpers";+import LinksRedirect from "discourse/components/links-redirect";+import PluginOutlet from "discourse/components/plugin-outlet";+import Avatar from "discourse/helpers/bound-avatar-template";+import icon from "discourse/helpers/d-icon";+import discourseTags from "discourse/helpers/discourse-tags";+import htmlSafe from "discourse/helpers/html-safe";++function tagClasses(tagChanges, state, className) {+ return (tagChanges || []).reduce((classMap, tagChange) => {+ if (tagChange[state]) {+ classMap[tagChange.name] = className;+ }+ return classMap;+ }, {});+}++export default class Revisions extends Component {+ get fakePreviousTagsTopic() {+ // discourseTags expects a topic structure+ return EmberObject.create({+ tags: (this.get("previousTagChanges") || []).map((tag) => tag.name),+ });+ }++ get previousTagClassesMap() {+ return tagClasses(this.get("previousTagChanges"), "deleted", "diff-del");+ }++ get fakeCurrentTagsTopic() {+ return EmberObject.create({+ tags: (this.get("currentTagChanges") || []).map((tag) => tag.name),+ });+ }++ get currentTagClassesMap() {+ return tagClasses(this.get("currentTagChanges"), "inserted", "diff-ins");+ }++ <template>+ <div+ id="revisions"+ data-post-id={{@model.post_id}}+ class={{@hiddenClasses}}+ >+ {{#if @model.title_changes}}+ <div class="row">+ <h2 class="revision__title">{{htmlSafe @titleDiff}}</h2>+ </div>+ {{/if}}+ {{#if @mobileView}}+ {{#if @userChanges}}+ <div class="row">+ {{Avatar @model.user_changes.previous.avatar_template "small"}}+ {{@model.user_changes.previous.username}}+ →+ {{Avatar @model.user_changes.current.avatar_template "small"}}+ {{@model.user_changes.current.username}}+ </div>+ {{/if}}+ {{#if @model.wiki_changes}}+ <div class="row">+ {{icon+ "far-pen-to-square"+ class=(if @model.wiki_changes.current "diff-ins" "diff-del")+ }}+ </div>+ {{/if}}+ {{#if @model.archetype_changes}}+ <div class="row">+ {{icon+ (if+ (eq @model.archetype_changes.current "private_message")+ "envelope"+ "comment"+ )+ }}+ </div>+ {{/if}}+ {{#if (and @model.category_id_changes (not @model.archetype_changes))}}+ <div class="row">+ {{#if @previousCategory}}+ {{htmlSafe @previousCategory}}+ {{else}}+ {{icon "far-eye-slash" class="diff-del"}}+ {{/if}}+ →+ {{#if @currentCategory}}+ {{htmlSafe @currentCategory}}+ {{else}}+ {{icon "far-eye-slash" class="diff-ins"}}+ {{/if}}+ </div>+ {{/if}}+ {{/if}}+ {{#if @model.tags_changes}}+ <div class="row -tag-revisions">+ {{discourseTags+ this.fakePreviousTagsTopic+ tagClasses=this.previousTagClassesMap+ }}++ {{#if (or @mobileView (eq @viewMode "inline"))}}+ → + {{/if}}++ {{discourseTags+ this.fakeCurrentTagsTopic+ tagClasses=this.currentTagClassesMap+ }}+ </div>+ {{/if}}+ {{#if @model.featured_link_changes}}+ <div class="row">+ {{@model.featured_link_changes.previous}}+ →+ {{@model.featured_link_changes.current}}+ </div>+ {{/if}}++ <span>+ <PluginOutlet+ @name="post-revisions"+ @connectorTagName="div"+ @outletArgs={{hash model=@model}}+ />+ </span>++ <LinksRedirect+ {{didInsert @calculateBodyDiff @bodyDiffHTML}}+ {{didUpdate @calculateBodyDiff @bodyDiffHTML}}+ class="row body-diff"+ >+ {{htmlSafe @bodyDiff}}+ </LinksRedirect>+ </div>+ </template>+}diff --git a/app/assets/javascripts/discourse/app/components/modal/history/revisions.hbs b/app/assets/javascripts/discourse/app/components/modal/history/revisions.hbsdeleted file mode 100644index ea47b790e7e..00000000000--- a/app/assets/javascripts/discourse/app/components/modal/history/revisions.hbs+++ /dev/null@@ -1,98 +0,0 @@-<div id="revisions" data-post-id={{@model.post_id}} class={{@hiddenClasses}}>- {{#if @model.title_changes}}- <div class="row">- <h2 class="revision__title">{{html-safe @titleDiff}}</h2>- </div>- {{/if}}- {{#if @mobileView}}- {{#if @userChanges}}- <div class="row">- {{bound-avatar-template- @model.user_changes.previous.avatar_template- "small"- }}- {{@model.user_changes.previous.username}}- →- {{bound-avatar-template- @model.user_changes.current.avatar_template- "small"- }}- {{@model.user_changes.current.username}}- </div>- {{/if}}- {{#if @model.wiki_changes}}- <div class="row">- {{d-icon- "far-pen-to-square"- class=(if @model.wiki_changes.current "diff-ins" "diff-del")- }}- </div>- {{/if}}- {{#if @model.archetype_changes}}- <div class="row">- {{d-icon- (if- (eq @model.archetype_changes.current "private_message")- "envelope"- "comment"- )- }}- </div>- {{/if}}- {{#if (and @model.category_id_changes (not @model.archetype_changes))}}- <div class="row">- {{#if @previousCategory}}- {{html-safe @previousCategory}}- {{else}}- {{d-icon "far-eye-slash" class="diff-del"}}- {{/if}}- →- {{#if @currentCategory}}- {{html-safe @currentCategory}}- {{else}}- {{d-icon "far-eye-slash" class="diff-ins"}}- {{/if}}- </div>- {{/if}}- {{/if}}- {{#if @model.tags_changes}}- <div class="row -tag-revisions">- <span class="discourse-tags">- {{#each @previousTagChanges as |t|}}- {{discourse-tag t.name extraClass=(if t.deleted "diff-del")}}- {{/each}}- </span>- {{#if (or @mobileView (eq @viewMode "inline"))}}- → - {{/if}}- <span class="discourse-tags">- {{#each @currentTagChanges as |t|}}- {{discourse-tag t.name extraClass=(if t.inserted "diff-ins")}}- {{/each}}- </span>- </div>- {{/if}}- {{#if @model.featured_link_changes}}- <div class="row">- {{@model.featured_link_changes.previous}}- →- {{@model.featured_link_changes.current}}- </div>- {{/if}}-- <span>- <PluginOutlet- @name="post-revisions"- @connectorTagName="div"- @outletArgs={{hash model=@model}}- />- </span>-- <LinksRedirect- {{did-insert @calculateBodyDiff @bodyDiffHTML}}- {{did-update @calculateBodyDiff @bodyDiffHTML}}- class="row body-diff"- >- {{html-safe @bodyDiff}}- </LinksRedirect>-</div>\ No newline at end of filediff --git a/app/assets/javascripts/discourse/app/lib/render-tags.js b/app/assets/javascripts/discourse/app/lib/render-tags.jsindex b168311926e..bceba1bf706 100644--- a/app/assets/javascripts/discourse/app/lib/render-tags.js+++ b/app/assets/javascripts/discourse/app/lib/render-tags.js@@ -77,12 +77,19 @@ export default function (topic, params) {if (tags && tags.length > 0) {for (let i = 0; i < tags.length; i++) {- buffer += renderTag(tags[i], {- description:- topic.tags_descriptions && topic.tags_descriptions[tags[i]],+ const tag = tags[i];+ const tagParams = params ? { ...params } : {};++ if (params?.tagClasses && params?.tagClasses[tag]) {+ tagParams.extraClass = params.tagClasses[tag];+ }++ buffer += renderTag(tag, {+ description: topic.tags_descriptions && topic.tags_descriptions[tag],isPrivateMessage,tagsForUser,tagName,+ ...tagParams,});// separator after each tagdiff --git a/app/assets/javascripts/discourse/tests/unit/lib/render-tag-test.js b/app/assets/javascripts/discourse/tests/unit/lib/render-tag-test.jsindex 3b4dd0bc1b6..cf49447e507 100644--- a/app/assets/javascripts/discourse/tests/unit/lib/render-tag-test.js+++ b/app/assets/javascripts/discourse/tests/unit/lib/render-tag-test.js@@ -20,4 +20,23 @@ module("Unit | Utility | render-tag", function (hooks) {"removes any html tags from description");});++ test("renderTag with extraClass", function (assert) {+ const result = renderTag("foo", {+ extraClass: "ins del",+ description: "foo description",+ });++ const div = document.createElement("div");+ div.innerHTML = result;+ const link = div.firstChild;++ assert.true(+ link.classList.contains("discourse-tag"),+ "has discourse-tag class"+ );+ assert.true(link.classList.contains("simple"), "has default simple class");+ assert.true(link.classList.contains("ins"), "has ins class");+ assert.true(link.classList.contains("del"), "has del class");+ });});diff --git a/app/assets/javascripts/discourse/tests/unit/lib/render-tags-test.js b/app/assets/javascripts/discourse/tests/unit/lib/render-tags-test.jsnew file mode 100644index 00000000000..dea58079e9d--- /dev/null+++ b/app/assets/javascripts/discourse/tests/unit/lib/render-tags-test.js@@ -0,0 +1,41 @@+import EmberObject from "@ember/object";+import { setupTest } from "ember-qunit";+import { module, test } from "qunit";+import renderTags from "discourse/lib/render-tags";++module("Unit | Utility | render-tags", function (hooks) {+ setupTest(hooks);++ test("renderTags with tagClasses param", function (assert) {+ const topic = EmberObject.create({+ tags: ["cat", "dog"],+ get(prop) {+ return this[prop];+ },+ });++ const tagClasses = {+ cat: "meow",+ dog: "woof",+ };++ const classResult = renderTags(topic, { tagClasses });+ const classDiv = document.createElement("div");+ classDiv.innerHTML = classResult;++ const catTagLink = classDiv.querySelector('[data-tag-name="cat"]');+ const dogTagLink = classDiv.querySelector('[data-tag-name="dog"]');++ assert.notStrictEqual(catTagLink, null, "cat tag exists");+ assert.notStrictEqual(dogTagLink, null, "dog tag exists");++ assert.true(+ catTagLink.classList.contains("meow"),+ "adds the meow class to the cat tag"+ );+ assert.true(+ dogTagLink.classList.contains("woof"),+ "adds the woof class to the dog tag"+ );+ });+});diff --git a/app/assets/stylesheets/common/base/history.scss b/app/assets/stylesheets/common/base/history.scssindex 1b1e97224b9..3423d75aaa8 100644--- a/app/assets/stylesheets/common/base/history.scss+++ b/app/assets/stylesheets/common/base/history.scss@@ -111,7 +111,7 @@}&:not(.--mode-inline) {- .-tag-revisions span {+ .-tag-revisions .discourse-tags {flex: 0 1 50%;min-width: 0;align-self: start;