| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
const LINK_ICON = '🔗'; |
| 23 |
const COPIED_ICON = ' - Copied'; |
| 24 |
const ERROR_ICON = '❌'; |
| 25 |
const RESET_DELAY = 2000; |
| 26 |
const HEADING_SELECTORS = 'h2, h3, h4, h5, h6'; |
| 27 |
|
| 28 |
|
| 29 |
class HeadingCopyManager |
| 30 |
{ |
| 31 |
|
| 32 |
static accessibility = null; |
| 33 |
|
| 34 |
|
| 35 |
static initialize() |
| 36 |
{ |
| 37 |
|
| 38 |
if ( !this.accessibility ) { |
| 39 |
this.accessibility = new globalThis.fheroes2.AccessibilityManager(); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
document.querySelectorAll( HEADING_SELECTORS ).forEach( heading => this.createHeadingLink( heading ) ); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static generateHeadingId( text ) |
| 51 |
{ |
| 52 |
return text.toLowerCase().replace( /[^a-z0-9]+/g, '-' ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
static createIconElement() |
| 58 |
{ |
| 59 |
const icon = this.accessibility.createAccessibleSpan( { text : LINK_ICON, ariaLabel : 'Copy link to heading', className : 'heading-anchor' } ); |
| 60 |
return icon; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static updateIconState( icon, success, headingText ) |
| 69 |
{ |
| 70 |
this.accessibility.updateElementState( { |
| 71 |
element : icon, |
| 72 |
success : success, |
| 73 |
successMessage : `Link to ${headingText} copied to clipboard`, |
| 74 |
errorMessage : `Failed to copy link to ${headingText}`, |
| 75 |
successText : COPIED_ICON, |
| 76 |
errorText : ERROR_ICON, |
| 77 |
defaultText : LINK_ICON, |
| 78 |
resetDelay : RESET_DELAY, |
| 79 |
isHtml : true |
| 80 |
} ); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
static handleCopy( e, heading, icon ) |
| 89 |
{ |
| 90 |
e.preventDefault(); |
| 91 |
e.stopPropagation(); |
| 92 |
|
| 93 |
const url = new URL( globalThis.location.href ); |
| 94 |
url.hash = heading.id; |
| 95 |
|
| 96 |
try { |
| 97 |
navigator.clipboard.writeText( url.toString() ) |
| 98 |
.then( () => { |
| 99 |
this.updateIconState( icon, true, heading.textContent ); |
| 100 |
|
| 101 |
|
| 102 |
history.pushState( null, null, `#${heading.id}` ); |
| 103 |
} ) |
| 104 |
.catch( err => { |
| 105 |
console.error( 'Failed to copy:', err ); |
| 106 |
this.updateIconState( icon, false, heading.textContent ); |
| 107 |
} ); |
| 108 |
} |
| 109 |
catch ( err ) { |
| 110 |
console.error( 'Failed to copy:', err ); |
| 111 |
this.updateIconState( icon, false, heading.textContent ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
static setupEventListeners( icon, heading ) |
| 120 |
{ |
| 121 |
|
| 122 |
icon.addEventListener( 'click', ( e ) => this.handleCopy( e, heading, icon ) ); |
| 123 |
|
| 124 |
|
| 125 |
this.accessibility.setupKeyboardEvents( icon, ( e ) => this.handleCopy( e, heading, icon ) ); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
static createHeadingLink( heading ) |
| 132 |
{ |
| 133 |
|
| 134 |
if ( !heading.id ) { |
| 135 |
heading.id = this.generateHeadingId( heading.textContent ); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
const icon = this.createIconElement(); |
| 140 |
|
| 141 |
|
| 142 |
heading.appendChild( icon ); |
| 143 |
|
| 144 |
|
| 145 |
this.setupEventListeners( icon, heading ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
document.addEventListener( 'DOMContentLoaded', () => { HeadingCopyManager.initialize(); } ); |