From bde4c1601a8c09d3d2328a7dfed0549b6b90b930 Mon Sep 17 00:00:00 2001 From: life Date: Mon, 21 Dec 2015 23:38:37 +0800 Subject: [PATCH] export pdf local ok --- public/config.js | 1 - public/plugins/export_pdf/plugin.js | 391 +++++++++++++----- .../{export_pdf2 => export_pdf}/tpl/html.tpl | 0 .../tpl/markdown.tpl | 0 public/plugins/export_pdf2/plugin.js | 361 ---------------- public/plugins/export_pdf_bak/plugin.js | 155 +++++++ .../plugin.json | 0 7 files changed, 453 insertions(+), 455 deletions(-) rename public/plugins/{export_pdf2 => export_pdf}/tpl/html.tpl (100%) rename public/plugins/{export_pdf2 => export_pdf}/tpl/markdown.tpl (100%) delete mode 100644 public/plugins/export_pdf2/plugin.js create mode 100644 public/plugins/export_pdf_bak/plugin.js rename public/plugins/{export_pdf => export_pdf_bak}/plugin.json (100%) diff --git a/public/config.js b/public/config.js index 2c89e5b9..581076ab 100644 --- a/public/config.js +++ b/public/config.js @@ -4,7 +4,6 @@ var Config = { "import_leanote", "import_evernote", "export_pdf", - "export_pdf2", "export_html", "export_leanote", "export_evernote", diff --git a/public/plugins/export_pdf/plugin.js b/public/plugins/export_pdf/plugin.js index 2de5a663..a6b194bd 100644 --- a/public/plugins/export_pdf/plugin.js +++ b/public/plugins/export_pdf/plugin.js @@ -1,112 +1,318 @@ /** - * 导出插件 + * 导出PDF插件 + * @author life life@leanote.com + * 选择目录, 将图片保存到文件夹中, 有个html文件(以笔记名命名) + * 注意, fs.existsSync总返回false, readFileSync可用 */ define(function() { - var exportPdf = { + var async; // = require('async'); + + var exportPDF = { langs: { 'en-us': { 'export': 'Export PDF', + 'Exporting': 'Exporting', + 'Exporting: ': 'Exporting: ', 'exportSuccess': 'PDF saved successful!', 'exportFailure': 'PDF saved failure!', - 'notExists': 'Please sync your note to ther server firslty.', - 'localUser': 'Not support for local user' }, 'zh-cn': { 'export': '导出PDF', + 'Exporting': '正在导出', + 'Exporting: ': '正在导出: ', 'exportSuccess': 'PDF导出成功!', - 'exportFailure': 'PDF导出失败!', - 'notExists': '请先同步该笔记!', - 'localUser': '本地用户不支持导出PDF' + 'exportFailure': 'PDF导出失败!' }, 'zh-hk': { 'export': '導出PDF', + 'Exporting': '正在導出', + 'Exporting: ': '正在導出: ', 'exportSuccess': 'PDF導出成功!', - 'exportFailure': 'PDF導出失敗!', - 'notExists': '請先同步該筆記!', - 'localUser': '本地用戶不支持導出PDF' + 'exportFailure': 'PDF導出失敗!' } }, _inited: false, - _input: null, - init: function() { var me = this; - - me._input = $(''); - $('#hiddenZone').append(me._input); - // 下载pdf输入框 - me._input.change(function() { - - }); - + if (me._inited) { + return; + } + async = require('async'); me._inited = true; + me._initExportPdf(); }, - exportPDF: function(note) { + // 调用main导出pdf + _exportPdfSeq: 1, + _exportPdfCallback: {}, + _initExportPdf: function () { + var me = this; + Api.ipc.on('export-pdf-ret', function(arg) { + var seq = arg.seq; + // console.log('export-pdf-ret'); + // console.log(arg); + // console.log(me._exportPdfCallback[seq]); + if (me._exportPdfCallback[seq]) { + me._exportPdfCallback[seq](arg); + } + }); + }, + exportPdf: function (htmlPath, targetPdfPath, isMarkdown, callback) { + this._exportPdfSeq++; + this._exportPdfCallback[this._exportPdfSeq] = callback; + Api.ipc.send('export-pdf', + {htmlPath: htmlPath, targetPdfPath: targetPdfPath, isMarkdown: isMarkdown, seq: this._exportPdfSeq}); + }, + + getPluginPath: function() { + return Api.evtService.getProjectBasePath() + '/public/plugins/export_pdf' ; + }, + + htmlTpl: '', + markdownTpl: '', + getTpl: function(isMarkdown) { + var tpl = isMarkdown ? this.markdownTpl : this.htmlTpl; + if(tpl) { + return tpl; + } + var basePluginPath = this.getPluginPath(); + + var tplName = isMarkdown ? 'markdown' : 'html'; + var tplPath = basePluginPath + '/tpl/' + tplName + '.tpl'; + tpl = Api.nodeFs.readFileSync(tplPath, 'utf-8'); + isMarkdown ? (this.markdownTpl = tpl) : (this.htmlTpl = tpl); + return tpl; + }, + // 生成html或markdown + render: function(note) { + var tpl = this.getTpl(note.IsMarkdown); + var title = note.Title || getMsg('Untitled'); + tpl = tpl.replace(/\{title\}/g, title); + tpl = tpl.replace("{content}", function () { + return note.Content; // 为什么要这样? 因为 $$ 替换后变成了一个!! + }); + return tpl; + }, + + replaceAll: function(src, pattern, to) { + if(!src) { + return src; + } + while(true) { + var oldSrc = src; + src = src.replace(pattern, to); + if(oldSrc === src) { + return src; + } + } + }, + + fixFilename: function(filename) { + var reg = new RegExp("/|#|\\$|!|\\^|\\*|'| |\"|%|&|\\(|\\)|\\+|\\,|/|:|;|<|>|=|\\?|@|\\||\\\\", 'g'); + filename = filename.replace(reg, "-"); + // 防止出现两个连续的- + while(filename.indexOf('--') != -1) { + filename = this.replaceAll(filename, '--', '-'); + } + if (filename.length > 1) { + // 最后一个- + filename = filename.replace(/\-$/, ''); + } + return filename; + }, + + // 得到可用的文件名, 避免冲突 + getPdfFilePath: function(pathInfo, n, cb) { + var me = this; + if(n > 1) { + pathInfo.nameNotExt = pathInfo.nameNotExtRaw + '-' + n; + } + var absPath = pathInfo.getFullPath(); + + // Api.nodeFs.existsSync(absPath) 总是返回false, 不知道什么原因 + // 在控制台上是可以的 + Api.nodeFs.exists(absPath, function(exists) { + if(!exists) { + cb(absPath); + } + else { + me.getPdfFilePath(pathInfo, n+1, cb); + } + }); + }, + + getTargetPath: function(callback) { + // showSaveDialog 不支持property选择文件夹 + Api.gui.dialog.showOpenDialog(Api.gui.getCurrentWindow(), + { + defaultPath: Api.gui.app.getPath('userDesktop') + '/', + properties: ['openDirectory'] + }, + function(targetPath) { + callback(targetPath); + } + ); + }, + + loadingIsClosed: false, + + exportPDFForNotebook: 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_pdf.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; + } + + var total = notes.length; + var i = 0; + async.eachSeries(notes, function(note, cb) { + if (me.loadingIsClosed) { + cb(); + me.hideLoading(); + return; + } + i++; + Api.loading.setProgress(100 * i / total); + me._exportPDF(note, targetPath, function() { + cb(); + }, i, total); + }, function() { + me.hideLoading(); + Notify.show({title: 'Info', body: getMsg('plugin.export_pdf.exportSuccess')}); + }); + }); + }); + }, + + hideLoading: function () { + setTimeout(function () { + Api.loading.hide(); + }, 1000); + }, + + exportPDF: 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_pdf.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._exportPDF(note, targetPath, function(ok) { + cb(); + }, i, total); + }); + + }, function () { + me.hideLoading(); + Notify.show({title: 'Info', body: getMsg('plugin.export_pdf.exportSuccess')}); + }); + }); + }, + + _exportPDF: function(note, path, callback, i, total) { + var me = this; + setTimeout(function () { + me._exportPDF(note, path, callback, i, total); + }, 1000); + }, + + _exportPDF: function(note, path, callback, i, total) { var me = this; if(!note) { return; } - var name = note.Title ? note.Title + '.pdf' : getMsg('Untitled') + '.pdf'; - - window.downloadPdfPath = false; - if(!me._inited) { - me.init(); + if (me.loadingIsClosed) { + callback(); + return; } - var closed = false; - var interval; - Api.loading.show('', {hasProgress: true, onClose: function () { - closed = true; - clearInterval(interval); - }}); - Api.loading.setProgress(1); - var progress = 1; - interval = setInterval(function () { - progress += 5; - if (progress > 90) { - progress = 90; + setTimeout(function () { + Api.loading.setMsg(Api.getMsg('plugin.export_pdf.Exporting: ') + (note.Title || getMsg('Untitled'))); + Api.loading.setProgressRate(i + '/' + total); + }, 100); + + var name = note.Title ? note.Title + '.pdf' : getMsg('Untitled') + '.pdf'; + name = me.fixFilename(name); + + var targetPath = path + '/' + name; + + // 将路径和名字区分开 + var pathInfo = Api.commonService.splitFile(targetPath); + pathInfo.nameNotExt = me.fixFilename(pathInfo.nameNotExt); // 重新修正一次 + var nameNotExt = pathInfo.nameNotExt; + pathInfo.nameNotExtRaw = pathInfo.nameNotExt; + + // 得到可用文件的绝对路径 + me.getPdfFilePath(pathInfo, 1, function(absPdfFilePath) { + // 得到存放assets的目录 + var html = me.render(note); + var tempPath = Api.gui.app.getPath('temp'); + var last = tempPath[tempPath.length-1]; + if ( last == '/' || last == '\\') { + tempPath = tempPath.substr(0, tempPath.length - 1); } - Api.loading.setProgress(progress); - }, 500); + var targetHtmlPath = tempPath + '/' + (new Date().getTime()) + '.html'; - // 保存 - Api.noteService.exportPdf(note.NoteId, function(curPath, filename, msg) { - clearInterval(interval); - if (closed) { - return; - } - Api.loading.setProgress(99); - Api.loading.hide(); - - setTimeout(function() { - if(curPath) { - me.downloadPdfPath = curPath; - - Api.gui.dialog.showSaveDialog(Api.gui.getCurrentWindow(), {title: name, defaultPath: name}, function(targetPath) { - if(targetPath && me.downloadPdfPath) { - Api.fileService.download(me.downloadPdfPath, targetPath, function(ok, msg) { - if(ok) { - Notify.show({title: 'Info', body: getMsg('plugin.export_pdf.exportSuccess')}); - } else { - Notify.show({type: 'warning', title: 'Warning', body: getMsg('plugin.export_pdf.exportFailure')}); - } - }); - } - }); - - } else { - var m = ""; - if(msg == "noteNotExists") { - m = getMsg('plugin.export_pdf.notExists'); - } - - Notify.show({type: 'warning', title: 'Warning', body: getMsg('plugin.export_pdf.exportFailure') + m}); - } - }, 100); - }); + // 把html文件写到tmp目录 + Api.commonService.writeFile(targetHtmlPath, html); + me.exportPdf(targetHtmlPath, absPdfFilePath, note.IsMarkdown, function (args) { + // console.log('export pdf ret'); + callback(args.ok); + }); + }); }, // 打开前要执行的 @@ -117,30 +323,29 @@ define(function() { var menu = { label: Api.getMsg('plugin.export_pdf.export'), enabled: function(noteIds) { - if(UserInfo.IsLocal) { - return false; - } - if (noteIds && noteIds.length == 1) { - return true; - } - return false; + return true; }, click: (function() { return function(noteIds) { - if (UserInfo.IsLocal) { - Notify.show({type: 'warning', title: 'Warning', body: getMsg('plugin.export_pdf.localUser')}); - return; - } - if (!noteIds || noteIds.length > 1) { - return; - } - Api.noteService.getNote(noteIds[0], function(note) { - me.exportPDF(note); - }); + me.init(); + me.exportPDF(noteIds); } })() }; Api.addExportMenu(menu); + + Api.addExportMenuForNotebook({ + label: Api.getMsg('plugin.export_pdf.export'), + enabled: function(notebookId) { + return true; + }, + click: (function() { + return function(notebookId) { + me.init(); + me.exportPDFForNotebook(notebookId); + } + })() + }); }, // 打开后 onOpenAfter: function() { @@ -150,6 +355,6 @@ define(function() { } }; - return exportPdf; + return exportPDF; }); diff --git a/public/plugins/export_pdf2/tpl/html.tpl b/public/plugins/export_pdf/tpl/html.tpl similarity index 100% rename from public/plugins/export_pdf2/tpl/html.tpl rename to public/plugins/export_pdf/tpl/html.tpl diff --git a/public/plugins/export_pdf2/tpl/markdown.tpl b/public/plugins/export_pdf/tpl/markdown.tpl similarity index 100% rename from public/plugins/export_pdf2/tpl/markdown.tpl rename to public/plugins/export_pdf/tpl/markdown.tpl diff --git a/public/plugins/export_pdf2/plugin.js b/public/plugins/export_pdf2/plugin.js deleted file mode 100644 index 5b86bc52..00000000 --- a/public/plugins/export_pdf2/plugin.js +++ /dev/null @@ -1,361 +0,0 @@ -/** - * 导出PDF插件2 - * @author life life@leanote.com - * 选择目录, 将图片保存到文件夹中, 有个html文件(以笔记名命名) - * 注意, fs.existsSync总返回false, readFileSync可用 - */ -define(function() { - var async; // = require('async'); - - var exportPDF = { - langs: { - 'en-us': { - 'export': 'Export PDF2', - 'Exporting': 'Exporting', - 'Exporting: ': 'Exporting: ', - 'exportSuccess': 'PDF saved successful!', - 'exportFailure': 'PDF saved failure!', - 'notExists': 'Please sync your note to ther server firslty.' - }, - 'zh-cn': { - 'export': '导出PDF2', - 'Exporting': '正在导出', - 'Exporting: ': '正在导出: ', - 'exportSuccess': 'PDF导出成功!', - 'exportFailure': 'PDF导出失败!' - }, - 'zh-hk': { - 'export': '導出PDF2', - 'Exporting': '正在導出', - 'Exporting: ': '正在導出: ', - 'exportSuccess': 'PDF導出成功!', - 'exportFailure': 'PDF導出失敗!' - } - }, - - _inited: false, - init: function() { - var me = this; - if (me._inited) { - return; - } - async = require('async'); - me._inited = true; - me._initExportPdf(); - }, - - // 调用main导出pdf - _exportPdfSeq: 1, - _exportPdfCallback: {}, - _initExportPdf: function () { - var me = this; - Api.ipc.on('export-pdf-ret', function(arg) { - var seq = arg.seq; - // console.log('export-pdf-ret'); - // console.log(arg); - // console.log(me._exportPdfCallback[seq]); - if (me._exportPdfCallback[seq]) { - me._exportPdfCallback[seq](arg); - } - }); - }, - exportPdf: function (htmlPath, targetPdfPath, isMarkdown, callback) { - this._exportPdfSeq++; - this._exportPdfCallback[this._exportPdfSeq] = callback; - Api.ipc.send('export-pdf', - {htmlPath: htmlPath, targetPdfPath: targetPdfPath, isMarkdown: isMarkdown, seq: this._exportPdfSeq}); - }, - - getPluginPath: function() { - return Api.evtService.getProjectBasePath() + '/public/plugins/export_pdf2' ; - }, - - htmlTpl: '', - markdownTpl: '', - getTpl: function(isMarkdown) { - var tpl = isMarkdown ? this.markdownTpl : this.htmlTpl; - if(tpl) { - return tpl; - } - var basePluginPath = this.getPluginPath(); - - var tplName = isMarkdown ? 'markdown' : 'html'; - var tplPath = basePluginPath + '/tpl/' + tplName + '.tpl'; - tpl = Api.nodeFs.readFileSync(tplPath, 'utf-8'); - isMarkdown ? (this.markdownTpl = tpl) : (this.htmlTpl = tpl); - return tpl; - }, - // 生成html或markdown - render: function(note) { - var tpl = this.getTpl(note.IsMarkdown); - var title = note.Title || getMsg('Untitled'); - tpl = tpl.replace(/\{title\}/g, title); - tpl = tpl.replace("{content}", function () { - return note.Content; // 为什么要这样? 因为 $$ 替换后变成了一个!! - }); - return tpl; - }, - - replaceAll: function(src, pattern, to) { - if(!src) { - return src; - } - while(true) { - var oldSrc = src; - src = src.replace(pattern, to); - if(oldSrc === src) { - return src; - } - } - }, - - fixFilename: function(filename) { - var reg = new RegExp("/|#|\\$|!|\\^|\\*|'| |\"|%|&|\\(|\\)|\\+|\\,|/|:|;|<|>|=|\\?|@|\\||\\\\", 'g'); - filename = filename.replace(reg, "-"); - // 防止出现两个连续的- - while(filename.indexOf('--') != -1) { - filename = this.replaceAll(filename, '--', '-'); - } - if (filename.length > 1) { - // 最后一个- - filename = filename.replace(/\-$/, ''); - } - return filename; - }, - - // 得到可用的文件名, 避免冲突 - getPdfFilePath: function(pathInfo, n, cb) { - var me = this; - if(n > 1) { - pathInfo.nameNotExt = pathInfo.nameNotExtRaw + '-' + n; - } - var absPath = pathInfo.getFullPath(); - - // Api.nodeFs.existsSync(absPath) 总是返回false, 不知道什么原因 - // 在控制台上是可以的 - Api.nodeFs.exists(absPath, function(exists) { - if(!exists) { - cb(absPath); - } - else { - me.getPdfFilePath(pathInfo, n+1, cb); - } - }); - }, - - getTargetPath: function(callback) { - // showSaveDialog 不支持property选择文件夹 - Api.gui.dialog.showOpenDialog(Api.gui.getCurrentWindow(), - { - defaultPath: Api.gui.app.getPath('userDesktop') + '/', - properties: ['openDirectory'] - }, - function(targetPath) { - callback(targetPath); - } - ); - }, - - loadingIsClosed: false, - - exportPDFForNotebook: 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_pdf2.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; - } - - var total = notes.length; - var i = 0; - async.eachSeries(notes, function(note, cb) { - if (me.loadingIsClosed) { - cb(); - me.hideLoading(); - return; - } - i++; - Api.loading.setProgress(100 * i / total); - me._exportPDF(note, targetPath, function() { - cb(); - }, i, total); - }, function() { - me.hideLoading(); - Notify.show({title: 'Info', body: getMsg('plugin.export_pdf2.exportSuccess')}); - }); - }); - }); - }, - - hideLoading: function () { - setTimeout(function () { - Api.loading.hide(); - }, 1000); - }, - - exportPDF: 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_pdf2.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._exportPDF(note, targetPath, function(ok) { - cb(); - }, i, total); - }); - - }, function () { - me.hideLoading(); - Notify.show({title: 'Info', body: getMsg('plugin.export_pdf2.exportSuccess')}); - }); - }); - }, - - _exportPDF: function(note, path, callback, i, total) { - var me = this; - setTimeout(function () { - me._exportPDF2(note, path, callback, i, total); - }, 1000); - }, - - _exportPDF2: function(note, path, callback, i, total) { - var me = this; - if(!note) { - return; - } - - if (me.loadingIsClosed) { - callback(); - return; - } - - setTimeout(function () { - Api.loading.setMsg(Api.getMsg('plugin.export_pdf2.Exporting: ') + (note.Title || getMsg('Untitled'))); - Api.loading.setProgressRate(i + '/' + total); - }, 100); - - var name = note.Title ? note.Title + '.pdf' : getMsg('Untitled') + '.pdf'; - name = me.fixFilename(name); - - var targetPath = path + '/' + name; - - // 将路径和名字区分开 - var pathInfo = Api.commonService.splitFile(targetPath); - pathInfo.nameNotExt = me.fixFilename(pathInfo.nameNotExt); // 重新修正一次 - var nameNotExt = pathInfo.nameNotExt; - pathInfo.nameNotExtRaw = pathInfo.nameNotExt; - - // 得到可用文件的绝对路径 - me.getPdfFilePath(pathInfo, 1, function(absPdfFilePath) { - // 得到存放assets的目录 - var html = me.render(note); - var tempPath = Api.gui.app.getPath('temp'); - var last = tempPath[tempPath.length-1]; - if ( last == '/' || last == '\\') { - tempPath = tempPath.substr(0, tempPath.length - 1); - } - var targetHtmlPath = tempPath + '/' + (new Date().getTime()) + '.html'; - - // 把html文件写到tmp目录 - Api.commonService.writeFile(targetHtmlPath, html); - me.exportPdf(targetHtmlPath, absPdfFilePath, note.IsMarkdown, function (args) { - // console.log('export pdf ret'); - callback(args.ok); - }); - }); - }, - - // 打开前要执行的 - onOpen: function() { - var me = this; - var gui = Api.gui; - - var menu = { - label: Api.getMsg('plugin.export_pdf2.export'), - enabled: function(noteIds) { - return true; - }, - click: (function() { - return function(noteIds) { - me.init(); - me.exportPDF(noteIds); - } - })() - }; - Api.addExportMenu(menu); - - Api.addExportMenuForNotebook({ - label: Api.getMsg('plugin.export_pdf2.export'), - enabled: function(notebookId) { - return true; - }, - click: (function() { - return function(notebookId) { - me.init(); - me.exportPDFForNotebook(notebookId); - } - })() - }); - }, - // 打开后 - onOpenAfter: function() { - }, - // 关闭时需要运行的 - onClose: function() { - } - }; - - return exportPDF; - -}); diff --git a/public/plugins/export_pdf_bak/plugin.js b/public/plugins/export_pdf_bak/plugin.js new file mode 100644 index 00000000..2de5a663 --- /dev/null +++ b/public/plugins/export_pdf_bak/plugin.js @@ -0,0 +1,155 @@ +/** + * 导出插件 + */ +define(function() { + var exportPdf = { + langs: { + 'en-us': { + 'export': 'Export PDF', + 'exportSuccess': 'PDF saved successful!', + 'exportFailure': 'PDF saved failure!', + 'notExists': 'Please sync your note to ther server firslty.', + 'localUser': 'Not support for local user' + }, + 'zh-cn': { + 'export': '导出PDF', + 'exportSuccess': 'PDF导出成功!', + 'exportFailure': 'PDF导出失败!', + 'notExists': '请先同步该笔记!', + 'localUser': '本地用户不支持导出PDF' + }, + 'zh-hk': { + 'export': '導出PDF', + 'exportSuccess': 'PDF導出成功!', + 'exportFailure': 'PDF導出失敗!', + 'notExists': '請先同步該筆記!', + 'localUser': '本地用戶不支持導出PDF' + } + }, + + _inited: false, + _input: null, + + init: function() { + var me = this; + + me._input = $(''); + $('#hiddenZone').append(me._input); + // 下载pdf输入框 + me._input.change(function() { + + }); + + me._inited = true; + }, + + exportPDF: function(note) { + var me = this; + if(!note) { + return; + } + + var name = note.Title ? note.Title + '.pdf' : getMsg('Untitled') + '.pdf'; + + window.downloadPdfPath = false; + if(!me._inited) { + me.init(); + } + + var closed = false; + var interval; + Api.loading.show('', {hasProgress: true, onClose: function () { + closed = true; + clearInterval(interval); + }}); + Api.loading.setProgress(1); + var progress = 1; + interval = setInterval(function () { + progress += 5; + if (progress > 90) { + progress = 90; + } + Api.loading.setProgress(progress); + }, 500); + + // 保存 + Api.noteService.exportPdf(note.NoteId, function(curPath, filename, msg) { + clearInterval(interval); + if (closed) { + return; + } + Api.loading.setProgress(99); + Api.loading.hide(); + + setTimeout(function() { + if(curPath) { + me.downloadPdfPath = curPath; + + Api.gui.dialog.showSaveDialog(Api.gui.getCurrentWindow(), {title: name, defaultPath: name}, function(targetPath) { + if(targetPath && me.downloadPdfPath) { + Api.fileService.download(me.downloadPdfPath, targetPath, function(ok, msg) { + if(ok) { + Notify.show({title: 'Info', body: getMsg('plugin.export_pdf.exportSuccess')}); + } else { + Notify.show({type: 'warning', title: 'Warning', body: getMsg('plugin.export_pdf.exportFailure')}); + } + }); + } + }); + + } else { + var m = ""; + if(msg == "noteNotExists") { + m = getMsg('plugin.export_pdf.notExists'); + } + + Notify.show({type: 'warning', title: 'Warning', body: getMsg('plugin.export_pdf.exportFailure') + m}); + } + }, 100); + }); + }, + + // 打开前要执行的 + onOpen: function() { + var me = this; + var gui = Api.gui; + + var menu = { + label: Api.getMsg('plugin.export_pdf.export'), + enabled: function(noteIds) { + if(UserInfo.IsLocal) { + return false; + } + if (noteIds && noteIds.length == 1) { + return true; + } + return false; + }, + click: (function() { + return function(noteIds) { + if (UserInfo.IsLocal) { + Notify.show({type: 'warning', title: 'Warning', body: getMsg('plugin.export_pdf.localUser')}); + return; + } + if (!noteIds || noteIds.length > 1) { + return; + } + Api.noteService.getNote(noteIds[0], function(note) { + me.exportPDF(note); + }); + } + })() + }; + Api.addExportMenu(menu); + }, + // 打开后 + onOpenAfter: function() { + }, + // 关闭时需要运行的 + onClose: function() { + } + }; + + return exportPdf; + +}); diff --git a/public/plugins/export_pdf/plugin.json b/public/plugins/export_pdf_bak/plugin.json similarity index 100% rename from public/plugins/export_pdf/plugin.json rename to public/plugins/export_pdf_bak/plugin.json