Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

147 рядки
2.9 KiB

  1. #!/usr/bin/node
  2. const fs = require('fs');
  3. var DIR = process.argv[2];
  4. var TITLE = process.argv[3] || "Gallery";
  5. if(!DIR || !TITLE) {
  6. console.log(`### ERROR! No directory and/or title provided!`);
  7. console.log(`Usage: node gallery.js {directory} {title}`);
  8. process.exit(1);
  9. }
  10. outHtml = `
  11. <head><title>${TITLE}</title>
  12. <script src="http://pablojs.com/downloads/pablo.min.js"></script>
  13. </head>
  14. <style>
  15. body {
  16. font-family: 'Segoe UI', Arial;
  17. background-color: #aaa;
  18. padding: 0;
  19. margin: 0;
  20. }
  21. .bg0 {
  22. background-image: url("../_tools/background.png")
  23. }
  24. .bg1 {
  25. background-color: #333;
  26. }
  27. .bg2 {
  28. background-color: #fff;
  29. }
  30. .imgbox img {
  31. width: 10.5rem;
  32. height: 10.5rem;
  33. object-fit: contain;
  34. cursor: pointer;
  35. }
  36. .imgbox {
  37. margin: 0.6rem;
  38. padding: 0.5rem;
  39. text-align: center;
  40. width: 12rem;
  41. height: 12rem;
  42. background-color: #e0e0e0;
  43. }
  44. h1 {
  45. font-size: 2.6rem;
  46. }
  47. .find, .find input {
  48. font-size: 1.5rem;
  49. }
  50. .top {
  51. position: -webkit-sticky;
  52. position: sticky;
  53. top: 0;
  54. background-color: #aaa;
  55. padding: 1rem;
  56. padding-top: 0;
  57. box-shadow: 0px 5px 18px 3px rgba(0,0,0,0.59);
  58. }
  59. .top button {
  60. float: right;
  61. position: relative;
  62. top: -2rem;
  63. font-size: 1.5rem;
  64. }
  65. .label {
  66. height: 2.5em;
  67. line-height: 1em;
  68. overflow: hidden;
  69. font-size: 0.9rem;
  70. }
  71. .grid {
  72. display: flex;
  73. flex-wrap: wrap;
  74. width: 100%;
  75. }
  76. </style>
  77. <body>
  78. <div class="top">
  79. <h1>${TITLE}</h1>
  80. <span class="find">Finder: <input onkeyup="search()" id="finder"><br></span>
  81. <button onclick="toggleBg()">Change Background</button>
  82. </div>
  83. <div class="grid">
  84. `
  85. var files = fs.readdirSync(DIR);
  86. for(let f of files) {
  87. if(!f.endsWith(".svg")) continue;
  88. outHtml += `<div id="${f}" class="imgbox"><img src="${f}" class="bg0" onclick="download('${f}', this)"/><div class="label">${f}</div></div>\n`
  89. }
  90. outHtml += `
  91. </div>
  92. <script>
  93. var bg = 0;
  94. function toggleBg() {
  95. bg++
  96. if(bg > 2) bg = 0;
  97. for(let img of document.getElementsByTagName("img")) {
  98. img.className = \`bg\${bg}\`;
  99. }
  100. }
  101. function search() {
  102. let q = document.getElementById('finder').value.trim().toLowerCase()
  103. if(q.length <= 0) {
  104. for(let imgbox of document.getElementsByClassName("imgbox")) {
  105. imgbox.style.display = "inline-block"
  106. }
  107. }
  108. for(let imgbox of document.getElementsByClassName("imgbox")) {
  109. imgbox.style.display = "inline-block"
  110. if(!imgbox.id.includes(q)) {
  111. imgbox.style.display = "none"
  112. }
  113. }
  114. }
  115. function download(f, e) {
  116. let a = document.createElement('a')
  117. a.href = f
  118. a.download = f
  119. //a.click()
  120. console.log(getBase64Image(e))
  121. }
  122. function getBase64Image(img) {
  123. var canvas = document.createElement("canvas");
  124. canvas.width = img.width;
  125. canvas.height = img.height;
  126. var ctx = canvas.getContext("2d");
  127. ctx.drawImage(img, 0, 0);
  128. var dataURL = canvas.toDataURL("image/png");
  129. return dataURL.replace(/^data:image\\/(png|jpg);base64,/, "");
  130. }
  131. </script>
  132. </body></html>`
  133. console.log(`### Generating gallery index.html for ${DIR}`);
  134. fs.writeFileSync(DIR + '/index.html', outHtml);