forked from Gothic-Modding-Community/gmc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f68add5
commit d57a0ae
Showing
5 changed files
with
197 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
nav: | ||
- Home: | ||
- ... | index*.md | ||
- ... | preferences*.md | ||
- zengin | ||
- genome | ||
- contribute |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Preferences | ||
|
||
<p id="preferences-start"></p> | ||
|
||
<script id="preferences-config-localization" type="application/json"> | ||
{ | ||
"en": { | ||
"h1Content": "Preferences", | ||
"firstParagraph": "This page allows to set various preferences for reading the docs:", | ||
"colorTitle": "Color", | ||
"colorDescription": "You can change the base color from the default \"#00aafc\".", | ||
"headingShadowTitle": "Heading shadows", | ||
"headingShadowDescription": "You can enable additional shadows for the heading to make them appear more bold." | ||
}, | ||
"pl": { | ||
"h1Content": "Preferencje", | ||
"firstParagraph": "Ta strona pozwala ustawić różne preferencje do czytania dokumentacji:", | ||
"colorTitle": "Kolor", | ||
"colorDescription": "Możesz zmienić bazowy kolor z domyślnego \"#00aafc\".", | ||
"headingShadowTitle": "Cienie nagłówków", | ||
"headingShadowDescription": "Możesz włączyć dodatkowe cienie do nagłówków żeby wyglądały bardziej pogrubione." | ||
} | ||
} | ||
</script> | ||
|
||
<script src="/gmc/assets/javascripts/preferences.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
* Licence MIT - Copyright (c) 2023 Kamil Krzyśków (HRY) | ||
*/ | ||
|
||
const gmcPreferenceHandler = (event) => { | ||
console.debug(event); | ||
const target = event.target; | ||
const option = target.dataset["option"]; | ||
// preferencesKey defined in extrahead template override | ||
let loadedPreferences = __md_get(preferencesKey); | ||
|
||
if (option === "color") { | ||
let prev = loadedPreferences["color"]; | ||
if (!prev) { | ||
loadedPreferences["color"] = {}; | ||
loadedPreferences["__global"] = {}; | ||
} | ||
|
||
// TODO Separate raw css builder from value assignment, perhaps it's time for OOP... | ||
// TODO Implement some HEX to HSL conversion logic | ||
// https://stackoverflow.com/questions/3732046/how-do-you-get-the-hue-of-a-xxxxxx-colour | ||
|
||
loadedPreferences["color"]["value"] = target.value; | ||
loadedPreferences["__global"]["cssRaw"] = `* { --md-typeset-a-color: ${target.value}!important; } | ||
[data-md-color-scheme="slate"] .md-header { border-bottom: .10rem solid ${target.value}; } | ||
[data-md-color-scheme="slate"] .md-tabs { background-color: ${target.value} !important; }`; | ||
} | ||
|
||
__md_set(preferencesKey, loadedPreferences) | ||
// Function defined in extrahead template override | ||
gmcLoadSetPreferences(); | ||
}; | ||
|
||
/** | ||
* Removes the translation prompts from the page. | ||
* Mostly a quick hack for the GMC Preferences page. | ||
* Once this function is used more than 3 times, | ||
* it would be better to have a backend preprocessor solution. | ||
*/ | ||
const removeRedundantTranslationIndicator = () => { | ||
const itemsToRemove = [ | ||
document.querySelector("#gmc-new-translation-button"), | ||
document.querySelector(".gmc-announce") | ||
]; | ||
|
||
for (const item of itemsToRemove) { | ||
console.debug(item) | ||
if (item) { | ||
item.remove(); | ||
} | ||
} | ||
}; | ||
|
||
/* | ||
This is run immediately when loaded to add the elements before the bundle.js | ||
overrides the title tooltip logic. DOMContentLoaded is too late. | ||
*/ | ||
(() => { | ||
|
||
// Defined in extrahead template override | ||
gGMC_REMOVE_TRANSLATION_PROMPTS = true; | ||
removeRedundantTranslationIndicator(); | ||
|
||
let lastElement = document.querySelector("#preferences-start"); | ||
const localizationElement = document.querySelector("#preferences-config-localization"); | ||
|
||
if (!lastElement || !localizationElement) { | ||
console.debug("Preferences management couldn't find specified elements"); | ||
return; | ||
} | ||
|
||
// TODO export into a separate function to keep localization gathering disconnected from rendering | ||
const localizationMatrix = JSON.parse(localizationElement.textContent); | ||
let currentLocalization = localizationMatrix["en"]; | ||
const langCode = location.pathname.split("/")[2]; | ||
const langCodeLocalization = localizationMatrix[langCode]; | ||
|
||
if (langCodeLocalization) { | ||
currentLocalization = {...currentLocalization, ...langCodeLocalization} | ||
} | ||
|
||
console.debug(lastElement) | ||
console.debug(localizationMatrix) | ||
console.debug(langCode) | ||
console.debug(langCodeLocalization) | ||
console.debug(currentLocalization) | ||
|
||
const h1Tag = document.querySelector(".md-content h1"); | ||
if (h1Tag) { | ||
h1Tag.childNodes[0].nodeValue = currentLocalization["h1Content"]; | ||
} | ||
lastElement.innerText = currentLocalization["firstParagraph"]; | ||
|
||
const optionNames = ["color", "headingShadow"]; | ||
|
||
for (let option of optionNames) { | ||
if (option !== "color") { | ||
console.debug("for now only color works") | ||
break; | ||
} | ||
|
||
console.debug(option) | ||
|
||
const title = document.createElement("h2"); | ||
title.innerText = currentLocalization[`${option}Title`]; | ||
lastElement.insertAdjacentElement("afterend", title); | ||
|
||
const description = document.createElement("p"); | ||
description.innerText = currentLocalization[`${option}Description`]; | ||
title.insertAdjacentElement("afterend", description); | ||
|
||
const inputWrapper = document.createElement("p"); | ||
const input = document.createElement("input"); | ||
input.type = "text"; | ||
if (option === "color") { | ||
input.type = option; | ||
} | ||
input.dataset["option"] = option; | ||
input.title = option; | ||
if (option !== "color") { | ||
input.className = "md-input"; | ||
} | ||
input.addEventListener("change", gmcPreferenceHandler); | ||
inputWrapper.appendChild(input); | ||
description.insertAdjacentElement("afterend", inputWrapper); | ||
|
||
lastElement = inputWrapper; | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters