Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[#62712] Highlight the relevant target element when the user gets to a page via a deep link

Judith Roth812f826bf035
diff --git a/app/components/work_packages/activities_tab/journals/item_component.sass b/app/components/work_packages/activities_tab/journals/item_component.sass
index eb2b07557c2..2b0d742e8d9 100644
--- a/app/components/work_packages/activities_tab/journals/item_component.sass
+++ b/app/components/work_packages/activities_tab/journals/item_component.sass
@@ -19,3 +19,12 @@
&--journal-notes-body__internal-comment
border-color: var(--display-brown-borderColor-emphasis)
+ & .--anchor-highlighted
+ border: 2px solid var(--borderColor-accent-emphasis)
+ border-radius: var(--borderRadius-medium) // if the element doesn't have a border yet, it should still look good / the same as our Boxes
+ // Box headers may have their own border (e.g. internal comments) which needs to be removed.
+ & .Box-header
+ margin: 0
+ border-top: none
+ border-left: none
+ border-right: none
diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/auto-scrolling.controller.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/auto-scrolling.controller.ts
index c33148f5e9c..f78d76fec1c 100644
--- a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/auto-scrolling.controller.ts
+++ b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/auto-scrolling.controller.ts
@@ -60,15 +60,16 @@ export default class AutoScrollingController extends BaseController {
// in case of a setAnchor click, we can go for a direct scroll approach
const scrollableContainer = this.scrollableContainer;
const activityElement = this.getActivityAnchorElement(anchorName, activityId);
+ const locationHash = `#${anchorName}-${activityId}`;
if (scrollableContainer && activityElement) {
+ this.brieflyHighlightAndResetUrl(activityElement, locationHash);
scrollableContainer.scrollTo({
top: activityElement.offsetTop - 90,
behavior: 'smooth',
});
}
-
- window.location.hash = `#${anchorName}-${activityId}`;
+ window.location.hash = locationHash;
}
performAutoScrollingOnStreamsUpdate(journalsContainerAtBottom = false) {
@@ -123,24 +124,25 @@ export default class AutoScrollingController extends BaseController {
const activityIdMatch = window.location.hash.match(anchorTypeRegex); // Ex. [ "#comment-80", "comment", "80" ]
if (activityIdMatch && activityIdMatch.length === 3) {
- this.scrollToActivity(activityIdMatch[1] as AnchorType, activityIdMatch[2]);
+ const activityElement = this.getActivityAnchorElement(activityIdMatch[1] as AnchorType, activityIdMatch[2]);
+ this.brieflyHighlightAndResetUrl(activityElement, window.location.hash);
+ this.scrollToActivity(activityElement);
} else if (this.indexOutlet.sortingAscending && (!this.isMobile() || this.isWithinNotificationCenter())) {
this.scrollToBottom();
}
}
- private scrollToActivity(activityAnchorName:AnchorType, activityId:string) {
+ private scrollToActivity(activityElement:HTMLElement|null) {
const maxAttempts = 20; // wait max 20 seconds for the activity to be rendered
- this.tryScroll(activityAnchorName, activityId, 0, maxAttempts);
+ this.tryScroll(activityElement, 0, maxAttempts);
}
private scrollToBottom() {
this.tryScrollToBottom(0, 20, 'auto');
}
- private tryScroll(activityAnchorName:AnchorType, activityId:string, attempts:number, maxAttempts:number) {
+ private tryScroll(activityElement:HTMLElement|null, attempts:number, maxAttempts:number) {
const scrollableContainer = this.scrollableContainer;
- const activityElement = this.getActivityAnchorElement(activityAnchorName, activityId);
const topPadding = 70;
if (activityElement && scrollableContainer) {
@@ -196,6 +198,19 @@ export default class AutoScrollingController extends BaseController {
});
}
+ private brieflyHighlightAndResetUrl(activityElement:HTMLElement|null, locationHash:string) {
+ if (activityElement) {
+ activityElement.classList.add('--anchor-highlighted');
+ setTimeout(() => {
+ document.addEventListener('click', () => {
+ activityElement.classList.remove('--anchor-highlighted');
+ const newLocation = window.location.href.replace(locationHash, '');
+ window.history.replaceState(null, 'Remove anchor', newLocation);
+ }, {once: true});
+ });
+ }
+ }
+
private getActivityAnchorElement(activityAnchorName:AnchorType, activityId:string):HTMLElement | null {
return document.querySelector(`[data-anchor-${activityAnchorName}-id="${activityId}"]`);
}
diff --git a/spec/features/activities/work_package/activities_spec.rb b/spec/features/activities/work_package/activities_spec.rb
index 63bc8f34b49..378e165e87b 100644
--- a/spec/features/activities/work_package/activities_spec.rb
+++ b/spec/features/activities/work_package/activities_spec.rb
@@ -274,6 +274,17 @@ RSpec.describe "Work package activity", :js, :with_cuprite do
activity_tab.expect_journal_notes(text: "First comment by admin")
end
+
+ it "highlights the comment specified in the URL until the user clicks anywhere" do
+ visit project_work_package_path(project, work_package.id, "activity", anchor: "activity-2")
+ wp_page.wait_for_activity_tab
+
+ highlighted_comment = page.find(".--anchor-highlighted")
+ expect(highlighted_comment).to have_content("First comment by admin")
+ # click anything (without triggering navigation or something else)
+ page.find(:xpath, "//*[text()='First comment by admin']").click
+ expect(page).to have_no_css(".--anchor-highlighted")
+ end
end
end
@@ -1042,12 +1053,20 @@ RSpec.describe "Work package activity", :js, :with_cuprite do
wp_page.wait_for_activity_tab
end
- it "scrolls to the comment specified in the URL" do
+ it "scrolls to the activity specified in the URL" do
wait_for_auto_scrolling_to_finish
activity_tab.expect_journal_container_at_position(50) # would be at the bottom if no anchor would be provided
activity_tab.expect_activity_anchor_link(text: format_time(comment_1.updated_at))
end
+
+ it "highlights the activity specified in the URL until the user clicks anywhere" do
+ highlighted_comment = page.find(".--anchor-highlighted")
+ expect(highlighted_comment).to have_content("created this on")
+ # click anything (without triggering navigation or something else)
+ page.find(:xpath, "//*[text()='created this on']").click
+ expect(page).to have_no_css(".--anchor-highlighted")
+ end
end
context "with #comment- anchor" do
@@ -1066,6 +1085,14 @@ RSpec.describe "Work package activity", :js, :with_cuprite do
activity_tab.expect_activity_anchor_link(text: format_time(comment_1.updated_at))
end
+
+ it "highlights the comment specified in the URL until the user clicks anywhere" do
+ highlighted_comment = page.find(".Box.--anchor-highlighted")
+ expect(highlighted_comment).to have_content("Comment 1")
+ # click anything (without triggering navigation or something else)
+ page.find(:xpath, "//*[text()='Comment 1']").click
+ expect(page).to have_no_css(".Box.--anchor-highlighted")
+ end
end
context "when on mobile screen size" do