Insert image URL into input bar

This commit is contained in:
Lorenz Hübschle-Schneider 2015-11-07 12:32:25 +01:00
parent aef26a2dc5
commit 960d5ba17a
2 changed files with 36 additions and 7 deletions

View file

@ -5,7 +5,7 @@ var weechat = angular.module('weechat');
weechat.factory('imgur', ['$rootScope', function($rootScope) {
var process = function(image) {
var process = function(image, callback) {
// Is it an image?
if (!image || !image.type.match(/image.*/)) return;
@ -16,7 +16,7 @@ weechat.factory('imgur', ['$rootScope', function($rootScope) {
// When image is read
reader.onload = function (event) {
var image = event.target.result.split(',')[1];
upload(image);
upload(image, callback);
};
// Read image as data url
@ -25,7 +25,7 @@ weechat.factory('imgur', ['$rootScope', function($rootScope) {
};
// Upload image to imgur from base64
var upload = function( base64img ) {
var upload = function( base64img, callback ) {
// Set client ID (Glowing Bear)
var clientId = "164efef8979cd4b";
@ -54,9 +54,12 @@ weechat.factory('imgur', ['$rootScope', function($rootScope) {
// Get response text
var response = JSON.parse(xhttp.responseText);
// Open image in new window
window.open(response.data.link, '_blank');
// Send link as message
if( response.data && response.data.link ) {
if (callback && typeof(callback) === "function") {
callback(response.data.link);
}
}
}
};

View file

@ -75,7 +75,33 @@ weechat.directive('inputBar', function() {
var file = files[0];
// Process image
imgur.process(file);
imgur.process(file, function(imageUrl) {
// Send image
if(imageUrl !== undefined && imageUrl !== '') {
// caret position in the input bar
var inputNode = $scope.getInputNode(),
caretPos = inputNode.selectionStart;
var prefix = $scope.command.substring(0, caretPos),
suffix = $scope.command.substring(caretPos, $scope.command.length);
// Add spaces if missing
if (prefix.length > 0 && prefix[prefix.length - 1] !== ' ') {
prefix += ' ';
}
if (suffix.length > 0 && suffix[0] !== ' ') {
suffix = ' '.concat(suffix);
}
$scope.command = prefix + String(imageUrl) + suffix;
setTimeout(function() {
inputNode.focus();
var pos = $scope.command.length - suffix.length;
inputNode.setSelectionRange(pos, pos);
}, 0);
}
});
};
// Send the message to the websocket