Browse Source

edit comments

pull/946/head
Alexey Makhov 10 years ago
parent
commit
91434bf5c3
  1. 11
      models/issue.go
  2. 3
      public/ng/css/gogs.css
  3. 39
      public/ng/js/gogs.js
  4. 5
      public/ng/less/gogs/repository.less
  5. 27
      routers/repo/commit.go
  6. 14
      templates/repo/comment_form.tmpl
  7. 2
      templates/repo/diff.tmpl

11
models/issue.go

@ -940,6 +940,17 @@ func CreateComment(userId, repoId, issueId int64, commitId, line string, cmtType
return comment, sess.Commit()
}
// UpdateComment update comment
func UpdateComment(comment *Comment) error {
_, err := x.Id(comment.Id).AllCols().Update(comment)
if err != nil {
return err
}
return err
}
// GetCommentById returns the comment with the given id
func GetCommentById(commentId int64) (*Comment, error) {
c := &Comment{Id: commentId}

3
public/ng/css/gogs.css

@ -1696,6 +1696,9 @@ The register and sign-in page style
background-color: #FFF8D2 !important;
border-color: #F0DB88 !important;
}
.diff-file-box .code-diff tbody tr.add-comment td {
padding: 10px;
}
.diff-file-box .code-diff tbody tr.add-comment:hover td,
.diff-file-box .code-diff tbody tr.comment:hover td,
.diff-file-box .code-diff tbody tr.add-comment:hover pre,

39
public/ng/js/gogs.js

@ -240,7 +240,7 @@ var Gogs = {};
function prepareToForm() {
$('.add-comment').hide('fast', function(){ $(this).remove(); });
$('button.answer').show();
$('button.answer').hide();
}
$(document).on('click', '.code-diff .lines-num span', function (e) {
@ -270,23 +270,57 @@ var Gogs = {};
var elem = (commentTr.length > 0) ? commentTr : $(this).parents('tr');
var url = commit[1] + '/commit/comment/' + commit[2];
elem.after(
$('<tr class="add-comment">').load(url + '?line=' + lineNum, function () {
$('<tr class="add-comment">').append(
$('<td colspan="3">').load(url + '?line=' + lineNum, function () {
$('.menu-line.add-nav').tabs();
$('#pull-commit-preview').markdown_preview(".commit-add-comment");
$('body').animate({
scrollTop: $(this).offset().top - 33 // height of button
}, 1000);
})
)
);
}
});
$('.code-diff').on('click', '#cancel-commit-conversation', function () {
prepareToForm();
$('button.answer').show();
return false;
});
$('.code-diff').on('click', '#cancel-edit-commit-conversation', function () {
prepareToForm();
$(this).parents('.commit-comment').children().show();
$(this).parents('#commit-conversation').parent().remove();
$('button.answer').show();
return false;
});
$('.edit-comment').click(function () {
prepareToForm();
var text = $(this).parents('div.panel:first').find('.markdown').text();
var id = $(this).parents('.commit-comment').attr('id');
id = id.substr(15);
var commit = document.location.href.match(/([a-zA-Z0-9:\/\/]+)\/commit\/([a-z0-9]+)/);
var url = commit[1] + '/commit/comment/' + commit[2];
$(this).parents('.commit-comment').children().hide();
$(this).parents('.commit-comment').append(
$('<div>').load(url + '?id='+id, function () {
$('.menu-line.add-nav').tabs();
$('#pull-commit-preview').markdown_preview(".commit-add-comment");
$('#commit-add-content').text(text.trim());
$('body').animate({
scrollTop: $(this).offset().top - 33 // height of button
}, 1000);
})
)
return false;
});
$('.remove-comment').click(function () {
if (confirm('Are you sure?')) {
var commit = document.location.href.match(/([a-zA-Z0-9:\/\/]+)\/commit\/([a-z0-9]+)/);
var url = commit[1] + '/commit/comment/delete/';
$.ajax({
@ -302,6 +336,7 @@ var Gogs = {};
}
}
});
}
return false;
});

5
public/ng/less/gogs/repository.less

@ -727,6 +727,11 @@
border-color: #F0DB88 !important;
}
}
&.add-comment {
td {
padding: 10px;
}
}
&.add-comment:hover, &.comment:hover {
td, pre {
background-color: #FFFFFF !important;

27
routers/repo/commit.go

@ -362,6 +362,7 @@ func CompareDiff(ctx *middleware.Context) {
func GetCommentForm(ctx *middleware.Context) {
ctx.Data["Repo"] = ctx.Repo
ctx.Data["Line"] = ctx.Query("line")
ctx.Data["CommentId"] = ctx.Query("id")
ctx.HTML(200, COMMENT_FORM)
}
@ -385,19 +386,39 @@ func CreateCommitComment(ctx *middleware.Context) {
}
}
var comment *models.Comment
var err error
commitId := ctx.ParamsEscape(":commitId")
content := ctx.Query("content")
line := ctx.Query("line")
lineRe, err := regexp.Compile("[0-9]+L[0-9]+")
if len(content) > 0 && lineRe.MatchString(line) {
if len(content) > 0 {
switch ctx.Params(":action") {
case "new":
line := ctx.Query("line")
lineRe, err := regexp.Compile("[0-9]+L[0-9]+")
if len(line) > 0 && !lineRe.MatchString(line) {
err := errors.New("Something went wrong")
send(200, err.Error(), err)
return
}
if comment, err = models.CreateComment(ctx.User.Id, ctx.Repo.Repository.Id, 0, commitId, line, models.COMMENT_TYPE_COMMENT, content, nil); err != nil {
send(500, nil, err)
return
}
log.Trace("%s Comment created: %s", ctx.Req.RequestURI, commitId)
case "edit":
commentId := com.StrTo(ctx.Query("id")).MustInt64()
comment, err = models.GetCommentById(commentId)
if err != nil {
send(500, nil, err)
return
}
comment.Content = content
if err = models.UpdateComment(comment); err != nil {
send(500, nil, err)
return
}
log.Trace("%s Comment updated: %v", ctx.Req.RequestURI, comment.Id)
default:
ctx.Handle(404, "commit.Comment", err)
return

14
templates/repo/comment_form.tmpl

@ -1,6 +1,6 @@
<td colspan="3">
<!--<td colspan="3">-->
{{if .SignedUser}}
<div id="commit-conversation" class="js-tab-show clear commit-comment">
<div id="commit-conversation" class="js-tab-show clear">
<div id="commit-conversation-list" class="left grid-5-6">
<div class="commit-add-comment clear">
<img class="avatar-40 radius" src="{{.SignedUser.AvatarLink}}" alt=""/>
@ -16,12 +16,16 @@
</div>
<div class="panel-content content">
<div id="submit-error" class="text-danger"></div>
<form id="commit-add-comment-form" action="{{AppSubUrl}}/{{.Repo.Owner.Name}}/{{.Repo.Repository.Name}}/commit/comment/new/{{.Repo.CommitId}}" method="post">
<form id="commit-add-comment-form" action="{{AppSubUrl}}/{{.Repo.Owner.Name}}/{{.Repo.Repository.Name}}/commit/comment/{{if .CommentId}}edit{{else}}new{{end}}/{{.Repo.CommitId}}" method="post">
{{.CsrfTokenHtml}}
{{if .CommentId}}
<input type="hidden" name="id" value="{{.CommentId}}" >
{{else}}
<input type="hidden" name="line" value="{{.Line}}" >
{{end}}
<textarea class="ipt ipt-radius js-preview-input" name="content" id="commit-add-content"></textarea>
<p class="submit text-right">
<button class="btn btn-gray btn-radius text-bold" id="cancel-commit-conversation" name="submit" value="close">{{$.i18n.Tr "cancel"}}</button>&nbsp;&nbsp;
<button class="btn btn-gray btn-radius text-bold" id="cancel-{{if .CommentId}}edit-{{end}}commit-conversation" name="submit" value="close">{{$.i18n.Tr "cancel"}}</button>&nbsp;&nbsp;
<button class="btn btn-green btn-radius text-bold" name="submit" value="comment">{{$.i18n.Tr "repo.commits.comment.add"}}</button>
</p>
</form>
@ -34,4 +38,4 @@
</div>
</div>
{{else}}<div class="alert alert-warning"><a class="btn btn-success btn-lg" href="{{AppSubUrl}}/user/sign_up">Sign up for free</a> to join this conversation. Already have an account? <a href="{{AppSubUrl}}/user/login">Sign in to comment</a></div>{{end}}
</td>
<!--</td>-->

2
templates/repo/diff.tmpl

@ -140,7 +140,7 @@
{{end}}
{{if $.SignedUser}}
{{if eq $.SignedUser.Id .Poster.Id}}
<!--<a href="#" class="edit-comment"><i class="octicon octicon-pencil"></i></a>-->
<a href="#" class="edit-comment"><i class="octicon octicon-pencil"></i></a>
<a href="#" data-id="{{.Id}}" class="remove-comment"><i class="octicon octicon-x"></i></a>
{{end}}
{{end}}

Loading…
Cancel
Save