-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Making playground examples clickable
- Loading branch information
1 parent
2987664
commit 7491023
Showing
6 changed files
with
134 additions
and
49 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,78 @@ | ||
import React from 'react'; | ||
import ValueLoader from '../value-loader'; | ||
|
||
export default class Playground extends React.Component { | ||
|
||
constructor(defaultValue) { | ||
super(); | ||
|
||
this.valueLoader = new ValueLoader({ | ||
loader: this.loadValue.bind(this), | ||
idleTime: 100, | ||
maxTime: 1000 | ||
}); | ||
|
||
this.valueLoader.on('data', this.onData.bind(this)); | ||
|
||
this.state = { | ||
value: defaultValue || '', | ||
data: null | ||
}; | ||
this.updateValue = this.updateValue.bind(this); | ||
|
||
this.valueLoader.value = this.state.value; | ||
} | ||
|
||
render() { | ||
return <div className="playground"> | ||
<div className="playground__editor"> | ||
<input type="text" value={ this.state.value } onChange={ this.updateValue } /> | ||
</div> | ||
|
||
{ this.renderData(this.state.data) } | ||
</div> | ||
} | ||
|
||
updateValue(e) { | ||
const value = e.target.value; | ||
this.setState({ value: value }); | ||
this.valueLoader.value = value; | ||
} | ||
|
||
loadValue(v) { | ||
} | ||
|
||
onData(v) { | ||
this.setState({ data: v }); | ||
} | ||
|
||
renderData() { | ||
|
||
} | ||
|
||
componentDidMount() { | ||
this.listener = e => { | ||
console.log(e.target.name); | ||
if(e.target.tagName === 'CODE' && withinExamples(e.target)) { | ||
const value = e.target.textContent; | ||
this.setState({ value: value }); | ||
this.valueLoader.value = value; | ||
} | ||
}; | ||
document.addEventListener('click', this.listener); | ||
} | ||
|
||
componentWillUnmount() { | ||
document.removeEventListener('click', this.listener); | ||
} | ||
} | ||
|
||
function withinExamples(el) { | ||
while(el) { | ||
if(el.classList.contains('playground-examples')) return true; | ||
|
||
el = el.parentNode; | ||
} | ||
|
||
return false; | ||
} |
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,72 +1,33 @@ | ||
import React from 'react'; | ||
import ValueLoader from '../value-loader'; | ||
import BasePlayground from './base'; | ||
|
||
const en = require('ecolect/language/en'); | ||
import en from 'ecolect/language/en'; | ||
|
||
export default class ValuePlayground extends React.Component { | ||
export default class ValuePlayground extends BasePlayground { | ||
|
||
constructor(value, defaultValue) { | ||
super(); | ||
|
||
console.log(en) | ||
super(defaultValue); | ||
|
||
this.value = value; | ||
this.valueMatcher = value.matcher(en.default || en); | ||
this.valueLoader = new ValueLoader({ | ||
loader: this.loadValue.bind(this), | ||
idleTime: 100, | ||
maxTime: 1000 | ||
}); | ||
|
||
this.valueLoader.on('data', this.onData.bind(this)); | ||
|
||
this.state = { | ||
value: defaultValue || '', | ||
data: null | ||
}; | ||
this.updateValue = this.updateValue.bind(this); | ||
|
||
this.valueLoader.value = this.state.value; | ||
this.valueMatcher = value.matcher(en); | ||
} | ||
|
||
render() { | ||
return <div className="playground"> | ||
<div className="playground__editor"> | ||
<input type="text" value={ this.state.value } onChange={ this.updateValue } /> | ||
</div> | ||
|
||
<div className="playground__data">{ this.renderValue(this.state.data) }</div> | ||
renderData() { | ||
return <div className="playground__data"> | ||
<div className="playground__data__value">{ this.renderValue(this.state.data) }</div> | ||
|
||
<div className="playground__js"> | ||
<div className="playground__data__js"> | ||
JavaScript value: | ||
<pre>{ JSON.stringify(this.state.data, null, 2) }</pre> | ||
</div> | ||
</div> | ||
} | ||
|
||
updateValue(e) { | ||
const value = e.target.value; | ||
this.setState({ value: value }); | ||
this.valueLoader.value = value; | ||
} | ||
|
||
loadValue(v) { | ||
return this.valueMatcher(v); | ||
} | ||
|
||
onData(v) { | ||
this.setState({ data: v }); | ||
} | ||
|
||
renderValue() { | ||
return <div></div>; | ||
} | ||
|
||
renderExamples() { | ||
const examples = this.getExamples(this.language.id) || []; | ||
|
||
return <ul> | ||
{ examples.map(e => <li>{ e }</li>) } | ||
</ul>; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -19,6 +19,10 @@ | |
|
||
{% macro examples(lang) %} | ||
|
||
<div class="playground-examples"> | ||
|
||
{{ caller() }} | ||
|
||
</div> | ||
|
||
{% endmacro %} |