updates for new svg view/download links
This commit is contained in:
58
extras/svg-links.js
Normal file
58
extras/svg-links.js
Normal file
@@ -0,0 +1,58 @@
|
||||
(() => {
|
||||
|
||||
if (document.readyState === 'complete') {
|
||||
add_svg_links();
|
||||
}
|
||||
|
||||
else {
|
||||
window.addEventListener('DOMContentLoaded', add_svg_links);
|
||||
}
|
||||
|
||||
function add_svg_links() {
|
||||
const svg_elems = document.querySelectorAll('figure > svg');
|
||||
|
||||
for (const svg_elem of svg_elems) {
|
||||
const figure_elem = svg_elem.parentElement;
|
||||
const anchor_elem = document.createElement('a');
|
||||
const lang = figure_elem.getAttribute('data-lang') || '';
|
||||
|
||||
let svg_html;
|
||||
|
||||
if ('svg_link_css' in window) {
|
||||
const svg_clone = svg_elem.cloneNode(true);
|
||||
const style_elem = document.createElement('style');
|
||||
svg_clone.insertBefore(style_elem, svg_clone.firstChild);
|
||||
|
||||
const css
|
||||
= (window.svg_link_css['*'] || '')
|
||||
+ (lang ? window.svg_link_css[lang] || '' : '');
|
||||
|
||||
style_elem.setAttribute('type', 'text/css');
|
||||
style_elem.appendChild(document.createTextNode(css));
|
||||
|
||||
svg_html = svg_clone.outerHTML;
|
||||
}
|
||||
|
||||
else {
|
||||
svg_html = svg_elem.outerHTML;
|
||||
}
|
||||
|
||||
const box = document.createElement('div');
|
||||
const svg_xml = '<?xml version="1.0" standalone="no" ?>\r\n'
|
||||
+ svg_html.replace(/(&(?!(amp|gt|lt|quot|apos))[^;]+;)/g, ($0, $1) => {
|
||||
box.innerHTML = $0;
|
||||
return box.textContent;
|
||||
});
|
||||
|
||||
const svg_blob = new Blob([ svg_xml ], { type: 'image/svg+xml; charset=utf-8' });
|
||||
const svg_object_url = URL.createObjectURL(svg_blob);
|
||||
|
||||
anchor_elem.href = svg_object_url;
|
||||
anchor_elem.className = 'view-svg';
|
||||
anchor_elem.textContent = 'View / Download Graphic';
|
||||
|
||||
figure_elem.insertBefore(anchor_elem, svg_elem);
|
||||
}
|
||||
}
|
||||
|
||||
})();
|
||||
Reference in New Issue
Block a user