Created
July 13, 2017 16:38
-
-
Save ngryman/3829cb4d95614cb74533eafef69e3c62 to your computer and use it in GitHub Desktop.
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
const Editor = () => { | |
const update = (e, actions) => { | |
// strip html and get the text only | |
const source = e.target.innerText | |
// parse text into tokens stored in `state.tokens` | |
actions.parse(source) | |
} | |
// render tokens to vnodes | |
const render = (tokens) => ( | |
<div> | |
{tokens.map(token => { | |
switch (token.type) { | |
case 'if': return <span class="statement if">{token.val}</span> | |
default: return <span>{token.val}</span> | |
} | |
}) | |
</div> | |
) | |
// the actual view | |
return (state, actions) => ( | |
<editor contenteditable oninput={e => update(e, actions)}> | |
{renderTokens(state.tokens)} | |
</editor> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment