forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorial.tsx
More file actions
335 lines (284 loc) · 14 KB
/
tutorial.tsx
File metadata and controls
335 lines (284 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/// <reference path="../../built/pxtlib.d.ts" />
import * as React from "react";
import * as ReactDOM from "react-dom";
import * as data from "./data";
import * as sui from "./sui";
import * as sounds from "./sounds";
import * as core from "./core";
import * as md from "./marked";
import * as compiler from "./compiler";
type ISettingsProps = pxt.editor.ISettingsProps;
/**
* We'll run this step when we first start the tutorial to figure out what blocks are used so we can
* filter the toolbox.
*/
export function getUsedBlocksAsync(tutorialId: string, tutorialmd: string): Promise<pxt.Map<number>> {
const code = pxt.tutorial.bundleTutorialCode(tutorialmd);
return Promise.resolve()
.then(() => {
if (code == '') return Promise.resolve({});
const usedBlocks: pxt.Map<number> = {};
return compiler.getBlocksAsync()
.then(blocksInfo => compiler.decompileSnippetAsync(code, blocksInfo))
.then(blocksXml => {
if (blocksXml) {
const headless = pxt.blocks.loadWorkspaceXml(blocksXml);
const allblocks = headless.getAllBlocks();
for (let bi = 0; bi < allblocks.length; ++bi) {
const blk = allblocks[bi];
usedBlocks[blk.type] = 1;
}
return usedBlocks;
} else {
throw new Error("Empty blocksXml, failed to decompile");
}
}).catch(() => {
pxt.log(`Failed to decompile tutorial: ${tutorialId}`);
throw new Error(`Failed to decompile tutorial: ${tutorialId}`);
})
});
}
export class TutorialMenuItem extends data.Component<ISettingsProps, {}> {
constructor(props: ISettingsProps) {
super(props);
this.openTutorialStep = this.openTutorialStep.bind(this);
}
openTutorialStep(step: number) {
let options = this.props.parent.state.tutorialOptions;
options.tutorialStep = step;
pxt.tickEvent(`tutorial.step`, { tutorial: options.tutorial, step: step }, { interactiveConsent: true });
this.props.parent.setTutorialStep(step);
}
renderCore() {
const { tutorialReady, tutorialStepInfo, tutorialStep } = this.props.parent.state.tutorialOptions;
const currentStep = tutorialStep;
if (!tutorialReady) return <div />;
function intermediateClassName(index: number) {
if (tutorialStepInfo.length < 8 // always show first 8
|| index == 0 // always show first
|| index == tutorialStepInfo.length - 1 // always show last
|| Math.abs(index - currentStep) < 2 // 1 around current step
) return "";
return "mobile hide";
}
return <div className="ui item">
<div className="ui item tutorial-menuitem" role="menubar">
{tutorialStepInfo.map((step, index) =>
(index == currentStep) ?
<span className="step-label" key={'tutorialStep' + index}>
<TutorialMenuItemLink index={index}
className={`ui circular label ${currentStep == index ? 'blue selected' : 'inverted'} ${!tutorialReady ? 'disabled' : ''}`}
ariaLabel={lf("Tutorial step {0}. This is the current step", index + 1)}
onClick={this.openTutorialStep}>{index + 1}</TutorialMenuItemLink>
</span> :
<span className={`ui step-label ${intermediateClassName(index)}`} key={'tutorialStep' + index} data-tooltip={`${index + 1}`} data-inverted="" data-position="bottom center">
<TutorialMenuItemLink index={index}
className={`ui empty circular label ${!tutorialReady ? 'disabled' : ''} clear`}
ariaLabel={lf("Tutorial step {0}", index + 1)}
onClick={this.openTutorialStep} />
</span>
)}
</div>
</div>;
}
}
interface TutorialMenuItemLinkProps {
index: number;
className: string;
ariaLabel: string;
onClick: (index: number) => void;
}
export class TutorialMenuItemLink extends data.Component<TutorialMenuItemLinkProps, {}> {
handleClick = () => {
this.props.onClick(this.props.index);
}
renderCore() {
const { className, ariaLabel, index } = this.props;
return <a className={className} role="menuitem" aria-label={ariaLabel} tabIndex={0} onClick={this.handleClick} onKeyDown={sui.fireClickOnEnter}>
{this.props.children}
</a>;
}
}
export interface TutorialHintState {
visible: boolean;
}
export class TutorialHint extends data.Component<ISettingsProps, TutorialHintState> {
constructor(props: ISettingsProps) {
super(props);
}
showHint() {
this.setState({ visible: true })
}
renderCore() {
const { visible } = this.state;
const options = this.props.parent.state.tutorialOptions;
const { tutorialReady, tutorialStepInfo, tutorialStep, tutorialName } = options;
if (!tutorialReady) return <div />;
const step = tutorialStepInfo[tutorialStep];
const tutorialHint = step.contentMd;
const tutorialFullscreen = step.fullscreen;
const tutorialUnplugged = !!step.unplugged && tutorialStep < tutorialStepInfo.length - 1;
const header = tutorialFullscreen ? tutorialName : lf("Hint");
const hide = () => this.setState({ visible: false });
const next = () => {
hide();
const nextStep = tutorialStep + 1;
options.tutorialStep = nextStep;
pxt.tickEvent(`tutorial.hint.next`, { tutorial: options.tutorial, step: nextStep });
this.props.parent.setTutorialStep(nextStep);
}
const isRtl = pxt.Util.isUserLanguageRtl();
const actions: sui.ModalButton[] = [{
label: lf("Ok"),
onclick: tutorialUnplugged ? next : hide,
icon: 'check',
className: 'green'
}]
return <sui.Modal isOpen={visible} className="hintdialog"
closeIcon={true} header={header} buttons={actions}
onClose={tutorialUnplugged ? next : hide} dimmer={true} longer={true}
closeOnDimmerClick closeOnDocumentClick closeOnEscape>
<md.MarkedContent markdown={tutorialHint} parent={this.props.parent} />
</sui.Modal>;
}
}
interface TutorialCardState {
popout?: boolean;
}
export class TutorialCard extends data.Component<ISettingsProps, TutorialCardState> {
public focusInitialized: boolean;
constructor(props: ISettingsProps) {
super(props);
this.state = {
}
this.showHint = this.showHint.bind(this);
this.closeLightbox = this.closeLightbox.bind(this);
this.tutorialCardKeyDown = this.tutorialCardKeyDown.bind(this);
this.okButtonKeyDown = this.okButtonKeyDown.bind(this);
this.previousTutorialStep = this.previousTutorialStep.bind(this);
this.nextTutorialStep = this.nextTutorialStep.bind(this);
this.finishTutorial = this.finishTutorial.bind(this);
}
previousTutorialStep() {
let options = this.props.parent.state.tutorialOptions;
const currentStep = options.tutorialStep;
const previousStep = currentStep - 1;
options.tutorialStep = previousStep;
pxt.tickEvent(`tutorial.previous`, { tutorial: options.tutorial, step: previousStep }, { interactiveConsent: true });
this.props.parent.setTutorialStep(previousStep);
}
nextTutorialStep() {
let options = this.props.parent.state.tutorialOptions;
const currentStep = options.tutorialStep;
const nextStep = currentStep + 1;
options.tutorialStep = nextStep;
pxt.tickEvent(`tutorial.next`, { tutorial: options.tutorial, step: nextStep }, { interactiveConsent: true });
this.props.parent.setTutorialStep(nextStep);
}
finishTutorial() {
this.closeLightbox();
this.props.parent.completeTutorial();
}
private closeLightboxOnEscape = (e: KeyboardEvent) => {
const charCode = core.keyCodeFromEvent(e);
if (charCode === 27) {
this.closeLightbox();
}
}
setPopout() {
this.setState({ popout: true });
}
private closeLightbox() {
sounds.tutorialNext();
document.documentElement.removeEventListener("keydown", this.closeLightboxOnEscape);
// Hide lightbox
this.props.parent.hideLightbox();
this.setState({ popout: false });
}
componentWillUpdate() {
document.documentElement.addEventListener("keydown", this.closeLightboxOnEscape);
}
private tutorialCardKeyDown(e: KeyboardEvent) {
const charCode = core.keyCodeFromEvent(e);
if (charCode == core.TAB_KEY) {
e.preventDefault();
const tutorialOkRef = this.refs["tutorialok"] as sui.Button;
const okButton = ReactDOM.findDOMNode(tutorialOkRef) as HTMLElement;
okButton.focus();
}
}
private okButtonKeyDown(e: KeyboardEvent) {
const charCode = core.keyCodeFromEvent(e);
if (charCode == core.TAB_KEY) {
e.preventDefault();
const tutorialCard = this.refs['tutorialmessage'] as HTMLElement;
tutorialCard.focus();
}
}
componentDidUpdate(prevProps: ISettingsProps, prevState: TutorialCardState) {
const tutorialCard = this.refs['tutorialmessage'] as HTMLElement;
const tutorialOkRef = this.refs["tutorialok"] as sui.Button;
const okButton = ReactDOM.findDOMNode(tutorialOkRef) as HTMLElement;
if (prevState.popout != this.state.popout && this.state.popout) {
// Setup focus trap around the tutorial card and the ok button
tutorialCard.addEventListener('keydown', this.tutorialCardKeyDown);
okButton.addEventListener('keydown', this.okButtonKeyDown);
tutorialCard.focus();
} else if (prevState.popout != this.state.popout && !this.state.popout) {
// Unregister event handlers
tutorialCard.removeEventListener('keydown', this.tutorialCardKeyDown);
okButton.removeEventListener('keydown', this.okButtonKeyDown);
tutorialCard.focus();
}
}
componentWillUnmount() {
// Clear the markdown cache when we unmount
md.MarkedContent.clearBlockSnippetCache();
}
private hasHint() {
const options = this.props.parent.state.tutorialOptions;
const { tutorialReady, tutorialStepInfo, tutorialStep } = options;
if (!tutorialReady) return false;
return tutorialStepInfo[tutorialStep].hasHint;
}
showHint() {
if (!this.hasHint()) return;
this.closeLightbox();
this.props.parent.showTutorialHint();
}
renderCore() {
const options = this.props.parent.state.tutorialOptions;
const { tutorialReady, tutorialStepInfo, tutorialStep } = options;
if (!tutorialReady) return <div />
const tutorialCardContent = tutorialStepInfo[tutorialStep].headerContentMd;
let tutorialAriaLabel = '';
const currentStep = tutorialStep;
const maxSteps = tutorialStepInfo.length;
const hasPrevious = tutorialReady && currentStep != 0;
const hasNext = tutorialReady && currentStep != maxSteps - 1;
const hasFinish = currentStep == maxSteps - 1;
const hasHint = this.hasHint();
if (hasHint) {
tutorialAriaLabel += lf("Press Space or Enter to show a hint.");
}
const isRtl = pxt.Util.isUserLanguageRtl();
return <div id="tutorialcard" className={`ui ${tutorialReady ? 'tutorialReady' : ''}`} >
<div className='ui buttons'>
{hasPrevious ? <sui.Button icon={`${isRtl ? 'right' : 'left'} chevron`} className={`prevbutton left attached green ${!hasPrevious ? 'disabled' : ''}`} text={lf("Back")} textClass="landscape only" ariaLabel={lf("Go to the previous step of the tutorial.")} onClick={this.previousTutorialStep} onKeyDown={sui.fireClickOnEnter} /> : undefined}
<div className="ui segment attached tutorialsegment">
<div role="button" className='avatar-image' onClick={this.showHint} onKeyDown={sui.fireClickOnEnter}></div>
{hasHint ? <sui.Button className="mini blue hintbutton hidelightbox" text={lf("Hint")} tabIndex={-1} onClick={this.showHint} onKeyDown={sui.fireClickOnEnter} /> : undefined}
<div ref="tutorialmessage" className={`tutorialmessage`} role="alert" aria-label={tutorialAriaLabel} tabIndex={hasHint ? 0 : -1}
onClick={this.showHint} onKeyDown={sui.fireClickOnEnter}>
<div className="content">
<md.MarkedContent markdown={tutorialCardContent} parent={this.props.parent} />
</div>
</div>
<sui.Button ref="tutorialok" id="tutorialOkButton" className="large green okbutton showlightbox" text={lf("Ok")} onClick={this.closeLightbox} onKeyDown={sui.fireClickOnEnter} />
</div>
{hasNext ? <sui.Button icon={`${isRtl ? 'left' : 'right'} chevron`} rightIcon className={`nextbutton right attached green ${!hasNext ? 'disabled' : ''}`} text={lf("Next")} textClass="landscape only" ariaLabel={lf("Go to the next step of the tutorial.")} onClick={this.nextTutorialStep} onKeyDown={sui.fireClickOnEnter} /> : undefined}
{hasFinish ? <sui.Button icon="left checkmark" className={`orange right attached ${!tutorialReady ? 'disabled' : ''}`} text={lf("Finish")} ariaLabel={lf("Finish the tutorial.")} onClick={this.finishTutorial} onKeyDown={sui.fireClickOnEnter} /> : undefined}
</div>
</div>;
}
}