Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Dec 2, 2024
1 parent e3e3260 commit 53571d6
Show file tree
Hide file tree
Showing 28 changed files with 132 additions and 107 deletions.
5 changes: 4 additions & 1 deletion modules/web/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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: <a href="%s">%s</a>`, setting.AppSubURL, setting.AppSubURL)))
return
}
normalized = true
Expand Down
1 change: 0 additions & 1 deletion options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2588,7 +2588,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
Expand Down
2 changes: 2 additions & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion templates/devtest/devtest-footer.tmpl
Original file line number Diff line number Diff line change
@@ -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 */}}
<script src="{{AssetUrlPrefix}}/js/devtest.js?v={{AssetVersion}}"></script>
{{template "base/footer" dict}}
{{template "base/footer" ctx.RootData}}
2 changes: 1 addition & 1 deletion templates/devtest/devtest-header.tmpl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{{template "base/head" dict}}
{{template "base/head" ctx.RootData}}
<link rel="stylesheet" href="{{AssetUrlPrefix}}/css/devtest.css?v={{AssetVersion}}">
3 changes: 1 addition & 2 deletions templates/devtest/gitea-ui.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@

<div>
<h1>ComboMarkdownEditor</h1>
<div>ps: no JS code attached, so just a layout</div>
{{template "shared/combomarkdowneditor" .}}
{{template "shared/combomarkdowneditor" dict "MarkdownPreviewContext" "/owner/path"}}
</div>

<h1>Tailwind CSS Demo</h1>
Expand Down
1 change: 1 addition & 0 deletions templates/org/settings/options.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<input id="email" name="email" type="email" value="{{.Org.Email}}" maxlength="255">
</div>
<div class="field {{if .Err_Description}}error{{end}}">
{{/* it is rendered as markdown, but the length is limited, so at the moment we do not use the markdown editor here */}}
<label for="description">{{ctx.Locale.Tr "org.org_desc"}}</label>
<textarea id="description" name="description" rows="2" maxlength="255">{{.Org.Description}}</textarea>
</div>
Expand Down
7 changes: 6 additions & 1 deletion templates/projects/new.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
</div>
<div class="field">
<label>{{ctx.Locale.Tr "repo.projects.description"}}</label>
<textarea name="content" placeholder="{{ctx.Locale.Tr "repo.projects.description_placeholder"}}">{{.content}}</textarea>
{{/* 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")
)}}
</div>

{{if not .PageIsEditProjects}}
Expand Down
4 changes: 2 additions & 2 deletions templates/repo/diff/box.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@
<template id="issue-comment-editor-template">
<div class="ui form comment">
{{template "shared/combomarkdowneditor" (dict
"MarkdownPreviewUrl" (print $.Repository.Link "/markup")
"MarkdownPreviewContext" $.RepoLink
"CustomInit" true
"MarkdownPreviewInRepo" $.Repository "MarkdownPreviewMode" "comment"
"TextareaName" "content"
"DropzoneParentContainer" ".ui.form"
)}}
Expand Down
8 changes: 3 additions & 5 deletions templates/repo/diff/comment_form.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
<input type="hidden" name="diff_base_cid">

{{template "shared/combomarkdowneditor" (dict
"MarkdownPreviewUrl" (print $.root.Repository.Link "/markup")
"MarkdownPreviewContext" $.root.RepoLink
"TextareaName" "content"
"TextareaPlaceholder" (ctx.Locale.Tr "repo.diff.comment.placeholder")
"CustomInit" true
"MarkdownPreviewInRepo" $.root.Repository "MarkdownPreviewMode" "comment"
"TextareaName" "content" "TextareaPlaceholder" (ctx.Locale.Tr "repo.diff.comment.placeholder")
"DropzoneParentContainer" "form"
"DisableAutosize" "true"
)}}
Expand All @@ -26,7 +25,6 @@
{{end}}

<div class="field footer tw-mx-2">
<span class="markup-info">{{svg "octicon-markdown"}} {{ctx.Locale.Tr "repo.diff.comment.markdown_info"}}</span>
<div class="tw-text-right">
{{if $.reply}}
<button class="ui submit primary tiny button btn-reply" type="submit">{{ctx.Locale.Tr "repo.diff.comment.reply"}}</button>
Expand Down
6 changes: 2 additions & 4 deletions templates/repo/diff/new_review.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
</div>
<div class="field">
{{template "shared/combomarkdowneditor" (dict
"MarkdownPreviewUrl" (print .Repository.Link "/markup")
"MarkdownPreviewContext" .RepoLink
"TextareaName" "content"
"TextareaPlaceholder" (ctx.Locale.Tr "repo.diff.review.placeholder")
"MarkdownPreviewInRepo" $.Repository "MarkdownPreviewMode" "comment"
"TextareaName" "content" "TextareaPlaceholder" (ctx.Locale.Tr "repo.diff.review.placeholder")
"DropzoneParentContainer" "form"
)}}
</div>
Expand Down
8 changes: 3 additions & 5 deletions templates/repo/issue/comment_tab.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

<div class="field">
{{template "shared/combomarkdowneditor" (dict
"MarkdownPreviewUrl" (print .Repository.Link "/markup")
"MarkdownPreviewContext" .RepoLink
"TextareaName" "content"
"TextareaContent" $textareaContent
"TextareaPlaceholder" (ctx.Locale.Tr "repo.diff.comment.placeholder")
"CustomInit" true
"MarkdownPreviewInRepo" $.Repository "MarkdownPreviewMode" "comment"
"TextareaName" "content" "TextareaContent" $textareaContent "TextareaPlaceholder" (ctx.Locale.Tr "repo.diff.comment.placeholder")
"DropzoneParentContainer" "form, .ui.form"
)}}
</div>
Expand Down
7 changes: 3 additions & 4 deletions templates/repo/issue/fields/textarea.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

{{if $useMarkdownEditor}}
{{template "shared/combomarkdowneditor" (dict
"CustomInit" true
"ContainerClasses" "tw-hidden"
"MarkdownPreviewUrl" (print .root.RepoLink "/markup")
"MarkdownPreviewContext" .root.RepoLink
"TextareaContent" .item.Attributes.value
"TextareaPlaceholder" .item.Attributes.placeholder
"MarkdownPreviewInRepo" $.root.Repository "MarkdownPreviewMode" "comment"
"TextareaContent" .item.Attributes.value "TextareaPlaceholder" .item.Attributes.placeholder
"DropzoneParentContainer" ".combo-editor-dropzone"
)}}

Expand Down
7 changes: 5 additions & 2 deletions templates/repo/issue/milestone_new.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@
</div>
<div class="field">
<label>{{ctx.Locale.Tr "repo.milestones.desc"}}</label>
<textarea name="content">{{.content}}</textarea>
{{template "shared/combomarkdowneditor" (dict
"MarkdownPreviewInRepo" $.Repository "MarkdownPreviewMode" "comment"
"TextareaName" "content" "TextareaContent" .content
"TextareaPlaceholder" (ctx.Locale.Tr "repo.milestones.desc")
)}}
</div>
<div class="divider"></div>
<div class="tw-text-right">
{{if .PageIsEditMilestone}}
<a class="ui primary basic button" href="{{.RepoLink}}/milestones">
Expand Down
4 changes: 2 additions & 2 deletions templates/repo/issue/view_content.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@
<div class="ui form comment">
<div class="field">
{{template "shared/combomarkdowneditor" (dict
"MarkdownPreviewUrl" (print .Repository.Link "/markup")
"MarkdownPreviewContext" .RepoLink
"CustomInit" true
"MarkdownPreviewInRepo" $.Repository "MarkdownPreviewMode" "comment"
"TextareaName" "content"
"DropzoneParentContainer" ".ui.form"
)}}
Expand Down
8 changes: 2 additions & 6 deletions templates/repo/release/new.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@
</div>
<div class="field">
{{template "shared/combomarkdowneditor" (dict
"MarkdownPreviewUrl" (print .Repository.Link "/markup")
"MarkdownPreviewContext" .RepoLink
"TextareaName" "content"
"TextareaContent" .content
"TextareaPlaceholder" (ctx.Locale.Tr "repo.release.message")
"TextareaAriaLabel" (ctx.Locale.Tr "repo.release.message")
"MarkdownPreviewInRepo" $.Repository "MarkdownPreviewMode" "comment"
"TextareaName" "content" "TextareaContent" .content "TextareaPlaceholder" (ctx.Locale.Tr "repo.release.message")
"DropzoneParentContainer" "form"
)}}
</div>
Expand Down
9 changes: 3 additions & 6 deletions templates/repo/wiki/new.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@
{{$content = ctx.Locale.Tr "repo.wiki.welcome"}}
{{end}}
{{template "shared/combomarkdowneditor" (dict
"MarkdownPreviewUrl" (print .Repository.Link "/markup")
"MarkdownPreviewContext" .RepoLink
"TextareaName" "content"
"TextareaPlaceholder" (ctx.Locale.Tr "repo.wiki.page_content")
"TextareaAriaLabel" (ctx.Locale.Tr "repo.wiki.page_content")
"TextareaContent" $content
"CustomInit" true
"MarkdownPreviewInRepo" $.Repository "MarkdownPreviewMode" "wiki"
"TextareaName" "content" "TextareaContent" $content "TextareaPlaceholder" (ctx.Locale.Tr "repo.wiki.page_content")
)}}

<div class="field tw-mt-4">
Expand Down
36 changes: 29 additions & 7 deletions templates/shared/combomarkdowneditor.tmpl
Original file line number Diff line number Diff line change
@@ -1,23 +1,37 @@
{{/*
Template Attributes:
* CustomInit: do not initialize the editor automatically
* ContainerId: id attribute for the container element
* ContainerClasses: additional classes for the container element
* MarkdownPreviewUrl: preview url for the preview tab
* MarkdownPreviewContext: preview context for the preview tab
* MarkdownPreviewInRepo: the repo to preview markdown
* MarkdownPreviewContext: preview context (the related url path when rendering) for the preview tab, eg: repo link or user home link
* MarkdownPreviewMode: content mode for the editor, eg: wiki, comment or default
* TextareaName: name attribute for the textarea
* TextareaContent: content for the textarea
* TextareaMaxLength: maxlength attribute for the textarea
* TextareaPlaceholder: placeholder attribute for the textarea
* TextareaAriaLabel: aria-label attribute for the textarea
* DropzoneParentContainer: container for file upload (leave it empty if no upload)
* DisableAutosize: whether to disable automatic height resizing
*/}}
<div {{if .ContainerId}}id="{{.ContainerId}}"{{end}} class="combo-markdown-editor {{.ContainerClasses}}" data-dropzone-parent-container="{{.DropzoneParentContainer}}">
{{if .MarkdownPreviewUrl}}
{{$ariaLabel := or .TextareaAriaLabel .TextareaPlaceholder}}
{{$repo := .MarkdownPreviewInRepo}}
{{$previewContext := .MarkdownPreviewContext}}
{{$previewMode := .MarkdownPreviewMode}}
{{$previewUrl := print AppSubUrl "/-/markup"}}
{{if $repo}}
{{$previewUrl = print $repo.Link "/markup"}}
{{end}}
{{$supportEasyMDE := or (eq $previewMode "comment") (eq $previewMode "wiki")}}
<div {{if .ContainerId}}id="{{.ContainerId}}"{{end}} class="combo-markdown-editor {{if .CustomInit}}custom-init{{end}} {{.ContainerClasses}}"
data-dropzone-parent-container="{{.DropzoneParentContainer}}"
data-content-mode="{{$previewMode}}" data-support-easy-mde="{{$supportEasyMDE}}"
data-preview-url="{{$previewUrl}}" data-preview-context="{{$previewContext}}"
>
<div class="ui top tabular menu">
<a class="active item" data-tab-for="markdown-writer">{{template "shared/misc/tabtitle" (ctx.Locale.Tr "write")}}</a>
<a class="item" data-tab-for="markdown-previewer" data-preview-url="{{.MarkdownPreviewUrl}}" data-preview-context="{{.MarkdownPreviewContext}}">{{template "shared/misc/tabtitle" (ctx.Locale.Tr "preview")}}</a>
<a class="item" data-tab-for="markdown-previewer">{{template "shared/misc/tabtitle" (ctx.Locale.Tr "preview")}}</a>
</div>
{{end}}
<div class="ui tab active" data-tab-panel="markdown-writer">
<markdown-toolbar>
<div class="markdown-toolbar-group">
Expand All @@ -40,17 +54,25 @@ Template Attributes:
<md-task-list class="markdown-toolbar-button" data-tooltip-content="{{ctx.Locale.Tr "editor.buttons.list.task.tooltip"}}">{{svg "octicon-tasklist"}}</md-task-list>
<button class="markdown-toolbar-button markdown-button-table-add" data-tooltip-content="{{ctx.Locale.Tr "editor.buttons.table.add.tooltip"}}">{{svg "octicon-table"}}</button>
</div>
{{if eq $previewMode "comment"}}
<div class="markdown-toolbar-group">
<md-mention class="markdown-toolbar-button" data-tooltip-content="{{ctx.Locale.Tr "editor.buttons.mention.tooltip"}}">{{svg "octicon-mention"}}</md-mention>
<md-ref class="markdown-toolbar-button" data-tooltip-content="{{ctx.Locale.Tr "editor.buttons.ref.tooltip"}}">{{svg "octicon-cross-reference"}}</md-ref>
</div>
{{end}}
<div class="markdown-toolbar-group">
<button class="markdown-toolbar-button markdown-switch-monospace" role="switch" data-enable-text="{{ctx.Locale.Tr "editor.buttons.enable_monospace_font"}}" data-disable-text="{{ctx.Locale.Tr "editor.buttons.disable_monospace_font"}}">{{svg "octicon-typography"}}</button>
{{if $supportEasyMDE}}
<button class="markdown-toolbar-button markdown-switch-easymde" data-tooltip-content="{{ctx.Locale.Tr "editor.buttons.switch_to_legacy.tooltip"}}">{{svg "octicon-arrow-switch"}}</button>
{{end}}
</div>
</markdown-toolbar>
<text-expander keys=": @ #" multiword="#" suffix="">
<textarea class="markdown-text-editor"{{if .TextareaName}} name="{{.TextareaName}}"{{end}}{{if .TextareaPlaceholder}} placeholder="{{.TextareaPlaceholder}}"{{end}}{{if .TextareaAriaLabel}} aria-label="{{.TextareaAriaLabel}}"{{end}}{{if .DisableAutosize}} data-disable-autosize="{{.DisableAutosize}}"{{end}}>{{.TextareaContent}}</textarea>
<textarea class="markdown-text-editor"
{{if .TextareaName}}name="{{.TextareaName}}"{{end}} {{if .TextareaMaxLength}}maxlength="{{.TextareaMaxLength}}"{{end}}
{{if .TextareaPlaceholder}}placeholder="{{.TextareaPlaceholder}}"{{end}} {{if $ariaLabel}}aria-label="{{$ariaLabel}}"{{end}}
{{if .DisableAutosize}}data-disable-autosize="{{.DisableAutosize}}"{{end}}
>{{.TextareaContent}}</textarea>
</text-expander>
<script>
if (localStorage?.getItem('markdown-editor-monospace') === 'true') {
Expand Down
1 change: 1 addition & 0 deletions templates/user/settings/profile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<p id="signed-user-email">{{.SignedUser.Email}}</p>
</div>
<div class="field {{if .Err_Description}}error{{end}}">
{{/* it is rendered as markdown, but the length is limited, so at the moment we do not use the markdown editor here */}}
<label for="description">{{ctx.Locale.Tr "user.user_bio"}}</label>
<textarea id="description" name="description" rows="2" placeholder="{{ctx.Locale.Tr "settings.biography_placeholder"}}" maxlength="255">{{.SignedUser.Description}}</textarea>
</div>
Expand Down
5 changes: 5 additions & 0 deletions web_src/css/editor/combomarkdowneditor.css
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
font-size: 0.85em;
}

.combo-markdown-editor .ui.tab.markup[data-tab-panel="markdown-previewer"] {
border-bottom: 1px solid var(--color-secondary);
padding-bottom: 1rem;
}

text-expander {
display: block;
position: relative;
Expand Down
7 changes: 0 additions & 7 deletions web_src/css/review.css
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,6 @@
padding: 10px 0;
}

.comment-code-cloud .footer .markup-info {
display: inline-block;
margin: 5px 0;
font-size: 12px;
color: var(--color-text-light);
}

.comment-code-cloud .footer .ui.right.floated {
padding-top: 6px;
}
Expand Down
11 changes: 9 additions & 2 deletions web_src/js/features/common-form.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {applyAreYouSure, initAreYouSure} from '../vendor/jquery.are-you-sure.ts';
import {handleGlobalEnterQuickSubmit} from './comp/QuickSubmit.ts';
import {queryElems} from '../utils/dom.ts';
import {initComboMarkdownEditor} from './comp/ComboMarkdownEditor.ts';

export function initGlobalFormDirtyLeaveConfirm() {
initAreYouSure(window.jQuery);
Expand All @@ -13,12 +15,13 @@ export function initGlobalFormDirtyLeaveConfirm() {
export function initGlobalEnterQuickSubmit() {
document.addEventListener('keydown', (e) => {
if (e.key !== 'Enter') return;
const target = e.target as HTMLElement;
const hasCtrlOrMeta = ((e.ctrlKey || e.metaKey) && !e.altKey);
if (hasCtrlOrMeta && e.target.matches('textarea')) {
if (hasCtrlOrMeta && target.matches('textarea')) {
if (handleGlobalEnterQuickSubmit(e.target)) {
e.preventDefault();
}
} else if (e.target.matches('input') && !e.target.closest('form')) {
} else if (target.matches('input') && !target.closest('form')) {
// input in a normal form could handle Enter key by default, so we only handle the input outside a form
// eslint-disable-next-line unicorn/no-lonely-if
if (handleGlobalEnterQuickSubmit(e.target)) {
Expand All @@ -27,3 +30,7 @@ export function initGlobalEnterQuickSubmit() {
}
});
}

export function initGlobalComboMarkdownEditor() {
queryElems<HTMLElement>(document, '.combo-markdown-editor:not(.custom-init)', (el) => initComboMarkdownEditor(el));
}
Loading

0 comments on commit 53571d6

Please sign in to comment.