Convert `Event/Key` to TypeScript
authorAlexander Ebert <ebert@woltlab.com>
Fri, 16 Oct 2020 14:57:29 +0000 (16:57 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 28 Oct 2020 11:28:29 +0000 (12:28 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Event/Key.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Event/Key.ts [new file with mode: 0644]

index ed222ce71143f02d818a42426e6e7c1bd1d26c5c..5af747fd2dec2068a22d4e558036dc5378089bad 100644 (file)
 /**
  * Provides reliable checks for common key presses, uses `Event.key` on supported browsers
  * or the deprecated `Event.which`.
- * 
- * @author     Alexander Ebert
- * @copyright  2001-2019 WoltLab GmbH
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
- * @module     EventKey (alias)
- * @module     WoltLabSuite/Core/Event/Key
+ *
+ * @author  Alexander Ebert
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  EventKey (alias)
+ * @module  WoltLabSuite/Core/Event/Key
  */
-define([], function() {
-       "use strict";
-       
-       function _isKey(event, key, which) {
-               if (!(event instanceof Event)) {
-                       throw new TypeError("Expected a valid event when testing for key '" + key + "'.");
-               }
-               
-               return event.key === key || event.which === which;
-       }
-       
-       /**
-        * @exports     WoltLabSuite/Core/Event/Key
-        */
-       return {
-               /**
-                * Returns true if the pressed key equals 'ArrowDown'.
-                * 
-                * @param       {Event}         event           event object
-                * @return      {boolean}
-                */
-               ArrowDown: function(event) {
-                       return _isKey(event, 'ArrowDown', 40);
-               },
-               
-               /**
-                * Returns true if the pressed key equals 'ArrowLeft'.
-                * 
-                * @param       {Event}         event           event object
-                * @return      {boolean}
-                */
-               ArrowLeft: function(event) {
-                       return _isKey(event, 'ArrowLeft', 37);
-               },
-               
-               /**
-                * Returns true if the pressed key equals 'ArrowRight'.
-                * 
-                * @param       {Event}         event           event object
-                * @return      {boolean}
-                */
-               ArrowRight: function(event) {
-                       return _isKey(event, 'ArrowRight', 39);
-               },
-               
-               /**
-                * Returns true if the pressed key equals 'ArrowUp'.
-                * 
-                * @param       {Event}         event           event object
-                * @return      {boolean}
-                */
-               ArrowUp: function(event) {
-                       return _isKey(event, 'ArrowUp', 38);
-               },
-               
-               /**
-                * Returns true if the pressed key equals 'Comma'.
-                * 
-                * @param       {Event}         event           event object
-                * @return      {boolean}
-                */
-               Comma: function(event) {
-                       return _isKey(event, ',', 44);
-               },
-               
-               /**
-                * Returns true if the pressed key equals 'End'.
-                *
-                * @param       {Event}         event           event object
-                * @return      {boolean}
-                */
-               End: function(event) {
-                       return _isKey(event, 'End', 35);
-               },
-               
-               /**
-                * Returns true if the pressed key equals 'Enter'.
-                * 
-                * @param       {Event}         event           event object
-                * @return      {boolean}
-                */
-               Enter: function(event) {
-                       return _isKey(event, 'Enter', 13);
-               },
-               
-               /**
-                * Returns true if the pressed key equals 'Escape'.
-                * 
-                * @param       {Event}         event           event object
-                * @return      {boolean}
-                */
-               Escape: function(event) {
-                       return _isKey(event, 'Escape', 27);
-               },
-               
-               /**
-                * Returns true if the pressed key equals 'Home'.
-                *
-                * @param       {Event}         event           event object
-                * @return      {boolean}
-                */
-               Home: function(event) {
-                       return _isKey(event, 'Home', 36);
-               },
-               
-               /**
-                * Returns true if the pressed key equals 'Space'.
-                *
-                * @param       {Event}         event           event object
-                * @return      {boolean}
-                */
-               Space: function(event) {
-                       return _isKey(event, 'Space', 32);
-               },
-               
-               /**
-                * Returns true if the pressed key equals 'Tab'.
-                * 
-                * @param       {Event}         event           event object
-                * @return      {boolean}
-                */
-               Tab: function(event) {
-                       return _isKey(event, 'Tab', 9);
-               }
-       };
+define(["require", "exports"], function (require, exports) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.Tab = exports.Space = exports.Home = exports.Escape = exports.Enter = exports.End = exports.Comma = exports.ArrowUp = exports.ArrowRight = exports.ArrowLeft = exports.ArrowDown = void 0;
+    function _test(event, key, which) {
+        if (!(event instanceof Event)) {
+            throw new TypeError("Expected a valid event when testing for key '" + key + "'.");
+        }
+        return event.key === key || event.which === which;
+    }
+    /**
+     * Returns true if the pressed key equals 'ArrowDown'.
+     *
+     * @deprecated 5.4 Use `event.key === "ArrowDown"` instead.
+     */
+    function ArrowDown(event) {
+        return _test(event, 'ArrowDown', 40);
+    }
+    exports.ArrowDown = ArrowDown;
+    /**
+     * Returns true if the pressed key equals 'ArrowLeft'.
+     *
+     * @deprecated 5.4 Use `event.key === "ArrowLeft"` instead.
+     */
+    function ArrowLeft(event) {
+        return _test(event, 'ArrowLeft', 37);
+    }
+    exports.ArrowLeft = ArrowLeft;
+    /**
+     * Returns true if the pressed key equals 'ArrowRight'.
+     *
+     * @deprecated 5.4 Use `event.key === "ArrowRight"` instead.
+     */
+    function ArrowRight(event) {
+        return _test(event, 'ArrowRight', 39);
+    }
+    exports.ArrowRight = ArrowRight;
+    /**
+     * Returns true if the pressed key equals 'ArrowUp'.
+     *
+     * @deprecated 5.4 Use `event.key === "ArrowUp"` instead.
+     */
+    function ArrowUp(event) {
+        return _test(event, 'ArrowUp', 38);
+    }
+    exports.ArrowUp = ArrowUp;
+    /**
+     * Returns true if the pressed key equals 'Comma'.
+     *
+     * @deprecated 5.4 Use `event.key === ","` instead.
+     */
+    function Comma(event) {
+        return _test(event, ',', 44);
+    }
+    exports.Comma = Comma;
+    /**
+     * Returns true if the pressed key equals 'End'.
+     *
+     * @deprecated 5.4 Use `event.key === "End"` instead.
+     */
+    function End(event) {
+        return _test(event, 'End', 35);
+    }
+    exports.End = End;
+    /**
+     * Returns true if the pressed key equals 'Enter'.
+     *
+     * @deprecated 5.4 Use `event.key === "Enter"` instead.
+     */
+    function Enter(event) {
+        return _test(event, 'Enter', 13);
+    }
+    exports.Enter = Enter;
+    /**
+     * Returns true if the pressed key equals 'Escape'.
+     *
+     * @deprecated 5.4 Use `event.key === "Escape"` instead.
+     */
+    function Escape(event) {
+        return _test(event, 'Escape', 27);
+    }
+    exports.Escape = Escape;
+    /**
+     * Returns true if the pressed key equals 'Home'.
+     *
+     * @deprecated 5.4 Use `event.key === "Home"` instead.
+     */
+    function Home(event) {
+        return _test(event, 'Home', 36);
+    }
+    exports.Home = Home;
+    /**
+     * Returns true if the pressed key equals 'Space'.
+     *
+     * @deprecated 5.4 Use `event.key === "Space"` instead.
+     */
+    function Space(event) {
+        return _test(event, 'Space', 32);
+    }
+    exports.Space = Space;
+    /**
+     * Returns true if the pressed key equals 'Tab'.
+     *
+     * @deprecated 5.4 Use `event.key === "Tab"` instead.
+     */
+    function Tab(event) {
+        return _test(event, 'Tab', 9);
+    }
+    exports.Tab = Tab;
 });
diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Event/Key.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Event/Key.ts
new file mode 100644 (file)
index 0000000..b7741af
--- /dev/null
@@ -0,0 +1,117 @@
+/**
+ * Provides reliable checks for common key presses, uses `Event.key` on supported browsers
+ * or the deprecated `Event.which`.
+ *
+ * @author  Alexander Ebert
+ * @copyright  2001-2019 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  EventKey (alias)
+ * @module  WoltLabSuite/Core/Event/Key
+ */
+
+function _test(event: KeyboardEvent, key: string, which: number) {
+  if (!(event instanceof Event)) {
+    throw new TypeError("Expected a valid event when testing for key '" + key + "'.");
+  }
+
+  return event.key === key || event.which === which;
+}
+
+/**
+ * Returns true if the pressed key equals 'ArrowDown'.
+ *
+ * @deprecated 5.4 Use `event.key === "ArrowDown"` instead.
+ */
+export function ArrowDown(event: KeyboardEvent): boolean {
+  return _test(event, 'ArrowDown', 40);
+}
+
+/**
+ * Returns true if the pressed key equals 'ArrowLeft'.
+ *
+ * @deprecated 5.4 Use `event.key === "ArrowLeft"` instead.
+ */
+export function ArrowLeft(event: KeyboardEvent): boolean {
+  return _test(event, 'ArrowLeft', 37);
+}
+
+/**
+ * Returns true if the pressed key equals 'ArrowRight'.
+ *
+ * @deprecated 5.4 Use `event.key === "ArrowRight"` instead.
+ */
+export function ArrowRight(event: KeyboardEvent): boolean {
+  return _test(event, 'ArrowRight', 39);
+}
+
+/**
+ * Returns true if the pressed key equals 'ArrowUp'.
+ *
+ * @deprecated 5.4 Use `event.key === "ArrowUp"` instead.
+ */
+export function ArrowUp(event: KeyboardEvent): boolean {
+  return _test(event, 'ArrowUp', 38);
+}
+
+/**
+ * Returns true if the pressed key equals 'Comma'.
+ *
+ * @deprecated 5.4 Use `event.key === ","` instead.
+ */
+export function Comma(event: KeyboardEvent): boolean {
+  return _test(event, ',', 44);
+}
+
+/**
+ * Returns true if the pressed key equals 'End'.
+ *
+ * @deprecated 5.4 Use `event.key === "End"` instead.
+ */
+export function End(event: KeyboardEvent): boolean {
+  return _test(event, 'End', 35);
+}
+
+/**
+ * Returns true if the pressed key equals 'Enter'.
+ *
+ * @deprecated 5.4 Use `event.key === "Enter"` instead.
+ */
+export function Enter(event: KeyboardEvent): boolean {
+  return _test(event, 'Enter', 13);
+}
+
+/**
+ * Returns true if the pressed key equals 'Escape'.
+ *
+ * @deprecated 5.4 Use `event.key === "Escape"` instead.
+ */
+export function Escape(event: KeyboardEvent): boolean {
+  return _test(event, 'Escape', 27);
+}
+
+/**
+ * Returns true if the pressed key equals 'Home'.
+ *
+ * @deprecated 5.4 Use `event.key === "Home"` instead.
+ */
+export function Home(event: KeyboardEvent): boolean {
+  return _test(event, 'Home', 36);
+}
+
+/**
+ * Returns true if the pressed key equals 'Space'.
+ *
+ * @deprecated 5.4 Use `event.key === "Space"` instead.
+ */
+export function Space(event: KeyboardEvent): boolean {
+  return _test(event, 'Space', 32);
+}
+
+/**
+ * Returns true if the pressed key equals 'Tab'.
+ *
+ * @deprecated 5.4 Use `event.key === "Tab"` instead.
+ */
+export function Tab(event: KeyboardEvent): boolean {
+  return _test(event, 'Tab', 9);
+}