@@ -2,7 +2,7 @@ document.addEventListener("DOMContentLoaded", function () {
2
2
volantis . requestAnimationFrame ( ( ) => {
3
3
VolantisApp . init ( ) ;
4
4
VolantisApp . subscribe ( ) ;
5
- VolantisFancyBox . init ( ) ;
5
+ new VolantisFancyBox ( ) ;
6
6
highlightKeyWords . startFromURL ( ) ;
7
7
locationHash ( ) ;
8
8
} ) ;
@@ -670,118 +670,117 @@ const VolantisApp = (() => {
670
670
Object . freeze ( VolantisApp ) ;
671
671
672
672
/* FancyBox */
673
- const VolantisFancyBox = ( ( ) => {
674
- const fn = { } ;
675
-
676
- fn . loadFancyBox = ( done ) => {
677
- volantis . css ( volantis . GLOBAL_CONFIG . cdn . fancybox_css ) ;
678
- volantis . js ( volantis . GLOBAL_CONFIG . cdn . fancybox_js ) . then ( ( ) => {
679
- if ( done ) done ( ) ;
680
- } )
673
+ class VolantisFancyBox {
674
+ constructor ( checkMain = true ) {
675
+ this . option = {
676
+ Hash : false ,
677
+ groupAll : true ,
678
+ caption : ( fancybox , slide ) => slide . thumbEl ?. alt || "" ,
679
+ wheel : "slide" ,
680
+ contentClick : 'iterateZoom' ,
681
+ Thumbs : {
682
+ showOnStart : false
683
+ } ,
684
+ Images : {
685
+ content : ( _ref , slide ) => {
686
+ const imgElement = slide . thumbEl ;
687
+ const pictureElement = imgElement . closest ( 'picture' ) ;
688
+ if ( imgElement . hasAttribute ( 'data-src' ) ) {
689
+ imgElement . setAttribute ( 'src' , imgElement . getAttribute ( 'data-src' ) ) ;
690
+ }
691
+ if ( pictureElement ) {
692
+ pictureElement . classList . remove ( "lazy" ) ;
693
+ let sources = pictureElement . getElementsByTagName ( 'source' ) ;
694
+ for ( let source of sources ) {
695
+ if ( source . hasAttribute ( 'data-srcset' ) ) {
696
+ source . setAttribute ( 'srcset' , source . getAttribute ( 'data-srcset' ) ) ;
697
+ }
698
+ }
699
+ return pictureElement . outerHTML ;
700
+ } else {
701
+ return imgElement . outerHTML ;
702
+ }
703
+ } ,
704
+ Panzoom : {
705
+ maxScale : 1.5 ,
706
+ panMode : "mousemove" ,
707
+ mouseMoveFactor : 1.1 ,
708
+ mouseMoveFriction : 0.12 ,
709
+ }
710
+ } ,
711
+ Toolbar : {
712
+ display : {
713
+ left : [ "infobar" ] ,
714
+ middle : [
715
+ "zoomIn" ,
716
+ "zoomOut" ,
717
+ "toggle1to1" ,
718
+ "rotateCCW" ,
719
+ "rotateCW" ,
720
+ "flipX" ,
721
+ "flipY" ,
722
+ ] ,
723
+ right : [ "slideshow" , "download" , "thumbs" , "close" ] ,
724
+ } ,
725
+ }
726
+ } ;
727
+ this . #init( checkMain ) ;
681
728
}
682
729
683
- /**
684
- * 加载及处理
685
- *
686
- * @param {* } checkMain 是否只处理文章区域的文章
687
- * @param {* } done FancyBox 加载完成后的动作,默认执行分组绑定
688
- * @returns
689
- */
690
- fn . init = ( checkMain = true , done = fn . groupBind ) => {
730
+ #init( checkMain ) {
691
731
if ( ! document . querySelector ( ".md .gallery img, .fancybox" ) && checkMain ) return ;
732
+ this . groupBind ( ) ;
733
+ }
734
+
735
+ async #checkFancybox( done ) {
692
736
if ( typeof Fancybox === "undefined" ) {
693
- fn . loadFancyBox ( done ) ;
737
+ await volantis . css ( volantis . GLOBAL_CONFIG . cdn . fancybox_css ) ;
738
+ await volantis . js ( volantis . GLOBAL_CONFIG . cdn . fancybox_js ) ;
739
+ done . call ( this ) ;
694
740
} else {
695
- done ( ) ;
741
+ done . call ( this ) ;
696
742
}
697
743
}
698
744
699
- /**
700
- * 图片元素预处理
701
- *
702
- * @param {* } selectors 选择器
703
- * @param {* } name 分组
704
- */
705
- fn . elementHandling = ( selectors , name ) => {
706
- const nodeList = document . querySelectorAll ( selectors ) ;
707
- nodeList . forEach ( $item => {
745
+ #elementHandling( selectors , groupName ) {
746
+ if ( ! selectors ) return ;
747
+ document . querySelectorAll ( selectors ) . forEach ( $item => {
708
748
if ( $item . hasAttribute ( 'fancybox' ) ) return ;
709
749
$item . setAttribute ( 'fancybox' , '' ) ;
710
750
const $link = document . createElement ( 'a' ) ;
711
- $link . setAttribute ( 'href' , $item . src ) ;
712
- $link . setAttribute ( 'data-caption' , $item . alt ) ;
713
- $link . setAttribute ( 'data-fancybox' , name ) ;
751
+ $link . setAttribute ( 'href' , $item . src || $item . dataset ?. src ) ;
752
+ $link . setAttribute ( 'data-caption' , $item . alt || '' ) ;
753
+ $link . setAttribute ( 'data-fancybox' , groupName ) ;
714
754
$link . classList . add ( 'fancybox' ) ;
715
755
$link . append ( $item . cloneNode ( ) ) ;
716
756
$item . replaceWith ( $link ) ;
717
- } )
757
+ } ) ;
718
758
}
719
759
720
- /**
721
- * 原生绑定
722
- *
723
- * @param {* } selectors 选择器
724
- */
725
- fn . bind = ( selectors ) => {
726
- fn . init ( false , ( ) => {
727
- Fancybox . bind ( selectors , {
728
- groupAll : true ,
729
- Hash : false ,
730
- hideScrollbar : false ,
731
- Thumbs : {
732
- autoStart : false ,
733
- } ,
734
- caption : function ( fancybox , slide ) {
735
- return slide . thumbEl ?. alt || "" ;
736
- }
737
- } ) ;
760
+ bind ( selectors ) {
761
+ this . #checkFancybox( ( ) => {
762
+ Fancybox ?. unbind ( selectors ) ;
763
+ Fancybox ?. bind ( selectors , this . option ) ;
738
764
} ) ;
739
765
}
740
766
741
- /**
742
- * 分组绑定
743
- *
744
- * @param {* } groupName 分组名称
745
- */
746
- fn . groupBind = ( groupName = null ) => {
747
- const group = new Set ( ) ;
748
-
749
- document . querySelectorAll ( ".gallery" ) . forEach ( ele => {
750
- if ( ele . querySelector ( "img" ) ) {
751
- group . add ( ele . getAttribute ( 'data-group' ) || 'default' ) ;
752
- }
753
- } )
754
-
755
- if ( ! ! groupName ) group . add ( groupName ) ;
756
-
757
- for ( const iterator of group ) {
758
- Fancybox . unbind ( '[data-fancybox="' + iterator + '"]' ) ;
759
- Fancybox . bind ( '[data-fancybox="' + iterator + '"]' , {
760
- Hash : false ,
761
- hideScrollbar : false ,
762
- Thumbs : {
763
- autoStart : false ,
767
+ groupBind ( selectors , groupName = 'default' ) {
768
+ this . #checkFancybox( ( ) => {
769
+ this . #elementHandling( selectors , groupName ) ;
770
+ const group = new Set ( ) ;
771
+ document . querySelectorAll ( '.gallery' ) . forEach ( ele => {
772
+ if ( ele . querySelector ( "img" ) ) {
773
+ group . add ( ele . getAttribute ( 'data-group' ) || 'default' ) ;
764
774
}
765
775
} ) ;
766
- }
767
- }
768
-
769
- return {
770
- init : fn . init ,
771
- bind : fn . bind ,
772
- groupBind : ( selectors , groupName = 'default' ) => {
773
- try {
774
- fn . elementHandling ( selectors , groupName ) ;
775
- fn . init ( false , ( ) => {
776
- fn . groupBind ( groupName )
777
- } ) ;
778
- } catch ( error ) {
779
- console . error ( error )
780
- }
781
- }
776
+ if ( groupName ) group . add ( groupName ) ;
777
+ group . forEach ( name => {
778
+ Fancybox ?. unbind ( `[data-fancybox="${ name } "]` ) ;
779
+ Fancybox ?. bind ( `[data-fancybox="${ name } "]` , this . option ) ;
780
+ } ) ;
781
+ } ) ;
782
782
}
783
- } ) ( )
784
- Object . freeze ( VolantisFancyBox ) ;
783
+ }
785
784
786
785
// highlightKeyWords 与 搜索功能搭配 https://github.com/next-theme/hexo-theme-next/blob/eb194a7258058302baf59f02d4b80b6655338b01/source/js/third-party/search/local-search.js
787
786
// Question: 锚点稳定性未知
0 commit comments