Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Nov 5, 2024
1 parent d0720cf commit d144107
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
type props = {
type: string;
url: string;
id?: string;
initialHeight?: number;
initialWidth?: number;
isUpload?: boolean;
Expand Down
28 changes: 24 additions & 4 deletions packages/media/src/react/placeholder/PlaceholderPlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { type ExtendConfig, bindFirst } from '@udecode/plate-common';
import {
type ExtendConfig,
bindFirst,
getAncestorNode,
getNodeString,
} from '@udecode/plate-common';
import { findEventRange, toTPlatePlugin } from '@udecode/plate-common/react';

import type { AllowedFileType } from './internal/mimes';
Expand Down Expand Up @@ -112,7 +117,6 @@ export const PlaceholderPlugin = toTPlatePlugin<
handlers: {
onDrop: ({ editor, event, tf }) => {
// using DnD plugin by default
if (!getOption('disabledDndPlugin')) return;

const { files } = event.dataTransfer;

Expand All @@ -121,6 +125,9 @@ export const PlaceholderPlugin = toTPlatePlugin<
/** Without this, the dropped file replaces the page */
event.preventDefault();
event.stopPropagation();

if (!getOption('disabledDndPlugin')) return;

/**
* When we drop a file, the selection won't move automatically to the
* drop location. Find the location from the event and upload the files
Expand All @@ -134,15 +141,28 @@ export const PlaceholderPlugin = toTPlatePlugin<

return true;
},
onPaste: ({ event, tf }) => {
onPaste: ({ editor, event, tf }) => {
const { files } = event.clipboardData;

if (files.length === 0) return false;

event.preventDefault();
event.stopPropagation();

tf.insert.media(files);
let inserted = false;
const ancestor = getAncestorNode(editor);

if (ancestor) {
const [node, path] = ancestor;

if (getNodeString(node).length === 0) {
tf.insert.media(files, path);
inserted = true;
}
}
if (!inserted) {
tf.insert.media(files);
}

return true;
},
Expand Down

0 comments on commit d144107

Please sign in to comment.