insertText lifecycle hook is called when text is about to be inserted
It is typically used for translation, as is the case in our i18n plugin.
string hook(string locale='en', string text)
// Let' get LOUD by turning everything into UPPERCASE pattern.on( 'insertText', (locale, text ) => text.toUpperCase() )
When we say that this hook is called when text is about to be inserted, that is a simplified view. In reality, this hook is called:
- For every string of text added to a given Point or Path
- For the combined result of these values, joined together with spaces
Let’s use an example to clarify things:
points.example .addText("seamAllowance") .addText(": 1 cm")
For the example point above, the
insertText hook will end up being called 3 times:
- First it will pass
seamAllowanceto the plugin
- Then it will pass
: 1 cmto the plugin
- Finally it will pass
seamAllowance : 1 cmto the plugin
insertText hook only run once with
Seam allowance: 1 cm would be problematic because
the seam allowance may differ, or perhaps we’re using imperial units, and so on.
Instead, you can (and should) divide your text into chunks that need translating, and chunks that do not.
This is also why we’re not inserting Seam allowance but rather seamAllowance; It is merely a key to indicate what translation we want to replace this text with.