|
| 1 | +import { getCachedSetting } from "./settings/cacheSettings"; |
1 | 2 | import { getToggles } from "./settings/toggles";
|
2 | 3 | import {
|
3 | 4 | addWrappersFrom,
|
4 | 5 | mutationObserver,
|
5 | 6 | disconnectObservers,
|
6 | 7 | } from "./wrappers/ElementWrapperClass";
|
7 |
| -import { clearWrappersAll } from "./wrappers/wrappers"; |
| 8 | +import { |
| 9 | + showHintsAll, |
| 10 | + clearWrappersAll, |
| 11 | + hideHintsAll, |
| 12 | +} from "./wrappers/wrappers"; |
8 | 13 |
|
9 | 14 | let enabled = false;
|
10 | 15 | const config = { attributes: true, childList: true, subtree: true };
|
11 | 16 |
|
12 | 17 | export async function updateHintsEnabled() {
|
13 | 18 | const newEnabled = getToggles().computed;
|
| 19 | + const alwaysComputeHintables = getCachedSetting("alwaysComputeHintables"); |
| 20 | + |
| 21 | + // Here we assume that just one change of state takes place. That is, in the |
| 22 | + // same call to this function, either the hints have been switched or the |
| 23 | + // setting alwaysComputeHintables has changed. Not both at the same time. This |
| 24 | + // function is also called when the content script first runs. |
14 | 25 |
|
| 26 | + // 1. disabled -> enabled |
15 | 27 | if (!enabled && newEnabled) {
|
| 28 | + if (alwaysComputeHintables) { |
| 29 | + showHintsAll(); |
| 30 | + } else { |
| 31 | + await observe(); |
| 32 | + } |
| 33 | + |
| 34 | + // 2. enabled -> disabled |
| 35 | + } else if (enabled && !newEnabled) { |
| 36 | + if (alwaysComputeHintables) { |
| 37 | + hideHintsAll(); |
| 38 | + } else { |
| 39 | + disconnectObservers(); |
| 40 | + clearWrappersAll(); |
| 41 | + } |
| 42 | + |
| 43 | + // 3. !alwaysComputeHintables -> alwaysComputeHintables |
| 44 | + } else if (!enabled && alwaysComputeHintables) { |
16 | 45 | await observe();
|
17 |
| - enabled = true; |
18 |
| - } |
19 | 46 |
|
20 |
| - if (enabled && !newEnabled) { |
| 47 | + // 4. alwaysComputeHintables -> !alwaysComputeHintables |
| 48 | + } else if (!enabled && !alwaysComputeHintables) { |
21 | 49 | disconnectObservers();
|
22 | 50 | clearWrappersAll();
|
23 |
| - enabled = false; |
24 | 51 | }
|
| 52 | + |
| 53 | + enabled = newEnabled; |
25 | 54 | }
|
26 | 55 |
|
27 | 56 | export default async function observe() {
|
28 | 57 | enabled = getToggles().computed;
|
29 |
| - if (enabled) { |
| 58 | + if (enabled || getCachedSetting("alwaysComputeHintables")) { |
30 | 59 | // We observe all the initial elements before any mutation
|
31 | 60 | if (document.body) addWrappersFrom(document.body);
|
32 | 61 |
|
|
0 commit comments