A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
feat: show overlay when dropping files
thecodrr992335f0a8a3
diff --git a/apps/web/src/components/editor/context.ts b/apps/web/src/components/editor/context.tsindex d1a4ee375..497902749 100644--- a/apps/web/src/components/editor/context.ts+++ b/apps/web/src/components/editor/context.ts@@ -1,4 +1,4 @@-import React, { useCallback, useContext } from "react";+import { useCallback } from "react";import { IEditor } from "./types";import createStore from "../../common/store";import BaseStore from "../../stores";diff --git a/apps/web/src/components/editor/index.tsx b/apps/web/src/components/editor/index.tsxindex 1cf0429d8..c2dc25f43 100644--- a/apps/web/src/components/editor/index.tsx+++ b/apps/web/src/components/editor/index.tsx@@ -1,4 +1,4 @@-import { useEffect, useCallback, useState } from "react";+import { useEffect, useCallback, useState, useRef } from "react";import { Box, Button, Flex, Text } from "rebass";import Properties from "../properties";import { useStore, store as editorstore } from "../../stores/editor-store";@@ -10,6 +10,10 @@ import { debounce, debounceWithId } from "../../utils/debounce";import { CharacterCounter } from "./types";import Tiptap from "./tiptap";import Header from "./header";+import { Attachment } from "../icons";+import { useEditorInstance } from "./context";+import { attachFile, insertAttachment } from "./plugins/picker";+import { DropEvent } from "react-dropzone";function updateWordCount(counter?: CharacterCounter) {AppEventManager.publish(@@ -48,6 +52,8 @@ export default function EditorManager({const isReadonly = useStore((store) => store.session.readonly || isPreviewMode);+ const [dropRef, overlayRef] = useDragOverlay();+ const editor = useEditorInstance();// TODO move this somewhere more appropriate// const init = useStore((store) => store.init);@@ -77,6 +83,7 @@ export default function EditorManager({return (<Flex+ ref={dropRef}flexDirection="column"id="editorContainer"flex={1}@@ -93,10 +100,101 @@ export default function EditorManager({onRequestFocus={() => toggleProperties(false)}/>{arePropertiesVisible && <Properties />}++ <Box+ ref={overlayRef}+ id="drag-overlay"+ sx={{+ position: "absolute",+ width: "100%",+ height: "100%",+ bg: "overlay",+ zIndex: 3,+ alignItems: "center",+ justifyContent: "center",+ display: "none",+ }}+ onDrop={async (e) => {+ if (!editor) return;+ for (let file of e.dataTransfer.files) {+ const result = await attachFile(file);+ if (!result) continue;+ editor.attachFile(result);+ }+ }}+ >+ <Flex+ sx={{+ border: "2px dashed var(--fontTertiary)",+ borderRadius: "default",+ p: 70,+ flexDirection: "column",+ pointerEvents: "none",+ }}+ >+ <Attachment size={72} />+ <Text variant={"heading"} sx={{ color: "icon", mt: 2 }}>+ Drop your files here to attach+ </Text>+ </Flex>+ </Box></Flex>);}+function useDragOverlay() {+ const dropElementRef = useRef<HTMLElement>();+ const overlayRef = useRef<HTMLElement>();++ useEffect(() => {+ const dropElement = dropElementRef.current;+ const overlay = overlayRef.current;++ if (!dropElement || !overlay) return;++ function isFile(e: DragEvent) {+ return (+ e.dataTransfer &&+ (e.dataTransfer.files.length > 0 ||+ e.dataTransfer.types.some((a) => a === "Files"))+ );+ }++ function showOverlay(e: DragEvent) {+ if (!overlay || !isFile(e)) return;++ overlay.style.display = "flex";+ }++ function hideOverlay(e: DragEvent | DropEvent) {+ if (!overlay) return;+ overlay.style.display = "none";+ }++ function allowDrag(e: DragEvent) {+ if (!e.dataTransfer || !isFile(e)) return;++ e.dataTransfer.dropEffect = "copy";+ e.preventDefault();+ }++ dropElement.addEventListener("dragenter", showOverlay);+ overlay.addEventListener("drop", hideOverlay);+ overlay.addEventListener("dragenter", allowDrag);+ overlay.addEventListener("dragover", allowDrag);+ overlay.addEventListener("dragleave", hideOverlay);+ return () => {+ dropElement.removeEventListener("dragenter", showOverlay);+ overlay.removeEventListener("drop", hideOverlay);+ overlay.removeEventListener("dragenter", allowDrag);+ overlay.removeEventListener("dragover", allowDrag);+ overlay.removeEventListener("dragleave", hideOverlay);+ };+ }, []);++ return [dropElementRef, overlayRef] as const;+}+type EditorProps = {readonly?: boolean;focusMode?: boolean;@@ -104,12 +202,9 @@ type EditorProps = {onRequestFocus?: () => void;};function Editor({ content, readonly, focusMode, onRequestFocus }: EditorProps) {+ const editor = useEditorInstance();useEffect(() => {- console.log("new contentn");- }, [content]);-- useEffect(() => {- console.log("rerender");+ console.log("Rerendering editor.");}, []);return (@@ -153,9 +248,17 @@ function Editor({ content, readonly, focusMode, onRequestFocus }: EditorProps) {debouncedOnEditorChange(sessionId, id, sessionId, content);if (counter) debouncedUpdateWordCount(counter);}}+ onInsertAttachment={(type) => {+ const mime = type === "file" ? "*/*" : "image/*";+ insertAttachment(mime).then((file) => {+ if (!file) return;+ editor?.attachFile(file);+ });+ }}/></Flex></FlexScrollContainer>+{/* TODO <Boxid="editorToolbar"sx={{diff --git a/apps/web/src/components/editor/plugins/picker.ts b/apps/web/src/components/editor/plugins/picker.tsindex d72e1babe..95b182aa8 100644--- a/apps/web/src/components/editor/plugins/picker.ts+++ b/apps/web/src/components/editor/plugins/picker.ts@@ -1,6 +1,5 @@import { SerializedKey } from "@notesnook/crypto/dist/src/types";import Compressor from "compressorjs";-import type { Editor, TinyMCE } from "tinymce";import { AppEventManager, AppEvents } from "../../../common/app-events";import { db } from "../../../common/db";import { showBuyDialog } from "../../../common/dialog-controller";@@ -159,6 +158,14 @@ export type AttachmentProgress = {loaded: number;};+export type Attachment = {+ hash: string;+ filename: string;+ type: string;+ size: number;+ dataurl?: string;+};+type AddAttachmentOptions = {expectedFileHash?: string;showProgress?: boolean;@@ -169,7 +176,7 @@ async function addAttachment(file: File,dataurl: string | undefined,options: AddAttachmentOptions = {}-) {+): Promise<Attachment> {const { expectedFileHash, forceWrite, showProgress = true } = options;const action = async () => {diff --git a/apps/web/src/components/editor/tiptap.tsx b/apps/web/src/components/editor/tiptap.tsxindex 1af3efa9a..9a8608811 100644--- a/apps/web/src/components/editor/tiptap.tsx+++ b/apps/web/src/components/editor/tiptap.tsx@@ -5,9 +5,9 @@ import { EditorContent } from "@tiptap/react";import { Flex } from "rebass";import { Editor } from "@tiptap/core";import "notesnook-editor/dist/styles.css";-import React, { PropsWithChildren, useEffect, useRef } from "react";+import { PropsWithChildren, useEffect, useRef } from "react";import useMobile from "../../utils/use-mobile";-import { AttachmentProgress, insertAttachment } from "./plugins/picker";+import { Attachment, AttachmentProgress } from "./plugins/picker";import { AppEventManager, AppEvents } from "../../common/app-events";import { EV, EVENTS } from "notes-core/common";import { CharacterCounter, IEditor } from "./types";@@ -16,6 +16,7 @@ import { createPortal } from "react-dom";type TipTapProps = {onChange?: (content: string, counter?: CharacterCounter) => void;+ onInsertAttachment?: (type: string) => void;onFocus?: () => void;content?: string;toolbarContainerId?: string;@@ -25,6 +26,7 @@ type TipTapProps = {function TipTap(props: TipTapProps) {const {onChange,+ onInsertAttachment,onFocus = () => {},content,toolbarContainerId,@@ -45,7 +47,6 @@ function TipTap(props: TipTapProps) {onFocus,onCreate: ({ editor }) => {counter.current = editor.storage.characterCount as CharacterCounter;- console.log("Init!");configure({editor: toIEditor(editor),canRedo: editor.can().redo(),@@ -56,7 +57,6 @@ function TipTap(props: TipTapProps) {if (onChange) onChange(editor.getHTML(), counter.current);},onDestroy: () => {- console.log("Destory!");configure({ editor: undefined });},onTransaction: ({ editor }) => {@@ -66,22 +66,8 @@ function TipTap(props: TipTapProps) {});},theme,- onOpenAttachmentPicker: (editor, type) => {- if (type === "file") {- insertAttachment().then((file) => {- if (!file) return;- setTimeout(() => {- editor.commands.insertAttachment(file);- });- });- } else if (type === "image") {- insertAttachment("image/*").then((image) => {- setTimeout(() => {- if (!image || !image.dataurl) return;- editor.commands.insertImage({ ...image, src: image.dataurl });- });- });- }+ onOpenAttachmentPicker: (_editor, type) => {+ onInsertAttachment?.(type);return true;},},@@ -147,7 +133,11 @@ function TipTap(props: TipTapProps) {/></Portal><EditorContent- style={{ flex: 1, cursor: "text", color: theme.colors.text }}+ style={{+ flex: 1,+ cursor: "text",+ color: theme.colors.text,+ }}editor={editor}/></Flex>@@ -183,5 +173,10 @@ function toIEditor(editor: Editor): IEditor {focus: () => editor.commands.focus("start"),undo: () => editor.commands.undo(),redo: () => editor.commands.redo(),+ attachFile: (file: Attachment) => {+ if (file.dataurl) {+ editor.commands.insertImage({ ...file, src: file.dataurl });+ } else editor.commands.insertAttachment(file);+ },};}diff --git a/apps/web/src/components/editor/types.ts b/apps/web/src/components/editor/types.tsindex c29685a89..9512e39a5 100644--- a/apps/web/src/components/editor/types.ts+++ b/apps/web/src/components/editor/types.ts@@ -1,4 +1,4 @@-import { HTMLContent } from "@tiptap/react";+import { Attachment } from "./plugins/picker";export type CharacterCounter = {words: () => number;@@ -9,4 +9,5 @@ export interface IEditor {focus: () => void;undo: () => void;redo: () => void;+ attachFile: (file: Attachment) => void;}