From 86aa3c808222c351c4bf0f9a391fc4f82d58b744 Mon Sep 17 00:00:00 2001 From: dalance Date: Fri, 29 Nov 2024 09:28:17 +0900 Subject: [PATCH] Update --- book/book.toml | 1 + book/theme/index.hbs | 45 --------------------------------- book/theme/language-picker.js | 47 +++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 45 deletions(-) create mode 100644 book/theme/language-picker.js diff --git a/book/book.toml b/book/book.toml index 737ec150..ae65426a 100644 --- a/book/book.toml +++ b/book/book.toml @@ -24,6 +24,7 @@ additional-css = [ additional-js = [ "mode-veryl.js", "editor.js", + "theme/language-picker.js", ] [output.html.fold] diff --git a/book/theme/index.hbs b/book/theme/index.hbs index 35e58ef3..51ee3307 100644 --- a/book/theme/index.hbs +++ b/book/theme/index.hbs @@ -148,51 +148,6 @@

{{ book_title }}

- - - - - {{#if print_enable}} diff --git a/book/theme/language-picker.js b/book/theme/language-picker.js new file mode 100644 index 00000000..4ef22501 --- /dev/null +++ b/book/theme/language-picker.js @@ -0,0 +1,47 @@ +const right_buttons = document.getElementsByClassName('right-buttons')[0]; +const language_toggle = ` + + +`; +right_buttons.insertAdjacentHTML('afterbegin', language_toggle); + +let langToggle = document.getElementById("language-toggle"); +let langList = document.getElementById("language-list"); +langToggle.addEventListener("click", (event) => { + langList.style.display = langList.style.display == "block" ? "none" : "block"; +}); +let lang = document.documentElement.lang; +let selectedLang = document.getElementById(lang); +if (selectedLang) { + selectedLang.parentNode.classList.add("theme-selected"); +} + +// The path to the root, taking the current +// language into account. +var full_path_to_root; +if (lang == "en") { + full_path_to_root = `${path_to_root}`; +} else { + full_path_to_root = `${path_to_root}../`; +} +let components = (path_to_root.match(/\//g) ?? []).length + 1; +let path = window.location.pathname.split('/').slice(-components).join('/'); +for (let lang of langList.querySelectorAll("a")) { + if (lang.id == "en") { + lang.href = `${full_path_to_root}${path}`; + } else { + lang.href = `${full_path_to_root}${lang.id}/${path}`; + } +}