Insert image URL into input bar

paste-quickfix
Lorenz Hübschle-Schneider 10 years ago
parent aef26a2dc5
commit 960d5ba17a
  1. 15
      js/imgur.js
  2. 28
      js/inputbar.js

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

@ -75,7 +75,33 @@ weechat.directive('inputBar', function() {
var file = files[0]; var file = files[0];
// Process image // 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 // Send the message to the websocket

Loading…
Cancel
Save