with-context-root.js 1008 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { ContextRoot } from '../../js/shared/context/root-context'
  2. import _ from 'lodash'
  3. // Unfortunately, we cannot currently use decorators here, since we need to
  4. // set a value on window, before the contexts are rendered.
  5. // When using decorators, the contexts are rendered before the story, so we
  6. // don't have the opportunity to set the window value first.
  7. export function withContextRoot(Story, scope) {
  8. const scopeWatchers = []
  9. const ide = {
  10. ...window._ide,
  11. $scope: {
  12. ...window._ide.$scope,
  13. ...scope,
  14. $watch: (key, callback) => {
  15. scopeWatchers.push([key, callback])
  16. },
  17. $applyAsync: callback => {
  18. window.setTimeout(() => {
  19. callback()
  20. for (const [key, watcher] of scopeWatchers) {
  21. watcher(_.get(ide.$scope, key))
  22. }
  23. }, 0)
  24. },
  25. $on: (eventName, callback) => {
  26. //
  27. },
  28. },
  29. }
  30. return (
  31. <ContextRoot ide={ide} settings={{}}>
  32. {Story}
  33. </ContextRoot>
  34. )
  35. }