-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
87 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters