From 53fb5aead88803cf3df8cfd0765c8b38ac8d3837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vidar=20Eld=C3=B8y?= Date: Sun, 10 Sep 2023 00:06:53 +0200 Subject: [PATCH] doc: save user preference for JS flavor PR-URL: https://github.com/nodejs/node/pull/49526 Fixes: https://github.com/nodejs/node/issues/49508 Reviewed-By: Antoine du Hamel Reviewed-By: Chemi Atlow --- doc/api_assets/api.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/doc/api_assets/api.js b/doc/api_assets/api.js index f63074ef39bc80..c7568206e29f19 100644 --- a/doc/api_assets/api.js +++ b/doc/api_assets/api.js @@ -136,6 +136,29 @@ updateHashes(); } + function setupFlavorToggles() { + const kFlavorPreference = 'customFlavor'; + const flavorSetting = localStorage.getItem(kFlavorPreference) === 'true'; + const flavorToggles = document.querySelectorAll('.js-flavor-toggle'); + + flavorToggles.forEach((toggleElement) => { + toggleElement.checked = flavorSetting; + toggleElement.addEventListener('change', (e) => { + const checked = e.target.checked; + + if (checked) { + localStorage.setItem(kFlavorPreference, true); + } else { + localStorage.removeItem(kFlavorPreference); + } + + flavorToggles.forEach((el) => { + el.checked = checked; + }); + }); + }); + } + function setupCopyButton() { const buttons = document.querySelectorAll('.copy-button'); buttons.forEach((button) => { @@ -182,6 +205,8 @@ // Make link to other versions of the doc open to the same hash target (if it exists). setupAltDocsLink(); + setupFlavorToggles(); + setupCopyButton(); }