From: Alexander Ebert Date: Fri, 16 Oct 2020 14:57:29 +0000 (+0200) Subject: Convert `Event/Key` to TypeScript X-Git-Tag: 5.4.0_Alpha_1~704^2~52 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=84731ddec71861e8ef8ec766bf16eae2ab42e019;p=GitHub%2FWoltLab%2FWCF.git Convert `Event/Key` to TypeScript --- diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Event/Key.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Event/Key.js index ed222ce711..5af747fd2d 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Event/Key.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Event/Key.js @@ -1,136 +1,120 @@ /** * 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 - * @module EventKey (alias) - * @module WoltLabSuite/Core/Event/Key + * + * @author Alexander Ebert + * @copyright 2001-2019 WoltLab GmbH + * @license GNU Lesser General Public License + * @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 index 0000000000..b7741afc24 --- /dev/null +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Event/Key.ts @@ -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 + * @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); +}