|
| 1 | +/** |
| 2 | + * Centralises hints that we show. |
| 3 | + * |
| 4 | + * @license |
| 5 | + * Copyright 2025 Google LLC |
| 6 | + * SPDX-License-Identifier: Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +import {WorkspaceSvg, Toast} from 'blockly'; |
| 10 | +import {SHORTCUT_NAMES} from './constants'; |
| 11 | +import {getShortActionShortcut} from './shortcut_formatting'; |
| 12 | + |
| 13 | +const unconstrainedMoveHintId = 'unconstrainedMoveHint'; |
| 14 | +const constrainedMoveHintId = 'constrainedMoveHint'; |
| 15 | +const copiedHintId = 'copiedHint'; |
| 16 | +const cutHintId = 'cutHint'; |
| 17 | +const helpHintId = 'helpHint'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Nudge the user to use unconstrained movement. |
| 21 | + * |
| 22 | + * @param workspace Workspace. |
| 23 | + * @param force Set to show it even if previously shown. |
| 24 | + */ |
| 25 | +export function showUnconstrainedMoveHint( |
| 26 | + workspace: WorkspaceSvg, |
| 27 | + force = false, |
| 28 | +) { |
| 29 | + const enter = getShortActionShortcut(SHORTCUT_NAMES.EDIT_OR_CONFIRM); |
| 30 | + const modifier = navigator.platform.startsWith('Mac') ? '⌥' : 'Ctrl'; |
| 31 | + const message = `Hold ${modifier} and use arrow keys to move freely, then ${enter} to accept the position`; |
| 32 | + Toast.show(workspace, { |
| 33 | + message, |
| 34 | + id: unconstrainedMoveHintId, |
| 35 | + oncePerSession: !force, |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Nudge the user to move a block that's in move mode. |
| 41 | + * |
| 42 | + * @param workspace Workspace. |
| 43 | + */ |
| 44 | +export function showConstrainedMovementHint(workspace: WorkspaceSvg) { |
| 45 | + const enter = getShortActionShortcut(SHORTCUT_NAMES.EDIT_OR_CONFIRM); |
| 46 | + const message = `Use the arrow keys to move, then ${enter} to accept the position`; |
| 47 | + Toast.show(workspace, { |
| 48 | + message, |
| 49 | + id: constrainedMoveHintId, |
| 50 | + oncePerSession: true, |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Clear active move-related hints, if any. |
| 56 | + * |
| 57 | + * @param workspace The workspace. |
| 58 | + */ |
| 59 | +export function clearMoveHints(workspace: WorkspaceSvg) { |
| 60 | + Toast.hide(workspace, constrainedMoveHintId); |
| 61 | + Toast.hide(workspace, unconstrainedMoveHintId); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Nudge the user to paste after a copy. |
| 66 | + * |
| 67 | + * @param workspace Workspace. |
| 68 | + */ |
| 69 | +export function showCopiedHint(workspace: WorkspaceSvg) { |
| 70 | + Toast.show(workspace, { |
| 71 | + message: `Copied. Press ${getShortActionShortcut('paste')} to paste.`, |
| 72 | + duration: 7000, |
| 73 | + id: copiedHintId, |
| 74 | + }); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Nudge the user to paste after a cut. |
| 79 | + * |
| 80 | + * @param workspace Workspace. |
| 81 | + */ |
| 82 | +export function showCutHint(workspace: WorkspaceSvg) { |
| 83 | + Toast.show(workspace, { |
| 84 | + message: `Cut. Press ${getShortActionShortcut('paste')} to paste.`, |
| 85 | + duration: 7000, |
| 86 | + id: cutHintId, |
| 87 | + }); |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Clear active paste-related hints, if any. |
| 92 | + * |
| 93 | + * @param workspace The workspace. |
| 94 | + */ |
| 95 | +export function clearPasteHints(workspace: WorkspaceSvg) { |
| 96 | + Toast.hide(workspace, cutHintId); |
| 97 | + Toast.hide(workspace, copiedHintId); |
| 98 | +} |
| 99 | + |
| 100 | +/** |
| 101 | + * Nudge the user to open the help. |
| 102 | + * |
| 103 | + * @param workspace The workspace. |
| 104 | + */ |
| 105 | +export function showHelpHint(workspace: WorkspaceSvg) { |
| 106 | + const shortcut = getShortActionShortcut('list_shortcuts'); |
| 107 | + const message = `Press ${shortcut} for help on keyboard controls`; |
| 108 | + const id = helpHintId; |
| 109 | + Toast.show(workspace, {message, id}); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Clear the help hint. |
| 114 | + * |
| 115 | + * @param workspace The workspace. |
| 116 | + */ |
| 117 | +export function clearHelpHint(workspace: WorkspaceSvg) { |
| 118 | + // TODO: We'd like to do this in MakeCode too as we override. |
| 119 | + // Could have an option for showing help in the plugin? |
| 120 | + Toast.hide(workspace, helpHintId); |
| 121 | +} |
0 commit comments