how to use refreshSelection?any example?

239 views
Skip to first unread message

陈小祥

unread,
Dec 2, 2019, 9:01:25 PM12/2/19
to Blockly
hi, i want to update flyout't content; now i use updateToolbox,it works, but i don't want to update the whole toolbox. i find the refreshSelection api,
 Blockly.Toolbox.prototype.refreshSelection = function() {
var selectedItem = this.tree_.getSelectedItem();
if (selectedItem && selectedItem.blocks) {
this.flyout_.show(selectedItem.blocks);
}
};
i can change the selecteditem.blocks and i works, but i don't how to put block into selecteditem.blocks;

Beka Westberg

unread,
Dec 3, 2019, 10:00:49 AM12/3/19
to Blockly
Hello,

If you are looking to just refresh a single category you could use the dynamic categories API. This allows you to create a function that is called whenever a category is opened. The function then returns XML defining what blocks should be shown in the category.

Here's an example from the docs of what a function might look like:
/**
 * Construct the blocks required by the flyout for the colours category.
 * @param {!Blockly.Workspace} workspace The workspace this flyout is for.
 * @return {!Array.<!Element>} Array of XML block elements.
 */

myApplication
.coloursFlyoutCallback = function(workspace) {
 
// Returns an array of hex colours, e.g. ['#4286f4', '#ef0447']
 
var colourList = myApplication.getPalette();
 
var xmlList = [];
 
if (Blockly.Blocks['colour_picker']) {
   
for (var i = 0; i < colourList.length; i++) {
     
var blockText = '<block type="colour_picker">' +
         
'<field name="COLOUR">' + colourList[i] + '</field>' +
         
'</block>';
     
var block = Blockly.Xml.textToDom(blockText);
      xmlList
.push(block);
   
}
 
}
 
return xmlList;
};

Do you think that could work for your use case?
--Beka

陈小祥

unread,
Dec 3, 2019, 7:58:12 PM12/3/19
to Blockly
I have solved it like bottom:
const dom = Blockly.Xml.textToDom(`<xml>
....
</xml>`);
this.workspace.getToolbox().tree_.getSelectedItem.push(...dom.children);
this.workspace.getToolbox().refreshSelection();

在 2019年12月3日星期二 UTC+8上午10:01:25,陈小祥写道:

Ray gautam

unread,
Dec 4, 2019, 2:44:04 AM12/4/19
to Blockly
there is a method called updateToolBox()  you can use that too

陈小祥

unread,
Dec 11, 2019, 3:40:12 AM12/11/19
to Blockly
updateToolbox will reset my category status, such as, i have open some category, when i use updateToolbox, all the categories are closed; How can i do for this case?

在 2019年12月4日星期三 UTC+8下午3:44:04,Ray gautam写道:

Beka Westberg

unread,
Dec 11, 2019, 10:19:02 AM12/11/19
to Blockly
Hello,

As far as I can tell, there is currently no public method for opening a specific category programmatically. I also cannot find a method for getting the currently open category.

There is a selectFirstCategory function, but that does not really handle your case :/

I think this is a good opportunity for a feature request. Or even a pull request!

I'm sorry I cannot be more helpful :/
--Beka

epas...@google.com

unread,
Dec 11, 2019, 5:16:36 PM12/11/19
to Blockly
Filing a feature request is probably a good idea. The current toolbox is mostly a copy of the Closure UI components we were using previously and is on the list for additional work, so getting feature requests for what folks would like will help with that process.

For the specific issue, it looks like renderTree is where the toolbox gets recreated. It might be worth adding some code there to try and persist the open category if it still exists in the new list of categories.

Cheers,
Erik

陈小祥

unread,
Dec 11, 2019, 7:52:00 PM12/11/19
to Blockly
ok, thanks for you replay, i'll have a try to understand the method renderTree and to persist the open category.

在 2019年12月12日星期四 UTC+8上午6:16:36,Erik Pasternak写道:
Reply all
Reply to author
Forward
0 new messages