0.2.0-beta.2 • Published 2 years ago

modern-api.clipboard v0.2.0-beta.2

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

modern-api.clipboard

Clipboard APIs based on Web Clipboard API.

Install

$ npm i modern-api.clipboard

Usage

copy and paste text

import {read, write} from 'modern-api.clipboard'

(async() => {
  await write('hello world') // set 'hello world' to clipboard
  const data = await read() // get 'hello world' from clipboard
})()

copy and paste image

import {read, write} from 'modern-api.clipboard'

(async() => {
  const img = await fetch('/hello.png')
  await write(await img.blob(), 'png') // set 'world.png' to clipboard

  const data = await read('png') // get 'hello.png' Blob
})()

API

/**
 * whether browser support clipboard APIs
 */
declare const clipboardSupported: boolean;
/**
 * whether browser support clipboard `write` API
 */
declare const writeSupported: boolean;
/**
 * whether browser support clipboard `writeText` API
 */
declare const writeTextSupported: boolean;
/**
* whether browser support clipboard `read` API
*/
declare const readSupported: boolean;
/**
 * whether browser support clipboard `readText` API
 */
declare const readTextSupported: boolean;
/**
 * write data to clipboard
 *
 * @param {string|Blob|PromiseLike<string|Blob>} data - data to copy or cut
 * @param {'text' | 'png' | 'jpeg' | 'svg'} type - data type, default 'text'
 *
 * @returns {Promise<void>}
 */
declare const write: (data: string | Blob | PromiseLike<string | Blob>, type?: 'text' | 'png' | 'jpeg' | 'svg') => Promise<void>;
/**
 * read data from clipboard
 *
 * @param {'text' | 'png' | 'jpeg' | 'svg'} type - data type, default 'text'
 *
 * @returns {Promise<string | Blob>}
 */
declare const read: (type?: 'text' | 'png' | 'jpeg' | 'svg') => Promise<string | Blob[]>;