Hello, I want to create a widget, while most of it will be written in lowercase, sometimes I need it in uppercase.
Is there similar way $item.toLocaleUpperCase() but in widget?
Example case of usage (simplified version, since this is just a sample):
Is there a reason this has to be a widget? I know you said this is a simplified example, but as it stands this looks like it would be easier to do with just variables.
I’m just curious if it is possible to add uppercase to widget or how much flexible widget is.
Since I don’t know much, I want to know the limit of using widget/when to use it/when I shouldn’t use it. (note: I knew widget is like creating template/macro code so far from my previous question).
It’s an interesting question, because to do it automatically would require the widget to know that it had been called at the start of a sentence, and as far as I know the widget has no access to the text surrounding it in the passage.
So to do what you are suggesting you’d either have to:
Add another argument to the widget to tell it to make itself upper-case (e.g. <<item "sword" true>>
Use a second widget to do the case-adjustment on its contents, e.g. (<<ufirst>><<item "sword">><</ufirst>>
If you look at @Chapel’s pronoun templates (not widgets, but the same idea) he’s had to make a ?He and a ?he to get around this, but widget names are not (I think) case sensitive, so you couldn’t make an <<item>> and an <<Item>> anyway.
Something else you could do is use a Javascript function that returns a string, then use .toLocaleUpperFirst() on the string it returns. Put this in your Javascript section:
setup.item = function(){
if (State.variables.item == "knight"){
return "sword";
}
else if (State.variables.item == "knight"){
return "staff";
}
}
If the purpose of your <<item>> widget is to display a specific word based on the current value of a Story variable, then that outcome is better achieved using a Template, especially if there are times when you want the returned word to be proper-cased.
1: Define the collection of key & value pairs that will be needed.
The following code needs to be added to your project’s Story JavaScript area. It defines a property on the special setup object, to contain the list of ‘items’ and their associated ‘word’ values.
The following code also needs to be added to the project’s Story JavaScript area. It defines a Template named item that has an alias of Item. The later will be used to indicate if the ‘word’ outputted by the template should be proper-cased or not.
The State.variables getter is use to access the $item Story Variable from JavaScript, and the <String>.toUpperFirst() method is used to upper-case the first letter of the word being returned if needed.
Template.add(['item', 'Item'], function () {
let word = setup.items[State.variables.item];
return this.name === 'Item' ? word.toUpperFirst() : word;
});
3: Using the template
note that the first line is using the template’s name, because the all lower-case variant of the associated ‘word’ is needed. And the second line is using the template’s alias, because the proper-case variant of the associated ‘word’ is needed
You decide to pick your ?item.
?Item are your favourite item