-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,906 additions
and
1,184 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
@@ -1 +1 @@ | ||
<?php return array('dependencies' => array('react', 'react-dom', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => '6d46a692118d073e9e1d'); | ||
<?php return array('dependencies' => array('react', 'react-dom', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-element', 'wp-i18n'), 'version' => 'b02df17e5e9500f32cdc'); |
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,14 @@ | ||
// noinspection NpmUsedModulesInstalled,JSUnresolvedVariable | ||
|
||
import {Fragment} from '@wordpress/element'; | ||
import ExportControl from "./ExportControl"; | ||
import ImportControl from "./ImportControl"; | ||
|
||
export default function DataControls({props}) { | ||
return ( | ||
<Fragment> | ||
<ExportControl props={props}/> | ||
<ImportControl props={props}/> | ||
</Fragment> | ||
); | ||
} |
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,35 @@ | ||
// noinspection NpmUsedModulesInstalled,JSUnresolvedVariable | ||
|
||
import {__} from '@wordpress/i18n'; | ||
import {BaseControl, Button} from '@wordpress/components'; | ||
|
||
export default function ExportControl({props}) { | ||
const { | ||
attributes: { | ||
markers, | ||
}, | ||
clientId, | ||
} = props; | ||
const hasMarkers = markers && 0 < markers.length; | ||
const formBlob = new Blob([JSON.stringify(markers)], {type: 'application/json'}); | ||
return ( | ||
<BaseControl | ||
label={__('Export', 'ootb-openstreetmap')} | ||
help={__('Download a JSON file with the locations of this map.', 'ootb-openstreetmap')} | ||
> | ||
<div> | ||
<Button | ||
icon="download" | ||
iconPosition="left" | ||
variant={hasMarkers ? 'primary' : 'secondary'} | ||
href={window.URL.createObjectURL(formBlob)} | ||
target="_blank" | ||
download={'ootb_' + clientId} | ||
disabled={!hasMarkers} | ||
showTooltip={true} | ||
text={__('Export locations', 'ootb-openstreetmap')} | ||
/> | ||
</div> | ||
</BaseControl> | ||
); | ||
} |
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,64 @@ | ||
// noinspection NpmUsedModulesInstalled,JSUnresolvedVariable | ||
|
||
import {__} from '@wordpress/i18n'; | ||
import validMarkers from "../Helpers/validMarkers"; | ||
import {BaseControl, FormFileUpload} from '@wordpress/components'; | ||
|
||
export default function ImportControl({props}) { | ||
const { | ||
attributes: { | ||
markers, | ||
}, | ||
setAttributes, | ||
} = props; | ||
const hasMarkers = markers && 0 < markers.length; | ||
|
||
const importMarkers = (event) => { | ||
const file = event.currentTarget.files[0]; | ||
if (!file || 0 === file.length) { | ||
return; | ||
} | ||
const reader = new FileReader(); | ||
reader.readAsText(file, "UTF-8"); | ||
reader.onload = function (fileObj) { | ||
const data = validMarkers(fileObj.target.result); | ||
if (!data) { | ||
return; | ||
} | ||
setAttributes({ | ||
markers: markers.concat(data), | ||
shouldUpdateBounds: true, | ||
}); | ||
} | ||
//noinspection JSUnusedLocalSymbols | ||
reader.onerror = function (fileObj) { | ||
console.log(__('Error reading file', 'ootb-openstreetmap')); | ||
} | ||
}; | ||
|
||
return ( | ||
<BaseControl | ||
label={__('Import', 'ootb-openstreetmap')} | ||
help={__('Import locations from a previously exported JSON file.', 'ootb-openstreetmap')} | ||
> | ||
<FormFileUpload | ||
icon="database-add" | ||
iconPosition="left" | ||
variant="primary" | ||
isDestructive={hasMarkers} | ||
label={ | ||
hasMarkers ? | ||
__('This map already includes locations. Uploading new locations will merge them with the existing ones.', 'ootb-openstreetmap') | ||
: null | ||
} | ||
text={__('Import locations', 'ootb-openstreetmap')} | ||
accept="application/json" | ||
onChange={ | ||
(event) => { | ||
importMarkers(event) | ||
} | ||
} | ||
/> | ||
</BaseControl> | ||
); | ||
} |
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,29 @@ | ||
import validateCoordinates from "./validateCoordinates"; | ||
|
||
export default function validMarkers(data) { | ||
try { | ||
const jsonData = JSON.parse(data.toString()); | ||
if (jsonData && typeof jsonData === 'object') { | ||
let clean = []; | ||
for (const location of jsonData) { | ||
// Skip if no lat or lng exist. | ||
if (!location.lat || !location.lng) { | ||
continue; | ||
} | ||
// Skip if either the latitude or the longitude is invalid. | ||
if (!validateCoordinates(location.lat, location.lng)) { | ||
continue; | ||
} | ||
// If no ID exists, create one. | ||
location.id = location.id ?? Date.now(); | ||
// Make sure that we don't import unnecessary keys. | ||
const {lat, lng, text, id} = location; | ||
clean.push({lat, lng, text, id}); | ||
} | ||
return clean; | ||
} | ||
} catch (e) { | ||
return false; | ||
} | ||
return false; | ||
} |
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,20 @@ | ||
/** | ||
* Perform basic validation on the coordinates. | ||
* @param lat | ||
* @param lng | ||
* @returns {*} | ||
*/ | ||
export default function validateCoordinates(lat = '', lng = '') { | ||
if (!lat || !lng) { | ||
return false; | ||
} | ||
return isValidLatitude(parseFloat(lat)) && isValidLongitude(parseFloat(lng)); | ||
} | ||
|
||
export function isValidLatitude(lat) { | ||
return isFinite(lat) && Math.abs(lat) <= 90; | ||
} | ||
|
||
export function isValidLongitude(lng) { | ||
return isFinite(lng) && Math.abs(lng) <= 180; | ||
} |