-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy.mjs
More file actions
92 lines (92 loc) · 3.57 KB
/
copy.mjs
File metadata and controls
92 lines (92 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
const copyjs = function(str, options = {
reSelect: true,
html: false,
copyFromSelector: false
}) {
class Reselect {
init() {
this.selectionOptions = {
start: {
index: window.getSelection().anchorOffset,
node: window.getSelection().anchorNode
},
end: {
index: window.getSelection().focusOffset,
node: window.getSelection().focusNode
}
};
return true;
}
reselect() {
if (!this.selectionOptions.start.node)
return false;
const newSelection = window.getSelection();
newSelection.removeAllRanges();
const range = document.createRange();
range.setStart(this.selectionOptions.start.node, this.selectionOptions.start.index);
range.setEnd(this.selectionOptions.end.node, this.selectionOptions.end.index);
newSelection.addRange(range);
return true;
}
}
const reselect = new Reselect();
reselect.init();
if (options.copyFromSelector) {
if (!options.html) {
const copyjs_textarea = document.createElement("textarea");
document.body.append(copyjs_textarea);
const target = document.querySelector(str);
copyjs_textarea.value = target.tagName === "TEXTAREA" || target.tagName === "INPUT" ?
target.value :
target.innerText;
copyjs_textarea.select();
document.execCommand("copy");
copyjs_textarea.remove();
} else {
const selectorSel = window.getSelection();
selectorSel.removeAllRanges();
const selectorRange = document.createRange();
const tag = document.querySelector(str).tagName;
if (tag === "INPUT" || tag === "TEXTAREA") {
const copyjs_div = document.createElement("div");
document.body.append(copyjs_div);
copyjs_div.innerHTML = document.querySelector(str).value;
const htmlSel = window.getSelection();
htmlSel.removeAllRanges();
const htmlRange = document.createRange();
htmlRange.selectNodeContents(copyjs_div);
htmlSel.addRange(htmlRange);
document.execCommand("copy");
copyjs_div.remove();
} else {
selectorRange.selectNodeContents(document.querySelector(str));
selectorSel.addRange(selectorRange);
document.execCommand("copy");
}
}
} else if (options.html) {
const copyjs_div = document.createElement("div");
document.body.append(copyjs_div);
copyjs_div.innerHTML = str;
const htmlSel = window.getSelection();
htmlSel.removeAllRanges();
const htmlRange = document.createRange();
htmlRange.selectNodeContents(copyjs_div);
htmlSel.addRange(htmlRange);
document.execCommand("copy");
copyjs_div.remove();
} else {
const copyjs_textarea = document.createElement("textarea");
document.body.append(copyjs_textarea);
copyjs_textarea.value = str;
copyjs_textarea.select();
document.execCommand("copy");
copyjs_textarea.remove();
}
if (options.reSelect) {
reselect.reselect();
}
return true;
}
export default copyjs;