mirror of
https://github.com/maputnik/editor.git
synced 2026-06-18 05:07:25 +00:00
Remove Immutable JS
This commit is contained in:
@@ -1,6 +1,4 @@
|
||||
import Immutable from 'immutable'
|
||||
import throttle from 'lodash.throttle'
|
||||
import entries from 'lodash.topairs'
|
||||
|
||||
/** Listens to map events to build up a store of available vector
|
||||
* layers contained in the tiles */
|
||||
@@ -47,22 +45,15 @@ export default class LayerWatcher {
|
||||
this._vectorLayers[vectorLayerId] = knownProperties
|
||||
})
|
||||
})
|
||||
|
||||
console.log(this.vectorLayers.toJSON())
|
||||
console.log(this.vectorLayers)
|
||||
}
|
||||
|
||||
/** Access all known sources and their vector tile ids */
|
||||
get sources() {
|
||||
return Immutable.Map(Object.keys(this._sources).map(key => {
|
||||
return [key, Immutable.Set(this._sources[key])]
|
||||
}))
|
||||
return this._sources
|
||||
}
|
||||
|
||||
get vectorLayers() {
|
||||
return Immutable.Map(entries(this._vectorLayers).map(([key, layer]) => {
|
||||
return [key, Immutable.Map(entries(layer).map(([propId, values]) => {
|
||||
return [propId, Immutable.Set(Object.keys(values))]
|
||||
}))]
|
||||
}))
|
||||
return this._vectorLayers
|
||||
}
|
||||
}
|
||||
|
||||
+8
-46
@@ -1,68 +1,30 @@
|
||||
import React from 'react';
|
||||
import Immutable from 'immutable'
|
||||
import spec from 'mapbox-gl-style-spec/reference/latest.min.js'
|
||||
|
||||
// Standard JSON to Immutable conversion except layers
|
||||
// are stored in an OrderedMap to make lookups id fast
|
||||
// It also ensures that every style has an id and
|
||||
// a created date for future reference
|
||||
function fromJSON(jsonStyle) {
|
||||
if (typeof jsonStyle === 'string' || jsonStyle instanceof String) {
|
||||
jsonStyle = JSON.parse(jsonStyle)
|
||||
}
|
||||
|
||||
return Immutable.Map(Object.keys(jsonStyle).map(key => {
|
||||
const val = jsonStyle[key]
|
||||
if(key === "layers") {
|
||||
return [key, Immutable.OrderedMap(val.map(l => [l.id, Immutable.fromJS(l)]))]
|
||||
} else if(key === "sources" || key === "metadata" || key === "transition") {
|
||||
return [key, Immutable.fromJS(val)]
|
||||
} else {
|
||||
return [key, val]
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
// Empty style is always used if no style could be restored or fetched
|
||||
const emptyStyle = ensureMetadataExists(fromJSON({
|
||||
const emptyStyle = ensureMetadataExists({
|
||||
version: 8,
|
||||
sources: {},
|
||||
layers: [],
|
||||
}))
|
||||
})
|
||||
|
||||
function ensureHasId(style) {
|
||||
if(style.has('id')) return style
|
||||
return style.set('id', Math.random().toString(36).substr(2, 9))
|
||||
if('id' in style) return style
|
||||
style.id = Math.random().toString(36).substr(2, 9)
|
||||
return style
|
||||
}
|
||||
|
||||
function ensureHasTimestamp(style) {
|
||||
if(style.has('id')) return style
|
||||
return style.set('created', new Date().toJSON())
|
||||
if('created' in style) return style
|
||||
style.created = new Date().toJSON()
|
||||
return style
|
||||
}
|
||||
|
||||
function ensureMetadataExists(style) {
|
||||
return ensureHasId(ensureHasTimestamp(style))
|
||||
}
|
||||
|
||||
// Turns immutable style back into JSON with the original order of the
|
||||
// layers preserved
|
||||
function toJSON(mapStyle) {
|
||||
const jsonStyle = {}
|
||||
for(let [key, value] of mapStyle.entries()) {
|
||||
if(key === "layers") {
|
||||
jsonStyle[key] = value.toIndexedSeq().toJS()
|
||||
} else if(key === 'sources' || key === "metadata" || key === "transition") {
|
||||
jsonStyle[key] = value.toJS()
|
||||
} else {
|
||||
jsonStyle[key] = value
|
||||
}
|
||||
}
|
||||
return jsonStyle
|
||||
}
|
||||
|
||||
export default {
|
||||
toJSON,
|
||||
fromJSON,
|
||||
ensureMetadataExists,
|
||||
emptyStyle,
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { colorizeLayers } from './style.js'
|
||||
import Immutable from 'immutable'
|
||||
import style from './style.js'
|
||||
|
||||
const storagePrefix = "maputnik"
|
||||
@@ -18,7 +17,7 @@ export function loadDefaultStyle(cb) {
|
||||
|
||||
request.onload = () => {
|
||||
if (request.status >= 200 && request.status < 400) {
|
||||
cb(style.ensureMetadataExists(style.fromJSON(request.responseText)))
|
||||
cb(style.ensureMetadataExists(JSON.parse(request.responseText)))
|
||||
} else {
|
||||
cb(style.emptyStyle)
|
||||
}
|
||||
@@ -104,15 +103,15 @@ export class StyleStore {
|
||||
const styleId = window.localStorage.getItem(storageKeys.latest)
|
||||
const styleItem = window.localStorage.getItem(styleKey(styleId))
|
||||
|
||||
if(styleItem) return cb(style.fromJSON(styleItem))
|
||||
if(styleItem) return cb(JSON.parse(styleItem))
|
||||
cb(style.emptyStyle)
|
||||
}
|
||||
|
||||
// Save current style replacing previous version
|
||||
save(mapStyle) {
|
||||
const key = styleKey(mapStyle.get('id'))
|
||||
window.localStorage.setItem(key, JSON.stringify(style.toJSON(mapStyle)))
|
||||
window.localStorage.setItem(storageKeys.latest, mapStyle.get('id'))
|
||||
const key = styleKey(mapStyle.id)
|
||||
window.localStorage.setItem(key, JSON.stringify(mapStyle))
|
||||
window.localStorage.setItem(storageKeys.latest, mapStyle.id)
|
||||
return mapStyle
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user