forked from openstreetmap/iD
-
Notifications
You must be signed in to change notification settings - Fork 0
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
4 changed files
with
102 additions
and
0 deletions.
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,63 @@ | ||
import { dispatch as d3_dispatch } from 'd3-dispatch'; | ||
import { select as d3_select } from 'd3-selection'; | ||
import { utilRebind } from '../../util'; | ||
|
||
export function uiFieldIframe(field, context) { | ||
const id = Math.round(Math.random() * 1e15); | ||
const dispatch = d3_dispatch('change'); | ||
let wrap = d3_select(null); | ||
let _tags; | ||
|
||
const frameOrigin = new URL(field.url).origin; | ||
const frameUrl = new URL(field.url); | ||
frameUrl.hash = window.location.origin; | ||
|
||
/** @returns {HTMLIFrameElement=} */ | ||
const getIframe = () => document.querySelector(`.iframe-${field.safeid}`); | ||
|
||
function sendToIframe() { | ||
getIframe().contentWindow.postMessage({ | ||
tags: _tags, | ||
locale: context.locale(), | ||
}, frameOrigin); | ||
} | ||
|
||
/** @param {MessageEvent<unknown>} event */ | ||
function onMessage(event) { | ||
if (+getIframe()?.dataset.instanceId !== id) { | ||
console.log(`unsubsribe from ${id}`); | ||
// this component has been unmounted, so unsubscribe | ||
window.removeEventListener('message', onMessage); | ||
return; | ||
} | ||
|
||
if (event.origin === frameOrigin) { | ||
console.log('got', event.data); | ||
if (typeof event.data === 'object' && typeof event.data.tags === 'object') { | ||
dispatch.call('change', this, () => event.data.tags); | ||
} else if (event.data === 'hydrate') { | ||
sendToIframe(); | ||
} | ||
} | ||
}; | ||
window.addEventListener('message', onMessage); | ||
|
||
function iframe(selection) { | ||
wrap = selection.selectAll('.form-field-input-wrap') | ||
.data([0]); | ||
|
||
wrap = wrap.enter() | ||
.append('iframe') | ||
.attr('class', `form-field-input-wrap iframe-${field.safeid}`) | ||
.attr('data-instance-id', id) | ||
.attr('src', frameUrl.toString()) | ||
.merge(wrap); | ||
} | ||
|
||
iframe.tags = (tags) => { | ||
_tags = tags; | ||
sendToIframe(); | ||
}; | ||
|
||
return utilRebind(iframe, dispatch, 'on'); | ||
} |
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