mirror of
https://github.com/maputnik/editor.git
synced 2025-12-29 01:20:01 +00:00
Added UrlInput component to tidy things up a little.
This commit is contained in:
@@ -8,10 +8,15 @@ class StringInput extends React.Component {
|
||||
style: PropTypes.object,
|
||||
default: PropTypes.string,
|
||||
onChange: PropTypes.func,
|
||||
onInput: PropTypes.func,
|
||||
multi: PropTypes.bool,
|
||||
required: PropTypes.bool,
|
||||
}
|
||||
|
||||
static defaultProps = {
|
||||
onInput: () => {},
|
||||
}
|
||||
|
||||
constructor(props) {
|
||||
super(props)
|
||||
this.state = {
|
||||
@@ -57,7 +62,9 @@ class StringInput extends React.Component {
|
||||
this.setState({
|
||||
editing: true,
|
||||
value: e.target.value
|
||||
})
|
||||
}, () => {
|
||||
this.props.onInput(this.state.value);
|
||||
});
|
||||
},
|
||||
onBlur: () => {
|
||||
if(this.state.value!==this.props.value) {
|
||||
|
||||
61
src/components/inputs/UrlInput.jsx
Normal file
61
src/components/inputs/UrlInput.jsx
Normal file
@@ -0,0 +1,61 @@
|
||||
import React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import StringInput from './StringInput'
|
||||
import SmallError from '../util/SmallError'
|
||||
|
||||
class UrlInput extends React.Component {
|
||||
static propTypes = {
|
||||
"data-wd-key": PropTypes.string,
|
||||
value: PropTypes.string,
|
||||
style: PropTypes.object,
|
||||
default: PropTypes.string,
|
||||
onChange: PropTypes.func,
|
||||
multi: PropTypes.bool,
|
||||
required: PropTypes.bool,
|
||||
}
|
||||
|
||||
state = {
|
||||
error: null,
|
||||
}
|
||||
|
||||
onInput = (url) => {
|
||||
let error;
|
||||
const getProtocol = (url) => {
|
||||
try {
|
||||
const urlObj = new URL(url);
|
||||
return urlObj.protocol;
|
||||
}
|
||||
catch (err) {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
const protocol = getProtocol(url);
|
||||
if (
|
||||
protocol &&
|
||||
protocol === "https:" &&
|
||||
window.location.protocol !== "http:"
|
||||
) {
|
||||
error = (
|
||||
<SmallError>
|
||||
CORs policy won't allow fetching resources served over http from https, use a <code>https://</code> domain
|
||||
</SmallError>
|
||||
);
|
||||
}
|
||||
|
||||
this.setState({error});
|
||||
}
|
||||
|
||||
render () {
|
||||
return (
|
||||
<div>
|
||||
<StringInput
|
||||
{...this.props}
|
||||
onInput={this.onInput}
|
||||
/>
|
||||
{this.state.error}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default UrlInput
|
||||
@@ -158,15 +158,12 @@ class AddSource extends React.Component {
|
||||
}
|
||||
|
||||
onAdd = () => {
|
||||
this.props.onAdd(this.state.sourceId, this.state.source);
|
||||
const {source, sourceId} = this.state;
|
||||
this.props.onAdd(sourceId, source);
|
||||
}
|
||||
|
||||
onChangeSource = (source) => {
|
||||
// let error = "CORs policy won't allow fetching resources served over http from https";
|
||||
this.setState({
|
||||
source,
|
||||
error,
|
||||
});
|
||||
this.setState({source});
|
||||
}
|
||||
|
||||
render() {
|
||||
@@ -198,15 +195,9 @@ class AddSource extends React.Component {
|
||||
mode={this.state.mode}
|
||||
source={this.state.source}
|
||||
/>
|
||||
{this.state.error &&
|
||||
<div className="maputnik-add-source-error" style={{fontSize: "12px", color: "#E57373"}}>
|
||||
Error: {this.state.error}
|
||||
</div>
|
||||
}
|
||||
<Button
|
||||
className="maputnik-add-source-button"
|
||||
onClick={this.onAdd}
|
||||
disabled={!!this.state.error}
|
||||
>
|
||||
Add Source
|
||||
</Button>
|
||||
|
||||
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
|
||||
import {latest} from '@mapbox/mapbox-gl-style-spec'
|
||||
import InputBlock from '../inputs/InputBlock'
|
||||
import StringInput from '../inputs/StringInput'
|
||||
import UrlInput from '../inputs/UrlInput'
|
||||
import NumberInput from '../inputs/NumberInput'
|
||||
import SelectInput from '../inputs/SelectInput'
|
||||
import JSONEditor from '../layers/JSONEditor'
|
||||
@@ -18,7 +19,7 @@ class TileJSONSourceEditor extends React.Component {
|
||||
render() {
|
||||
return <div>
|
||||
<InputBlock label={"TileJSON URL"} doc={latest.source_vector.url.doc}>
|
||||
<StringInput
|
||||
<UrlInput
|
||||
value={this.props.source.url}
|
||||
onChange={url => this.props.onChange({
|
||||
...this.props.source,
|
||||
@@ -52,7 +53,7 @@ class TileURLSourceEditor extends React.Component {
|
||||
const tiles = this.props.source.tiles || []
|
||||
return tiles.map((tileUrl, tileIndex) => {
|
||||
return <InputBlock key={tileIndex} label={prefix[tileIndex] + " Tile URL"} doc={latest.source_vector.tiles.doc}>
|
||||
<StringInput
|
||||
<UrlInput
|
||||
value={tileUrl}
|
||||
onChange={this.changeTileUrl.bind(this, tileIndex)}
|
||||
/>
|
||||
|
||||
20
src/components/util/SmallError.jsx
Normal file
20
src/components/util/SmallError.jsx
Normal file
@@ -0,0 +1,20 @@
|
||||
import React from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import './SmallError.scss';
|
||||
|
||||
|
||||
class SmallError extends React.Component {
|
||||
static propTypes = {
|
||||
children: PropTypes.node,
|
||||
}
|
||||
|
||||
render () {
|
||||
return (
|
||||
<div className="SmallError">
|
||||
Error: {this.props.children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default SmallError
|
||||
7
src/components/util/SmallError.scss
Normal file
7
src/components/util/SmallError.scss
Normal file
@@ -0,0 +1,7 @@
|
||||
@import '../../styles/vars';
|
||||
|
||||
.SmallError {
|
||||
color: #E57373;
|
||||
font-size: $font-size-5;
|
||||
margin-top: $margin-2
|
||||
}
|
||||
Reference in New Issue
Block a user