2

First of all, I've asked a simpler version of this question before delving too much about it in here. However, as I searched things up, things got more complicated and I can describe it a little more.

I'm trying to create an Electron App with Google's Blockly. Renaming variables in Blockly's domain opens up a prompt in the user's browser to ask for the new variable's name, as you can see on it's own website (click on 'Count' and choose 'Rename variable...').

The problem is: Electron doesn't support window.prompt() and it doesn't do anything if I let the code as is, so, after searching a bit, I learned that you can override Blockly.prompt to use your own. My guess: so you can use an alternative to window.prompt()

I installed electron-prompt and was trying to use it to get the user's input in the new prompt window to send it back to Blockly's core functions that handle the renaming. Here's what I'm trying:

var prompt = require('electron-prompt');
var setPrompt = function()
{ 
    return prompt
    ({
        title: 'Renaming',
        label: 'Renaming variable to:',
        type: 'input'
    })
};

var getPrompt = function()
{
    return setPrompt().then(function(value){return value})
}

var promptReturn = function()
{
    return getPrompt().then(function(value){return value})
}

Blockly.prompt = function(message, defaultValue, callback)
{
    callback(promptReturn().then(function(value){return value}));
};

EDIT: Source code from electron-prompt is here and, by looking at it and information I tried to adapt from here, I changed to the code above, inferring that prompt returns a promise. However, it seems the callback in Blockly.prompt doesn't wait for the input via the modal opened in setPrompt() and throws an error, but if I use a simple function just returning a string in callback, it works as intended...

Now I'm confused if it's about myself still using Promises wrong or if that callback in Blockly.prompt doesn't support "waiting for promises"...

Hope this helps explain what I've tried using after looking up more information about this problem.

4

1 回答 1

5

我设法解决了它,我发布它以防有人需要关于Blockly.prompt覆盖的明确答案。似乎callback覆盖时的函数必须返回新变量的字符串,所以我尝试promise在覆盖中使用并调用一个简单的函数来应用then()如下的值:

var prompt = require('electron-prompt');
var renameVar = function(name)
{
    return name;
}

Blockly.prompt = function(msg, defaultValue, callback)
{
    prompt
    ({
        title: 'Renaming',
        label: 'Renaming variable to:',
        type: 'input'
    }).then((name)=>{callback(renameVar(name))})
}
于 2018-03-24T06:44:40.343 回答