@@ -25,3 +25,61 @@ document.addEventListener("DOMContentLoaded", function () {
2525
2626 document . getElementById ( "current-year" ) . textContent = new Date ( ) . getFullYear ( ) . toString ( ) ;
2727} ) ;
28+
29+ function closeMdActionsMenus ( except ) {
30+ document . querySelectorAll ( '.md-actions-toggle[aria-expanded="true"]' ) . forEach ( function ( toggle ) {
31+ if ( toggle === except ) {
32+ return ;
33+ }
34+ toggle . setAttribute ( 'aria-expanded' , 'false' ) ;
35+ toggle . parentNode . querySelector ( '.md-actions-menu' ) . classList . remove ( 'show' ) ;
36+ } ) ;
37+ }
38+
39+ function toggleMdActionsMenu ( toggle ) {
40+ var expanded = toggle . getAttribute ( 'aria-expanded' ) === 'true' ;
41+ closeMdActionsMenus ( toggle ) ;
42+ toggle . setAttribute ( 'aria-expanded' , expanded ? 'false' : 'true' ) ;
43+ toggle . parentNode . querySelector ( '.md-actions-menu' ) . classList . toggle ( 'show' , ! expanded ) ;
44+ }
45+
46+ function copyMarkdownPage ( button ) {
47+ var label = button . querySelector ( '.label' ) ;
48+ var original = label . textContent ;
49+
50+ var restore = function ( message ) {
51+ label . textContent = message ;
52+ window . setTimeout ( function ( ) {
53+ label . textContent = original ;
54+ } , 2000 ) ;
55+ } ;
56+
57+ fetch ( button . dataset . copyUrl , { headers : { 'Accept' : 'text/markdown, text/plain' } } )
58+ . then ( function ( response ) {
59+ if ( ! response . ok ) {
60+ throw new Error ( 'HTTP ' + response . status ) ;
61+ }
62+ return response . text ( ) ;
63+ } )
64+ . then ( async function ( markdown ) {
65+ return await navigator . clipboard . writeText ( markdown ) ;
66+ } )
67+ . then ( function ( ) {
68+ restore ( 'Copied!' ) ;
69+ } )
70+ . catch ( function ( ) {
71+ restore ( 'Copy failed' ) ;
72+ } ) ;
73+ }
74+
75+ document . addEventListener ( 'click' , function ( event ) {
76+ if ( ! event . target . closest ( '.md-actions' ) ) {
77+ closeMdActionsMenus ( ) ;
78+ }
79+ } ) ;
80+
81+ document . addEventListener ( 'keydown' , function ( event ) {
82+ if ( event . key === 'Escape' ) {
83+ closeMdActionsMenus ( ) ;
84+ }
85+ } ) ;
0 commit comments