diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2024-11-08 10:21:13 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-08 02:21:13 +0000 |
commit | 623a2d41cc866d31e6348c58cccf336b67a4b2c7 (patch) | |
tree | 0f0e064098dd40d63be2c20935f72de8d0e46c0e | |
parent | 028e6120943107fdfb25bf8ada9e3fc2e14e37e0 (diff) | |
download | gitea-623a2d41cc866d31e6348c58cccf336b67a4b2c7.tar.gz gitea-623a2d41cc866d31e6348c58cccf336b67a4b2c7.zip |
Refactor issue page info (#32445)
Fix a longstanding TODO since 2021 (#14826) / 2018 (#2531)
-rw-r--r-- | routers/web/repo/issue.go | 6 | ||||
-rw-r--r-- | templates/repo/issue/view_content.tmpl | 7 | ||||
-rw-r--r-- | templates/repo/issue/view_title.tmpl | 6 | ||||
-rw-r--r-- | templates/repo/pulls/files.tmpl | 4 | ||||
-rw-r--r-- | web_src/js/features/repo-issue-content.ts | 9 | ||||
-rw-r--r-- | web_src/js/features/repo-issue.ts | 10 | ||||
-rw-r--r-- | web_src/js/types.ts | 7 | ||||
-rw-r--r-- | web_src/js/utils.ts | 14 |
8 files changed, 36 insertions, 27 deletions
diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 1ee6e98afb..c4fc535446 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -1443,11 +1443,11 @@ func ViewIssue(ctx *context.Context) { } if issue.IsPull && !ctx.Repo.CanRead(unit.TypeIssues) { - ctx.Data["IssueType"] = "pulls" + ctx.Data["IssueDependencySearchType"] = "pulls" } else if !issue.IsPull && !ctx.Repo.CanRead(unit.TypePullRequests) { - ctx.Data["IssueType"] = "issues" + ctx.Data["IssueDependencySearchType"] = "issues" } else { - ctx.Data["IssueType"] = "all" + ctx.Data["IssueDependencySearchType"] = "all" } ctx.Data["IsProjectsEnabled"] = ctx.Repo.CanRead(unit.TypeProjects) diff --git a/templates/repo/issue/view_content.tmpl b/templates/repo/issue/view_content.tmpl index 1f9bbd86aa..3f7b2dc78f 100644 --- a/templates/repo/issue/view_content.tmpl +++ b/templates/repo/issue/view_content.tmpl @@ -1,11 +1,4 @@ <div class="issue-content"> - <!-- I know, there is probably a better way to do this (moved from sidebar.tmpl, original author: 6543 @ 2021-02-28) --> - <!-- Agree, there should be a better way, eg: introduce window.config.pageData (original author: wxiaoguang @ 2021-09-05) --> - <input type="hidden" id="repolink" value="{{$.RepoRelPath}}"> - <input type="hidden" id="repoId" value="{{.Repository.ID}}"> - <input type="hidden" id="issueIndex" value="{{.Issue.Index}}"> - <input type="hidden" id="type" value="{{.IssueType}}"> - {{$createdStr:= DateUtils.TimeSince .Issue.CreatedUnix}} <div class="issue-content-left comment-list prevent-before-timeline"> <div class="ui timeline"> diff --git a/templates/repo/issue/view_title.tmpl b/templates/repo/issue/view_title.tmpl index 0f796ce9bf..26b36d6ffc 100644 --- a/templates/repo/issue/view_title.tmpl +++ b/templates/repo/issue/view_title.tmpl @@ -3,6 +3,12 @@ {{template "base/alert" .}} </div> {{end}} +<div class="tw-hidden" id="issue-page-info" + data-issue-index="{{$.Issue.Index}}" + data-issue-dependency-search-type="{{$.IssueDependencySearchType}}" + data-issue-repo-link="{{$.RepoLink}}" + data-issue-repo-id="{{$.Repository.ID}}" +></div> <div class="issue-title-header"> {{$canEditIssueTitle := and (or .HasIssuesOrPullsWritePermission .IsIssuePoster) (not .Repository.IsArchived)}} <div class="issue-title" id="issue-title-display"> diff --git a/templates/repo/pulls/files.tmpl b/templates/repo/pulls/files.tmpl index 402bec800b..e7baa484a1 100644 --- a/templates/repo/pulls/files.tmpl +++ b/templates/repo/pulls/files.tmpl @@ -1,8 +1,4 @@ {{template "base/head" .}} - -<input type="hidden" id="repolink" value="{{$.RepoRelPath}}"> -<input type="hidden" id="issueIndex" value="{{.Issue.Index}}"> - <div role="main" aria-label="{{.Title}}" class="page-content repository view issue pull files diff"> {{template "repo/header" .}} <div class="ui container fluid padded"> diff --git a/web_src/js/features/repo-issue-content.ts b/web_src/js/features/repo-issue-content.ts index 7aaf9765ee..88672cc255 100644 --- a/web_src/js/features/repo-issue-content.ts +++ b/web_src/js/features/repo-issue-content.ts @@ -3,8 +3,8 @@ import {svg} from '../svg.ts'; import {showErrorToast} from '../modules/toast.ts'; import {GET, POST} from '../modules/fetch.ts'; import {showElem} from '../utils/dom.ts'; +import {parseIssuePageInfo} from '../utils.ts'; -const {appSubUrl} = window.config; let i18nTextEdited; let i18nTextOptions; let i18nTextDeleteFromHistory; @@ -121,15 +121,14 @@ function showContentHistoryMenu(issueBaseUrl, $item, commentId) { } export async function initRepoIssueContentHistory() { - const issueIndex = $('#issueIndex').val(); - if (!issueIndex) return; + const issuePageInfo = parseIssuePageInfo(); + if (!issuePageInfo.issueNumber) return; const $itemIssue = $('.repository.issue .timeline-item.comment.first'); // issue(PR) main content const $comments = $('.repository.issue .comment-list .comment'); // includes: issue(PR) comments, review comments, code comments if (!$itemIssue.length && !$comments.length) return; - const repoLink = $('#repolink').val(); - const issueBaseUrl = `${appSubUrl}/${repoLink}/issues/${issueIndex}`; + const issueBaseUrl = `${issuePageInfo.repoLink}/issues/${issuePageInfo.issueNumber}`; try { const response = await GET(`${issueBaseUrl}/content-history/overview`); diff --git a/web_src/js/features/repo-issue.ts b/web_src/js/features/repo-issue.ts index 392af776f8..721a746aa2 100644 --- a/web_src/js/features/repo-issue.ts +++ b/web_src/js/features/repo-issue.ts @@ -4,7 +4,7 @@ import {createTippy, showTemporaryTooltip} from '../modules/tippy.ts'; import {hideElem, showElem, toggleElem} from '../utils/dom.ts'; import {setFileFolding} from './file-fold.ts'; import {ComboMarkdownEditor, getComboMarkdownEditor, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.ts'; -import {toAbsoluteUrl} from '../utils.ts'; +import {parseIssuePageInfo, toAbsoluteUrl} from '../utils.ts'; import {GET, POST} from '../modules/fetch.ts'; import {showErrorToast} from '../modules/toast.ts'; import {initRepoIssueSidebar} from './repo-issue-sidebar.ts'; @@ -57,13 +57,11 @@ function excludeLabel(item) { } export function initRepoIssueSidebarList() { - const repolink = $('#repolink').val(); - const repoId = $('#repoId').val(); + const issuePageInfo = parseIssuePageInfo(); const crossRepoSearch = $('#crossRepoSearch').val(); - const tp = $('#type').val(); - let issueSearchUrl = `${appSubUrl}/${repolink}/issues/search?q={query}&type=${tp}`; + let issueSearchUrl = `${issuePageInfo.repoLink}/issues/search?q={query}&type=${issuePageInfo.issueDependencySearchType}`; if (crossRepoSearch === 'true') { - issueSearchUrl = `${appSubUrl}/issues/search?q={query}&priority_repo_id=${repoId}&type=${tp}`; + issueSearchUrl = `${appSubUrl}/issues/search?q={query}&priority_repo_id=${issuePageInfo.repoId}&type=${issuePageInfo.issueDependencySearchType}`; } $('#new-dependency-drop-list') .dropdown({ diff --git a/web_src/js/types.ts b/web_src/js/types.ts index 9c601456bd..f5c4a40bca 100644 --- a/web_src/js/types.ts +++ b/web_src/js/types.ts @@ -37,6 +37,13 @@ export type IssuePathInfo = { indexString?: string, } +export type IssuePageInfo = { + repoLink: string, + repoId: number, + issueNumber: number, + issueDependencySearchType: string, +} + export type Issue = { id: number; number: number; diff --git a/web_src/js/utils.ts b/web_src/js/utils.ts index 066a7c7b54..4fed74e20f 100644 --- a/web_src/js/utils.ts +++ b/web_src/js/utils.ts @@ -1,5 +1,5 @@ -import {encode, decode} from 'uint8-to-base64'; -import type {IssuePathInfo} from './types.ts'; +import {decode, encode} from 'uint8-to-base64'; +import type {IssuePageInfo, IssuePathInfo} from './types.ts'; // transform /path/to/file.ext to file.ext export function basename(path: string): string { @@ -43,6 +43,16 @@ export function parseIssueNewHref(href: string): IssuePathInfo { return {ownerName, repoName, pathType, indexString}; } +export function parseIssuePageInfo(): IssuePageInfo { + const el = document.querySelector('#issue-page-info'); + return { + issueNumber: parseInt(el?.getAttribute('data-issue-index')), + issueDependencySearchType: el?.getAttribute('data-issue-dependency-search-type') || '', + repoId: parseInt(el?.getAttribute('data-issue-repo-id')), + repoLink: el?.getAttribute('data-issue-repo-link') || '', + }; +} + // parse a URL, either relative '/path' or absolute 'https://localhost/path' export function parseUrl(str: string): URL { return new URL(str, str.startsWith('http') ? undefined : window.location.origin); |