Skip to content
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

feat: human-readable URLs based on query strings #389

Merged
merged 7 commits into from
Jul 16, 2024
Merged
Prev Previous commit
Next Next commit
fix: Suppress leading # in color query params
  • Loading branch information
gwhitney committed Jul 16, 2024
commit cad64d763202b814b3537861b231fb7c41c8ff66
22 changes: 18 additions & 4 deletions src/shared/Paramable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,14 @@ export class Paramable<PD extends GenericParamDescription>
const defaultString = typeFunctions[param.type].derealize(
param.default as never
)
if (tv[key] !== defaultString) saveParams[key] = tv[key]
if (tv[key] !== defaultString) {
// Avoid percent-encoding for colors
let qv = tv[key]
if (param.type === ParamType.COLOR && qv[0] === '#') {
qv = qv.substring(1)
}
saveParams[key] = qv
}
}
}
const urlParams = new URLSearchParams(saveParams)
Expand All @@ -429,9 +436,16 @@ export class Paramable<PD extends GenericParamDescription>
loadQuery(query: string): void {
const params = new URLSearchParams(query)
for (const [key, value] of params) {
if (key in this.tentativeValues)
this.tentativeValues[key as keyof PD] = value
else console.warn(`Invalid property ${key} for ${this.name}`)
if (key in this.tentativeValues) {
const param = this.params[key]
const pdKey = key as keyof PD
if (
param.type === ParamType.COLOR
&& value.match(/^[0-9a-fA-F]{6}$/)
)
this.tentativeValues[pdKey] = '#' + value
else this.tentativeValues[pdKey] = value
} else console.warn(`Invalid property ${key} for ${this.name}`)
}
}
}
Loading