cookieconsent: prepare theme and docs #1070

This commit is contained in:
Sören Weber 2025-03-24 23:15:25 +01:00
parent b38321f619
commit 4b844a8acc
No known key found for this signature in database
GPG Key ID: BEC6D55545451B6D
4 changed files with 45 additions and 41 deletions

View File

@ -143,21 +143,21 @@ function switchTab(tabGroup, tabId) {
// Store the selection to make it persistent // Store the selection to make it persistent
if (window.localStorage) { if (window.localStorage) {
var selectionsJSON = window.localStorage.getItem(window.relearn.absBaseUri + '/tab-selections'); var selectionsJSON = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/tab-selections');
if (selectionsJSON) { if (selectionsJSON) {
var tabSelections = JSON.parse(selectionsJSON); var tabSelections = JSON.parse(selectionsJSON);
} else { } else {
var tabSelections = {}; var tabSelections = {};
} }
tabSelections[tabGroup] = tabId; tabSelections[tabGroup] = tabId;
window.localStorage.setItem(window.relearn.absBaseUri + '/tab-selections', JSON.stringify(tabSelections)); window.relearn.setItem(window.localStorage, window.relearn.absBaseUri + '/tab-selections', JSON.stringify(tabSelections));
} }
} }
} }
function restoreTabSelections() { function restoreTabSelections() {
if (window.localStorage) { if (window.localStorage) {
var selectionsJSON = window.localStorage.getItem(window.relearn.absBaseUri + '/tab-selections'); var selectionsJSON = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/tab-selections');
if (selectionsJSON) { if (selectionsJSON) {
var tabSelections = JSON.parse(selectionsJSON); var tabSelections = JSON.parse(selectionsJSON);
} else { } else {
@ -324,7 +324,7 @@ function initMermaid(update, attrs) {
var search; var search;
if (update) { if (update) {
search = sessionStorage.getItem(window.relearn.absBaseUri + '/search-value'); search = window.relearn.getItem(window.sessionStorage, window.relearn.absBaseUri + '/search-value');
unmark(); unmark();
} }
var is_initialized = update ? update_func(attrs) : init_func(attrs); var is_initialized = update ? update_func(attrs) : init_func(attrs);
@ -373,7 +373,7 @@ function initMermaid(update, attrs) {
}); });
} }
if (update && search && search.length) { if (update && search && search.length) {
sessionStorage.setItem(window.relearn.absBaseUri + '/search-value', search); window.relearn.setItem(window.sessionStorage, window.relearn.absBaseUri + '/search-value', search);
mark(); mark();
} }
} }
@ -1330,7 +1330,7 @@ function clearHistory() {
var visitedItem = window.relearn.absBaseUri + '/visited-url/'; var visitedItem = window.relearn.absBaseUri + '/visited-url/';
for (var item in sessionStorage) { for (var item in sessionStorage) {
if (item.substring(0, visitedItem.length) === visitedItem) { if (item.substring(0, visitedItem.length) === visitedItem) {
sessionStorage.removeItem(item); window.relearn.removeItem(window.sessionStorage, item);
var url = item.substring(visitedItem.length); var url = item.substring(visitedItem.length);
// in case we have `relativeURLs=true` we have to strip the // in case we have `relativeURLs=true` we have to strip the
// relative path to root // relative path to root
@ -1344,11 +1344,11 @@ function clearHistory() {
function initHistory() { function initHistory() {
var visitedItem = window.relearn.absBaseUri + '/visited-url/'; var visitedItem = window.relearn.absBaseUri + '/visited-url/';
sessionStorage.setItem(visitedItem + document.querySelector('body').dataset.url, 1); window.relearn.setItem(window.sessionStorage, visitedItem + document.querySelector('body').dataset.url, 1);
// loop through the sessionStorage and see if something should be marked as visited // loop through the sessionStorage and see if something should be marked as visited
for (var item in sessionStorage) { for (var item in sessionStorage) {
if (item.substring(0, visitedItem.length) === visitedItem && sessionStorage.getItem(item) == 1) { if (item.substring(0, visitedItem.length) === visitedItem && window.relearn.getItem(window.sessionStorage, item) == 1) {
var url = item.substring(visitedItem.length); var url = item.substring(visitedItem.length);
// in case we have `relativeURLs=true` we have to strip the // in case we have `relativeURLs=true` we have to strip the
// relative path to root // relative path to root
@ -1416,7 +1416,7 @@ function scrollToPositions() {
return; return;
} }
var search = sessionStorage.getItem(window.relearn.absBaseUri + '/search-value'); var search = window.relearn.getItem(window.sessionStorage, window.relearn.absBaseUri + '/search-value');
if (search && search.length) { if (search && search.length) {
search = regexEscape(search); search = regexEscape(search);
var found = elementContains(search, elc); var found = elementContains(search, elc);
@ -1496,7 +1496,7 @@ function mark() {
bodyInnerLinks[i].classList.add('highlight'); bodyInnerLinks[i].classList.add('highlight');
} }
var value = sessionStorage.getItem(window.relearn.absBaseUri + '/search-value'); var value = window.relearn.getItem(window.sessionStorage, window.relearn.absBaseUri + '/search-value');
var highlightableElements = document.querySelectorAll('.highlightable'); var highlightableElements = document.querySelectorAll('.highlightable');
highlight(highlightableElements, value, { element: 'mark', className: 'search' }); highlight(highlightableElements, value, { element: 'mark', className: 'search' });
@ -1596,7 +1596,7 @@ function highlightNode(node, re, nodeName, className) {
} }
function unmark() { function unmark() {
sessionStorage.removeItem(window.relearn.absBaseUri + '/search-value'); window.relearn.removeItem(window.sessionStorage, window.relearn.absBaseUri + '/search-value');
var markedElements = document.querySelectorAll('mark.search'); var markedElements = document.querySelectorAll('mark.search');
for (var i = 0; i < markedElements.length; i++) { for (var i = 0; i < markedElements.length; i++) {
var parent = markedElements[i].parentNode; var parent = markedElements[i].parentNode;
@ -1670,7 +1670,7 @@ function elementContains(txt, e) {
function searchInputHandler(value) { function searchInputHandler(value) {
unmark(); unmark();
if (value.length) { if (value.length) {
sessionStorage.setItem(window.relearn.absBaseUri + '/search-value', value); window.relearn.setItem(window.sessionStorage, window.relearn.absBaseUri + '/search-value', value);
mark(); mark();
} }
} }
@ -1682,7 +1682,7 @@ function initSearch() {
e.addEventListener('keydown', function (event) { e.addEventListener('keydown', function (event) {
if (event.key == 'Escape') { if (event.key == 'Escape') {
var input = event.target; var input = event.target;
var search = sessionStorage.getItem(window.relearn.absBaseUri + '/search-value'); var search = window.relearn.getItem(window.sessionStorage, window.relearn.absBaseUri + '/search-value');
if (!search || !search.length) { if (!search || !search.length) {
input.blur(); input.blur();
} }
@ -1722,13 +1722,13 @@ function initSearch() {
var urlParams = new URLSearchParams(window.location.search); var urlParams = new URLSearchParams(window.location.search);
var value = urlParams.get('search-by'); var value = urlParams.get('search-by');
if (value) { if (value) {
sessionStorage.setItem(window.relearn.absBaseUri + '/search-value', value); window.relearn.setItem(window.sessionStorage, window.relearn.absBaseUri + '/search-value', value);
} }
mark(); mark();
// set initial search value for inputs on page load // set initial search value for inputs on page load
if (sessionStorage.getItem(window.relearn.absBaseUri + '/search-value')) { if (window.relearn.getItem(window.sessionStorage, window.relearn.absBaseUri + '/search-value')) {
var search = sessionStorage.getItem(window.relearn.absBaseUri + '/search-value'); var search = window.relearn.getItem(window.sessionStorage, window.relearn.absBaseUri + '/search-value');
inputs.forEach(function (e) { inputs.forEach(function (e) {
e.value = search; e.value = search;
var event = document.createEvent('Event'); var event = document.createEvent('Event');

View File

@ -19,17 +19,17 @@ var variants = {
setup: function () { setup: function () {
this.addCustomVariantStyles(); this.addCustomVariantStyles();
var customvariantstylesheet = window.localStorage.getItem(window.relearn.absBaseUri + '/customvariantstylesheet'); var customvariantstylesheet = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/customvariantstylesheet');
var customvariant = window.localStorage.getItem(window.relearn.absBaseUri + '/customvariant'); var customvariant = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/customvariant');
if (!customvariantstylesheet || !customvariant) { if (!customvariantstylesheet || !customvariant) {
customvariantstylesheet = ''; customvariantstylesheet = '';
window.localStorage.removeItem(window.relearn.absBaseUri + '/customvariantstylesheet'); window.relearn.removeItem(window.localStorage, window.relearn.absBaseUri + '/customvariantstylesheet');
customvariant = ''; customvariant = '';
window.localStorage.removeItem(window.relearn.absBaseUri + '/customvariant'); window.relearn.removeItem(window.localStorage, window.relearn.absBaseUri + '/customvariant');
} else if (customvariant && !window.relearn.themevariants.includes(customvariant)) { } else if (customvariant && !window.relearn.themevariants.includes(customvariant)) {
// this can only happen on initial load, if a previously selected variant is not available anymore // this can only happen on initial load, if a previously selected variant is not available anymore
customvariant = window.relearn.themevariants[0]; customvariant = window.relearn.themevariants[0];
window.localStorage.setItem(window.relearn.absBaseUri + '/customvariant', customvariant); window.relearn.setItem( window.localStorage, window.relearn.absBaseUri + '/customvariant', customvariant);
} }
this.updateCustomVariantStyles(customvariantstylesheet); this.updateCustomVariantStyles(customvariantstylesheet);
@ -40,11 +40,11 @@ var variants = {
init: function (variant, old_path) { init: function (variant, old_path) {
this.addCustomVariantOption(); this.addCustomVariantOption();
window.relearn.markVariant(); window.relearn.markVariant();
window.relearn.changeVariant(window.localStorage.getItem(window.relearn.absBaseUri + '/variant')); window.relearn.changeVariant(window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/variant'));
}, },
addCustomVariantOption: function () { addCustomVariantOption: function () {
var customvariant = window.localStorage.getItem(window.relearn.absBaseUri + '/customvariant'); var customvariant = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/customvariant');
if (!customvariant ) { if (!customvariant ) {
return; return;
} }
@ -86,13 +86,13 @@ var variants = {
}, },
saveCustomVariant: function () { saveCustomVariant: function () {
var variant = window.localStorage.getItem(window.relearn.absBaseUri + '/variant'); var variant = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/variant');
if (variant != this.customvariantname) { if (variant != this.customvariantname) {
window.localStorage.setItem(window.relearn.absBaseUri + '/customvariant', variant); window.relearn.setItem( window.localStorage, window.relearn.absBaseUri + '/customvariant', variant);
} }
var stylesheet = this.generateStylesheet(this.customvariantname); var stylesheet = this.generateStylesheet(this.customvariantname);
window.localStorage.setItem(window.relearn.absBaseUri + '/variant', this.customvariantname); window.relearn.setItem( window.localStorage, window.relearn.absBaseUri + '/variant', this.customvariantname);
window.localStorage.setItem(window.relearn.absBaseUri + '/customvariantstylesheet', stylesheet); window.relearn.setItem( window.localStorage, window.relearn.absBaseUri + '/customvariantstylesheet', stylesheet);
this.updateCustomVariantStyles(stylesheet); this.updateCustomVariantStyles(stylesheet);
this.addCustomVariantOption(); this.addCustomVariantOption();
@ -185,7 +185,7 @@ var variants = {
} else { } else {
style = this.findLoadedStylesheet('R-variant-styles-' + variant, [':root:not([data-r-output-format="print"])[data-r-theme-variant="' + variant + '"]']); style = this.findLoadedStylesheet('R-variant-styles-' + variant, [':root:not([data-r-output-format="print"])[data-r-theme-variant="' + variant + '"]']);
if (!style) { if (!style) {
var customvariantbase = window.localStorage.getItem(window.relearn.absBaseUri + '/customvariant'); var customvariantbase = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/customvariant');
style = this.findLoadedStylesheet('R-format-style', [':root:not([data-r-output-format="print"])[data-r-theme-variant="' + customvariantbase + '"]']); style = this.findLoadedStylesheet('R-format-style', [':root:not([data-r-output-format="print"])[data-r-theme-variant="' + customvariantbase + '"]']);
if (!style) { if (!style) {
alert('There is nothing to be generated as auto mode variants will be generated by Hugo'); alert('There is nothing to be generated as auto mode variants will be generated by Hugo');
@ -223,8 +223,8 @@ var variants = {
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
changeColor: function (c) { changeColor: function (c) {
var variant = window.localStorage.getItem(window.relearn.absBaseUri + '/variant'); var variant = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/variant');
var customvariantbase = window.localStorage.getItem(window.relearn.absBaseUri + '/customvariant'); var customvariantbase = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/customvariant');
if (customvariantbase && this.customvariantname != variant) { if (customvariantbase && this.customvariantname != variant) {
alert('You already have changes based on the "' + customvariantbase + '" variant. Please proceed editing the custom variant, reset your changes or ignore this message.'); alert('You already have changes based on the "' + customvariantbase + '" variant. Please proceed editing the custom variant, reset your changes or ignore this message.');
@ -273,15 +273,15 @@ var variants = {
}, },
resetVariant: function () { resetVariant: function () {
var customvariant = window.localStorage.getItem(window.relearn.absBaseUri + '/customvariant'); var customvariant = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/customvariant');
if (customvariant && confirm('You have made changes to your custom variant. Are you sure you want to reset all changes?')) { if (customvariant && confirm('You have made changes to your custom variant. Are you sure you want to reset all changes?')) {
var variant = window.localStorage.getItem(window.relearn.absBaseUri + '/variant'); var variant = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/variant');
if (variant != this.customvariantname) { if (variant != this.customvariantname) {
customvariant = variant; customvariant = variant;
} }
window.localStorage.removeItem(window.relearn.absBaseUri + '/customvariant'); window.relearn.removeItem(window.localStorage, window.relearn.absBaseUri + '/customvariant');
window.localStorage.removeItem(window.relearn.absBaseUri + '/customvariantstylesheet'); window.relearn.removeItem(window.localStorage, window.relearn.absBaseUri + '/customvariantstylesheet');
window.localStorage.setItem(window.relearn.absBaseUri + '/variant', customvariant); window.relearn.setItem( window.localStorage, window.relearn.absBaseUri + '/variant', customvariant);
this.updateCustomVariantStyles(''); this.updateCustomVariantStyles('');
this.removeCustomVariantOption(); this.removeCustomVariantOption();
@ -291,7 +291,7 @@ var variants = {
}, },
getStylesheet: function () { getStylesheet: function () {
var variant = window.localStorage.getItem(window.relearn.absBaseUri + '/variant'); var variant = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + '/variant');
var style = this.generateStylesheet(variant); var style = this.generateStylesheet(variant);
if (style) { if (style) {
console.log(style); console.log(style);

View File

@ -134,12 +134,16 @@
window.relearn.disableAnchorScrolling={{ printf "%t" (eq .Site.Params.disableAnchorScrolling true) | safeJS }}; window.relearn.disableAnchorScrolling={{ printf "%t" (eq .Site.Params.disableAnchorScrolling true) | safeJS }};
window.relearn.disableInlineCopyToClipboard={{ printf "%t" (eq $disableInlineCopyToClipboard true) | safeJS }}; window.relearn.disableInlineCopyToClipboard={{ printf "%t" (eq $disableInlineCopyToClipboard true) | safeJS }};
window.relearn.enableBlockCodeWrap={{ printf "%t" (eq $wrap true) | safeJS }}; window.relearn.enableBlockCodeWrap={{ printf "%t" (eq $wrap true) | safeJS }};
{{ "// legal" | safeJS }}
window.relearn.getItem = (s,n) => {return s.getItem(n)};
window.relearn.setItem = (s,n,v) => {return s.setItem(n,v)};
window.relearn.removeItem = (s,n) => {return s.removeItem(n)};
{{ "// variant stuff" | safeJS }} {{ "// variant stuff" | safeJS }}
window.relearn.themevariants = [ {{ delimit $quotedthemevariants ", " | safeJS }} ]; window.relearn.themevariants = [ {{ delimit $quotedthemevariants ", " | safeJS }} ];
window.relearn.customvariantname = "my-custom-variant"; window.relearn.customvariantname = "my-custom-variant";
window.relearn.changeVariant = function(variant) { window.relearn.changeVariant = function(variant) {
var oldVariant = document.documentElement.dataset.rThemeVariant; var oldVariant = document.documentElement.dataset.rThemeVariant;
window.localStorage.setItem(window.relearn.absBaseUri + "/variant", variant); window.relearn.setItem(window.localStorage, window.relearn.absBaseUri + "/variant", variant);
document.documentElement.dataset.rThemeVariant = variant; document.documentElement.dataset.rThemeVariant = variant;
if (oldVariant != variant) { if (oldVariant != variant) {
document.dispatchEvent( new CustomEvent('themeVariantLoaded', { detail: { variant, oldVariant } }) ); document.dispatchEvent( new CustomEvent('themeVariantLoaded', { detail: { variant, oldVariant } }) );
@ -147,15 +151,15 @@
} }
} }
window.relearn.markVariant = function() { window.relearn.markVariant = function() {
var variant = window.localStorage.getItem(window.relearn.absBaseUri + "/variant"); var variant = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + "/variant");
document.querySelectorAll(".R-variantswitcher select").forEach((select) => {select.value = variant;}); document.querySelectorAll(".R-variantswitcher select").forEach((select) => {select.value = variant;});
} }
window.relearn.initVariant = function() { window.relearn.initVariant = function() {
var variant = window.localStorage.getItem(window.relearn.absBaseUri + "/variant") ?? ""; var variant = window.relearn.getItem(window.localStorage, window.relearn.absBaseUri + "/variant") ?? "";
if( variant == window.relearn.customvariantname ){ if( variant == window.relearn.customvariantname ){
}else if( !variant || !window.relearn.themevariants.includes(variant) ){ }else if( !variant || !window.relearn.themevariants.includes(variant) ){
variant = window.relearn.themevariants[0]; variant = window.relearn.themevariants[0];
window.localStorage.setItem(window.relearn.absBaseUri + "/variant", variant); window.relearn.setItem(window.localStorage, window.relearn.absBaseUri + "/variant", variant);
} }
document.documentElement.dataset.rThemeVariant = variant; document.documentElement.dataset.rThemeVariant = variant;
} }

View File

@ -1 +1 @@
7.5.0+1f07a0f256020d2af6adecca5e983e4feaf81df4 7.5.0+b38321f619da16d489092b2d84b4b906680ad3d6