Skip to content

Commit ecd4ff9

Browse files
committed
added copy2clipboard button by default to every text input
1 parent c559c6c commit ecd4ff9

3 files changed

Lines changed: 81 additions & 56 deletions

File tree

arc/js/app.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,59 @@ function editEntryFor(id) {
2525
$('#editable_' + id ).click();
2626
}
2727

28+
// taken from https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript/30810322
29+
function copyTextToClipboard(text) {
30+
var textArea = document.createElement("textarea");
31+
var range = document.createRange();
32+
33+
34+
textArea.style.position = 'fixed';
35+
textArea.style.top = 0;
36+
textArea.style.left = 0;
37+
38+
// Ensure it has a small width and height. Setting to 1px / 1em
39+
// doesn't work as this gives a negative w/h on some browsers.
40+
textArea.style.width = '2em';
41+
textArea.style.height = '2em';
42+
43+
// We don't need padding, reducing the size if it does flash render.
44+
textArea.style.padding = 0;
45+
46+
// Clean up any borders.
47+
textArea.style.border = 'none';
48+
textArea.style.outline = 'none';
49+
textArea.style.boxShadow = 'none';
50+
51+
// Avoid flash of white box if rendered for any reason.
52+
textArea.style.background = 'transparent';
53+
54+
textArea.value = text;
55+
56+
textArea.readOnly = false;
57+
textArea.contentEditable = true;
58+
59+
document.body.appendChild(textArea);
60+
61+
textArea.select();
62+
63+
range.selectNodeContents(textArea);
64+
var s = window.getSelection();
65+
s.removeAllRanges();
66+
s.addRange(range);
67+
68+
textArea.setSelectionRange(0, 999999);
69+
70+
try {
71+
var successful = document.execCommand('copy');
72+
var msg = successful ? 'successful' : 'unsuccessful';
73+
console.log('Copying text command was ' + msg);
74+
} catch (err) {
75+
console.log('Oops, unable to copy');
76+
}
77+
78+
document.body.removeChild(textArea);
79+
}
80+
2881
function downloadFor(id) {
2982
var name = $('#editable_' + id).text();
3083
var file = FilesGet(id);

arc/js/entries/base.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,27 @@ Entry.prototype.formGroup = function(input) {
6060
'</div>';
6161
}
6262

63+
Entry.prototype.btn = function(name, icon) {
64+
return '<button id="btn_entry_' + name + '_' + this.id + '" type="button" class="btn btn-default">' +
65+
'<span class="fa fa-' + icon + '"></span>' +
66+
'</button>';
67+
}
68+
6369
Entry.prototype.input = function(type, with_value) {
64-
return '<input ' +
70+
return '<div class="input-group mif">' +
71+
'<input ' +
6572
'class="form-control" ' +
6673
'data-entry-type="' + this.type + '" ' +
6774
'type="' + type + '" ' +
6875
'name="' + this.id + '" ' +
6976
'id="' + this.id + '" ' +
7077
'value="' + ( with_value ? this.safeValue() : '' ) + '"' +
7178
( type == 'file' ? 'multiple' : '' ) +
72-
'/>';
79+
'/>' +
80+
'<span class="input-group-btn">' +
81+
this.btn( 'copy', 'clipboard' ) +
82+
'</span>' +
83+
'</div>';
7384
}
7485

7586
Entry.prototype.textarea = function(with_md, with_value) {
@@ -110,4 +121,18 @@ Entry.prototype.Render = function(with_value){
110121
return "Unhandled entry type " + this.type;
111122
}
112123

113-
Entry.prototype.OnRendered = function() { }
124+
Entry.prototype.OnRendered = function() {
125+
var elem_id = this.id;
126+
var $btn_entry_copy = $('#btn_entry_copy_' + elem_id);
127+
$btn_entry_copy.click(function() {
128+
var prev = $btn_entry_copy.html();
129+
var value = $('#' + elem_id).val();
130+
131+
copyTextToClipboard(value);
132+
133+
$btn_entry_copy.html("<small>Copied!</small>");
134+
setTimeout(function(){
135+
$btn_entry_copy.html(prev);
136+
}, 1000);
137+
});
138+
}

arc/js/entries/password.js

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -28,59 +28,6 @@ function generatePassword( length, charset ) {
2828
return pass;
2929
}
3030

31-
// taken from https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript/30810322
32-
function copyTextToClipboard(text) {
33-
var textArea = document.createElement("textarea");
34-
var range = document.createRange();
35-
36-
37-
textArea.style.position = 'fixed';
38-
textArea.style.top = 0;
39-
textArea.style.left = 0;
40-
41-
// Ensure it has a small width and height. Setting to 1px / 1em
42-
// doesn't work as this gives a negative w/h on some browsers.
43-
textArea.style.width = '2em';
44-
textArea.style.height = '2em';
45-
46-
// We don't need padding, reducing the size if it does flash render.
47-
textArea.style.padding = 0;
48-
49-
// Clean up any borders.
50-
textArea.style.border = 'none';
51-
textArea.style.outline = 'none';
52-
textArea.style.boxShadow = 'none';
53-
54-
// Avoid flash of white box if rendered for any reason.
55-
textArea.style.background = 'transparent';
56-
57-
textArea.value = text;
58-
59-
textArea.readOnly = false;
60-
textArea.contentEditable = true;
61-
62-
document.body.appendChild(textArea);
63-
64-
textArea.select();
65-
66-
range.selectNodeContents(textArea);
67-
var s = window.getSelection();
68-
s.removeAllRanges();
69-
s.addRange(range);
70-
71-
textArea.setSelectionRange(0, 999999);
72-
73-
try {
74-
var successful = document.execCommand('copy');
75-
var msg = successful ? 'successful' : 'unsuccessful';
76-
console.log('Copying text command was ' + msg);
77-
} catch (err) {
78-
console.log('Oops, unable to copy');
79-
}
80-
81-
document.body.removeChild(textArea);
82-
}
83-
8431
function PasswordEntry(name, value) {
8532
Entry.call( this, ENTRY_TYPE_PASSWORD, name, value );
8633
}

0 commit comments

Comments
 (0)