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
// ES6 | |
class Cat(name) { | |
constructor({name}) { | |
this.name = name; | |
this.isFat = false; | |
} | |
sayMeow() { | |
console.log('meow'); | |
} | |
} |
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
function Cat(name: string) { | |
this.name = name; | |
this.isFat = false; | |
this.sayMeow = function() { | |
console.log('meow'); | |
} | |
} | |
const cat = new Cat('Snowball'); |
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
//input | |
console.log(getCurrentBlock(newEditorState).toObject()); | |
//output | |
{ | |
"key":"fe4s0", | |
"text":"header h1", | |
"type":"header-one", | |
"depth":0, | |
"inlineStyleRanges":[], | |
"entityRanges":[], |
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
class RichEditorExample extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {editorState: EditorState.createEmpty()}; | |
this.focus = () => this.refs.editor.focus(); | |
} | |
onChange = (editorState) => { | |
this.setState({editorState}); | |
} |
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
function findTextPositionByOffsetKey(offsetKey, editorState) { | |
const keys = offsetKey.split('-'); | |
const key = {blockKey: keys[0], entityKey: keys[1], leafKey: keys[2]}; | |
const leaf = editorState.getBlockTree(key.blockKey).getIn([key.decoratorKey, 'leaves', key.leafKey]); | |
const {startTextPosition, endTextPosition} = leaf; | |
return [startTextPosition, endTextPosition]; | |
} |
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, {useState} from 'react'; | |
import {Editor, EditorState} from 'draft-js'; | |
function getCurrentBlock(editorState) { | |
const currentSelection = editorState.getSelection(); | |
const blockKey = currentSelection.getStartKey(); | |
return(editorState.getCurrentContent().getBlockForKey(blockKey)); | |
} | |
function getCurrentLetter(editorState) { |
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
function getCurrentBlock(editorState) { | |
const currentSelection = editorState.getSelection(); | |
const blockKey = currentSelection.getStartKey(); | |
return(editorState.getCurrentContent().getBlockForKey(blockKey)); | |
} | |
function getCurrentLetter(editorState) { | |
const currentBlock = getCurrentBlock(editorState); | |
const blockText = currentBlock.getText(); | |
return blockText[editorState.getSelection().getStartOffset() - 1]; |
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, {useState} from 'react'; | |
import {Editor, EditorState} from 'draft-js'; | |
const SimpleEditorExample = () => { | |
const [editorState, setEditorState] = useState(EditorState.createEmpty()); | |
return ( | |
<Editor | |
editorState={editorState} | |
onChange={setEditorState} | |
/> |
NewerOlder