Skip to content

Commit

Permalink
DEV: allows to decorate username selector (#9869)
Browse files Browse the repository at this point in the history
Usage:

```
api.addUsernameSelectorDecorator(username => {
  return iconHTML("calendar-alt");
});
```
  • Loading branch information
jjaffeux committed May 25, 2020
1 parent fd2d7ca commit 8825395
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { htmlSafe } from "@ember/template";
import { registerUnbound } from "discourse-common/lib/helpers";

let usernameDecorators = [];
export function addUsernameSelectorDecorator(decorator) {
usernameDecorators.push(decorator);
}

export function resetUsernameDecorators() {
usernameDecorators = [];
}

export default registerUnbound("decorate-username-selector", username => {
const decorations = [];

usernameDecorators.forEach(decorator => {
decorations.push(decorator(username));
});

return decorations.length ? htmlSafe(decorations.join("")) : "";
});
14 changes: 14 additions & 0 deletions app/assets/javascripts/discourse/app/lib/plugin-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { replaceFormatter } from "discourse/lib/utilities";
import { modifySelectKit } from "select-kit/mixins/plugin-api";
import { addGTMPageChangedCallback } from "discourse/lib/page-tracker";
import { registerCustomAvatarHelper } from "discourse/helpers/user-avatar";
import { addUsernameSelectorDecorator } from "discourse/helpers/decorate-username-selector";
import { disableNameSuppression } from "discourse/widgets/poster-name";
import { registerCustomPostMessageCallback as registerCustomPostMessageCallback1 } from "discourse/controllers/topic";
import Sharing from "discourse/lib/sharing";
Expand Down Expand Up @@ -927,6 +928,19 @@ class PluginApi {
addComposerUploadMarkdownResolver(resolver);
}

/**
* Registers a function to decorate each autocomplete usernames.
*
* Example:
*
* api.appendUsernameDecorator(username => {
* return `<span class="status">[is_away]</class>`;
* })
*/
addUsernameSelectorDecorator(decorator) {
addUsernameSelectorDecorator(decorator);
}

/**
* Registers a "beforeSave" function on the composer. This allows you to
* implement custom logic that will happen before the user makes a post.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{{avatar user imageSize="tiny"}}
<span class='username'>{{format-username user.username}}</span>
<span class='name'>{{user.name}}</span>
{{decorate-username-selector user.username}}
</a>
</li>
{{/each}}
Expand Down
2 changes: 2 additions & 0 deletions test/javascripts/helpers/qunit-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { resetWidgetCleanCallbacks } from "discourse/components/mount-widget";
import { resetTopicTitleDecorators } from "discourse/components/topic-title";
import { resetDecorators as resetPostCookedDecorators } from "discourse/widgets/post-cooked";
import { resetDecorators as resetPluginOutletDecorators } from "discourse/components/plugin-connector";
import { resetUsernameDecorators } from "discourse/helpers/decorate-username-selector";
import { resetCache as resetOneboxCache } from "pretty-text/oneboxer";
import { resetCustomPostMessageCallbacks } from "discourse/controllers/topic";
import User from "discourse/models/user";
Expand Down Expand Up @@ -162,6 +163,7 @@ export function acceptance(name, options) {
resetPostCookedDecorators();
resetPluginOutletDecorators();
resetTopicTitleDecorators();
resetUsernameDecorators();
resetOneboxCache();
resetCustomPostMessageCallbacks();
Discourse._runInitializer("instanceInitializers", function(
Expand Down

0 comments on commit 8825395

Please sign in to comment.