Preview for listing comments

Signed-off-by: Jonas Franz <info@jonasfranz.software>
This commit is contained in:
Jonas Franz 2018-05-11 10:59:20 +02:00
parent 17af2d17be
commit 75b7d9b86a
No known key found for this signature in database
GPG Key ID: 506AEEBE80BEDECD
5 changed files with 168 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import (
"fmt"
"strings"
"code.gitea.io/gitea/modules/markup/markdown"
"github.com/Unknwon/com"
"github.com/go-xorm/builder"
"github.com/go-xorm/xorm"
@ -788,3 +789,39 @@ func DeleteComment(comment *Comment) error {
}
return nil
}
func fetchCodeComments(e Engine, issue *Issue, currentUser *User) (map[string]map[int64][]*Comment, error) {
pathToLineToComment := make(map[string]map[int64][]*Comment)
comments, err := findComments(e, FindCommentsOptions{
Type: CommentTypeCode,
IssueID: issue.ID,
})
if err != nil {
return nil, err
}
if err = issue.loadRepo(e); err != nil {
return nil, err
}
for _, comment := range comments {
if err = comment.loadReview(e); err != nil && !IsErrReviewNotExist(err) {
return nil, err
}
if comment.Review != nil && comment.Review.Type == ReviewTypePending {
if currentUser == nil || currentUser.ID != comment.Review.ReviewerID {
continue
}
}
comment.RenderedContent = string(markdown.Render([]byte(comment.Content), issue.Repo.Link(),
issue.Repo.ComposeMetas()))
if pathToLineToComment[comment.TreePath] == nil {
pathToLineToComment[comment.TreePath] = make(map[int64][]*Comment)
}
pathToLineToComment[comment.TreePath][comment.Line] = append(pathToLineToComment[comment.TreePath][comment.Line], comment)
}
return pathToLineToComment, nil
}
// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line
func FetchCodeComments(issue *Issue, currentUser *User) (map[string]map[int64][]*Comment, error) {
return fetchCodeComments(x, issue, currentUser)
}

View File

@ -15,6 +15,7 @@ import (
"mime"
"net/url"
"path/filepath"
"reflect"
"runtime"
"strings"
"time"
@ -186,6 +187,36 @@ func NewFuncMap() []template.FuncMap {
"ParseDeadline": func(deadline string) []string {
return strings.Split(deadline, "|")
},
"mul": func(first int, second int64) int64 { return second * int64(first) },
"dict": func(values ...interface{}) (map[string]interface{}, error) {
if len(values) == 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{})
for i := 0; i < len(values); i++ {
key, isset := values[i].(string)
if !isset {
if reflect.TypeOf(values[i]).Kind() == reflect.Map {
m := values[i].(map[string]interface{})
for i, v := range m {
dict[i] = v
}
} else {
return nil, errors.New("dict values must be maps")
}
} else {
i++
if i == len(values) {
return nil, errors.New("specify the key for non array values")
}
dict[key] = values[i]
}
}
return dict, nil
},
}}
}

View File

@ -471,6 +471,13 @@ func ViewPullFiles(ctx *context.Context) {
ctx.Data["RawPath"] = setting.AppSubURL + "/" + path.Join(headTarget, "raw", "commit", endCommitID)
ctx.Data["RequireHighlightJS"] = true
pathToLineToComment, err := models.FetchCodeComments(issue, ctx.User)
if err != nil {
ctx.ServerError("FetchCodeComments", err)
return
}
ctx.Data["CodeComments"] = pathToLineToComment
ctx.HTML(200, tplPullFiles)
}

View File

@ -114,6 +114,48 @@
<a class="ui green button add-code-comment add-code-comment-right" data-side="right" data-idx="{{$line.RightIdx}}">+</a>
{{end}}
</td>
{{if index $.CodeComments $file.Name (mul $line.LeftIdx -1)}}
<td class="add-comment-left add-comment-right">
<div class="field comment-code-cloud">
<div class="comment-list">
<ui class="ui comments">
{{ template "repo/diff/comments" dict "root" $ "comments" (index $.CodeComments $file.Name (mul $line.LeftIdx -1))}}
</ui>
</div>
<form class="ui form" action="/jonas/ggre/pulls/1/files/reviews/comments" method="post">
<input name="_csrf" value="cvHYSIf3f6KNll956UBanD4Dy7c6MTUyNTk2MDkwNjg3NTcyMjY5OQ==" type="hidden">
<input name="side" value="previous" type="hidden">
<input name="line" value="2" type="hidden">
<input name="path" value="README.md" type="hidden">
<input name="commit_id" value="e324f1688f063d6fd9268fb8f02149c9eb1a867c" type="hidden">
<input name="is_review" value="true" type="hidden">
<input name="diff_start_cid" type="hidden">
<input name="diff_end_cid" type="hidden">
<input name="diff_base_cid" type="hidden">
<div class="ui top attached tabular menu" data-write="write489406" data-preview="preview489406">
<a class="item active" data-tab="write489406">Write</a>
<a class="item" data-tab="preview489406" data-url="/api/v1/markdown" data-context="/jonas/ggre">Preview</a>
</div>
<div class="ui bottom attached tab segment active" data-tab="write489406">
<div class="field">
<textarea name="content" placeholder="Leave a comment"></textarea>
</div>
</div>
<div class="ui bottom attached tab segment markdown" data-tab="preview489406"></div>
<div class="footer">
<span class="markdown-info"><i class="octicon octicon-markdown"></i> Styling with markdown is supported.</span>
<div class="ui right floated">
<div class="ui submit tiny basic button btn-cancel">Cancel</div>
<div class="ui submit tiny basic button btn-add-single">Add single comment</div>
<button type="submit" class="ui submit green tiny button btn-add-comment">Add comment</button>
<div class="ui submit green tiny button btn-start-review">Start review</div>
</div>
</div>
</form>
</div>
</td>
{{end}}
<td class="lines-code lines-code-new halfwidth">
<pre><code class="wrap {{if $highlightClass}}language-{{$highlightClass}}{{else}}nohighlight{{end}}">{{if $line.RightIdx}}{{$section.GetComputedInlineDiffFor $line}}{{end}}</code></pre>
</td>

View File

@ -0,0 +1,51 @@
{{range .comments}}
{{ $createdStr:= TimeSinceUnix .CreatedUnix $.root.Lang }}
<div class="comment" id="{{.HashTag}}">
<a class="avatar" {{if gt .Poster.ID 0}}href="{{.Poster.HomeLink}}"{{end}}>
<img src="{{.Poster.RelAvatarLink}}">
</a>
<div class="content">
<div class="ui top attached header">
<span class="text grey"><a {{if gt .Poster.ID 0}}href="{{.Poster.HomeLink}}"{{end}}>{{.Poster.Name}}</a> {{$.root.i18n.Tr "repo.issues.commented_at" .HashTag $createdStr | Safe}}</span>
<div class="ui right actions">
{{if gt .ShowTag 0}}
<div class="item tag">
{{if eq .ShowTag 1}}
{{$.root.i18n.Tr "repo.issues.poster"}}
{{else if eq .ShowTag 2}}
{{$.root.i18n.Tr "repo.issues.collaborator"}}
{{else if eq .ShowTag 3}}
{{$.root.i18n.Tr "repo.issues.owner"}}
{{end}}
</div>
{{end}}
{{template "repo/issue/view_content/add_reaction" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID) }}
{{if or $.root.IsRepositoryAdmin (eq .Poster.ID $.root.SignedUserID)}}
<div class="item action">
<a class="edit-content" href="#"><i class="octicon octicon-pencil"></i></a>
<a class="delete-comment" href="#" data-comment-id={{.HashTag}} data-url="{{$.root.RepoLink}}/comments/{{.ID}}/delete" data-locale="{{$.root.i18n.Tr "repo.issues.delete_comment_confirm"}}"><i class="octicon octicon-x"></i></a>
</div>
{{end}}
</div>
</div>
<div class="ui attached segment">
<div class="render-content markdown has-emoji">
{{if .RenderedContent}}
{{.RenderedContent|Str2html}}
{{else}}
<span class="no-content">{{$.root.i18n.Tr "repo.issues.no_content"}}</span>
{{end}}
</div>
<div class="raw-content hide">{{.Content}}</div>
<div class="edit-content-zone hide" data-write="issuecomment-{{.ID}}-write" data-preview="issuecomment-{{.ID}}-preview" data-update-url="{{$.root.RepoLink}}/comments/{{.ID}}" data-context="{{$.root.RepoLink}}"></div>
</div>
{{$reactions := .Reactions.GroupByType}}
{{if $reactions}}
<div class="ui attached segment reactions">
{{template "repo/issue/view_content/reactions" Dict "ctx" $ "ActionURL" (Printf "%s/comments/%d/reactions" $.root.RepoLink .ID) "Reactions" $reactions }}
</div>
{{end}}
</div>
</div>
{{end}}