-
Notifications
You must be signed in to change notification settings - Fork 130
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
Accept a style
parameter to specify the copied style.
#80
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,15 @@ | |
// Project: https://github.com/sudodoki/copy-to-clipboard | ||
// Definitions by: Denis Carriere <https://github.com/DenisCarriere>, MartynasZilinskas <https://github.com/MartynasZilinskas> | ||
|
||
interface StyleOption { | ||
background?: string; | ||
text?: string; | ||
} | ||
|
||
interface Options { | ||
debug?: boolean; | ||
message?: string; | ||
format?: string; // MIME type | ||
style?: boolean | StyleOption; // MIME type | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know about typescript, I didn't check this is working as expected, so please double check! |
||
|
||
declare function copy(text: string, options?: Options): boolean; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,10 +16,18 @@ function copy(text, options) { | |
range, | ||
selection, | ||
mark, | ||
style, | ||
success = false; | ||
if (!options) { | ||
options = {}; | ||
} | ||
|
||
if (options.style) { | ||
// If style is the boolean `true`, use the default values. | ||
// Otherwise, overwrite the default values with whatever is passed. | ||
style = Object.assign({ background: 'white', text: 'black' }, options.style); | ||
} | ||
|
||
debug = options.debug || false; | ||
try { | ||
reselectPrevious = deselectCurrent(); | ||
|
@@ -31,6 +39,10 @@ function copy(text, options) { | |
mark.textContent = text; | ||
// reset user styles for span element | ||
mark.style.all = "unset"; | ||
if (style) { | ||
mark.style.backgroundColor = style.background; | ||
mark.style.color = style.text; | ||
} | ||
// prevents scrolling to the end of the page | ||
mark.style.position = "fixed"; | ||
mark.style.top = 0; | ||
|
@@ -43,11 +55,14 @@ function copy(text, options) { | |
mark.style.msUserSelect = "text"; | ||
mark.style.userSelect = "text"; | ||
mark.addEventListener("copy", function(e) { | ||
// Prevents the "copy" event from reaching the outside world. | ||
e.stopPropagation(); | ||
if (options.format) { | ||
if (!style) { | ||
// Force a text copy instead of an html copy, to make it easier to | ||
// work with word processors. | ||
e.preventDefault(); | ||
e.clipboardData.clearData(); | ||
e.clipboardData.setData(options.format, text); | ||
e.clipboardData.setData('text/plain', text); | ||
} | ||
}); | ||
|
||
|
@@ -65,7 +80,7 @@ function copy(text, options) { | |
debug && console.error("unable to copy using execCommand: ", err); | ||
debug && console.warn("trying IE specific stuff"); | ||
try { | ||
window.clipboardData.setData(options.format || "text", text); | ||
window.clipboardData.setData("text", text); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked the documentation on microsoft website and there's no possibility to pass rich text. So I reverted to the previous code here. |
||
success = true; | ||
} catch (err) { | ||
debug && console.error("unable to copy using clipboardData: ", err); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought that
data-test
wasn't used at all so I repurposed this to make it easier to target buttons to add the click event listeners.