11document . addEventListener ( 'DOMContentLoaded' , function ( ) {
22 var i = 0 ;
3+ //The download as png doesn't work on WebKit
34 var isWebkit = 'WebkitAppearance' in document . documentElement . style ;
5+ //Prepare mermaid
46 mermaid . initialize ( {
57 flowchart : {
68 useMaxWidth : false
79 } } ) ;
10+ //Parse every hook
811 [ ] . forEach . call ( document . querySelectorAll ( '.mermaid-noise' ) , function ( el ) {
912 i ++ ;
13+ //Get the markdown text
1014 var convert = el . innerHTML ;
1115 convert = convert . replace ( / \[ n \] / g, "\n" ) ;
16+ //Get the hook name
1217 var hook = convert . slice ( 15 ) . split ( ']' ) ;
1318 hook = hook [ 0 ] ;
19+
1420 document . querySelector ( '.body' ) . innerHTML += "<h3 id='" + hook + "'>Hook " + hook + "</h3>" ;
1521 document . querySelector ( '.buttons' ) . innerHTML += "<a href='#" + hook + "' class='button'>" + hook + "</a><input type='checkbox' data-hook='" + hook + "' data-id='" + i + "' alt='Hide' title='Hide' class='hide-hook' style='margin-top:3px'/>" ;
1622 if ( ! isWebkit ) {
1723 document . querySelector ( '.body' ) . innerHTML += "<button class='button button-primary mermaid-download' data-id='" + i + "'>Download</button>" ;
1824 }
1925 document . querySelector ( '.body' ) . innerHTML += "<div class='mermaid-" + i + "'>" + convert + "</div><hr data-id='" + i + "'>" ;
26+ //Generate the flowchart
2027 mermaid . init ( ".mermaid-" + i ) ;
2128 } ) ;
29+ //Go to top
2230 document . querySelector ( 'button.gotop' ) . addEventListener ( 'click' , function ( ) {
2331 document . body . scrollTop = document . documentElement . scrollTop = 0 ;
2432 } ) ;
33+ //Hide system for hooks
2534 [ ] . forEach . call ( document . querySelectorAll ( '.hide-hook' ) , function ( e ) {
2635 e . addEventListener ( 'click' , function ( ) {
2736 if ( e . checked ) {
@@ -36,6 +45,7 @@ document.addEventListener('DOMContentLoaded', function () {
3645 }
3746 } ) ;
3847 } ) ;
48+ //Download system
3949 if ( ! isWebkit ) {
4050 [ ] . forEach . call ( document . querySelectorAll ( 'button.mermaid-download' ) , function ( e ) {
4151 e . addEventListener ( 'click' , function ( ) {
@@ -44,39 +54,48 @@ document.addEventListener('DOMContentLoaded', function () {
4454 } ) ;
4555
4656 function svgToPng ( source ) {
47- var name = source . querySelector ( '#a div' ) . innerHTML ;
57+ //Prepare the canvas
4858 var canvas = document . createElement ( "canvas" ) ;
4959 canvas . setAttribute ( 'width' , source . getBBox ( ) . width ) ;
5060 canvas . setAttribute ( 'height' , source . getBBox ( ) . height ) ;
5161 var ctx = canvas . getContext ( "2d" ) ;
62+ //Prepare the image
5263 var img = new Image ( ) ;
64+ //this doesn't work but maybe enalbe the support for webkit
5365 img . setAttribute ( 'crossOrigin' , 'anonymous' ) ;
5466 img . crossOrigin = "Anonymous" ;
67+ //Get the hook name
68+ var name = source . querySelector ( '#a div' ) . innerHTML ;
5569 var svgString = new XMLSerializer ( ) . serializeToString ( source ) ;
5670 //Hack for Mermaid to replace div to text object
5771 svgString = svgString . replace ( / d i v / g, "text" ) ;
58- //Hack for mermaid for text alignment
59- svgString = svgString . replace ( / t r a n s l a t e \( 0 \, 0 \) / g, 'translate(-2,0)' ) ;
72+ //Convert the svg string in a blob
6073 var svg = new Blob ( [ svgString ] , {
6174 type : "image/svg+xml;charset=utf-8"
6275 } ) ;
76+ //Convert as an URL
6377 var url = window . URL . createObjectURL ( svg ) ;
6478 img . onload = function ( ) {
6579 ctx . drawImage ( img , 0 , 0 ) ;
6680 var png = canvas . toDataURL ( "image/png" ) ;
81+ //Generate a img tag
6782 var target = document . createElement ( 'img' ) ;
6883 target . className = "on-the-fly" ;
84+ //We pass the empty canvas
6985 target . src = png ;
7086 document . body . appendChild ( target ) ;
7187 window . URL . revokeObjectURL ( png ) ;
7288 } ;
89+ //Add the URL in the img
7390 img . src = url ;
91+ //Little timeout to get the image
7492 var timeout = window . setTimeout ( function ( ) {
7593 var a = document . createElement ( 'a' ) ;
7694 a . download = name + '.png' ;
7795 a . href = document . querySelector ( '.on-the-fly' ) . src ;
7896 document . body . appendChild ( a ) ;
79- a . addEventListener ( "click" , function ( e ) {
97+ a . addEventListener ( "click" , function ( ) {
98+ //Cleaning
8099 a . parentNode . removeChild ( a ) ;
81100 document . querySelector ( '.on-the-fly' ) . parentNode . removeChild ( document . querySelector ( '.on-the-fly' ) ) ;
82101 } ) ;
0 commit comments