feat:1、实现文档自动保存

2、实现记住上次打开的文档,当文档编辑时会自动定位到上次打开的文档。
This commit is contained in:
lifei6671
2018-08-17 17:33:45 +08:00
parent cd61aa14db
commit bb7d1c1503
12 changed files with 245 additions and 123 deletions

View File

@@ -423,6 +423,7 @@ textarea{
margin: 0 15px;
padding: 10px 20px;
line-height: 25px;
word-break: break-word;
}
.manual-search-reader .search-item:hover{
background-color: #F5F5F5;

32
static/js/array.js Normal file
View File

@@ -0,0 +1,32 @@
/**
* 删除数组中的匹配值
* @param $callback
*/
Array.prototype.remove = function ($callback) {
var $isFunction = typeof $callback === "function";
var arr = [];
for(var $i = 0,$len = this.length; $i < $len;$i ++){
if($isFunction){
if($callback(this[$i])){
arr.push($i);
}
}else if(this[$i] == $callback){
arr.push($i);
}
}
for($i = 0,$len = arr.length; $i < $len;$i++){
this.slice($i,1);
}
};
//格式化文件大小
function formatBytes($size) {
if (typeof $size === "number") {
var $units = [" B", " KB", " MB", " GB", " TB"];
for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
return $size.toFixed(2) + $units[$i];
}
return $size;
}

View File

@@ -1,6 +1,64 @@
/**
* Created by lifei6671 on 2017/4/29 0029.
*/
/**
* 打开最后选中的节点
*/
function openLastSelectedNode() {
//如果文档树或编辑器没有准备好则不加载文档
if (window.treeCatalog == null || window.editor == null) {
return false;
}
var $isSelected = false;
if(window.localStorage){
var $selectedNodeId = window.localStorage.getItem("MinDoc::LastLoadDocument:" + window.book.identify);
try{
if($selectedNodeId){
//遍历文档树判断是否存在节点
$.each(window.documentCategory,function (i, n) {
if(n.id == $selectedNodeId && !$isSelected){
var $node = {"id" : n.id};
window.treeCatalog.deselect_all();
window.treeCatalog.select_node($node);
$isSelected = true;
}
});
}
}catch($ex){
console.log($ex)
}
}
//如果节点不存在,则默认选中第一个节点
if (!$isSelected && window.documentCategory.length > 0){
var doc = window.documentCategory[0];
if(doc && doc.id > 0){
var node = {"id": doc.id};
$("#sidebar").jstree(true).select_node(node);
$isSelected = true;
}
}
return $isSelected;
}
/**
* 设置最后选中的文档
* @param $node
*/
function setLastSelectNode($node) {
if(window.localStorage) {
if (typeof $node === "undefined" || !$node) {
window.localStorage.removeItem("MinDoc::LastLoadDocument:" + window.book.identify);
} else {
var nodeId = $node.id ? $node.id : $node.node.id;
window.localStorage.setItem("MinDoc::LastLoadDocument:" + window.book.identify, nodeId);
}
}
}
/**
* 保存排序
* @param node
@@ -24,8 +82,6 @@ function jstree_save(node, parent) {
shade: [0.1, '#fff'] //0.1透明度的白色背景
});
console.log(JSON.stringify(nodeData));
$.ajax({
url : window.sortURL,
type :"post",
@@ -88,6 +144,13 @@ function openDeleteDocumentDialog($node) {
layer.close(index);
if(res.errcode === 0){
window.treeCatalog.delete_node($node);
window.documentCategory.remove(function (item) {
return item.id == $node.id;
});
// console.log(window.documentCategory)
setLastSelectNode();
}else{
layer.msg("删除失败",{icon : 2})
}
@@ -153,10 +216,9 @@ function pushDocumentCategory($node) {
function pushVueLists($lists) {
window.vueApp.lists = [];
for(var j in $lists){
var item = $lists[j];
$.each($lists,function (i, item) {
window.vueApp.lists.push(item);
}
});
}
/**
@@ -245,14 +307,6 @@ window.documentHistory = function() {
}
});
};
//格式化文件大小
function formatBytes($size) {
var $units = [" B", " KB", " MB", " GB", " TB"];
for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
return $size.toFixed(2) + $units[$i];
}
function uploadImage($id,$callback) {
/** 粘贴上传图片 **/
@@ -370,5 +424,26 @@ $(function () {
}
}
});
/**
* 启动自动保存默认30s自动保存一次
*/
if(window.book.auto_save){
setTimeout(function () {
setInterval(function () {
var $then = $("#markdown-save");
if(!window.saveing && $then.hasClass("change")){
$then.trigger("click");
}
},30000);
},30000);
}
/**
* 当离开窗口时存在未保存的文档会提示保存
*/
$(window).on("beforeunload",function () {
if($("#markdown-save").hasClass("change")){
return '您输入的内容尚未保存确定离开此页面吗';
}
});
});
});

View File

@@ -1,6 +1,7 @@
var events = function () {
var articleOpen = function (event, $param) {
//当打开文档时将文档ID加入到本地缓存。
window.localStorage && window.localStorage.setItem("MinDoc::LastLoadDocument:" + window.book.identify, $param.$id);
var prevState = window.history.state || {};
if ('pushState' in history) {

View File

@@ -43,18 +43,8 @@ $(function () {
};
this.addKeyMap(keyMap);
var $select_node_id = window.treeCatalog.get_selected();
if ($select_node_id) {
var $select_node = window.treeCatalog.get_node($select_node_id[0])
if ($select_node) {
$select_node.node = {
id: $select_node.id
};
loadDocument($select_node);
}
}
//如果没有选中节点则选中默认节点
openLastSelectedNode();
uploadImage("docEditor", function ($state, $res) {
if ($state === "before") {
return layer.load(1, {
@@ -67,6 +57,7 @@ $(function () {
}
}
});
},
onchange: function () {
resetEditorChanged(true);
@@ -164,6 +155,7 @@ $(function () {
pushDocumentCategory(node);
window.selectNode = node;
pushVueLists(res.data.attach);
setLastSelectNode($node);
} else {
layer.msg("文档加载失败");
}
@@ -202,6 +194,7 @@ $(function () {
$.ajax({
beforeSend: function () {
index = layer.load(1, { shade: [0.1, '#fff'] });
window.saveing = true;
},
url: window.editURL,
data: { "identify": window.book.identify, "doc_id": doc_id, "markdown": content, "html": html, "cover": $is_cover ? "yes" : "no", "version": version },
@@ -212,6 +205,7 @@ $(function () {
layer.close(index);
if (res.errcode === 0) {
resetEditorChanged(false);
window.saveing = false;
for (var i in window.documentCategory) {
var item = window.documentCategory[i];
@@ -223,6 +217,7 @@ $(function () {
if (typeof callback === "function") {
callback();
}
} else if(res.errcode === 6005) {
var confirmIndex = layer.confirm('文档已被其他人修改确定覆盖已存在的文档吗', {
btn: ['确定', '取消'] // 按钮
@@ -237,6 +232,7 @@ $(function () {
error : function (XMLHttpRequest, textStatus, errorThrown) {
layer.close(index);
layer.msg("服务器错误:" + errorThrown);
window.saveing = false;
}
});
}
@@ -357,8 +353,11 @@ $(function () {
}
}
}
}).on('loaded.jstree', function () {
window.treeCatalog = $(this).jstree();
}).on("ready.jstree",function () {
window.treeCatalog = $("#sidebar").jstree(true);
//如果没有选中节点则选中默认节点
// openLastSelectedNode();
}).on('select_node.jstree', function (node, selected, event) {
if ($("#markdown-save").hasClass('change')) {
@@ -371,7 +370,9 @@ $(function () {
}
loadDocument(selected);
}).on("move_node.jstree", jstree_save);
}).on("move_node.jstree", jstree_save).on("delete_node.jstree",function($node,$parent) {
openLastSelectedNode();
});
/**
* 打开文档模板
*/