DEV: Remove jQuery animate calls (#15321)

Affects j/k navigation and PM interaction with @discobot.
This commit is contained in:
Penar Musaraj
2021-12-16 11:00:09 -05:00
committed by GitHub
parent 9365c4b364
commit 48b7696dbc
3 changed files with 70 additions and 54 deletions

View File

@@ -1,4 +1,6 @@
import { ajax } from "discourse/lib/ajax";
import discourseDebounce from "discourse-common/lib/debounce";
import isElementInViewport from "discourse/lib/is-element-in-viewport";
import { withPluginApi } from "discourse/lib/plugin-api";
const PLUGIN_ID = "new-user-narrative";
@@ -39,6 +41,68 @@ function initialize(api) {
}
return this._super(bookmark, post);
},
subscribe() {
this._super(...arguments);
this.messageBus.subscribe(`/topic/${this.get("model.id")}`, (data) => {
const topic = this.model;
// scroll only for discobot (-2 is discobot id)
if (
topic.get("isPrivateMessage") &&
this.currentUser &&
this.currentUser.get("id") !== data.user_id &&
data.user_id === -2 &&
data.type === "created"
) {
const postNumber = data.post_number;
const notInPostStream =
topic.get("highest_post_number") <= postNumber;
const postNumberDifference = postNumber - topic.get("currentPost");
if (
notInPostStream &&
postNumberDifference > 0 &&
postNumberDifference < 7
) {
this._scrollToDiscobotPost(data.post_number);
}
}
});
// No need to unsubscribe, core unsubscribes /topic/* routes
},
_scrollToDiscobotPost(postNumber) {
discourseDebounce(
this,
function () {
const post = document.querySelector(
`.topic-post article#post_${postNumber}`
);
if (!post || isElementInViewport(post)) {
return;
}
const headerOffset =
parseInt(
getComputedStyle(document.body).getPropertyValue(
"--header-offset"
),
10
) || 0;
const viewportOffset = post.getBoundingClientRect();
window.scrollTo({
top: window.scrollY + viewportOffset.top - headerOffset,
behavior: "smooth",
});
},
postNumber,
500
);
},
});
api.attachWidgetAction("header", "headerSearchContextTrigger", function () {