mirror of https://github.com/sgoudham/carbon.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.2 KiB
JavaScript
52 lines
1.2 KiB
JavaScript
import Morph from 'morphmorph'
|
|
|
|
let createHistory
|
|
let history
|
|
if (typeof window !== 'undefined') {
|
|
createHistory = require('history').createBrowserHistory
|
|
history = createHistory()
|
|
}
|
|
|
|
const mapper = new Morph({
|
|
types: {
|
|
bool: v => {
|
|
if (v == null) return undefined
|
|
if (v === 'false') return false
|
|
return Boolean(v)
|
|
}
|
|
}
|
|
})
|
|
|
|
const mappings = [
|
|
{ field: 'bg:backgroundColor' },
|
|
{ field: 't:theme' },
|
|
{ field: 'l:language' },
|
|
{ field: 'ds:dropShadow', type: 'bool' },
|
|
{ field: 'wc:windowControls', type: 'bool' },
|
|
{ field: 'wa:widthAdjustment', type: 'bool' },
|
|
{ field: 'pv:paddingVertical' },
|
|
{ field: 'ph:paddingHorizontal' },
|
|
{ field: 'ln:lineNumbers', type: 'bool' },
|
|
{ field: 'code:code' }
|
|
]
|
|
|
|
const reverseMappings = mappings.map(mapping =>
|
|
Object.assign({}, mapping, {
|
|
field: mapping.field
|
|
.split(':')
|
|
.reverse()
|
|
.join(':')
|
|
})
|
|
)
|
|
|
|
const keysToQuery = keys =>
|
|
`?${Object.keys(keys)
|
|
.map(key => `${key}=${keys[key]}`)
|
|
.join('&')}`
|
|
|
|
export const getQueryStringState = query => mapper.map(mappings, query)
|
|
export const updateQueryString = state =>
|
|
history.replace({
|
|
search: encodeURI(keysToQuery(mapper.map(reverseMappings, state)))
|
|
})
|