Skip to content

Instantly share code, notes, and snippets.

@aaronshaf
Created December 15, 2016 05:48
Show Gist options
  • Save aaronshaf/e18248217aace0670eafe4d8492044c2 to your computer and use it in GitHub Desktop.
Save aaronshaf/e18248217aace0670eafe4d8492044c2 to your computer and use it in GitHub Desktop.
why no links from imported state?
import React from 'react'
import ReactDOM from 'react-dom'
import { Editor, EditorState, RichUtils } from 'draft-js'
import { stateToMarkdown } from 'draft-js-export-markdown'
import { stateFromHTML } from 'draft-js-import-html'
class MyEditor extends React.Component {
constructor (props) {
super(props)
this.state = {
editorState: EditorState.createWithContent(props.contentState)
}
this.focus = () => this.refs.editor.focus()
this.onChange = (editorState) => {
this.setState({
editorState
})
}
this.logState = () => {
const a = this.state.editorState.getCurrentContent()
console.log(stateToMarkdown(a))
}
this.handleKeyCommand = this.handleKeyCommand.bind(this)
}
handleKeyCommand (command) {
console.debug(command)
const newState = RichUtils.handleKeyCommand(this.state.editorState, command)
if (newState) {
this.onChange(newState)
return 'handled'
}
return 'not-handled'
}
render () {
return (
<div>
<Editor
editorState={this.state.editorState}
handleKeyCommand={this.handleKeyCommand}
onChange={this.onChange}
placeholder='Edit article'
/>
</div>
)
}
}
document.getElementById('edit-button').addEventListener('click', () => {
document.getElementById('article-content').style.display = 'none'
const contentState = stateFromHTML('foo <a href="https://www.domain.com/bar">bar</a> baz')
console.debug({contentState: contentState.toJS()})
ReactDOM.render(
<MyEditor contentState={contentState} />,
document.getElementById('article-editor')
)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment