plugins.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* global Fluid, CONFIG */
  2. HTMLElement.prototype.wrap = function(wrapper) {
  3. this.parentNode.insertBefore(wrapper, this);
  4. this.parentNode.removeChild(this);
  5. wrapper.appendChild(this);
  6. };
  7. Fluid.plugins = {
  8. typing: function(text) {
  9. if (!('Typed' in window)) { return; }
  10. var typed = new window.Typed('#subtitle', {
  11. strings: [
  12. ' ',
  13. text
  14. ],
  15. cursorChar: CONFIG.typing.cursorChar,
  16. typeSpeed : CONFIG.typing.typeSpeed,
  17. loop : CONFIG.typing.loop
  18. });
  19. typed.stop();
  20. var subtitle = document.getElementById('subtitle');
  21. if (subtitle) {
  22. subtitle.innerText = '';
  23. }
  24. jQuery(document).ready(function() {
  25. typed.start();
  26. });
  27. },
  28. fancyBox: function(selector) {
  29. if (!CONFIG.image_zoom.enable || !('fancybox' in jQuery)) { return; }
  30. jQuery(selector || '.markdown-body :not(a) > img, .markdown-body > img').each(function() {
  31. var $image = jQuery(this);
  32. var imageUrl = $image.attr('data-src') || $image.attr('src') || '';
  33. if (CONFIG.image_zoom.img_url_replace) {
  34. var rep = CONFIG.image_zoom.img_url_replace;
  35. var r1 = rep[0] || '';
  36. var r2 = rep[1] || '';
  37. if (r1) {
  38. if (/^re:/.test(r1)) {
  39. r1 = r1.replace(/^re:/, '');
  40. var reg = new RegExp(r1, 'gi');
  41. imageUrl = imageUrl.replace(reg, r2);
  42. } else {
  43. imageUrl = imageUrl.replace(r1, r2);
  44. }
  45. }
  46. }
  47. var $imageWrap = $image.wrap(`
  48. <a class="fancybox fancybox.image" href="${imageUrl}"
  49. itemscope itemtype="http://schema.org/ImageObject" itemprop="url"></a>`
  50. ).parent('a');
  51. if ($imageWrap.length !== 0) {
  52. if ($image.is('.group-image-container img')) {
  53. $imageWrap.attr('data-fancybox', 'group').attr('rel', 'group');
  54. } else {
  55. $imageWrap.attr('data-fancybox', 'default').attr('rel', 'default');
  56. }
  57. var imageTitle = $image.attr('title') || $image.attr('alt');
  58. if (imageTitle) {
  59. $imageWrap.attr('title', imageTitle).attr('data-caption', imageTitle);
  60. }
  61. }
  62. });
  63. jQuery.fancybox.defaults.hash = false;
  64. jQuery('.fancybox').fancybox({
  65. loop : true,
  66. helpers: {
  67. overlay: {
  68. locked: false
  69. }
  70. }
  71. });
  72. },
  73. imageCaption: function(selector) {
  74. if (!CONFIG.image_caption.enable) { return; }
  75. jQuery(selector || `.markdown-body > p > img, .markdown-body > figure > img,
  76. .markdown-body > p > a.fancybox, .markdown-body > figure > a.fancybox`).each(function() {
  77. var $target = jQuery(this);
  78. var $figcaption = $target.next('figcaption');
  79. if ($figcaption.length !== 0) {
  80. $figcaption.addClass('image-caption');
  81. } else {
  82. var imageTitle = $target.attr('title') || $target.attr('alt');
  83. if (imageTitle) {
  84. $target.after(`<figcaption aria-hidden="true" class="image-caption">${imageTitle}</figcaption>`);
  85. }
  86. }
  87. });
  88. },
  89. codeWidget() {
  90. var enableLang = CONFIG.code_language.enable && CONFIG.code_language.default;
  91. var enableCopy = CONFIG.copy_btn && 'ClipboardJS' in window;
  92. if (!enableLang && !enableCopy) {
  93. return;
  94. }
  95. function getBgClass(ele) {
  96. return Fluid.utils.getBackgroundLightness(ele) >= 0 ? 'code-widget-light' : 'code-widget-dark';
  97. }
  98. var copyTmpl = '';
  99. copyTmpl += '<div class="code-widget">';
  100. copyTmpl += 'LANG';
  101. copyTmpl += '</div>';
  102. jQuery('.markdown-body pre').each(function() {
  103. var $pre = jQuery(this);
  104. if ($pre.find('code.mermaid').length > 0) {
  105. return;
  106. }
  107. if ($pre.find('span.line').length > 0) {
  108. return;
  109. }
  110. var lang = '';
  111. if (enableLang) {
  112. lang = CONFIG.code_language.default;
  113. if ($pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2 && $pre.children().hasClass('hljs')) {
  114. lang = $pre[0].children[0].classList[1];
  115. } else if ($pre[0].getAttribute('data-language')) {
  116. lang = $pre[0].getAttribute('data-language');
  117. } else if ($pre.parent().hasClass('sourceCode') && $pre[0].children.length > 0 && $pre[0].children[0].classList.length >= 2) {
  118. lang = $pre[0].children[0].classList[1];
  119. $pre.parent().addClass('code-wrapper');
  120. } else if ($pre.parent().hasClass('markdown-body') && $pre[0].classList.length === 0) {
  121. $pre.wrap('<div class="code-wrapper"></div>');
  122. }
  123. lang = lang.toUpperCase().replace('NONE', CONFIG.code_language.default);
  124. }
  125. $pre.append(copyTmpl.replace('LANG', lang).replace('code-widget">',
  126. getBgClass($pre[0]) + (enableCopy ? ' code-widget copy-btn" data-clipboard-snippet><i class="iconfont icon-copy"></i>' : ' code-widget">')));
  127. if (enableCopy) {
  128. var clipboard = new ClipboardJS('.copy-btn', {
  129. target: function(trigger) {
  130. var nodes = trigger.parentNode.childNodes;
  131. for (var i = 0; i < nodes.length; i++) {
  132. if (nodes[i].tagName === 'CODE') {
  133. return nodes[i];
  134. }
  135. }
  136. }
  137. });
  138. clipboard.on('success', function(e) {
  139. e.clearSelection();
  140. e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-copy', 'icon-success');
  141. setTimeout(function() {
  142. e.trigger.innerHTML = e.trigger.innerHTML.replace('icon-success', 'icon-copy');
  143. }, 2000);
  144. });
  145. }
  146. });
  147. }
  148. };