Skip to content

Commit 0dddb6c

Browse files
Cherry pick keyboardcontrols=1 flag to stable12.0 (#11206)
1 parent 78372a9 commit 0dddb6c

7 files changed

Lines changed: 36 additions & 6 deletions

File tree

localtypings/pxteditor.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ declare namespace pxt.editor {
914914
forceUpdate(): void;
915915

916916
reloadEditor(): void;
917-
openBlocks(): void;
917+
openBlocks(showKeyboardControlsHint?: boolean): void;
918918
openJavaScript(giveFocusOnLoading?: boolean): void;
919919
openPython(giveFocusOnLoading?: boolean): void;
920920
openAssets(): void;

pxtlib/auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ namespace pxt.auth {
7373
export const DEFAULT_USER_PREFERENCES: () => UserPreferences = () => ({
7474
language: pxt.appTarget.appTheme.defaultLocale,
7575
highContrast: false,
76-
accessibleBlocks: false,
76+
accessibleBlocks: undefined, // Defaulted at read time depending on flag
7777
colorThemeIds: {}, // Will lookup pxt.appTarget.appTheme.defaultColorTheme for active target
7878
reader: "",
7979
skillmap: { mapProgress: {}, completedTags: {} },

webapp/src/accessibility.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as React from "react";
44
import * as auth from "./auth";
5+
import * as core from "./core";
56
import * as data from "./data";
67
import * as sui from "./sui";
78

@@ -34,7 +35,7 @@ export class EditorAccessibilityMenu extends data.Component<EditorAccessibilityM
3435
}
3536

3637
openBlocks(e: React.MouseEvent<HTMLElement>) {
37-
this.props.parent.openBlocks();
38+
this.props.parent.openBlocks(core.isKeyboardControlsByDefault());
3839
}
3940

4041
openJavaScript() {

webapp/src/app.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,13 @@ export class ProjectView
716716
pxt.shell.setEditorLanguagePref("js");
717717
}
718718

719-
openBlocks() {
719+
openBlocks(showKeyboardControlsHint?: boolean) {
720720
if (this.updatingEditorFile) return; // already transitioning
721721

722+
if (showKeyboardControlsHint) {
723+
this.blocksEditor.pendingKeyboardControlsHint = true;
724+
}
725+
722726
if (this.isBlocksActive()) {
723727
if (this.state.embedSimView) this.setState({ embedSimView: false });
724728
// This timeout prevents key events from being handled by Blockly's keyboard

webapp/src/auth.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,11 @@ class AuthClient extends pxt.auth.AuthClient {
117117
// Identity not available, read from local storage
118118
switch (path) {
119119
case HIGHCONTRAST: return /^true$/i.test(pxt.storage.getLocal(HIGHCONTRAST));
120-
case ACCESSIBLE_BLOCKS: return /^true$/i.test(pxt.storage.getLocal(ACCESSIBLE_BLOCKS));
120+
case ACCESSIBLE_BLOCKS: {
121+
const stored = pxt.storage.getLocal(ACCESSIBLE_BLOCKS);
122+
if (stored == null) return core.isKeyboardControlsByDefault();
123+
return /^true$/i.test(stored);
124+
}
121125
case COLOR_THEME_IDS: return pxt.U.jsonTryParse(pxt.storage.getLocal(COLOR_THEME_IDS)) as pxt.auth.ColorThemeIdsState;
122126
case LANGUAGE: return pxt.storage.getLocal(LANGUAGE);
123127
case READER: return pxt.storage.getLocal(READER);
@@ -134,7 +138,7 @@ class AuthClient extends pxt.auth.AuthClient {
134138
switch (field) {
135139
case FIELD_USER_PREFERENCES: return { ...state.preferences };
136140
case FIELD_HIGHCONTRAST: return state.preferences?.highContrast ?? pxt.auth.DEFAULT_USER_PREFERENCES().highContrast;
137-
case FIELD_KEYBOARD_CONTROLS: return state.preferences?.accessibleBlocks ?? pxt.auth.DEFAULT_USER_PREFERENCES().accessibleBlocks;
141+
case FIELD_KEYBOARD_CONTROLS: return state.preferences?.accessibleBlocks ?? core.isKeyboardControlsByDefault();
138142
case FIELD_COLOR_THEME_IDS: return state.preferences?.colorThemeIds ?? pxt.auth.DEFAULT_USER_PREFERENCES().colorThemeIds;
139143
case FIELD_LANGUAGE: return state.preferences?.language ?? pxt.auth.DEFAULT_USER_PREFERENCES().language;
140144
case FIELD_READER: return state.preferences?.reader ?? pxt.auth.DEFAULT_USER_PREFERENCES().reader;
@@ -250,6 +254,7 @@ export async function setAccessibleBlocksPrefAsync(accessibleBlocks: boolean, ev
250254
"auth.setAccessibleBlocks",
251255
{
252256
enabling: accessibleBlocks ? "true" : "false",
257+
defaultOn: core.isKeyboardControlsByDefault() ? "true" : "false",
253258
eventSource: eventSource,
254259
local: !cli ? "true" : "false"
255260
}

webapp/src/blocks.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export class Editor extends toolboxeditor.ToolboxEditor {
5959
loadingXmlPromise: Promise<any>;
6060
compilationResult: pxtblockly.BlockCompilationResult;
6161
shouldFocusWorkspace = false;
62+
pendingKeyboardControlsHint = false;
6263
functionsDialog: CreateFunctionDialog = null;
6364

6465
showCategories: boolean = true;
@@ -997,9 +998,21 @@ export class Editor extends toolboxeditor.ToolboxEditor {
997998
if (accessibleBlocksEnabled) {
998999
(this.editor.getSvgGroup() as SVGElement).focus();
9991000
Blockly.hideChaff();
1001+
1002+
if (this.pendingKeyboardControlsHint) {
1003+
this.pendingKeyboardControlsHint = false;
1004+
this.showKeyboardControlsHint();
1005+
}
10001006
}
10011007
}
10021008

1009+
showKeyboardControlsHint() {
1010+
if (!this.editor || !Blockly.Msg["HELP_PROMPT"]) return;
1011+
const shortcut = pxt.BrowserUtils.isMac() ? "⌘ /" : lf("Ctrl") + " + /";
1012+
const message = Blockly.Msg["HELP_PROMPT"].replace("%1", shortcut);
1013+
Blockly.Toast.show(this.editor, { message, id: "helpHint", oncePerSession: true });
1014+
}
1015+
10031016
hasUndo() {
10041017
const undoStack = this.editor?.getUndoStack();
10051018
const redoStack = this.editor?.getRedoStack();

webapp/src/core.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,13 @@ export function getHighContrastOnce(): boolean {
343343
return ThemeManager.isCurrentThemeHighContrast();
344344
}
345345

346+
/**
347+
* Returns true if keyboard controls should be enabled by default.
348+
*/
349+
export function isKeyboardControlsByDefault(): boolean {
350+
return /keyboardcontrols=1/i.test(window.location.href);
351+
}
352+
346353
export async function toggleAccessibleBlocks(eventSource: string) {
347354
await setAccessibleBlocks(!data.getData<boolean>(auth.ACCESSIBLE_BLOCKS), eventSource);
348355
}

0 commit comments

Comments
 (0)