-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor!: UrlSearchParams based Hash
#7073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CommanderStorm
wants to merge
27
commits into
maplibre:main
Choose a base branch
from
CommanderStorm:URLSearchParams-based-hash
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 23 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
18ab1a8
Add tests for Hash
CommanderStorm bd37fe2
Migrate to using UrlSearchParams
CommanderStorm 53977e2
Add test that only passes after
CommanderStorm 9a4aa80
Merge branch 'main' into URLSearchParams-based-hash
CommanderStorm 661f343
Update src/ui/hash.test.ts
CommanderStorm 80f0973
Apply suggestion from @Copilot
CommanderStorm 578887c
Revert "Apply suggestion from @Copilot"
CommanderStorm 2cc15ba
remove _buildHashString
CommanderStorm be00422
fix typo
CommanderStorm a3fa7b6
simplify a bit more
CommanderStorm 615b59c
Apply suggestion from @CommanderStorm
CommanderStorm 919d058
Merge branch 'main' into URLSearchParams-based-hash
CommanderStorm 38a0c92
Split some tests
CommanderStorm f59809a
Merge branch 'main' into URLSearchParams-based-hash
CommanderStorm 85dcd78
fix merge issue
CommanderStorm 9ce289c
split tests
CommanderStorm 9e859e6
refactor _getCurrentHash to use the params
CommanderStorm f1e70d8
update tests
CommanderStorm 26839d7
Merge branch 'main' into URLSearchParams-based-hash
CommanderStorm cbdca58
Merge branch 'main' into URLSearchParams-based-hash
CommanderStorm 8ba995c
Apply suggestion from @CommanderStorm
CommanderStorm 411fd7a
Update src/ui/hash.ts
CommanderStorm ae6ae6e
Apply suggestions from code review
CommanderStorm 97cebf2
Apply suggestion from @CommanderStorm
CommanderStorm a794bd0
fix lints
CommanderStorm 3e04b9f
update changed tests names
CommanderStorm 068d16b
Merge branch 'main' into URLSearchParams-based-hash
CommanderStorm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -65,40 +65,25 @@ | |
| if (pitch) hash += (`/${Math.round(pitch)}`); | ||
|
|
||
| if (this._hashName) { | ||
| const hashName = this._hashName; | ||
| let found = false; | ||
| const parts = window.location.hash.slice(1).split('&').map(part => { | ||
| const key = part.split('=')[0]; | ||
| if (key === hashName) { | ||
| found = true; | ||
| return `${key}=${hash}`; | ||
| } | ||
| return part; | ||
| }).filter(a => a); | ||
| if (!found) { | ||
| parts.push(`${hashName}=${hash}`); | ||
| } | ||
| return `#${parts.join('&')}`; | ||
| const params = this._getHashParams(); | ||
| params.set(this._hashName, hash); | ||
| return `#${decodeURIComponent(params.toString()).replace(/=&|=$/g, '')}`; | ||
| } | ||
|
|
||
| return `#${hash}`; | ||
| } | ||
|
|
||
| _getHashParams = () => { | ||
| return new URLSearchParams(window.location.hash.replace('#', '')); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
| }; | ||
|
CommanderStorm marked this conversation as resolved.
|
||
|
|
||
| _getCurrentHash = () => { | ||
| // Get the current hash from location, stripped from its number sign | ||
| const hash = window.location.hash.replace('#', ''); | ||
| const params = this._getHashParams(); | ||
| if (this._hashName) { | ||
| // Split the parameter-styled hash into parts and find the value we need | ||
| let keyval; | ||
| hash.split('&').map( | ||
| part => part.split('=') | ||
| ).forEach(part => { | ||
| if (part[0] === this._hashName) { | ||
| keyval = part; | ||
| } | ||
| }); | ||
| return (keyval ? keyval[1] || '' : '').split('/'); | ||
| return (params.get(this._hashName) || '').split('/'); | ||
| } | ||
|
CommanderStorm marked this conversation as resolved.
|
||
| // For unnamed hashes, get the first key | ||
| const hash = [...params.keys()][0] ?? ''; | ||
| return hash.split('/'); | ||
| }; | ||
|
|
||
|
|
@@ -121,30 +106,25 @@ | |
| }; | ||
|
|
||
| _updateHashUnthrottled = () => { | ||
| // Replace if already present, else append the updated hash string | ||
| const location = window.location.href.replace(/(#.*)?$/, this.getHashString()); | ||
| window.history.replaceState(window.history.state, null, location); | ||
| }; | ||
|
|
||
| _removeHash = () => { | ||
| const currentHash = this._getCurrentHash(); | ||
| if (currentHash.length === 0) { | ||
| return; | ||
| } | ||
| const baseHash = currentHash.join('/'); | ||
| let targetHash = baseHash; | ||
| if (targetHash.split('&').length > 0) { | ||
| targetHash = targetHash.split('&')[0]; // #3/1/2&foo=bar -> #3/1/2 | ||
| } | ||
| const params = this._getHashParams(); | ||
|
|
||
| if (this._hashName) { | ||
| targetHash = `${this._hashName}=${baseHash}`; | ||
| } | ||
| let replaceString = window.location.hash.replace(targetHash, ''); | ||
| if (replaceString.startsWith('#&')) { | ||
| replaceString = replaceString.slice(0, 1) + replaceString.slice(2); | ||
| } else if (replaceString === '#') { | ||
| replaceString = ''; | ||
| params.delete(this._hashName); | ||
| } else { | ||
| // For unnamed hash (#zoom/lat/lng&other=params), remove first entry | ||
| const keys = Array.from(params.keys()); | ||
| if (keys.length > 0) { | ||
| params.delete(keys[0]); | ||
| } | ||
| } | ||
|
|
||
| const newHash = decodeURIComponent(params.toString()); | ||
|
CommanderStorm marked this conversation as resolved.
Outdated
|
||
| const replaceString = newHash ? `#${newHash}` : '' | ||
| let location = window.location.href.replace(/(#.+)?$/, replaceString); | ||
| location = location.replace('&&', '&'); | ||
| window.history.replaceState(window.history.state, null, location); | ||
|
|
@@ -155,8 +135,8 @@ | |
| */ | ||
| _updateHash: () => ReturnType<typeof setTimeout> = throttle(this._updateHashUnthrottled, 30 * 1000 / 100); | ||
|
|
||
| _isValidHash(hash: number[]) { | ||
| if (hash.length < 3 || hash.some(isNaN)) { | ||
| _isValidHash(hash: string[]) { | ||
| if (hash.length < 3 || hash.some(h => isNaN(+h))) { | ||
| return false; | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d characterize this as a routine bug fix rather than a backwards-incompatible change. I don’t think anyone would be expecting the
%2Fto persist, and we continue to recognize it anyways.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A case where this is needed for example is if you have a link to a file or location as part of the query parameters, then you can't use "/" but you have to use the encoded way. I'm not sure this answers any question, but I wanted to give a use case.
If this is kept the same and doesn't break, that's great.