Skip to content

Commit

Permalink
feat: support multiple images paste/drop
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Jul 20, 2023
1 parent 6908698 commit 4fd2093
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 23 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"typecheck": "tsc --noEmit",
"lint": "eslint src --ext .ts,.tsx",
"test": "vitest",
"semantic-release": "semantic-release"
"semantic-release": "semantic-release",
"image-upload-backend": "node ./src/stories/file-backend.js"
},
"files": [
"dist"
Expand Down Expand Up @@ -105,8 +106,10 @@
"@ngneat/falso": "^6.4.0",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^14.0.0",
"@types/express": "^4.17.17",
"@types/js-yaml": "4.0.5",
"@types/mdast": "3.0.11",
"@types/multer": "^1.4.7",
"@types/node": "^20.2.5",
"@types/react": "^18.2.7",
"@types/react-dom": "^18.2.4",
Expand Down
78 changes: 78 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/stories/file-backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { extname } from 'path'
// Set up the storage configuration for Multer
const storage = diskStorage({
destination: './uploads',
filename: function (req, file, callback) {
filename: function (_req, file, callback) {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9)
const extension = extname(file.originalname)
callback(null, file.fieldname + '-' + uniqueSuffix + extension)
Expand Down
31 changes: 15 additions & 16 deletions src/system/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import {
REMOVE_LIST_COMMAND
} from '@lexical/list'
import { $isHeadingNode } from '@lexical/rich-text'
import { $findMatchingParent, $getNearestNodeOfType, $insertNodeToNearestRoot } from '@lexical/utils'
import { $findMatchingParent, $getNearestNodeOfType, $insertNodeToNearestRoot, $wrapNodeInElement } from '@lexical/utils'
import {
$createParagraphNode,
$getRoot,
$getSelection,
$insertNodes,
$isRangeSelection,
$isRootOrShadowRoot,
BLUR_COMMAND,
Expand Down Expand Up @@ -38,6 +40,7 @@ import { createEmptyHistoryState } from '@lexical/react/LexicalHistoryPlugin'
import { MarkdownParseOptions } from '../import'
import { ExportMarkdownFromLexicalOptions } from '../export/export'
import { importMarkdownToLexical } from '../import'
import { r } from 'vitest/dist/types-ad1c3f45'

type Teardowns = Array<() => void>

Expand Down Expand Up @@ -132,18 +135,11 @@ export const [EditorSystem, EditorSystemType] = system((r) => {
})

r.sub(r.pipe(insertImage, r.o.withLatestFrom(activeEditor)), ([src, theEditor]) => {
theEditor?.getEditorState().read(() => {
const selection = $getSelection()

if ($isRangeSelection(selection)) {
const focusNode = selection.focus.getNode()

if (focusNode !== null) {
theEditor.update(() => {
const imageNode = $createImageNode({ altText: '', src, title: '' })
$insertNodeToNearestRoot(imageNode)
})
}
theEditor?.update(() => {
const imageNode = $createImageNode({ altText: '', src, title: '' })
$insertNodes([imageNode])
if ($isRootOrShadowRoot(imageNode.getParentOrThrow())) {
$wrapNodeInElement(imageNode, $createParagraphNode).selectEnd()
}
})
})
Expand Down Expand Up @@ -325,10 +321,13 @@ export const [EditorSystem, EditorSystemType] = system((r) => {
} // If no image was present in the collection, bail.

const imageUploadHandlerValue = r.getValue(imageUploadHandler)
const theImage = cbPayload[0].getAsFile()!

imageUploadHandlerValue(theImage)
.then((url) => r.pub(insertImage, url))
Promise.all(cbPayload.map((file) => imageUploadHandlerValue(file.getAsFile()!)))
.then((urls) => {
urls.forEach((url) => {
r.pub(insertImage, url)
})
})
.catch((e) => {
throw e
})
Expand Down
12 changes: 7 additions & 5 deletions src/ui/ImagesPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,13 @@ function onDrop(event: DragEvent, editor: LexicalEditor, imageUploadHandler: (fi

if (cbPayload.length > 0) {
event.preventDefault()
imageUploadHandler(cbPayload[0].getAsFile()!)
.then((url) => {
editor.dispatchCommand(INSERT_IMAGE_COMMAND, {
src: url,
altText: ''
Promise.all(cbPayload.map((image) => imageUploadHandler(image.getAsFile()!)))
.then((urls) => {
urls.forEach((url) => {
editor.dispatchCommand(INSERT_IMAGE_COMMAND, {
src: url,
altText: ''
})
})
})
.catch((e) => {
Expand Down

0 comments on commit 4fd2093

Please sign in to comment.