diff --git a/public/js/app/api.js b/public/js/app/api.js index 6604e7d4..d724ff13 100644 --- a/public/js/app/api.js +++ b/public/js/app/api.js @@ -138,6 +138,17 @@ var Api = { return me._exportMenus; }, + // 导出, 笔记本下 + _exportMenusForNotebook: [], + addExportMenuForNotebook: function(menu) { + var me = this; + me._exportMenusForNotebook.push(menu); + }, + getExportMenusForNotebook: function() { + var me = this; + return me._exportMenusForNotebook; + }, + // 更多菜单 _moreMenus: [], getMoreMenus: function() { diff --git a/public/js/app/notebook.js b/public/js/app/notebook.js index 233769fd..d19accc0 100644 --- a/public/js/app/notebook.js +++ b/public/js/app/notebook.js @@ -1149,7 +1149,7 @@ Notebook.init = function() { this.menu.append(this.rename); this.menu.append(this.del); - // 导入菜单 + // 导入菜单 var importMenus = Api.getImportMenus(); if(importMenus && importMenus.length) { var importSubmenus = new gui.Menu(); @@ -1168,9 +1168,43 @@ Notebook.init = function() { submenu: importSubmenus, label: getMsg('Import notes') }); + this.menu.append(gui.getSeparatorMenu()); this.menu.append(this.imports); } + // 导出 + var exportsSubMenus = new gui.Menu(); + var exportMenus = Api.getExportMenusForNotebook() || []; + for(var i = 0; i < exportMenus.length; ++i) { + (function(j) { + + var menu = exportMenus[j]; + var clickBac = menu.click; + + var menuItem = new gui.MenuItem({ + label: menu.label, + click: function(e) { + var notebookId = $(me.target).attr('notebookId'); + clickBac && clickBac(notebookId); + } + }); + + exportMenus[i].menu = menuItem; + exportsSubMenus.append(menuItem); + })(i); + } + + if(exportMenus.length > 0) { + this.exports = new gui.MenuItem({ + label: getMsg('Export notes'), + submenu: exportsSubMenus, + click: function(e) { + } + }); + + this.menu.append(this.exports); + } + this.enable = function(name, ok) { this[name].enabled = ok; } diff --git a/public/plugins/export_html/plugin.js b/public/plugins/export_html/plugin.js index bbaed344..9e49fb1d 100644 --- a/public/plugins/export_html/plugin.js +++ b/public/plugins/export_html/plugin.js @@ -167,13 +167,7 @@ define(function() { }); }, - loadingIsClosed: false, - - exportHTML: function (noteIds) { - var me = this; - if (!noteIds || noteIds.length == 0) { - return; - } + getTargetPath: function(callback) { // showSaveDialog 不支持property选择文件夹 Api.gui.dialog.showOpenDialog(Api.gui.getCurrentWindow(), { @@ -181,48 +175,113 @@ define(function() { properties: ['openDirectory'] }, function(targetPath) { - if(!targetPath) { + callback(targetPath); + } + ); + }, + + loadingIsClosed: false, + + exportHTMLForNotebook: function (notebookId) { + var me = this; + if (!notebookId) { + return; + } + me.getTargetPath(function(targetPath) { + if (!targetPath) { + return; + } + + me.loadingIsClosed = false; + Api.loading.show(Api.getMsg('plugin.export_html.Exporting'), + { + hasProgress: true, + isLarge: true, + onClose: function () { + me.loadingIsClosed = true; + setTimeout(function() { + me.hideLoading(); + }); + }}); + Api.loading.setProgress(1); + + Api.noteService.getNotes(notebookId, function(notes) { + if (!notes) { + me.hideLoading(); return; } - me.loadingIsClosed = false; - Api.loading.show(Api.getMsg('plugin.export_html.Exporting'), - { - hasProgress: true, - isLarge: true, - onClose: function () { - me.loadingIsClosed = true; - setTimeout(function() { - Api.loading.hide(); - }); - }}); - Api.loading.setProgress(1); - + var total = notes.length; var i = 0; - var total = noteIds.length; - - async.eachSeries(noteIds, function(noteId, cb) { + async.eachSeries(notes, function(note, cb) { if (me.loadingIsClosed) { cb(); + me.hideLoading(); return; } - - // setTimeout(function () { - i++; Api.loading.setProgress(100 * i / total); - Api.noteService.getNote(noteId, function(note) { - me._exportHTML(note, targetPath, function() { - cb(); - }, i, total); - }); - - // }, i * 1000); - - }, function () { - Api.loading.hide(); + me._exportHTML(note, targetPath, function() { + cb(); + }, i, total); + }, function() { + me.hideLoading(); Notify.show({title: 'Info', body: getMsg('plugin.export_html.exportSuccess')}); }); + }); + }); + }, + + hideLoading: function () { + setTimeout(function () { + Api.loading.hide(); + }, 1000); + }, + + exportHTML: function (noteIds) { + var me = this; + if (!noteIds || noteIds.length == 0) { + return; + } + me.getTargetPath(function(targetPath) { + if (!targetPath) { + return; + } + + me.loadingIsClosed = false; + Api.loading.show(Api.getMsg('plugin.export_html.Exporting'), + { + hasProgress: true, + isLarge: true, + onClose: function () { + me.loadingIsClosed = true; + setTimeout(function() { + me.hideLoading(); + }); + }}); + Api.loading.setProgress(1); + + var i = 0; + var total = noteIds.length; + + async.eachSeries(noteIds, function(noteId, cb) { + if (me.loadingIsClosed) { + cb(); + return; + } + + i++; + Api.loading.setProgress(100 * i / total); + Api.noteService.getNote(noteId, function(note) { + me._exportHTML(note, targetPath, function() { + cb(); + }, i, total); + }); + + }, function () { + me.hideLoading(); + Notify.show({title: 'Info', body: getMsg('plugin.export_html.exportSuccess')}); + }); }); }, @@ -286,6 +345,18 @@ define(function() { })() }; Api.addExportMenu(menu); + + Api.addExportMenuForNotebook({ + label: Api.getMsg('plugin.export_html.export'), + enabled: function(notebookId) { + return true; + }, + click: (function() { + return function(notebookId) { + me.exportHTMLForNotebook(notebookId); + } + })() + }); }, // 打开后 onOpenAfter: function() {