From 960d5ba17ab4c3b86ff637abbac3307758b6857d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Sat, 7 Nov 2015 12:32:25 +0100 Subject: [PATCH] Insert image URL into input bar --- js/imgur.js | 15 +++++++++------ js/inputbar.js | 28 +++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/js/imgur.js b/js/imgur.js index be003c8..a004392 100644 --- a/js/imgur.js +++ b/js/imgur.js @@ -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); + } + } } }; diff --git a/js/inputbar.js b/js/inputbar.js index 6610017..09152f5 100644 --- a/js/inputbar.js +++ b/js/inputbar.js @@ -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