From 01f59cecac63d3c36f2ce5805b1fe1c36edfbf53 Mon Sep 17 00:00:00 2001 From: life Date: Fri, 20 Mar 2015 00:35:19 +0800 Subject: [PATCH] img save as & notification --- node_modules/file.js | 70 ++++++++++++++++++++++++++++++++---- note.html | 2 ++ public/css/theme/basic.less | 8 +++++ public/css/theme/simple.css | 7 ++++ public/css/theme/simple.less | 2 +- public/js/app/service.js | 63 +++++++++++++++++++++++++++++++- 6 files changed, 143 insertions(+), 9 deletions(-) diff --git a/node_modules/file.js b/node_modules/file.js index 5d48258b..8d8ed0d2 100644 --- a/node_modules/file.js +++ b/node_modules/file.js @@ -1,5 +1,6 @@ var db = require('db'); var fs = require('fs'); +var needle = require('needle'); var path = require('path'); var Evt = require('evt'); var User = require('user'); @@ -310,12 +311,12 @@ var File = { if(!srcIsExists) { return callback(false, 'File Not Exists'); } - console.log(srcPath); - console.log(toPath); + // console.log(srcPath); + // console.log(toPath); var toIsExists = fs.existsSync(toPath); function cp() { Common.copyFile(srcPath, toPath, function(ok) { - callback(ok); + callback && callback(ok); }); } if(toIsExists) { @@ -323,7 +324,7 @@ var File = { if(!error) { cp(); } else { - callback(false, 'The Target File Cannot Overwrite'); + callback && callback(false, 'The Target File Cannot Overwrite'); } }); } else { @@ -331,8 +332,6 @@ var File = { } }, - - // 附件操作 addAttach: function(filePaths, noteId, callback) { if(!noteId || !filePaths) { @@ -400,7 +399,64 @@ var File = { } } }); - } + }, + + // 下载图片, 本地的, 或外站的 + downloadImg: function(src, callback) { + var me = this; + // 本地的 + if(src.indexOf('http://127.0.0.1') != -1) { + var ret = /fileId=([a-zA-Z0-9]{24})/.exec(src); + if(ret && ret.length == 2) { + var fileId = ret[1]; + me.getImage(fileId, function(filePath) { + callback(filePath); + }); + } else { + callback(); + } + + } else { + // 远程的图片 + needle.get(src, function(err, resp) { + // console.log(resp); + /* + { 'accept-ranges': 'bytes', + 'content-disposition': 'inline; filename="logo.png"', + 'content-length': '8583', + 'content-type': 'image/png', + date: 'Mon, 19 Jan 2015 15:01:47 GMT', + */ + // log(resp.headers); + if(err || !resp || resp.statusCode == 404) { + callback(false); + } else { + // 当图片没有时候还是执行这一步 + + var typeStr = resp.headers['content-type']; + var type = 'png'; + if(typeStr) { + var typeArr = typeStr.split('/'); + if(typeStr.length > 1) { + type = typeArr[1]; + } + } + + var filename = Common.uuid() + '.' + type; + var imagePath = User.getCurUserImagesPath(); + var imagePathAll = imagePath + '/' + filename; + fs.writeFile(imagePathAll, resp.body, function(err) { + if(err) { + callback(false); + } else { + callback(imagePathAll); + } + }); + } + }); + } + + }, }; module.exports = File; \ No newline at end of file diff --git a/note.html b/note.html index ce8f6895..7ee9db3b 100755 --- a/note.html +++ b/note.html @@ -771,6 +771,8 @@ function log(o) { + +

diff --git a/public/css/theme/basic.less b/public/css/theme/basic.less index acd34e92..68b84817 100644 --- a/public/css/theme/basic.less +++ b/public/css/theme/basic.less @@ -1578,6 +1578,9 @@ body { } } +#preview-contents { + -webkit-user-select: text; +} #searchNoteInput, #searchNotebookForList, @@ -1750,5 +1753,10 @@ body { text-align: center; margin: 10px 0; opacity: 0.8; } +img::selection { + background: transparent; + color: #ffffff; +} + @import '../traffic.less'; @import '../ani.less'; diff --git a/public/css/theme/simple.css b/public/css/theme/simple.css index 15132169..bfa0d0f6 100644 --- a/public/css/theme/simple.css +++ b/public/css/theme/simple.css @@ -1424,6 +1424,9 @@ body.init .loading-footer { body.init #pageInner { display: none; } +#preview-contents { + -webkit-user-select: text; +} #leftNotebook { overflow: hidden; border-radius: 5px 0 0 0; @@ -1544,6 +1547,10 @@ body.init #pageInner { margin: 10px 0; opacity: 0.8; } +img::selection { + background: transparent; + color: #ffffff; +} .win-tool { padding: 5px; position: absolute; diff --git a/public/css/theme/simple.less b/public/css/theme/simple.less index 26244564..04fb56dd 100644 --- a/public/css/theme/simple.less +++ b/public/css/theme/simple.less @@ -18,7 +18,7 @@ ::selection { background:@selectionBg; color:@selectionColor; } ::-moz-selection { background:@selectionBg; color:@selectionColor; } ::-webkit-selection { background:@selectionBg; color:@selectionColor; } - + a { color: @aBlackColor; cursor: pointer; diff --git a/public/js/app/service.js b/public/js/app/service.js index a2c5119a..90d45617 100644 --- a/public/js/app/service.js +++ b/public/js/app/service.js @@ -73,6 +73,7 @@ function isMac() { // 窗口大小设置 var win = gui.Window.get(); +var downloadImgPath; $(function() { var isMacP = isMac(); $('.tool-close, .tool-close-blur').click(function() { @@ -97,6 +98,26 @@ $(function() { // 全屏模式 // win.toggleKioskMode(); }); + + // 下载图片输入框 + $('#downloadImgInput').change(function() { + var name = $(this).val(); + $(this).val(''); // 为防止重名不触发 + if(downloadImgPath) { + FileService.download(downloadImgPath, name, function(ok, msg) { + // console.log(ok + ' -=-'); + if(ok) { + new window.Notification('Info', { + body: 'Image saved successful!', + }); + } else { + new window.Notification('Warning', { + body: msg || 'Image saved failure!', + }); + } + }); + } + }); }); // bind close event @@ -125,6 +146,38 @@ function Menu() { document.execCommand('paste'); } }); + + this.saveAs = new gui.MenuItem({ + label: 'Save as', + click: function() { + // document.execCommand("selectAll"); + // document.execCommand('paste'); + var src = $curTarget.attr('src'); + if(!src) { + alert('error'); + } + // 得到图片, 打开dialog + FileService.downloadImg(src, function(curPath) { + if(curPath) { + var paths = curPath.split(/\/|\\/); + var name = paths[paths.length-1]; + downloadImgPath = curPath; + $('#downloadImgInput').attr('nwsaveas', name); + $('#downloadImgInput').click(); + + } else { + // alert会死? + // alert('File not exists'); + // https://github.com/nwjs/nw.js/wiki/Notification + var notification = new window.Notification('Warning', { + body: 'File not exists', + // icon: appIcon + }); + } + }); + } + }); + this.openInBrowser = new gui.MenuItem({ label: 'Open link in browser', click: function() { @@ -138,6 +191,8 @@ function Menu() { this.menu.append(this.copy); this.menu.append(this.paste); this.menu.append(new gui.MenuItem({ type: 'separator' })); + this.menu.append(this.saveAs); + this.menu.append(new gui.MenuItem({ type: 'separator' })); this.menu.append(this.openInBrowser); // You can have submenu! @@ -156,6 +211,9 @@ Menu.prototype.canCopy = function(bool) { Menu.prototype.canPaste = function(bool) { this.paste.enabled = bool; }; +Menu.prototype.canSaveAs = function(bool) { + this.saveAs.enabled = bool; +}; Menu.prototype.canOpenInBroswer = function(bool) { this.openInBrowser.enabled = bool; }; @@ -167,9 +225,11 @@ var FS = require('fs'); // 右键菜单 var winHref = ''; -$('#noteTitle, #searchNoteInput, #searchNotebookForList, #addTagInput, #wmd-input, #editorContent').on('contextmenu', function (e) { +var $curTarget; +$('#noteTitle, #searchNoteInput, #searchNotebookForList, #addTagInput, #wmd-input, #preview-contents, #editorContent').on('contextmenu', function (e) { e.preventDefault(); var $target = $(e.target); + $curTarget = $target; var text = $target.text(); winHref = $target.attr('href'); if(!winHref) { @@ -184,6 +244,7 @@ $('#noteTitle, #searchNoteInput, #searchNotebookForList, #addTagInput, #wmd-inpu } menu.canOpenInBroswer(!!winHref); + menu.canSaveAs($target.is('img') && $target.attr('src')); var selectionType = window.getSelection().type.toUpperCase(); // var clipData = gui.Clipboard.get().get(); // menu.canPaste(clipData.length > 0);