Debian Codegraph @

docs/assets/js/heading-copy-manager.js

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2025 *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 // Constants
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 // Class to handle heading copy functionality
29 class HeadingCopyManager
30 {
31 // Static instance of AccessibilityManager
32 static accessibility = null;
33
34 // Static initialization method
35 static initialize()
36 {
37 // Initialize accessibility manager if not already initialized
38 if ( !this.accessibility ) {
39 this.accessibility = new globalThis.fheroes2.AccessibilityManager();
40 }
41
42 // Apply to all headings
43 document.querySelectorAll( HEADING_SELECTORS ).forEach( heading => this.createHeadingLink( heading ) );
44 }
45
46 // Creates a unique ID for a heading based on its text content
47 // Parameters:
48 // text - The heading text
49 // Returns: A unique ID
50 static generateHeadingId( text )
51 {
52 return text.toLowerCase().replace( /[^a-z0-9]+/g, '-' );
53 }
54
55 // Creates a link icon element with proper accessibility attributes
56 // Returns: The icon element
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 // Updates the icon appearance based on the copy result
64 // Parameters:
65 // icon - The icon element
66 // success - Whether the copy was successful
67 // headingText - The text of the heading
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 // Use innerHTML for the heading icon
80 } );
81 }
82
83 // Handles the copy functionality for a heading
84 // Parameters:
85 // e - The event object
86 // heading - The heading element
87 // icon - The icon element
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 // Update URL without scrolling
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 // Sets up event listeners for a heading icon
116 // Parameters:
117 // icon - The icon element
118 // heading - The heading element
119 static setupEventListeners( icon, heading )
120 {
121 // Add click event handler
122 icon.addEventListener( 'click', ( e ) => this.handleCopy( e, heading, icon ) );
123
124 // Add keyboard event handler
125 this.accessibility.setupKeyboardEvents( icon, ( e ) => this.handleCopy( e, heading, icon ) );
126 }
127
128 // Creates a heading link with proper accessibility attributes
129 // Parameters:
130 // heading - The heading element
131 static createHeadingLink( heading )
132 {
133 // Generate ID if needed
134 if ( !heading.id ) {
135 heading.id = this.generateHeadingId( heading.textContent );
136 }
137
138 // Create icon element
139 const icon = this.createIconElement();
140
141 // Add icon to the heading
142 heading.appendChild( icon );
143
144 // Set up event listeners
145 this.setupEventListeners( icon, heading );
146 }
147 }
148
149 // Initialize when the DOM is fully loaded
150 document.addEventListener( 'DOMContentLoaded', () => { HeadingCopyManager.initialize(); } );