Browse Source

merge AriaNg commit(https://github.com/mayswind/AriaNg/commit/56ee8b5691e51dca02355dd1907b22d4fb49b2c0)

MaysWind 3 years ago
parent
commit
0d98642928

+ 1 - 0
app/index.html

@@ -416,6 +416,7 @@
 <script src="scripts/services/ariaNgAssetsCacheService.js"></script>
 <script src="scripts/services/ariaNgLanguageLoader.js"></script>
 <script src="scripts/services/ariaNgCommonService.js"></script>
+<script src="scripts/services/ariaNgKeyboardService.js"></script>
 <script src="scripts/services/ariaNgNotificationService.js"></script>
 <script src="scripts/services/ariaNgLocalizationService.js"></script>
 <script src="scripts/services/ariaNgLogService.js"></script>

+ 3 - 2
app/scripts/controllers/list.js

@@ -121,12 +121,13 @@
             }
         });
 
-        $rootScope.keydownActions.selectAll = function () {
+        $rootScope.keydownActions.selectAll = function (event) {
             $scope.$apply(function () {
                 $scope.selectAllTasks();
             });
         };
-        $rootScope.keydownActions.delete = function () {
+
+        $rootScope.keydownActions.delete = function (event) {
             $scope.$apply(function () {
                 $scope.removeTasks();
             });

+ 2 - 4
app/scripts/controllers/new.js

@@ -1,7 +1,7 @@
 (function () {
     'use strict';
 
-    angular.module('ariaNg').controller('NewTaskController', ['$rootScope', '$scope', '$location', '$timeout', 'ariaNgFileTypes', 'ariaNgCommonService', 'ariaNgLogService', 'ariaNgFileService', 'ariaNgSettingService', 'aria2TaskService', 'aria2SettingService', 'ariaNgNativeElectronService', function ($rootScope, $scope, $location, $timeout, ariaNgFileTypes, ariaNgCommonService, ariaNgLogService, ariaNgFileService, ariaNgSettingService, aria2TaskService, aria2SettingService, ariaNgNativeElectronService) {
+    angular.module('ariaNg').controller('NewTaskController', ['$rootScope', '$scope', '$location', '$timeout', 'ariaNgFileTypes', 'ariaNgCommonService', 'ariaNgLogService', 'ariaNgKeyboardService', 'ariaNgFileService', 'ariaNgSettingService', 'aria2TaskService', 'aria2SettingService', 'ariaNgNativeElectronService', function ($rootScope, $scope, $location, $timeout, ariaNgFileTypes, ariaNgCommonService, ariaNgLogService, ariaNgKeyboardService, ariaNgFileService, ariaNgSettingService, aria2TaskService, aria2SettingService, ariaNgNativeElectronService) {
         var tabStatusItems = [
             {
                 name: 'links',
@@ -745,9 +745,7 @@
                 return;
             }
 
-            var keyCode = event.keyCode || event.which || event.charCode;
-
-            if ((event.code === 'Enter' || keyCode === 13) && (event.ctrlKey || event.metaKey) && $scope.newTaskForm.$valid) { // Ctrl+Enter
+            if (ariaNgKeyboardService.isCtrlEnterPressed(event) && $scope.newTaskForm.$valid) {
                 $scope.startDownload();
             }
         };

+ 5 - 7
app/scripts/core/root.js

@@ -1,7 +1,7 @@
 (function () {
     'use strict';
 
-    angular.module('ariaNg').run(['$window', '$rootScope', '$location', '$document', '$timeout', 'ariaNgCommonService', 'ariaNgNotificationService', 'ariaNgLogService', 'ariaNgSettingService', 'aria2TaskService', 'ariaNgNativeElectronService', function ($window, $rootScope, $location, $document, $timeout, ariaNgCommonService, ariaNgNotificationService, ariaNgLogService, ariaNgSettingService, aria2TaskService, ariaNgNativeElectronService) {
+    angular.module('ariaNg').run(['$window', '$rootScope', '$location', '$document', '$timeout', 'ariaNgCommonService', 'ariaNgKeyboardService', 'ariaNgNotificationService', 'ariaNgLogService', 'ariaNgSettingService', 'aria2TaskService', 'ariaNgNativeElectronService', function ($window, $rootScope, $location, $document, $timeout, ariaNgCommonService, ariaNgKeyboardService, ariaNgNotificationService, ariaNgLogService, ariaNgSettingService, aria2TaskService, ariaNgNativeElectronService) {
         var autoRefreshAfterPageLoad = false;
 
         var isUrlMatchUrl2 = function (url, url2) {
@@ -491,15 +491,13 @@
                 return;
             }
 
-            var keyCode = event.keyCode || event.which || event.charCode;
-
-            if ((event.code === 'KeyA' || keyCode === 65) && (event.ctrlKey || event.metaKey)) { // Ctrl+A / Command+A
+            if (ariaNgKeyboardService.isCtrlAPressed(event)) {
                 if (angular.isFunction($rootScope.keydownActions.selectAll)) {
-                    $rootScope.keydownActions.selectAll();
+                    return $rootScope.keydownActions.selectAll(event);
                 }
-            } else if (event.code === 'Delete' || keyCode === 46) { // Delete
+            } else if (ariaNgKeyboardService.isDeletePressed(event)) {
                 if (angular.isFunction($rootScope.keydownActions.delete)) {
-                    $rootScope.keydownActions.delete();
+                    return $rootScope.keydownActions.delete(event);
                 }
             }
         }, true);

+ 39 - 0
app/scripts/services/ariaNgKeyboardService.js

@@ -0,0 +1,39 @@
+(function () {
+    'use strict';
+
+    angular.module('ariaNg').factory('ariaNgKeyboardService', ['$window', function ($window) {
+        var platform = '';
+
+        if ($window.navigator && $window.navigator.userAgentData && $window.navigator.userAgentData.platform) {
+            platform = $window.navigator.userAgentData.platform;
+        } else if ($window.navigator && $window.navigator.platform) {
+            platform = $window.navigator.platform;
+        }
+
+        var isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(platform);
+
+        var isModifierKeyPressed = function (event) {
+            if (isMacLike) {
+                return event.metaKey;
+            } else {
+                return event.ctrlKey;
+            }
+        };
+
+        var getKeyCode = function (event) {
+            return event.keyCode || event.which || event.charCode;
+        };
+
+        return {
+            isCtrlAPressed: function (event) {
+                return (isModifierKeyPressed(event) && (event.code === 'KeyA' || getKeyCode(event) === 65)); // Ctrl+A / Command+A
+            },
+            isCtrlEnterPressed: function (event) {
+                return (isModifierKeyPressed(event) && (event.code === 'Enter' || getKeyCode(event) === 13)); // Ctrl+Enter / Command+Return
+            },
+            isDeletePressed: function (event) {
+                return (event.code === 'Delete' || getKeyCode(event) === 46); // Delete
+            }
+        };
+    }]);
+}());