|
1 | | -document.addEventListener('DOMContentLoaded', function () { |
2 | | - var i = 0; |
3 | | - //The download as png doesn't work on WebKit |
4 | | - var isWebkit = 'WebkitAppearance' in document.documentElement.style; |
5 | | - //Prepare mermaid |
6 | | - mermaid.initialize({ |
7 | | - flowchart: { |
8 | | - useMaxWidth: false |
9 | | - }}); |
10 | | - //Parse every hook |
11 | | - [].forEach.call(document.querySelectorAll('.mermaid-noise'), function (el) { |
12 | | - i++; |
13 | | - //Get the markdown text |
14 | | - var convert = el.innerHTML; |
15 | | - convert = convert.replace(/\[n\]/g, "\n"); |
16 | | - //Get the hook name |
17 | | - var hook = convert.slice(15).split(']'); |
18 | | - hook = hook[0]; |
19 | | - |
20 | | - document.querySelector('.body').innerHTML += "<h3 id='" + hook + "'>Hook " + hook + "</h3>"; |
21 | | - 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'/>"; |
22 | | - if (!isWebkit) { |
23 | | - document.querySelector('.body').innerHTML += "<button class='button button-primary mermaid-download' data-id='" + i + "'>Download</button>"; |
24 | | - } |
25 | | - document.querySelector('.body').innerHTML += "<div class='mermaid-" + i + "'>" + convert + "</div><hr data-id='" + i + "'>"; |
26 | | - //Generate the flowchart |
27 | | - mermaid.init(".mermaid-" + i); |
28 | | - }); |
29 | | - //Go to top |
30 | | - document.querySelector('button.gotop').addEventListener('click', function () { |
31 | | - document.body.scrollTop = document.documentElement.scrollTop = 0; |
32 | | - }); |
33 | | - //Hide system for hooks |
34 | | - [].forEach.call(document.querySelectorAll('.hide-hook'), function (e) { |
35 | | - e.addEventListener('click', function () { |
36 | | - if (e.checked) { |
37 | | - document.querySelector(".mermaid-" + e.dataset.id).style.display = 'none'; |
38 | | - document.querySelector("#" + e.dataset.hook).style.display = 'none'; |
39 | | - document.querySelector(".mermaid-download[data-id='" + e.dataset.id + "']").style.display = 'none'; |
40 | | - document.querySelector("hr[data-id='" + e.dataset.id + "']").style.display = 'none'; |
41 | | - } else { |
42 | | - document.querySelector(".mermaid-" + e.dataset.id).style.display = 'block'; |
43 | | - document.querySelector("#" + e.dataset.hook).style.display = 'block'; |
44 | | - document.querySelector("hr[data-id='" + e.dataset.id + "']").style.display = 'block'; |
45 | | - } |
46 | | - }); |
47 | | - }); |
48 | | - //Download system |
49 | | - if (!isWebkit) { |
50 | | - [].forEach.call(document.querySelectorAll('button.mermaid-download'), function (e) { |
51 | | - e.addEventListener('click', function () { |
52 | | - svgToPng(document.querySelector(".mermaid-" + e.dataset.id + ' svg')); |
53 | | - }); |
54 | | - }); |
55 | | - |
56 | | - function svgToPng(source) { |
57 | | - //Prepare the canvas |
58 | | - var canvas = document.createElement("canvas"); |
59 | | - canvas.setAttribute('width', source.getBBox().width); |
60 | | - canvas.setAttribute('height', source.getBBox().height); |
61 | | - var ctx = canvas.getContext("2d"); |
62 | | - //Prepare the image |
63 | | - var img = new Image(); |
64 | | - //this doesn't work but maybe enalbe the support for webkit |
65 | | - img.setAttribute('crossOrigin', 'anonymous'); |
66 | | - img.crossOrigin = "Anonymous"; |
67 | | - //Get the hook name |
68 | | - var name = source.querySelector('#a div').innerHTML; |
69 | | - var svgString = new XMLSerializer().serializeToString(source); |
70 | | - //Hack for Mermaid to replace div to text object |
71 | | - svgString = svgString.replace(/div/g, "text"); |
72 | | - //Convert the svg string in a blob |
73 | | - var svg = new Blob([svgString], { |
74 | | - type: "image/svg+xml;charset=utf-8" |
75 | | - }); |
76 | | - //Convert as an URL |
77 | | - var url = window.URL.createObjectURL(svg); |
78 | | - img.onload = function () { |
79 | | - ctx.drawImage(img, 0, 0); |
80 | | - var png = canvas.toDataURL("image/png"); |
81 | | - //Generate a img tag |
82 | | - var target = document.createElement('img'); |
83 | | - target.className = "on-the-fly"; |
84 | | - //We pass the empty canvas |
85 | | - target.src = png; |
86 | | - document.body.appendChild(target); |
87 | | - window.URL.revokeObjectURL(png); |
88 | | - }; |
89 | | - //Add the URL in the img |
90 | | - img.src = url; |
91 | | - //Little timeout to get the image |
92 | | - var timeout = window.setTimeout(function () { |
93 | | - var a = document.createElement('a'); |
94 | | - a.download = name + '.png'; |
95 | | - a.href = document.querySelector('.on-the-fly').src; |
96 | | - document.body.appendChild(a); |
97 | | - a.addEventListener("click", function () { |
98 | | - //Cleaning |
99 | | - a.parentNode.removeChild(a); |
100 | | - document.querySelector('.on-the-fly').parentNode.removeChild(document.querySelector('.on-the-fly')); |
101 | | - }); |
102 | | - a.click(); |
103 | | - window.clearTimeout(timeout); |
104 | | - }, 200); |
105 | | - } |
106 | | - } |
| 1 | +jQuery(document).ready(function () { |
| 2 | + jQuery("#wp-admin-bar-hook-flowchart a").click(function () { |
| 3 | + jQuery(".hookr-flowchart").show(); |
| 4 | + var i = 0; |
| 5 | + //Parse every hook |
| 6 | + [].forEach.call(document.querySelectorAll('.mermaid-noise'), function (el) { |
| 7 | + i++; |
| 8 | + //Get the markdown text |
| 9 | + var convert = el.innerHTML; |
| 10 | + convert = convert.replace(/\[n\]/g, "\n"); |
| 11 | + //Get the hook name |
| 12 | + var hook = convert.slice(15).split(']'); |
| 13 | + hook = hook[0]; |
107 | 14 |
|
| 15 | + document.querySelector('.hookr-flowchart').innerHTML += "<h3 id='" + hook + "'>Hook " + hook + "</h3>"; |
| 16 | + 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'/>"; |
| 17 | + document.querySelector('.hookr-flowchart').innerHTML += "<div class='mermaid-" + i + "'>" + convert + "</div><hr data-id='" + i + "'>"; |
| 18 | + //Generate the flowchart |
| 19 | + mermaid.init({ |
| 20 | + logLevel: 0, |
| 21 | + flowchart: { |
| 22 | + useMaxWidth: false |
| 23 | + }}, ".mermaid-" + i); |
| 24 | + }); |
| 25 | + //Go to top |
| 26 | + document.querySelector('button.gotop').addEventListener('click', function () { |
| 27 | + document.body.scrollTop = document.documentElement.scrollTop = 0; |
| 28 | + }); |
| 29 | + //Hide system for hooks |
| 30 | + [].forEach.call(document.querySelectorAll('.hide-hook'), function (e) { |
| 31 | + e.addEventListener('click', function () { |
| 32 | + if (e.checked) { |
| 33 | + document.querySelector(".mermaid-" + e.dataset.id).style.display = 'none'; |
| 34 | + document.querySelector("#" + e.dataset.hook).style.display = 'none'; |
| 35 | + document.querySelector(".mermaid-download[data-id='" + e.dataset.id + "']").style.display = 'none'; |
| 36 | + document.querySelector("hr[data-id='" + e.dataset.id + "']").style.display = 'none'; |
| 37 | + } else { |
| 38 | + document.querySelector(".mermaid-" + e.dataset.id).style.display = 'block'; |
| 39 | + document.querySelector("#" + e.dataset.hook).style.display = 'block'; |
| 40 | + document.querySelector("hr[data-id='" + e.dataset.id + "']").style.display = 'block'; |
| 41 | + } |
| 42 | + }); |
| 43 | + }); |
| 44 | + jQuery("#wpbody,#wpfooter").hide(); |
| 45 | + }); |
108 | 46 | }); |
0 commit comments