修复非wiki路径下atom重新得到焦点检查库变化时报错问题

This commit is contained in:
TevinLi 2016-08-24 23:12:36 +08:00
parent 9a8f9c5939
commit d64ab53579
3 changed files with 40 additions and 14 deletions

View File

@ -1,7 +1,7 @@
# amWiki 文库
amWiki是一款基于atom编辑器markdown语法的轻量级wiki文库系统。
使用amWiki创建文库,仅需要您使用atom编辑markdown文档和支持http静态访问网页空间
使用amWiki,您无需服务端开发,只需使用atom编辑markdown文档和支持http静态访问网页空间
传送门:[amWiki项目托管地址](https://github.com/TevinLi/amWiki "amWiki项目托管地址")
### amWiki优势

View File

@ -31,6 +31,7 @@ module.exports = {
//监听用户操作
this.subscriptions = new CompositeDisposable();
this.subscriptions.add(atom.commands.onDidDispatch(function (e) {
//console.log(e.type);
//重命名、新增文件夹或文件、创建副本
if (/core\:confirm/.test(e.type) && e.target.className == 'editor mini is-focused') {
that.toCheckLib(state);
@ -39,11 +40,13 @@ module.exports = {
else if (/tree-view\:(remove|cut|paste)/.test(e.type)) {
that.toCheckLib(state);
}
//删除项目文件夹
//tree-view:remove-project-folder
}));
//拖拽操作监听
atom.packages.activatePackage('tree-view').then(function (pkg) {
that.treeView = pkg.mainModule.treeView;
that.treeViewState = pkg.mainModule.state;
that.treeView = pkg.mainModule.treeView; //目录树主对象
that.treeState = pkg.mainModule.state; //目录树全局状态
that.treeView[0].addEventListener('drop', function (e) {
for (var i = 0; i < e.path.length; i++) {
if (e.path[i].className.indexOf('project-root') >= 0) {
@ -60,13 +63,14 @@ module.exports = {
lastTimestamp = Date.now();
});
window.addEventListener('focus', function () {
//如果atom插件设置“得到焦点时检查库变化”项处于开启状态
if (atom.config.get('amWiki.checkLibraryOnWindowFocus')) {
if (Date.now() - lastTimestamp > 30000) {
that.toCheckLib(state);
}
}
});
//当atom启动时全量检查文库
//当atom启动时如果插件设置“启动时全量检查文库变化”项处于开启状态,全量检查文库
if (atom.config.get('amWiki.checkAllLibraryOnAtomStart')) {
for (var l = 0; l < state.libraryList.length; l++) {
this.checkLibrary(state, state.libraryList[l]);
@ -76,32 +80,55 @@ module.exports = {
//准备检查文库
toCheckLib: function (state, curLibrary, recursive) {
var that = this;
//当存在curLibrary参数时直接检查此文库
if (curLibrary) {
this.checkLibrary(state, curLibrary);
} else {
if (this.treeView) {
}
//当不存在curLibrary参数时尝试获取当前库
else {
//如果treeView已经准备完毕
if (this.treeView && this.treeView.selectedPath) {
curLibrary = this.treeView.selectedPath.replace(/library(.|\s)*$/, 'library/').replace(/\\/g, '/');
//如果treeView当前路径包含library
if (curLibrary && curLibrary.indexOf('library') >= 0) {
this.checkLibrary(state, curLibrary);
} else if (this.treeViewState) {
curLibrary = this.treeViewState.selectedPath.replace(/library(.|\s)*$/, 'library/').replace(/\\/g, '/');
}
//如果treeView当前路径补/library/字段后是已存在的wiki
else if (typeof state.libraryMD5[curLibrary + '/library/'] != 'undefined') {
this.checkLibrary(state, curLibrary + '/library/');
}
//从treeState获取路径
else if (this.treeState && this.treeState.selectedPath) {
curLibrary = this.treeState.selectedPath.replace(/library(.|\s)*$/, 'library/').replace(/\\/g, '/');
//如果treeState当前路径包含library
if (curLibrary && curLibrary.indexOf('library') >= 0) {
this.checkLibrary(state, curLibrary);
}
//如果treeState当前路径补/library/字段后是已存在的wiki
else if (typeof state.libraryMD5[curLibrary + '/library/'] != 'undefined') {
this.checkLibrary(state, curLibrary + '/library/');
}
//如果treeState当前路径也不匹配不检查文库更新
}
} else {
//如果treeView未准备完毕0.1秒后重新检查且最多只递归3次
}
//如果treeView未准备完毕0.2秒后重新检查且最多只递归3次
else {
setTimeout(function () {
recursive = recursive ? recursive + 1 : 1;
if (recursive <= 3) {
that.toCheckLib(state, curLibrary, recursive);
}
}, 100);
}, 200);
}
}
},
//检查文库变化
checkLibrary: function (state, curLibrary) {
curLibrary = curLibrary.split('library/')[0] + 'library/';
if (typeof state.libraryMD5[curLibrary] == 'undefined') {
return;
}
console.log(curLibrary);
//延迟0.1秒,等待系统文件操作完毕再读取
setTimeout(function () {
directories.readLibraryDir(curLibrary, function (err, tree, files) {

View File

@ -204,8 +204,7 @@ module.exports = {
child_process.exec(cmd + ' ' + url);
},
//关闭服务器
destroy: function(){
this.server.close();
destroy: function () {
this.server && this.server.close();
}
};