mirror of
https://github.com/a-nyx/maputnik-with-pmtiles.git
synced 2025-01-30 05:55:32 +01:00
Improve optional metadata handling
This commit is contained in:
parent
adc00138a8
commit
a7b32cffe3
2 changed files with 24 additions and 14 deletions
26
src/style.js
26
src/style.js
|
@ -13,15 +13,7 @@ function fromJSON(jsonStyle) {
|
||||||
jsonStyle = JSON.parse(jsonStyle)
|
jsonStyle = JSON.parse(jsonStyle)
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!('id' in jsonStyle)) {
|
return Immutable.Map(Object.keys(jsonStyle).map(key => {
|
||||||
jsonStyle.id = Math.random().toString(36).substr(2, 9)
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!('created' in jsonStyle)) {
|
|
||||||
jsonStyle.created = new Date().toJSON()
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Immutable.Map(Object.keys(jsonStyle).map(key => {
|
|
||||||
const val = jsonStyle[key]
|
const val = jsonStyle[key]
|
||||||
if(key === "layers") {
|
if(key === "layers") {
|
||||||
return [key, Immutable.OrderedMap(val.map(l => [l.id, Immutable.fromJS(l)]))]
|
return [key, Immutable.OrderedMap(val.map(l => [l.id, Immutable.fromJS(l)]))]
|
||||||
|
@ -33,6 +25,21 @@ function fromJSON(jsonStyle) {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function ensureHasId(style) {
|
||||||
|
if(style.has('id')) return style
|
||||||
|
console.log('has no id', style.toJS())
|
||||||
|
return style.set('id', Math.random().toString(36).substr(2, 9))
|
||||||
|
}
|
||||||
|
|
||||||
|
function ensureHasTimestamp(style) {
|
||||||
|
if(style.has('id')) return style
|
||||||
|
return style.set('created', new Date().toJSON())
|
||||||
|
}
|
||||||
|
|
||||||
|
function ensureMetadataExists(style) {
|
||||||
|
return ensureHasId(ensureHasTimestamp(style))
|
||||||
|
}
|
||||||
|
|
||||||
// Compare style with other style and return changes
|
// Compare style with other style and return changes
|
||||||
//TODO: Write own diff algo that operates on immutable collections
|
//TODO: Write own diff algo that operates on immutable collections
|
||||||
// Should be able to improve performance since we can only compare
|
// Should be able to improve performance since we can only compare
|
||||||
|
@ -74,4 +81,5 @@ export default {
|
||||||
toJSON,
|
toJSON,
|
||||||
fromJSON,
|
fromJSON,
|
||||||
diffStyles,
|
diffStyles,
|
||||||
|
ensureMetadataExists,
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ import Immutable from 'immutable'
|
||||||
import style from './style.js'
|
import style from './style.js'
|
||||||
|
|
||||||
const storagePrefix = "maputnik"
|
const storagePrefix = "maputnik"
|
||||||
|
const stylePrefix = 'style'
|
||||||
const storageKeys = {
|
const storageKeys = {
|
||||||
latest: [storagePrefix, 'latest_style'].join(':'),
|
latest: [storagePrefix, 'latest_style'].join(':'),
|
||||||
accessToken: [storagePrefix, 'access_token'].join(':')
|
accessToken: [storagePrefix, 'access_token'].join(':')
|
||||||
|
@ -40,8 +41,8 @@ export function loadDefaultStyle(cb) {
|
||||||
// Return style ids and dates of all styles stored in local storage
|
// Return style ids and dates of all styles stored in local storage
|
||||||
function loadStoredStyles() {
|
function loadStoredStyles() {
|
||||||
const styles = []
|
const styles = []
|
||||||
for (let i = 0; i < localStorage.length; i++) {
|
for (let i = 0; i < window.localStorage.length; i++) {
|
||||||
const key = localStorage.key(i)
|
const key = window.localStorage.key(i)
|
||||||
if(isStyleKey(key)) {
|
if(isStyleKey(key)) {
|
||||||
styles.push(fromKey(key))
|
styles.push(fromKey(key))
|
||||||
}
|
}
|
||||||
|
@ -51,7 +52,7 @@ function loadStoredStyles() {
|
||||||
|
|
||||||
function isStyleKey(key) {
|
function isStyleKey(key) {
|
||||||
const parts = key.split(":")
|
const parts = key.split(":")
|
||||||
return parts.length == 2 && parts[0] === storagePrefix
|
return parts.length == 3 && parts[0] === storagePrefix && parts[1] === stylePrefix
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load style id from key
|
// Load style id from key
|
||||||
|
@ -61,13 +62,13 @@ function fromKey(key) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const parts = key.split(":")
|
const parts = key.split(":")
|
||||||
const styleId = parts[1]
|
const styleId = parts[2]
|
||||||
return styleId
|
return styleId
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate key that identifies the style with a version
|
// Calculate key that identifies the style with a version
|
||||||
function styleKey(styleId) {
|
function styleKey(styleId) {
|
||||||
return [storagePrefix, styleId].join(":")
|
return [storagePrefix, stylePrefix, styleId].join(":")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store style independent settings
|
// Store style independent settings
|
||||||
|
@ -101,6 +102,7 @@ export class StyleStore {
|
||||||
|
|
||||||
// Save current style replacing previous version
|
// Save current style replacing previous version
|
||||||
save(mapStyle) {
|
save(mapStyle) {
|
||||||
|
mapStyle = style.ensureMetadataExists(mapStyle)
|
||||||
const key = styleKey(mapStyle.get('id'))
|
const key = styleKey(mapStyle.get('id'))
|
||||||
window.localStorage.setItem(key, JSON.stringify(style.toJSON(mapStyle)))
|
window.localStorage.setItem(key, JSON.stringify(style.toJSON(mapStyle)))
|
||||||
window.localStorage.setItem(storageKeys.latest, mapStyle.get('id'))
|
window.localStorage.setItem(storageKeys.latest, mapStyle.get('id'))
|
||||||
|
|
Loading…
Reference in a new issue