Skip to content

Commit 9367532

Browse files
feat: replace alert with an illustrative toast
Use for Copy and Enter on a block. This is closer to the interaction envisaged on #192 This implementation is illustrative to gather feedback on the interaction.
1 parent 7e46977 commit 9367532

File tree

3 files changed

+156
-4
lines changed

3 files changed

+156
-4
lines changed

src/navigation.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
FlyoutCursor,
1919
} from './flyout_cursor';
2020
import {PassiveFocus} from './passive_focus';
21+
import {toast} from './toast';
2122

2223
/**
2324
* Class that holds all methods necessary for keyboard navigation to work.
@@ -544,7 +545,7 @@ export class Navigation {
544545
* - Resume editing by returning the cursor to its previous location, if any.
545546
* - Move the cursor to the top connection point on on the first top block.
546547
* - Move the cursor to the default location on the workspace.
547-
*
548+
*
548549
* @param workspace The main Blockly workspace.
549550
* @param keepPosition Whether to retain the cursor's previous position.
550551
*/
@@ -1270,14 +1271,14 @@ export class Navigation {
12701271
} else if (nodeType === Blockly.ASTNode.types.BLOCK) {
12711272
const block = curNode.getLocation() as Blockly.Block;
12721273
if (!tryShowFullBlockFieldEditor(block)) {
1273-
const metaKey = navigator.platform.startsWith('Mac') ? 'Cmd' : 'Ctrl';
1274+
const metaKey = navigator.platform.startsWith('Mac') ? '' : 'Ctrl';
12741275
const canMoveInHint = `Press right arrow to move in or ${metaKey} + Enter for more options`;
12751276
const genericHint = `Press ${metaKey} + Enter for options`;
1276-
const hint =
1277+
const message =
12771278
curNode.in()?.getSourceBlock() === block
12781279
? canMoveInHint
12791280
: genericHint;
1280-
alert(hint);
1281+
toast(workspace, {message});
12811282
}
12821283
} else if (
12831284
curNode.isConnection() ||

src/navigation_controller.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {Navigation} from './navigation';
3131
import {Announcer} from './announcer';
3232
import {LineCursor} from './line_cursor';
3333
import {ShortcutDialog} from './shortcut_dialog';
34+
import {toast} from './toast';
3435

3536
const KeyCodes = BlocklyUtils.KeyCodes;
3637
const createSerializedKey = ShortcutRegistry.registry.createSerializedKey.bind(
@@ -342,6 +343,12 @@ export class NavigationController {
342343
workspace.hideChaff();
343344
this.copyData = sourceBlock.toCopyData();
344345
this.copyWorkspace = sourceBlock.workspace;
346+
if (this.copyData) {
347+
toast(workspace, {
348+
message: `Copied. Press ${navigator.platform.startsWith('Mac') ? '⌘' : 'Ctrl'} + V to paste.`,
349+
duration: 3000,
350+
});
351+
}
345352
return !!this.copyData;
346353
}
347354

src/toast.ts

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import {WorkspaceSvg} from 'blockly';
2+
3+
/**
4+
* Toast options.
5+
*/
6+
export interface ToastOptions {
7+
/**
8+
* Message text.
9+
*/
10+
message: string;
11+
/**
12+
* Duration in milliseconds before the toast is removed.
13+
* Defaults to 5000.
14+
*/
15+
duration?: number;
16+
}
17+
18+
/**
19+
* Shows a message as a toast positioned over the workspace.
20+
*
21+
* This is illustrative to gather feedback on the interaction.
22+
*
23+
* If retained, we'd expect to allow applications to override with their own implementations.
24+
*
25+
* Further work is needed on the accessibility of this toast:
26+
* - testing screen reader support
27+
* - considering whether support for stacked toasts is needed
28+
* - shortcut to focus? though it's the next tab stop currently
29+
*
30+
* @param workspace The workspace for positioning.
31+
* @param options Options.
32+
*/
33+
export function toast(workspace: WorkspaceSvg, options: ToastOptions): void {
34+
const {message, duration = 5000} = options;
35+
const className = 'blocklyToast';
36+
workspace.getInjectionDiv().querySelector(`.${className}`)?.remove();
37+
38+
const toast = document.createElement('div');
39+
toast.className = className;
40+
toast.setAttribute('role', 'status');
41+
toast.setAttribute('aria-live', 'polite');
42+
const {
43+
FIELD_TEXT_FONTWEIGHT: fontWeight,
44+
FIELD_TEXT_FONTFAMILY: fontFamily,
45+
FIELD_TEXT_FONTSIZE: fontSizePt,
46+
} = workspace.getRenderer().getConstants();
47+
assignStyle(toast, {
48+
fontFamily,
49+
fontWeight,
50+
fontSize: `${fontSizePt}pt`,
51+
position: 'absolute',
52+
maxWidth: '20em',
53+
bottom: '2rem',
54+
right: '-50rem',
55+
padding: '1rem',
56+
color: 'white',
57+
backgroundColor: 'rebeccapurple',
58+
borderRadius: '0.4rem',
59+
zIndex: '999',
60+
display: 'flex',
61+
alignItems: 'center',
62+
gap: '0.8rem',
63+
lineHeight: '1.5',
64+
transition: 'right 0.3s ease-out',
65+
});
66+
67+
toast.appendChild(
68+
infoIcon({
69+
width: '1.5em',
70+
height: '1.5em',
71+
}),
72+
);
73+
const messageElement = toast.appendChild(document.createElement('div'));
74+
messageElement.innerText = message;
75+
const closeButton = toast.appendChild(document.createElement('button'));
76+
assignStyle(closeButton, {
77+
margin: '0',
78+
padding: '0.2rem',
79+
backgroundColor: 'transparent',
80+
color: 'white',
81+
border: 'none',
82+
});
83+
closeButton.ariaLabel = 'Close';
84+
closeButton.appendChild(
85+
closeIcon({
86+
width: '1.5em',
87+
height: '1.5em',
88+
}),
89+
);
90+
closeButton.addEventListener('click', () => {
91+
toast.remove();
92+
workspace.markFocused();
93+
});
94+
95+
workspace.getInjectionDiv().appendChild(toast);
96+
requestAnimationFrame(() => {
97+
toast.style.right = '2rem';
98+
});
99+
100+
let timeout: ReturnType<typeof setTimeout> | undefined;
101+
const setToastTimeout = () => {
102+
timeout = setTimeout(() => toast.remove(), duration);
103+
};
104+
const clearToastTimeout = () => clearTimeout(timeout);
105+
toast.addEventListener('focusin', clearToastTimeout);
106+
toast.addEventListener('focusout', setToastTimeout);
107+
toast.addEventListener('mouseenter', clearToastTimeout);
108+
toast.addEventListener('mousemove', clearToastTimeout);
109+
toast.addEventListener('mouseleave', setToastTimeout);
110+
setToastTimeout();
111+
}
112+
113+
function icon(innerHTML: string, style: Partial<CSSStyleDeclaration> = {}) {
114+
const icon = document.createElement('svg');
115+
assignStyle(icon, style);
116+
icon.innerHTML = innerHTML;
117+
icon.ariaHidden = 'hidden';
118+
return icon;
119+
}
120+
121+
function infoIcon(style: Partial<CSSStyleDeclaration> = {}) {
122+
return icon(
123+
`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
124+
<circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2"/>
125+
<rect x="11" y="9" width="2" height="9" fill="currentColor"/>
126+
<circle cx="12.0345" cy="7.03448" r="1.03448" fill="currentColor"/>
127+
</svg>`,
128+
style,
129+
);
130+
}
131+
132+
function closeIcon(style: Partial<CSSStyleDeclaration> = {}) {
133+
return icon(
134+
`<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
135+
<rect x="19.7782" y="2.80762" width="2" height="24" transform="rotate(45 19.7782 2.80762)" fill="currentColor"/>
136+
<rect x="2.80762" y="4.22183" width="2" height="24" transform="rotate(-45 2.80762 4.22183)" fill="currentColor"/>
137+
</svg>`,
138+
style,
139+
);
140+
}
141+
142+
function assignStyle(target: HTMLElement, style: Partial<CSSStyleDeclaration>) {
143+
return Object.assign(target.style, style);
144+
}

0 commit comments

Comments
 (0)