Skip to content

Instantly share code, notes, and snippets.

@ngryman
Created July 13, 2017 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ngryman/3829cb4d95614cb74533eafef69e3c62 to your computer and use it in GitHub Desktop.
Save ngryman/3829cb4d95614cb74533eafef69e3c62 to your computer and use it in GitHub Desktop.
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