mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-17 16:45:21 +00:00
export pdf local ok
This commit is contained in:
@@ -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 = $('<input id="exportPdf" type="file" nwsaveas="" style=""/>');
|
||||
$('#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;
|
||||
|
||||
});
|
||||
|
Reference in New Issue
Block a user