Skip to content

Commit

Permalink
add copy to clipboard for ssh-key pair and webhook url (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisFlorin authored Nov 22, 2024
1 parent aa2a2a7 commit 53ea80a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
15 changes: 15 additions & 0 deletions gitium/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,18 @@ function gitium_admin_init() {
}
}
add_action( 'admin_init', 'gitium_admin_init' );

add_action('admin_enqueue_scripts', 'enqueue_script_for_gitium_page');
function enqueue_script_for_gitium_page($hook) {
// Check if the current page is your plugin's settings page
if ((isset($_GET['page']) && $_GET['page'] === 'gitium/gitium.php') || (isset($_GET['page']) && $_GET['page'] === 'gitium/gitium-settings.php')) {
// Enqueue your JavaScript file
wp_enqueue_script(
'my-plugin-script', // Handle for the script
plugin_dir_url(__FILE__) . 'js/copy-to-clipboard.js', // URL to the script
array('jquery'), // Dependencies
'1.1', // Version number
true // Load in footer
);
}
}
9 changes: 7 additions & 2 deletions gitium/inc/class-gitium-submenu-configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,13 @@ private function setup_step_1_key_pair() {
<th scope="row"><label for="key_pair"><?php _e( 'Key pair', 'gitium' ); ?></label></th>
<td>
<p>
<input type="text" class="regular-text" name="key_pair" id="key_pair" value="<?php echo esc_attr( $git_public_key ); ?>" readonly="readonly">
<input type="submit" name="GitiumSubmitRegenerateKeypair" class="button" value="<?php _e( 'Regenerate Key', 'gitium' ); ?>" />
<input type="text" class="regular-text" name="key_pair" id="key_pair" value="<?php echo esc_attr( $git_public_key ); ?>" readonly="readonly">
<input type="submit" name="GitiumSubmitRegenerateKeypair" class="button" value="<?php _e( 'Regenerate Key', 'gitium' ); ?>" />
</p>
<p>
<div>
<button id="copyButton" class="button" data-copy-text="<?php echo esc_attr($git_public_key); ?>">Copy Key Pair</button>
</div>
</p>
<p class="description"><?php _e( 'If your code use ssh keybased authentication for git you need to allow write access to your repository using this key.', 'gitium' ); ?><br />
<?php _e( 'Checkout instructions for <a href="https://help.github.com/articles/generating-ssh-keys#step-3-add-your-ssh-key-to-github" target="_blank">github</a> or <a href="https://confluence.atlassian.com/display/BITBUCKET/Add+an+SSH+key+to+an+account#AddanSSHkeytoanaccount-HowtoaddakeyusingSSHforOSXorLinux" target="_blank">bitbucket</a>.', 'gitium' ); ?>
Expand Down
15 changes: 10 additions & 5 deletions gitium/inc/class-gitium-submenu-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ private function show_webhook_table_webhook_url() {
<th><label for="webhook-url"><?php _e( 'Webhook URL', 'gitium' ); ?>:</label></th>
<td>
<p><code id="webhook-url"><?php echo esc_url( gitium_get_webhook() ); ?></code>
<?php if ( ! defined( 'GIT_WEBHOOK_URL' ) || GIT_WEBHOOK_URL == '' ) : ?>
<input type="submit" name="GitiumSubmitRegenerateWebhook" class="button" value="<?php _e( 'Regenerate Webhook', 'gitium' ); ?>" />
<a class="button" href="<?php echo esc_url( gitium_get_webhook() ); ?>" target="_blank">Merge changes</a></p>
<?php endif; ?>
<p class="description"><?php _e( 'Pinging this URL triggers an update from remote repository.', 'gitium' ); ?></p>
<?php if ( ! defined( 'GIT_WEBHOOK_URL' ) || GIT_WEBHOOK_URL == '' ) : ?>
<input type="submit" name="GitiumSubmitRegenerateWebhook" class="button" value="<?php _e( 'Regenerate Webhook', 'gitium' ); ?>" />
<a class="button" href="<?php echo esc_url( gitium_get_webhook() ); ?>" target="_blank">Merge changes</a></p>
<?php endif; ?>
<p>
<div>
<button id="copyButton" class="button" data-copy-text="<?php echo esc_url( gitium_get_webhook() ) ?>">Copy Webhook URL</button>
</div>
</p>
<p class="description"><?php _e( 'Pinging this URL triggers an update from remote repository.', 'gitium' ); ?></p>
</td>
</tr>
<?php
Expand Down
38 changes: 38 additions & 0 deletions gitium/js/copy-to-clipboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
document.addEventListener('DOMContentLoaded', function() {
const copyButton = document.getElementById('copyButton');

copyButton.addEventListener('click', function() {
// Get the text to copy from the button's data attribute
const textToCopy = copyButton.getAttribute('data-copy-text');

// Check if navigator.clipboard is supported
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(textToCopy)
.then(() => {
alert('Copied to clipboard using navigator: ' + textToCopy);
})
.catch(err => {
console.error('Could not copy text with navigator: ', err);
});
} else {
// Deprecated fallback for older browsers
console.warn('Using deprecated document.execCommand("copy") as a fallback. Update your browser for better clipboard support.');

// Fallback using document.execCommand
const tempTextarea = document.createElement('textarea');
tempTextarea.value = textToCopy;
document.body.appendChild(tempTextarea);
tempTextarea.select();

try {
document.execCommand('copy');
alert('Copied to clipboard (using fallback): ' + textToCopy);
} catch (err) {
console.error('Fallback copy failed: ', err);
}

// Remove the temporary textarea
document.body.removeChild(tempTextarea);
}
});
});

0 comments on commit 53ea80a

Please sign in to comment.