From d5aa013106fd87683e6c24130bcd8e75580a2605 Mon Sep 17 00:00:00 2001 From: Cell <1024@lruihao.cn> Date: Sat, 21 Mar 2026 00:47:12 +0800 Subject: [PATCH] feat(code): remember the selected language of the code toggle --- assets/js/theme.js | 20 ++++++++++++++++---- layouts/_markup/render-codeblock-toggle.html | 2 +- layouts/_partials/init/index.html | 2 +- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/assets/js/theme.js b/assets/js/theme.js index ec29497c..cbcd1848 100644 --- a/assets/js/theme.js +++ b/assets/js/theme.js @@ -644,6 +644,10 @@ class FixIt { $firstBlock.parentNode.insertBefore($container, $firstBlock); const activeTabIndex = $tabs.findIndex(tab => tab.classList.contains('active')); + const langPref = window.localStorage.getItem('config_lang_perf'); + const hasCodeToggle = $tabs.some(tab => tab.dataset.codeToggle === 'true'); + const langPrefIndex = (langPref && hasCodeToggle) ? $tabs.findIndex(tab => tab.dataset.tabTitle.toLowerCase() === langPref) : -1; + const resolvedIndex = langPrefIndex !== -1 ? langPrefIndex : activeTabIndex; const beforeTabs = $tabs[0]?.getAttribute('before_tabs'); if (beforeTabs) { const $before = document.createElement('span'); @@ -653,7 +657,7 @@ class FixIt { } $tabs.forEach(($tab, index) => { const title = $tab.dataset.tabTitle || 'Code'; - const defaultActiveTab = activeTabIndex === -1 && index === 0; + const defaultActiveTab = resolvedIndex === -1 && index === 0; // tab button const $btn = document.createElement('span'); @@ -676,6 +680,14 @@ class FixIt { // 2. switch active tab UI $items.querySelectorAll('.tab-item').forEach(b => b.classList.remove('active')); $btn.classList.add('active'); + if ($tab.dataset.codeToggle === 'true') { + window.localStorage.setItem('config_lang_perf', $tab.dataset.tabTitle.toLowerCase()); + const activeItems = document.querySelectorAll(` + .tab-item[title="${$tab.dataset.tabTitle.toLowerCase()}"]:not(.active), + .tab-item[title="${$tab.dataset.tabTitle.toUpperCase()}"]:not(.active)` + ); + Util.forEach(activeItems, t => t.click()); + } // 3. switch content $tabs.forEach(b => b.classList.remove('active')); @@ -698,7 +710,7 @@ class FixIt { $items.appendChild($btn); // move block to content - $tab.classList.toggle('active', activeTabIndex === index || defaultActiveTab); + $tab.classList.toggle('active', resolvedIndex === index || defaultActiveTab); $tab.classList.remove('is-collapsed'); $tab.classList.remove('d-none'); $tab.dataset.tabInit = 'true'; @@ -709,8 +721,8 @@ class FixIt { $container.appendChild($content); // initialize actions for the active tab - if (activeTabIndex !== -1) { - const $activeBtn = $items.querySelector(`.tab-item[data-index="${activeTabIndex}"]`); + if (resolvedIndex !== -1) { + const $activeBtn = $items.querySelector(`.tab-item[data-index="${resolvedIndex}"]`); if ($activeBtn) $activeBtn.click(); } else { $items.firstElementChild.click(); diff --git a/layouts/_markup/render-codeblock-toggle.html b/layouts/_markup/render-codeblock-toggle.html index bb3eb682..570a4bdd 100644 --- a/layouts/_markup/render-codeblock-toggle.html +++ b/layouts/_markup/render-codeblock-toggle.html @@ -55,7 +55,7 @@ {{- $active := cond (eq $format $activeFormat) ", .active" "" -}} {{- $jsonViewer := cond (eq $format "json") ", enable=false" "" -}} {{- $labelName := cond (ne $.Attributes.before_tabs nil) $format (upper $format) -}} - {{- $block := printf "```%s {name=%q%s%s%s}\n%s\n```\n\n" $format $labelName $active $jsonViewer $commonAttrsStr $content -}} + {{- $block := printf "```%s {data-code-toggle=true, name=%q%s%s%s}\n%s\n```\n\n" $format $labelName $active $jsonViewer $commonAttrsStr $content -}} {{- $renderText = add $renderText $block -}} {{- end -}} {{- $renderText | .Page.RenderString -}} diff --git a/layouts/_partials/init/index.html b/layouts/_partials/init/index.html index 1916e573..9e5e015d 100644 --- a/layouts/_partials/init/index.html +++ b/layouts/_partials/init/index.html @@ -1,4 +1,4 @@ -{{- hugo.Store.Set "version" "v0.4.5-20260320160308-b0955598" -}} +{{- hugo.Store.Set "version" "v0.4.5-20260320164713-d9e45154" -}} {{- .Store.Set "this" dict -}} {{- partial "init/detection-env.html" . -}}