img save as & notification

This commit is contained in:
life
2015-03-20 00:35:19 +08:00
parent c27e92d681
commit 01f59cecac
6 changed files with 143 additions and 9 deletions

68
node_modules/file.js generated vendored
View File

@@ -1,5 +1,6 @@
var db = require('db'); var db = require('db');
var fs = require('fs'); var fs = require('fs');
var needle = require('needle');
var path = require('path'); var path = require('path');
var Evt = require('evt'); var Evt = require('evt');
var User = require('user'); var User = require('user');
@@ -310,12 +311,12 @@ var File = {
if(!srcIsExists) { if(!srcIsExists) {
return callback(false, 'File Not Exists'); return callback(false, 'File Not Exists');
} }
console.log(srcPath); // console.log(srcPath);
console.log(toPath); // console.log(toPath);
var toIsExists = fs.existsSync(toPath); var toIsExists = fs.existsSync(toPath);
function cp() { function cp() {
Common.copyFile(srcPath, toPath, function(ok) { Common.copyFile(srcPath, toPath, function(ok) {
callback(ok); callback && callback(ok);
}); });
} }
if(toIsExists) { if(toIsExists) {
@@ -323,7 +324,7 @@ var File = {
if(!error) { if(!error) {
cp(); cp();
} else { } else {
callback(false, 'The Target File Cannot Overwrite'); callback && callback(false, 'The Target File Cannot Overwrite');
} }
}); });
} else { } else {
@@ -331,8 +332,6 @@ var File = {
} }
}, },
// 附件操作 // 附件操作
addAttach: function(filePaths, noteId, callback) { addAttach: function(filePaths, noteId, callback) {
if(!noteId || !filePaths) { 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; module.exports = File;

View File

@@ -771,6 +771,8 @@ function log(o) {
<!-- 为了上传图片 --> <!-- 为了上传图片 -->
<input id="chooseImageInput" type="file" name="file" multiple style="display: none"/> <input id="chooseImageInput" type="file" name="file" multiple style="display: none"/>
<input id="downloadImgInput" type="file" nwsaveas="" style=""/>
<!-- contextTips --> <!-- contextTips -->
<div class="context-tips" id="conflictTips"> <div class="context-tips" id="conflictTips">
<p> <p>

View File

@@ -1578,6 +1578,9 @@ body {
} }
} }
#preview-contents {
-webkit-user-select: text;
}
#searchNoteInput, #searchNoteInput,
#searchNotebookForList, #searchNotebookForList,
@@ -1750,5 +1753,10 @@ body {
text-align: center; margin: 10px 0; opacity: 0.8; text-align: center; margin: 10px 0; opacity: 0.8;
} }
img::selection {
background: transparent;
color: #ffffff;
}
@import '../traffic.less'; @import '../traffic.less';
@import '../ani.less'; @import '../ani.less';

View File

@@ -1424,6 +1424,9 @@ body.init .loading-footer {
body.init #pageInner { body.init #pageInner {
display: none; display: none;
} }
#preview-contents {
-webkit-user-select: text;
}
#leftNotebook { #leftNotebook {
overflow: hidden; overflow: hidden;
border-radius: 5px 0 0 0; border-radius: 5px 0 0 0;
@@ -1544,6 +1547,10 @@ body.init #pageInner {
margin: 10px 0; margin: 10px 0;
opacity: 0.8; opacity: 0.8;
} }
img::selection {
background: transparent;
color: #ffffff;
}
.win-tool { .win-tool {
padding: 5px; padding: 5px;
position: absolute; position: absolute;

View File

@@ -73,6 +73,7 @@ function isMac() {
// 窗口大小设置 // 窗口大小设置
var win = gui.Window.get(); var win = gui.Window.get();
var downloadImgPath;
$(function() { $(function() {
var isMacP = isMac(); var isMacP = isMac();
$('.tool-close, .tool-close-blur').click(function() { $('.tool-close, .tool-close-blur').click(function() {
@@ -97,6 +98,26 @@ $(function() {
// 全屏模式 // 全屏模式
// win.toggleKioskMode(); // 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 // bind close event
@@ -125,6 +146,38 @@ function Menu() {
document.execCommand('paste'); 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({ this.openInBrowser = new gui.MenuItem({
label: 'Open link in browser', label: 'Open link in browser',
click: function() { click: function() {
@@ -138,6 +191,8 @@ function Menu() {
this.menu.append(this.copy); this.menu.append(this.copy);
this.menu.append(this.paste); this.menu.append(this.paste);
this.menu.append(new gui.MenuItem({ type: 'separator' })); 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); this.menu.append(this.openInBrowser);
// You can have submenu! // You can have submenu!
@@ -156,6 +211,9 @@ Menu.prototype.canCopy = function(bool) {
Menu.prototype.canPaste = function(bool) { Menu.prototype.canPaste = function(bool) {
this.paste.enabled = bool; this.paste.enabled = bool;
}; };
Menu.prototype.canSaveAs = function(bool) {
this.saveAs.enabled = bool;
};
Menu.prototype.canOpenInBroswer = function(bool) { Menu.prototype.canOpenInBroswer = function(bool) {
this.openInBrowser.enabled = bool; this.openInBrowser.enabled = bool;
}; };
@@ -167,9 +225,11 @@ var FS = require('fs');
// 右键菜单 // 右键菜单
var winHref = ''; 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(); e.preventDefault();
var $target = $(e.target); var $target = $(e.target);
$curTarget = $target;
var text = $target.text(); var text = $target.text();
winHref = $target.attr('href'); winHref = $target.attr('href');
if(!winHref) { if(!winHref) {
@@ -184,6 +244,7 @@ $('#noteTitle, #searchNoteInput, #searchNotebookForList, #addTagInput, #wmd-inpu
} }
menu.canOpenInBroswer(!!winHref); menu.canOpenInBroswer(!!winHref);
menu.canSaveAs($target.is('img') && $target.attr('src'));
var selectionType = window.getSelection().type.toUpperCase(); var selectionType = window.getSelection().type.toUpperCase();
// var clipData = gui.Clipboard.get().get(); // var clipData = gui.Clipboard.get().get();
// menu.canPaste(clipData.length > 0); // menu.canPaste(clipData.length > 0);