Skip to content

Instantly share code, notes, and snippets.

@gustav1105
gustav1105 / type-def-draft-utils-plugins.ts
Created February 17, 2020 04:47
type-definitions-draft-utils-plugins
declare module "draft-js-plugins-utils" {
import { EditorState, DraftEntityType, EntityInstance } from "draft-js";
function createLinkAtSelection(
editorState: EditorState,
url: string
): EditorState;
function getCurrentEntityKey(editorState: EditorState): string | null;
@gustav1105
gustav1105 / visible-selected-rectangle.tsx
Created February 17, 2020 04:42
visible-selected-rectangle
React.useEffect(() => {
if (getVisibleSelectionRect(window) !== null) {
setSelectionRect(getVisibleSelectionRect(window));
}
}, [editorState, setSelectionRect]);
@gustav1105
gustav1105 / selected-rect.tsx
Created February 17, 2020 04:40
selected-rect
const [selectionRect, setSelectionRect] = React.useState<{
left: number;
width: number;
right: number;
top: number;
bottom: number;
height: number;
}>({ left: 0, width: 0, right: 0, top: 0, bottom: 0, height: 0 });
@gustav1105
gustav1105 / link-strategy.tsx
Created February 17, 2020 04:38
link-stragtegy
@gustav1105
gustav1105 / link-strategy.tsx
Created February 17, 2020 04:38
link-stragtegy
@gustav1105
gustav1105 / composite-deocrator.tsx
Created February 17, 2020 04:33
composite-decorator
import * as React from "react";
import { EditorContext } from "./EditorContext";
export function DecoratedLink({
className,
children,
entityKey,
target
}: {
className: string;
@gustav1105
gustav1105 / draft-utils-plugin.tsx
Created February 17, 2020 04:31
draft-utils-plugin
<TextField
variant="outlined"
InputProps={{
endAdornment: (
<InputAdornment position="start">
<IconButton
onClick={() => {
setEditorState(
draftUtils.createLinkAtSelection(editorState, anchorElUrl)
);
@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%"
}}
@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 / 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() {