Skip to content

Commit

Permalink
Merge pull request #10 from DiriectorDoc/v0.3
Browse files Browse the repository at this point in the history
v0.3
  • Loading branch information
DiriectorDoc committed Apr 13, 2021
2 parents c0d2a5c + 2c3bdd4 commit 0100ba6
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 66 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode/*
minifier.bat
minifier.min.js
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ To install, simply add the following script tag below the tag where `jQuery.js`

```html
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script src="https://diriectordoc.github.io/jQlipboard-Docs/src/v0.2/jQlipboard.js"></script>
<script src="https://diriectordoc.github.io/jQlipboard-Docs/src/v0.3/jQlipboard.js"></script>
```

Or, for a minified script:

```html
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://diriectordoc.github.io/jQlipboard-Docs/src/v0.2/jQlipboard.min.js"></script>
<script src="https://diriectordoc.github.io/jQlipboard-Docs/src/v0.3/jQlipboard.min.js"></script>
```

## Copying
Expand Down
92 changes: 57 additions & 35 deletions jQlipboard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* jQlipboard v0.2
* jQlipboard v0.3
* A jQuery plugin that makes handling clipboard processes easier
*
*
Expand All @@ -24,7 +24,7 @@
function select(nodeB, offB, nodeE, offE){
let range = new Range();
$.deselect()
if(offB){
if(nodeE){
range.setStart(nodeB, offB)
range.setEnd(nodeE, offE)
} else {
Expand All @@ -48,18 +48,18 @@
this.select()
$.copy()
select(nodeB, offB, nodeE, offE)
if(this.css("user-select") === "none"){
if(this.css("user-select") == "none"){
$.copy(this.val() || this.html())
}
}
return this
} else {
return this
.css({
display: "block",
position: "absolute", // Ensures that appending the object does not mess up the existing document

opacity: 0,
color: "rgba(0,0,0,0)", // Makes the object invisible. `display:none` will not work since it supresses selecting
left: "-9999in", // Makes the object display out of sight. `display:none` will not work since it supresses selecting

"-webkit-user-select": "text",
"-khtml-user-select": "text",
Expand Down Expand Up @@ -128,47 +128,69 @@
};

/*
* @param {string} text
* @returns {boolean}
* @param {*} data - Anything that can be turned into a string
* @returns {(boolean|undefined)}
*/
$.copy = text => {
if(text !== undefined){
$("<a>")
.html(text)
.copy()
} else {
try {
if(!document.execCommand("copy")){
throw false
$.copy = data => {
switch(typeof data){
case "object":
if(data == null){
$("<img>").copy()
return
}
return true
} catch(err){
if(err){
console.error(err)
if(data instanceof Date)
data = data.toISOString();
else if(data instanceof HTMLElement)
data = data.outerHTML;
else if(data.toString() != "[object Object]")
data = data.toString();
else
data = JSON.stringify(data);
case "number":
case "string":
$('<script type="text/plain">')
.html(data)
.copy()
break;
case "undefined":
try {
if(!document.execCommand("copy")){
throw false
}
return true
} catch(err){
if(err){
console.error(err)
}
if(navigator.clipboard){
console.info("Trying navigator.clipboard.writeText() instead")
let success = true;
navigator.clipboard.writeText(selec)
.then(nothing)
.catch(function(){
console.error("Cannot copy text to clipboard")
success = false
})
return success
}
console.error("Cannot copy text to clipboard")
return false
}
if(navigator.clipboard){
console.info("Trying navigator.clipboard.writeText() instead")
let success = true;
navigator.clipboard.writeText(selec.toString())
.then(nothing)
.catch(function(){
console.error("Cannot copy text to clipboard")
success = false
})
return success
default:
try {
return $.copy(data.toString())
} catch(err){
console.error("Could not convert item to a copiable string.\n",data,err)
}
console.error("Cannot copy text to clipboard")
return false
}
}
};

$.jQlipboard = {version: "v0.2"};
$.jQlipboard = {version: "v0.3"};
}((function(){
try{
return jQuery
} catch(e){
console.warn("jQuery not detected. You must use a jQuery version of 1.0 or newer to run this plugin.")
return false
}
})()));
})()));
4 changes: 2 additions & 2 deletions jQlipboard.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 42 additions & 27 deletions jQlipboard.tomin.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
select=(nodeB, offB, nodeE, offE)=>{
let range = new Range();
$.deselect()
if(offB){
if(nodeE){
range.setStart(nodeB, offB)
range.setEnd(nodeE, offE)
} else {
Expand All @@ -35,17 +35,17 @@
this.select()
$.copy()
select(nodeB, offB, nodeE, offE)
if (this.css("user-select") === "none") {
if (this.css("user-select") == "none") {
$.copy(this.val() || this.html())
}
}
return this
} else {
return this
.css({
display: "block",
position: "absolute", // Ensures that appending the object does not mess up the existing document
opacity: 0, // ↴
color: "rgba(0,0,0,0)", // Makes the object invisible. `display:none` will not work since it disables the avility to select it
left: "-9999in", // Makes the object display out of sight. `display:none` will not work since it supresses selecting
"-webkit-user-select": "text",
"-khtml-user-select": "text",
"-moz-user-select": "text", // Ensures that the appended object can be selected, just in case it was disabled in the stylesheet
Expand Down Expand Up @@ -87,32 +87,47 @@
}
};

$.copy = text=>{
if(text !== undefined){
$("<a>")
.html(text)
.copy()
} else {
try {
return exec("copy")
} catch(err){
if(err){
error(err)
$.copy = data=>{
switch(typeof data){
case "object":
if(data == null){
$("<img>").copy()
break
}
let error = a=>!!error("Cannot copy text to clipboard",a);
if(navigator.clipboard){
let success = !info("Trying navigator.clipboard.writeText() instead");
navigator.clipboard.writeText(w+"")
.then(a=>0)
.catch(y=>{
success = error(y)
})
return success;
data = data instanceof Date ? data.toISOString() : (data instanceof HTMLElement ? data.outerHTML : (data.toString() != "[object Object]" ? data.toString() : JSON.stringify(data)))
case "number":
case "string":
$('<script type="text/plain">')
.html(data)
.copy()
break;
case "undefined":
try {
return exec("copy")
} catch(err){
if(err){
error(err)
}
let error = a=>!!error("Cannot copy text to clipboard");
if(navigator.clipboard){
let success = !info("Trying navigator.clipboard.writeText() instead");
navigator.clipboard.writeText(w)
.then(a=>0)
.catch(y=>{
success = error()
})
return success;
}
return error()
}
default:
try {
return $.copy(data.toString())
} catch(err){
error("Could not convert item to a copiable string.\n",data,err)
}
return error()
}
}
};

$.jQlipboard = {version: "v0.2"};
$.jQlipboard = {version: "v0.3"};
})(window.jQuery || console.warn("jQuery not detected. You must use a jQuery version of 1.0 or newer to run this plugin."));

0 comments on commit 0100ba6

Please sign in to comment.