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

feat: Paste files in Super notes to upload and embed them #2060

Merged
merged 6 commits into from
Nov 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@ import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext

import { useEffect } from 'react'
import { FileNode } from './Nodes/FileNode'
import { $createParagraphNode, $insertNodes, $isRootOrShadowRoot, COMMAND_PRIORITY_EDITOR } from 'lexical'
import {
$createParagraphNode,
$insertNodes,
$isRootOrShadowRoot,
COMMAND_PRIORITY_EDITOR,
COMMAND_PRIORITY_NORMAL,
PASTE_COMMAND,
} from 'lexical'
import { $createFileNode } from './Nodes/FileUtils'
import { $wrapNodeInElement } from '@lexical/utils'
import { $wrapNodeInElement, mergeRegister } from '@lexical/utils'
import { useFilesController } from '@/Controllers/FilesControllerProvider'
import { FilesControllerEvent } from '@/Controllers/FilesController'

Expand All @@ -18,22 +25,51 @@ export default function FilePlugin(): JSX.Element | null {
throw new Error('FilePlugin: FileNode not registered on editor')
}

return editor.registerCommand<string>(
INSERT_FILE_COMMAND,
(payload) => {
const fileNode = $createFileNode(payload)
$insertNodes([fileNode])
if ($isRootOrShadowRoot(fileNode.getParentOrThrow())) {
$wrapNodeInElement(fileNode, $createParagraphNode).selectEnd()
const uploadFilesList = (files: FileList) => {
Array.from(files).forEach(async (file) => {
try {
const uploadedFiles = await filesController.uploadNewFile(file)
if (uploadedFiles) {
uploadedFiles.forEach((uploadedFile) => {
editor.dispatchCommand(INSERT_FILE_COMMAND, uploadedFile.uuid)
})
}
} catch (error) {
console.error(error)
}
const newLineNode = $createParagraphNode()
$insertNodes([newLineNode])
})
}

return mergeRegister(
editor.registerCommand<string>(
INSERT_FILE_COMMAND,
(payload) => {
const fileNode = $createFileNode(payload)
$insertNodes([fileNode])
if ($isRootOrShadowRoot(fileNode.getParentOrThrow())) {
$wrapNodeInElement(fileNode, $createParagraphNode).selectEnd()
}
const newLineNode = $createParagraphNode()
$insertNodes([newLineNode])

return true
},
COMMAND_PRIORITY_EDITOR,
return true
},
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
PASTE_COMMAND,
(payload) => {
const files = payload instanceof InputEvent ? payload.dataTransfer?.files : null
if (files?.length) {
uploadFilesList(files)
return true
}
return false
},
COMMAND_PRIORITY_NORMAL,
),
)
}, [editor])
}, [editor, filesController])

useEffect(() => {
const disposer = filesController.addEventObserver((event, data) => {
Expand Down