🎨 评论支持换行

pull/37/head
ruibaby 2018-10-07 11:14:01 +08:00
parent 975aa8278e
commit 1d2fdfbbec
4 changed files with 77 additions and 18 deletions

View File

@ -4,6 +4,7 @@ import cc.ryanc.halo.model.domain.Comment;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.User;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.enums.BlogPropertiesEnum;
import cc.ryanc.halo.model.enums.CommentStatusEnum;
import cc.ryanc.halo.model.enums.PostTypeEnum;
@ -27,10 +28,7 @@ import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@ -150,10 +148,11 @@ public class CommentController extends BaseController {
*
* @param commentId
* @param commentContent
* @return /admin/comments
* @return JsonResult
*/
@PostMapping(value = "/reply")
public String replyComment(@RequestParam("commentId") Long commentId,
@ResponseBody
public JsonResult replyComment(@RequestParam("commentId") Long commentId,
@RequestParam("postId") Long postId,
@RequestParam("commentContent") String commentContent,
@RequestParam("userAgent") String userAgent,
@ -182,7 +181,7 @@ public class CommentController extends BaseController {
comment.setCommentAuthorAvatarMd5(SecureUtil.md5(user.getUserEmail()));
comment.setCommentDate(DateUtil.date());
String lastContent = "<a href='#comment-id-" + lastComment.getCommentId() + "'>@" + lastComment.getCommentAuthor() + "</a> ";
comment.setCommentContent(lastContent + OwoUtil.markToImg(HtmlUtil.escape(commentContent)));
comment.setCommentContent(lastContent + OwoUtil.markToImg(HtmlUtil.escape(commentContent).replace("&lt;br/&gt;", "<br/>")));
comment.setCommentAgent(userAgent);
comment.setCommentParent(commentId);
comment.setCommentStatus(CommentStatusEnum.PUBLISHED.getCode());
@ -191,10 +190,11 @@ public class CommentController extends BaseController {
//邮件通知
new EmailToAuthor(comment, lastComment, post, user, commentContent).start();
return new JsonResult(1, "回复成功!");
} catch (Exception e) {
log.error("回复评论失败:{}", e.getMessage());
return new JsonResult(0, "回复失败!");
}
return "redirect:/admin/comments";
}
/**

View File

@ -127,10 +127,10 @@ public class FrontCommentController {
if (comment.getCommentParent() > 0) {
lastComment = commentService.findCommentById(comment.getCommentParent()).get();
String lastContent = "<a href='#comment-id-" + lastComment.getCommentId() + "'>@" + lastComment.getCommentAuthor() + "</a> ";
comment.setCommentContent(lastContent + OwoUtil.markToImg(HtmlUtil.escape(comment.getCommentContent())));
comment.setCommentContent(lastContent + OwoUtil.markToImg(HtmlUtil.escape(comment.getCommentContent()).replace("&lt;br/&gt;", "<br/>")));
} else {
//将评论内容的字符专为安全字符
comment.setCommentContent(OwoUtil.markToImg(HtmlUtil.escape(comment.getCommentContent())));
comment.setCommentContent(OwoUtil.markToImg(HtmlUtil.escape(comment.getCommentContent()).replace("&lt;br/&gt;", "<br/>")));
}
if (StrUtil.isNotEmpty(comment.getCommentAuthorUrl())) {
comment.setCommentAuthorUrl(URLUtil.formatUrl(comment.getCommentAuthorUrl()));

View File

@ -38,10 +38,10 @@ $('#comment-submit').click(function () {
async: false,
data: {
'postId': $('input[name=postId]').val(),
'commentContent': $('textarea[name=commentContent]').val(),
'commentAuthor': $('input[name=commentAuthor]').val(),
'commentAuthorEmail': $('input[name=commentAuthorEmail]').val(),
'commentAuthorUrl': $('input[name=commentAuthorUrl]').val(),
'commentContent': formatContent(content.val()),
'commentAuthor': author.val(),
'commentAuthorEmail': email.val(),
'commentAuthorUrl': url.val(),
'commentAgent': navigator.userAgent,
'commentParent': $('input[name=commentParent]').val()
},
@ -78,12 +78,22 @@ $('.comment-cancel-reply').click(function () {
$('#commentContent').attr("placeholder","");
$(".comment-cancel-reply").hide();
});
/**
*
*/
function loadAvatar() {
$(".comment-author-avatar").attr("src","//gravatar.loli.net/avatar/"+md5(localStorage.getItem("email"))+"?s=256&d="+avatarType);
if($('input[name=commentAuthorEmail]').val()!='' && $('input[name=commentAuthorEmail]').val()!=null){
$(".comment-author-avatar").attr("src","//gravatar.loli.net/avatar/"+md5($('input[name=commentAuthorEmail]').val())+"?s=256&d="+avatarType);
}
}
/**
* ua
* @param string
* @returns {string}
*/
var parser = new UAParser();
function show_ua(string){
parser.setUA(string);
@ -95,3 +105,15 @@ function show_ua(string){
var os = uua.os.name + ' ' + uua.os.version;
return '<span class="ua">'+browser+'</span><span class="ua">'+os+'</span>';
}
/**
*
* @param a a
* @returns {*}
*/
function formatContent(a) {
a = a.replace(/\r\n/g, '<br/>');
a = a.replace(/\n/g, '<br/>');
a = a.replace(/\s/g, ' ');
return a;
}

View File

@ -129,16 +129,16 @@
<h4 class="modal-title"><@spring.message code="common.btn.reply" /></h4>
</div>
<form method="post" action="/admin/comments/reply">
<input type="hidden" id="commentId" name="commentId" value=""/>
<input type="hidden" id="userAgent" name="userAgent" value=""/>
<input type="hidden" id="postId" name="postId" value="" />
<div class="modal-body">
<textarea class="form-control comment-input-content" rows="5" id="commentContent" name="commentContent" style="resize: none"></textarea>
<div class="OwO"></div>
</div>
<div class="modal-footer">
<input type="hidden" id="commentId" name="commentId" value=""/>
<input type="hidden" id="userAgent" name="userAgent" value=""/>
<input type="hidden" id="postId" name="postId" value="" />
<button type="button" class="btn btn-default" data-dismiss="modal"><@spring.message code="common.btn.cancel" /></button>
<button type="submit" class="btn btn-primary"><@spring.message code="common.btn.define" /></button>
<button type="button" class="btn btn-primary" onclick="reply()"><@spring.message code="common.btn.define" /></button>
</div>
</form>
</div>
@ -163,12 +163,49 @@
window.location.href=url;
}
/**
* 显示回复模态框
*
* @param commentId commentId
* @param postId postId
*/
function replyShow(commentId,postId) {
$('#userAgent').val(navigator.userAgent);
$('#commentId').val(commentId);
$('#postId').val(postId);
$('#commentReplyModal').modal();
}
function reply() {
$.ajax({
type: 'POST',
url: '/admin/comments/reply',
async: false,
data: {
'commentId': $("#commentId").val(),
'userAgent': $("#userAgent").val(),
'postId': $("#postId").val(),
'commentContent': formatContent($("#commentContent").val())
},
success: function (data) {
if(data.code==1){
window.location.reload();
}
}
});
}
/**
* 格式化字符串
* @param a a
* @returns {*}
*/
function formatContent(a) {
a = a.replace(/\r\n/g, '<br/>');
a = a.replace(/\n/g, '<br/>');
a = a.replace(/\s/g, ' ');
return a;
}
</script>
</div>
<#include "module/_footer.ftl">