Skip to content

Commit a3c3646

Browse files
feat: convert several files to typescript (#38)
* chore: convert line_cursor to typescript * chore: convert constants to ts * chore: more ts conversions and cleanup * chore: update announcer to typescript * chore: update navigation to TS * chore: convert SHORTCUT_NAMES to an enum * chore: format
1 parent ec607e4 commit a3c3646

File tree

7 files changed

+557
-522
lines changed

7 files changed

+557
-522
lines changed

src/announcer.js renamed to src/announcer.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
*/
66

77
import {ShortcutRegistry} from 'blockly/core';
8+
// @ts-expect-error No types in js file
89
import {keyCodeArrayToString} from './keynames';
910

1011
/**
1112
* Class for surfacing information about keyboard navigation state.
1213
*/
1314
export class Announcer {
15+
outputDiv: HTMLElement | null;
1416
/**
1517
* Constructor for an Announcer.
1618
*/
@@ -38,14 +40,19 @@ export class Announcer {
3840
const prettyPrinted = keyCodeArrayToString(codeArray);
3941
text += `<tr><td>${keyboardShortcut}</td> <td>${prettyPrinted}</td></tr>`;
4042
}
41-
this.outputDiv.innerHTML = text + '\n</table/>';
43+
if (this.outputDiv) {
44+
this.outputDiv.innerHTML = text + '\n</table/>';
45+
}
4246
}
4347

4448
/**
4549
* Set the text of the output div, with no validation.
46-
* @param {string} text The text to display.
50+
*
51+
* @param text The text to display.
4752
*/
48-
setText(text) {
49-
this.outputDiv.innerHTML = text;
53+
setText(text: string) {
54+
if (this.outputDiv) {
55+
this.outputDiv.innerHTML = text;
56+
}
5057
}
5158
}

src/constants.js

Lines changed: 0 additions & 63 deletions
This file was deleted.

src/constants.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/**
8+
* @fileoverview Constants for keyboard navigation.
9+
* @author [email protected] (Abby Schmiedt)
10+
*/
11+
12+
/**
13+
* Keyboard navigation states.
14+
* The different parts of Blockly that the user navigates between.
15+
*/
16+
export enum STATE {
17+
WORKSPACE = 'workspace',
18+
FLYOUT = 'flyout',
19+
TOOLBOX = 'toolbox',
20+
}
21+
22+
/**
23+
* Default keyboard navigation shortcut names.
24+
*/
25+
export enum SHORTCUT_NAMES {
26+
PREVIOUS = 'previous',
27+
NEXT = 'next',
28+
IN = 'in',
29+
OUT = 'out',
30+
INSERT = 'insert',
31+
MARK = 'mark',
32+
DISCONNECT = 'disconnect',
33+
TOOLBOX = 'toolbox',
34+
EXIT = 'exit',
35+
COPY = 'keyboard_nav_copy',
36+
CUT = 'keyboard_nav_cut',
37+
PASTE = 'keyboard_nav_paste',
38+
DELETE = 'keyboard_nav_delete',
39+
/* eslint-disable @typescript-eslint/naming-convention */
40+
MOVE_WS_CURSOR_UP = 'workspace_up',
41+
MOVE_WS_CURSOR_DOWN = 'workspace_down',
42+
MOVE_WS_CURSOR_LEFT = 'workspace_left',
43+
MOVE_WS_CURSOR_RIGHT = 'workspace_right',
44+
TOGGLE_KEYBOARD_NAV = 'toggle_keyboard_nav',
45+
/* eslint-enable @typescript-eslint/naming-convention */
46+
}
47+
48+
/**
49+
* Types of possible messages passed into the loggingCallback in the Navigation
50+
* class.
51+
*/
52+
export enum LOGGING_MSG_TYPE {
53+
ERROR = 'error',
54+
WARN = 'warn',
55+
LOG = 'log',
56+
}
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import * as Blockly from 'blockly/core';
1515
* Class for a flyout cursor.
1616
* This controls how a user navigates blocks in the flyout.
1717
* This cursor only allows a user to go to the previous or next stack.
18-
* @constructor
19-
* @extends {Blockly.Cursor}
2018
*/
2119
export class FlyoutCursor extends Blockly.Cursor {
2220
/**
@@ -28,11 +26,11 @@ export class FlyoutCursor extends Blockly.Cursor {
2826

2927
/**
3028
* Moves the cursor to the next stack of blocks in the flyout.
31-
* @returns {Blockly.ASTNode} The next element, or null if the current node is
29+
*
30+
* @returns The next element, or null if the current node is
3231
* not set or there is no next value.
33-
* @override
3432
*/
35-
next() {
33+
override next(): Blockly.ASTNode | null {
3634
const curNode = this.getCurNode();
3735
if (!curNode) {
3836
return null;
@@ -47,20 +45,20 @@ export class FlyoutCursor extends Blockly.Cursor {
4745

4846
/**
4947
* This is a no-op since a flyout cursor can not go in.
50-
* @returns {null} Always null.
51-
* @override
48+
*
49+
* @returns Always null.
5250
*/
53-
in() {
51+
override in(): null {
5452
return null;
5553
}
5654

5755
/**
5856
* Moves the cursor to the previous stack of blocks in the flyout.
59-
* @returns {Blockly.ASTNode} The previous element, or null if the current
57+
*
58+
* @returns The previous element, or null if the current
6059
* node is not set or there is no previous value.
61-
* @override
6260
*/
63-
prev() {
61+
override prev(): Blockly.ASTNode | null {
6462
const curNode = this.getCurNode();
6563
if (!curNode) {
6664
return null;
@@ -75,10 +73,10 @@ export class FlyoutCursor extends Blockly.Cursor {
7573

7674
/**
7775
* This is a no-op since a flyout cursor can not go out.
78-
* @returns {null} Always null.
79-
* @override
76+
*
77+
* @returns Always null.
8078
*/
81-
out() {
79+
override out(): null {
8280
return null;
8381
}
8482
}
@@ -89,5 +87,5 @@ export const registrationName = 'FlyoutCursor';
8987
Blockly.registry.register(registrationType, registrationName, FlyoutCursor);
9088

9189
export const pluginInfo = {
92-
[registrationType]: registrationName,
90+
[registrationType.toString()]: registrationName,
9391
};

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import * as Blockly from 'blockly/core';
88
// @ts-expect-error No types in js file
99
import {NavigationController} from './navigation_controller';
10-
// @ts-expect-error No types in js file
1110
import {installCursor} from './line_cursor';
1211

1312
/** Plugin for keyboard navigation. */

0 commit comments

Comments
 (0)