From a6c2e6f3872f87eb0abd9d8b595aa98f74c67ff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lorenz=20H=C3=BCbschle-Schneider?= Date: Tue, 10 Nov 2015 19:40:30 +0100 Subject: [PATCH] Insert URL at caret when dropping image into GB unfortunately this pollutes the root scope a bit more --- js/imgur-drop-directive.js | 4 +-- js/inputbar.js | 50 ++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/js/imgur-drop-directive.js b/js/imgur-drop-directive.js index 50a4ca9..7e114d0 100644 --- a/js/imgur-drop-directive.js +++ b/js/imgur-drop-directive.js @@ -3,7 +3,7 @@ var weechat = angular.module('weechat'); -weechat.directive('imgurDrop', ['connection','imgur', function(connection, imgur) { +weechat.directive('imgurDrop', ['connection','imgur','$rootScope', function(connection, imgur, $rootScope) { return { restrict: 'A', link: function($scope, element, attr) { @@ -26,7 +26,7 @@ weechat.directive('imgurDrop', ['connection','imgur', function(connection, imgur // Send image if(imageUrl !== undefined && imageUrl !== '') { - connection.sendMessage(String(imageUrl)); + $rootScope.insertAtCaret(String(imageUrl)); } }; diff --git a/js/inputbar.js b/js/inputbar.js index aa44927..9d18506 100644 --- a/js/inputbar.js +++ b/js/inputbar.js @@ -70,36 +70,38 @@ weechat.directive('inputBar', function() { }, 0); }; + $rootScope.insertAtCaret = function(toInsert) { + // 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 + toInsert + suffix; + + setTimeout(function() { + inputNode.focus(); + var pos = $scope.command.length - suffix.length; + inputNode.setSelectionRange(pos, pos); + // force refresh? + $scope.$apply(); + }, 0); + }; + $scope.uploadImage = function($event, files) { // Send image url after upload var sendImageUrl = 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); - // force refresh? - $scope.$apply(); - }, 0); + $rootScope.insertAtCaret(String(imageUrl)); } - }; if(typeof files !== "undefined" && files.length > 0) {