Debian Codegraph @

docs/assets/js/code-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 // Class to handle code copy functionality
22 class CodeCopyManager
23 {
24 // Static instance of AccessibilityManager
25 static accessibility = null;
26
27 // Static initialization method
28 static initialize()
29 {
30 // Initialize accessibility manager if not already initialized
31 if ( !this.accessibility ) {
32 this.accessibility = new globalThis.fheroes2.AccessibilityManager();
33 }
34
35 // Apply to all code blocks
36 document.querySelectorAll( "div.highlight" ).forEach( block => this.initializeCodeBlock( block ) );
37 }
38
39 // Creates a copy button using the template
40 // Returns: The button element
41 static createCopyButton()
42 {
43 return this.accessibility.createAccessibleButton( { text : 'Copy', ariaLabel : 'Copy code to clipboard', className : 'btn btn-primary copy-btn' } );
44 }
45
46 // Updates the button appearance and announces success to screen readers
47 // Parameters:
48 // button - The button element
49 // success - Whether the copy was successful
50 // code - The code content that was copied
51 static updateButtonState( button, success, code )
52 {
53 // Truncate the code if it's too long
54 const truncatedCode = code.length > 50 ? `${code.substring( 0, 50 )}...` : code;
55
56 this.accessibility.updateElementState( {
57 element : button,
58 success : success,
59 successMessage : `Code copied to clipboard: ${truncatedCode}`,
60 errorMessage : 'Failed to copy code to clipboard',
61 successText : 'Copied!',
62 errorText : 'Failed to copy',
63 defaultText : 'Copy',
64 resetDelay : 2000
65 } );
66 }
67
68 // Handles the copy functionality for a code block
69 // Parameters:
70 // block - The code block element
71 // button - The copy button
72 static handleCopyCode( block, button )
73 {
74 try {
75 const code = block.querySelector( "pre" ).innerText;
76 navigator.clipboard.writeText( code ).then( () => { this.updateButtonState( button, true, code ); } ).catch( err => {
77 console.error( "Failed to copy code:", err );
78 this.updateButtonState( button, false, code );
79 } );
80 }
81 catch ( err ) {
82 console.error( "Failed to copy code:", err );
83 this.updateButtonState( button, false, '' );
84 }
85 }
86
87 // Sets up event listeners for a copy button
88 // Parameters:
89 // button - The copy button
90 // block - The code block element
91 static setupEventListeners( button, block )
92 {
93 // Click event handler
94 button.addEventListener( "click", () => this.handleCopyCode( block, button ) );
95
96 // Keyboard event handler
97 this.accessibility.setupKeyboardEvents( button, () => this.handleCopyCode( block, button ) );
98 }
99
100 // Initializes a code block with a copy button
101 // Parameters:
102 // block - The code block element
103 static initializeCodeBlock( block )
104 {
105 const button = this.createCopyButton();
106 block.appendChild( button );
107 this.setupEventListeners( button, block );
108 }
109 }
110
111 // Initialize when the DOM is fully loaded
112 document.addEventListener( "DOMContentLoaded", () => { CodeCopyManager.initialize(); } );