File tree Expand file tree Collapse file tree 2 files changed +26
-10
lines changed
Expand file tree Collapse file tree 2 files changed +26
-10
lines changed Original file line number Diff line number Diff line change 1111 </s-button >
1212
1313 <s-tooltip align =" right" >
14- <s-icon-button
15- slot =" trigger"
16- @click =" copyCode"
17- >
14+ <s-icon-button slot =" trigger" @click =" copyCode" >
1815 <SIconCopy />
1916 </s-icon-button >
2017 {{ t("buttons.copy") }}
2118 </s-tooltip >
2219 </div >
2320 <s-fold :folded =" folded" >
2421 <s-scroll-view id =" formattedCode" >
25- <pre >{{ formatted }}</pre >
22+ <pre ref = " formattedCodePre " >{{ formatted }}</pre >
2623 </s-scroll-view >
2724 </s-fold >
2825 </div >
@@ -60,12 +57,29 @@ watch(
6057const folded = ref (true );
6158
6259import { Snackbar } from " sober" ;
60+ const formattedCodePre = ref <HTMLPreElement >();
6361function copyCode() {
64- navigator .clipboard .writeText (formatted .value );
65- Snackbar .builder ({
66- text: t (" title.copySuccess" ),
67- type: " success" ,
68- });
62+ try {
63+ navigator .clipboard .writeText (formatted .value );
64+ Snackbar .builder ({
65+ text: t (" title.copySuccess" ),
66+ type: " success" ,
67+ });
68+ } catch (e ) {
69+ if (e instanceof Error ) console .error (e );
70+ if (window .location .href .match (/ ^ http:\/\/ / ))
71+ Snackbar .builder ({
72+ text:
73+ t (" title.copyFail" ) +
74+ " : Pages over HTTP are not allowed to use clipboard API" ,
75+ type: " error" ,
76+ });
77+ else
78+ Snackbar .builder ({
79+ text: t (" title.copyFail" ),
80+ type: " error" ,
81+ });
82+ }
6983}
7084 </script >
7185
Original file line number Diff line number Diff line change @@ -60,6 +60,7 @@ export default {
6060 copySuccess : "已复制" ,
6161 deleteSuccess : "已删除" ,
6262 moreOptions : "更多选项" ,
63+ copyFail : "复制失败" ,
6364 } ,
6465 } ,
6566 "en-US" : {
@@ -117,6 +118,7 @@ export default {
117118 copySuccess : "Copied" ,
118119 deleteSuccess : "Deleted" ,
119120 moreOptions : "More options" ,
121+ copyFail : "Copy failed" ,
120122 } ,
121123 } ,
122124 } ,
You can’t perform that action at this time.
0 commit comments