Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Blocks with custom fields not adjusting to sufficient width to accommodate text when reloaded

37 views
Skip to first unread message

Chris Rowan

unread,
Nov 30, 2024, 12:00:02 PM11/30/24
to Blockly
I have a block with a custom dropdown field. The field loads data which is displayed in the block. When the block is first created, the width of the block expands to accommodate the width of the text, but when the workspace is saved, then reloaded, the block only renders at a fixed width cutting off the longer text. 

Tried to adjust block width at several points during after loading of the blocks, even adding a delay to do it a second after the workspace fully loaded, without success. Tried a mixing this listens for load completion then does something akin to this (this is a very specific version that targets the field with the widest text content), all without luck.

There must be some magical built in Blockly method to accomplish this? I just need the block to render as wide as its content...

block.adjustWidth = function() {
        if (!this.workspace || this.isLoading) return;
       
        let maxWidth = this.minWidth;
       
        this.inputList.forEach((input, i) => {
            const inputWidth = input.fieldRow.reduce((width, field) => {
                const fieldSize = field.getSize();
                // Add extra padding for path fields
                const extraPadding = field.name?.includes('META_PATH') ? 100 : 0;
                console.log(`Field ${field.name} size:`, fieldSize, 'padding:', extraPadding);
                return width + (fieldSize?.width || 0) + extraPadding;
            }, 0);
           
            maxWidth = Math.max(maxWidth, inputWidth + 50);
        });
       
        this.width = maxWidth;
        this.render();
        this.bumpNeighbours();
    };

Chris Rowan

unread,
Dec 3, 2024, 1:45:20 PM12/3/24
to Blockly
Bumping. How do I force Blockly to rerender all the blocks to the width of their content?

Mark Friedman

unread,
Dec 3, 2024, 1:56:32 PM12/3/24
to blo...@googlegroups.com
Chris,

  Your issue with the width of the block after it is restored from a save seems like either a Blockly bug or an issue with your custom field.  While it's possible that you could maybe work around the problem by manually adjusting the width, it might be better to try and figure out the root cause.  Could you provide the source code for your custom field and for a block that uses that field, so that we can take a look?  Seeing the code for your saving and restoring of the workspace might also help.

-Mark


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/blockly/6ff8a049-bece-43ea-9406-7a87eb4a29ben%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages