avm99963 | ea37fdf | 2021-02-03 01:27:13 +0100 | [diff] [blame] | 1 | # Add a new feature |
avm99963 | cf83759 | 2021-02-15 20:02:54 +0100 | [diff] [blame] | 2 | All the features are hidden behind a switch, so users can turn them on or off as |
| 3 | desired. This doc explains how to add this feature switch option to the options |
| 4 | page, and some guidance on where should the feature be developed. |
| 5 | |
| 6 | ## Add an option |
| 7 | Each feature has a boolean option "linked" to it, which determines whether the |
| 8 | feature will be enabled or not. |
| 9 | |
| 10 | ### How to add the feature switch option |
| 11 | 1. First of all, think of a short codename for the feature. |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 12 | 2. Modify the `//src/common/common.js` file by adding an entry in the |
| 13 | `optionsPrototype` object. |
avm99963 | cf83759 | 2021-02-15 20:02:54 +0100 | [diff] [blame] | 14 | - All features should have the `false` value set as a default, so existing |
| 15 | users have to explicitly enable the option after they receive the extension |
| 16 | update. Otherwise, it might cause confusion, because users wouldn't know if |
| 17 | the feature was added by the extension or Google. |
| 18 | 3. Now, modify the `//src/options/options.html` file by appending the following |
| 19 | HTML code in the corresponding section: |
| 20 | ``` |
| 21 | <div class="option"><input type="checkbox" id="{{codename}}"> <label for="{{codename}}" data-i18n="{{codename}}"></label> <span class="experimental-label" data-i18n="experimental_label"></span></div> |
| 22 | ``` |
| 23 | where you should substitute `{{codename}}` with the codename you've chosen. |
| 24 | The _experimental_ label is optional and should only be used with features which |
| 25 | are unreliable (or could be at some point in the future) due to their nature. |
| 26 | 4. Finally, add the option string at `//src/_locales/en/manifest.json`, by |
| 27 | adding the string under the `options_{{codename}}` key. |
| 28 | |
| 29 | ### How to add additional options for the feature |
| 30 | Apart from the feature switch option, additional options can be defined in order |
| 31 | to further customize the behavior of a feature. |
| 32 | |
| 33 | This is not usually the case, and it is preferred that the number of options is |
| 34 | kept to a minimum so the extension is as simple as possible. In any case, this |
| 35 | section explains how to add them in case they are needed. |
| 36 | |
| 37 | *** promo |
| 38 | As a source of inspiration, take a look at the Community Console dark mode |
| 39 | feature: apart from the feature switch option (`ccdarktheme`) it has 2 |
| 40 | additional options associated with it: |
| 41 | |
| 42 | - The `ccdarktheme_mode` option can be configured in the options page with a |
| 43 | selector, and determines whether the dark/light themes are set according to the |
| 44 | OS's dark mode setting, or whether the user is the one in charge of switching |
| 45 | between them by clicking a button/switch in the Community Console. |
| 46 | - Note that this option is only applied when the feature switch is turned |
| 47 | on. |
| 48 | - The `ccdarktheme_switch_enabled` option is used to save whether the user |
| 49 | manually enabled the dark theme or not by using the switch/button in the |
| 50 | Community Console. |
| 51 | - It is only applied when `ccdarktheme_mode` is set to `switch` and the |
| 52 | feature is enabled. |
| 53 | *** |
| 54 | |
| 55 | These are the steps which you should generally follow when adding additional |
| 56 | options: |
| 57 | |
| 58 | 1. Think of a codename for the additional option. It should be the feature |
| 59 | codename appended by an underscore and a suffix |
| 60 | (`{{feature_codename}}_{{suffix}}`). |
| 61 | 2. Modify the `//src/common/common.js` file by doing the following things: |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 62 | 1. Add an entry for the option in the `optionsPrototype` object. |
avm99963 | cf83759 | 2021-02-15 20:02:54 +0100 | [diff] [blame] | 63 | 2. Append the option's codename to the `specialOptions` object. This is so |
| 64 | the option can be handled in a specific way when showing/saving it in the |
| 65 | options page, or so it is handled outside of the options page. |
| 66 | 3. If you want to handle the option from the options page, follow these steps: |
| 67 | 1. Modify the `//src/options/options.html` file to add the appropriate code |
| 68 | which implements the option (usually in the same `.option` div as the |
| 69 | feature switch). |
| 70 | - Don't include text, only the HTML structure. If you add a `data-i18n` |
| 71 | attribute to an HTML element, its contents will be replaced with the |
| 72 | corresponding i18n string (for instance, |
| 73 | `<span data-i18n="test"></span>` will be rendered as |
| 74 | `<span data-i18n="test">Test</span>` if `Test` was the string defined |
| 75 | with the key `options_test`). |
| 76 | 2. Modify the `//src/options/options.js` file to add the following things: |
| 77 | 1. In the switch statement inside the save function, add a case to |
| 78 | handle saving your additional option. |
| 79 | 2. In the switch statement inside the load event listener, add another |
| 80 | case so your option is correctly set in the options page with the saved |
| 81 | value. |
| 82 | 3. Add the corresponding i18n strings at `//src/_locales/en/manifest.json`. |
| 83 | 4. If you want to handle the option outside of the options page, include an |
| 84 | empty case in both switches at `//src/options/options.js`, so the option is not |
| 85 | handled there. |
| 86 | |
| 87 | ## Develop the feature |
| 88 | TODO(issue #32): Write this section. |