Skip to content

Commit

Permalink
feat: copy methods
Browse files Browse the repository at this point in the history
  • Loading branch information
akijoey committed Dec 29, 2023
1 parent 81abd23 commit 4fa2fa4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 16 deletions.
35 changes: 35 additions & 0 deletions src/encaps/copy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
interface CopyOptions {
target?: HTMLElement
}

const copy = (text: string, { target = document.body }: CopyOptions = {}) => {
const textarea = document.createElement('textarea')
const activeElement = document.activeElement

const selection = document.getSelection()
const currentRange = selection?.rangeCount > 0 && selection.getRangeAt(0)

textarea.value = text
target.append(textarea)
textarea.focus()
textarea.select()
document.execCommand('copy')
textarea.remove()

if (currentRange) {
selection.removeAllRanges()
selection.addRange(currentRange)
}

if (activeElement) {
activeElement.focus()
}
}

const install = (): void => {
Object.assign(globalThis, {
copy
})
}

export { copy, install }
56 changes: 41 additions & 15 deletions src/encaps/request.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
interface Response {
data: any
status?: number
statusText?: string
request?: XMLHttpRequest
}

interface RequestOptions {
url: string
data: any
data?: any
}

type Jsonp = (options: JsonpOptions) => Promise<any>
type Jsonp = (options: JsonpOptions) => Promise<Response>
interface JsonpOptions extends RequestOptions {
callback: string
}

type Ajax = (options: AjaxOptions) => Promise<any>
type Ajax = (options: AjaxOptions) => Promise<Response>
interface AjaxOptions extends RequestOptions {
method: string
headers: { [key: string]: string }
method?: string
headers?: { [key: string]: string }
responseType?: XMLHttpRequestResponseType
}

const jsonp: Jsonp = async ({ url, data, callback }) => {
return await new Promise(resolve => {
const jsonp: Jsonp = ({ url, data, callback }) => {
return new Promise(resolve => {
Object.assign(window, {
[callback]: (data: any) => resolve(data)
[callback]: (data: any) => resolve({ data })
})
data.callback = callback
const params: string[] = []
Expand All @@ -30,31 +38,49 @@ const jsonp: Jsonp = async ({ url, data, callback }) => {
})
}

const ajax: Ajax = async ({ url, method, data, headers }) => {
return await new Promise((resolve, reject) => {
const ajax: Ajax = ({ url, method = 'GET', data, headers = {}, responseType }) => {
return new Promise((resolve, reject) => {
const request = new XMLHttpRequest()
request.open(method, url)
Object.keys(headers).forEach(key => {
request.setRequestHeader(key, headers[key])
})
if (responseType !== undefined) {
request.responseType = responseType
}
request.send(data)
request.onreadystatechange = () => {
if (request.readyState === 4) {
if (request.status >= 200 && request.status <= 400) {
resolve(request)
const { status, statusText, response: data } = request;
const response = { data, status, statusText, request };
if (status >= 200 && status <= 400) {
resolve(response)
} else {
reject(request)
reject(response)
}
}
}
})
}

const download = (url: string, filename: string) => {
return ajax({ url, responseType: 'blob' }).then(response => {
const link = document.createElement('a');
const url = URL.createObjectURL(response.data);
link.href = url;
link.download = filename;
link.click();
URL.revokeObjectURL(url);
return response;
})
}

const install = (): void => {
Object.assign(globalThis, {
jsonp,
ajax
ajax,
download
})
}

export { jsonp, ajax, install }
export { jsonp, ajax, download, install }
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import * as imageExports from './extends/image'
import * as objectExports from './extends/object'
import * as timerExports from './encaps/timer'
import * as requestExports from './encaps/request'
import * as copyExports from './encaps/copy'

const stars = [
arrayExports,
functionExports,
imageExports,
objectExports,
timerExports,
requestExports
requestExports,
copyExports
]

const starcasket = Object.assign(
Expand Down

0 comments on commit 4fa2fa4

Please sign in to comment.