Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

filed label truncation

43 views
Skip to first unread message

Sahithi Mangena

unread,
Dec 27, 2024, 4:54:34 AM12/27/24
to blo...@googlegroups.com
Hello,

Field label text is not getting truncated over more characters. So we tried to override the maxdispalylength property of Fieldlabel but the overridden is only getting applied when we are saving the block with some text but while rendering the blocks on load the truncation is not getting applied to the field label. So to get applied with the maxdispalylength everytime for a fieldLabel what are the things we need to do. 
Currently we registered one custom like below.
mport * as Blockly from 'blockly';

export class TruncatedFieldLabel extends Blockly.FieldLabel {
  private _fullText: string;
 override maxDisplayLength = 50;
  constructor(value: string, maxDisplayLength?: number) {
    super(value);
    this.maxDisplayLength = 50;
  }

  static override fromJson(options: any): TruncatedFieldLabel {
    return new TruncatedFieldLabel(options.text, options.maxLength || 100);
  }

And in blocks we did
 this.block.appendDummyInput("RHS_VALUE")
   .appendField(new TruncatedFieldLabel(this.visibilename,50), "visible_value");

But as is mentioned this is only getting applied when we directly change or save the text but while rendering the blocks this truncation is not getting applied. So it gets applied everytime what are the things we need to change? Please help us here.

Thanks and regards 
sahithi


Confidentiality Warning:
This message and any attachments are intended only for the use of the intended recipient(s), are confidential, and may be privileged. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution, or other use of this message and any attachments is strictly prohibited. If received in error, please notify the sender immediately and permanently delete it.

Mark Friedman

unread,
Dec 27, 2024, 2:02:23 PM12/27/24
to blo...@googlegroups.com
Hi, Sahithi!  I haven't tried it, but I suspect that you might want to extend FieldLabelSerializable rather than FieldLabel.

Hope this helps.

-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/CAKjwci%3D_53uPwz-usaMDk9TR5K604gjZwHNprACQd4oMZ%3DnnNA%40mail.gmail.com.

Sahithi Mangena

unread,
Jan 3, 2025, 1:59:42 AMJan 3
to blo...@googlegroups.com
Hello ,

I have tried the export class TruncatedFieldLabel extends Blockly.FieldLabelSerializable but the issue persists. If you see there is a recent change with the maxdisplaylength property for the field label 
truncation removed truncation removed by changing the maxdisplaylength to infinite. So we want to override this so that our field label can have truncation after certain characters. Need suggestions on how we can achieve this. 

Thanks and regards
sahithi

Mark Friedman

unread,
Jan 3, 2025, 5:42:19 PMJan 3
to blo...@googlegroups.com
Sahithi,

Code like the following appears to work for me:

export class TruncatedFieldLabel extends FieldLabelSerializable {
    // private _fullText: string;
    override maxDisplayLength = 50;

    constructor(value: string, maxDisplayLength: number, config?: FieldLabelConfig) {
        super(value, undefined, config);
        this.maxDisplayLength = maxDisplayLength;

    }

    static override fromJson(options: any): TruncatedFieldLabel {
        return new TruncatedFieldLabel(options.text, options.maxLength || 100, options);
    }
}

Blocks['trunk_label'] = {
    init: function () {
        this.appendDummyInput("RHS_VALUE")
            .appendField(new TruncatedFieldLabel(this.visibilename,10), "visible_value");
        this.getField("visible_value").setValue('foobar12345');
    }
}

I can create a trunk_label block, set its value to a string with greater length than specified in the field's maximum value and see that it is truncated.  I can also save the block to JSON/XML and reload it and see that it is still truncated.  I can programmatically reset the field to a shorter value and see that it is not truncated.

Hope this helps.

-Mark


Sahithi Mangena

unread,
Jan 6, 2025, 1:17:26 AMJan 6
to blo...@googlegroups.com
Hello Mark,

For me all the solutions are working when I am saving the block with the text but when I reload the blocks again from the xml the truncation is not working for the block again. 

thanks and regards,
sahithi

Mark Friedman

unread,
Jan 6, 2025, 2:05:53 PMJan 6
to blo...@googlegroups.com
Sahithi,

  Can you post, or provide a link to, your code that isn't working (including the saving and loading code), because I retested my code and it handles loading from XML just fine.

Also, here is a cleaned up and more complete version of the code that is working for me:

// Field definition

**
 * Config options for the label field.
 */
export interface TruncatedFieldLabelConfig extends FieldLabelConfig {
    maxDisplayLength?: number;
}

/**
 * fromJson config options for the label field.
 */
export interface TruncatedFieldLabelFromJsonConfig extends FieldLabelFromJsonConfig {
    maxDisplayLength?: number;

}

export class TruncatedFieldLabel extends FieldLabelSerializable {
    // private _fullText: string;
    override maxDisplayLength = 50;

    constructor(value: string, maxDisplayLength?: number, config?: TruncatedFieldLabelConfig) {
        super(value, undefined, config);
        if (maxDisplayLength) {
            this.maxDisplayLength = maxDisplayLength;
        }
    }

    static override fromJson(options: TruncatedFieldLabelFromJsonConfig): TruncatedFieldLabel {
        const text = parsing.replaceMessageReferences(options.text);
        return new this(text, options.maxDisplayLength, options);
    }
}

// Block definition

Blocks['trunk_label'] = {
    init: function () {
        this.appendDummyInput("RHS_VALUE")
            .appendField(new TruncatedFieldLabel(this.visibilename,10), "visible_value");
        this.getField("visible_value").setValue('foobar12345');
    }
}
-Mark


Reply all
Reply to author
Forward
0 new messages