From 5f773ec98885e20ee344d67ee71df662cb33fdf5 Mon Sep 17 00:00:00 2001 From: fantasticit Date: Thu, 14 Jul 2022 21:49:39 +0800 Subject: [PATCH] tiptap: improve drag --- .../src/tiptap/core/extensions/dragable.ts | 22 ++++++++++++++----- .../tiptap/core/wrappers/attachment/index.tsx | 7 +++++- .../tiptap/core/wrappers/callout/index.tsx | 8 ++++++- .../tiptap/core/wrappers/code-block/index.tsx | 3 ++- .../tiptap/core/wrappers/countdown/index.tsx | 7 +++--- .../core/wrappers/document-children/index.tsx | 2 ++ .../wrappers/document-reference/index.tsx | 8 ++++++- .../core/wrappers/dragable/index.module.scss | 2 +- .../tiptap/core/wrappers/dragable/index.tsx | 7 ++++-- .../src/tiptap/core/wrappers/flow/index.tsx | 6 ++++- .../src/tiptap/core/wrappers/iframe/index.tsx | 3 ++- .../src/tiptap/core/wrappers/image/index.tsx | 3 ++- .../src/tiptap/core/wrappers/katex/index.tsx | 2 ++ .../src/tiptap/core/wrappers/mind/index.tsx | 6 ++++- 14 files changed, 67 insertions(+), 19 deletions(-) diff --git a/packages/client/src/tiptap/core/extensions/dragable.ts b/packages/client/src/tiptap/core/extensions/dragable.ts index d5e94092..460d1026 100644 --- a/packages/client/src/tiptap/core/extensions/dragable.ts +++ b/packages/client/src/tiptap/core/extensions/dragable.ts @@ -28,6 +28,7 @@ export const Dragable = Extension.create({ name: 'dragable', addProseMirrorPlugins() { + let scrollContainer; let dropElement; let currentNode; let editorView; @@ -54,6 +55,11 @@ export const Dragable = Extension.create({ editorView.dragging = { slice, move: true }; } + function onScroll() { + if (!dropElement) return; + dropElement.style.opacity = 0; + } + return [ new Plugin({ view(view) { @@ -64,6 +70,12 @@ export const Dragable = Extension.create({ dropElement.className = 'drag-handler'; dropElement.addEventListener('dragstart', drag); view.dom.parentElement.appendChild(dropElement); + + scrollContainer = view.dom.parentElement.parentElement?.parentElement?.parentElement; + + if (scrollContainer) { + scrollContainer.addEventListener('scroll', onScroll); + } } return { @@ -75,6 +87,10 @@ export const Dragable = Extension.create({ dropElement.removeEventListener('dragstart', drag); dropElement.parentNode.removeChild(dropElement); } + + if (scrollContainer) { + scrollContainer.removeEventListener('scroll', onScroll); + } }, }; }, @@ -91,7 +107,7 @@ export const Dragable = Extension.create({ } }, 50); }, - mousemove(view, event) { + mousedown(view, event) { if (!dropElement) return; const coords = { left: event.clientX, top: event.clientY }; @@ -134,10 +150,6 @@ export const Dragable = Extension.create({ dropElement.style.top = rect.top + 6 + 'px'; dropElement.style.opacity = 1; }, - mouseleave() { - if (!dropElement || currentNode) return; - dropElement.style.opacity = 0; - }, }, }, }), diff --git a/packages/client/src/tiptap/core/wrappers/attachment/index.tsx b/packages/client/src/tiptap/core/wrappers/attachment/index.tsx index 50687a57..e16293e1 100644 --- a/packages/client/src/tiptap/core/wrappers/attachment/index.tsx +++ b/packages/client/src/tiptap/core/wrappers/attachment/index.tsx @@ -6,6 +6,7 @@ import { Tooltip } from 'components/tooltip'; import { useToggle } from 'hooks/use-toggle'; import { useCallback, useEffect, useRef, useState } from 'react'; import { uploadFile } from 'services/file'; +import { Attachment } from 'tiptap/core/extensions/attachment'; import { DragableWrapper } from 'tiptap/core/wrappers/dragable'; import { download, extractFileExtension, extractFilename, normalizeFileSize } from 'tiptap/prose-utils'; @@ -154,5 +155,9 @@ export const AttachmentWrapper = ({ editor, node, updateAttributes }) => { } })(); - return {content}; + return ( + + {content} + + ); }; diff --git a/packages/client/src/tiptap/core/wrappers/callout/index.tsx b/packages/client/src/tiptap/core/wrappers/callout/index.tsx index f9d03715..bea85295 100644 --- a/packages/client/src/tiptap/core/wrappers/callout/index.tsx +++ b/packages/client/src/tiptap/core/wrappers/callout/index.tsx @@ -4,6 +4,7 @@ import { EmojiPicker } from 'components/emoji-picker'; import { convertColorToRGBA } from 'helpers/color'; import { Theme, ThemeEnum } from 'hooks/use-theme'; import { useCallback, useMemo } from 'react'; +import { Callout } from 'tiptap/core/extensions/callout'; import { DragableWrapper } from 'tiptap/core/wrappers/dragable'; import styles from './index.module.scss'; @@ -26,7 +27,12 @@ export const CalloutWrapper = ({ editor, node, updateAttributes }) => { ); return ( - +
(); return ( - +