Skip to content

Commit

Permalink
fix: image editors should be rendered inline
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Jul 8, 2023
1 parent a658faa commit 09fd9dc
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 12 deletions.
13 changes: 4 additions & 9 deletions src/export/visitors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,10 @@ const LexicalImageVisitor: LexicalExportVisitor<ImageNode, Mdast.Image> = {
testLexicalNode: $isImageNode,
visitLexicalNode({ mdastParent, lexicalNode, actions }) {
actions.appendToParent(mdastParent, {
type: 'paragraph',
children: [
{
type: 'image',
url: lexicalNode.getSrc(),
alt: lexicalNode.getAltText(),
title: lexicalNode.getTitle()
}
]
type: 'image',
url: lexicalNode.getSrc(),
alt: lexicalNode.getAltText(),
title: lexicalNode.getTitle()
})
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/stories/assets/image-markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Hello World
---

# Block Image

![image](https://picsum.photos/200/300)

# Two inline images

![image](https://picsum.photos/200/300) ![image](https://picsum.photos/200/300)
11 changes: 9 additions & 2 deletions src/stories/boilerplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,24 @@ interface WrappedEditorProps {
onChange?: (markdown: string) => void
viewMode?: ViewMode
className?: string
imageUploadHandler?: (image: File) => Promise<string>
}

async function imageUploadHandler(image: File) {
async function expressImageUploadHandler(image: File) {
const formData = new FormData()
formData.append('image', image)
const response = await fetch('/uploads/new', { method: 'POST', body: formData })
const json = (await response.json()) as { url: string }
return json.url
}

export const WrappedLexicalEditor: React.FC<WrappedEditorProps> = ({ viewMode, markdown, onChange, className }) => {
export const WrappedLexicalEditor: React.FC<WrappedEditorProps> = ({
viewMode,
markdown,
onChange,
className,
imageUploadHandler = expressImageUploadHandler
}) => {
return (
<MDXEditor
markdown={markdown}
Expand Down
8 changes: 8 additions & 0 deletions src/stories/images.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react'
import { WrappedLexicalEditor } from './boilerplate'

import imageMarkdown from './assets/image-markdown.md?raw'
export function Hello() {
// eslint-disable-next-line @typescript-eslint/require-await
return <WrappedLexicalEditor markdown={imageMarkdown} imageUploadHandler={async () => 'https://picsum.photos/200/300'} />
}
2 changes: 1 addition & 1 deletion src/ui/NodeDecorators/ImageEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export function ImageEditor({ src, title, nodeKey }: ImageEditorProps): JSX.Elem
return (
<Suspense fallback={null}>
<>
<div draggable={draggable} className={styles.imageWrapper}>
<div draggable={draggable} className={styles.imageWrapper} data-editor-block-type="image">
<LazyImage
className={classNames({
[styles.focusedImage]: isFocused
Expand Down
4 changes: 4 additions & 0 deletions src/ui/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,10 @@
outline: highlight solid 2px;
}

.imageWrapper {
display: inline;
}

.imageWrapper[draggable=true] {
cursor: move; /* fallback if grab cursor is unsupported */
cursor: grab;
Expand Down

0 comments on commit 09fd9dc

Please sign in to comment.