Skip to content

Commit

Permalink
fix: copy selection
Browse files Browse the repository at this point in the history
  • Loading branch information
akijoey committed Jan 6, 2024
1 parent 76b05d8 commit 4bd99e1
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 72 deletions.
24 changes: 13 additions & 11 deletions src/encaps/copy.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
interface CopyOptions {
export interface CopyOptions {
target?: HTMLElement
}

const copy = (text: string, { target = document.body }: CopyOptions = {}) => {
export const copy = (
text: string,
{ target = document.body }: CopyOptions = {}
): void => {
const textarea = document.createElement('textarea')
const activeElement = document.activeElement
const activeElement = document.activeElement as HTMLElement | null

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

const currentRange =
selection !== null && selection.rangeCount > 0 && selection.getRangeAt(0)

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

if (currentRange) {
if (selection !== null && currentRange !== false) {
selection.removeAllRanges()
selection.addRange(currentRange)
}
if (activeElement) {

if (activeElement !== null) {
activeElement.focus()
}
}

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

export { copy, install }
26 changes: 26 additions & 0 deletions src/encaps/file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { ajax, type Response } from './request'

export const saveAs = (blob: Blob, filename: string): void => {
const link = document.createElement('a')
const url = URL.createObjectURL(blob)
link.href = url
link.download = filename
link.click()
URL.revokeObjectURL(url)
}

export const download = async (
url: string,
filename: string
): Promise<Response> => {
const response = await ajax({ url, responseType: 'blob' })
saveAs(response.data, filename)
return response
}

export const install = (): void => {
Object.assign(globalThis, {
saveAs,
download
})
}
49 changes: 20 additions & 29 deletions src/encaps/request.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
interface Response {
export interface Response {
data: any
status?: number
statusText?: string
request?: XMLHttpRequest
}

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

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

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

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

const ajax: Ajax = ({ url, method = 'GET', data, headers = {}, responseType }) => {
return new Promise((resolve, reject) => {
export const ajax: Ajax = async ({
url,
method = 'GET',
data,
headers = {},
responseType
}) => {
return await new Promise((resolve, reject) => {
const request = new XMLHttpRequest()
request.open(method, url)
Object.keys(headers).forEach(key => {
Expand All @@ -51,8 +57,8 @@ const ajax: Ajax = ({ url, method = 'GET', data, headers = {}, responseType }) =
request.send(data)
request.onreadystatechange = () => {
if (request.readyState === 4) {
const { status, statusText, response: data } = request;
const response = { data, status, statusText, request };
const { status, statusText, response: data } = request
const response = { data, status, statusText, request }
if (status >= 200 && status <= 400) {
resolve(response)
} else {
Expand All @@ -63,24 +69,9 @@ const ajax: Ajax = ({ url, method = 'GET', data, headers = {}, responseType }) =
})
}

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 => {
export const install = (): void => {
Object.assign(globalThis, {
jsonp,
ajax,
download
ajax
})
}

export { jsonp, ajax, download, install }
10 changes: 4 additions & 6 deletions src/encaps/timer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
type Timer = (
export type Timer = (
callback: Function,
delay: number | undefined
) => (this: any, ...args: any[]) => void

const debounce: Timer = function debounce(callback, delay) {
export const debounce: Timer = function debounce(callback, delay) {
let timer: NodeJS.Timeout | undefined
return function (...args) {
if (timer !== undefined) {
Expand All @@ -15,7 +15,7 @@ const debounce: Timer = function debounce(callback, delay) {
}
}

const throttle: Timer = function throttle(callback, delay) {
export const throttle: Timer = function throttle(callback, delay) {
let vaild = true
return function (...args) {
if (vaild) {
Expand All @@ -28,11 +28,9 @@ const throttle: Timer = function throttle(callback, delay) {
}
}

const install = (): void => {
export const install = (): void => {
Object.assign(globalThis, {
debounce,
throttle
})
}

export { debounce, throttle, install }
12 changes: 5 additions & 7 deletions src/extends/array.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
const swap = function swap(this: any[], x: number, y: number): any[] {
export const swap = function swap(this: any[], x: number, y: number): any[] {
this[x] = this.splice(y, 1, this[x])[0]
return this
}

const shuffle = function shuffle(this: any[]): any {
export const shuffle = function shuffle(this: any[]): any {
for (let i = this.length; i > 0; i--) {
swap.call(this, i - 1, Math.floor(Math.random() * i))
}
return this
}

const unique = function unique(this: any[]): any {
export const unique = function unique(this: any[]): any {
return [...new Set(this)]
}

const partition = function partition(
export const partition = function partition(
this: any[],
callback: (item: any, index: number, array: any[]) => any,
context?: any
Expand All @@ -29,13 +29,11 @@ const partition = function partition(
)
}

const install = (): void => {
export const install = (): void => {
Object.assign(Array.prototype, {
swap,
shuffle,
unique,
partition
})
}

export { swap, shuffle, unique, partition, install }
20 changes: 12 additions & 8 deletions src/extends/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@
]
*/

const before = function before(this: Function, callback: Function): Function {
export const before = function before(
this: Function,
callback: Function
): Function {
const self = this
return function (this: any) {
callback.apply(this, arguments)
return self.apply(this, arguments)
}
}

const after = function after(this: Function, callback: Function): Function {
export const after = function after(
this: Function,
callback: Function
): Function {
const self = this
return function (this: any) {
const result = self.apply(this, arguments)
Expand All @@ -25,26 +31,26 @@ const after = function after(this: Function, callback: Function): Function {
}
}

const curry = function curry(this: Function, ...args: any[]): any {
export const curry = function curry(this: Function, ...args: any[]): any {
if (args.length < this.length) {
return curry.bind(this, ...args)
}
return this(...args)
}

const compose = function compose(this: Function, ...args: any[]): any {
export const compose = function compose(this: Function, ...args: any[]): any {
return [this, ...args].reduce((prev, current) => {
return (...args: any[]) => prev(current(...args))
})
}

const pipe = function pipe(this: Function, ...args: any[]): any {
export const pipe = function pipe(this: Function, ...args: any[]): any {
return [this, ...args].reduceRight((prev, current) => {
return (...args: any[]) => prev(current(...args))
})
}

const install = (): void => {
export const install = (): void => {
Object.assign(Function.prototype, {
before,
after,
Expand All @@ -53,5 +59,3 @@ const install = (): void => {
pipe
})
}

export { before, after, curry, compose, pipe, install }
8 changes: 3 additions & 5 deletions src/extends/image.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const imageObserver = new IntersectionObserver(entries => {
export const imageObserver = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const image = entry.target as HTMLImageElement
Expand All @@ -9,16 +9,14 @@ const imageObserver = new IntersectionObserver(entries => {
})
})

const lazyLoad = function lazyLoad(this: HTMLImageElement): void {
export const lazyLoad = function lazyLoad(this: HTMLImageElement): void {
this.dataset.src = this.src
this.removeAttribute('src')
imageObserver.observe(this)
}

const install = (): void => {
export const install = (): void => {
Object.assign(HTMLImageElement.prototype, {
lazyLoad
})
}

export { lazyLoad, install }
10 changes: 4 additions & 6 deletions src/extends/object.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const inherit = (Child: any, Parent: any): void => {
export const inherit = (Child: any, Parent: any): void => {
const prototype = Object.create(Parent.prototype)
prototype.constructor = Child
Child.prototype = prototype
Object.setPrototypeOf(Child, Parent)
}

const cloneDeep = (obj: any): any => {
export const cloneDeep = (obj: any): any => {
const res = (Array.isArray(obj) ? [] : {}) as any
Object.keys(obj).forEach(key => {
if (obj[key] !== undefined && typeof obj[key] === 'object') {
Expand All @@ -17,7 +17,7 @@ const cloneDeep = (obj: any): any => {
return res
}

const freezeDeep = (obj: any): any => {
export const freezeDeep = (obj: any): any => {
Object.getOwnPropertyNames(obj).forEach(name => {
const prop = obj[name]
if (typeof prop === 'object' && prop !== null) {
Expand All @@ -27,12 +27,10 @@ const freezeDeep = (obj: any): any => {
return Object.freeze(obj)
}

const install = (): void => {
export const install = (): void => {
Object.assign(Object, {
inherit,
cloneDeep,
freezeDeep
})
}

export { inherit, cloneDeep, freezeDeep, install }

0 comments on commit 4bd99e1

Please sign in to comment.