Skip to content

Instantly share code, notes, and snippets.

@janjiss
Created March 13, 2017 03:26
Show Gist options
  • Save janjiss/e29d77dd871313df0b3d2b4c891217cc to your computer and use it in GitHub Desktop.
Save janjiss/e29d77dd871313df0b3d2b4c891217cc to your computer and use it in GitHub Desktop.
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { Raw, Editor } from 'slate'
const initialState = {
nodes: [
{
kind: "block",
type: "paragraph",
nodes: [
{
kind: "text",
text: "Example text that we want to linkify"
}
]
}
]
}
const schema = {
nodes: {
'link': (props) => { return <a>{props.children}</a> },
}
}
class EscritorioEditor extends Component {
constructor(props) {
super(props)
const editorState = Raw.deserialize(initialState, { terse: true })
this.state = { editorState }
this.onChange = (state) => this._onChange(state)
this.wrapLink = (state) => this._wrapLink(state)
}
// On change, update the app's React state with the new editor state.
_onChange(editorState) {
this.setState({ editorState })
}
_wrapLink(e) {
this.onChange(this.state.editorState.transform().wrapInline('link').moveForward(3).focus().apply())
}
render() {
return (
<div>
<button onClick={this.wrapLink}>Wrap Link!</button>
<div className="editable">
<Editor
schema={schema}
state={this.state.editorState}
onChange={this.onChange}
>
</Editor>
</div>
</div>
)
}
}
const editorElement = document.getElementById('editor')
ReactDOM.render(
<EscritorioEditor />,
editorElement
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment