-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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> | ||
); | ||
}; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. deserialization (DB to slate) |
||
return { id: block._id, type, children }; | ||
}); | ||
return deserializedContent; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import { | |
Rectangle, | ||
Heading1, | ||
Heading2, | ||
Image, | ||
} from './ElementStyle'; | ||
import getShortLink from '../utils/getShortLink'; | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. image Styled component |
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; |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Image plugin to support he drag and drop of existing image |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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'); | ||
} | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
There was a problem hiding this comment.
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.