utils.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. 'use strict';
  2. function _toConsumableArray(e) {
  3. if (Array.isArray(e)) {
  4. for (var t = 0, n = Array(e.length); t < e.length; t++)
  5. n[t] = e[t];
  6. return n;
  7. }
  8. return Array.from(e);
  9. }
  10. HTMLElement.prototype.wrap = function (e) {
  11. this.parentNode.insertBefore(e, this), this.parentNode.removeChild(this), e.appendChild(this);
  12. }, NexT.utils = {
  13. wrapImageWithFancyBox: function () {
  14. document.querySelectorAll('.post-body :not(a) > img, .post-body > img').forEach(function (e) {
  15. var t = $(e), n = t.attr('data-src') || t.attr('src'), a = t.wrap('<a class="fancybox fancybox.image" href="' + n + '" itemscope itemtype="http://schema.org/ImageObject" itemprop="url"></a>').parent('a');
  16. t.is('.post-gallery img') ? a.attr('data-fancybox', 'gallery').attr('rel', 'gallery') : t.is('.group-picture img') ? a.attr('data-fancybox', 'group').attr('rel', 'group') : a.attr('data-fancybox', 'default').attr('rel', 'default');
  17. var r = t.attr('title') || t.attr('alt');
  18. r && (a.append('<p class="image-caption">' + r + '</p>'), a.attr('title', r).attr('data-caption', r));
  19. }), $.fancybox.defaults.hash = !1, $('.fancybox').fancybox({
  20. loop: !0,
  21. helpers: { overlay: { locked: !1 } }
  22. });
  23. },
  24. registerExtURL: function () {
  25. document.querySelectorAll('span.exturl').forEach(function (e) {
  26. var t = document.createElement('a');
  27. t.href = decodeURIComponent(atob(e.dataset.url).split('').map(function (e) {
  28. return '%' + ('00' + e.charCodeAt(0).toString(16)).slice(-2);
  29. }).join('')), t.rel = 'noopener external nofollow noreferrer', t.target = '_blank', t.className = e.className, t.innerHTML = e.innerHTML, e.parentNode.replaceChild(t, e);
  30. });
  31. },
  32. registerCopyCode: function () {
  33. document.querySelectorAll('figure.highlight').forEach(function (e) {
  34. var t = document.createElement('div');
  35. e.wrap(t), t.classList.add('highlight-container'), t.insertAdjacentHTML('beforeend', '<div class="copy-btn"><i class="fa fa-clipboard"></i></div>');
  36. var n = e.parentNode.querySelector('.copy-btn');
  37. n.addEventListener('click', function (e) {
  38. var t = e.currentTarget, n = [].concat(_toConsumableArray(t.parentNode.querySelectorAll('.code .line'))).map(function (e) {
  39. return e.innerText;
  40. }).join('\n'), a = document.createElement('textarea');
  41. a.style.top = window.scrollY + 'px', a.style.position = 'absolute', a.style.opacity = '0', a.readOnly = !0, a.value = n, document.body.append(a);
  42. var r = document.getSelection(), o = 0 < r.rangeCount && r.getRangeAt(0);
  43. a.select(), a.setSelectionRange(0, n.length), a.readOnly = !1;
  44. var i = document.execCommand('copy');
  45. CONFIG.copycode.show_result && (t.querySelector('i').className = i ? 'fa fa-check' : 'fa fa-times'), a.blur(), t.blur(), o && (r.removeAllRanges(), r.addRange(o)), document.body.removeChild(a);
  46. }), n.addEventListener('mouseleave', function (e) {
  47. setTimeout(function () {
  48. e.target.querySelector('i').className = 'fa fa-clipboard';
  49. }, 300);
  50. });
  51. });
  52. },
  53. wrapTableWithBox: function () {
  54. document.querySelectorAll('table').forEach(function (e) {
  55. var t = document.createElement('div');
  56. t.className = 'table-container', e.wrap(t);
  57. });
  58. },
  59. registerVideoIframe: function () {
  60. document.querySelectorAll('iframe').forEach(function (t) {
  61. if (["www.youtube.com","player.vimeo.com","player.youku.com","player.bilibili.com","www.tudou.com"].some(function (e) {
  62. return t.src.includes(e);
  63. }) && !t.parentNode.matches('.video-container')) {
  64. var e = document.createElement('div');
  65. e.className = 'video-container', t.wrap(e);
  66. var n = Number(t.width), a = Number(t.height);
  67. n && a && (t.parentNode.style.paddingTop = a / n * 100 + '%');
  68. }
  69. });
  70. },
  71. registerScrollPercent: function () {
  72. var r = document.querySelector('.back-to-top'), o = document.querySelector('.reading-progress-bar');
  73. window.addEventListener('scroll', function () {
  74. if (r || o) {
  75. var e = document.querySelector('.container').offsetHeight, t = window.innerHeight, n = t < e ? e - t : document.body.scrollHeight - t, a = Math.min(100 * window.scrollY / n, 100);
  76. r && (r.classList.toggle('back-to-top-on', 50 < window.scrollY), r.querySelector('span').innerText = Math.round(a) + '%'), o && (o.style.width = a.toFixed(2) + '%');
  77. }
  78. }), r && r.addEventListener('click', function () {
  79. window.anime({
  80. targets: document.scrollingElement,
  81. duration: 500,
  82. easing: 'linear',
  83. scrollTop: 0
  84. });
  85. });
  86. },
  87. registerTabsTag: function () {
  88. document.querySelectorAll('.tabs ul.nav-tabs .tab').forEach(function (e) {
  89. e.addEventListener('click', function (e) {
  90. e.preventDefault();
  91. var t = e.currentTarget;
  92. if (!t.classList.contains('active')) {
  93. [].concat(_toConsumableArray(t.parentNode.children)).forEach(function (e) {
  94. e.classList.remove('active');
  95. }), t.classList.add('active');
  96. var n = document.getElementById(t.querySelector('a').getAttribute('href').replace('#', ''));
  97. [].concat(_toConsumableArray(n.parentNode.children)).forEach(function (e) {
  98. e.classList.remove('active');
  99. }), n.classList.add('active'), n.dispatchEvent(new Event('tabs:click', { bubbles: !0 }));
  100. }
  101. });
  102. }), window.dispatchEvent(new Event('tabs:register'));
  103. },
  104. registerCanIUseTag: function () {
  105. window.addEventListener('message', function (e) {
  106. var t = e.data;
  107. if ('string' == typeof t && t.includes('ciu_embed')) {
  108. var n = t.split(':')[1], a = t.split(':')[2];
  109. document.querySelector('iframe[data-feature=' + n + ']').style.height = parseInt(a, 10) + 5 + 'px';
  110. }
  111. }, !1);
  112. },
  113. registerActiveMenuItem: function () {
  114. document.querySelectorAll('.menu-item').forEach(function (e) {
  115. var t = e.querySelector('a[href]');
  116. if (t) {
  117. var n = t.pathname === location.pathname || t.pathname === location.pathname.replace('index.html', ''), a = !CONFIG.root.startsWith(t.pathname) && location.pathname.startsWith(t.pathname);
  118. e.classList.toggle('menu-item-active', t.hostname === location.hostname && (n || a));
  119. }
  120. });
  121. },
  122. registerLangSelect: function () {
  123. var n = document.querySelector('.lang-select');
  124. n && (n.value = CONFIG.page.lang, n.addEventListener('change', function () {
  125. var e = n.options[n.selectedIndex];
  126. document.querySelector('.lang-select-label span').innerText = e.text;
  127. var t = e.dataset.href;
  128. window.pjax ? window.pjax.loadUrl(t) : window.location.href = t;
  129. }));
  130. },
  131. registerSidebarTOC: function () {
  132. var i = document.querySelectorAll('.post-toc li'), c = [].concat(_toConsumableArray(i)).map(function (e) {
  133. var t = e.querySelector('a.nav-link');
  134. return t.addEventListener('click', function (e) {
  135. e.preventDefault();
  136. var t = document.getElementById(e.currentTarget.getAttribute('href').replace('#', '')).getBoundingClientRect().top + window.scrollY;
  137. window.anime({
  138. targets: document.scrollingElement,
  139. duration: 500,
  140. easing: 'linear',
  141. scrollTop: t + 10
  142. });
  143. }), document.getElementById(t.getAttribute('href').replace('#', ''));
  144. }), l = document.querySelector('.post-toc-wrap');
  145. !function r(o) {
  146. o = Math.floor(o + 10000);
  147. var t = new IntersectionObserver(function (e, t) {
  148. var n = document.documentElement.scrollHeight + 100;
  149. if (o < n)
  150. return t.disconnect(), void r(n);
  151. var a = function (e) {
  152. var t = 0, n = e[t];
  153. if (0 < n.boundingClientRect.top)
  154. return 0 === (t = c.indexOf(n.target)) ? 0 : t - 1;
  155. for (; t < e.length; t++) {
  156. if (!(e[t].boundingClientRect.top <= 0))
  157. return c.indexOf(n.target);
  158. n = e[t];
  159. }
  160. return c.indexOf(n.target);
  161. }(e);
  162. !function (e) {
  163. if (!e.classList.contains('active-current')) {
  164. document.querySelectorAll('.post-toc .active').forEach(function (e) {
  165. e.classList.remove('active', 'active-current');
  166. }), e.classList.add('active', 'active-current');
  167. for (var t = e.parentNode; !t.matches('.post-toc');)
  168. t.matches('li') && t.classList.add('active'), t = t.parentNode;
  169. window.anime({
  170. targets: l,
  171. duration: 200,
  172. easing: 'linear',
  173. scrollTop: l.scrollTop - l.offsetHeight / 2 + e.getBoundingClientRect().top - l.getBoundingClientRect().top
  174. });
  175. }
  176. }(i[a]);
  177. }, {
  178. rootMargin: o + 'px 0px -100% 0px',
  179. threshold: 0
  180. });
  181. c.forEach(function (e) {
  182. e && t.observe(e);
  183. });
  184. }(document.documentElement.scrollHeight);
  185. },
  186. hasMobileUA: function () {
  187. var e = navigator.userAgent;
  188. return /iPad|iPhone|Android|Opera Mini|BlackBerry|webOS|UCWEB|Blazer|PSP|IEMobile|Symbian/g.test(e);
  189. },
  190. isTablet: function () {
  191. return window.screen.width < 992 && 767 < window.screen.width && this.hasMobileUA();
  192. },
  193. isMobile: function () {
  194. return window.screen.width < 767 && this.hasMobileUA();
  195. },
  196. isDesktop: function () {
  197. return !this.isTablet() && !this.isMobile();
  198. },
  199. supportsPDFs: function () {
  200. var e = navigator.userAgent, t = e.includes('irefox') && 18 < parseInt(e.split('rv:')[1].split('.')[0], 10), n = void 0 !== navigator.mimeTypes['application/pdf'], a = /iphone|ipad|ipod/i.test(e.toLowerCase());
  201. return t || n && !a;
  202. },
  203. initSidebarDimension: function () {
  204. var e = document.querySelector('.sidebar-nav'), t = 'none' !== e.style.display ? e.offsetHeight : 0, n = CONFIG.sidebar.offset || 12, a = CONFIG.back2top.enable && CONFIG.back2top.sidebar ? document.querySelector('.back-to-top').offsetHeight : 0, r = 2 * CONFIG.sidebar.padding + t + a;
  205. 'Pisces' !== CONFIG.scheme && 'Gemini' !== CONFIG.scheme || (r += 2 * n - 22);
  206. var o = document.body.offsetHeight - r + 'px';
  207. document.querySelector('.site-overview-wrap').style.maxHeight = o, document.querySelector('.post-toc-wrap').style.maxHeight = o;
  208. },
  209. updateSidebarPosition: function () {
  210. var e = document.querySelector('.sidebar-nav'), t = document.querySelector('.post-toc');
  211. if (t ? (e.style.display = '', e.classList.add('motion-element'), document.querySelector('.sidebar-nav-toc').click()) : (e.style.display = 'none', e.classList.remove('motion-element'), document.querySelector('.sidebar-nav-overview').click()), NexT.utils.initSidebarDimension(), this.isDesktop() && 'Pisces' !== CONFIG.scheme && 'Gemini' !== CONFIG.scheme) {
  212. var n = CONFIG.page.sidebar;
  213. 'boolean' != typeof n && (n = 'always' === CONFIG.sidebar.display || 'post' === CONFIG.sidebar.display && t), n && window.dispatchEvent(new Event('sidebar:show'));
  214. }
  215. },
  216. getScript: function (e, n, t) {
  217. if (t)
  218. n();
  219. else {
  220. var a = document.createElement('script');
  221. a.onload = a.onreadystatechange = function (e, t) {
  222. !t && a.readyState && !/loaded|complete/.test(a.readyState) || (a.onload = a.onreadystatechange = null, a = void 0, !t && n && setTimeout(n, 0));
  223. }, a.src = e, document.head.appendChild(a);
  224. }
  225. },
  226. loadComments: function (e, n) {
  227. if (CONFIG.comments.lazyload && e) {
  228. var t = new IntersectionObserver(function (e, t) {
  229. e[0].isIntersecting && (n(), t.disconnect());
  230. });
  231. return t.observe(e), t;
  232. }
  233. n();
  234. }
  235. };