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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
DRY up activity anchor url parsing
Kabiru Mwenja45b41784fb7e
diff --git a/frontend/src/app/features/work-packages/components/wp-single-view-tabs/activity-panel/activity-base.controller.ts b/frontend/src/app/features/work-packages/components/wp-single-view-tabs/activity-panel/activity-base.controller.tsindex 518a0d33762..daf27fc2561 100644--- a/frontend/src/app/features/work-packages/components/wp-single-view-tabs/activity-panel/activity-base.controller.ts+++ b/frontend/src/app/features/work-packages/components/wp-single-view-tabs/activity-panel/activity-base.controller.ts@@ -36,6 +36,7 @@ import { WpSingleViewService } from 'core-app/features/work-packages/routing/wp-import { BrowserDetector } from 'core-app/core/browser/browser-detector.service';import { DeviceService } from 'core-app/core/browser/device.service';import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';+import { UrlHelpers } from 'core-stimulus/controllers/dynamic/work-packages/activities-tab/services/url-helpers';@Directive()export class ActivityPanelBaseController extends UntilDestroyedMixin implements OnInit {@@ -62,17 +63,13 @@ export class ActivityPanelBaseController extends UntilDestroyedMixin implementsthis.turboFrameSrc = this.buildTurboFrameSrc();}- protected buildTurboFrameSrc(): string {+ protected buildTurboFrameSrc():string {const baseUrl = `${this.pathHelper.staticBase}/work_packages/${this.workPackageId}/activities`;- const hash = window.location.hash;+ const anchorInfo = UrlHelpers.extractActivityAnchor(window.location.hash);- if (hash) {- // Extract anchor (e.g., "#comment-123" or "#activity-456")- const anchorMatch = hash.match(/^#(comment|activity)-(\d+)$/i);- if (anchorMatch && anchorMatch.length === 3) {- const anchor = hash.slice(1); // Remove # prefix- return `${baseUrl}?anchor=${encodeURIComponent(anchor)}`;- }+ if (anchorInfo) {+ const anchor = `${anchorInfo.type}-${anchorInfo.id}`;+ return `${baseUrl}?anchor=${encodeURIComponent(anchor)}`;}return baseUrl;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.tsindex f78d76fec1c..e1d6e4f8d12 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@@ -29,16 +29,12 @@*/import BaseController from './base.controller';--enum AnchorType {- Comment = 'comment',- Activity = 'activity',-}+import { UrlHelpers, ActivityAnchorType, ActivityAnchor } from './services/url-helpers';interface CustomEventWithIdParam extends Event {params:{id:string;- anchorName:AnchorType;+ anchorName:ActivityAnchorType;};}@@ -59,7 +55,7 @@ export default class AutoScrollingController extends BaseController {// not using the scrollToActivity method here as it is causing flickering issues// in case of a setAnchor click, we can go for a direct scroll approachconst scrollableContainer = this.scrollableContainer;- const activityElement = this.getActivityAnchorElement(anchorName, activityId);+ const activityElement = this.getActivityAnchorElement({ type: anchorName, id: activityId });const locationHash = `#${anchorName}-${activityId}`;if (scrollableContainer && activityElement) {@@ -120,12 +116,12 @@ export default class AutoScrollingController extends BaseController {}private handleInitialScroll() {- const anchorTypeRegex = new RegExp(`#(${AnchorType.Comment}|${AnchorType.Activity})-(\\d+)`, 'i');- const activityIdMatch = window.location.hash.match(anchorTypeRegex); // Ex. [ "#comment-80", "comment", "80" ]+ const hash = window.location.hash;+ const anchorInfo = UrlHelpers.extractActivityAnchor(hash);- if (activityIdMatch && activityIdMatch.length === 3) {- const activityElement = this.getActivityAnchorElement(activityIdMatch[1] as AnchorType, activityIdMatch[2]);- this.brieflyHighlightAndResetUrl(activityElement, window.location.hash);+ if (anchorInfo) {+ const activityElement = this.getActivityAnchorElement(anchorInfo);+ this.brieflyHighlightAndResetUrl(activityElement, hash);this.scrollToActivity(activityElement);} else if (this.indexOutlet.sortingAscending && (!this.isMobile() || this.isWithinNotificationCenter())) {this.scrollToBottom();@@ -179,7 +175,6 @@ export default class AutoScrollingController extends BaseController {let timeoutId:ReturnType<typeof setTimeout>;const observer = new MutationObserver(() => {- // eslint-disable-next-line @typescript-eslint/no-unsafe-argumentclearTimeout(timeoutId);timeoutId = setTimeout(() => {@@ -211,8 +206,8 @@ export default class AutoScrollingController extends BaseController {}}- private getActivityAnchorElement(activityAnchorName:AnchorType, activityId:string):HTMLElement | null {- return document.querySelector(`[data-anchor-${activityAnchorName}-id="${activityId}"]`);+ private getActivityAnchorElement(activityAnchor:ActivityAnchor):HTMLElement | null {+ return document.querySelector(`[data-anchor-${activityAnchor.type}-id="${activityAnchor.id}"]`);}private get inputContainer():HTMLElement | null {diff --git a/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/services/url-helpers.ts b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/services/url-helpers.tsnew file mode 100644index 00000000000..ab9761be18c--- /dev/null+++ b/frontend/src/stimulus/controllers/dynamic/work-packages/activities-tab/services/url-helpers.ts@@ -0,0 +1,69 @@+/*+ * -- copyright+ * OpenProject is an open source project management software.+ * Copyright (C) 2023 the OpenProject GmbH+ *+ * This program is free software; you can redistribute it and/or+ * modify it under the terms of the GNU General Public License version 3.+ *+ * OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+ * Copyright (C) 2006-2013 Jean-Philippe Lang+ * Copyright (C) 2010-2013 the ChiliProject Team+ *+ * This program is free software; you can redistribute it and/or+ * modify it under the terms of the GNU General Public License+ * as published by the Free Software Foundation; either version 2+ * of the License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU General Public License for more details.+ *+ * You should have received a copy of the GNU General Public License+ * along with this program; if not, write to the Free Software+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+ *+ * See COPYRIGHT and LICENSE files for more details.+ * +++ */++/* eslint-disable no-unused-vars */+export enum ActivityAnchorType {+ Comment = 'comment',+ Activity = 'activity',+}+/* eslint-enable no-unused-vars */++export interface ActivityAnchor {+ type:ActivityAnchorType;+ id:string;+}++const anchorTypeRegex = new RegExp(`#(${ActivityAnchorType.Comment}|${ActivityAnchorType.Activity})-(\\d+)`, 'i');++export namespace UrlHelpers {+ /**+ * Extracts activity anchor information from a URL anchor string.+ *+ * @param anchor - The anchor string to parse (e.g., "#comment-80", "#activity-45")+ * @returns An ActivityAnchor object containing the type and id, or null if parsing fails+ *+ * @example+ * ```typescript+ * const result = extractActivityAnchor("#comment-80");+ * // Returns: { type: "comment", id: "80" }+ * ```+ */+ export function extractActivityAnchor(anchor:string):ActivityAnchor | null {+ const activityIdMatch = anchor.match(anchorTypeRegex); // Ex. [ "#comment-80", "comment", "80" ]++ if (activityIdMatch?.[1] && activityIdMatch?.[2]) {+ const type = activityIdMatch[1];+ if (Object.values(ActivityAnchorType).includes(type as ActivityAnchorType)) {+ return { type: type as ActivityAnchorType, id: activityIdMatch[2] };+ }+ }+ return null;+ }+}