mirror of
https://github.com/a-nyx/maputnik-with-pmtiles.git
synced 2024-12-27 09:35:25 +01:00
Fetch basic default style on startup
This commit is contained in:
parent
2cc4055416
commit
f444ebf65d
2 changed files with 33 additions and 7 deletions
|
@ -5,7 +5,7 @@ import { Drawer, Container, Block, Fixed } from 'rebass'
|
||||||
import {Map} from './map.jsx'
|
import {Map} from './map.jsx'
|
||||||
import {Toolbar} from './toolbar.jsx'
|
import {Toolbar} from './toolbar.jsx'
|
||||||
import { StyleManager } from './style.js'
|
import { StyleManager } from './style.js'
|
||||||
import { SettingsStore, StyleStore } from './stylestore.js'
|
import { loadDefaultStyle, SettingsStore, StyleStore } from './stylestore.js'
|
||||||
import { WorkspaceDrawer } from './workspace.jsx'
|
import { WorkspaceDrawer } from './workspace.jsx'
|
||||||
|
|
||||||
import theme from './theme.js'
|
import theme from './theme.js'
|
||||||
|
@ -25,6 +25,10 @@ export default class App extends React.Component {
|
||||||
workContext: "layers",
|
workContext: "layers",
|
||||||
currentStyle: this.styleStore.latestStyle(),
|
currentStyle: this.styleStore.latestStyle(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loadDefaultStyle(mapStyle => {
|
||||||
|
this.onStyleUpload(mapStyle)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
getChildContext() {
|
getChildContext() {
|
||||||
|
@ -35,10 +39,10 @@ export default class App extends React.Component {
|
||||||
}
|
}
|
||||||
|
|
||||||
onStyleDownload() {
|
onStyleDownload() {
|
||||||
this.styleStore.save(newStyle)
|
|
||||||
const mapStyle = JSON.stringify(this.state.currentStyle.toJS(), null, 4)
|
const mapStyle = JSON.stringify(this.state.currentStyle.toJS(), null, 4)
|
||||||
const blob = new Blob([mapStyle], {type: "application/json;charset=utf-8"});
|
const blob = new Blob([mapStyle], {type: "application/json;charset=utf-8"});
|
||||||
saveAs(blob, mapStyle.id + ".json");
|
saveAs(blob, mapStyle.id + ".json");
|
||||||
|
this.onStyleSave(mapStyle)
|
||||||
}
|
}
|
||||||
|
|
||||||
onStyleUpload(newStyle) {
|
onStyleUpload(newStyle) {
|
||||||
|
|
|
@ -7,13 +7,37 @@ const storageKeys = {
|
||||||
accessToken: [storagePrefix, 'access_token'].join('')
|
accessToken: [storagePrefix, 'access_token'].join('')
|
||||||
}
|
}
|
||||||
|
|
||||||
const emptyStyle = {
|
// Empty style is always used if no style could be restored or fetched
|
||||||
|
const emptyStyle = ensureOptionalStyleProps(Immutable.fromJS({
|
||||||
version: 8,
|
version: 8,
|
||||||
sources: {},
|
sources: {},
|
||||||
layers: [],
|
layers: [],
|
||||||
|
}))
|
||||||
|
|
||||||
|
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
|
||||||
|
export function loadDefaultStyle(cb) {
|
||||||
|
var request = new XMLHttpRequest();
|
||||||
|
request.open('GET', defaultStyleUrl, true);
|
||||||
|
|
||||||
|
request.onload = () => {
|
||||||
|
if (request.status >= 200 && request.status < 400) {
|
||||||
|
cb(Immutable.fromJS(JSON.parse(request.responseText)))
|
||||||
|
} else {
|
||||||
|
cb(emptyStyle)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
request.onerror = function() {
|
||||||
|
console.log('Could not fetch default style')
|
||||||
|
cb(emptyStyle)
|
||||||
|
};
|
||||||
|
|
||||||
|
request.send();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 < localStorage.length; i++) {
|
||||||
|
@ -78,9 +102,7 @@ export class StyleStore {
|
||||||
|
|
||||||
// Find the last edited style
|
// Find the last edited style
|
||||||
latestStyle() {
|
latestStyle() {
|
||||||
if(this.mapStyles.length == 0) {
|
if(this.mapStyles.length == 0) return emptyStyle
|
||||||
return ensureOptionalStyleProps(Immutable.fromJS(emptyStyle))
|
|
||||||
}
|
|
||||||
const styleId = window.localStorage.getItem(storageKeys.latest)
|
const styleId = window.localStorage.getItem(storageKeys.latest)
|
||||||
const styleItem = window.localStorage.getItem(styleKey(styleId))
|
const styleItem = window.localStorage.getItem(styleKey(styleId))
|
||||||
return Immutable.fromJS(JSON.parse(styleItem))
|
return Immutable.fromJS(JSON.parse(styleItem))
|
||||||
|
|
Loading…
Reference in a new issue