🍎 新增文章/页面自动保存的功能,防止编辑的时候丢失文章,另外,ctrl+c可以保存为草稿。

pull/18/head
ruibaby 2018-05-24 21:53:20 +08:00
parent c1770f12a8
commit e362bd9df4
5 changed files with 88 additions and 23 deletions

View File

@ -27,6 +27,7 @@ public class JsonResult {
/** /**
* *
*
* @param code * @param code
* @param msg * @param msg
*/ */
@ -37,6 +38,7 @@ public class JsonResult {
/** /**
* *
*
* @param code * @param code
* @param msg * @param msg
* @param result * @param result
@ -46,4 +48,15 @@ public class JsonResult {
this.msg = msg; this.msg = msg;
this.result = result; this.result = result;
} }
/**
*
*
* @param code
* @param result
*/
public JsonResult(Integer code, Object result) {
this.code = code;
this.result = result;
}
} }

View File

@ -2,6 +2,7 @@ package cc.ryanc.halo.web.controller.admin;
import cc.ryanc.halo.model.domain.*; import cc.ryanc.halo.model.domain.*;
import cc.ryanc.halo.model.dto.HaloConst; import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.model.dto.JsonResult;
import cc.ryanc.halo.model.dto.LogsRecord; import cc.ryanc.halo.model.dto.LogsRecord;
import cc.ryanc.halo.service.CategoryService; import cc.ryanc.halo.service.CategoryService;
import cc.ryanc.halo.service.LogsService; import cc.ryanc.halo.service.LogsService;
@ -9,6 +10,7 @@ import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.TagService; import cc.ryanc.halo.service.TagService;
import cc.ryanc.halo.utils.HaloUtils; import cc.ryanc.halo.utils.HaloUtils;
import cc.ryanc.halo.web.controller.core.BaseController; import cc.ryanc.halo.web.controller.core.BaseController;
import lombok.Value;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -196,30 +198,51 @@ public class PostController extends BaseController{
/** /**
* 稿 * 稿
*
* @param post post * @param post post
* @param session session * @param session session
* @return * @return
*/ */
@PostMapping(value = "/new/autoPush") @PostMapping(value = "/new/autoPush")
@ResponseBody @ResponseBody
public Post autoPushPost(@ModelAttribute Post post, HttpSession session){ public JsonResult autoPushPost(@RequestParam(value = "postId",defaultValue = "0") Long postId,
@RequestParam(value = "postTitle") String postTitle,
@RequestParam(value = "postUrl") String postUrl,
@RequestParam(value = "postContentMd") String postContentMd,
@RequestParam(value = "postType",defaultValue = "post") String postType,
HttpSession session){
Post post=null;
User user = (User)session.getAttribute(HaloConst.USER_SESSION_KEY); User user = (User)session.getAttribute(HaloConst.USER_SESSION_KEY);
if(postId==0) {
post = new Post();
}else{
post = postService.findByPostId(postId).get();
}
try{ try{
if(StringUtils.isEmpty(post.getPostTitle())){ if(StringUtils.isEmpty(postTitle)){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
post.setPostTitle("草稿:"+dateFormat.format(new Date())); post.setPostTitle("草稿:"+dateFormat.format(new Date()));
}else{
post.setPostTitle(postTitle);
} }
if(StringUtils.isEmpty(post.getPostUrl())){ if(StringUtils.isEmpty(postUrl)){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
post.setPostTitle(dateFormat.format(new Date())); post.setPostUrl(dateFormat.format(new Date()));
}else{
post.setPostUrl(postUrl);
} }
post.setPostId(postId);
post.setPostStatus(1);
post.setPostContentMd(postContentMd);
post.setPostType(postType);
post.setPostDate(new Date()); post.setPostDate(new Date());
post.setPostUpdate(new Date()); post.setPostUpdate(new Date());
post.setUser(user); post.setUser(user);
}catch (Exception e){ }catch (Exception e){
log.error("未知错误:", e.getMessage()); log.error("未知错误:", e.getMessage());
return new JsonResult(0,"保存失败");
} }
return postService.saveByPost(post); return new JsonResult(1,postService.saveByPost(post));
} }

View File

@ -40,6 +40,8 @@
<div class="col-md-9"> <div class="col-md-9">
<#if post??> <#if post??>
<input type="hidden" id="postId" name="postId" value="${post.postId?c}"> <input type="hidden" id="postId" name="postId" value="${post.postId?c}">
<#else >
<input type="hidden" id="postId" name="postId" value="">
</#if> </#if>
<div style="margin-bottom: 10px;"> <div style="margin-bottom: 10px;">
<input type="text" class="form-control input-lg" id="post_title" name="post_title" placeholder="请输入页面标题" value="<#if post??>${post.postTitle}</#if>"> <input type="text" class="form-control input-lg" id="post_title" name="post_title" placeholder="请输入页面标题" value="<#if post??>${post.postTitle}</#if>">
@ -220,9 +222,7 @@
url: '/admin/page/new/push', url: '/admin/page/new/push',
async: false, async: false,
data: { data: {
<#if post??>
'postId': $('#postId').val(), 'postId': $('#postId').val(),
</#if>
'postStatus': status, 'postStatus': status,
'postTitle': Title, 'postTitle': Title,
'postUrl' : $('#postUrl').html().toString(), 'postUrl' : $('#postUrl').html().toString(),
@ -255,12 +255,46 @@
}); });
} }
setInterval("autoPush()","30000");
/**
* 自动保存文章
*/
function autoPush() {
var Title = "";
if(postTitle.val()){
Title = postTitle.val();
}
$.ajax({
type: 'POST',
url: '/admin/posts/new/autoPush',
async: false,
data: {
'postId': $('#postId').val(),
'postTitle': Title,
'postUrl' : $('#postUrl').html().toString(),
'postContentMd': editor.getMarkdown(),
'postType' : "page"
},
success: function (data) {
if(!$("#post_title").val()){
$("#post_title").val(data.result.postTitle);
}
if(!$("#postId").val()){
$("#postId").val(data.result.postId);
}
if($("#postUrl").html()==''){
$("#postUrl").html(data.result.postUrl);
}
}
});
}
/** /**
* Ctrl+C保存 * Ctrl+C保存
*/ */
$(document).keydown(function (event) { $(document).keydown(function (event) {
if(event.ctrlKey&&event.keyCode === 83){ if(event.ctrlKey&&event.keyCode === 83){
push(1); autoPush();
} }
}); });
</script> </script>

View File

@ -39,6 +39,8 @@
<div class="col-md-9"> <div class="col-md-9">
<#if post??> <#if post??>
<input type="hidden" id="postId" name="postId" value="${post.postId?c}"> <input type="hidden" id="postId" name="postId" value="${post.postId?c}">
<#else>
<input type="hidden" id="postId" name="postId" value="">
</#if> </#if>
<div style="margin-bottom: 10px;"> <div style="margin-bottom: 10px;">
<input type="text" class="form-control input-lg" id="post_title" name="post_title" placeholder="请输入文章标题" onblur="autoComplateUrl();" value="<#if post??>${post.postTitle}</#if>"> <input type="text" class="form-control input-lg" id="post_title" name="post_title" placeholder="请输入文章标题" onblur="autoComplateUrl();" value="<#if post??>${post.postTitle}</#if>">
@ -310,9 +312,7 @@
url: '/admin/posts/new/push', url: '/admin/posts/new/push',
async: false, async: false,
data: { data: {
<#if post??>
'postId': $('#postId').val(), 'postId': $('#postId').val(),
</#if>
'postStatus': status, 'postStatus': status,
'postTitle': Title, 'postTitle': Title,
'postUrl' : $('#postUrl').html().toString(), 'postUrl' : $('#postUrl').html().toString(),
@ -346,7 +346,7 @@
} }
}); });
} }
//setInterval("autoPush()","5000"); setInterval("autoPush()","30000");
/** /**
* 自动保存文章 * 自动保存文章
*/ */
@ -360,25 +360,20 @@
url: '/admin/posts/new/autoPush', url: '/admin/posts/new/autoPush',
async: false, async: false,
data: { data: {
<#if post??>
'postId': $('#postId').val(), 'postId': $('#postId').val(),
</#if>
'postStatus': 1,
'postTitle': Title, 'postTitle': Title,
'postUrl' : $('#postUrl').html().toString(), 'postUrl' : $('#postUrl').html().toString(),
'postContentMd': editor.getMarkdown(), 'postContentMd': editor.getMarkdown()
'postContent': editor.getHTML(),
'postThumbnail': $('#selectImg')[0].src
}, },
success: function (data) { success: function (data) {
if(!$("#post_title").val()){ if(!$("#post_title").val()){
$("#post_title").val(data.postTitle); $("#post_title").val(data.result.postTitle);
} }
if(!$("#postId").val()){ if(!$("#postId").val()){
$("#postId").val(data.postId); $("#postId").val(data.result.postId);
} }
if($("#postUrl").html()==null || $("#postUrl").html()==""){ if($("#postUrl").html()==''){
$("#postUrl").val(data.postUrl); $("#postUrl").html(data.result.postUrl);
} }
} }
}); });
@ -389,7 +384,7 @@
*/ */
$(document).keydown(function (event) { $(document).keydown(function (event) {
if(event.ctrlKey&&event.keyCode === 83){ if(event.ctrlKey&&event.keyCode === 83){
//autoPush(); autoPush();
} }
}); });
</script> </script>

View File

@ -19,7 +19,7 @@
</h3> </h3>
</div> </div>
<div class="post-content"> <div class="post-content">
${post.postContent} ${post.postContent?if_exists}
</div> </div>
<div class="post-footer"> <div class="post-footer">
<div class="meta"> <div class="meta">