Insert URL at caret when dropping image into GB

unfortunately this pollutes the root scope a bit more
This commit is contained in:
Lorenz Hübschle-Schneider 2015-11-10 19:40:30 +01:00
parent 3f661ded6c
commit a6c2e6f387
2 changed files with 28 additions and 26 deletions

View file

@ -3,7 +3,7 @@
var weechat = angular.module('weechat'); var weechat = angular.module('weechat');
weechat.directive('imgurDrop', ['connection','imgur', function(connection, imgur) { weechat.directive('imgurDrop', ['connection','imgur','$rootScope', function(connection, imgur, $rootScope) {
return { return {
restrict: 'A', restrict: 'A',
link: function($scope, element, attr) { link: function($scope, element, attr) {
@ -26,7 +26,7 @@ weechat.directive('imgurDrop', ['connection','imgur', function(connection, imgur
// Send image // Send image
if(imageUrl !== undefined && imageUrl !== '') { if(imageUrl !== undefined && imageUrl !== '') {
connection.sendMessage(String(imageUrl)); $rootScope.insertAtCaret(String(imageUrl));
} }
}; };

View file

@ -70,36 +70,38 @@ weechat.directive('inputBar', function() {
}, 0); }, 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) { $scope.uploadImage = function($event, files) {
// Send image url after upload // Send image url after upload
var sendImageUrl = function(imageUrl) { var sendImageUrl = function(imageUrl) {
// Send image // Send image
if(imageUrl !== undefined && imageUrl !== '') { if(imageUrl !== undefined && imageUrl !== '') {
// caret position in the input bar $rootScope.insertAtCaret(String(imageUrl));
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);
} }
}; };
if(typeof files !== "undefined" && files.length > 0) { if(typeof files !== "undefined" && files.length > 0) {