2021-06-16 05:32:38 -04:00
|
|
|
import { ContextRoot } from '../../js/shared/context/root-context'
|
2021-09-30 07:29:25 -04:00
|
|
|
import _ from 'lodash'
|
2021-06-16 05:32:38 -04:00
|
|
|
|
|
|
|
// Unfortunately, we cannot currently use decorators here, since we need to
|
|
|
|
// set a value on window, before the contexts are rendered.
|
|
|
|
// When using decorators, the contexts are rendered before the story, so we
|
|
|
|
// don't have the opportunity to set the window value first.
|
|
|
|
export function withContextRoot(Story, scope) {
|
2021-09-30 07:29:25 -04:00
|
|
|
const scopeWatchers = []
|
|
|
|
|
2021-06-16 05:32:38 -04:00
|
|
|
const ide = {
|
|
|
|
...window._ide,
|
|
|
|
$scope: {
|
|
|
|
...window._ide.$scope,
|
|
|
|
...scope,
|
2021-09-30 07:29:25 -04:00
|
|
|
$watch: (key, callback) => {
|
|
|
|
scopeWatchers.push([key, callback])
|
|
|
|
},
|
|
|
|
$applyAsync: callback => {
|
|
|
|
window.setTimeout(() => {
|
|
|
|
callback()
|
|
|
|
for (const [key, watcher] of scopeWatchers) {
|
|
|
|
watcher(_.get(ide.$scope, key))
|
|
|
|
}
|
|
|
|
}, 0)
|
|
|
|
},
|
2021-10-21 06:31:51 -04:00
|
|
|
$on: (eventName, callback) => {
|
|
|
|
//
|
|
|
|
},
|
2021-06-16 05:32:38 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ContextRoot ide={ide} settings={{}}>
|
|
|
|
{Story}
|
|
|
|
</ContextRoot>
|
|
|
|
)
|
|
|
|
}
|