From c9e582c6b6cbc7beae66d24b05be6e1d338aa81b Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Wed, 4 Dec 2024 10:11:34 +0800 Subject: [PATCH] Refactor markdown editor and use it for milestone description editor (#32688) Refactor markdown editor to clarify its "preview" behavior and remove jQuery code. Close #15045 --------- Co-authored-by: silverwind --- modules/web/route.go | 5 +- options/locale/locale_en-US.ini | 1 - routers/web/web.go | 2 + templates/devtest/devtest-footer.tmpl | 2 +- templates/devtest/devtest-header.tmpl | 2 +- templates/devtest/gitea-ui.tmpl | 3 +- templates/org/settings/options.tmpl | 1 + templates/projects/new.tmpl | 11 +++- templates/repo/diff/box.tmpl | 5 +- templates/repo/diff/comment_form.tmpl | 12 ++-- templates/repo/diff/new_review.tmpl | 4 +- templates/repo/issue/comment_tab.tmpl | 7 ++- templates/repo/issue/fields/textarea.tmpl | 7 ++- templates/repo/issue/milestone_new.tmpl | 9 ++- templates/repo/issue/view_content.tmpl | 5 +- templates/repo/release/new.tmpl | 5 +- templates/repo/wiki/new.tmpl | 8 +-- templates/shared/combomarkdowneditor.tmpl | 38 +++++++++--- templates/user/settings/profile.tmpl | 1 + web_src/css/editor/combomarkdowneditor.css | 5 ++ web_src/css/repo.css | 2 +- web_src/css/review.css | 25 -------- web_src/js/features/common-form.ts | 8 ++- .../js/features/comp/ComboMarkdownEditor.ts | 58 +++++++++++-------- web_src/js/features/repo-editor.ts | 8 +-- web_src/js/features/repo-issue.ts | 5 -- web_src/js/features/repo-release.ts | 10 ---- web_src/js/features/repo-wiki.ts | 7 +-- web_src/js/index.ts | 7 ++- 29 files changed, 147 insertions(+), 116 deletions(-) diff --git a/modules/web/route.go b/modules/web/route.go index 77c411a97b..787521dfb0 100644 --- a/modules/web/route.go +++ b/modules/web/route.go @@ -9,6 +9,7 @@ import ( "reflect" "strings" + "code.gitea.io/gitea/modules/htmlutil" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/web/middleware" @@ -214,7 +215,9 @@ func (r *Router) normalizeRequestPath(resp http.ResponseWriter, req *http.Reques normalizedPath = "/" } else if !strings.HasPrefix(normalizedPath+"/", "/v2/") { // do not respond to other requests, to simulate a real sub-path environment - http.Error(resp, "404 page not found, sub-path is: "+setting.AppSubURL, http.StatusNotFound) + resp.Header().Add("Content-Type", "text/html; charset=utf-8") + resp.WriteHeader(http.StatusNotFound) + _, _ = resp.Write([]byte(htmlutil.HTMLFormat(`404 page not found, sub-path is: %s`, setting.AppSubURL, setting.AppSubURL))) return } normalized = true diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 8da7412ade..57d2d89c5a 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2590,7 +2590,6 @@ diff.generated = generated diff.vendored = vendored diff.comment.add_line_comment = Add line comment diff.comment.placeholder = Leave a comment -diff.comment.markdown_info = Styling with markdown is supported. diff.comment.add_single_comment = Add single comment diff.comment.add_review_comment = Add comment diff.comment.start_review = Start review diff --git a/routers/web/web.go b/routers/web/web.go index 5ed046a983..e89d069449 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -485,6 +485,8 @@ func registerRoutes(m *web.Router) { m.Methods("GET, HEAD", "/*", public.FileHandlerFunc()) }, optionsCorsHandler()) + m.Post("/-/markup", reqSignIn, web.Bind(structs.MarkupOption{}), misc.Markup) + m.Group("/explore", func() { m.Get("", func(ctx *context.Context) { ctx.Redirect(setting.AppSubURL + "/explore/repos") diff --git a/templates/devtest/devtest-footer.tmpl b/templates/devtest/devtest-footer.tmpl index 1c755508a5..a1b3b86e5c 100644 --- a/templates/devtest/devtest-footer.tmpl +++ b/templates/devtest/devtest-footer.tmpl @@ -1,3 +1,3 @@ {{/* TODO: the devtest.js is isolated from index.js, so no module is shared and many index.js functions do not work in devtest.ts */}} -{{template "base/footer" dict}} +{{template "base/footer" ctx.RootData}} diff --git a/templates/devtest/devtest-header.tmpl b/templates/devtest/devtest-header.tmpl index a5910b96e6..ee08545640 100644 --- a/templates/devtest/devtest-header.tmpl +++ b/templates/devtest/devtest-header.tmpl @@ -1,2 +1,2 @@ -{{template "base/head" dict}} +{{template "base/head" ctx.RootData}} diff --git a/templates/devtest/gitea-ui.tmpl b/templates/devtest/gitea-ui.tmpl index 303421fe13..5b40268761 100644 --- a/templates/devtest/gitea-ui.tmpl +++ b/templates/devtest/gitea-ui.tmpl @@ -183,8 +183,7 @@

ComboMarkdownEditor

-
ps: no JS code attached, so just a layout
- {{template "shared/combomarkdowneditor" .}} + {{template "shared/combomarkdowneditor" dict "MarkdownPreviewContext" "/owner/path"}}

Tailwind CSS Demo

diff --git a/templates/org/settings/options.tmpl b/templates/org/settings/options.tmpl index 62debfc0ae..3b817d068b 100644 --- a/templates/org/settings/options.tmpl +++ b/templates/org/settings/options.tmpl @@ -23,6 +23,7 @@
+ {{/* it is rendered as markdown, but the length is limited, so at the moment we do not use the markdown editor here */}}
diff --git a/templates/projects/new.tmpl b/templates/projects/new.tmpl index bd173b54bc..a936079c46 100644 --- a/templates/projects/new.tmpl +++ b/templates/projects/new.tmpl @@ -18,7 +18,16 @@
- + {{/* TODO: repo-level project and org-level project have different behaviros to render */}} + {{/* the "Repository" is nil when the project is org-level */}} + {{template "shared/combomarkdowneditor" (dict + "MarkdownPreviewInRepo" $.Repository + "MarkdownPreviewContext" (Iif $.Repository "" .HomeLink) + "MarkdownPreviewMode" (Iif $.Repository "comment") + "TextareaName" "content" + "TextareaContent" .content + "TextareaPlaceholder" (ctx.Locale.Tr "repo.projects.description_placeholder") + )}}
{{if not .PageIsEditProjects}} diff --git a/templates/repo/diff/box.tmpl b/templates/repo/diff/box.tmpl index 20e0c9db66..0f1458bfbf 100644 --- a/templates/repo/diff/box.tmpl +++ b/templates/repo/diff/box.tmpl @@ -240,8 +240,9 @@