Correctly load previously saved style

This commit is contained in:
lukasmartinelli 2016-09-21 08:25:10 +02:00
parent af34049069
commit 9d4053dd78
4 changed files with 11 additions and 10 deletions

View file

@ -29,10 +29,9 @@ export default class App extends React.Component {
workContext: "layers", workContext: "layers",
currentStyle: this.styleStore.latestStyle(), currentStyle: this.styleStore.latestStyle(),
} }
if(this.state.currentStyle.get('layers').size === 0) {
loadDefaultStyle(mapStyle => { loadDefaultStyle(mapStyle => this.onStyleUpload(mapStyle))
this.onStyleUpload(mapStyle) }
})
} }
getChildContext() { getChildContext() {
@ -57,6 +56,7 @@ export default class App extends React.Component {
onStyleSave() { onStyleSave() {
const snapshotStyle = this.state.currentStyle.set('modified', new Date().toJSON()) const snapshotStyle = this.state.currentStyle.set('modified', new Date().toJSON())
this.setState({ currentStyle: snapshotStyle }) this.setState({ currentStyle: snapshotStyle })
this.styleStore.save(snapshotStyle)
} }
onStyleChanged(newStyle) { onStyleChanged(newStyle) {

View file

@ -27,7 +27,6 @@ function fromJSON(jsonStyle) {
function ensureHasId(style) { function ensureHasId(style) {
if(style.has('id')) return 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)) return style.set('id', Math.random().toString(36).substr(2, 9))
} }

View file

@ -10,21 +10,22 @@ const storageKeys = {
} }
// Empty style is always used if no style could be restored or fetched // Empty style is always used if no style could be restored or fetched
const emptyStyle = style.fromJSON({ const emptyStyle = style.ensureMetadataExists(style.fromJSON({
version: 8, version: 8,
sources: {}, sources: {},
layers: [], layers: [],
}) }))
const defaultStyleUrl = "https://raw.githubusercontent.com/osm2vectortiles/mapbox-gl-styles/master/styles/basic-v9-cdn.json" const defaultStyleUrl = "https://raw.githubusercontent.com/osm2vectortiles/mapbox-gl-styles/master/styles/basic-v9-cdn.json"
// Fetch a default style via URL and return it or a fallback style via callback // Fetch a default style via URL and return it or a fallback style via callback
export function loadDefaultStyle(cb) { export function loadDefaultStyle(cb) {
console.log('Load default style')
var request = new XMLHttpRequest() var request = new XMLHttpRequest()
request.open('GET', defaultStyleUrl, true) request.open('GET', defaultStyleUrl, true)
request.onload = () => { request.onload = () => {
if (request.status >= 200 && request.status < 400) { if (request.status >= 200 && request.status < 400) {
cb(style.fromJSON(request.responseText)) cb(style.ensureMetadataExists(style.fromJSON(request.responseText)))
} else { } else {
cb(emptyStyle) cb(emptyStyle)
} }
@ -102,7 +103,6 @@ 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'))

View file

@ -48,7 +48,9 @@ export class Toolbar extends React.Component {
const reader = new FileReader(); const reader = new FileReader();
reader.readAsText(file, "UTF-8"); reader.readAsText(file, "UTF-8");
reader.onload = e => { reader.onload = e => {
this.props.onStyleUpload(style.fromJSON(JSON.parse(e.target.result))); let mapStyle = style.fromJSON(JSON.parse(e.target.result))
mapStyle = style.ensureMetadataExists(mapStyle)
this.props.onStyleUpload(mapStyle);
} }
reader.onerror = e => console.log(e.target); reader.onerror = e => console.log(e.target);
} }