Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

insert Image via url #24

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,21 @@ const blockInitalValue2 = [
},
},
},
{
block: {
_id: '5',
type: 'image',
properties: {
document: [
{
text: '',
properties: [
'https://repository-images.githubusercontent.com/125159715/61f2ee00-865a-11e9-8ce5-f7028c561633',
],
},
],
},
},
},
];
export default App;
4 changes: 3 additions & 1 deletion src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { withLinks } from './Helpers/LinkHelper';
import Element from './plugins/Element';
import Leaf from './plugins/Leaf';
import withBlockID from './plugins/withBlockID';
import withImages from './plugins/withImages';
import withBreak from './plugins/withBreak';
import serialize from './serialize/index';
import deserialize from './deserialize/index';
import { ADD, UPDATE, DELETE } from './constant/operations';
Expand Down Expand Up @@ -40,7 +42,7 @@ const Editor: (props: Props) => any = ({
isHoveringToolBar = false,
}) => {
const [editorData, setData] = useState(EMPTY_NODE);
const withPlugins = [withReact, withHistory, withLinks, withBlockID] as const;
const withPlugins = [withReact, withHistory, withLinks, withBlockID, withImages, withBreak] as const;
const editor: any = useMemo(() => pipe(createEditor(), ...withPlugins), []);
const renderElement = useCallback((props) => <Element {...props} />, []);
const renderLeaf = useCallback((props) => <Leaf {...props} placeholderStyles={placeholderStyles} />, []);
Expand Down
33 changes: 33 additions & 0 deletions src/Helpers/ImageHelper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React, { FC, SVGProps } from 'react';
import { Transforms } from 'slate';
import { ReactEditor, useSlate } from 'slate-react';

import { Button, Icon } from './Helper';

export const insertImage = (editor: ReactEditor, url: string) => {
const text = { text: '' };
const image = { type: 'image', url, children: [text] };
Transforms.insertNodes(editor, image);
};

interface InsertImageButtonProps {
icon: FC<SVGProps<SVGSVGElement>>;
iconColor?: string;
}
export const InsertImageButton: (props: InsertImageButtonProps) => JSX.Element = ({ icon, iconColor = '#ffffff' }) => {
const editor = useSlate();
return (
<Button
onMouseDown={(event) => {
event.preventDefault();
const url = window.prompt('Enter the URL of the image:');
if (!url) return;
insertImage(editor, url);
}}
>
<Icon reversed active={false} svg={icon} color={iconColor}>
image
</Icon>
</Button>
);
};
Comment on lines +17 to +33
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insert Image via url logic and button

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for that you can change position of image by dragging and dropping to the upper nodes but it has internal bug of duplication

@Hamzaalam we have to resolve this issue, there should be some way to add some text to the bottom of the image, especially if the last node is an image node. Also, moving the image doesn't remove it.

The best way would be to add an empty node on either enter key or down key press.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fahad-Mahmood inserting the empty block at the bottom of image is working on Enter , just need to trace the cursor and only triggers this at the end of the line.

3 changes: 3 additions & 0 deletions src/ToolBar/ToolBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BlockButton } from '../Helpers/BlockHelper';
import { MarkButton } from '../Helpers/MarkHelper';
import { LinkButton, insertLink } from '../Helpers/LinkHelper';
import { Menu, Portal, LinkInput } from '../Helpers/Helper';
import { InsertImageButton } from '../Helpers/ImageHelper';

// icons
import { ReactComponent as Bold } from '../assets/bold.svg';
Expand All @@ -18,6 +19,7 @@ import { ReactComponent as Underline } from '../assets/underline.svg';
import { ReactComponent as H1 } from '../assets/h1.svg';
import { ReactComponent as H2 } from '../assets/h2.svg';
import { ReactComponent as CLEAR_FORMAT } from '../assets/clear_format.svg';
import { ReactComponent as Image } from '../assets/image.svg';

const ICON_COLOR = '#b5b9c6';
export const FixedMenu: any = React.forwardRef(({ ...props }, ref: React.Ref<HTMLDivElement>) => (
Expand Down Expand Up @@ -113,6 +115,7 @@ export const ToolBar: React.FC<ToolBarProps> = () => {
<MarkButton format="italic" icon={Italic} iconColor={ICON_COLOR} />
<MarkButton format="underlined" icon={Underline} iconColor={ICON_COLOR} />
<LinkButton showInput={LinkButtonClick} icon={Link} iconColor={ICON_COLOR} />
<InsertImageButton icon={Image} iconColor={ICON_COLOR} />
<BlockButton format="heading-one" icon={H1} iconColor={ICON_COLOR} />
<BlockButton format="heading-two" icon={H2} iconColor={ICON_COLOR} />
<BlockButton format="block-quote" icon={Quote} iconColor={ICON_COLOR} />
Expand Down
1 change: 1 addition & 0 deletions src/assets/image.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/deserialize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ const deserialization = (blockContentList: any) => {
const { block } = blockContent;
const children = getChildNodes(block);
const type = getNodeType(block.type);
if (type === 'image' && block.properties) {
return { id: block._id, type, children, url: block.properties.document[0].properties };
}

Comment on lines +59 to +62
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deserialization (DB to slate)

return { id: block._id, type, children };
});
return deserializedContent;
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/Element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Rectangle,
Heading1,
Heading2,
Image,
} from './ElementStyle';
import getShortLink from '../utils/getShortLink';

Expand Down Expand Up @@ -41,6 +42,15 @@ const Element: React.FC<RenderElementProps> = ({ attributes, children, element }
</LinkContainer>
</Link>
);
case `image`:
return (
<div {...attributes}>
<div contentEditable={false}>
<Image src={element.url} />
</div>
{children}
</div>
);
Comment on lines +45 to +53
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image element to render the image inside of slate editor

default:
return <p {...attributes}>{children}</p>;
}
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/ElementStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,9 @@ export const Heading1 = styled.h1`
export const Heading2 = styled.h2`
color: ${({ theme }) => (theme.colors !== undefined ? theme.colors.text : '#050b21')};
`;
export const Image = styled.img`
display: block;
max-width: 100%;
max-height: 20em;
padding: 4px 0px 0px 20px;
`;
Comment on lines +82 to +87
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image Styled component

18 changes: 18 additions & 0 deletions src/plugins/withBreak.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Transforms } from 'slate';

const withBreak = (editor: any) => {
const localEditor = editor;
localEditor.insertBreak = () => {
const newLine = {
type: 'paragraph',
children: [
{
text: '',
},
],
};
Transforms.insertNodes(editor, newLine);
};
return localEditor;
};
export default withBreak;
11 changes: 11 additions & 0 deletions src/plugins/withImages.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ReactEditor } from 'slate-react';

const withImages = (editor: ReactEditor) => {
const { isVoid } = editor;
const localEditor = editor;
localEditor.isVoid = (element) => {
return element.type === 'image' ? true : isVoid(element);
};
return localEditor;
};
export default withImages;
Comment on lines +3 to +11
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Image plugin to support he drag and drop of existing image

9 changes: 8 additions & 1 deletion src/serialize/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ const checkIdAndReturnBlock = (type: string, document: any, node: any) => {
const serializeParagraph = (paragraphNode: any, textType: string) => {
const document = paragraphNode.children.map((childNodes: any) => {
const text = getParagraphText(childNodes);
const properties = getParagraphProperties(childNodes);
let properties = [];
if (textType === 'image') {
properties.push(paragraphNode.url);
} else {
properties = getParagraphProperties(childNodes);
}
Comment on lines +41 to +46
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serialization logic (slate to DB structure)

return { text, properties };
});
const paragraphBlock = checkIdAndReturnBlock(textType, document, paragraphNode);
Expand All @@ -64,6 +69,8 @@ const serialize = (slateNodesList: any) => {
case 'heading-one':
case 'heading-two':
return serializeHeading(node, node.type);
case 'image':
return serializeParagraph(node, 'image');
default:
return serializeParagraph(node, 'text');
}
Expand Down