Skip to content

Commit d83676c

Browse files
authored
Fix markup init after issue comment editing (#35536)
Fix #35533
1 parent f09bea7 commit d83676c

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

routers/web/repo/issue_comment.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import (
99
"html/template"
1010
"net/http"
1111
"strconv"
12+
"strings"
1213

1314
issues_model "code.gitea.io/gitea/models/issues"
1415
"code.gitea.io/gitea/models/renderhelper"
1516
user_model "code.gitea.io/gitea/models/user"
1617
"code.gitea.io/gitea/modules/git"
1718
"code.gitea.io/gitea/modules/gitrepo"
19+
"code.gitea.io/gitea/modules/htmlutil"
1820
"code.gitea.io/gitea/modules/log"
1921
"code.gitea.io/gitea/modules/markup/markdown"
2022
repo_module "code.gitea.io/gitea/modules/repository"
@@ -287,9 +289,10 @@ func UpdateCommentContent(ctx *context.Context) {
287289
ctx.ServerError("RenderString", err)
288290
return
289291
}
290-
} else {
291-
contentEmpty := fmt.Sprintf(`<span class="no-content">%s</span>`, ctx.Tr("repo.issues.no_content"))
292-
renderedContent = template.HTML(contentEmpty)
292+
}
293+
294+
if strings.TrimSpace(string(renderedContent)) == "" {
295+
renderedContent = htmlutil.HTMLFormat(`<span class="no-content">%s</span>`, ctx.Tr("repo.issues.no_content"))
293296
}
294297

295298
ctx.JSON(http.StatusOK, map[string]any{

templates/repo/issue/view_content.tmpl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,6 @@
168168
{{template "repo/issue/view_content/reference_issue_dialog" .}}
169169
{{template "shared/user/block_user_dialog" .}}
170170

171-
<div class="tw-hidden" id="no-content">
172-
<span class="no-content">{{ctx.Locale.Tr "repo.issues.no_content"}}</span>
173-
</div>
174-
175171
<div class="ui g-modal-confirm delete modal">
176172
<div class="header">
177173
{{svg "octicon-trash"}}

web_src/js/features/repo-issue-edit.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ async function tryOnEditContent(e: DOMEvent<MouseEvent>) {
1313
if (!clickTarget) return;
1414

1515
e.preventDefault();
16-
const segment = clickTarget.closest('.comment-header').nextElementSibling;
17-
const editContentZone = segment.querySelector('.edit-content-zone');
18-
const renderContent = segment.querySelector('.render-content');
19-
const rawContent = segment.querySelector('.raw-content');
16+
const commentContent = clickTarget.closest('.comment-header').nextElementSibling;
17+
const editContentZone = commentContent.querySelector('.edit-content-zone');
18+
let renderContent = commentContent.querySelector('.render-content');
19+
const rawContent = commentContent.querySelector('.raw-content');
2020

2121
let comboMarkdownEditor : ComboMarkdownEditor;
2222

@@ -47,30 +47,32 @@ async function tryOnEditContent(e: DOMEvent<MouseEvent>) {
4747

4848
const response = await POST(editContentZone.getAttribute('data-update-url'), {data: params});
4949
const data = await response.json();
50-
if (response.status === 400) {
51-
showErrorToast(data.errorMessage);
50+
if (!response.ok) {
51+
showErrorToast(data?.errorMessage ?? window.config.i18n.error_occurred);
5252
return;
5353
}
54+
5455
reinitializeAreYouSure(editContentZone.querySelector('form')); // the form is no longer dirty
5556
editContentZone.setAttribute('data-content-version', data.contentVersion);
56-
if (!data.content) {
57-
renderContent.innerHTML = document.querySelector('#no-content').innerHTML;
58-
rawContent.textContent = '';
59-
} else {
60-
renderContent.innerHTML = data.content;
61-
rawContent.textContent = comboMarkdownEditor.value();
62-
const refIssues = renderContent.querySelectorAll<HTMLElement>('p .ref-issue');
63-
attachRefIssueContextPopup(refIssues);
64-
}
65-
const content = segment;
66-
if (!content.querySelector('.dropzone-attachments')) {
57+
58+
// replace the render content with new one, to trigger re-initialization of all features
59+
const newRenderContent = renderContent.cloneNode(false) as HTMLElement;
60+
newRenderContent.innerHTML = data.content;
61+
renderContent.replaceWith(newRenderContent);
62+
renderContent = newRenderContent;
63+
64+
rawContent.textContent = comboMarkdownEditor.value();
65+
const refIssues = renderContent.querySelectorAll<HTMLElement>('p .ref-issue');
66+
attachRefIssueContextPopup(refIssues);
67+
68+
if (!commentContent.querySelector('.dropzone-attachments')) {
6769
if (data.attachments !== '') {
68-
content.insertAdjacentHTML('beforeend', data.attachments);
70+
commentContent.insertAdjacentHTML('beforeend', data.attachments);
6971
}
7072
} else if (data.attachments === '') {
71-
content.querySelector('.dropzone-attachments').remove();
73+
commentContent.querySelector('.dropzone-attachments').remove();
7274
} else {
73-
content.querySelector('.dropzone-attachments').outerHTML = data.attachments;
75+
commentContent.querySelector('.dropzone-attachments').outerHTML = data.attachments;
7476
}
7577
comboMarkdownEditor.dropzoneSubmitReload();
7678
} catch (error) {

0 commit comments

Comments
 (0)