Skip to content

Commit ec585b0

Browse files
authored
Update blocks.jsx
1 parent 5647336 commit ec585b0

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

src/containers/blocks.jsx

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class Blocks extends React.Component {
137137
'handleEnableProcedureReturns'
138138
]);
139139
this.ScratchBlocks.prompt = this.handlePromptStart;
140+
this.ScratchBlocks.customPrompt = this.handleCustomPrompt;
140141
this.ScratchBlocks.statusButtonCallback = this.handleConnectionModalStart;
141142
this.ScratchBlocks.recordSoundCallback = this.handleOpenSoundRecorder;
142143

@@ -609,6 +610,43 @@ class Blocks extends React.Component {
609610
p.prompt.showCloudOption = (optVarType === this.ScratchBlocks.SCALAR_VARIABLE_TYPE) && this.props.canUseCloud;
610611
this.setState(p);
611612
}
613+
handleCustomPrompt (title, scale, enterInfo, closeInfo) {
614+
const isObject = (value) => typeof value === 'object' && !Array.isArray(value);
615+
let needsExit = false;
616+
const exitFunc = (message) => {
617+
needsExit = true;
618+
console.error(message);
619+
};
620+
621+
/* validate arguments */
622+
if (isObject(scale)) {
623+
if (!scale.width || !scale.height) exitFunc("Custom Modal -- Missing width/height number property in Param 2");
624+
} else {
625+
exitFunc("Custom Modal -- Param 2 must be a object with 'width' and 'height' number properties");
626+
}
627+
if (isObject(enterInfo)) {
628+
if (!scale.name || !scale.callback) exitFunc("Custom Modal -- Missing width/height number property in Param 3");
629+
if (scale.callback && typeof scale.callback !== 'function') exitFunc("Custom Modal -- callback property in Param 3 must be a function");
630+
} else {
631+
exitFunc("Custom Modal -- Param 3 must be a object with properties: 'name' (string) and 'callback' (function)");
632+
}
633+
if (isObject(closeInfo)) {
634+
if (!scale.name || !scale.callback) exitFunc("Custom Modal -- Missing width/height number property in Param 4");
635+
if (scale.callback && typeof scale.callback !== 'function') exitFunc("Custom Modal -- callback property in Param 4 must be a function");
636+
} else {
637+
exitFunc("Custom Modal -- Param 4 must be a object with properties: 'name' (string) and 'callback' (function)");
638+
}
639+
if (needsExit) return;
640+
641+
this.setState({prompt: {
642+
isCustom: true,
643+
title,
644+
width: Number(scale.width), height: Number(scale.height),
645+
enterInfo, closeInfo
646+
});
647+
648+
return document.querySelector(`div[class="ReactModalPortal"] div[class*="prompt_body_"]`);
649+
}
612650
handleConnectionModalStart (extensionId) {
613651
this.props.onOpenConnectionModal(extensionId);
614652
}
@@ -625,13 +663,19 @@ class Blocks extends React.Component {
625663
* to the variable validation prompt callback used in scratch-blocks.
626664
*/
627665
handlePromptCallback (input, variableOptions) {
666+
if (this.state.prompt.isCustom) {
667+
this.state.prompt.enterInfo.callback();
668+
this.setState({prompt: null});
669+
return;
670+
}
628671
this.state.prompt.callback(
629672
input,
630673
this.props.vm.runtime.getAllVarNamesOfType(this.state.prompt.varType),
631674
variableOptions);
632675
this.handlePromptClose();
633676
}
634677
handlePromptClose () {
678+
if (this.state.prompt.isCustom) this.state.prompt.closeInfo.callback();
635679
this.setState({prompt: null});
636680
}
637681
handleCustomProceduresClose (data) {
@@ -687,7 +731,19 @@ class Blocks extends React.Component {
687731
onDrop={this.handleDrop}
688732
{...props}
689733
/>
690-
{this.state.prompt ? (
734+
{this.state.prompt ? this.state.prompt.isCustom ? (
735+
<Prompt
736+
isCustom={this.state.prompt.isCustom}
737+
width={this.state.prompt.width}
738+
height={this.state.prompt.height}
739+
title={this.state.prompt.title}
740+
enterTitle={this.state.prompt.enterInfo.name}
741+
closeTitle={this.state.prompt.closeInfo.name}
742+
vm={vm}
743+
onCancel={this.handlePromptClose}
744+
onOk={this.handlePromptCallback}
745+
/>
746+
) : (
691747
<Prompt
692748
defaultValue={this.state.prompt.defaultValue}
693749
isStage={vm.runtime.getEditingTarget().isStage}

0 commit comments

Comments
 (0)