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

Refactor Rapid integration to load the editor in an iframe #2403

Merged
merged 10 commits into from
Sep 9, 2024
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,3 @@ yarn-error.log*

#vscode
/.vscode

#static public files
/public/static
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"@nivo/line": "^0.79.1",
"@nivo/radar": "^0.79.1",
"@popperjs/core": "^2.8.4",
"@rapideditor/rapid": "^2.1.1",
"@react-leaflet/core": "^2.1.0",
"@rjsf/core": "^4.2.3",
"@turf/bbox": "^6.0.1",
Expand Down Expand Up @@ -137,11 +136,9 @@
"update-layers": "node scripts/update_layers.js",
"update-layers-prod": "NODE_ENV=production node scripts/update_layers.js",
"update-presets": "node scripts/update_presets.js",
"copy-static": "bash -c \"mkdir -p public/static/id; mkdir -p public/static/rapid; if ! (test -a public/static/rapid/rapid.js); then cp -R node_modules/@rapideditor/rapid/dist/* public/static/rapid; fi\"",
"update-static": "bash -c \"mkdir -p public/static/id; mkdir -p public/static/rapid; cp -R node_modules/@rapideditor/rapid/dist/* public/static/rapid;\"",
"start-js": "react-scripts --max_old_space_size=4096 start",
"start": "npm-run-all -p copy-static update-layers watch-postcss start-js",
"build": "yarn update-static && yarn run build-intl && yarn run update-layers-prod && yarn run build-postcss && react-scripts --max_old_space_size=4096 build",
"start": "npm-run-all -p update-layers watch-postcss start-js",
"build": "yarn run build-intl && yarn run update-layers-prod && yarn run build-postcss && react-scripts --max_old_space_size=4096 build",
"test": "react-scripts test --transformIgnorePatterns \"node_modules/(?!react-leaflet)/\" --resetMocks=false --detectOpenHandles",
"test:cov": "react-scripts test --transformIgnorePatterns \"node_modules/(?!react-leaflet)/\" --coverage --resetMocks=false",
"test:cov-full": "react-scripts test --transformIgnorePatterns \"node_modules/(?!react-leaflet)/\" --coverage --watchAll --resetMocks=false",
Expand Down
73 changes: 73 additions & 0 deletions public/static/rapid-editor.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<!-- Adapted from Rapid's code examples at https://github.com/facebook/Rapid/tree/main/dist/examples -->
<title>Rapid Editor</title>
<script type='text/javascript' src='https://cdn.jsdelivr.net/npm/@rapideditor/rapid@2/dist/rapid.min.js'></script>
<link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/@rapideditor/rapid@2/dist/rapid.min.css'>

<meta name='viewport' content='width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no'/>
<meta name='mobile-web-app-capable' content='yes'/>
<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent'/>
<style type='text/css'>
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
/* disable elastic page bounce upon scroll */
overflow: hidden;
}
</style>
</head>

<body>
<div id='rapid-container'></div>

<script>
function setupRapid(container) {
if (typeof Rapid === 'undefined' || !Rapid.utilDetect().support) {
container.innerHTML = 'Sorry, the Rapid editor does not support your web browser.';
container.style.padding = '20px';
return;
}

// Get the token passed in from the parent window. May be null in dev mode.
const params = new URLSearchParams(document.location.hash.slice(1));
const token = params.get('token');
params.delete('token');
document.location.hash = params.toString();

// Create and configure the main editor Context
const context = new Rapid.Context();
context.containerNode = container;
context.assetPath = 'https://cdn.jsdelivr.net/npm/@rapideditor/rapid@2/dist/';

if (token) {
context.preauth = {
// TODO: use REACT_APP_OSM_SERVER and REACT_APP_OSM_API_SERVER here?
// Although Rapid also hardcodes these URLs internally. See:
// https://github.com/facebook/Rapid/issues/1341
// ...probably best to match those for now (weird bugs will occur otherwise)
url: 'https://www.openstreetmap.org',
apiUrl: 'https://api.openstreetmap.org',
access_token: token,
};
context.apiConnections = [context.preauth];
} else {
console.warn(
"MapRoulette has initialized the Rapid iframe without an OSM API access token; "
+ "this should only happen in dev mode."
);
}

window.rapidContext = context; // allow parent window to access context
context.initAsync();
}

const container = document.getElementById('rapid-container');
setupRapid(container);
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion src/components/HOCs/WithEditor/WithEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const mapStateToProps = state => {
return ({
editor: state.openEditor,
configuredEditor: _get(userEntity, 'settings.defaultEditor', DEFAULT_EDITOR),
rapidContext: _get(state, 'rapidEditor.context'),
rapidEditorState: _get(state, 'rapidEditor'),
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ exports[`mapStateToProps provides an editor prop with the current open editor 1`
Object {
"configuredEditor": 0,
"editor": 1,
"rapidContext": undefined,
"rapidEditorState": undefined,
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,10 @@ export class ActiveTaskControls extends Component {
}

initiateCompletion = (taskStatus, submitRevision) => {
const hasUnsavedRapidChanges = this.props.rapidContext?.systems.editor.hasChanges()
const intl = this.props.intl
const message = intl.formatMessage(messages.rapidDiscardUnsavedChanges)

if (!hasUnsavedRapidChanges || window.confirm(message)) {
if (!this.props.rapidEditorState.hasUnsavedChanges || window.confirm(message)) {
this.setState({
confirmingTask: this.props.task,
osmComment: `${this.props.task.parent.checkinComment}${constructChangesetUrl(this.props.task)}`,
Expand Down
Loading