@@ -20,10 +20,9 @@ export default class Page {
2020 */
2121 static get CSS ( ) {
2222 return {
23+ copyButton : 'copy-button' ,
24+ copyButtonCopied : 'copy-button__copied' ,
2325 page : 'page' ,
24- copyLinkBtn : 'block-header__copy-link' ,
25- header : 'block-header--anchor' ,
26- headerLinkCopied : 'block-header--link-copied' ,
2726 } ;
2827 }
2928
@@ -35,11 +34,15 @@ export default class Page {
3534 this . tableOfContent = this . createTableOfContent ( ) ;
3635
3736 /**
38- * Add click event listener to capture copy link button clicks
37+ * Add click event listener
3938 */
4039 const page = document . querySelector ( `.${ Page . CSS . page } ` ) ;
4140
42- page . addEventListener ( 'click' , this . copyAnchorLinkIfNeeded ) ;
41+ page . addEventListener ( 'click' , ( event ) => {
42+ if ( event . target . classList . contains ( Page . CSS . copyButton ) ) {
43+ this . handleCopyButtonClickEvent ( event ) ;
44+ }
45+ } ) ;
4346 }
4447
4548 /**
@@ -69,10 +72,7 @@ export default class Page {
6972 try {
7073 // eslint-disable-next-line no-new
7174 new TableOfContent ( {
72- tagSelector :
73- 'h2.block-header--anchor,' +
74- 'h3.block-header--anchor,' +
75- 'h4.block-header--anchor' ,
75+ tagSelector : '.block-header' ,
7676 appendTo : document . getElementById ( 'layout-sidebar-right' ) ,
7777 } ) ;
7878 } catch ( error ) {
@@ -81,27 +81,31 @@ export default class Page {
8181 }
8282
8383 /**
84- * Checks if ' copy link' button was clicked and copies the link to clipboard
84+ * Handles copy button click events
8585 *
86- * @param e - click event
86+ * @param {Event } e - Event Object.
87+ * @returns {Promise<void> }
8788 */
88- copyAnchorLinkIfNeeded = async ( e ) => {
89- const copyLinkButtonClicked = e . target . closest ( `. ${ Page . CSS . copyLinkBtn } ` ) ;
89+ async handleCopyButtonClickEvent ( { target } ) {
90+ if ( target . classList . contains ( Page . CSS . copyButtonCopied ) ) return ;
9091
91- if ( ! copyLinkButtonClicked ) {
92- return ;
93- }
92+ let textToCopy = target . getAttribute ( 'data-text-to-copy' ) ;
93+ if ( ! textToCopy ) return ;
94+
95+ // Check if text to copy is an anchor link
96+ if ( / ^ # \S * $ / . test ( textToCopy ) )
97+ textToCopy = window . location . origin + window . location . pathname + textToCopy ;
9498
95- const header = e . target . closest ( `. ${ Page . CSS . header } ` ) ;
96- const link = header . querySelector ( 'a' ) . href ;
99+ try {
100+ await copyToClipboard ( textToCopy ) ;
97101
98- await copyToClipboard ( link ) ;
99- header . classList . add ( Page . CSS . headerLinkCopied ) ;
102+ target . classList . add ( Page . CSS . copyButtonCopied ) ;
103+ target . addEventListener ( 'mouseleave' , ( ) => {
104+ setTimeout ( ( ) => target . classList . remove ( Page . CSS . copyButtonCopied ) , 5e2 ) ;
105+ } , { once : true } ) ;
100106
101- header . addEventListener ( 'mouseleave' , ( ) => {
102- setTimeout ( ( ) => {
103- header . classList . remove ( Page . CSS . headerLinkCopied ) ;
104- } , 500 ) ;
105- } , { once : true } ) ;
107+ } catch ( error ) {
108+ console . error ( error ) ; // @todo send to Hawk
109+ }
106110 }
107111}
0 commit comments