enhance(repo): only show blame and diff unescape buttons when needed (#40563)

The blame page and the diff file menu always offered "Unescape", even
for files without escaped characters. Blame now renders the buttons only
when `EscapeStatus.Escaped` is true, like the file view. The diff menu
decides when it opens, because escape status is only known once its
lines are rendered, which also covers expanded and later loaded lines.

| Before | After |
| --- | --- |
| <img width="441" height="48" alt="Blame before"
src="https://github.com/user-attachments/assets/f36af26d-020d-4403-ad85-da7ff9125015">
| <img width="349" height="48" alt="Blame after"
src="https://github.com/user-attachments/assets/c5ee4693-e947-421c-93f1-fd9a74440dc0">
|
| <img width="146" height="175" alt="Diff menu before"
src="https://github.com/user-attachments/assets/126d507c-5d17-45b8-bb3a-81c2256a27ed">
| <img width="146" height="137" alt="Diff menu after"
src="https://github.com/user-attachments/assets/4a6b56b8-6bec-4b9d-8c3a-dc8092c3639d">
|

Fixes: https://github.com/go-gitea/gitea/issues/22613

Assisted-by: OpenCode:hy4-flash-free
Assisted-by: Claude Code:claude-opus-5-5

---------

Co-authored-by: silverwind <me@silverwind.io>
This commit is contained in:
Vu Nguyen Anh Tuanandsilverwind authored and GitHub committed 2026-11-27 19:45:08 +00:00
1 parent fe428123db
commit 609c808150
3 files changed
+16 -7

No files matched your search

+4 -2
View File
@@ -23,8 +23,10 @@
{{end}}
<a class="ui tiny button" href="{{.RepoLink}}/src/{{.RefTypeNameSubURL}}/{{.TreePath | PathEscapeSegments}}">{{ctx.Locale.Tr "repo.normal_view"}}</a>
<a class="ui tiny button" href="{{.RepoLink}}/commits/{{.RefTypeNameSubURL}}/{{.TreePath | PathEscapeSegments}}">{{ctx.Locale.Tr "repo.file_history"}}</a>
<button type="button" class="ui tiny button unescape-button">{{ctx.Locale.Tr "repo.unescape_control_characters"}}</button>
<button type="button" class="ui tiny button escape-button tw-hidden">{{ctx.Locale.Tr "repo.escape_control_characters"}}</button>
{{if .EscapeStatus.Escaped}}
<button type="button" class="ui tiny button unescape-button">{{ctx.Locale.Tr "repo.unescape_control_characters"}}</button>
<button type="button" class="ui tiny button escape-button tw-hidden">{{ctx.Locale.Tr "repo.escape_control_characters"}}</button>
{{end}}
</div>
</div>
</h4>
+2 -4
View File
@@ -148,10 +148,8 @@
{{if not $file.IsSubmodule}}
<button type="button" class="btn tw-p-1" data-global-init="initDiffHeaderPopupMenu">{{svg "octicon-kebab-horizontal" 18}}</button>
<div class="tippy-target">
{{if not (or $file.IsIncomplete $file.IsBin)}}
<button type="button" class="unescape-button item" data-unicode-content-selector="#diff-{{$file.NameHash}}">{{ctx.Locale.Tr "repo.unescape_control_characters"}}</button>
<button type="button" class="escape-button tw-hidden item" data-unicode-content-selector="#diff-{{$file.NameHash}}">{{ctx.Locale.Tr "repo.escape_control_characters"}}</button>
{{end}}
<button type="button" class="unescape-button tw-hidden item" data-unicode-content-selector="#diff-{{$file.NameHash}}">{{ctx.Locale.Tr "repo.unescape_control_characters"}}</button>
<button type="button" class="escape-button tw-hidden item" data-unicode-content-selector="#diff-{{$file.NameHash}}">{{ctx.Locale.Tr "repo.escape_control_characters"}}</button>
<button type="button" class="item" data-clipboard-text="{{$file.GetDiffFileBaseName}}">{{ctx.Locale.Tr "copy_filename"}}</button>
{{if not $.PageIsWiki}}
{{if $file.IsDeleted}}
+10 -1
View File
@@ -4,7 +4,9 @@ import {initRepoIssueContentHistory} from './repo-issue-content.ts';
import {validateTextareaNonEmpty} from './comp/ComboMarkdownEditor.ts';
import {initExpandAndCollapseFilesButton, initDiffFileViewedForm} from './pull-view-file.ts';
import {showErrorToast} from '../modules/toast.ts';
import {queryElemSiblings, hideElem, showElem, animateOnce, addDelegatedEventListener, createElementFromHTML, queryElems} from '../utils/dom.ts';
import {
queryElemSiblings, hideElem, showElem, toggleElem, animateOnce, addDelegatedEventListener, createElementFromHTML, queryElems,
} from '../utils/dom.ts';
import {errorMessage} from '../modules/errors.ts';
import {POST, GET} from '../modules/fetch.ts';
import {createTippy} from '../modules/tippy.ts';
@@ -142,6 +144,7 @@ function initRepoDiffConversationNav() {
function initDiffHeaderPopupMenu(btn: HTMLElement) {
const popup = btn.nextElementSibling;
if (!popup?.matches('.tippy-target')) throw new Error('Popup element not found');
const fileCode = btn.closest('.diff-file-box')!.querySelector('.file-code')!;
createTippy(btn, {
content: popup,
theme: 'menu',
@@ -149,6 +152,12 @@ function initDiffHeaderPopupMenu(btn: HTMLElement) {
trigger: 'click',
interactive: true,
hideOnClick: true,
onShow: () => {
const hasEscaped = Boolean(fileCode.querySelector('.has-escaped'));
const isEscaped = fileCode.classList.contains('unicode-escaped');
toggleElem(popup.querySelector('.unescape-button')!, hasEscaped && isEscaped);
toggleElem(popup.querySelector('.escape-button')!, hasEscaped && !isEscaped);
},
});
}