feat: file drop handling for super notes (#1990)
This commit is contained in:
@@ -29,6 +29,7 @@ import AndroidBackHandlerProvider from '@/NativeMobileWeb/useAndroidBackHandler'
|
|||||||
import ConfirmDeleteAccountContainer from '@/Components/ConfirmDeleteAccountModal/ConfirmDeleteAccountModal'
|
import ConfirmDeleteAccountContainer from '@/Components/ConfirmDeleteAccountModal/ConfirmDeleteAccountModal'
|
||||||
import DarkModeHandler from '../DarkModeHandler/DarkModeHandler'
|
import DarkModeHandler from '../DarkModeHandler/DarkModeHandler'
|
||||||
import ApplicationProvider from './ApplicationProvider'
|
import ApplicationProvider from './ApplicationProvider'
|
||||||
|
import { ErrorBoundary } from '@/Utils/ErrorBoundary'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
application: WebApplication
|
application: WebApplication
|
||||||
@@ -219,7 +220,9 @@ const ApplicationView: FunctionComponent<Props> = ({ application, mainApplicatio
|
|||||||
searchOptionsController={viewControllerManager.searchOptionsController}
|
searchOptionsController={viewControllerManager.searchOptionsController}
|
||||||
linkingController={viewControllerManager.linkingController}
|
linkingController={viewControllerManager.linkingController}
|
||||||
/>
|
/>
|
||||||
<NoteGroupView application={application} />
|
<ErrorBoundary>
|
||||||
|
<NoteGroupView application={application} />
|
||||||
|
</ErrorBoundary>
|
||||||
</FileDragNDropProvider>
|
</FileDragNDropProvider>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -9,10 +9,12 @@ import FilePlugin from './Plugins/EncryptedFilePlugin/FilePlugin'
|
|||||||
import BlockPickerMenuPlugin from './Plugins/BlockPickerPlugin/BlockPickerPlugin'
|
import BlockPickerMenuPlugin from './Plugins/BlockPickerPlugin/BlockPickerPlugin'
|
||||||
import { ErrorBoundary } from '@/Utils/ErrorBoundary'
|
import { ErrorBoundary } from '@/Utils/ErrorBoundary'
|
||||||
import { LinkingController } from '@/Controllers/LinkingController'
|
import { LinkingController } from '@/Controllers/LinkingController'
|
||||||
import LinkingControllerProvider from './Contexts/LinkingControllerProvider'
|
import LinkingControllerProvider from '../../Controllers/LinkingControllerProvider'
|
||||||
import { BubbleNode } from './Plugins/ItemBubblePlugin/Nodes/BubbleNode'
|
import { BubbleNode } from './Plugins/ItemBubblePlugin/Nodes/BubbleNode'
|
||||||
import ItemBubblePlugin from './Plugins/ItemBubblePlugin/ItemBubblePlugin'
|
import ItemBubblePlugin from './Plugins/ItemBubblePlugin/ItemBubblePlugin'
|
||||||
import { NodeObserverPlugin } from './Plugins/NodeObserverPlugin/NodeObserverPlugin'
|
import { NodeObserverPlugin } from './Plugins/NodeObserverPlugin/NodeObserverPlugin'
|
||||||
|
import { FilesController } from '@/Controllers/FilesController'
|
||||||
|
import FilesControllerProvider from '@/Controllers/FilesControllerProvider'
|
||||||
|
|
||||||
const StringEllipses = '...'
|
const StringEllipses = '...'
|
||||||
const NotePreviewCharLimit = 160
|
const NotePreviewCharLimit = 160
|
||||||
@@ -21,9 +23,10 @@ type Props = {
|
|||||||
application: WebApplication
|
application: WebApplication
|
||||||
note: SNNote
|
note: SNNote
|
||||||
linkingController: LinkingController
|
linkingController: LinkingController
|
||||||
|
filesController: FilesController
|
||||||
}
|
}
|
||||||
|
|
||||||
export const BlockEditor: FunctionComponent<Props> = ({ note, application, linkingController }) => {
|
export const BlockEditor: FunctionComponent<Props> = ({ note, application, linkingController, filesController }) => {
|
||||||
const controller = useRef(new BlockEditorController(note, application))
|
const controller = useRef(new BlockEditorController(note, application))
|
||||||
|
|
||||||
const handleChange = useCallback(
|
const handleChange = useCallback(
|
||||||
@@ -51,19 +54,21 @@ export const BlockEditor: FunctionComponent<Props> = ({ note, application, linki
|
|||||||
<div className="relative h-full w-full p-5">
|
<div className="relative h-full w-full p-5">
|
||||||
<ErrorBoundary>
|
<ErrorBoundary>
|
||||||
<LinkingControllerProvider controller={linkingController}>
|
<LinkingControllerProvider controller={linkingController}>
|
||||||
<BlocksEditorComposer initialValue={note.text} nodes={[FileNode, BubbleNode]}>
|
<FilesControllerProvider controller={filesController}>
|
||||||
<BlocksEditor
|
<BlocksEditorComposer initialValue={note.text} nodes={[FileNode, BubbleNode]}>
|
||||||
onChange={handleChange}
|
<BlocksEditor
|
||||||
className="relative relative resize-none text-base focus:shadow-none focus:outline-none"
|
onChange={handleChange}
|
||||||
>
|
className="relative relative resize-none text-base focus:shadow-none focus:outline-none"
|
||||||
<ItemSelectionPlugin currentNote={note} />
|
>
|
||||||
<FilePlugin />
|
<ItemSelectionPlugin currentNote={note} />
|
||||||
<ItemBubblePlugin />
|
<FilePlugin />
|
||||||
<BlockPickerMenuPlugin />
|
<ItemBubblePlugin />
|
||||||
<NodeObserverPlugin nodeType={BubbleNode} onRemove={handleBubbleRemove} />
|
<BlockPickerMenuPlugin />
|
||||||
<NodeObserverPlugin nodeType={FileNode} onRemove={handleBubbleRemove} />
|
<NodeObserverPlugin nodeType={BubbleNode} onRemove={handleBubbleRemove} />
|
||||||
</BlocksEditor>
|
<NodeObserverPlugin nodeType={FileNode} onRemove={handleBubbleRemove} />
|
||||||
</BlocksEditorComposer>
|
</BlocksEditor>
|
||||||
|
</BlocksEditorComposer>
|
||||||
|
</FilesControllerProvider>
|
||||||
</LinkingControllerProvider>
|
</LinkingControllerProvider>
|
||||||
</ErrorBoundary>
|
</ErrorBoundary>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6,9 +6,12 @@ import { FileNode } from './Nodes/FileNode'
|
|||||||
import { $createParagraphNode, $insertNodes, $isRootOrShadowRoot, COMMAND_PRIORITY_EDITOR } from 'lexical'
|
import { $createParagraphNode, $insertNodes, $isRootOrShadowRoot, COMMAND_PRIORITY_EDITOR } from 'lexical'
|
||||||
import { $createFileNode } from './Nodes/FileUtils'
|
import { $createFileNode } from './Nodes/FileUtils'
|
||||||
import { $wrapNodeInElement } from '@lexical/utils'
|
import { $wrapNodeInElement } from '@lexical/utils'
|
||||||
|
import { useFilesController } from '@/Controllers/FilesControllerProvider'
|
||||||
|
import { FilesControllerEvent } from '@/Controllers/FilesController'
|
||||||
|
|
||||||
export default function FilePlugin(): JSX.Element | null {
|
export default function FilePlugin(): JSX.Element | null {
|
||||||
const [editor] = useLexicalComposerContext()
|
const [editor] = useLexicalComposerContext()
|
||||||
|
const filesController = useFilesController()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!editor.hasNodes([FileNode])) {
|
if (!editor.hasNodes([FileNode])) {
|
||||||
@@ -19,7 +22,6 @@ export default function FilePlugin(): JSX.Element | null {
|
|||||||
INSERT_FILE_COMMAND,
|
INSERT_FILE_COMMAND,
|
||||||
(payload) => {
|
(payload) => {
|
||||||
const fileNode = $createFileNode(payload)
|
const fileNode = $createFileNode(payload)
|
||||||
// $insertNodeToNearestRoot(fileNode)
|
|
||||||
$insertNodes([fileNode])
|
$insertNodes([fileNode])
|
||||||
if ($isRootOrShadowRoot(fileNode.getParentOrThrow())) {
|
if ($isRootOrShadowRoot(fileNode.getParentOrThrow())) {
|
||||||
$wrapNodeInElement(fileNode, $createParagraphNode).selectEnd()
|
$wrapNodeInElement(fileNode, $createParagraphNode).selectEnd()
|
||||||
@@ -31,5 +33,16 @@ export default function FilePlugin(): JSX.Element | null {
|
|||||||
)
|
)
|
||||||
}, [editor])
|
}, [editor])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const disposer = filesController.addEventObserver((event, data) => {
|
||||||
|
if (event === FilesControllerEvent.FileUploadedToNote) {
|
||||||
|
const fileUuid = data[FilesControllerEvent.FileUploadedToNote].uuid
|
||||||
|
editor.dispatchCommand(INSERT_FILE_COMMAND, fileUuid)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return disposer
|
||||||
|
}, [filesController, editor])
|
||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { useCallback, useMemo } from 'react'
|
|||||||
import { useApplication } from '@/Components/ApplicationView/ApplicationProvider'
|
import { useApplication } from '@/Components/ApplicationView/ApplicationProvider'
|
||||||
import LinkedItemBubble from '@/Components/LinkedItems/LinkedItemBubble'
|
import LinkedItemBubble from '@/Components/LinkedItems/LinkedItemBubble'
|
||||||
import { createLinkFromItem } from '@/Utils/Items/Search/createLinkFromItem'
|
import { createLinkFromItem } from '@/Utils/Items/Search/createLinkFromItem'
|
||||||
import { useLinkingController } from '@/Components/BlockEditor/Contexts/LinkingControllerProvider'
|
import { useLinkingController } from '@/Controllers/LinkingControllerProvider'
|
||||||
import { LinkableItem } from '@/Utils/Items/Search/LinkableItem'
|
import { LinkableItem } from '@/Utils/Items/Search/LinkableItem'
|
||||||
import { useResponsiveAppPane } from '@/Components/ResponsivePane/ResponsivePaneProvider'
|
import { useResponsiveAppPane } from '@/Components/ResponsivePane/ResponsivePaneProvider'
|
||||||
import { LexicalNode } from 'lexical'
|
import { LexicalNode } from 'lexical'
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import { ContentType, SNNote } from '@standardnotes/snjs'
|
|||||||
import { getLinkingSearchResults } from '@/Utils/Items/Search/getSearchResults'
|
import { getLinkingSearchResults } from '@/Utils/Items/Search/getSearchResults'
|
||||||
import Popover from '@/Components/Popover/Popover'
|
import Popover from '@/Components/Popover/Popover'
|
||||||
import { INSERT_BUBBLE_COMMAND, INSERT_FILE_COMMAND } from '../Commands'
|
import { INSERT_BUBBLE_COMMAND, INSERT_FILE_COMMAND } from '../Commands'
|
||||||
import { useLinkingController } from '../../Contexts/LinkingControllerProvider'
|
import { useLinkingController } from '../../../../Controllers/LinkingControllerProvider'
|
||||||
import { PopoverClassNames } from '../ClassNames'
|
import { PopoverClassNames } from '../ClassNames'
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import ProtectedItemOverlay from '@/Components/ProtectedItemOverlay/ProtectedIte
|
|||||||
import { ElementIds } from '@/Constants/ElementIDs'
|
import { ElementIds } from '@/Constants/ElementIDs'
|
||||||
import { PrefDefaults } from '@/Constants/PrefDefaults'
|
import { PrefDefaults } from '@/Constants/PrefDefaults'
|
||||||
import { StringDeleteNote, STRING_DELETE_LOCKED_ATTEMPT, STRING_DELETE_PLACEHOLDER_ATTEMPT } from '@/Constants/Strings'
|
import { StringDeleteNote, STRING_DELETE_LOCKED_ATTEMPT, STRING_DELETE_PLACEHOLDER_ATTEMPT } from '@/Constants/Strings'
|
||||||
import { featureTrunkEnabled, FeatureTrunkName } from '@/FeatureTrunk'
|
|
||||||
import { log, LoggingDomain } from '@/Logging'
|
import { log, LoggingDomain } from '@/Logging'
|
||||||
import { debounce, isDesktopApplication, isMobileScreen } from '@/Utils'
|
import { debounce, isDesktopApplication, isMobileScreen } from '@/Utils'
|
||||||
import { classNames } from '@/Utils/ConcatenateClassNames'
|
import { classNames } from '@/Utils/ConcatenateClassNames'
|
||||||
@@ -996,7 +995,7 @@ class NoteView extends AbstractComponent<NoteViewProps, State> {
|
|||||||
const renderHeaderOptions = isMobileScreen() ? !this.state.plaintextEditorFocused : true
|
const renderHeaderOptions = isMobileScreen() ? !this.state.plaintextEditorFocused : true
|
||||||
|
|
||||||
const editorMode =
|
const editorMode =
|
||||||
featureTrunkEnabled(FeatureTrunkName.Blocks) && this.note.noteType === NoteType.Blocks
|
this.note.noteType === NoteType.Blocks
|
||||||
? 'blocks'
|
? 'blocks'
|
||||||
: this.state.editorStateDidLoad && !this.state.editorComponentViewer && !this.state.textareaUnloading
|
: this.state.editorStateDidLoad && !this.state.editorComponentViewer && !this.state.textareaUnloading
|
||||||
? 'plain'
|
? 'plain'
|
||||||
@@ -1155,6 +1154,7 @@ class NoteView extends AbstractComponent<NoteViewProps, State> {
|
|||||||
application={this.application}
|
application={this.application}
|
||||||
note={this.note}
|
note={this.note}
|
||||||
linkingController={this.viewControllerManager.linkingController}
|
linkingController={this.viewControllerManager.linkingController}
|
||||||
|
filesController={this.viewControllerManager.filesController}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
import { CrossControllerEvent } from '../CrossControllerEvent'
|
import { CrossControllerEvent } from '../CrossControllerEvent'
|
||||||
import { InternalEventBus, InternalEventPublishStrategy } from '@standardnotes/snjs'
|
import { InternalEventBus, InternalEventPublishStrategy, removeFromArray } from '@standardnotes/snjs'
|
||||||
import { WebApplication } from '../../Application/Application'
|
import { WebApplication } from '../../Application/Application'
|
||||||
import { Disposer } from '@/Types/Disposer'
|
import { Disposer } from '@/Types/Disposer'
|
||||||
|
|
||||||
export abstract class AbstractViewController {
|
type ControllerEventObserver<Event = void, EventData = void> = (event: Event, data: EventData) => void
|
||||||
|
|
||||||
|
export abstract class AbstractViewController<Event = void, EventData = void> {
|
||||||
dealloced = false
|
dealloced = false
|
||||||
protected disposers: Disposer[] = []
|
protected disposers: Disposer[] = []
|
||||||
|
private eventObservers: ControllerEventObserver<Event, EventData>[] = []
|
||||||
|
|
||||||
constructor(public application: WebApplication, protected eventBus: InternalEventBus) {}
|
constructor(public application: WebApplication, protected eventBus: InternalEventBus) {}
|
||||||
|
|
||||||
@@ -23,5 +26,19 @@ export abstract class AbstractViewController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
;(this.disposers as unknown) = undefined
|
;(this.disposers as unknown) = undefined
|
||||||
|
|
||||||
|
this.eventObservers.length = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
addEventObserver(observer: ControllerEventObserver<Event, EventData>): () => void {
|
||||||
|
this.eventObservers.push(observer)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
removeFromArray(this.eventObservers, observer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
notifyEvent(event: Event, data: EventData): void {
|
||||||
|
this.eventObservers.forEach((observer) => observer(event, data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,7 +34,17 @@ const NonMutatingFileActions = [PopoverFileItemActionType.DownloadFile, PopoverF
|
|||||||
|
|
||||||
type FileContextMenuLocation = { x: number; y: number }
|
type FileContextMenuLocation = { x: number; y: number }
|
||||||
|
|
||||||
export class FilesController extends AbstractViewController {
|
export type FilesControllerEventData = {
|
||||||
|
[FilesControllerEvent.FileUploadedToNote]: {
|
||||||
|
uuid: string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export enum FilesControllerEvent {
|
||||||
|
FileUploadedToNote,
|
||||||
|
}
|
||||||
|
|
||||||
|
export class FilesController extends AbstractViewController<FilesControllerEvent, FilesControllerEventData> {
|
||||||
allFiles: FileItem[] = []
|
allFiles: FileItem[] = []
|
||||||
attachedFiles: FileItem[] = []
|
attachedFiles: FileItem[] = []
|
||||||
showFileContextMenu = false
|
showFileContextMenu = false
|
||||||
@@ -388,6 +398,10 @@ export class FilesController extends AbstractViewController {
|
|||||||
type: ToastType.Success,
|
type: ToastType.Success,
|
||||||
message: `Uploaded file "${uploadedFile.name}"`,
|
message: `Uploaded file "${uploadedFile.name}"`,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
this.notifyEvent(FilesControllerEvent.FileUploadedToNote, {
|
||||||
|
[FilesControllerEvent.FileUploadedToNote]: { uuid: uploadedFile.uuid },
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return uploadedFiles
|
return uploadedFiles
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import { ReactNode, createContext, useContext, memo } from 'react'
|
||||||
|
import { observer } from 'mobx-react-lite'
|
||||||
|
import { FilesController } from '@/Controllers/FilesController'
|
||||||
|
|
||||||
|
const FilesControllerContext = createContext<FilesController | undefined>(undefined)
|
||||||
|
|
||||||
|
export const useFilesController = () => {
|
||||||
|
const value = useContext(FilesControllerContext)
|
||||||
|
|
||||||
|
if (!value) {
|
||||||
|
throw new Error('Component must be a child of <FilesControllerProvider />')
|
||||||
|
}
|
||||||
|
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
type ChildrenProps = {
|
||||||
|
children: ReactNode
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProviderProps = {
|
||||||
|
controller: FilesController
|
||||||
|
} & ChildrenProps
|
||||||
|
|
||||||
|
const MemoizedChildren = memo(({ children }: ChildrenProps) => <>{children}</>)
|
||||||
|
|
||||||
|
const FilesControllerProvider = ({ controller, children }: ProviderProps) => {
|
||||||
|
return (
|
||||||
|
<FilesControllerContext.Provider value={controller}>
|
||||||
|
<MemoizedChildren children={children} />
|
||||||
|
</FilesControllerContext.Provider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default observer(FilesControllerProvider)
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import { ReactNode, createContext, useContext, memo } from 'react'
|
import { ReactNode, createContext, useContext, memo } from 'react'
|
||||||
|
|
||||||
import { observer } from 'mobx-react-lite'
|
import { observer } from 'mobx-react-lite'
|
||||||
import { LinkingController } from '@/Controllers/LinkingController'
|
import { LinkingController } from '@/Controllers/LinkingController'
|
||||||
|
|
||||||
Reference in New Issue
Block a user