-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add 'scope' option for providing custom scope variables (#201)
- Loading branch information
1 parent
dd8d042
commit dd64d93
Showing
9 changed files
with
59 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
typeCode, | ||
assertFirstFrameContains, | ||
loadPlayroom, | ||
} from '../support/utils'; | ||
|
||
describe('useScope', () => { | ||
beforeEach(() => { | ||
loadPlayroom(); | ||
}); | ||
|
||
it('works', () => { | ||
typeCode('{{}hello()} {{}world()}', { delay: 0 }); | ||
assertFirstFrameContains('HELLO WORLD'); | ||
}); | ||
}); |
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,4 @@ | ||
export default () => ({ | ||
hello: () => 'HELLO', | ||
world: () => 'WORLD', | ||
}); |
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 @@ | ||
export default () => {}; |
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 |
---|---|---|
@@ -1,32 +1,13 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
import scopeEval from 'scope-eval'; | ||
|
||
export default class RenderCode extends Component { | ||
static displayName = 'RenderCode'; | ||
// eslint-disable-next-line import/no-unresolved | ||
import useScope from '__PLAYROOM_ALIAS__USE_SCOPE__'; | ||
|
||
static propTypes = { | ||
code: PropTypes.string.isRequired, | ||
scope: PropTypes.object, | ||
initialState: PropTypes.object, | ||
}; | ||
|
||
static defaultProps = { | ||
scope: {}, | ||
initialState: {}, | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
|
||
this.state = this.props.initialState; | ||
} | ||
|
||
render() { | ||
const { code, scope } = this.props; | ||
|
||
const el = scopeEval(code, { ...scope, React, this: this }); | ||
|
||
return el; | ||
} | ||
export default function RenderCode({ code, scope }) { | ||
return scopeEval(code, { | ||
...(useScope() ?? {}), | ||
...scope, | ||
React, | ||
}); | ||
} |
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,4 @@ | ||
/* eslint-disable-next-line import/no-unresolved */ | ||
const useScope = require('__PLAYROOM_ALIAS__USE_SCOPE__'); | ||
|
||
module.exports = useScope.default || useScope; |