Created
March 13, 2017 03:26
-
-
Save janjiss/e29d77dd871313df0b3d2b4c891217cc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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