diff --git a/main.js b/main.js index 2e5be588..a26d7495 100644 --- a/main.js +++ b/main.js @@ -231,6 +231,7 @@ function openIt() { killPort(_openIt); } + function _openIt() { // console.log(arguments); // app.getPath('appData'); @@ -294,3 +295,5 @@ function _openIt() { // 作为调试 debug && setMenu(); } + + diff --git a/node_modules/evt.js b/node_modules/evt.js index b8bccdb6..c47781a0 100644 --- a/node_modules/evt.js +++ b/node_modules/evt.js @@ -11,6 +11,9 @@ if(!fs.existsSync(dataBasePath)) { */ // dataBasePath = ''; + +var protocol = require('remote').require('protocol'); + var Evt = { defaultUrl: 'https://leanote.com', @@ -37,10 +40,45 @@ var Evt = { localUrl: 'http://127.0.0.1:8912', dataBasePath: '', + // 是否有这个方法, 就代表是否可以用(含callback) + // https://github.com/atom/electron/commit/7d97bb6fe0a6feef886d927ea894bcb2f3521577 + // 老版本没有这个问题 + canUseProtocol: function () { + // return false; + return protocol.registerFileProtocol; + }, + + getImageLocalUrlPrefix: function () { + if (this.canUseProtocol()) { + return 'leanote://file/getImage'; + } + return this.localUrl + '/api/file/getImage'; + }, + + getAttachLocalUrlPrefix: function () { + if (this.canUseProtocol()) { + return 'leanote://file/getAttach'; + } + return this.localUrl + '/api/file/getAttach'; + }, + + getAllAttachsLocalUrlPrefix: function () { + if (this.canUseProtocol()) { + return 'leanote://file/getAllAttachs'; + } + return this.localUrl + '/api/file/getAllAttachs'; + }, + getImageLocalUrl: function(fileId) { + if (this.canUseProtocol()) { + return 'leanote://file/getImage?fileId=' + fileId; + } return this.localUrl + '/api/file/getImage?fileId=' + fileId; }, getAttachLocalUrl: function(fileId) { + if (this.canUseProtocol()) { + return 'leanote://file/getAttach?fileId=' + fileId; + } return this.localUrl + '/api/file/getAttach?fileId=' + fileId; }, getAllAttachLocalUrl: function(noteId) { diff --git a/node_modules/note.js b/node_modules/note.js index 4bfdf35b..727ce221 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -484,17 +484,18 @@ var Note = { // http://leanote.com/file/outputImage?fileId=54f9079f38f4115c0200001b var reg0 = new RegExp(url + '/file/outputImage', 'g'); - content = content.replace(reg0, Evt.localUrl + '/api/file/getImage'); + content = content.replace(reg0, Evt.getImageLocalUrlPrefix()); var reg = new RegExp(url + '/api/file/getImage', 'g'); - content = content.replace(reg, Evt.localUrl + '/api/file/getImage'); + content = content.replace(reg, Evt.getImageLocalUrlPrefix()); var reg2 = new RegExp(url + '/api/file/getAttach', 'g'); - content = content.replace(reg2, Evt.localUrl + '/api/file/getAttach'); + content = content.replace(reg2, Evt.getAttachLocalUrlPrefix()); + // 无用 // api/file/getAllAttachs?noteId=xxxxxxxxx, 这里的noteId是服务器上的noteId啊 var reg3 = new RegExp(url + '/api/file/getAllAttachs', 'g'); - content = content.replace(reg3, Evt.localUrl + '/api/file/getAllAttachs'); + content = content.replace(reg3, Evt.getAllAttachsLocalUrlPrefix()); return content; }, @@ -507,13 +508,13 @@ var Note = { // console.log(Evt.localUrl + '/api/file/getImage'); // console.log(content); - var reg = new RegExp(Evt.localUrl + '/api/file/getImage', 'g'); + var reg = new RegExp(Evt.getImageLocalUrlPrefix(), 'g'); content = content.replace(reg, Evt.leanoteUrl + '/api/file/getImage'); - var reg2 = new RegExp(Evt.localUrl + '/api/file/getAttach', 'g'); + var reg2 = new RegExp(Evt.getAttachLocalUrlPrefix(), 'g'); content = content.replace(reg2, Evt.leanoteUrl + '/api/file/getAttach'); - var reg3 = new RegExp(Evt.localUrl + '/api/file/getAllAttachs', 'g'); + var reg3 = new RegExp(Evt.getAllAttachsLocalUrlPrefix(), 'g'); content = content.replace(reg3, Evt.leanoteUrl + '/api/file/getAllAttachs'); return content; @@ -1338,6 +1339,7 @@ var Note = { return callback && callback(false); } else { // 每一个笔记得到图片, 附件信息和数据 + note.Content = me.fixContentUrl(note.Content); async.eachSeries(notes, function(note, cb) { me.getNoteFiles(note, function(files) { note.Content = me.fixNoteContentForSend(note.Content); @@ -1349,15 +1351,27 @@ var Note = { }); }); }, function() { + console.log(notes); callback(notes); }); } }); }, + // 历史原因, 支持了protocol, 但url还是有127 + fixContentUrl: function(content) { + if (!content) { + return content; + } + if (Evt.canUseProtocol()) { + return content.replace(/http:\/\/127.0.0.1:8912\/api\//g, 'leanote://'); + } + }, + // 得到笔记的文件 getNoteFiles: function(note, callback) { var noteId = note.NoteId; + // 先处理内容URL var content = note.Content; // 1. 先得到附件 @@ -1373,7 +1387,8 @@ var Note = { // http://localhost:8002/api/file/getImage?fileId=xxxxxx, 得到fileId, 查询数据库, 得到图片 // console.log(content); // console.log(Evt.localUrl + '/api/file/getImage?fileId=([0-9a-zA-Z]{24})'); - var reg = new RegExp(Evt.localUrl + "/api/file/getImage\\?fileId=([0-9a-zA-Z]{24})", 'g'); + // var reg = new RegExp(Evt.localUrl + "/api/file/getImage\\?fileId=([0-9a-zA-Z]{24})", 'g'); + var reg = new RegExp(Evt.getImageLocalUrlPrefix() + "\\?fileId=([0-9a-zA-Z]{24})", 'g'); var fileIds = []; // var fileIdsMap = {}; // 防止多个 while((result = reg.exec(content)) != null) { diff --git a/node_modules/server.js b/node_modules/server.js index 07a8d2e6..b2ca872c 100644 --- a/node_modules/server.js +++ b/node_modules/server.js @@ -7,6 +7,8 @@ var path = require('path'); var fs = require('fs'); var Common = require('common'); +var protocol = require('remote').require('protocol'); + // http server, 处理笔记图片 var Server = { // port: 8008, @@ -39,6 +41,13 @@ var Server = { if(me._started) { return; } + + if (Evt.canUseProtocol()) { + me.initProtocol(); + me._started = true; + return; + } + // return; var basePath = process.cwd(); var server = http.createServer(function (request, response) { @@ -120,10 +129,19 @@ var Server = { }, // 关闭服务 - close: function() { + close: function(callback) { + this._started = false; + + // 注销prototol, 如果频繁刷新, 会报错, calling a released render + if (Evt.canUseProtocol()) { + protocol.unregisterProtocol('leanote', function () { + callback && callback(); + }); + return; + } this.server.close(function(err) { + callback && callback(); }); - console.log('close'); this._started = false; }, @@ -176,6 +194,45 @@ var Server = { return me.e404(res); } }) + }, + + //--------------------- + // 新server + // latest 0.31 series + //--------------------- + + initProtocol: function () { + // 先注销, 为了防止刷新 + // protocol.unregisterProtocol('leanote', function () { + + protocol.registerFileProtocol('leanote', function(request, callback) { + // console.log(request.url); + var url = request.url; + var ret = /fileId=([a-zA-Z0-9]{24})/.exec(url); + if (ret && ret[1]) { + // console.log(ret); + if(!File) { + File = require('file'); + } + File.getImage(ret[1], function(fileLocalPath) { + if(fileLocalPath) { + callback({path: fileLocalPath}); + } else { + callback(); + } + }) + var fileId = ret[1]; + } + // var url = request.url.substr(7); + // callback({path: '/Users/life/Desktop/newicon/blog@2x.png'}); + }, function (error) { + if (error) { + console.error('Failed to register protocol') + console.log(error); + } + }); + // }); + } }; module.exports = Server; \ No newline at end of file diff --git a/public/config.js b/public/config.js index 8003bf53..f5e160d8 100644 --- a/public/config.js +++ b/public/config.js @@ -24,5 +24,5 @@ var Config = { } ], "lang": "en-us", - "theme": "" + "theme": "black" }; \ No newline at end of file diff --git a/public/js/app/note.js b/public/js/app/note.js index 4eb0abf6..4a048dbf 100644 --- a/public/js/app/note.js +++ b/public/js/app/note.js @@ -422,6 +422,10 @@ Note.genAbstract = function(content, len) { return d.innerHTML; }; +Note.fixImageSrc = function(src) { + return fixContentUrl(src); +}; + Note.getImgSrc = function(content) { if(!content) { return ""; @@ -779,9 +783,9 @@ Note.renderChangedNote = function(changedNote) { $thumb = $leftNoteNav.find(".item-thumb"); // 有可能之前没有图片 if($thumb.length > 0) { - $thumb.find("img").attr("src", changedNote.ImgSrc); + $thumb.find("img").attr("src", Note.fixImageSrc(changedNote.ImgSrc)); } else { - $leftNoteNav.append(tt('
', changedNote.ImgSrc)); + $leftNoteNav.append(tt('
', Note.fixImageSrc(changedNote.ImgSrc))); $leftNoteNav.addClass("item-image"); } $leftNoteNav.find(".item-desc").removeAttr("style"); @@ -982,7 +986,7 @@ Note._getNoteHtmlObjct = function(note, isShared) { var tmp; if(note.ImgSrc) { - tmp = tt(Note.itemTpl, classes, this.newNoteSeq(), note.NoteId, note.ImgSrc, note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc); + tmp = tt(Note.itemTpl, classes, this.newNoteSeq(), note.NoteId, Note.fixImageSrc(note.ImgSrc), note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc); } else { tmp = tt(Note.itemTplNoImg, classes, this.newNoteSeq(), note.NoteId, note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc); } @@ -1028,7 +1032,7 @@ Note._renderNotes = function(notes, forNewNote, isShared, tang) { // 第几趟 var tmp; if(note.ImgSrc) { - tmp = tt(Note.itemTpl, classes, i, note.NoteId, note.ImgSrc, note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc || ''); + tmp = tt(Note.itemTpl, classes, i, note.NoteId, Note.fixImageSrc(note.ImgSrc), note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc || ''); } else { tmp = tt(Note.itemTplNoImg, classes, i, note.NoteId, note.Title, Notebook.getNotebookTitle(note.NotebookId), goNowToDatetime(note.UpdatedTime), note.Desc || ''); } diff --git a/public/js/app/service.js b/public/js/app/service.js index 5de11bb2..9a96c6bd 100644 --- a/public/js/app/service.js +++ b/public/js/app/service.js @@ -4,6 +4,7 @@ var Evt = require('evt'); var app = require('remote').require('app'); var basePath = app.getPath('appData') + '/leanote'; // /Users/life/Library/Application Support/Leanote'; // require('nw.gui').App.dataPath; Evt.setDataBasePath(basePath); +var protocol = require('remote').require('protocol'); if(!/login.html/.test(location.href)) { // 启动服务器, 图片 diff --git a/public/js/common.js b/public/js/common.js index 2c907743..000e193f 100644 --- a/public/js/common.js +++ b/public/js/common.js @@ -344,6 +344,14 @@ function switchEditor(isMarkdown) { } } +// 将http://127.0.0.1:8912转为leanote:// +function fixContentUrl(content) { + if (EvtService.canUseProtocol()) { + return content.replace(/http:\/\/127.0.0.1:8912\/api\//g, 'leanote://'); + } + return content; +} + // editor 设置内容 // 可能是tinymce还没有渲染成功 var previewToken = "
FORTOKEN
" @@ -357,6 +365,9 @@ function _setEditorContent(content, isMarkdown, preview, callback) { if(!content) { content = ""; } + + content = fixContentUrl(content); + if(clearIntervalForSetContent) { clearInterval(clearIntervalForSetContent); } @@ -1704,16 +1715,16 @@ var Notify = { var onClose = function(afterFunc) { try { - // 先把服务关掉 - Server.close(); - SyncService.stop(); - - // 先保存之前改变的 - Note.curChangedSaveIt(); - // 保存状态 - State.saveCurState(function() { - afterFunc && afterFunc(); - }); + // 先把服务/协议关掉 + Server.close(function () { + SyncService.stop(); + // 先保存之前改变的 + Note.curChangedSaveIt(); + // 保存状态 + State.saveCurState(function() { + afterFunc && afterFunc(); + }); + }); } catch(e) { afterFunc && afterFunc(); }