Skip to content

Document Extension.buildTooltipForDropdown Function. #2794

@BeksOmega

Description

@BeksOmega

Where

Maybe as a subsection to extensions.

What

  • Text
  • Image or Gif
  • Other: Code

Old content

Nothin'

Suggested content

The Extensions namespace also provides a convenience method for building an extension function that will map a dropdown value to a tooltip string.

This method includes multiple checks to ensure tooltips, dropdown options, and message references are aligned. This aims to catch errors as early as possible, without requiring developers to manually test tooltips under each option. After the page is loaded, each tooltip text string will be checked for matching message keys in the internationalized string table. Deferring this until the page is loaded decouples loading dependencies. Later, upon loading the first block of any given type, the extension will validate every dropdown option has a matching tooltip in the lookupTable. Errors are reported as warnings in the console, and are never fatal.

First create a table that maps dropdown options (the language-neutral keys not the human-readable text) to string table message keys:

Blockly.Constants.Math.TOOLTIPS_BY_OP = {
  'ADD': '%{BKY_MATH_ARITHMETIC_TOOLTIP_ADD}',
  'MINUS': '%{BKY_MATH_ARITHMETIC_TOOLTIP_MINUS}',
  'MULTIPLY': '%{BKY_MATH_ARITHMETIC_TOOLTIP_MULTIPLY}',
  'DIVIDE': '%{BKY_MATH_ARITHMETIC_TOOLTIP_DIVIDE}',
  'POWER': '%{BKY_MATH_ARITHMETIC_TOOLTIP_POWER}',
  // etc...
};

And then register it like so:

Blockly.Extensions.register('math_op_tooltip',
    Blockly.Extensions.buildTooltipForDropdown(
        'OP', Blockly.Constants.Math.TOOLTIPS_BY_OP));

Additional context

I grabbed the majority of the text directly from here. It should probably be replaced with a link to the online documentation if this gets added.

I think this is a really cool hidden function of Blockly. It's not /necessary/ to document it, but I think it'd be great if more people knew about it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    issue: docsDescribes missing or incorrect documentation

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions