direct link ability

This allows to link directly to one variant of an emoji by using the
URL fragment, which will in turn be selected from the available boxes.
This commit is contained in:
Johann150 2024-04-23 23:02:23 +02:00
parent 4cc1f0d7ca
commit 2bd7ec3818
No known key found for this signature in database
GPG Key ID: 9EE6577A2A06F8F1
1 changed files with 28 additions and 0 deletions

View File

@ -224,6 +224,14 @@ const NeomojiMixer = (function(NeomojiMixer) {
//Randomize initial view
randomize();
// If there was a hash, restore as a direct permalink.
if (document.location.hash != "") {
loadFromHash(document.location.hash);
}
window.addEventListener("hashchange", () => {
loadFromHash(document.location.hash);
});
//Show little statistic
var sum = 0;
var variety = 1;
@ -243,6 +251,26 @@ const NeomojiMixer = (function(NeomojiMixer) {
}
function loadFromHash(hash) {
let parts = hash
.slice(1) // the first character is always the '#' sign
.split('+');
// define a constant order for the parts to appear in the hash
const parts_order = ["body", "eyes", "mouth", "arms"];
if (parts.length == parts_order.length) {
// convert the part names to part indices
parts = parts.map((name, i) =>
Array.from(part_handlers[parts_order[i]].name_element.options).findIndex(x => x.value === name)
);
if (parts.every(x => x != -1)) {
// all part names were found
parts.forEach((part_index, i) => part_handlers[parts_order[i]].setIndex(part_index));
}
}
}
function randomize() { //Randomize which parts are shown
for (const i in part_handlers) {
part_handlers[i].randomize();