Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/insert media #3725

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/late-lies-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@udecode/plate-media': patch
---

`insertMedia`: Should insert in the current block if it is empty.
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