generated from astahmer/tailwind-to-css-in-js
-
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
5 changed files
with
86 additions
and
2 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { compressToEncodedURIComponent, decompressFromEncodedURIComponent } from 'lz-string' | ||
|
||
// adapted from https://github.dev/dsherret/ts-ast-viewer/blob/c71e238123d972bae889b3829e23b44f39d8d5c2/site/src/utils/UrlSaver.ts#L1-L29 | ||
function getDecompressedStringFromUrl(name: string) { | ||
if (typeof window === 'undefined') return | ||
|
||
const search = new URLSearchParams(window.location.search) | ||
const code = (search.get(name) ?? '').trim() | ||
return decompressFromEncodedURIComponent(code) ?? '' // will be null on error | ||
} | ||
|
||
function updateUrlWithCompressedString(name: string, value: string) { | ||
if (value.length === 0) { | ||
updateUrlWithParam(name, '') | ||
} else { | ||
const compressed = compressToEncodedURIComponent(value) | ||
const url = new URL(window.location.href) | ||
url.searchParams.set(name, compressed) | ||
|
||
// completely arbitrary limit of characters, but it appears to not work anymore around that | ||
if (url.toString().length >= 14_500) { | ||
throw new Error('The compressed string is too large to be stored in the URL.') | ||
} else { | ||
updateUrlWithParam(name, compressed) | ||
} | ||
} | ||
} | ||
|
||
function updateUrlWithParam(name: string, value: string | number) { | ||
if (typeof window === 'undefined') return | ||
|
||
const url = new URL(window.location.href) | ||
url.searchParams.set(name, String(value)) | ||
window.history.replaceState(undefined, '', url) | ||
} | ||
|
||
const resetUrl = () => { | ||
if (typeof window === 'undefined') return | ||
|
||
window.history.replaceState(undefined, '', window.location.origin + window.location.pathname) | ||
} | ||
|
||
const deletingParamInUrl = (name: string) => { | ||
if (typeof window === 'undefined') return | ||
|
||
const url = new URL(window.location.href) | ||
url.searchParams.delete(name) | ||
window.history.replaceState(undefined, '', url) | ||
} | ||
|
||
export class UrlSaver { | ||
getValue(name: string) { | ||
return getDecompressedStringFromUrl(name) | ||
} | ||
|
||
setValue(name: string, value: string) { | ||
updateUrlWithCompressedString(name, value) | ||
} | ||
|
||
reset(name: string) { | ||
deletingParamInUrl(name) | ||
} | ||
|
||
resetAll() { | ||
resetUrl() | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.