Skip to content

Instantly share code, notes, and snippets.

@gustav1105
gustav1105 / draft-js-editor-dependencies.txt
Last active September 16, 2021 12:24
draft-js-editor-dependencies
"@material-ui/core": "4.9.2",
"@material-ui/icons": "4.9.1",
"@material-ui/lab": "4.0.0-alpha.42",
"@types/draft-js": "0.10.38",
"draft-js": "0.11.4",
"draft-js-plugins-utils": "2.0.3",
@gustav1105
gustav1105 / editor-state-context.tsx
Last active February 16, 2020 10:10
editor-state-context
import * as React from "react";
import { EditorState } from "draft-js";
export const EditorContext = React.createContext<EditorState>(
EditorState.createEmpty()
);
@gustav1105
gustav1105 / editor-component.tsx
Last active February 16, 2020 10:09
editor-component
<Box onClick={focusEditor} p={4}>
<EditorContext.Provider value={editorState}>
<Editor
editorState={editorState}
onChange={setEditorState}
placeholder="Click here to start typing in the editor..."
blockRendererFn={renderBlock}
ref={editor}
/>
</EditorContext.Provider>
@gustav1105
gustav1105 / editor-component-imports.tsx
Last active February 16, 2020 10:06
editor-component-imports
import * as React from "react";
import { EditorContext } from "./EditorContext";
import {
Box,
Paper,
TextField,
InputAdornment,
IconButton
} from "@material-ui/core";
import { ToggleButtonGroup, ToggleButton } from "@material-ui/lab";
@gustav1105
gustav1105 / editor-state-and-focus-functions.tsx
Created February 16, 2020 10:31
editor-state-and-focus-functions
const editor = React.useRef<Editor>(null);
const [editorState, setEditorState] = React.useState<EditorState>(
EditorState.createEmpty(
new CompositeDecorator([
{
strategy: linkStrategy,
component: DecoratedLink
}
])
@gustav1105
gustav1105 / toggle-button-group.tsx
Last active February 16, 2020 11:13
toggle-button-group
<Box mb={2}>
<ToggleButtonGroup
exclusive
onChange={handleToggleButtonGroup}
value={toggleButtonGroupValue}
>
<ToggleButton value="header-one">
<Title />
</ToggleButton>
<ToggleButton value="insert-image">
@gustav1105
gustav1105 / toggle-button-on-change.tsx
Last active February 16, 2020 11:19
toggle-button-on-change
const [toggleButtonGroupValue, setToggleButtonGroupValue] = React.useState<
string | null
>(null);
const handleToggleButtonGroup = (
event: React.MouseEvent<HTMLElement, MouseEvent>,
value: string
) => {
event.stopPropagation();
@gustav1105
gustav1105 / insert-image-to-editor.tsx
Created February 16, 2020 12:54
insert-image-to-editor
<input
id="fileInput"
style={{ display: "none" }}
type="file"
accept="image/png,image/jpeg,image/jpg,image/gif"
onChange={event => {
const reader = new FileReader();
reader.addEventListener(
"load",
function() {
@gustav1105
gustav1105 / render-block.tsx
Created February 16, 2020 16:38
render-block
const renderBlock = (contentBlock: ContentBlock) => {
if (contentBlock.getType() === "atomic") {
const entityKey = contentBlock.getEntityAt(0);
const entityData = editorState
.getCurrentContent()
.getEntity(entityKey)
.getData();
return {
component: MediaComponent,
editable: false,
@gustav1105
gustav1105 / media-component.tsx
Created February 16, 2020 16:41
media-component
import * as React from "react";
//eslint-disable-next-line
export const MediaComponent = ({ blockProps }: any) => {
const src = blockProps.src;
if (src.file) {
return (
<img
style={{
width: "100%"
}}