Fix markup init after issue comment editing (#35536)

Fix #35533
This commit is contained in:
wxiaoguang
2025-09-26 00:29:32 +08:00
committed by GitHub
parent f09bea7af1
commit d83676c97a
3 changed files with 28 additions and 27 deletions

View File

@@ -9,12 +9,14 @@ import (
"html/template" "html/template"
"net/http" "net/http"
"strconv" "strconv"
"strings"
issues_model "code.gitea.io/gitea/models/issues" issues_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/models/renderhelper" "code.gitea.io/gitea/models/renderhelper"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/htmlutil"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup/markdown" "code.gitea.io/gitea/modules/markup/markdown"
repo_module "code.gitea.io/gitea/modules/repository" repo_module "code.gitea.io/gitea/modules/repository"
@@ -287,9 +289,10 @@ func UpdateCommentContent(ctx *context.Context) {
ctx.ServerError("RenderString", err) ctx.ServerError("RenderString", err)
return return
} }
} else { }
contentEmpty := fmt.Sprintf(`<span class="no-content">%s</span>`, ctx.Tr("repo.issues.no_content"))
renderedContent = template.HTML(contentEmpty) if strings.TrimSpace(string(renderedContent)) == "" {
renderedContent = htmlutil.HTMLFormat(`<span class="no-content">%s</span>`, ctx.Tr("repo.issues.no_content"))
} }
ctx.JSON(http.StatusOK, map[string]any{ ctx.JSON(http.StatusOK, map[string]any{

View File

@@ -168,10 +168,6 @@
{{template "repo/issue/view_content/reference_issue_dialog" .}} {{template "repo/issue/view_content/reference_issue_dialog" .}}
{{template "shared/user/block_user_dialog" .}} {{template "shared/user/block_user_dialog" .}}
<div class="tw-hidden" id="no-content">
<span class="no-content">{{ctx.Locale.Tr "repo.issues.no_content"}}</span>
</div>
<div class="ui g-modal-confirm delete modal"> <div class="ui g-modal-confirm delete modal">
<div class="header"> <div class="header">
{{svg "octicon-trash"}} {{svg "octicon-trash"}}

View File

@@ -13,10 +13,10 @@ async function tryOnEditContent(e: DOMEvent<MouseEvent>) {
if (!clickTarget) return; if (!clickTarget) return;
e.preventDefault(); e.preventDefault();
const segment = clickTarget.closest('.comment-header').nextElementSibling; const commentContent = clickTarget.closest('.comment-header').nextElementSibling;
const editContentZone = segment.querySelector('.edit-content-zone'); const editContentZone = commentContent.querySelector('.edit-content-zone');
const renderContent = segment.querySelector('.render-content'); let renderContent = commentContent.querySelector('.render-content');
const rawContent = segment.querySelector('.raw-content'); const rawContent = commentContent.querySelector('.raw-content');
let comboMarkdownEditor : ComboMarkdownEditor; let comboMarkdownEditor : ComboMarkdownEditor;
@@ -47,30 +47,32 @@ async function tryOnEditContent(e: DOMEvent<MouseEvent>) {
const response = await POST(editContentZone.getAttribute('data-update-url'), {data: params}); const response = await POST(editContentZone.getAttribute('data-update-url'), {data: params});
const data = await response.json(); const data = await response.json();
if (response.status === 400) { if (!response.ok) {
showErrorToast(data.errorMessage); showErrorToast(data?.errorMessage ?? window.config.i18n.error_occurred);
return; return;
} }
reinitializeAreYouSure(editContentZone.querySelector('form')); // the form is no longer dirty reinitializeAreYouSure(editContentZone.querySelector('form')); // the form is no longer dirty
editContentZone.setAttribute('data-content-version', data.contentVersion); editContentZone.setAttribute('data-content-version', data.contentVersion);
if (!data.content) {
renderContent.innerHTML = document.querySelector('#no-content').innerHTML; // replace the render content with new one, to trigger re-initialization of all features
rawContent.textContent = ''; const newRenderContent = renderContent.cloneNode(false) as HTMLElement;
} else { newRenderContent.innerHTML = data.content;
renderContent.innerHTML = data.content; renderContent.replaceWith(newRenderContent);
rawContent.textContent = comboMarkdownEditor.value(); renderContent = newRenderContent;
const refIssues = renderContent.querySelectorAll<HTMLElement>('p .ref-issue');
attachRefIssueContextPopup(refIssues); rawContent.textContent = comboMarkdownEditor.value();
} const refIssues = renderContent.querySelectorAll<HTMLElement>('p .ref-issue');
const content = segment; attachRefIssueContextPopup(refIssues);
if (!content.querySelector('.dropzone-attachments')) {
if (!commentContent.querySelector('.dropzone-attachments')) {
if (data.attachments !== '') { if (data.attachments !== '') {
content.insertAdjacentHTML('beforeend', data.attachments); commentContent.insertAdjacentHTML('beforeend', data.attachments);
} }
} else if (data.attachments === '') { } else if (data.attachments === '') {
content.querySelector('.dropzone-attachments').remove(); commentContent.querySelector('.dropzone-attachments').remove();
} else { } else {
content.querySelector('.dropzone-attachments').outerHTML = data.attachments; commentContent.querySelector('.dropzone-attachments').outerHTML = data.attachments;
} }
comboMarkdownEditor.dropzoneSubmitReload(); comboMarkdownEditor.dropzoneSubmitReload();
} catch (error) { } catch (error) {