Formatter API

triangle-exclamation

Implementation of a new formatter

After creating a new formatter in the workspace, a text editor with the formatter implementation template will open.

The formatter is represented as a JavaScript function with the signature initially provided in the template:

function formatText(input) {
         return { 
                formattedText: input.textToFormat,
                cursorPosition: input.cursorPosition 
          };
}

Description of the formatter function signature:

Parameter name
Type
Type
Description

input.textToFormat

input

String (JAVA)

Text to be formatted. You must convert the JAVA type of the variable to the type JavaScript (new String(input.textToFormat); ) in order to call methods for the type String (e.g. replace())

input.locale

input

String (JAVA)

A string specifying the language in which the application is displayed (e.g. "pl", "en"). You must convert the JAVA type of the variable to the type JavaScript (new String(input.locale); ) in order to call methods for the type String (e.g. replace())

input.cursorPosition

input

int

Cursor position in the text field before formatting

output.formattedText

output

String

Text after formatting

output.cursorPosition

output

String

Cursor position in the text field after formatting - an integer in the range <0,output.formattedText.length> - if a number outside the range is provided, the webforms application will not change the cursor position (the browser's default behavior will be preserved)

Formatter implementation should be done by changing only the body of the function.

For proper operation, the formatter must be a valid function in the JavaScript. Otherwise, when attempting to save the formatter in the current user workspace, an appropriate system error message will be generated.

chevron-rightExample formatter code (adds a thousands separator to a number and always places the cursor at the end):hashtag

Version with the caret positioned at the natural position (taking thousand separators into account). The formatter below will not work for a TextField of type EMAIL due to JavaScript limitations (input type="email" does not implement retrieving the caret position).

chevron-rightVersion with the caret positioned at the natural positionhashtag

Handling formatters according to the previous template

For formatters created according to the previous template shown below, the cursor after formatting the text will be placed at the end if it was at the end before formatting. If the cursor was in the body of the text, then the cursor in the field after formatting will be placed in the same position as it was before formatting.

Description of the previous formatter function signature:

Parameter name
Type
Type
Description

textToFormat

input

String

Text to be formatted

locale

input

String

A string specifying the language in which the application is displayed (e.g. "pl", "en")

formattedText

output

String

Text after formatting

Limitations

  • Formatters cannot use prototype inheritance mechanisms

  • In formatters that format outputs from the object service, the objects passed to the formatter are Java objects; to perform operations on strings, they must be converted to JavaScript objects (e.g.: const textToFormat = new String(input.textToFormat + ""))

circle-info

Demo application: demoFormatery

Last updated

Was this helpful?