简体   繁体   中英

User input JavaScript with Electron - alternative to Window.Prompt()

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.

I managed to solve it, I'm posting it in case anyone needs a clear answer regarding Blockly.prompt overriding. It seems the callback function when overriding must return the string of the new variable, so I tried using the promise within the overriding and calling a simple function just to apply the value of then() as follows:

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))})
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM