-
Notifications
You must be signed in to change notification settings - Fork 449
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
Kerwin
committed
Apr 26, 2023
1 parent
bc27535
commit 9e2fcf4
Showing
12 changed files
with
130 additions
and
183 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export function copyToClip(text: string) { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const input: HTMLTextAreaElement = document.createElement('textarea') | ||
input.setAttribute('readonly', 'readonly') | ||
input.value = text | ||
document.body.appendChild(input) | ||
input.select() | ||
if (document.execCommand('copy')) | ||
document.execCommand('copy') | ||
document.body.removeChild(input) | ||
resolve(text) | ||
} | ||
catch (error) { | ||
reject(error) | ||
} | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 +1,57 @@ | ||
export * from './local' | ||
interface StorageData<T = any> { | ||
data: T | ||
expire: number | null | ||
} | ||
|
||
export function createLocalStorage(options?: { expire?: number | null }) { | ||
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7 | ||
|
||
const { expire } = Object.assign({ expire: DEFAULT_CACHE_TIME }, options) | ||
|
||
function set<T = any>(key: string, data: T) { | ||
const storageData: StorageData<T> = { | ||
data, | ||
expire: expire !== null ? new Date().getTime() + expire * 1000 : null, | ||
} | ||
|
||
const json = JSON.stringify(storageData) | ||
window.localStorage.setItem(key, json) | ||
} | ||
|
||
function get(key: string) { | ||
const json = window.localStorage.getItem(key) | ||
if (json) { | ||
let storageData: StorageData | null = null | ||
|
||
try { | ||
storageData = JSON.parse(json) | ||
} | ||
catch { | ||
// Prevent failure | ||
} | ||
|
||
if (storageData) { | ||
const { data, expire } = storageData | ||
if (expire === null || expire >= Date.now()) | ||
return data | ||
} | ||
|
||
remove(key) | ||
return null | ||
} | ||
} | ||
|
||
function remove(key: string) { | ||
window.localStorage.removeItem(key) | ||
} | ||
|
||
function clear() { | ||
window.localStorage.clear() | ||
} | ||
|
||
return { set, get, remove, clear } | ||
} | ||
|
||
export const ls = createLocalStorage() | ||
|
||
export const ss = createLocalStorage({ expire: null }) |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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