chore: fix file node selection issues

This commit is contained in:
Aman Harwara
2023-12-05 02:53:21 +05:30
parent 357e763c3e
commit 9265e7afe9
2 changed files with 19 additions and 8 deletions

View File

@@ -49,7 +49,7 @@ const ImagePreview: FunctionComponent<Props> = ({
const widthIfEmbedded = imageWidth * (imageZoomPercent / PercentageDivisor) const widthIfEmbedded = imageWidth * (imageZoomPercent / PercentageDivisor)
return ( return (
<div className="group flex h-full min-h-0 w-full items-center justify-center"> <div className="group relative flex h-full min-h-0 w-full items-center justify-center">
<div <div
className="relative flex h-full w-full items-center justify-center overflow-auto" className="relative flex h-full w-full items-center justify-center overflow-auto"
style={{ style={{
@@ -103,7 +103,7 @@ const ImagePreview: FunctionComponent<Props> = ({
<div className="mx-2"> <div className="mx-2">
<input <input
type="number" type="number"
className="w-10 text-center bg-default" className="w-10 bg-default text-center"
defaultValue={imageZoomPercent} defaultValue={imageZoomPercent}
onKeyDown={(event) => { onKeyDown={(event) => {
event.stopPropagation() event.stopPropagation()

View File

@@ -6,13 +6,12 @@ import { FileNode } from './Nodes/FileNode'
import { import {
$createParagraphNode, $createParagraphNode,
$insertNodes, $insertNodes,
$isRootOrShadowRoot,
COMMAND_PRIORITY_EDITOR, COMMAND_PRIORITY_EDITOR,
COMMAND_PRIORITY_NORMAL, COMMAND_PRIORITY_NORMAL,
PASTE_COMMAND, PASTE_COMMAND,
} from 'lexical' } from 'lexical'
import { $createFileNode } from './Nodes/FileUtils' import { $createFileNode } from './Nodes/FileUtils'
import { $wrapNodeInElement, mergeRegister } from '@lexical/utils' import { mergeRegister } from '@lexical/utils'
import { useFilesController } from '@/Controllers/FilesControllerProvider' import { useFilesController } from '@/Controllers/FilesControllerProvider'
import { FilesControllerEvent } from '@/Controllers/FilesController' import { FilesControllerEvent } from '@/Controllers/FilesController'
import { useLinkingController } from '@/Controllers/LinkingControllerProvider' import { useLinkingController } from '@/Controllers/LinkingControllerProvider'
@@ -53,11 +52,8 @@ export default function FilePlugin({ currentNote }: { currentNote: SNNote }): JS
(payload) => { (payload) => {
const fileNode = $createFileNode(payload) const fileNode = $createFileNode(payload)
$insertNodes([fileNode]) $insertNodes([fileNode])
if ($isRootOrShadowRoot(fileNode.getParentOrThrow())) {
$wrapNodeInElement(fileNode, $createParagraphNode).selectEnd()
}
const newLineNode = $createParagraphNode() const newLineNode = $createParagraphNode()
fileNode.getParentOrThrow().insertAfter(newLineNode) fileNode.insertAfter(newLineNode)
return true return true
}, },
@@ -75,6 +71,21 @@ export default function FilePlugin({ currentNote }: { currentNote: SNNote }): JS
}, },
COMMAND_PRIORITY_NORMAL, COMMAND_PRIORITY_NORMAL,
), ),
editor.registerNodeTransform(FileNode, (node) => {
/**
* Before this was added, we used to wrap the file node in a paragraph node,
* which caused issues with selection. We no longer do that, but for existing
* notes that have this, we use this transform to remove the wrapper node.
*/
const parent = node.getParent()
if (!parent) {
return
}
if (parent.getChildrenSize() === 1) {
parent.insertBefore(node)
parent.remove()
}
}),
) )
}, [application, currentNote.protected, editor, filesController, linkingController]) }, [application, currentNote.protected, editor, filesController, linkingController])