-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add map widget #2051
Merged
Merged
Add map widget #2051
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Change Log | ||
|
||
All notable changes to this project will be documented in this file. | ||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Docs coming soon! | ||
|
||
Netlify CMS was recently converted from a single npm package to a "monorepo" of over 20 packages. | ||
That's over 20 Readme's! We haven't created one for this package yet, but we will soon. | ||
|
||
In the meantime, you can: | ||
|
||
1. Check out the [main readme](https://github.com/netlify/netlify-cms/#readme) or the [documentation | ||
site](https://www.netlifycms.org) for more info. | ||
2. Reach out to the [community chat](https://gitter.im/netlify/netlifycms/) if you need help. | ||
3. Help out and [write the readme yourself](https://github.com/netlify/netlify-cms/edit/master/packages/netlify-cms-widget-list/README.md)! | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "netlify-cms-widget-map", | ||
"description": "Widget for editing spatial data in Netlify CMS.", | ||
"version": "2.0.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. New packages can start at 1.0.0 |
||
"homepage": "https://www.netlifycms.org/docs/widgets/#map", | ||
"repository": "https://github.com/netlify/netlify-cms/tree/master/packages/netlify-cms-widget-map", | ||
"bugs": "https://github.com/netlify/netlify-cms/issues", | ||
"main": "dist/netlify-cms-widget-map.js", | ||
"license": "MIT", | ||
"keywords": [ | ||
"netlify", | ||
"netlify-cms", | ||
"widget", | ||
"spatial", | ||
"map" | ||
], | ||
"sideEffects": false, | ||
"scripts": { | ||
"watch": "webpack -w", | ||
"develop": "npm run watch", | ||
"build": "cross-env NODE_ENV=production webpack" | ||
}, | ||
"devDependencies": { | ||
"cross-env": "^5.2.0", | ||
"css-loader": "^2.1.0", | ||
"to-string-loader": "^1.1.5", | ||
"webpack": "^4.16.1", | ||
"webpack-cli": "^3.1.0" | ||
}, | ||
"peerDependencies": { | ||
"emotion": "^9.2.6", | ||
"lodash": "^4.17.10", | ||
"netlify-cms-ui-default": "^2.0.0", | ||
"prop-types": "^15.5.10", | ||
"react": "^16.4.1", | ||
"react-immutable-proptypes": "^2.1.0" | ||
}, | ||
"dependencies": { | ||
"ol": "^5.3.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { WidgetPreviewContainer } from 'netlify-cms-ui-default'; | ||
|
||
const MapPreview = ({ value }) => ( | ||
<WidgetPreviewContainer>{value ? value.toString() : null}</WidgetPreviewContainer> | ||
); | ||
|
||
MapPreview.propTypes = { | ||
value: PropTypes.string, | ||
}; | ||
|
||
export default MapPreview; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import withMapControl from './withMapControl'; | ||
|
||
export { withMapControl }; | ||
export const MapControl = withMapControl(); | ||
export MapPreview from './MapPreview'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import olStyles from 'ol/ol.css'; | ||
import Map from 'ol/Map.js'; | ||
import View from 'ol/View.js'; | ||
import GeoJSON from 'ol/format/GeoJSON'; | ||
import Draw from 'ol/interaction/Draw.js'; | ||
import TileLayer from 'ol/layer/Tile.js'; | ||
import VectorLayer from 'ol/layer/Vector.js'; | ||
import OSMSource from 'ol/source/OSM.js'; | ||
import VectorSource from 'ol/source/Vector.js'; | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import { injectGlobal } from 'react-emotion'; | ||
|
||
injectGlobal` | ||
${olStyles} | ||
`; | ||
|
||
const formatOptions = { | ||
dataProjection: 'EPSG:4326', | ||
featureProjection: 'EPSG:3857', | ||
}; | ||
const getDefaultFormat = () => new GeoJSON(formatOptions); | ||
|
||
const getDefaultMap = (target, featuresLayer) => | ||
new Map({ | ||
target, | ||
layers: [new TileLayer({ source: new OSMSource() }), featuresLayer], | ||
view: new View({ center: [0, 0], zoom: 2 }), | ||
}); | ||
|
||
export default function withMapControl({ getFormat, getMap } = {}) { | ||
return class MapControl extends React.Component { | ||
static propTypes = { | ||
onChange: PropTypes.func.isRequired, | ||
field: PropTypes.object.isRequired, | ||
value: PropTypes.node, | ||
}; | ||
|
||
static defaultProps = { | ||
value: '', | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.mapContainer = React.createRef(); | ||
} | ||
|
||
componentDidMount() { | ||
const { field, onChange, value } = this.props; | ||
const format = getFormat ? getFormat(field) : getDefaultFormat(field); | ||
const features = value ? [format.readFeature(value)] : []; | ||
|
||
const featuresSource = new VectorSource({ features, wrapX: false }); | ||
const featuresLayer = new VectorLayer({ source: featuresSource }); | ||
|
||
const target = this.mapContainer.current; | ||
const map = getMap ? getMap(target, featuresLayer) : getDefaultMap(target, featuresLayer); | ||
if (features.length > 0) { | ||
map.getView().fit(featuresSource.getExtent(), { maxZoom: 16, padding: [80, 80, 80, 80] }); | ||
} | ||
|
||
const draw = new Draw({ source: featuresSource, type: field.get('type', 'Point') }); | ||
map.addInteraction(draw); | ||
|
||
const writeOptions = { decimals: field.get('decimals', 7) }; | ||
draw.on('drawend', ({ feature }) => { | ||
featuresSource.clear(); | ||
onChange(format.writeGeometry(feature.getGeometry(), writeOptions)); | ||
}); | ||
} | ||
|
||
render() { | ||
return <div ref={this.mapContainer}> </div>; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const { getConfig } = require('../../scripts/webpack.js'); | ||
|
||
const baseWebpackConfig = getConfig(); | ||
|
||
module.exports = { | ||
...baseWebpackConfig, | ||
module: { | ||
rules: [ | ||
...baseWebpackConfig.module.rules, | ||
{ | ||
test: /\.css$/, | ||
include: [/ol/], | ||
use: ['to-string-loader', 'css-loader'], | ||
}, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
title: map | ||
label: "Map" | ||
--- | ||
|
||
The map widget allows you to edit spatial data using an interactive map. Spatial data is saved as a GeoJSON string in WGS84 projection and is limited to a single geometry. | ||
|
||
- **Name:** `map` | ||
- **UI:** interactive map | ||
- **Data type:** GeoJSON string | ||
- **Options:** | ||
- `decimals`: accepts a number to specify precision of saved coordinates; defaults to 7 decimals | ||
- `default`: accepts a GeoJSON string containing a single geometry; defaults to an empty string | ||
- `type`: accepts one string value of `Point`, `LineString` or `Polygon`; defaults to `Point` | ||
- **Example:** | ||
```yaml | ||
- {label: "Location", name: "location", widget: "map" } | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace
netlify-cms-widget-list
withnetlify-cms-widget-map