Skip to content
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 2 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dev-test/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ collections: # A list of collections the CMS should be able to edit
valueField: 'title'
- { label: 'Title', name: 'title', widget: 'string' }
- { label: 'Boolean', name: 'boolean', widget: 'boolean', default: true }
- { label: 'Map', name: 'map', widget: 'map' }
- { label: 'Text', name: 'text', widget: 'text', hint: 'Plain text, not markdown' }
- { label: 'Number', name: 'number', widget: 'number', hint: 'To infinity and beyond!' }
- { label: 'Markdown', name: 'markdown', widget: 'markdown' }
Expand Down
2 changes: 1 addition & 1 deletion packages/netlify-cms-core/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {
},
{
test: /\.css$/,
include: [/(redux-notifications|react-datetime)/],
include: [/(ol|redux-notifications|react-datetime)/],
use: ['to-string-loader', 'css-loader'],
},
],
Expand Down
4 changes: 4 additions & 0 deletions packages/netlify-cms-widget-map/CHANGELOG.md
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.
11 changes: 11 additions & 0 deletions packages/netlify-cms-widget-map/README.md
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-map/README.md)!
41 changes: 41 additions & 0 deletions packages/netlify-cms-widget-map/package.json
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": "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"
}
}
13 changes: 13 additions & 0 deletions packages/netlify-cms-widget-map/src/MapPreview.js
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;
5 changes: 5 additions & 0 deletions packages/netlify-cms-widget-map/src/index.js
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';
76 changes: 76 additions & 0 deletions packages/netlify-cms-widget-map/src/withMapControl.js
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>;
}
};
}
17 changes: 17 additions & 0 deletions packages/netlify-cms-widget-map/webpack.config.js
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'],
},
],
},
};
1 change: 1 addition & 0 deletions packages/netlify-cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"netlify-cms-widget-file": "^2.2.0",
"netlify-cms-widget-image": "^2.1.1",
"netlify-cms-widget-list": "^2.1.0",
"netlify-cms-widget-map": "^1.0.0",
"netlify-cms-widget-markdown": "^2.1.0",
"netlify-cms-widget-number": "^2.0.5",
"netlify-cms-widget-object": "^2.0.5",
Expand Down
2 changes: 2 additions & 0 deletions packages/netlify-cms/src/widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { ListControl, ListPreview } from 'netlify-cms-widget-list/src';
import { ObjectControl, ObjectPreview } from 'netlify-cms-widget-object/src';
import { RelationControl, RelationPreview } from 'netlify-cms-widget-relation/src';
import { BooleanControl } from 'netlify-cms-widget-boolean/src';
import { MapControl, MapPreview } from 'netlify-cms-widget-map/src';

const { registerWidget } = cms;

Expand All @@ -28,3 +29,4 @@ registerWidget('select', SelectControl, SelectPreview);
registerWidget('object', ObjectControl, ObjectPreview);
registerWidget('relation', RelationControl, RelationPreview);
registerWidget('boolean', BooleanControl);
registerWidget('map', MapControl, MapPreview);
18 changes: 18 additions & 0 deletions website/content/docs/widgets/map.md
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" }
```
Loading