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

feat: add options to enable/disable prompt fallback. #136

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ copy('Text', {
|Value |Default |Notes|
|------|--------|-----|
|options.debug |false| `Boolean`. Optional. Enable output to console. |
|options.prompt |true | `Boolean`. Optional. Enable prompt fallback. |
|options.message|Copy to clipboard: `#{key}`, Enter| `String`. Optional. Prompt message. `*` |
|options.format|"text/html"| `String`. Optional. Set the MIME type of what you want to copy as. Use `text/html` to copy as HTML, `text/plain` to avoid inherited styles showing when pasted into rich text editor. |
|options.onCopy|null| `function onCopy(clipboardData: object): void`. Optional. Receives the clipboardData element for adding custom behavior such as additional formats |
Expand Down
44 changes: 43 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,54 @@
// Definitions by: Denis Carriere <https://github.com/DenisCarriere>, MartynasZilinskas <https://github.com/MartynasZilinskas>

interface Options {
/**
* Enable output to console.
*
* @default false
*/
debug?: boolean;
/**
* Enable prompt fallback.
*
* @default true
*/
prompt?: boolean;
/**
* Prompt message.
*
* @default 'Copy to clipboard: `#{key}`, Enter'
*/
message?: string;
/**
* Set the MIME type of what you want to copy as.
*
* Use `text/html` to copy as HTML, `text/plain` to avoid inherited styles showing when pasted into rich text editor.
*
* @default 'text/plain'
*/
format?: string; // MIME type
onCopy?: (clipboardData: object) => void;
/**
*
* Receives the clipboardData element for adding custom behavior such as additional formats.
*/
onCopy?: (clipboardData: DataTransfer | null) => void;
}

/**
*
* @example
* ```
* import copy from 'copy-to-clipboard';
*
* copy('Text');
*
* // Copy with options
* copy('Text', {
* debug: true,
* message: 'Press #{key} to copy',
* });
* ```
*/
declare function copy(text: string, options?: Options): boolean;
declare namespace copy { }
export = copy;
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,13 @@ function copy(text, options) {
options.onCopy && options.onCopy(window.clipboardData);
success = true;
} catch (err) {
var enablePromptFallback = typeof options.prompt === "undefined" ? true : options.prompt;
debug && console.error("unable to copy using clipboardData: ", err);
debug && console.error("falling back to prompt");
message = format("message" in options ? options.message : defaultMessage);
window.prompt(message, text);
if (enablePromptFallback) {
debug && console.error("falling back to prompt");
message = format("message" in options ? options.message : defaultMessage);
window.prompt(message, text);
}
}
} finally {
if (selection) {
Expand Down