Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No paste #6

Merged
merged 7 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
jQlipboard.tomin.js
minifier.bat
minifier.min.js
minifier.min.js
61 changes: 3 additions & 58 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,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 type="text/javascript" src="https://raw.githubusercontent.com/DiriectorDoc/jQlipboard/master/JQlipboard.js"></script>
<script type="text/javascript" src="https://raw.githubusercontent.com/DiriectorDoc/jQlipboard/master/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 type="text/javascript" src="https://raw.githubusercontent.com/DiriectorDoc/jQlipboard/master/JQlipboard.min.js"></script>
<script type="text/javascript" src="https://raw.githubusercontent.com/DiriectorDoc/jQlipboard/master/jQlipboard.min.js"></script>
```

## Copying
Expand Down Expand Up @@ -44,39 +44,6 @@ Using `$(...).copy()` on any other type of element will instead copy the inside
</body>
```

## Pasting
Using `$.paste()` is identical to pasting using regular means, such as <kbd>Ctrl</kbd> + <kbd>V</kbd>.

> **Note:** By default, pasting is not enabled. Having it off by default saves RAM and avoids unnecessary permission prompts. To enable it, use `{pasting: true}` in your [initialization](#initilization) config.

```javascript
/* May throw an error if used while the document is not focused */
$.paste()
```

To paste text into a textarea of some sort, use `$(...).paste()`:

```javascript
let element = $('<input type="text" />').appendTo("body")

/* This will put whatever text is in your clipboard into the textarea */
element.paste()
```

Using `$(...).paste()` on any other type of element will instead paste the text as innerHTML.

```html
<body>
<div id="copy-text">This text will be replaced</div>

<script>
$.copy("This text will replae the existing text")

$("#copy-text").paste()
</script>
</body>
```

## Selecting
Using `$(...).select()` will highlight the target element. This will work on almost any element. This function extends the use of the existing jQuery function [`.select()`](https://api.jquery.com/select/), which will trigger instead if used on an `<input>` or `<textarea>` element.

Expand All @@ -90,26 +57,4 @@ Using `$(...).select()` will highlight the target element. This will work on alm
</body>
```

Calling the function `$.deselect()` will nullify any selection there may be on the page.

## Initilization
Initialization is not mandatory. If jQlipboard is not initialized, it will simply use the default config. Only initialize if you plan on using the paste feature somewhere on your page.

```javascript
$.jQlipboard({
/*
* Enables the paste command. Off by default to save ram
* and avoid unnecessary permission prompts.
* All other config options are reliant on this one. If
* this is set to false or not at all, any other config
* option will be ignored.
*/
pasting: /* true, false (default) */,

// Determines when the page will request permession to use the clipboard; on load or when needed
permissionPrompt: /* "immediate", "when needed" (default) */,

// Detects when you modify the clipboard on your own and adjusts the functions accordingly
copyListener: /* true, false (default) */
})
```
Calling the function `$.deselect()` will nullify any selection there may be on the page.
131 changes: 7 additions & 124 deletions jQlipboard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* jQlipboard v0.1.7
* jQlipboard v0.1.8
* A jQuery plugin that makes handling clipboard processes easier
*
*
Expand All @@ -13,32 +13,11 @@
(function($) {

if(!$) return;

console.log("%cjQlipboard no longer supports pasting by default.", "font-size: 2em");
console.log("To continusing the paste functions, see https://github.com/DiriectorDoc/jQlipboard/tree/withPaste")

let pasteOn,
selec = window.getSelection();

function setQlipboard($this){
if(pasteOn){
$.jQlipboard.qlipboard.jqobj = $this instanceof $ ? $this.clone() : null;
$.jQlipboard.qlipboard.text = "";

navigator.clipboard.readText()
.then(text => {
$.jQlipboard.qlipboard.text = text
})
.catch(warning)
if($.jQlipboard.qlipboard.jqobj){
$.jQlipboard.qlipboard.text = $this[0] && "IMG" == $this[0].tagName ? "image" : $this.val() || $this.html() || ""
}
}
}

function warning(err){
console.error(err)
console.warn("Browser does not have permission to access clipboard. Some features may not work until permission is granted.")
console.info('To grant permission, go into your browser setting and allow "Clipboard"')
warning = nothing
}
let selec = window.getSelection();

function nothing(){
return
Expand Down Expand Up @@ -71,7 +50,6 @@
this.select()
$.copy()
select(nodeB, offB, nodeE, offE)
setQlipboard(this)
if(this.css("user-select") === "none"){
$.copy(this.val() || this.html())
}
Expand All @@ -97,36 +75,6 @@
}
};

/*
* @returns {jQuery} this
*/
$.fn.paste = function(){
if(pasteOn){
let tag = this[0].tagName;
if("INPUT" == tag || "TEXTAREA" == tag){
if(this.is(":focus") || this[0] === document.activeElement){
return $.paste() ? this:this.val($.jQlipboard.qlipboard.text)
}
let nodeB = selec.baseNode,
offB = selec.baseOffset,
nodeE = selec.extentNode,
offE = selec.extentOffset;
this.focus()
if(!$.paste()){
let text = this.val(),
e = this[0];
this.val(text.slice(0, e.selectionStart) + $.jQlipboard.qlipboard.text + text.slice(e.selectionEnd))
}
select(nodeB, offB, nodeE, offE)
return this
} else {
return this.append($.jQlipboard.qlipboard.jqobj.clone())
}
}
console.warn("Pasting is truned off by default. You need to enable it upon intitalization.")
return this
};

/*
* @returns {jQuery} this
*/
Expand Down Expand Up @@ -205,7 +153,7 @@
console.info("Trying navigator.clipboard.writeText() instead")
let success = true;
navigator.clipboard.writeText(selec.toString())
.then(setQlipboard)
.then(nothing)
.catch(function(){
console.error("Cannot copy text to clipboard")
success = false
Expand All @@ -217,73 +165,8 @@
}
}
};

/*
* @returns {boolean}
*/
$.paste = function(){
if(pasteOn){
try {
if(!document.execCommand("paste")){
throw false
}
return true
} catch(err){
if(err){
console.warn(err)
}
let success = true;
navigator.clipboard.readText()
.then(clipText => {
let $focus = $(document.activeElement),
e = $focus[0],
text = $focus.val();
text = text.slice(0, e.selectionStart) + clipText + text.slice(e.selectionEnd);
$focus.val(text)
})
.catch(err => {
console.error("Could not execute paste", err)
success = false
})
return success
}
}
console.warn("Pasting is truned off by default. You need to enable it upon intitalization.")
return false
};

$.jQlipboard = function(config){
if(!config){
config = {}
}
config = {
permissionPrompt: config.permissionPrompt || "when needed",
copyListener: config.copyListener || config.copyListener === undefined,
...config
}
pasteOn = config.pasting;
if(pasteOn){
$.jQlipboard.qlipboard = {}
} else {
delete $.jQlipboard.qlipboard
}
if(config.permissionPrompt == "immediate" && config.pasting){
navigator.clipboard.readText()
.then(nothing)
.catch(warning)
}
if(config.copyListener && config.pasting){
if($.fn.on){
$(document).on("copy", setQlipboard)
} else {
$(document).bind("copy", setQlipboard)
}
}
};

$.jQlipboard()

$.jQlipboard.version = "0.1.7";
$.jQlipboard.version = "0.1.8";
}((function(){
try{
return jQuery
Expand Down
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.