Allow download style

This commit is contained in:
lukasmartinelli 2016-09-09 16:58:48 +02:00
parent 52e4c92ccf
commit 3cc989e70f
5 changed files with 42 additions and 15 deletions

View file

@ -16,6 +16,7 @@
"license": "MIT", "license": "MIT",
"homepage": "https://github.com/lukasmartinelli/mapolo#readme", "homepage": "https://github.com/lukasmartinelli/mapolo#readme",
"dependencies": { "dependencies": {
"file-saver": "^1.3.2",
"mapbox-gl": "^0.23.0", "mapbox-gl": "^0.23.0",
"mapbox-gl-style-spec": "^8.8.0", "mapbox-gl-style-spec": "^8.8.0",
"node-sass": "^3.9.2", "node-sass": "^3.9.2",

View file

@ -1,4 +1,5 @@
import React from 'react' import React from 'react'
import {saveAs} from 'file-saver'
import { Drawer, Container, Block, Fixed } from 'rebass' import { Drawer, Container, Block, Fixed } from 'rebass'
import {Map} from './map.jsx' import {Map} from './map.jsx'
@ -49,6 +50,12 @@ export default class App extends React.Component {
} }
} }
onStyleDownload() {
const mapStyle = this.state.styleManager.exportStyle()
const blob = new Blob([mapStyle], {type: "application/json;charset=utf-8"});
saveAs(blob, "glstyle.json");
}
onStyleUpload(newStyle) { onStyleUpload(newStyle) {
this.setState({ styleManager: new StyleManager(newStyle) }) this.setState({ styleManager: new StyleManager(newStyle) })
} }
@ -64,7 +71,7 @@ export default class App extends React.Component {
render() { render() {
return <div style={{ fontFamily: theme.fontFamily, color: theme.color }}> return <div style={{ fontFamily: theme.fontFamily, color: theme.color }}>
<Toolbar onStyleUpload={this.onStyleUpload.bind(this)} /> <Toolbar onStyleUpload={this.onStyleUpload.bind(this)} onStyleDownload={this.onStyleDownload.bind(this)} />
<WorkspaceDrawer styleManager={this.state.styleManager}/> <WorkspaceDrawer styleManager={this.state.styleManager}/>
<div className={layout.map}> <div className={layout.map}>
<Map styleManager={this.state.styleManager} /> <Map styleManager={this.state.styleManager} />

View file

@ -19,6 +19,10 @@ export class StyleManager {
console.log(command) console.log(command)
} }
exportStyle() {
return JSON.stringify(this.mapStyle, null, 4)
}
layer(layerId) { layer(layerId) {
console.log(this.mapStyle) console.log(this.mapStyle)
return this.mapStyle.layers[layerId] return this.mapStyle.layers[layerId]

View file

@ -10,7 +10,7 @@ const baseColors = {
lowgray: '#dcdcdc', lowgray: '#dcdcdc',
white: '#fff', white: '#fff',
blue: '#00d9f7', blue: '#00d9f7',
green: '#0f8', green: '#B4C7AD',
orange: '#fb3', orange: '#fb3',
red: '#f04', red: '#f04',
} }

View file

@ -3,14 +3,22 @@ import FileReaderInput from 'react-file-reader-input';
import { Button, Text } from 'rebass'; import { Button, Text } from 'rebass';
import { Menu, NavItem, Tooltip, Container, Block, Fixed } from 'rebass' import { Menu, NavItem, Tooltip, Container, Block, Fixed } from 'rebass'
import {MdSettings, MdPalette, MdLayers, MdSave, MdFolderOpen} from 'react-icons/lib/md'; import { MdFileDownload, MdFileUpload, MdSettings, MdPalette, MdLayers, MdSave, MdFolderOpen} from 'react-icons/lib/md';
import theme from './theme.js'; import theme from './theme.js';
export class Toolbar extends React.Component { export class Toolbar extends React.Component {
static propTypes = {
onStyleUpload: React.PropTypes.func.isRequired,
onStyleDownload: React.PropTypes.func.isRequired
}
constructor(props) { constructor(props) {
super(props); super(props);
this.onUpload = this.onUpload.bind(this); this.onUpload = this.onUpload.bind(this);
this.state = {
styleUploaded: false
}
} }
onUpload(_, files) { onUpload(_, files) {
@ -20,11 +28,26 @@ export class Toolbar extends React.Component {
reader.onload = e => { reader.onload = e => {
const style = JSON.parse(e.target.result); const style = JSON.parse(e.target.result);
this.props.onStyleUpload(style); this.props.onStyleUpload(style);
this.setState({
styleUploaded: true
})
} }
reader.onerror = e => console.log(e.target); reader.onerror = e => console.log(e.target);
} }
render() { render() {
let downloadButton = null
if(this.state.styleUploaded) {
downloadButton = <Block>
<Button onClick={this.props.onStyleDownload} big={true}>
<Tooltip inverted rounded title="Download style">
<MdFileDownload />
</Tooltip>
</Button>
</Block>
}
console.log(this.state)
return <Container style={{ return <Container style={{
zIndex: 100, zIndex: 100,
position: "fixed", position: "fixed",
@ -36,16 +59,14 @@ export class Toolbar extends React.Component {
}> }>
<Block> <Block>
<FileReaderInput onChange={this.onUpload}> <FileReaderInput onChange={this.onUpload}>
<Button big={true}> <Button big={true} theme={this.state.styleUploaded ? "default" : "success"}>
<Tooltip inverted rounded title="Save"> <Tooltip inverted rounded title="Upload style">
<MdSave /> <MdFileUpload />
</Tooltip> </Tooltip>
</Button> </Button>
</FileReaderInput> </FileReaderInput>
</Block> </Block>
<Block> {downloadButton}
<Button big={true}><MdFolderOpen /></Button>
</Block>
<Block> <Block>
<Button big={true}> <Button big={true}>
<Tooltip inverted rounded title="Layers"> <Tooltip inverted rounded title="Layers">
@ -53,12 +74,6 @@ export class Toolbar extends React.Component {
</Tooltip> </Tooltip>
</Button> </Button>
</Block> </Block>
<Block>
<Button big={true}><MdPalette /></Button>
</Block>
<Block>
<Button big={true}><MdSettings /></Button>
</Block>
</Container> </Container>
} }
} }