forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorialCodeValidation.tsx
More file actions
142 lines (123 loc) · 6.14 KB
/
tutorialCodeValidation.tsx
File metadata and controls
142 lines (123 loc) · 6.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
/// <reference path="../../built/pxtlib.d.ts" />
import * as React from "react";
import * as data from "./data";
import * as sui from "./sui";
import * as compiler from "./compiler";
type ISettingsProps = pxt.editor.ISettingsProps;
interface TutorialCodeValidationProps extends ISettingsProps {
onYesButtonClick: () => void;
onNoButtonClick: () => void;
validationTelemetry: (command: string) => void;
initialVisible: boolean;
isTutorialCodeInvalid: boolean;
ruleComponents: pxt.tutorial.TutorialRuleStatus[];
areStrictRulesPresent: boolean;
}
interface TutorialCodeValidationState {
visible: boolean;
ruleBlocks?: pxt.Map<string[]>;
}
export class ShowValidationMessage extends data.Component<TutorialCodeValidationProps, TutorialCodeValidationState> {
protected renderedBlockCache: pxt.Map<string> = {};
constructor(props: TutorialCodeValidationProps) {
super(props);
this.state = { visible: this.props.initialVisible, ruleBlocks: {} };
}
showTutorialValidationMessage(vis: boolean) {
this.setState({ visible: vis });
}
moveOnToNextTutorialStep() {
this.props.validationTelemetry("continue");
this.props.onYesButtonClick();
this.showTutorialValidationMessage(false);
}
stayOnThisTutorialStep() {
this.props.validationTelemetry("edit");
this.props.onNoButtonClick();
}
componentDidMount() {
this.props.ruleComponents.forEach(rule => {
if (rule.blockIds && !this.state.ruleBlocks?.[rule.ruleName]?.length) {
this.getRuleBlocksAsync(rule);
}
})
}
UNSAFE_componentWillReceiveProps(nextProps: TutorialCodeValidationProps, nextState: TutorialCodeValidationState) {
nextProps.ruleComponents.forEach(rule => {
const prev = this.props.ruleComponents.find(r => r.ruleName == rule.ruleName);
// Update if the list of blockids is different
if ((rule.blockIds && rule.blockIds.sort().toString() != prev?.blockIds?.sort()?.toString())
|| rule.blockIds?.length != nextState.ruleBlocks?.[rule.ruleName]?.length) {
this.getRuleBlocksAsync(rule);
}
})
}
renderRule(rule: pxt.tutorial.TutorialRuleStatus, index?: number) {
const blockUris = this.state.ruleBlocks?.[rule.ruleName];
if (!blockUris) {
return <p key={index + rule.ruleName}>{(rule.ruleTurnOn && !rule.ruleStatus) ? rule.ruleMessage : ''}</p>
} else {
return <div>
<div className="codeValidationPopUpText">{rule.ruleTurnOn && !rule.ruleStatus ? rule.ruleMessage : ''}</div>
<div className="validationRendering">
{blockUris.map((blockUri, index) => <div> <img key={index + blockUri} src={blockUri} alt="block rendered" /></div>)}
</div>
</div>
}
}
getRuleBlocksAsync(rule: pxt.tutorial.TutorialRuleStatus) {
if (rule.blockIds) {
// Get all APIs from compiler
compiler.getBlocksAsync().then(blocksInfo => {
return Promise.all(rule.blockIds.map(id => {
if (this.renderedBlockCache[id]) return Promise.resolve(this.renderedBlockCache[id]);
const symbol = blocksInfo.blocksById[id];
if (!symbol) return Promise.resolve(undefined);
// Render toolbox block from symbol info
const fn = pxt.blocks.blockSymbol(id);
if (fn) {
const comp = pxt.blocks.compileInfo(fn);
const blockXml = pxt.blocks.createToolboxBlock(blocksInfo, fn, comp);
const svg = pxt.blocks.render(`<xml xmlns="https://developers.google.com/blockly/xml">${blockXml.outerHTML}</xml>`,
{ snippetMode: true, splitSvg: false }) as SVGSVGElement;
const viewBox = svg.getAttribute("viewBox").split(/\s+/).map(d => parseInt(d));
return pxt.blocks.layout.blocklyToSvgAsync(svg, viewBox[0], viewBox[1], viewBox[2], viewBox[3])
.then(sg => {
if (!sg) return Promise.resolve<string>(undefined);
return pxt.BrowserUtils.encodeToPngAsync(sg.xml, { width: sg.width, height: sg.height, pixelDensity: 1 });
})
}
return Promise.resolve(undefined)
})).then(blockUris => {
this.setState({
ruleBlocks: {
...this.state.ruleBlocks,
[rule.ruleName]: blockUris.filter(b => !!b)
}
});
})
})
}
}
renderCore() {
const codeInvalid = this.props.isTutorialCodeInvalid;
const rules = this.props.ruleComponents;
const rulesDefined = (rules != undefined);
const strictRulePresent = this.props.areStrictRulesPresent;
return <div>
{this.state.visible && <div className="mask" role="region" onClick={this.props.onNoButtonClick} />}
<div className={`tutorialCodeValidation no-select ${(!codeInvalid || (codeInvalid && !strictRulePresent)) ? 'hidden' : ''}`}>
<div>
{rulesDefined && rules.map((rule, index) => this.renderRule(rule, index))}
</div>
<div className="codeValidationPopUpText">
{lf("Do you still want to continue?")}
</div>
<div className="moveOnButtons">
<sui.Button className="yes" ariaLabel={lf("yes button for tutorial code validation")} onClick={this.moveOnToNextTutorialStep.bind(this)} onKeyDown={sui.fireClickOnEnter} > {lf("Continue Anyway")} </sui.Button>
<sui.Button className="no" ariaLabel={lf("no button for tutorial code validation")} onClick={this.stayOnThisTutorialStep.bind(this)} onKeyDown={sui.fireClickOnEnter} > {lf("Keep Editing")} </sui.Button>
</div>
</div>
</div>;
}
}