Skip to content

Instantly share code, notes, and snippets.

View Katerina198b's full-sized avatar

Ekaterina Zakharenkova Katerina198b

  • Warsaw
View GitHub Profile
// ES6
class Cat(name) {
constructor({name}) {
this.name = name;
this.isFat = false;
}
sayMeow() {
console.log('meow');
}
}
function Cat(name: string) {
this.name = name;
this.isFat = false;
this.sayMeow = function() {
console.log('meow');
}
}
const cat = new Cat('Snowball');
//input
console.log(getCurrentBlock(newEditorState).toObject());
//output
{
"key":"fe4s0",
"text":"header h1",
"type":"header-one",
"depth":0,
"inlineStyleRanges":[],
"entityRanges":[],
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});
}
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];
}
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) {
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];
@Katerina198b
Katerina198b / simple-draft.js
Last active December 21, 2021 10:01
Simple draft js and hooks example
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}
/>