1
+ <% if (locals .siteData .feature ? .share ? .enable ) { % >
2
+ < button type= " button" id= " share-btn" class = " text-gray-400 hover:text-blue-700 dark:text-gray-400 dark:hover:text-gray-300" >
3
+ < svg xmlns= " http://www.w3.org/2000/svg" width= " 24" height= " 24" viewBox= " 0 0 24 24" fill= " none" stroke= " currentColor" stroke- width= " 2" stroke- linecap= " round"
4
+ stroke- linejoin= " round" class = " w-4 h-4" >
5
+ < path d= " M4 12v8a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-8" / >
6
+ < polyline points= " 16 6 12 2 8 6" / >
7
+ < line x1= " 12" y1= " 2" x2= " 12" y2= " 15" / >
8
+ < / svg>
9
+ < / button>
10
+
11
+ < div id= " share-popup" class = " hidden fixed inset-0 z-50" onclick= " handleClickOutside(event)" >
12
+ < div class = " absolute inset-0 bg-gray-500 bg-opacity-75 dark:bg-gray-900 dark:bg-opacity-75" >< / div>
13
+ < div class = " relative z-10 flex items-end sm:items-center justify-center min-h-full p-4 text-center sm:p-0" >
14
+ < div class = " relative transform overflow-visible rounded-lg bg-white dark:bg-gray-800 px-4 pb-4 pt-5 text-left shadow-xl transition-all sm:my-8 w-fit" >
15
+ < div class = " social-share text-center"
16
+ data- sites= " <%= Object.entries(locals.siteData.feature?.share?.options || {})
17
+ .filter(([_, enabled]) => enabled)
18
+ .map(([platform]) => platform)
19
+ .join(',') %>"
20
+ data- mobile- sites= " <%= Object.entries(locals.siteData.feature?.share?.options || {})
21
+ .filter(([_, enabled]) => enabled)
22
+ .map(([platform]) => platform)
23
+ .join(',') %>"
24
+ data- wechat- qrcode- title= " 微信扫一扫:分享"
25
+ data- wechat- qrcode- helper= " 微信扫一扫后点击右上角分享" >
26
+ < / div>
27
+ < / div>
28
+ < / div>
29
+ < / div>
30
+
31
+ < link rel= " stylesheet" href= " <%= locals.siteData.feature?.share?.cdn || 'https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16' %>/css/share.min.css" >
32
+ < script src= " <%= locals.siteData.feature?.share?.cdn || 'https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16' %>/js/social-share.min.js" >< / script>
33
+
34
+ < style>
35
+ /* 修复微信二维码显示被遮挡的问题 */
36
+ .social - share .wechat - qrcode {
37
+ z- index: 9999 ;
38
+ width: fit- content;
39
+ }
40
+ < / style>
41
+
42
+ < script>
43
+ document .getElementById (' share-btn' )? .addEventListener (' click' , function () {
44
+ document .getElementById (' share-popup' )? .classList .remove (' hidden' );
45
+ document .body .style .overflow = ' hidden' ;
46
+ });
47
+
48
+ function handleClickOutside (event ) {
49
+ const sharePopup = document .getElementById (' share-popup' );
50
+ const shareContent = sharePopup? .querySelector (' .transform' );
51
+
52
+ // 如果点击的是弹窗内容之外的区域
53
+ if (event .target === sharePopup || ! shareContent? .contains (event .target )) {
54
+ hideSharePopup ();
55
+ }
56
+ }
57
+
58
+ function hideSharePopup () {
59
+ document .getElementById (' share-popup' )? .classList .add (' hidden' );
60
+ document .body .style .overflow = ' ' ;
61
+ }
62
+
63
+ // 处理 ESC 键关闭
64
+ document .addEventListener (' keydown' , function (e ) {
65
+ if (e .key === ' Escape' ) {
66
+ hideSharePopup ();
67
+ }
68
+ });
69
+
70
+ document .addEventListener (' DOMContentLoaded' , function () {
71
+ // 初始化 social share
72
+ socialShare (' .social-share' , {
73
+ wechatQrcodeTitle: ' 微信扫一扫:分享' ,
74
+ wechatQrcodeHelper: ' 微信扫一扫后点击右上角分享' ,
75
+ url: window .location .href ,
76
+ source: document .title ,
77
+ wechat : function (elem , data ) {
78
+ // 自定义微信点击行为
79
+ if (elem .classList .contains (' icon-wechat' )) {
80
+ return false ; // 阻止默认跳转
81
+ }
82
+ }
83
+ });
84
+
85
+ // 防止微信图标点击跳转
86
+ document .querySelector (' .icon-wechat' )? .addEventListener (' click' , function (e ) {
87
+ e .preventDefault ();
88
+ e .stopPropagation ();
89
+ return false ;
90
+ }, true );
91
+ });
92
+ < / script>
93
+ < % } % >
0 commit comments