hugo-theme-anubis2/assets/js/load-mermaid.js

70 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2024-04-09 10:42:47 +02:00
/*!
* Simple-Jekyll-Search
* Copyright 2015-2020, Christian Fei
* Licensed under the MIT License.
*/
(function(window){
'use strict'
2024-04-09 14:03:09 +02:00
2024-04-09 10:42:47 +02:00
const elementCode = '.mermaid'
const loadMermaid = function(theme) {
window.mermaid.initialize({theme})
window.mermaid.init({theme}, document.querySelectorAll(elementCode))
}
const saveOriginalData = function(){
return new Promise((resolve, reject) => {
try {
var els = document.querySelectorAll(elementCode),
count = els.length;
els.forEach(element => {
element.setAttribute('data-original-code', element.innerHTML)
count--
if(count == 0){
resolve()
}
});
} catch (error) {
2024-04-09 14:03:09 +02:00
reject(error)
2024-04-09 10:42:47 +02:00
}
})
}
const resetProcessed = function(){
return new Promise((resolve, reject) => {
try {
var els = document.querySelectorAll(elementCode),
count = els.length;
els.forEach(element => {
if(element.getAttribute('data-original-code') != null){
element.removeAttribute('data-processed')
element.innerHTML = element.getAttribute('data-original-code')
}
count--
if(count == 0){
resolve()
}
});
} catch (error) {
2024-04-09 14:03:09 +02:00
reject(error)
2024-04-09 10:42:47 +02:00
}
})
2024-04-09 14:03:09 +02:00
}
2024-04-09 10:42:47 +02:00
const init = ()=>{
saveOriginalData()
.catch( console.error )
document.body.addEventListener('dark-theme-set', ()=>{
resetProcessed()
.then(loadMermaid('dark'))
.catch(console.error)
})
document.body.addEventListener('light-theme-set', ()=>{
resetProcessed()
.then(loadMermaid('default'))
.catch(console.error)
})
}
window.initMermaid = init
2024-04-09 14:03:09 +02:00
})(window);